forked from vaibhavviswanathan/CGoL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mips.sv
588 lines (501 loc) · 18.3 KB
/
mips.sv
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//---------------------------------------------------------
// mips.sv
// [email protected] 23 Jan 2008
// Changes 7/3/07
// Updated to SystemVerilog
// Fixed endianness
// 23 Jan 08 added $stop
// 13 Feb 08 solutions version using PLA
// 14 Jan 10 separated controller into synthesized portion and ALUdec
// switched to 2-phase clock to match lab
// modified constant declaration in datapath
// 27 Jan 10 added timescale directive
// removed unused instr wire from mips module
// 3 Feb 2010 added wd and regwrite to regfile sensitivity list
// 25 Jan 11 changed exmem, regfile to use always_latch
// 26 Jan 11 changed nonblocking to blocking assignments in clock generator
//
// Model of subset of MIPS processor described in Ch 1 of CMOS VLSI Design
// note that no sign extension is done because width is
// only 8 bits
//---------------------
// Set delay unit to 1 ns and simulation precision to 0.1 ns (100 ps)
`timescale 1ns / 100ps
// states and instructions
typedef enum logic [3:0] {FETCH1 = 4'b0000, FETCH2, FETCH3, FETCH4,
DECODE, MEMADR, LBRD, LBWR, SBWR,
RTYPEEX, RTYPEWR, BEQEX, JEX} statetype;
typedef enum logic [5:0] {LB = 6'b100000,
SB = 6'b101000,
RTYPE = 6'b000000,
BEQ = 6'b000100,
J = 6'b000010} opcode;
typedef enum logic [5:0] {ADD = 6'b100000,
SUB = 6'b100010,
AND = 6'b100100,
OR = 6'b100101,
SLT = 6'b101010} functcode;
// testbench for testing
module testbench #(parameter WIDTH = 8, REGBITS = 3)();
logic ph1, ph2;
logic reset;
logic memread, memwrite;
logic [WIDTH-1:0] adr, writedata;
logic [WIDTH-1:0] memdata;
// instantiate devices to be tested
// .* notation instantiates all ports in the mips module
// with the correspondingly named signals in this module
mips #(WIDTH,REGBITS) dut(.*);
// external memory for code and data
exmemory #(WIDTH) exmem(ph1, ph2, memwrite, adr, writedata, memdata);
// initialize test
initial
begin
reset <= 1; # 22; reset <= 0;
end
// generate clock to sequence tests
always
begin
ph1 = 0; ph2 = 0; #1;
ph1 = 1; # 4;
ph1 = 0; #1;
ph2 = 1; # 4;
end
always @(posedge ph2)
begin
if(memwrite) begin
assert(adr == 76 & writedata == 7)
$display("Simulation completed successfully");
else $error("Simulation failed");
$finish;
end
end
endmodule
// external memory accessed by MIPS
module exmemory #(parameter WIDTH = 8)
(input logic ph1, ph2,
input logic memwrite,
input logic [WIDTH-1:0] adr, writedata,
output logic [WIDTH-1:0] memdata);
logic [31:0] mem [2**(WIDTH-2)-1:0];
logic [31:0] word;
logic [1:0] bytesel;
logic [WIDTH-2:0] wordadr;
initial
$readmemh("memfile.dat", mem);
assign bytesel = adr[1:0];
assign wordadr = adr[WIDTH-1:2];
// read and write bytes from 32-bit word
always_latch
if(ph2 & memwrite)
case (bytesel)
2'b00: mem[wordadr][7:0] <= writedata;
2'b01: mem[wordadr][15:8] <= writedata;
2'b10: mem[wordadr][23:16] <= writedata;
2'b11: mem[wordadr][31:24] <= writedata;
endcase
assign word = mem[wordadr];
always_comb
case (bytesel)
2'b00: memdata = word[7:0];
2'b01: memdata = word[15:8];
2'b10: memdata = word[23:16];
2'b11: memdata = word[31:24];
endcase
endmodule
// simplified MIPS processor
module mips #(parameter WIDTH = 8, REGBITS = 3)
(input logic ph1, ph2, reset,
input logic [WIDTH-1:0] memdata,
output logic memread, memwrite,
output logic [WIDTH-1:0] adr, writedata);
logic zero, alusrca, memtoreg, iord, pcen, regwrite, regdst;
logic [1:0] pcsrc, alusrcb;
logic [3:0] irwrite;
logic [2:0] alucontrol;
opcode op;
functcode funct;
// instantaiate controller and datapath
// use regular controller for RTL simulation
// PLA-based controller has a slightly different partitioning
// for the circuit implementation but is functionally equivalent.
// . notation connects signals by name, even if the port order
// isn't the same when the schematic or layout editor netlists the
// module for circuit simulation
controller cont(.ph1, .ph2, .reset, .op, .funct, .zero, .memread, .memwrite,
.alusrca, .memtoreg, .iord, .pcen, .regwrite, .regdst,
.pcsrc, .alusrcb, .alucontrol, .irwrite);
datapath #(WIDTH, REGBITS)
dp(.ph1, .ph2, .reset, .memdata, .alusrca, .memtoreg, .iord, .pcen,
.regwrite, .regdst, .pcsrc, .alusrcb, .irwrite, .alucontrol,
.zero, .op, .funct, .adr, .writedata);
endmodule
module controller(input logic ph1, ph2, reset,
input opcode op,
input functcode funct,
input logic zero,
output logic memread, memwrite, alusrca,
output logic memtoreg, iord, pcen,
output logic regwrite, regdst,
output logic [1:0] pcsrc, alusrcb,
output logic [2:0] alucontrol,
output logic [3:0] irwrite);
logic [1:0] aluop;
// This module is broken into two parts for the VLSI labs. The
// aludec is built by hand, while the controller_synth is synthesized.
// control FSM
controller_synth contsyn(.ph1, .ph2, .reset, .op, .zero, .memread, .memwrite,
.alusrca, .memtoreg, .iord, .pcen, .regwrite, .regdst,
.pcsrc, .alusrcb, .aluop, .irwrite);
// other control decoding
aludec ac(.aluop, .funct, .alucontrol);
endmodule
module controller_synth(input logic ph1, ph2, reset,
input opcode op,
input logic zero,
output logic memread, memwrite, alusrca,
output logic memtoreg, iord, pcen,
output logic regwrite, regdst,
output logic [1:0] pcsrc, alusrcb,
output logic [1:0] aluop,
output logic [3:0] irwrite);
logic pcwrite, branch;
statetype state;
// control FSM
statelogic statelog(ph1, ph2, reset, op, state);
outputlogic outputlog(state, memread, memwrite, alusrca,
memtoreg, iord,
regwrite, regdst, pcsrc, alusrcb, irwrite,
pcwrite, branch, aluop);
// other control decoding
assign pcen = pcwrite | (branch & zero); // program counter enable
endmodule
module statelogic(input logic ph1, ph2, reset,
input opcode op,
output statetype state);
statetype nextstate;
logic [3:0] ns, state_logic;
// resetable state register with inital value of FETCH1
mux2 #(4) resetmux(nextstate, FETCH1, reset, ns);
flop #(4) statereg(ph1, ph2, ns, state_logic);
assign state = statetype'(state_logic);
// next state logic
always_comb
begin
case (state)
FETCH1: nextstate = FETCH2;
FETCH2: nextstate = FETCH3;
FETCH3: nextstate = FETCH4;
FETCH4: nextstate = DECODE;
DECODE: case(op)
LB: nextstate = MEMADR;
SB: nextstate = MEMADR;
RTYPE: nextstate = RTYPEEX;
BEQ: nextstate = BEQEX;
J: nextstate = JEX;
default: nextstate = FETCH1; // should never happen
endcase
MEMADR: case(op)
LB: nextstate = LBRD;
SB: nextstate = SBWR;
default: nextstate = FETCH1; // should never happen
endcase
LBRD: nextstate = LBWR;
LBWR: nextstate = FETCH1;
SBWR: nextstate = FETCH1;
RTYPEEX: nextstate = RTYPEWR;
RTYPEWR: nextstate = FETCH1;
BEQEX: nextstate = FETCH1;
JEX: nextstate = FETCH1;
default: nextstate = FETCH1; // should never happen
endcase
end
endmodule
module outputlogic(input statetype state,
output logic memread, memwrite, alusrca,
output logic memtoreg, iord,
output logic regwrite, regdst,
output logic [1:0] pcsrc, alusrcb,
output logic [3:0] irwrite,
output logic pcwrite, branch,
output logic [1:0] aluop);
always_comb
begin
// set all outputs to zero, then
// conditionally assert just the appropriate ones
irwrite = 4'b0000;
pcwrite = 0; branch = 0;
regwrite = 0; regdst = 0;
memread = 0; memwrite = 0;
alusrca = 0; alusrcb = 2'b00; aluop = 2'b00;
pcsrc = 2'b00;
iord = 0; memtoreg = 0;
case (state)
FETCH1:
begin
memread = 1;
irwrite = 4'b0001;
alusrcb = 2'b01;
pcwrite = 1;
end
FETCH2:
begin
memread = 1;
irwrite = 4'b0010;
alusrcb = 2'b01;
pcwrite = 1;
end
FETCH3:
begin
memread = 1;
irwrite = 4'b0100;
alusrcb = 2'b01;
pcwrite = 1;
end
FETCH4:
begin
memread = 1;
irwrite = 4'b1000;
alusrcb = 2'b01;
pcwrite = 1;
end
DECODE: alusrcb = 2'b11;
MEMADR:
begin
alusrca = 1;
alusrcb = 2'b10;
end
LBRD:
begin
memread = 1;
iord = 1;
end
LBWR:
begin
regwrite = 1;
memtoreg = 1;
end
SBWR:
begin
memwrite = 1;
iord = 1;
end
RTYPEEX:
begin
alusrca = 1;
aluop = 2'b10;
end
RTYPEWR:
begin
regdst = 1;
regwrite = 1;
end
BEQEX:
begin
alusrca = 1;
aluop = 2'b01;
branch = 1;
pcsrc = 2'b01;
end
JEX:
begin
pcwrite = 1;
pcsrc = 2'b10;
end
endcase
end
endmodule
module aludec(input logic [1:0] aluop,
input logic [5:0] funct,
output logic [2:0] alucontrol);
always_comb
case (aluop)
2'b00: alucontrol = 3'b010; // add for lb/sb/addi
2'b01: alucontrol = 3'b110; // subtract (for beq)
default: case(funct) // R-Type instructions
ADD: alucontrol = 3'b010;
SUB: alucontrol = 3'b110;
AND: alucontrol = 3'b000;
OR: alucontrol = 3'b001;
SLT: alucontrol = 3'b111;
default: alucontrol = 3'b101; // should never happen
endcase
endcase
endmodule
module datapath #(parameter WIDTH = 8, REGBITS = 3)
(input logic ph1, ph2, reset,
input logic [WIDTH-1:0] memdata,
input logic alusrca, memtoreg, iord,
input logic pcen, regwrite, regdst,
input logic [1:0] pcsrc, alusrcb,
input logic [3:0] irwrite,
input logic [2:0] alucontrol,
output logic zero,
output opcode op,
output functcode funct,
output logic [WIDTH-1:0] adr, writedata);
logic [REGBITS-1:0] ra1, ra2, wa;
logic [WIDTH-1:0] pc, nextpc, data, rd1, rd2, wd, a, srca,
srcb, aluresult, aluout, immx4;
logic [31:0] instr;
logic [WIDTH-1:0] CONST_ZERO;
logic [WIDTH-1:0] CONST_ONE;
assign CONST_ZERO = 0;
assign CONST_ONE = 1;
assign op = opcode'(instr[31:26]);
assign funct = functcode'(instr[5:0]);
// shift left immediate field by 2
assign immx4 = {instr[WIDTH-3:0],2'b00};
// register file address fields
assign ra1 = instr[REGBITS+20:21];
assign ra2 = instr[REGBITS+15:16];
mux2 #(REGBITS) regmux(instr[REGBITS+15:16],
instr[REGBITS+10:11], regdst, wa);
// independent of bit width, load instruction into four 8-bit registers over four cycles
flopen #(8) ir0(ph1, ph2, irwrite[0], memdata[7:0], instr[7:0]);
flopen #(8) ir1(ph1, ph2, irwrite[1], memdata[7:0], instr[15:8]);
flopen #(8) ir2(ph1, ph2, irwrite[2], memdata[7:0], instr[23:16]);
flopen #(8) ir3(ph1, ph2, irwrite[3], memdata[7:0], instr[31:24]);
// datapath
flopenr #(WIDTH) pcreg(ph1, ph2, reset, pcen, nextpc, pc);
flop #(WIDTH) datareg(ph1, ph2, memdata, data);
flop #(WIDTH) areg(ph1, ph2, rd1, a);
flop #(WIDTH) wrdreg(ph1, ph2, rd2, writedata);
flop #(WIDTH) resreg(ph1, ph2, aluresult, aluout);
mux2 #(WIDTH) adrmux(pc, aluout, iord, adr);
mux2 #(WIDTH) src1mux(pc, a, alusrca, srca);
mux4 #(WIDTH) src2mux(writedata, CONST_ONE, instr[WIDTH-1:0],
immx4, alusrcb, srcb);
mux3 #(WIDTH) pcmux(aluresult, aluout, immx4,
pcsrc, nextpc);
mux2 #(WIDTH) wdmux(aluout, data, memtoreg, wd);
regfile #(WIDTH,REGBITS) rf(ph1, ph2, regwrite, ra1, ra2,
wa, wd, rd1, rd2);
alu #(WIDTH) alunit(srca, srcb, alucontrol, aluresult, zero);
endmodule
module alu #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] a, b,
input logic [2:0] alucontrol,
output logic [WIDTH-1:0] result,
output logic zero);
logic [WIDTH-1:0] b2, andresult, orresult, sumresult, sltresult;
andN andblock(a, b, andresult);
orN orblock(a, b, orresult);
condinv binv(b, alucontrol[2], b2);
adder addblock(a, b2, alucontrol[2], sumresult);
// slt should be 1 if most significant bit of sum is 1
assign sltresult = sumresult[WIDTH-1];
mux4 resultmux(andresult, orresult, sumresult, sltresult, alucontrol[1:0], result);
zerodetect #(WIDTH) zd(result, zero);
endmodule
module regfile #(parameter WIDTH = 8, REGBITS = 3)
(input logic ph1, ph2,
input logic regwrite,
input logic [REGBITS-1:0] ra1, ra2, wa,
input logic [WIDTH-1:0] wd,
output logic [WIDTH-1:0] rd1, rd2);
logic [WIDTH-1:0] RAM [2**REGBITS-1:0];
// three ported register file
// read two ports combinationally
// write third port during phase2 (second half-cycle)
// register 0 hardwired to 0
always_latch
if (ph2 & regwrite) RAM[wa] <= wd;
assign rd1 = ra1 ? RAM[ra1] : 0;
assign rd2 = ra2 ? RAM[ra2] : 0;
endmodule
module zerodetect #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] a,
output logic y);
assign y = (a==0);
endmodule
module flop #(parameter WIDTH = 8)
(input logic ph1, ph2,
input logic [WIDTH-1:0] d,
output logic [WIDTH-1:0] q);
logic [WIDTH-1:0] mid;
latch #(WIDTH) master(ph2, d, mid);
latch #(WIDTH) slave(ph1, mid, q);
endmodule
module flopen #(parameter WIDTH = 8)
(input logic ph1, ph2, en,
input logic [WIDTH-1:0] d,
output logic [WIDTH-1:0] q);
logic [WIDTH-1:0] d2;
mux2 #(WIDTH) enmux(q, d, en, d2);
flop #(WIDTH) f(ph1, ph2, d2, q);
endmodule
module flopenr #(parameter WIDTH = 8)
(input logic ph1, ph2, reset, en,
input logic [WIDTH-1:0] d,
output logic [WIDTH-1:0] q);
logic [WIDTH-1:0] d2, resetval;
assign resetval = 0;
mux3 #(WIDTH) enrmux(q, d, resetval, {reset, en}, d2);
flop #(WIDTH) f(ph1, ph2, d2, q);
endmodule
module latch #(parameter WIDTH = 8)
(input logic ph,
input logic [WIDTH-1:0] d,
output logic [WIDTH-1:0] q);
always_latch
if (ph) q <= d;
endmodule
module mux2 #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] d0, d1,
input logic s,
output logic [WIDTH-1:0] y);
assign y = s ? d1 : d0;
endmodule
module mux3 #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] d0, d1, d2,
input logic [1:0] s,
output logic [WIDTH-1:0] y);
always_comb
casez (s)
2'b00: y = d0;
2'b01: y = d1;
2'b1?: y = d2;
endcase
endmodule
module mux4 #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] d0, d1, d2, d3,
input logic [1:0] s,
output logic [WIDTH-1:0] y);
always_comb
case (s)
2'b00: y = d0;
2'b01: y = d1;
2'b10: y = d2;
2'b11: y = d3;
endcase
endmodule
module andN #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] a, b,
output logic [WIDTH-1:0] y);
assign y = a & b;
endmodule
module orN #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] a, b,
output logic [WIDTH-1:0] y);
assign y = a | b;
endmodule
module inv #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] a,
output logic [WIDTH-1:0] y);
assign y = ~a;
endmodule
module condinv #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] a,
input logic invert,
output logic [WIDTH-1:0] y);
logic [WIDTH-1:0] ab;
inv inverter(a, ab);
mux2 invmux(a, ab, invert, y);
endmodule
module adder #(parameter WIDTH = 8)
(input logic [WIDTH-1:0] a, b,
input logic cin,
output logic [WIDTH-1:0] y);
assign y = a + b + cin;
endmodule