-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.sv
507 lines (425 loc) · 11.3 KB
/
processor.sv
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
module processor
(
input logic clk,
input logic rst,
input logic timer_interrupt
);
// wires
// pc_out
logic [31:0] pc_out_IF;
logic [31:0] pc_out_DE;
logic [31:0] pc_out_MW;
logic [31:0] new_pc;
// inst
logic [31:0] inst_IF;
logic [31:0] inst_DE;
logic [31:0] inst_MW;
logic [ 4:0] waddr;
// rs1
logic [ 4:0] rs1_DE;
logic [ 4:0] rs1_MW;
// rs2
logic [ 4:0] rs2_DE;
logic [ 4:0] rs2_MW;
// rd
logic [ 4:0] rd_DE;
logic [ 4:0] rd_MW;
logic [ 6:0] opcode;
logic [ 2:0] funct3;
logic [ 6:0] funct7;
// rdata1
logic [31:0] rdata1_DE;
logic [31:0] rdata1_MW;
// rdata2
logic [31:0] rdata2_DE;
logic [31:0] rdata2_MW;
logic [31:0] opr_a;
logic [31:0] opr_b;
// opr_res
logic [31:0] opr_res_IF;
logic [31:0] opr_res_DE;
logic [31:0] opr_res_MW;
// imm_val
logic [31:0] imm_val_DE;
logic [31:0] imm_val_MW;
// wdata
logic [31:0] wdata_DE;
logic [31:0] wdata_MW;
logic [31:0] rdata;
logic br_taken;
logic [3 :0] aluop;
// rf_en
logic rf_en_DE;
logic rf_en_MW;
logic sel_a;
logic sel_b;
// rd_en
logic rd_en_DE;
logic rd_en_MW;
// wr_en
logic wr_en_DE;
logic wr_en_MW;
// wb_sel
logic [ 1:0] wb_sel_DE;
logic [ 1:0] wb_sel_MW;
// mem_acc_mode
logic [ 2:0] mem_acc_mode_DE;
logic [ 2:0] mem_acc_mode_MW;
logic [ 2:0] br_type;
// br_take
logic br_take_IF;
logic br_take_DE;
// csr_rd
logic csr_rd_DE;
logic csr_rd_MW;
// csr_wr
logic csr_wr_DE;
logic csr_wr_MW;
// is_mret
logic is_mret_DE;
logic is_mret_MW;
logic [31:0] csr_rdata;
// epc
logic [31:0] epc_IF;
logic [31:0] epc_MW;
// epc_taken
logic epc_taken_IF;
logic epc_taken_MW;
logic [31:0] epc_pc;
logic [31:0] forward_opr_a;
logic [31:0] forward_opr_b;
logic forward_a;
logic forward_b;
logic stall_IF;
logic flush_DE;
// --------------------- Instruction Fetch (IF) ---------------------
// PC MUX
mux_2x1 mux_2x1_pc
(
// inputs
.in_0 ( pc_out_IF + 32'd4 ),
.in_1 ( opr_res_IF ),
.select_line ( br_take_IF ),
// outputs
.out ( new_pc )
);
mux_2x1 mux_2x1_epc
(
// inputs
.in_0 ( new_pc ),
.in_1 ( epc_IF ),
.select_line ( epc_taken_IF ),
// outputs
.out ( epc_pc )
);
// program counter
pc pc_i
(
// inputs
.clk ( clk ),
.rst ( rst ),
.en ( ~stall_IF ),
.pc_in ( epc_pc ),
//outputs
.pc_out( pc_out_IF )
);
// instruction memory
inst_mem inst_mem_i
(
// inputs
.addr ( pc_out_IF ),
// outputs
.data ( inst_IF )
);
// ---------------------------------------------------------------
// IF <-> DE Buffer
always_ff @( posedge clk )
begin
if ( rst )
begin
pc_out_DE <= 0;
inst_DE <= 0;
end
else if ( flush_DE )
begin
inst_DE <= 32'h00000013;
pc_out_DE <= 0;
end
else
begin
pc_out_DE <= pc_out_IF; // PC
inst_DE <= inst_IF; // instruction
end
end
// --------------------- Decode-Execute (DE) ---------------------
// instruction decoder
inst_dec inst_dec_i
(
// inputs
.inst ( inst_DE ),
// outputs
.rs1 ( rs1_DE ),
.rs2 ( rs2_DE ),
.rd ( rd_DE ),
.opcode( opcode ),
.funct3( funct3 ),
.funct7( funct7 )
);
// register file
reg_file reg_file_i
(
// inputs
.clk ( clk ),
.rf_en ( rf_en_MW ),
.rs1 ( rs1_DE ),
.rs2 ( rs2_DE ),
.rd ( waddr ),
.wdata ( wdata_DE ),
// outputs
.rdata1( rdata1_DE ),
.rdata2( rdata2_DE )
);
// controller
controller controller_i
(
// inputs
.opcode ( opcode ),
.funct3 ( funct3 ),
.funct7 ( funct7 ),
.br_taken ( br_taken ),
// outputs
.aluop ( aluop ),
.rf_en ( rf_en_DE ),
.sel_a ( sel_a ),
.sel_b ( sel_b ),
.rd_en ( rd_en_DE ),
.wr_en ( wr_en_DE ),
.wb_sel ( wb_sel_DE ),
.mem_acc_mode ( mem_acc_mode_DE ),
.br_type ( br_type ),
.br_take ( br_take_DE ),
.csr_rd ( csr_rd_DE ),
.csr_wr ( csr_wr_DE ),
.is_mret ( is_mret_DE )
);
// immediate generator
imm_gen imm_gen_i
(
// inputs
.inst ( inst_DE ),
// outputs
.imm_val( imm_val_DE )
);
// forward_a
always_comb
begin
if (forward_a)
begin
if (rf_en_MW)
begin
forward_opr_a = opr_res_MW;
end
else
begin
forward_opr_a = 0;
end
end
else
begin
forward_opr_a = rdata1_DE;
end
end
// forward_b
always_comb
begin
if (forward_b)
begin
if (rf_en_MW)
begin
forward_opr_b = opr_res_MW;
end
else
begin
forward_opr_b = 0;
end
end
else
begin
forward_opr_b = rdata2_DE;
end
end
// ALU opr_a MUX
mux_2x1 mux_2x1_alu_opr_a
(
// inputs
.in_0 ( pc_out_DE ),
.in_1 ( forward_opr_a ),
.select_line ( sel_a ),
// outputs
.out ( opr_a )
);
// ALU opr_b MUX
mux_2x1 mux_2x1_alu_opr_b
(
// inputs
.in_0 ( forward_opr_b ),
.in_1 ( imm_val_DE ),
.select_line ( sel_b ),
// outputs
.out ( opr_b )
);
// ALU
alu alu_i
(
// inputs
.aluop ( aluop ),
.opr_a ( opr_a ),
.opr_b ( opr_b ),
// outputs
.opr_res ( opr_res_DE )
);
// br_cond
br_cond br_cond_i
(
// inputs
.rdata1 ( rdata1_DE ),
.rdata2 ( rdata2_DE ),
.br_type ( br_type ),
// outputs
.br_taken ( br_taken )
);
// ---------------------------------------------------------------
// Feedback to IF stage
always_comb
begin
br_take_IF = br_take_DE;
opr_res_IF = opr_res_DE;
end
// DE <-> MEM-WB Buffer
always_ff @ ( posedge clk )
begin
if ( rst )
begin
pc_out_MW <= 0;
inst_MW <= 0;
opr_res_MW <= 0;
rdata1_MW <= 0;
rdata2_MW <= 0;
imm_val_MW <= 0;
rs1_MW <= 0;
rs2_MW <= 0;
rd_MW <= 0;
// control signals
rf_en_MW <= 0;
rd_en_MW <= 0;
wr_en_MW <= 0;
mem_acc_mode_MW <= 0;
csr_rd_MW <= 0;
csr_wr_MW <= 0;
is_mret_MW <= 0;
wb_sel_MW <= 0;
end
else
begin
pc_out_MW <= pc_out_DE;
inst_MW <= inst_DE;
opr_res_MW <= opr_res_DE;
rdata1_MW <= rdata1_DE;
rdata2_MW <= rdata2_DE;
imm_val_MW <= imm_val_DE;
rs1_MW <= rs1_DE;
rs2_MW <= rs2_DE;
rd_MW <= rd_DE;
// control signals
rf_en_MW <= rf_en_DE;
rd_en_MW <= rd_en_DE;
wr_en_MW <= wr_en_DE;
mem_acc_mode_MW <= mem_acc_mode_DE;
csr_rd_MW <= csr_rd_DE;
csr_wr_MW <= csr_wr_DE;
is_mret_MW <= is_mret_DE;
wb_sel_MW <= wb_sel_DE;
end
end
// ----------------------- Memory-Writeback ----------------------
// data memory
data_mem data_mem_i
(
// inputs
.clk ( clk ),
.rd_en ( rd_en_MW ),
.wr_en ( wr_en_MW ),
.addr ( opr_res_MW ),
.mem_acc_mode ( mem_acc_mode_MW ),
.wdata ( rdata2_MW ),
// outputs
.rdata ( rdata )
);
// csr
csr_reg csr_reg_i
(
// inputs
.clk ( clk ),
.rst ( rst ),
.addr ( imm_val_MW ),
.wdata ( rdata1_MW ),
.pc ( pc_out_MW ),
.trap ( timer_interrupt ),
.csr_rd ( csr_rd_MW ),
.csr_wr ( csr_wr_MW ),
.is_mret ( is_mret_MW ),
.inst ( inst_MW ),
// outputs
.rdata ( csr_rdata ),
.epc ( epc_MW ),
.epc_taken ( epc_taken_MW )
);
// Writeback MUX
mux_4x1 wb_mux
(
// inputs
.in_0 ( pc_out_MW + 32'd4 ),
.in_1 ( opr_res_MW ),
.in_2 ( rdata ),
.in_3 ( csr_rdata ),
.select_line ( wb_sel_MW ),
// outputs
.out ( wdata_MW )
);
// ---------------------------------------------------------------
// Feedback to IF stage
always_comb
begin
epc_IF = epc_MW;
epc_taken_IF = epc_taken_MW;
end
// Feedback to DE stage
always_comb
begin
waddr = inst_MW[11:7];
wdata_DE = wdata_MW;
end
// ---------------------------------------------------------------
// ------------------------- Hazard Unit -------------------------
hazard_unit hazard_unit_i
(
// FORWARDING
// inputs
.rs1_DE ( rs1_DE ),
.rs2_DE ( rs2_DE ),
.rd_MW ( rd_MW ),
.rf_en_MW ( rf_en_MW ),
// outputs
.forward_a ( forward_a ),
.forward_b ( forward_b ),
// STALLING & FLUSHING
// inputs
.inst_IF ( inst_IF ),
.rd_DE ( rd_DE ),
.wb_sel_DE ( wb_sel_DE ),
.br_taken ( br_take_DE ),
// outputs
.stall_IF ( stall_IF ),
.flush_DE ( flush_DE )
);
endmodule