-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpu.v
268 lines (227 loc) · 6.09 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
`ifndef CPU_V
`define CPU_V
`include "execute/execute_stage.v"
`include "decode/decode_stage.v"
`include "fetch/fetch.v"
`include "memory/mem_stage.v"
`include "register/fetch_pipeline_reg.v"
`include "register/writeback_pipeline_reg.v"
`include "hazard/hazard_unit.v"
module cpu(clock);
input wire clock;
// Inputs from control and decode
wire [31:0] pc_branch_d;
wire pc_src_d;
// Input from hazard unit
wire StallF;
wire StallD;
wire FlushE;
wire [1:0] ForwardAE;
wire [1:0] ForwardBE;
// Outputs to decode
wire [31:0] pc_plus_4f;
wire [31:0] pc_plus_4d;
wire [31:0] instructionf;
wire [31:0] instructiond;
wire RegWriteD;
wire MemtoRegD;
wire MemWriteD;
wire [3:0] ALUControlD;
wire ALUSrcD;
wire RegDstD;
wire [31:0] RD1D;
wire [31:0] RD2D;
wire [4:0] RsD;
wire [4:0] RtD;
wire [4:0] RdD;
wire [31:0] SignImmD;
wire syscallD;
wire [31:0] syscall_functD;
wire [31:0] syscall_param1D;
wire RegWriteE;
wire MemtoRegE;
wire MemWriteE;
wire RegDstE;
wire [3:0] ALUControlE;
// ALU outputs (ignored)
wire [31:0] RD1E; // ALU outputs.
wire [31:0] RD2E;
wire [4:0] RsE;
wire [4:0] RtE;
wire [4:0] RdE;
wire [31:0] SignImmE;
wire [4:0] shamtE;
wire [4:0] WriteRegE;
wire [31:0] WriteDataE;
wire [31:0] ALUOutE;
// Inputs from the Fetch stage.
wire [31:0] instruction;
wire [31:0] pc_plus_four;
// Outputs to EX
wire [4:0] shamtD;
// Outputs of the memory stage.
wire RegWriteM;
wire MemtoRegM;
wire [31:0] Writeback_RD;
wire [31:0] ALUOutM;
wire [4:0] WriteRegM;
// Outputs of Writeback pipe
wire RegWriteW;
wire MemtoRegW;
wire [31:0] ReadDataW;
wire [31:0] ALUOutW;
wire [4:0] WriteRegW;
//this wire is a mux for ResultW
wire [31:0] ResultW;
assign ResultW = MemtoRegW ? ReadDataW : ALUOutW;
// This is true if the current instruction is a jump / branch instruction.
// This is distinct from pc_branch_d, which stores the pc to jump to.
// This is distinct from pc_src_d, which decides whether the processor
// actually jumps.
wire BranchD;
fetch fetch(
.clk(clock),
// Inputs from decode and control.
.pc_branch_d(pc_branch_d),
.pcsrc_d(pc_src_d),
// Inputs from the hazard unit.
.stallf(StallF),
// Outputs to the decode stage.
.pc_plus_4f(pc_plus_4f),
.instructionf(instructionf)
);
fetch_pipeline_reg fpipe(
.clock(clock)
, .clear(pc_src_d)
, .StallD(StallD)
, .pc_plus_four_F(pc_plus_4f)
, .instruction_F(instructionf)
, .pc_plus_four_D(pc_plus_4d)
, .instruction_D(instructiond));
decode_stage decode(
.clock(clock),
// Inputs from fetch.
.instruction(instructiond),
.pc_plus_four(pc_plus_4d),
// Inputs from writeback.
.writeback_value(ResultW),
.writeback_id(WriteRegW),
.reg_write_W(RegWriteW),
// Decode to EX.
.reg_rs_value(RD1D),
.reg_rt_value(RD2D),
.immediate(SignImmD),
.reg_rs_id(RsD),
.reg_rt_id(RtD),
.reg_rd_id(RdD),
.shamtD(shamtD),
// Control to EX
.reg_write_D(RegWriteD),
.mem_to_reg(MemtoRegD),
.mem_write(MemWriteD),
.alu_op(ALUControlD),
.alu_src(ALUSrcD),
.reg_dest(RegDstD),
// Outputs back to fetch.
.pc_src (pc_src_d),
.jump_address(pc_branch_d),
// Syscall logic
.syscall(syscallD),
.syscall_funct(syscall_functD),
.syscall_param1(syscall_param1D)
);
execute_stage EX_stage(
.clk(clock),
// Input from the hazard control unit.
.FlushE(FlushE),
// Input from the decode stage.
.RegWriteD(RegWriteD),
.MemtoRegD(MemtoRegD),
.MemWriteD(MemWriteD),
.ALUControlD(ALUControlD),
.ALUSrcD(ALUSrcD),
.RegDstD(RegDstD),
.RD1D(RD1D),
.RD2D(RD2D),
.RsD(RsD),
.RtD(RtD),
.RdD(RdD),
.SignImmD(SignImmD),
.shamtD(shamtD),
.syscallD(syscallD),
.syscall_functD(syscall_functD),
.syscall_param1D(syscall_param1D),
// Output to the mem stage.
.RegWriteE(RegWriteE),
.MemtoRegE(MemtoRegE),
.MemWriteE(MemWriteE),
.RegDstE(RegDstE),
.ALUControlE(ALUControlE),
.RD1E(RD1E),
.RD2E(RD2E),
.RsE(RsE),
.RtE(RtE),
.RdE(RdE),
.SignImmE(SignImmE),
.ResultW(ResultW),
.ALUOutM(ALUOutM),
// Input from the hazard unit.
.ForwardAE(ForwardAE),
.ForwardBE(ForwardBE),
// Wires from the control unit forwarded to the mem stage.
.WriteRegE(WriteRegE),
.WriteDataE(WriteDataE),
.ALUOutE(ALUOutE)
);
mem_stage myMemStage(
.CLK(clock),
.RegWriteE(RegWriteE),
.MemtoRegE(MemtoRegE),
.MemWriteE(MemWriteE),
.ALUOutE(ALUOutE),
.WriteDataE(WriteDataE),
.WriteRegE(WriteRegE),
.RegWriteM(RegWriteM),
.MemtoRegM(MemtoRegM),
.RD(Writeback_RD),
.ALUOutM(ALUOutM),
.WriteRegM(WriteRegM)
);
//writeback pipe
writeback_pipeline_reg wpipe(
.clock(clock),
.RegWriteM(RegWriteM),
.MemtoRegM(MemtoRegM),
.ReadDataM(Writeback_RD),
.ALUOutM(ALUOutM),
.WriteRegM(WriteRegM),
.RegWriteW(RegWriteW),
.MemtoRegW(MemtoRegW),
.ReadDataW(ReadDataW),
.ALUOutW(ALUOutW),
.WriteRegW(WriteRegW));
hazard_unit hazard(
// Inputs
.RsD(RsD),
.RtD(RtD),
.BranchD(pc_src_d),
.RsE(RsE),
.RtE(RtE),
.WriteRegE(WriteRegE),
.MemtoRegE(MemtoRegE),
.RegWriteE(RegWriteE),
.WriteRegM(WriteRegM),
.MemtoRegM(MemtoRegM),
.RegWriteM(RegWriteM),
.WriteRegW(WriteRegW),
.RegWriteW(RegWriteW),
.syscallD(syscallD),
// Outputs
.StallF(StallF),
.StallD(StallD),
.FlushE(FlushE),
.ForwardAE(ForwardAE),
.ForwardBE(ForwardBE)
);
endmodule
`endif