-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.v
441 lines (387 loc) · 12.2 KB
/
cpu.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
`default_nettype none
`include "alu.v"
`include "cmp.v"
`include "muldiv.v"
`include "regfile.v"
module cpu(
input wire clk, interrupt,
// data bus connections
output reg [15:0] dbus_addr,
output reg [31:0] dbus_write,
input wire [31:0] dbus_read,
output reg dbus_wen, dbus_ren,
// instruction bus connections
output reg [15:0] ibus_addr,
input wire [31:0] ibus_read
);
localparam [31:0] NOP_INSTR = 32'h00000000;
// TODO for now always direct to 0
localparam [31:0] mtvec = 32'h0;
// used to mark whether a jump or branch should be taken and where
reg branch_or_jump;
reg [31:0] branch_jump_addr;
// combines control flow changes from branches/jumps and traps
wire change_control;
reg [31:0] new_addr;
// these are used to break the flow from any stage in the pipeline
// this differentiation is needed as every stage before but not after
// must be flushed along with the pc change
reg break_fetch, break_decode, break_exec, break_wb;
reg break_fetch_latch;
reg [31:0] baddr_fetch, baddr_decode, baddr_exec, baddr_wb;
// represent the flow of instruction data along the pipeline
reg [31:0] ir_exec, ir_wb;
reg [31:0] pc_fetch, pc_decode, pc_exec;
// break up the instruction into its components for the exec and wb stages
wire [6:0] opcode_exec, opcode_wb, funct7;
wire [4:0] rd, rs1, rs2;
wire [2:0] funct3;
wire [31:0] immI, immS, immB, immU, immJ;
// used to select either regfile as input to alu and cmp unit
// or if they need to be bypassed from a reg write that is yet to happen
reg [31:0] s1, s2;
// alu buses
reg [31:0] alu_a, alu_b;
wire [31:0] alu_q;
reg [2:0] alu_mode;
// cmp buses
reg [31:0] cmp_a, cmp_b;
wire cmp_q;
reg [2:0] cmp_mode;
// muldiv buses
wire [31:0] muldiv_q;
// execute output bus, used for input to buffer reg in write back
reg [31:0] exec_out;
// reg buses
reg [31:0] reg_d;
wire [31:0] reg_s1, reg_s2;
// selects always come from the instruction
reg reg_wen;
initial pc_fetch = 0;
// these are used in the execution stage
assign opcode_exec = ir_exec[6:0];
assign rs1 = ir_exec[19:15];
assign rs2 = ir_exec[24:20];
assign funct3 = ir_exec[14:12];
assign funct7 = ir_exec[31:25];
// these are used in the write back phase
assign opcode_wb = ir_wb[6:0];
assign rd = ir_wb[11:7];
// construct all possible immediates, only needed in execute
assign immI = {{21{ir_exec[31]}}, ir_exec[30:20]};
assign immS = {{21{ir_exec[31]}}, ir_exec[30:25], ir_exec[11:7]};
assign immB = {{20{ir_exec[31]}}, ir_exec[7], ir_exec[30:25], ir_exec[11:8], 1'b0};
assign immU = {ir_exec[31:12], 12'b0};
assign immJ = {{12{ir_exec[31]}}, ir_exec[19:12], ir_exec[20], ir_exec[30:21], 1'b0};
regfile regfile (
.d(reg_d),
.s1(reg_s1),
.s2(reg_s2),
.s1sel(rs1),
.s2sel(rs2),
.dsel(rd),
.wen(reg_wen),
.clk(clk)
);
assign change_control = interrupt || break_fetch || break_decode || break_exec || break_wb;
always @(*) begin
if (interrupt) begin
new_addr <= {mtvec[31:2], 2'b00};
end else if (break_wb) begin
new_addr <= baddr_wb;
end else if (break_exec) begin
new_addr <= baddr_exec;
end else if (break_decode) begin
new_addr <= baddr_decode;
end else begin // if (break_fetch)
new_addr <= baddr_fetch;
end
end
/* FETCH
* - current pc_fetch is always fed into ibus_addr, so after clock
* - ibus_read will have the instruction ready
* - at the same time it either increments pc_fetch, or changes it
* to new one from branch/jump/vector table
*/
// increment or branch
always @(posedge clk) begin
if (change_control) begin
pc_fetch <= new_addr;
end else begin
pc_fetch <= pc_fetch + 4;
end
end
always @(*) begin
ibus_addr <= pc_fetch;
end
// TODO, just always set to never breaking from here, will need to later for interrupts
initial break_fetch = 0;
initial baddr_fetch = 0;
// can't stop the passing of rom into ir_exec directly
// so instead latch a value so break_decode won't pass on instruction
// on the next cycle
always @(posedge clk) begin
if (break_fetch || break_decode || break_exec || break_wb) begin
break_fetch_latch <= 1;
end else begin
break_fetch_latch <= 0;
end
end
// pass pc down pipeline
always @(posedge clk) begin
pc_decode <= pc_fetch;
end
/* DECODE
* - ibus connected to single cycle memory so ibus_read
* will always have the next instruction by now
* - need to decide whether to pass it on or whether flow is being
* changed by jump/branch or trap
*/
// TODO exceptions not generated in this stage yet
initial break_decode = 0;
initial baddr_decode = 0;
// decide to pass down ir or flush pipeline
always @(posedge clk) begin
if (break_fetch_latch || break_decode || break_exec || break_wb) begin
ir_exec <= NOP_INSTR;
end else begin
ir_exec <= ibus_read;
end
end
// pass pc down the pipeline
always @(posedge clk) begin
pc_exec <= pc_decode;
end
/* EXECUTE */
alu alu (
.a(alu_a),
.b(alu_b),
.q(alu_q),
.mode(alu_mode)
);
cmp cmp (
.a(cmp_a),
.b(cmp_b),
.q(cmp_q),
.mode(cmp_mode)
);
muldiv muldiv (
.a(reg_s1),
.b(reg_s2),
.q(muldiv_q),
.mode(funct3)
);
// decide if a bypass from the current write back stage is needed
always @(*) begin
if (reg_wen && (rs1 == rd)) begin
s1 <= reg_d;
end else begin
s1 <= reg_s1;
end
if (reg_wen && (rs2 == rd)) begin
s2 <= reg_d;
end else begin
s2 <= reg_s2;
end
end
// functions handled by the alu
always @(*) begin
case (opcode_exec)
// math reg
7'b0110011: begin
alu_a <= s1;
alu_b <= s2;
// TODO likely a critical path, so far this tests the fastest
if (funct3 == 3'b000) begin
// if (funct7[5]) begin
// alu_mode <= 3'b010;
// end else begin
// alu_mode <= funct3;
// end
alu_mode <= funct3 | {1'b0, funct7[5], 1'b0};
end else if (funct3 == 3'b101) begin
if (funct7[5]) begin
alu_mode <= 3'b011;
end else begin
alu_mode <= funct3;
end
// alu_mode <= {~funct7[5], funct7[5], funct3[0]};
end else begin
alu_mode <= funct3;
end
end
// math imm
7'b0010011: begin
alu_a <= s1;
alu_b <= immI;
if (funct3 == 3'b101) begin
if (funct7[5]) begin
alu_mode <= 3'b011;
end else begin
alu_mode <= funct3;
end
// alu_mode <= {funct3[2:1] ^ {2{funct7[5]}}, funct3[0]};
end else begin
alu_mode <= funct3;
end
end
// loads
7'b0000011: begin
alu_a <= s1;
alu_b <= immI;
alu_mode <= 3'b000;
end
// stores
7'b0100011: begin
alu_a <= s1;
alu_b <= immS;
alu_mode <= 3'b000;
end
// branches
7'b1100011: begin
alu_a <= pc_exec;
alu_b <= immB;
alu_mode <= 3'b000;
end
// LUI
7'b0110111: begin
alu_a <= s1;
alu_b <= immU;
alu_mode <= 3'b000;
end
// AUIPC
7'b0010111: begin
alu_a <= pc_exec;
alu_b <= immU;
alu_mode <= 3'b000;
end
// JAL
7'b1101111: begin
alu_a <= pc_exec;
alu_b <= immJ;
alu_mode <= 3'b000;
end
// JALR
// 7'b1100111
// default to ensure no latch generation
default: begin
alu_a <= s1;
alu_b <= immI;
alu_mode <= 3'b000;
end
endcase
end
// functions handled by the comparison unit
always @(*) begin
cmp_mode <= funct3;
cmp_a <= s1;
case (opcode_exec)
// imm compares
7'b0010011: begin
cmp_b <= immI;
end
// branches and reg compares
// only 7'b1100011, 7'b0110011, default ensures no latch generates
default: begin
cmp_b <= s2;
end
endcase
end
// decide if a change of control needs to be taken
// from a branch or jump
always @(*) begin
case (opcode_exec)
7'b1101111, 7'b1100111: begin
branch_or_jump <= 1;
end
7'b1100011: begin
branch_or_jump <= cmp_q;
end
default: begin
branch_or_jump <= 0;
end
endcase
branch_jump_addr <= alu_q;
end
// decide what to clock into the buffer register
always @(posedge clk) begin
casex (opcode_exec)
// ALU and CMP ops
7'b0?10011: begin
casex (funct3)
3'b01?: exec_out <= cmp_q;
default: exec_out <= alu_q;
endcase
end
// MULDIV extension
7'b0110011: begin
exec_out <= muldiv_q;
end
// LUI bypass
7'b0110111: begin
exec_out <= immU;
end
// every other supported instruction uses alu
default: begin
exec_out <= alu_q;
end
endcase
end
// dealing with what to pass to ram
always @(*) begin
// data line will only ever be from rs2
dbus_write <= s2;
// address from alu, result of rs1 + imm
dbus_addr <= alu_q;
// decide if this is a write or a read / nothing
case (opcode_exec)
7'b0100011: dbus_wen <= 1;
default: dbus_wen <= 0;
endcase
end
// decide to break flow whether from branch/jump or exception
always @(*) begin
// TODO add execption as if, then jumps in following else if
if (branch_or_jump) begin
break_exec <= 1;
baddr_exec <= branch_jump_addr;
end else begin
break_exec <= 0;
baddr_exec <= 0;
end
end
// decide to pass down ir or flush pipeline
always @(posedge clk) begin
if (break_exec || break_wb) begin
ir_wb <= NOP_INSTR;
end else begin
ir_wb <= ir_exec;
end
end
/* WRITE BACK */
always @(*) begin
case (opcode_wb)
7'b0010011, 7'b0110011, 7'b0110111, 7'b0010111: begin
reg_wen <= 1;
reg_d <= exec_out;
end
7'b0000011: begin
reg_wen <= 1;
reg_d <= dbus_read;
end
7'b1101111, 7'b1100111: begin
reg_wen <= 1;
// this will already have been incremented to the next instr
reg_d <= pc_exec;
end
default: begin
// need default reg_d output to avoid latch generation
reg_d <= exec_out;
reg_wen <= 0;
end
endcase
end
// TODO exceptions not generated in this stage yet
initial break_wb = 0;
initial baddr_wb = 0;
endmodule