-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmips_32.v
372 lines (331 loc) · 8.35 KB
/
mips_32.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
`timescale 1ns / 1ps
// for future; vim wire formatting macro is @R
//set up module: use :<>norm! @M
module mips_32(
input clk, reset,
output[31:0] result
);
//// define all the wires here.
//// IF
wire [9:0] branch_address;
wire branch_taken;
wire [9:0] jump_address;
wire jump;
wire [9:0] pc_plus4;
wire [31:0] instr;
wire [31:0] if_id_instr;
wire [31:0] id_ex_instr;
wire [9:0] if_id_pc_plus4;
wire IF_Flush;
wire mem_wb_reg_write;
wire [4:0] mem_wb_destination_reg;
wire [31:0] write_back_data;
wire Data_Hazard;
wire [31:0] reg1, reg2;
wire [31:0] id_ex_reg1, id_ex_reg2;
wire [31:0] imm_value;
wire [31:0] id_ex_imm_value;
wire [4:0] destination_reg;
wire [4:0] id_ex_destination_reg;
wire mem_to_reg, mem_write, mem_read, alu_src, reg_write;
wire id_ex_mem_to_reg, id_ex_mem_read, id_ex_mem_write, id_ex_reg_write, id_ex_alu_src;
wire [1:0] alu_op;
wire [1:0] id_ex_alu_op;
wire [31:0] ex_mem_alu_result;
wire [1:0] Forward_A, Forward_B;
wire [31:0] alu_in2_out, alu_result;
wire ex_mem_reg_write;
wire [31:0] ex_mem_instr;
wire [4:0] ex_mem_destination_reg;
wire [31:0] ex_mem_alu_in2_out;
wire ex_mem_mem_to_reg;
wire ex_mem_mem_read;
wire ex_mem_mem_write;
wire [31:0] mem_wb_alu_result;
wire [31:0] mem_wb_mem_read_data;
wire [31:0] mem_read_data;
wire mem_wb_mem_to_reg;
///////////////////////////// Instruction Fetch
IF_pipe_stage if_pipeline_stage (
.clk(clk),
.reset(reset),
.en(Data_Hazard),
.branch_address(branch_address),
.branch_taken(branch_taken),
.jump_address(jump_address),
.jump(jump),
.pc_plus4(pc_plus4),
.instr(instr)
);
///////////////////////////// IF/ID registers
//special case, since have extra input to take in
//every other in between case, use `pipe_reg`
// one reg for each input/output for the pipeline in-between
pipe_reg_en #(.WIDTH(10)) pc_plus4_if_reg (
.clk(clk),
.reset(reset),
.en(Data_Hazard),
.flush(IF_Flush),
.d(pc_plus4),
.q(if_id_pc_plus4)
);
pipe_reg_en #(.WIDTH(32)) pipe_instr_reg (
.clk(clk),
.reset(reset),
.en(Data_Hazard),
.flush(IF_Flush),
.d(instr),
.q(if_id_instr)
);
///////////////////////////// Instruction Decode
ID_pipe_stage id_pipeline(
.clk(clk),
.reset(reset),
.pc_plus4(if_id_pc_plus4),
.instr(if_id_instr),
.mem_wb_reg_write(mem_wb_reg_write),
.mem_wb_write_reg_addr(mem_wb_destination_reg),
.mem_wb_write_back_data(write_back_data),
.Data_Hazard(Data_Hazard),
.IF_Flush(IF_Flush),
.reg1(reg1),
.reg2(reg2),
.imm_value(imm_value),
.branch_address(branch_address),
.jump_address(jump_address),
.branch_taken(branch_taken),
.destination_reg(destination_reg),
.mem_to_reg(mem_to_reg),
.alu_op(alu_op),
.mem_read(mem_read),
.mem_write(mem_write),
.alu_src(alu_src),
.reg_write(reg_write),
.jump(jump)
);
///////////////////////////// ID/EX registers
// if_id_instr
pipe_reg #(.WIDTH(32)) ID_EX_if_id_instr_reg (
.clk(clk),
.reset(reset),
.d(if_id_instr),
.q(id_ex_instr)
);
// reg1
pipe_reg #(.WIDTH(32)) ID_EX_reg1_reg (
.clk(clk),
.reset(reset),
.d(reg1),
.q(id_ex_reg1)
);
// reg2
pipe_reg #(.WIDTH(32)) ID_EX_reg2_reg (
.clk(clk),
.reset(reset),
.d(reg2),
.q(id_ex_reg2)
);
// imm_value
pipe_reg #(.WIDTH(32)) ID_EX_imm_value_reg (
.clk(clk),
.reset(reset),
.d(imm_value),
.q(id_ex_imm_value)
);
// destination_reg
pipe_reg #(.WIDTH(5)) ID_EX_destination_reg_reg (
.clk(clk),
.reset(reset),
.d(destination_reg),
.q(id_ex_destination_reg)
);
// mem_to_reg
pipe_reg #(.WIDTH(1)) ID_EX_mem_to_reg_reg (
.clk(clk),
.reset(reset),
.d(mem_to_reg),
.q(id_ex_mem_to_reg)
);
// alu_op
pipe_reg #(.WIDTH(2)) ID_EX_alu_op_reg (
.clk(clk),
.reset(reset),
.d(alu_op),
.q(id_ex_alu_op)
);
// mem_read
pipe_reg #(.WIDTH(1)) ID_EX_mem_read_reg (
.clk(clk),
.reset(reset),
.d(mem_read),
.q(id_ex_mem_read)
);
// mem_write
pipe_reg #(.WIDTH(1)) ID_EX_mem_write_reg (
.clk(clk),
.reset(reset),
.d(mem_write),
.q(id_ex_mem_write)
);
// alu_src
pipe_reg #(.WIDTH(1)) ID_EX_alu_src_reg (
.clk(clk),
.reset(reset),
.d(alu_src),
.q(id_ex_alu_src)
);
// reg_write
pipe_reg #(.WIDTH(1)) ID_EX_reg_write_reg (
.clk(clk),
.reset(reset),
.d(reg_write),
.q(id_ex_reg_write)
);
///////////////////////////// Hazard_detection unit
Hazard_detection hazardous(
.id_ex_mem_read(id_ex_mem_read),
.id_ex_destination_reg(id_ex_destination_reg),
.if_id_rs(if_id_instr[25:21]),
.if_id_rt(if_id_instr[20:16]),
.branch_taken(branch_taken), //
.jump(jump),
.Data_Hazard(Data_Hazard),
.IF_Flush(IF_Flush)
);
///////////////////////////// Execution
EX_pipe_stage ex_stage(
.id_ex_instr(id_ex_instr),
.reg1(id_ex_reg1),
.reg2(id_ex_reg2),
.id_ex_imm_value(id_ex_imm_value),
.ex_mem_alu_result(ex_mem_alu_result), // used for result forwarding
.mem_wb_write_back_result(write_back_data), //also result forwarding
.id_ex_alu_src(id_ex_alu_src),
.id_ex_alu_op(id_ex_alu_op),
.Forward_A (Forward_A), //also result forwarding
.Forward_B(Forward_B), //also result forwarding
.alu_in2_out(alu_in2_out), // internal???
.alu_result(alu_result)
);
///////////////////////////// Forwarding unit
EX_Forwarding_unit ex_forwarding (
.ex_mem_reg_write(ex_mem_reg_write),
.ex_mem_write_reg_addr(ex_mem_destination_reg),
.id_ex_instr_rs(id_ex_instr[25:21]),
.id_ex_instr_rt(id_ex_instr[20:16]),
.mem_wb_reg_write(mem_wb_reg_write),
.mem_wb_write_reg_addr(mem_wb_destination_reg),
.Forward_A(Forward_A),
.Forward_B(Forward_B)
);
///////////////////////////// EX/MEM registers
// id_ex_instr
pipe_reg #(.WIDTH(32)) EX_MEM_id_ex_instr_reg (
.clk(clk),
.reset(reset),
.d(id_ex_instr),
.q(ex_mem_instr)
);
// id_ex_destination_reg
pipe_reg #(.WIDTH(5)) EX_MEM_id_ex_destination_reg (
.clk(clk),
.reset(reset),
.d(id_ex_destination_reg),
.q(ex_mem_destination_reg)
);
// alu_result
pipe_reg #(.WIDTH(32)) EX_MEM_alu_result_reg (
.clk(clk),
.reset(reset),
.d(alu_result),
.q(ex_mem_alu_result)
);
// alu_in2_out
pipe_reg #(.WIDTH(32)) EX_MEM_alu_in2_out_reg (
.clk(clk),
.reset(reset),
.d(alu_in2_out),
.q(ex_mem_alu_in2_out)
);
// id_ex_mem_to_reg
pipe_reg #(.WIDTH(1)) EX_MEM_id_ex_mem_to_reg_reg (
.clk(clk),
.reset(reset),
.d(id_ex_mem_to_reg),
.q(ex_mem_mem_to_reg)
);
// id_ex_mem_read
pipe_reg #(.WIDTH(1)) EX_MEM_id_ex_mem_read_reg (
.clk(clk),
.reset(reset),
.d(id_ex_mem_read),
.q(ex_mem_mem_read)
);
// id_ex_mem_write
pipe_reg #(.WIDTH(1)) EX_MEM_id_ex_mem_write_reg (
.clk(clk),
.reset(reset),
.d(id_ex_mem_write),
.q(ex_mem_mem_write)
);
// id_ex_reg_write
pipe_reg #(.WIDTH(1)) EX_MEM_id_ex_reg_write_reg (
.clk(clk),
.reset(reset),
.d(id_ex_reg_write),
.q(ex_mem_reg_write)
);
///////////////////////////// memory
data_memory data_mem(
.clk(clk),
.mem_access_addr(ex_mem_alu_result),
.mem_write_data(ex_mem_alu_in2_out),
.mem_write_en(ex_mem_mem_write),
.mem_read_en(ex_mem_mem_read),
.mem_read_data(mem_read_data)
);
///////////////////////////// MEM/WB registers
// ex_mem_alu_result
pipe_reg #(.WIDTH(32)) MEM_WB_ex_mem_alu_result_reg (
.clk(clk),
.reset(reset),
.d(ex_mem_alu_result),
.q(mem_wb_alu_result)
);
// mem_read_data
pipe_reg #(.WIDTH(32)) MEM_WB_mem_read_data_reg (
.clk(clk),
.reset(reset),
.d(mem_read_data),
.q(mem_wb_mem_read_data)
);
// ex_mem_mem_to_reg
pipe_reg #(.WIDTH(1)) MEM_WB_ex_mem_mem_to_reg_reg (
.clk(clk),
.reset(reset),
.d(ex_mem_mem_to_reg),
.q(mem_wb_mem_to_reg)
);
// ex_mem_reg_write
pipe_reg #(.WIDTH(1)) MEM_WB_ex_mem_reg_write_reg (
.clk(clk),
.reset(reset),
.d(ex_mem_reg_write),
.q(mem_wb_reg_write)
);
// ex_mem_destination_reg
pipe_reg #(.WIDTH(5)) MEM_WB_ex_mem_destination_reg_reg (
.clk(clk),
.reset(reset),
.d(ex_mem_destination_reg),
.q(mem_wb_destination_reg)
);
///////////////////////////// writeback
mux2 #(.mux_width(32)) writeback_mux (
.a(mem_wb_alu_result), //0
.b(mem_wb_mem_read_data), //1
.sel(mem_wb_mem_to_reg),
.y(write_back_data)
);
assign result = write_back_data;
endmodule