-
Notifications
You must be signed in to change notification settings - Fork 0
/
anant_vima_kshitij.v
1186 lines (1054 loc) · 24.9 KB
/
anant_vima_kshitij.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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Anant Anurag 2014A3PS0184P
Vima Gupta 2014A3PS0246P
Kshitij Khandelwal 2015A3PS0156P
*/
module TOP(clk, rst);
input wire clk, rst;
wire [15:0] PC;
wire [1:0] PCSrc;
wire [2:0] ALU_OP;
wire sign_extend;
wire ALUSrcA;
wire [2:0] ALUSrcB;
wire [1:0] ReadR1;
wire ReadR2;
wire RegDstWrite;
wire MemToReg;
wire PCBEqCond, PCBNqCond;
wire PCWrite;
wire MemWrite, MemRead;
wire IRWrite;
wire RegWrite;
wire [3:0] OPCODE, FUNCFIELD;
wire [15:0] PC_OUT;
wire [15:0] PC_IN;
wire ZERO_OUT;
wire PCEnableWrite;
wire A1_Out;
wire NZERO;
wire A2_Out;
wire O1_Out;
wire [15:0] D_ReadReg1RT, D_ReadReg2RT;
wire [15:0] D_Offset, D_RegSW;
wire [15:0] D_BT;
wire [15:0] ALUOUT_IN, ALUOUT_OUT;
wire [15:0] D_Instruction;
wire [1:0] A_2bit_Offset, A_2bit_RegSWLW;
wire [3:0] A_Offset, A_RegSWLW;
wire [3:0] A_ReadReg1RT, A_ReadReg2RT;
wire [3:0] A_WriteRegRT_BT;
wire [15:0] IR_InstructionOut;
wire [15:0] MDR_IN, MDR_OUT;
wire [15:0] D_WriteData;
wire [15:0] ALU_1_IN, ALU_2_IN;
wire [15:0] D_JUMP_SE_Out, D_SE_Out, D_USE_Out, D_L1S_Out;
controllerFSM FSM_Main(PCSrc, ALU_OP, sign_extend,
ALUSrcA ,ALUSrcB, ReadR1, ReadR2,
RegDstWrite, MemToReg, PCBEqCond,
PCBNqCond, PCWrite, MemWrite,
MemRead, IRWrite, RegWrite,
OPCODE,FUNCFIELD, clk, rst);
and A1(A1_Out, ZERO_OUT, PCBEqCond);
not N1(NZERO, ZERO_OUT);
and A2(A2_Out, NZERO, PCBNqCond);
or O1(O1_Out, A1_Out, A2_Out);
or O2(PCEnableWrite, O1_Out, PCWrite);
muxPC MPC(PC_IN, ALUOUT_IN, ALUOUT_OUT, D_BT, PCSrc);
programCounter ProgCount(PC_OUT, PC_IN, PCEnableWrite, clk, rst);
instructionMemory IM(D_Instruction, PC_OUT, 1'b1, rst);
concat11 c11(A_RegSWLW, A_2bit_RegSWLW);
concat10 c10(A_Offset, A_2bit_Offset);
instructionRegister IR(IR_InstructionOut ,OPCODE, FUNCFIELD,
A_ReadReg1RT, A_ReadReg2RT,
A_2bit_Offset, A_2bit_RegSWLW,
A_WriteRegRT_BT,
D_Instruction,
IRWrite,
clk, rst);
dataMemory DM(MDR_IN, ALUOUT_OUT, D_RegSW, MemRead, MemWrite, rst);
memoryDataRegister MDR(MDR_OUT, MDR_IN, clk);
wire [15:0] memory[15:0];
registerFile RF(D_ReadReg1RT, D_ReadReg2RT,
D_Offset, D_RegSW,
D_BT,
MDR_OUT, ALUOUT_OUT,
A_ReadReg1RT, A_ReadReg2RT,
A_Offset, A_RegSWLW,
A_WriteRegRT_BT,
RegDstWrite, RegWrite, MemToReg,
clk, rst);
sign_extend_12bto16b se12t16b(D_JUMP_SE_Out, IR_InstructionOut[11:0]);
sign_extend_8bto16b se8t16b(D_SE_Out, IR_InstructionOut[7:0]);
unsign_extend_8bto16b use8t16b(D_USE_Out, IR_InstructionOut[7:0]);
left_1b_shift l1bs(D_L1S_Out, D_SE_Out);
MUXpreALU MPA(ALU_1_IN, ALU_2_IN,
PC_OUT,
D_ReadReg1RT, D_BT, D_Offset,
D_ReadReg2RT, D_RegSW,
D_JUMP_SE_Out , D_SE_Out, D_USE_Out, D_L1S_Out,
sign_extend,
ReadR1, ReadR2,
ALUSrcA, ALUSrcB);
alu ALU_MAIN(ALUOUT_IN,ZERO_OUT,
ALU_1_IN,ALU_2_IN,
ALU_OP);
ALUOut AO(ALUOUT_OUT, ALUOUT_IN, clk);
endmodule
module programCounter(PC_OUT, PC_IN, C_PCWrite, clk, rst);
output reg [15:0] PC_OUT;
input wire [15:0] PC_IN;
input wire C_PCWrite;
input wire clk, rst;
always@(posedge clk)
begin
if(rst == 1'b1)
begin
PC_OUT <= 16'd0;
end
else begin
if (C_PCWrite == 1'b1) begin
PC_OUT <= PC_IN;
end
else begin
PC_OUT <= PC_OUT;
end
end
end
endmodule
module memoryDataRegister(MDR_OUT, MDR_IN, clk);
output reg [15:0] MDR_OUT;
input wire [15:0] MDR_IN;
input wire clk;
always@(posedge clk)
begin
MDR_OUT <= MDR_IN;
end
endmodule
module dataMemory( D_Data,
A_DataAddress,
D_WriteData,
C_DMRead, C_DMWrite,
rst
);
output reg [15:0] D_Data;
input wire [15:0] A_DataAddress;
input wire [15:0] D_WriteData;
input wire C_DMRead, C_DMWrite;
input wire rst;
reg [15:0] memory[0:8192]; // 8192 = 8*1024, 8k*2bytes=16kB
initial $readmemh("data_file.txt", memory);
always @(*) begin
if (rst == 1) begin
D_Data = 16'b0000_0000_0000_0000;
end
else begin
if (C_DMWrite == 1) begin
memory[A_DataAddress] <= D_WriteData;
end
// else begin
// memory[A_DataAddress] <= memory[A_DataAddress];
// end
if (C_DMRead == 1) begin
D_Data <= memory[A_DataAddress];
end
// else begin
// D_Data <= D_Data;
// end
end
end
endmodule
module instructionMemory( D_Instruction,
A_InstrAddress,
C_IMRead,
rst
);
output reg [15:0] D_Instruction;
input wire [15:0] A_InstrAddress;
input wire C_IMRead;
input wire rst;
reg [15:0] memory[0:8192]; // 8192 = 8*1024, 8k*2bytes=16kB
initial $readmemb("instruction_file.txt", memory);
always @(*) begin
if (rst == 1) begin
D_Instruction = 16'b0000_0000_0000_0000;
end
else begin
if (C_IMRead == 1'b1) begin
D_Instruction = memory[A_InstrAddress];
end
// else begin // Dont know what else to do
// memory[A_InstrAddress] = memory[A_InstrAddress];
// end
end
end
endmodule
module alu ( out,z,
a,b,
alu_op
);
output reg [15:0] out;
output reg z;
input wire [15:0] a,b;
input wire [2:0] alu_op;
parameter ADD = 3'b000 ;
parameter SUB = 3'b001 ;
parameter SHR = 3'b010 ;// shift right a by b bits
parameter SHL = 3'b011 ;// shift left a by b bits
parameter NAND = 3'b100 ;
parameter OR = 3'b101 ;
parameter DIR = 3'b110 ;
parameter SAR = 3'b111 ;// shift arithmetic right
always@(*)
begin
case(alu_op)
ADD : begin
out <= a+b;
z <= (out==0);
end
SUB : begin
out <= a-b;
z <= (out==0);
end
SHR : begin
out <= a>>b;
z <= (out==0);
end
SHL : begin
out <= a<<b;
z <= (out==0);
end
NAND: begin
out <= ~(a&b);
z <= (out==0);
end
OR : begin
out <= a|b;
z <= (out==0);
end
DIR : begin
out <= a;
z <= (out==0);
end
SAR : begin
out <= a>>>b;
z <= (out==0);
end
default:begin
out <= 16'd0;
z <= 1'bz;
end
endcase
end
endmodule
module ALUOut(ALUOUT_OUT, ALUOUT_IN, clk);
output reg [15:0] ALUOUT_OUT;
input wire [15:0] ALUOUT_IN;
input wire clk;
always@(posedge clk)
begin
ALUOUT_OUT <= ALUOUT_IN;
end
endmodule
module instructionRegister( D_Instr,
OPCODE, FUNCFIELD,
A_ReadReg1RT, A_ReadReg2RT,
A_Offset, A_RegSWLW,
A_WriteRegRT_BT,
D_MemData,
C_IRWrite,
clk, rst
);
output reg[15:0] D_Instr;
output reg [3:0] OPCODE, FUNCFIELD;
output reg [3:0] A_ReadReg1RT, A_ReadReg2RT;
output reg [1:0] A_Offset, A_RegSWLW;
output reg [3:0] A_WriteRegRT_BT;
input wire [15:0] D_MemData;
input wire C_IRWrite;
input wire clk, rst;
always@(posedge clk)
begin
if (rst == 1)
begin
OPCODE <= 4'b0000;
FUNCFIELD <= 4'b0000;
A_ReadReg1RT <= 4'b0000;
A_ReadReg2RT <= 4'b0000;
A_Offset <= 4'b0000;
A_RegSWLW <= 4'b0000;
A_WriteRegRT_BT <= 4'b0000;
end
else begin
if (C_IRWrite == 1)
begin
D_Instr <= D_MemData;
OPCODE <= D_MemData[15:12];
FUNCFIELD <= D_MemData[3:0];
A_ReadReg1RT <= D_MemData[7:4];
A_ReadReg2RT <= D_MemData[3:0];
A_Offset <= D_MemData[9:8];
A_RegSWLW <= D_MemData[11:10];
A_WriteRegRT_BT <= D_MemData[11:8];
end
else begin
OPCODE = OPCODE;
FUNCFIELD = FUNCFIELD;
A_ReadReg1RT = A_ReadReg1RT;
A_ReadReg2RT = A_ReadReg2RT;
A_Offset = A_Offset;
A_RegSWLW = A_RegSWLW;
A_WriteRegRT_BT = A_WriteRegRT_BT;
end
end
end
endmodule
module MUXpreALU( ALU_1_IN, ALU_2_IN,
PC,
D_ReadReg1RT, D_BT, D_Offset,
D_ReadReg2RT, D_RegSW,
D_JUMP_SE_Out , D_SE_Out, D_USE_Out, D_L1S_Out,
C_SignExtend,
C_RegDstRead1R, C_RegDstRead2R,
C_ALUSrc_A, C_ALUSrc_B
);
output reg [15:0] ALU_1_IN, ALU_2_IN;
input wire [15:0] PC;
input wire [15:0] D_ReadReg1RT, D_BT, D_Offset;
input wire [15:0] D_ReadReg2RT, D_RegSW;
input wire [15:0] D_JUMP_SE_Out, D_SE_Out, D_USE_Out, D_L1S_Out;
input wire C_SignExtend;
input wire [1:0] C_RegDstRead1R;
input wire C_RegDstRead2R;
input wire C_ALUSrc_A;
input wire [2:0] C_ALUSrc_B;
reg [15:0] M1_Out, M2_Out, M3_Out;
always@(*)
begin
case(C_RegDstRead1R)
2'b00 : M1_Out <= D_ReadReg1RT;
2'b01 : M1_Out <= D_BT;
2'b10 : M1_Out <= D_Offset;
default : M1_Out <= 16'd1;
endcase
case(C_RegDstRead2R)
1'b0 : M2_Out <= D_ReadReg2RT;
1'b1 : M2_Out <= D_RegSW;
default : M2_Out <= 16'd1;
endcase
case(C_SignExtend)
1'b0 : M3_Out <= D_USE_Out;
1'b1 : M3_Out <= D_SE_Out;
default : M3_Out <= 16'd0;
endcase
case(C_ALUSrc_A)
1'b0 : ALU_1_IN <= PC;
1'b1 : ALU_1_IN <= M1_Out;
default : ALU_1_IN <= 16'd1;
endcase
case(C_ALUSrc_B)
3'b000 : ALU_2_IN <= M2_Out;
3'b001 : ALU_2_IN <= 2'b01;
3'b010 : ALU_2_IN <= M3_Out;
3'b011 : ALU_2_IN <= D_L1S_Out;
3'b100 : ALU_2_IN <= D_JUMP_SE_Out;
3'b101 : ALU_2_IN <= D_JUMP_SE_Out[7:4];
default : ALU_2_IN <= 16'd1;
endcase
end
endmodule
module sign_extend_12bto16b(JUMP_SE_Out, instr11to0);
output wire [15:0] JUMP_SE_Out;
input wire [11:0] instr11to0;
assign JUMP_SE_Out = {{4{instr11to0[11]}}, instr11to0};
endmodule
module sign_extend_8bto16b(SE_Out, instr7to0);
output wire [15:0] SE_Out;
input wire [7:0] instr7to0;
assign SE_Out = {{8{instr7to0[7]}}, instr7to0[7:0]};
endmodule
module unsign_extend_8bto16b(USE_Out, instr7to0);
output wire [15:0] USE_Out;
input wire [7:0] instr7to0;
assign USE_Out = {8'b0000_0000, instr7to0[7:0]};
endmodule
module left_1b_shift(L1S_Out, SE_Out);
output wire [15:0] L1S_Out;
input wire [15:0] SE_Out;
assign L1S_Out = SE_Out << 1'b1;
endmodule
module registerFile( D_ReadReg1RT, D_ReadReg2RT,
D_Offset, D_RegSW,
D_BT,
D_MDR_IN, D_ALU_IN,
A_ReadReg1RT, A_ReadReg2RT,
A_Offset, A_RegSWLW,
A_WriteRegRT_BT,
C_RegDstWrite, C_RegWrite, C_MemToReg,
clk, rst
);
output reg [15:0] D_ReadReg1RT, D_ReadReg2RT;
output reg [15:0] D_Offset, D_RegSW;
output reg [15:0] D_BT;
input wire [3:0] A_ReadReg1RT, A_ReadReg2RT;
input wire [3:0] A_Offset, A_RegSWLW;
input wire [3:0] A_WriteRegRT_BT;
input wire [15:0] D_MDR_IN, D_ALU_IN;
input wire C_RegDstWrite, C_RegWrite, C_MemToReg;
input wire clk, rst;
reg [15:0] memory[0:15];
reg [3:0] a_write;
reg [15:0] d_write;
initial
$readmemh("rf_data.txt",memory);
always@(posedge clk or rst)
begin
// Selecting Address to write to RF
if (C_RegDstWrite == 1)
begin
a_write = A_WriteRegRT_BT;
end
else begin
a_write = A_RegSWLW;
end
// Selecting Data to write to RF
if (C_MemToReg == 1)
begin
d_write = D_MDR_IN;
//d_write = 16'd7;
end
else begin
d_write = D_ALU_IN;
//d_write = 16'd7;
end
if (C_RegWrite == 1)
begin
memory[a_write] = d_write;
end
//else begin
// a_write = 4'd0;
// d_write = 16'd99;
//end
end
always@(posedge clk or rst)
begin
memory[4'b0000] = 16'b0000_0000_0000_0000;
if (rst == 0)
begin
D_ReadReg1RT <= memory[A_ReadReg1RT];
D_ReadReg2RT <= memory[A_ReadReg2RT];
D_Offset <= memory[A_Offset];
D_RegSW <= memory[A_RegSWLW];
D_BT <= memory[A_WriteRegRT_BT];
end
//else begin
// memory[4'd0] <= 16'd0;
// memory[4'd1] <= 16'd0;
// memory[4'd2] <= 16'd0;
// memory[4'd3] <= 16'd0;
// memory[4'd4] <= 16'd0;
// memory[4'd5] <= 16'd0;
// memory[4'd6] <= 16'd0;
// memory[4'd7] <= 16'd0;
// memory[4'd8] <= 16'd0;
// memory[4'd9] <= 16'd0;
// memory[4'd10] <= 16'd0;
// memory[4'd11] <= 16'd0;
// memory[4'd12] <= 16'd0;
// memory[4'd13] <= 16'd0;
// memory[4'd14] <= 16'd0;
// memory[4'd15] <= 16'd0;
//end
end
endmodule
module controllerFSM(PCSrc, ALUOp, sign_extend,
ALUSrcA ,ALUSrcB, ReadR1, ReadR2,
RegWriteDst, MemToReg, PCBEqCond,
PCBNqCond, PCWrite, MemWrite,
MemRead, IRWrite, RegWrite,
opcode,func_field, clk, rst);
reg [5:0] NextState, CurrentState;
output reg [1:0] PCSrc;
output reg [2:0] ALUOp;
output reg sign_extend;
output reg ALUSrcA;
output reg [2:0] ALUSrcB;
output reg [1:0] ReadR1;
output reg ReadR2;
output reg RegWriteDst;
output reg MemToReg;
output reg PCBEqCond, PCBNqCond;
output reg PCWrite;
output reg MemWrite, MemRead;
output reg IRWrite;
output reg RegWrite;
input wire [3:0] opcode, func_field;
input wire clk, rst;
parameter I_FETCH = 6'b00_0000;
parameter I_DECODE = 6'b00_0001;
parameter EX_ADD = 6'b00_0010;
parameter EX_ADDI1 = 6'b00_0011;
parameter EX_ADDI2 = 6'b00_0100;
parameter EX_LW_SW = 6'b00_0101;
parameter EX_BEQ = 6'b00_0110;
parameter EX_BNQ = 6'b00_0111;
parameter EX_JMP = 6'b00_1000;
parameter EX_NAND = 6'b00_1001;
parameter EX_NANDI = 6'b00_1010;
parameter EX_OR = 6'b00_1110;
parameter EX_ORI = 6'b00_1111;
parameter EX_SUB = 6'b01_0000;
parameter EX_SUBI1 = 6'b01_0001;
parameter EX_SUBI2 = 6'b01_0010;
parameter EX_SLL = 6'b10_0100;
parameter EX_SRL = 6'b10_0101;
parameter EX_SRA = 6'b10_0110;
parameter MEM_SW = 6'b01_0110;
parameter MEM_LW = 6'b10_0011;
parameter MEM_RTYPE = 6'b10_1000;
parameter WRITEBACK = 6'b11_1111;
parameter OP_ADD = 4'b1000;
parameter OP_ADDI1 = 4'b1001;
parameter OP_ADDI2 = 4'b1010;
parameter OP_SUB = 4'b1100;
parameter OP_SUBI1 = 4'b1101;
parameter OP_SUBI2 = 4'b1110;
parameter OP_SHIFT = 4'b0000;
parameter FF_SHIFT_LEFT_LOGICAL = 4'b0001;
parameter FF_SHIFT_RIGHT_LOGICAL = 4'b0010;
parameter FF_SHIFT_RIGHT_ARITHMETIC = 4'b0011;
parameter OP_NAND = 4'b1011;
parameter OP_NANDI = 4'b0111;
parameter OP_OR = 4'b1111;
parameter OP_ORI = 4'b0110;
parameter OP_BEQ = 4'b0100;
parameter OP_BNQ = 4'b0101;
parameter OP_JMP = 4'b0011;
parameter OP_LW = 4'b0001;
parameter OP_SW = 4'b0010;
// To update the state
// initial
// CurrentState <= I_FETCH;
always @(posedge clk, posedge rst)
begin
if (rst) begin
CurrentState <= I_FETCH;
// NextState <= I_FETCH;
end
else begin CurrentState <= NextState;
end
end
// To calculate the next state
always@(*)
begin
case(CurrentState)
I_FETCH :NextState <= I_DECODE;
I_DECODE :begin
case(opcode)
OP_ADD : NextState <= EX_ADD;
OP_ADDI1 : NextState <= EX_ADDI1;
OP_ADDI2 : NextState <= EX_ADDI2;
OP_SUB : NextState <= EX_SUB;
OP_SUBI1 : NextState <= EX_SUBI1;
OP_SUBI2 : NextState <= EX_SUBI2;
OP_SHIFT : begin
if (func_field == FF_SHIFT_LEFT_LOGICAL) begin
NextState <= EX_SLL;
end
else if(func_field == FF_SHIFT_RIGHT_LOGICAL) begin
NextState <= EX_SRL;
end
else if (func_field == FF_SHIFT_RIGHT_ARITHMETIC) begin
NextState <= EX_SRA;
end
else begin
NextState <= I_FETCH;
end
end
OP_NAND : NextState <= EX_NAND;
OP_NANDI : NextState <= EX_NANDI;
OP_OR : NextState <= EX_OR;
OP_ORI : NextState <= EX_ORI;
OP_BEQ : NextState <= EX_BEQ;
OP_BNQ : NextState <= EX_BNQ;
OP_JMP : NextState <= EX_JMP;
OP_LW : NextState <= EX_LW_SW;
OP_SW : NextState <= EX_LW_SW;
default : NextState <= I_FETCH;
endcase
end
EX_ADD :NextState <= MEM_RTYPE;
EX_ADDI1 :NextState <= MEM_RTYPE;
EX_ADDI2 :NextState <= MEM_RTYPE;
EX_SUB :NextState <= MEM_RTYPE;
EX_SUBI1 :NextState <= MEM_RTYPE;
EX_SUBI2 :NextState <= MEM_RTYPE;
EX_SLL :NextState <= MEM_RTYPE;
EX_SRL :NextState <= MEM_RTYPE;
EX_SRA :NextState <= MEM_RTYPE;
EX_NAND :NextState <= MEM_RTYPE;
EX_NANDI :NextState <= MEM_RTYPE;
EX_OR :NextState <= MEM_RTYPE;
EX_ORI :NextState <= MEM_RTYPE;
EX_BEQ :NextState <= I_FETCH;
EX_BNQ :NextState <= I_FETCH;
EX_JMP :NextState <= I_FETCH;
EX_LW_SW :begin
if (opcode == OP_LW) NextState <= MEM_LW;
else if (opcode == OP_SW) NextState <= MEM_SW;
else NextState <= I_FETCH;
end
MEM_RTYPE :NextState <= I_FETCH;
MEM_SW :NextState <= I_FETCH;
MEM_LW :NextState <= WRITEBACK;
WRITEBACK :NextState <= I_FETCH;
default :NextState <= I_FETCH;
endcase
end
// Output Calculation in MOORE it is only state dependent
always@(*)
begin
case(CurrentState)
I_FETCH :begin
PCSrc <= 2'b00;
ALUOp <= 3'b000;
sign_extend <= 1'bx;
ALUSrcA <= 1'b0;
ALUSrcB <= 3'b001;
ReadR1 <= 2'bxx;
ReadR2 <= 1'bx;
RegWriteDst <= 1'bx;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b1;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b1;
RegWrite <= 1'b0;
end
I_DECODE :begin
PCSrc <= 2'bxx;
ALUOp <= 3'bxxx;
sign_extend <= 1'bx;
ALUSrcA <= 1'b0;
ALUSrcB <= 3'b001;
ReadR1 <= 2'b00;
ReadR2 <= 1'b0;
RegWriteDst <= 1'bx;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_ADD :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b000;
sign_extend <= 1'bx;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b000;
ReadR1 <= 2'b00;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_ADDI1 :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b000;
sign_extend <= 1'b1;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b010;
ReadR1 <= 2'b01;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_ADDI2 :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b000;
sign_extend <= 1'b0;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b010;
ReadR1 <= 2'b01;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_SUB :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b001;
sign_extend <= 1'bx;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b000;
ReadR1 <= 2'b00;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_SUBI1 :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b001;
sign_extend <= 1'b1;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b010;
ReadR1 <= 2'b01;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_SUBI2 :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b001;
sign_extend <= 1'b0;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b010;
ReadR1 <= 2'b01;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_SLL :begin // NOT MADE YET
PCSrc <= 2'bxx;
ALUOp <= 3'b011;
sign_extend <= 1'b0;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b101;
ReadR1 <= 2'b01;
ReadR2 <= 1'bx;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_SRL :begin // NOT MADE YET
PCSrc <= 2'bxx;
ALUOp <= 3'b010;
sign_extend <= 1'b0;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b101;
ReadR1 <= 2'b01;
ReadR2 <= 1'bx;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_SRA :begin // NOT MADE YET
PCSrc <= 2'bxx;
ALUOp <= 3'b111;
sign_extend <= 1'b0;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b101;
ReadR1 <= 2'b01;
ReadR2 <= 1'bx;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_NAND :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b100;
sign_extend <= 1'bx;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b000;
ReadR1 <= 2'b00;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_NANDI :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b100;
sign_extend <= 1'b1;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b010;
ReadR1 <= 2'b01;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_OR :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b101;
sign_extend <= 1'bx;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b000;
ReadR1 <= 2'b00;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_ORI :begin
PCSrc <= 2'bxx;
ALUOp <= 3'b101;
sign_extend <= 1'b1;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b010;
ReadR1 <= 2'b01;
ReadR2 <= 1'b0;
RegWriteDst <= 1'b1;
MemToReg <= 1'bx;
PCBEqCond <= 1'b0;
PCBNqCond <= 1'b0;
PCWrite <= 1'b0;
MemWrite <= 1'b0;
MemRead <= 1'b0;
IRWrite <= 1'b0;
RegWrite <= 1'b0;
end
EX_BEQ :begin
PCSrc <= 2'b10;
ALUOp <= 3'b001;
sign_extend <= 1'bx;
ALUSrcA <= 1'b1;
ALUSrcB <= 3'b000;
ReadR1 <= 2'b00;
ReadR2 <= 1'b0;