forked from louay-hesham/MultiCycleMIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maindecoder.sv
147 lines (132 loc) · 3.09 KB
/
maindecoder.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
module maindec( input logic clk, reset,
input logic [5:0] op,
output logic IorD, IRwrite, memwrite, memtoreg,
output logic branch, pcwrite, regwrite, regdst,
output logic alusrcA,
output logic [1:0] alusrcB, aluop, pcsrc);
logic [14:0] controlWord;
logic [3:0] state;
initial state = 4'b0000;
always_ff @(posedge clk)
if (~reset)
begin
case (state)
4'b0000: assign state = 4'b0001;
4'b0001:
begin
case (op)
6'b000000: assign state = 4'b0110; // RTYPE
6'b100011: assign state = 4'b0010; // LW
6'b101011: assign state = 4'b0010; // SW
6'b000100: assign state = 4'b1000; // BEQ
6'b001000: assign state = 4'b1001; // ADDI
6'b000010: assign state = 4'b1011; // J
default: assign state = 4'bzzzz; // illegal op
endcase
end
4'b0010:
begin
case (op)
6'b100011: assign state = 4'b0011; // LW
6'b101011: assign state = 4'b0101; // SW
endcase
end
4'b0011: assign state = 4'b0100;
4'b0100: assign state = 4'b0000;
4'b0101: assign state = 4'b0000;
4'b0110: assign state = 4'b0111;
4'b0111: assign state = 4'b0000;
4'b1000: assign state = 4'b0000;
4'b1001: assign state = 4'b1010;
4'b1010: assign state = 4'b0000;
4'b1011: assign state = 4'b0000;
endcase
end
always_comb
begin
case(state)
4'b0000: //fetch
begin
assign IorD = 0;
assign alusrcA = 0;
assign alusrcB = 2'b01;
assign aluop = 2'b00;
assign pcsrc = 2'b00;
assign IRwrite = 1;
assign pcwrite = 1;
assign regwrite = 0;
assign branch = 0;
assign memwrite = 0;
assign regdst = 0;
assign memtoreg = 0;
end
4'b0001: //decode
begin
assign alusrcA = 0;
assign alusrcB = 2'b11;
assign aluop = 2'b00;
assign IRwrite = 0;
assign pcwrite = 0;
end
4'b0010: //MemAdr (SW and LW)
begin
assign alusrcA = 1;
assign alusrcB = 2'b10;
assign aluop = 2'b00;
end
4'b0011:
begin //MemRead (LW)
assign IorD = 1;
end
4'b0100: //WriteBack (LW)
begin
assign regdst = 0;
assign memtoreg = 1;
assign regwrite = 1;
end
4'b0101: //MemWrite (SW)
begin
assign IorD = 1;
assign memwrite = 1;
end
4'b0110: //Execute (R-type)
begin
assign alusrcA = 1;
assign alusrcB = 2'b00;
assign aluop = 2'b10;
end
4'b0111: //Write back (R-type)
begin
assign regdst = 1;
assign memtoreg = 0;
assign regwrite = 1;
end
4'b1000: //Branch (BEQ)
begin
assign alusrcA = 1;
assign alusrcB = 2'b00;
assign aluop = 2'b01;
assign pcsrc = 2'b01;
assign branch = 1;
end
4'b1001: //Execute (ADDI)
begin
assign alusrcA = 1;
assign alusrcB = 2'b10;
assign aluop = 2'b00;
end
4'b1010: //Writeback (ADDI)
begin
assign regdst = 0;
assign memtoreg = 0;
assign regwrite = 1;
end
4'b1011: //Jump
begin
assign pcsrc = 2'b10;
assign pcwrite = 1;
end
endcase
assign controlWord = {pcwrite, memwrite, IRwrite, regwrite, alusrcA, branch, IorD, memtoreg, regdst, alusrcB, pcsrc, aluop};
end
endmodule