-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpu.v
More file actions
501 lines (392 loc) · 13.3 KB
/
cpu.v
File metadata and controls
501 lines (392 loc) · 13.3 KB
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
// RISCV32I CPU top module
// port modification allowed for debugging purposes
`include "consts.v"
module cpu(
input wire clk_in, // system clock signal
input wire rst_in, // reset signal
input wire rdy_in, // ready signal, pause cpu when low
input wire [ 7:0] mem_din, // data input bus
output wire [ 7:0] mem_dout, // data output bus
output wire [31:0] mem_a, // address bus (only 17:0 is used)
output wire mem_wr, // write/read signal (1 for write)
input wire io_buffer_full, // 1 if uart buffer is full
output wire [31:0] dbgreg_dout // cpu register output (debugging demo)
);
// implementation goes here
// Specifications:
// - Pause cpu(freeze pc, registers, etc.) when rdy_in is low
// - rst > rdy
// - Memory read result will be returned in the next cycle. Write takes 1 cycle(no need to wait)
// - Memory is of size 128KB, with valid address ranging from 0x0 to 0x20000
// - I/O port is mapped to address higher than 0x30000 (mem_a[17:16]==2'b11)
// - 0x30000 read: read a byte from input
// - 0x30000 write: write a byte to output (write 0x00 is ignored)
// - 0x30004 read: read clocks passed since cpu starts (in dword, 4 bytes)
// - 0x30004 write: indicates program stop (will output '\0' through uart tx)
// Stall bus wires
wire if_stall_req_o;
wire id_stall_req_o;
wire mem_stall_req_o;
wire if_stall_enable_i;
wire id_stall_enable_i;
wire ex_stall_enable_i;
wire mem_stall_enable_i;
wire wb_stall_enable_i;
// -------------------- PCReg wires --------------------
wire pcreg_jump_enable_i;
wire[`AddrLen - 1: 0] pcreg_jump_pc_i;
wire pcreg_is_branch_i;
wire pcreg_branch_taken_i;
wire[`AddrLen - 1: 0] pcreg_branch_pc_i;
wire[`AddrLen - 1: 0] pcreg_branch_target_i;
// -------------------- IF wires --------------------
wire[`AddrLen - 1: 0] if_pc_i;
wire if_pc_jump_enable_i;
wire if_from_ram_enable_o;
wire[`AddrLen - 1: 0] if_pc_memctrl_o;
wire if_pc_jump_enable_o;
wire if_inst_ready_i;
wire[`RegLen - 1: 0] if_inst_i;
wire if_inst_ready_o;
wire[`AddrLen - 1: 0] if_pc_o;
wire[`InstLen - 1: 0] if_inst_o;
// -------------------- ID wires --------------------
wire[`AddrLen - 1: 0] id_pc_i;
wire[`InstLen - 1: 0] id_inst_i;
wire[`RegLen - 1: 0] id_rs1_data_i, id_rs2_data_i; // Reg -> ID
wire id_rs1_read_enable_o, id_rs2_read_enable_o; // ID -> Reg
wire[`RegAddrLen - 1: 0] id_rs1_addr_o, id_rs2_addr_o;
wire[`AddrLen - 1: 0] id_pc_o;
wire[`RegLen - 1: 0] id_rs1_data_o, id_rs2_data_o;
wire[`AluOpLen - 1: 0] id_aluop_o;
wire[`AluSelLen - 1: 0] id_alusel_o;
wire[`Funct3Len - 1: 0] id_funct3_o;
wire[`RegLen - 1: 0] id_imm_o;
wire[`RegAddrLen - 1: 0] id_rd_addr_o;
wire id_rd_write_enable_o;
wire[`AddrLen - 1: 0] id_next_pc_o;
wire[`AddrLen - 1: 0] id_jump_pc_o;
wire id_is_jal_o;
wire id_jump_enable_o;
// -------------------- EX wires --------------------
wire[`RegLen - 1: 0] ex_rs1_data_i;
wire[`RegLen - 1: 0] ex_rs2_data_i;
wire[`AluOpLen - 1: 0] ex_aluop_i;
wire[`AluSelLen - 1: 0] ex_alusel_i;
wire[`Funct3Len - 1: 0] ex_funct3_i;
wire[`RegLen - 1: 0] ex_imm_i;
wire[`RegAddrLen - 1: 0] ex_rd_addr_i;
wire ex_rd_write_enable_i;
wire[`AddrLen - 1: 0] ex_next_pc_i;
wire[`AddrLen - 1: 0] ex_jump_pc_i;
wire[`AddrLen - 1: 0] ex_next_pc_o;
wire[`RegLen - 1: 0] ex_store_data_o;
wire ex_load_enable_o;
wire ex_store_enable_o;
wire[`AddrLen - 1: 0] ex_load_store_addr_o;
wire[`Funct3Len - 1: 0] ex_funct3_o;
wire[`RegLen - 1: 0] ex_rd_data_o;
wire[`RegAddrLen - 1: 0] ex_rd_addr_o;
wire ex_rd_write_enable_o;
wire ex_rd_ready_o;
wire ex_is_branch_o;
wire ex_branch_taken_o;
wire[`AddrLen - 1: 0] ex_branch_pc_o;
wire[`AddrLen - 1: 0] ex_branch_target_o;
wire[`AddrLen - 1: 0] ex_jump_pc_o;
wire ex_jump_enable_o;
// -------------------- MEM wires --------------------
wire mem_wr_enable_i;
wire mem_wr_i;
wire[`AddrLen - 1: 0] mem_next_pc_i;
wire mem_wr_enable_o;
wire mem_wr_o;
wire[`AddrLen - 1: 0] mem_next_pc_o;
wire[`Funct3Len - 1: 0] mem_funct3_i;
wire[`RegAddrLen - 1: 0] mem_rd_addr_i;
wire mem_rd_write_enable_i;
wire[`RegLen - 1: 0] mem_rd_data_i;
wire[`AddrLen - 1: 0] mem_load_store_addr_i;
wire[1: 0] mem_load_store_type_i;
wire[`RegLen - 1: 0] mem_store_data_i;
wire mem_load_store_ready_i;
wire[`RegLen - 1: 0] mem_load_data_i;
wire[`RegLen - 1: 0] mem_rd_data_o;
wire[`RegAddrLen - 1: 0] mem_rd_addr_o;
wire mem_rd_write_enable_o;
// -------------------- WB wires --------------------
wire[`RegLen - 1: 0] wb_rd_data_i;
wire[`RegAddrLen - 1: 0] wb_rd_addr_i;
wire wb_rd_write_enable_i;
// -------------------- Miscellaneous --------------------
wire rdy_ = rdy_in & ~io_buffer_full;
wire last_is_load;
wire[`RegAddrLen - 1: 0] last_load_rd_addr;
wire is_if_output;
wire is_mem_output;
wire id_stall_req_resume;
wire if_icache_hitted_o;
MemCtrl memctrl(
.clk(clk_in),
.rst(rst_in),
.rdy(rdy_),
.ram_data_i(mem_din),
.ram_data_o(mem_dout),
.ram_addr_o(mem_a),
.ram_wr_o(mem_wr),
.if_from_ram_enable_i(if_from_ram_enable_o),
.if_pc_i(if_pc_memctrl_o),
.if_pc_jump_enable_i(if_pc_jump_enable_o),
.is_if_output_o(is_if_output),
.if_inst_ready_o(if_inst_ready_i),
.if_inst_o(if_inst_i),
.id_stall_req_i(id_stall_req_o),
.id_stall_pc_i(id_pc_o),
.id_stall_req_resume_o(id_stall_req_resume),
.mem_wr_enable_i(mem_wr_enable_o), // from MEM
.mem_wr_i(mem_wr_o), // from MEM
.mem_next_pc_i(mem_next_pc_o),
.load_store_addr_i(mem_load_store_addr_i),
.load_store_type_i(mem_load_store_type_i),
.store_data_i(mem_store_data_i),
.is_mem_output_o(is_mem_output),
.load_store_ready_o(mem_load_store_ready_i),
.load_data_o(mem_load_data_i)
);
StallBus stallbus(
.if_stall_req_i(if_stall_req_o),
.id_stall_req_i(id_stall_req_o),
.mem_stall_req_i(mem_stall_req_o),
.if_stall_enable_o(if_stall_enable_i),
.id_stall_enable_o(id_stall_enable_i),
.ex_stall_enable_o(ex_stall_enable_i),
.mem_stall_enable_o(mem_stall_enable_i),
.wb_stall_enable_o(wb_stall_enable_i)
);
Register register(
.clk(clk_in),
.rst(rst_in),
.rs1_enable_i(id_rs1_read_enable_o),
.rs2_enable_i(id_rs2_read_enable_o),
.rs1_addr_i(id_rs1_addr_o),
.rs2_addr_i(id_rs2_addr_o),
.rs1_data_o(id_rs1_data_i),
.rs2_data_o(id_rs2_data_i),
.rd_enable_i(wb_rd_write_enable_i),
.rd_addr_i(wb_rd_addr_i),
.rd_data_i(wb_rd_data_i),
.dbgregs_o(dbgreg_dout)
);
assign pcreg_is_branch_i = ex_is_branch_o;
assign pcreg_branch_taken_i = ex_branch_taken_o;
assign pcreg_branch_pc_i = ex_branch_pc_o;
assign pcreg_branch_target_i = ex_branch_target_o;
assign pcreg_jump_enable_i = ex_jump_enable_o | id_jump_enable_o;
assign pcreg_jump_pc_i = (ex_jump_enable_o)? ex_jump_pc_o: id_jump_pc_o; // B_func taken/JALR: ex_jump; JAL: id_jump
PCReg pc_reg(
.clk(clk_in),
.rst(rst_in),
.rdy(rdy_),
.stall_enable(if_stall_enable_i),
.jump_enable_i(pcreg_jump_enable_i),
.jump_pc_i(pcreg_jump_pc_i),
.is_branch_i(pcreg_is_branch_i),
.branch_taken_i(pcreg_branch_taken_i),
.branch_pc_i(pcreg_branch_pc_i),
.branch_target_i(pcreg_branch_target_i),
.icache_hitted_i(if_icache_hitted_o),
.inst_ready_i(if_inst_ready_o),
.pc_o(if_pc_i),
.pc_jump_enable_o(if_pc_jump_enable_i)
);
InstFetch inst_fetch(
.clk(clk_in),
.rst(rst_in),
.stall_req_o(if_stall_req_o),
.pc_i(if_pc_i),
.pc_jump_enable_i(if_pc_jump_enable_i),
.if_from_ram_enable_o(if_from_ram_enable_o),
.pc_memctrl_o(if_pc_memctrl_o),
.pc_jump_enable_o(if_pc_jump_enable_o),
.is_if_output_i(is_if_output),
.inst_ready_i(if_inst_ready_i),
.inst_i(if_inst_i),
.icache_hitted_o(if_icache_hitted_o),
.inst_ready_o(if_inst_ready_o),
.pc_o(if_pc_o),
.inst_o(if_inst_o)
);
wire if_id_rst_i = rst_in; // | id_jump_enable_o | ex_jump_enable_o;
// wire id_stall_enable_add_jump = id_stall_enable_i ||
// (pcreg_jump_enable_i && (if_icache_hitted_o || (!if_icache_hitted_o && !if_inst_ready_o)));
// JAL detected in ID | B_func taken/JALR detected in EX, reset IF_ID(NOP).
IF_ID if_id(
.clk(clk_in),
.rst(if_id_rst_i),
.rdy(rdy_),
.stall_enable(id_stall_enable_i),
.if_pc(if_pc_o),
.if_inst(if_inst_o),
.id_pc(id_pc_i),
.id_inst(id_inst_i)
);
InstDecode inst_decode(
.pc_i(id_pc_i),
.inst_i(id_inst_i),
.predicted_pc_i(if_pc_i),
.stall_req_o(id_stall_req_o),
.pc_o(id_pc_o),
// Read after LOAD signal from ID_EX(last inst)
.last_is_load_i(last_is_load),
.last_load_rd_addr_i(last_load_rd_addr),
.stall_req_resume_i(id_stall_req_resume),
// data from Reg
.rs1_data_i(id_rs1_data_i),
.rs2_data_i(id_rs2_data_i),
// addr to Reg
.rs1_read_enable_o(id_rs1_read_enable_o),
.rs2_read_enable_o(id_rs2_read_enable_o),
.rs1_addr_o(id_rs1_addr_o),
.rs2_addr_o(id_rs2_addr_o),
// Forwarding sources from EX
.rd_ready_ex_fw_i(ex_rd_ready_o), // ready: LOAD is not ready
.rd_addr_ex_fw_i(ex_rd_addr_o),
.rd_data_ex_fw_i(ex_rd_data_o),
// Forwarding sources from MEM
.rd_ready_mem_fw_i(mem_rd_write_enable_o),
.rd_addr_mem_fw_i(mem_rd_addr_o),
.rd_data_mem_fw_i(mem_rd_data_o),
// to ID_EX
.rs1_data_o(id_rs1_data_o),
.rs2_data_o(id_rs2_data_o),
.aluop_o(id_aluop_o),
.alusel_o(id_alusel_o),
.funct3_o(id_funct3_o),
.imm_o(id_imm_o),
.rd_addr_o(id_rd_addr_o),
.rd_write_enable_o(id_rd_write_enable_o),
.next_pc_o(id_next_pc_o),
.jump_pc_o(id_jump_pc_o),
.is_jal_o(id_is_jal_o),
.jump_enable_o(id_jump_enable_o)
);
wire id_ex_jump_rst = ex_jump_enable_o;
// B_func taken/JALR detected in EX | Read after LOAD stall: reset ID_EX(NOP)
ID_EX id_ex(
.clk(clk_in),
.rst(rst_in),
.rdy(rdy_),
.stall_enable(ex_stall_enable_i),
.jump_rst(id_ex_jump_rst),
.id_rs1_data(id_rs1_data_o),
.id_rs2_data(id_rs2_data_o),
.id_aluop(id_aluop_o),
.id_alusel(id_alusel_o),
.id_funct3(id_funct3_o),
.id_imm(id_imm_o),
.id_rd_addr(id_rd_addr_o),
.id_rd_write_enable(id_rd_write_enable_o),
.id_next_pc(id_next_pc_o),
.id_jump_pc(id_jump_pc_o),
.ex_rs1_data(ex_rs1_data_i),
.ex_rs2_data(ex_rs2_data_i),
.ex_aluop(ex_aluop_i),
.ex_alusel(ex_alusel_i),
.ex_funct3(ex_funct3_i),
.ex_imm(ex_imm_i),
.ex_rd_addr(ex_rd_addr_i),
.ex_rd_write_enable(ex_rd_write_enable_i),
.ex_next_pc(ex_next_pc_i),
.ex_jump_pc(ex_jump_pc_i),
// LOAD signal
.last_is_load(last_is_load),
.last_load_rd_addr(last_load_rd_addr)
);
Execution excution(
.rs1_data_i(ex_rs1_data_i),
.rs2_data_i(ex_rs2_data_i),
.aluop_i(ex_aluop_i),
.alusel_i(ex_alusel_i),
.funct3_i(ex_funct3_i),
.imm_i(ex_imm_i),
.rd_addr_i(ex_rd_addr_i),
.rd_write_enable_i(ex_rd_write_enable_i),
.next_pc_i(ex_next_pc_i),
.jump_pc_i(ex_jump_pc_i),
.predicted_pc_i(id_pc_i),
.next_pc_o(ex_next_pc_o),
.store_data_o(ex_store_data_o),
.load_enable_o(ex_load_enable_o),
.store_enable_o(ex_store_enable_o),
.load_store_addr_o(ex_load_store_addr_o),
.funct3_o(ex_funct3_o),
.rd_write_enable_o(ex_rd_write_enable_o),
.rd_data_o(ex_rd_data_o),
.rd_addr_o(ex_rd_addr_o),
.rd_ready_o(ex_rd_ready_o),
.is_branch_o(ex_is_branch_o),
.branch_taken_o(ex_branch_taken_o),
.branch_pc_o(ex_branch_pc_o),
.branch_target_o(ex_branch_target_o),
.jump_pc_o(ex_jump_pc_o),
.jump_enable_o(ex_jump_enable_o)
);
wire ex_mem_rst_i = rst_in | id_stall_req_resume;
EX_MEM ex_mem(
.clk(clk_in),
.rst(ex_mem_rst_i),
.rdy(rdy_),
.stall_enable(mem_stall_enable_i),
.ex_next_pc(ex_next_pc_o),
.ex_store_data(ex_store_data_o),
.ex_load_enable(ex_load_enable_o),
.ex_store_enable(ex_store_enable_o),
.ex_load_store_addr(ex_load_store_addr_o),
.ex_funct3(ex_funct3_o),
.ex_rd_data(ex_rd_data_o),
.ex_rd_addr(ex_rd_addr_o),
.ex_rd_write_enable(ex_rd_write_enable_o),
.mem_wr_enable(mem_wr_enable_i),
.mem_wr(mem_wr_i),
.mem_next_pc(mem_next_pc_i),
.mem_funct3(mem_funct3_i),
.mem_rd_addr(mem_rd_addr_i),
.mem_rd_write_enable(mem_rd_write_enable_i),
.mem_rd_data(mem_rd_data_i),
.mem_load_store_addr(mem_load_store_addr_i),
.mem_load_store_type(mem_load_store_type_i),
.mem_store_data(mem_store_data_i)
);
MemoryAccess memory_access(
.wr_enable_i(mem_wr_enable_i),
.wr_i(mem_wr_i),
.next_pc_i(mem_next_pc_i),
.funct3_i(mem_funct3_i),
.rd_addr_i(mem_rd_addr_i),
.rd_write_enable_i(mem_rd_write_enable_i),
.rd_data_i(mem_rd_data_i),
.stall_req_o(mem_stall_req_o),
.wr_enable_o(mem_wr_enable_o),
.wr_o(mem_wr_o),
.next_pc_o(mem_next_pc_o),
.is_mem_output_i(is_mem_output),
.load_store_ready_i(mem_load_store_ready_i),
.load_data_i(mem_load_data_i),
.rd_data_o(mem_rd_data_o),
.rd_addr_o(mem_rd_addr_o),
.rd_write_enable_o(mem_rd_write_enable_o)
);
MEM_WB mem_wb(
.clk(clk_in),
.rst(rst_in),
.rdy(rdy_),
.stall_enable(wb_stall_enable_i),
.mem_rd_data(mem_rd_data_o),
.mem_rd_addr(mem_rd_addr_o),
.mem_rd_write_enable(mem_rd_write_enable_o),
.wb_rd_data(wb_rd_data_i),
.wb_rd_addr(wb_rd_addr_i),
.wb_rd_write_enable(wb_rd_write_enable_i)
);
// simple WB: wb_xxx connected to registers
endmodule