-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIPS.v
311 lines (304 loc) · 6.48 KB
/
MIPS.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
// build a module for assembling
// FPGA: EP2C35F672C6
module MIPS
(
clk,
rst,
Sel,
SRAMaddress, // SRAM Address bus 18 Bits
SRAMWEn, // SRAM Write Enable
SRAMdata, // SRAM Data bus 16 Bits
);
// input and outputs
input clk;
input rst;
input Sel;
output SRAMWEn;
output [17:0] SRAMaddress;
inout [15:0] SRAMdata;
// wires
wire WB_En21;
wire WB_En22;
wire WB_En32;
wire WB_En42;
wire MEM_R_En21;
wire MEM_R_En22;
wire MEM_R_En32;
wire MEM_R_En42;
wire MEM_W_En21;
wire MEM_W_En22;
wire MEM_W_En32;
wire Is_Imm1;
wire Is_Imm2;
wire Is_Imm3;
wire Is_Imm4;
wire shouldForward11;
wire shouldForward12;
wire Branch_Taken;
wire SRAM_NOT_READY;
wire Stall;
wire loadForwardStall;
wire superStall;
wire [1:0] BR_Type1;
wire [1:0] BR_Type2;
wire [4:0] src11;
wire [4:0] src21;
wire [4:0] src12;
wire [4:0] src22;
wire [4:0] dest1;
wire [4:0] dest2;
wire [4:0] dest3;
wire [4:0] dest4;
wire [3:0] EXE_Cmd1;
wire [3:0] EXE_Cmd2;
wire [31:0] PC11;
wire [31:0] PC12;
wire [31:0] PC2;
wire [31:0] PC3;
wire [31:0] PC4;
wire [31:0] Instruction1;
wire [31:0] Instruction2;
wire [31:0] Branch_Address;
wire [31:0] readdata11;
wire [31:0] readdata12;
wire [31:0] readdata21;
wire [31:0] readdata22;
wire [31:0] readdata23;
wire [31:0] data11;
wire [31:0] data12;
wire [31:0] data21;
wire [31:0] data22;
wire [31:0] Immediate1;
wire [31:0] Immediate2;
wire [31:0] Immediate3;
wire [31:0] forwardVal11;
wire [31:0] forwardVal12;
wire [31:0] memForwardVal;
wire [31:0] WB_Data0;
wire [31:0] WB_Data;
wire [31:0] ALU_Result31;
wire [31:0] ALU_Result32;
wire [31:0] ALU_Result42;
wire [31:0] Mem_Data1;
wire [31:0] Mem_Data2;
// assemble modules
// instruction fetch
IF_Stage IFS
(
.clk(clk),
.rst(rst),
.stall(Stall),
.loadForwardStall(loadForwardStall),
.superStall(superStall),
.branch_address(Branch_Address),
.Instruction(Instruction1),
.branch_taken(Branch_Taken),
.PC(PC11)
);
// instruction fetch register
IF_Stage_reg IFR
(
.clk(clk),
.rst(rst),
.stall(Stall),
.loadForwardStall(loadForwardStall),
.superStall(superStall),
.branch_taken(Branch_Taken),
.Instruction_in(Instruction1),
.PC_in(PC11),
.Instruction(Instruction2),
.PC(PC12)
);
// instruction decode
ID_Stage IDS
(
.clk(clk),
.rst(rst),
.writedata(WB_Data),
.write(WB_En42),
.lastDestination(dest4),
.Instruction_in(Instruction2),
.WB_En(WB_En21),
.MEM_R_En(MEM_R_En21),
.MEM_W_En(MEM_W_En21),
.Is_Imm(Is_Imm1),
.BR_Type(BR_Type1),
.EXE_Cmd(EXE_Cmd1),
.readdata1(readdata11),
.readdata2(readdata21),
.Immediate(Immediate1),
.data1(data11),
.data2(data21),
.src1(src11),
.src2(src21),
.dest(dest1)
);
// hazard detectoin unit
Hazard HU
(
.Sel(Sel),
.BR_Type(BR_Type1),
.SRAM_NOT_READY( SRAM_NOT_READY ),
.WB_En1(WB_En22),
.WB_En2(WB_En32),
.Is_Imm(Is_Imm1),
.src1(src11),
.src2(src21),
.dest1(dest2),
.dest2(dest3),
.Stall(Stall),
.superStall(superStall)
);
// instruction decode register
ID_Stage_reg IDR
(
.clk(clk),
.rst(rst),
.stall(Stall),
.loadForwardStall(loadForwardStall),
.superStall(superStall),
.branch_taken(Branch_Taken),
.readdata1_in(readdata11),
.readdata2_in(readdata21),
.Is_Imm_in(Is_Imm1),
.Immediate_in(Immediate1),
.data1_in(data11),
.data2_in(data21),
.dest_in(dest1),
.WB_En_in(WB_En21),
.MEM_R_En_in(MEM_R_En21),
.MEM_W_En_in(MEM_W_En21),
.BR_Type_in(BR_Type1),
.EXE_Cmd_in(EXE_Cmd1),
.PC_in(PC12),
.src1_in(src11),
.src2_in(src21),
.readdata1(readdata12),
.readdata2(readdata22),
.Is_Imm(Is_Imm2),
.Immediate(Immediate2),
.data1(data12),
.data2(data22),
.src1(src12),
.src2(src22),
.dest(dest2),
.WB_En(WB_En22),
.MEM_R_En(MEM_R_En22),
.MEM_W_En(MEM_W_En22),
.BR_Type(BR_Type2),
.EXE_Cmd(EXE_Cmd2),
.PC(PC2)
);
ForwardUnit FU
(
.BR_Type(BR_Type2), // Pass BR_Type through levels
.WB_En1(WB_En32),
.WB_En2(WB_En42),
.mem_W_En(MEM_W_En22),
.mem_R_En1(MEM_R_En32),
.mem_R_En2(MEM_R_En42),
.Is_Imm(Is_Imm2),
.src1(src12),
.src2(src22),
.readdata2(readdata22),
.dest1(dest3),
.dest2(dest4),
.aluResult1(WB_Data0),
.aluResult2(WB_Data),
.srcOut1(forwardVal11),
.srcOut2(forwardVal12),
.memOut(memForwardVal),
.shouldForward1(shouldForward11),
.shouldForward2(shouldForward12),
.loadForwardStall(loadForwardStall)
);
// execution
EXE_Stage EXES
(
.BR_Type(BR_Type2),
.EXE_Cmd(EXE_Cmd2),
.readdata1(data12),
.readdata2(readdata22),
.Immediate(Immediate2),
.data2(data22),
.branch_taken(Branch_Taken),
.PC_in(PC11),
.branch_address(Branch_Address),
.ALU_result(ALU_Result31),
.shouldForward1(shouldForward11),
.shouldForward2(shouldForward12),
.forwardVal1(forwardVal11),
.forwardVal2(forwardVal12)
);
// execution register
EXE_Stage_reg EXER
(
.clk(clk),
.rst(rst),
.loadForwardStall(loadForwardStall),
.superStall(superStall),
.PC_in(PC2),
.PC(PC3),
.WB_En_in(WB_En22),
.MEM_R_En_in(MEM_R_En22),
.MEM_W_En_in(MEM_W_En22),
.readdata_in(memForwardVal),
.Is_Imm_in(Is_Imm2),
.Immediate_in(Immediate3),
.ALU_result_in(ALU_Result31),
.dest_in(dest2),
.WB_En(WB_En32),
.MEM_R_En(MEM_R_En32),
.MEM_W_En(MEM_W_En32),
.readdata(readdata23),
.Is_Imm(Is_Imm3),
.Immediate(Immediate3),
.ALU_result(ALU_Result32),
.dest(dest3)
);
// memory
MEM_Stage MEMS
(
.clk(clk),
.rst(rst),
.read(MEM_R_En32),
.write(MEM_W_En32),
.aluResult(ALU_Result32),
.readdata(Mem_Data1),
.writedata(readdata23),
.wbData(WB_Data0),
.SRAMaddress( SRAMaddress ),
.SRAMWEn( SRAMWEn ),
.SRAM_NOT_READY( SRAM_NOT_READY ),
.SRAMdata( SRAMdata )
);
// memory register
MEM_Stage_reg MEMR
(
.clk(clk),
.rst(rst),
.superStall(superStall),
.PC_in(PC3),
.PC(PC4),
.WB_En_in(WB_En32),
.MEM_R_En_in(MEM_R_En32),
.Is_Imm_in(Is_Imm3),
.ALU_result_in(ALU_Result32),
.Mem_Data_in(Mem_Data1),
.dest_in(dest3),
.WB_En(WB_En42),
.MEM_R_En(MEM_R_En42),
.Is_Imm(Is_Imm4),
.ALU_result(ALU_Result42),
.Mem_Data(Mem_Data2),
.dest(dest4)
);
// writeback
WB_Stage WBS
(
.MEM_R_En(MEM_R_En42),
.ALU_result(ALU_Result42),
.Mem_Data(Mem_Data2),
.WB_Data(WB_Data)
);
endmodule