-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.v
429 lines (373 loc) · 7.96 KB
/
Code.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
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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 03/27/2019 07:49:02 PM
// Design Name:
// Module Name: lab03
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module ProgramCounter(
input [31:0] newPc,
input clk,
output reg [31:0] Pc);
always @ (posedge clk) begin
Pc = newPc;
end
endmodule
module adder (
input [31:0] Pc,
output reg [31:0] newPc);
initial begin
newPc = 32'd100;
end
always @ (*) begin
newPc = Pc + 4;
end
endmodule
module IFID(
input [31:0] doin,
input clk,
output reg [31:0] doout);
always @ (posedge clk) begin
doout = doin;
end
endmodule
module InstrFetch(
input [31:0] a,
output reg [31:0] do
);
reg [31:0]mem[0:128];
initial
begin
mem[32'd100] = 32'h8C220000;
mem[32'd104] = 32'h8C230004;
mem[32'd108] = 32'h8C240008;
mem[32'd112] = 32'h8c25000C;
mem[32'd116] = 32'h004A3020;
end
always @ (*)
begin
//$display("mem[a] = %h", mem[a]);
do = mem[a];
//$display("do = %h", do);
end
endmodule
module ControlUnit(
//just output values, not used
input [31:0] op,
//input [31:0] func,
output reg wreg,
output reg m2reg,
output reg wmem,
output reg[1:0] aluc,
output reg aluimm,
output reg regrt
);
reg [5:0] opcode;
always @ (*)
begin
opcode = op[31:26];
case(opcode)
//R type instruction
6'b000000:
begin
regrt=0;
aluc=2'b10;
wreg=1;
aluimm=0;
wmem=0;
m2reg=0;
end
//I type instruction
default:
begin
regrt=1;
aluc=2'b00;
wreg=1;
aluimm=1;
wmem=0;
m2reg=1;
end
endcase
end
endmodule
module RorI(
input [31:0] rd,
//input [31:0] rt,
input regrt,
output reg[4:0] stuff
);
wire[4:0] rd2;
wire[4:0] rt;
assign rd2 = rd[15:11];
assign rt = rd[20:16];
always @ (*)
begin
case(regrt)
1'b0:stuff=rd2;
1'b1:stuff=rt;
endcase
end
endmodule
module InstrReg(
input [31:0] rna, //address
input clk,
input we,
//wn is register write
input [4:0] wn,
//d is data
input [31:0] d,
output reg[31:0] QA,
output reg[31:0] QB
);
reg[4:0] rnaa;
reg[4:0] rnb;
reg [31:0]ireg[0:31];
integer i;
initial
begin
for (i=0; i < 32; i=i+1) begin
ireg[i]=32'd0;
end
end
always @ (posedge clk)
begin
case(we)
//J and sw type
1'b0:ireg[wn]=d;
//R and lw type
1'b1:ireg[wn]=d;
endcase
end
always @ (negedge clk) begin
rnaa = rna[25:21];
rnb = rna[20:16];
QA = ireg[rnaa];
QB = ireg[rnb];
$display("%b %b",ireg[rnaa], ireg[rnb]);
end
endmodule
module signext(
input [31:0] imm,
output reg[31:0] imm2
);
reg[15:0] immtemp;
reg[31:0] temp;
always @ (*)
begin
immtemp = imm[15:0];
immtemp = immtemp>>15;
case(immtemp)
16'd1:
begin
temp[15:0] = imm[15:0];
temp[31:16] = 16'h1111;
imm2 = temp;
end
default:
begin
temp[15:0] = imm[15:0];
temp[31:16] = 16'h0000;
imm2 = temp;
end
endcase
end
endmodule
module IDEXE(
input clk,
input wreg,
input m2reg,
input wmem,
input [1:0] aluc,
input aluimm,
input [4:0] writeback,
input [31:0] QA,
input [31:0] QB,
input [31:0] imm,
input [31:0] immext,
output reg ewreg,
output reg em2reg,
output reg ewmem,
output reg[1:0] ealuc,
output reg ealuimm,
output reg[4:0] wn,
output reg[31:0] eQA,
output reg[31:0] eQB,
output reg[5:0] eimm,
output reg[31:0] eimm2
);
always @ (posedge clk) begin
ewreg=wreg;
em2reg=m2reg;
ewmem=wmem;
ealuc=aluc;
ealuimm=aluimm;
wn=writeback;
eQA=QA;
eQB=QB;
eimm2=immext;
eimm=imm[5:0];
end
endmodule
module eQBorIMM2(
input ealuimm,
input [31:0] eQB,
input [31:0] eimm2,
output reg[31:0] select
);
always @ (*) begin
case(ealuimm)
1'b0:select=eQB;
1'b1:select=eimm2;
endcase
end
endmodule
module ALUcontrol(
input [1:0] aluop,
input [5:0] func,
output reg[3:0] aluc
);
always @ (*)
begin
case(aluc)
//branch
2'b01:aluc=4'b0110; //subtract
//R type
2'b10:
begin
case(func)
6'b100000:aluc=4'b0010; //add
6'b100010:aluc=4'b0110; //subtract
6'b100100:aluc=4'b0000; //AND
6'b100101:aluc=4'b0001; //OR
6'b101010:aluc=4'b0111; //slt
endcase
end
//I type
default: aluc=4'b0010; //add
endcase
end
endmodule
module ALU(
input [31:0] a,
input [31:0] b,
input [3:0] aluc,
output reg [31:0] r
);
always @ (*) begin
case(aluc)
4'b0010: r=a+b; //add
4'b0110: r=a-b; //subtract
4'b0000: r=a&b; //and
4'b0001: r=a|b; //or
4'b0111: r=a<b; //slt
endcase
end
endmodule
module EXEMEM(
input clk,
input ewreg,
input em2reg,
input ewmem,
input [4:0] ewn,
input [31:0] er,
input [31:0] eeQB,
output reg mwreg,
output reg mm2reg,
output reg mwmem,
output reg[4:0] mwn,
output reg[31:0] mr,
output reg[31:0] meQB
);
always @ (posedge clk) begin
mwreg=ewreg;
mm2reg=em2reg;
mwmem=ewmem;
mwn=ewn;
mr=er;
meQB=eeQB;
end
endmodule
module DataMem(
input we,
input [31:0] a,
input [31:0] di,
output reg[31:0] do
);
reg [31:0]dmem[0:128];
initial
begin
dmem[32'd0] = 32'hA00000AA;
dmem[32'd4] = 32'h10000011;
dmem[32'd8] = 32'h20000022;
dmem[32'd12] = 32'h30000033;
dmem[32'd16] = 32'h40000044;
dmem[32'd20] = 32'h50000055;
dmem[32'd24] = 32'h60000066;
dmem[32'd28] = 32'h70000077;
dmem[32'd32] = 32'h80000088;
dmem[32'd36] = 32'h90000099;
end
always @ (*) begin
//perform mad combos here
case(we)
// load word
1'b0:
begin
do=dmem[a];
end
//store word
1'b1:
begin
dmem[a]=di;
end
endcase
end
endmodule
module MEMWB(
input clk,
input mwreg,
input mm2reg,
input [4:0] mwn,
input [31:0] mr,
input [31:0] do,
output reg wwreg,
output reg wm2reg,
output reg[4:0] wn,
output reg[31:0] wr,
output reg[31:0] wdo
);
always @ (posedge clk) begin
wwreg=mwreg;
wm2reg=mm2reg;
wn=mwn;
wr=mr;
wdo=do;
end
endmodule
module wb(
input [31:0] wr,
input [31:0] wdo,
input wm2reg,
output reg[31:0] d
);
// R type m2reg is 0
always @ (*) begin
case(wm2reg)
1'b0:d=wr;
1'b1:d=wdo;
endcase
end
endmodule