-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol_unit.v
172 lines (159 loc) · 3.62 KB
/
control_unit.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
module control_unit(input[5:0] opCode,
input clk, reset,
output reg[1:0] ALUOp,
output reg PCWriteCond,
output reg[1:0] ALUSrcB, PCSource,
output reg PCWrite, IorD, MemWrite, MemtoReg, IRWrite, ALUSrcA, RegWrite, RegDst);
// states
localparam FETCH = 3'd0,
DECODE = 3'd1,
EXECUTE = 3'd2,
INCREMENT_PC = 3'd3,
INCREMENT_PC_EXECUTE = 3'd4,
BRANCH = 3'd5,
END_STATE = 3'd6,
BRANCH_EXECUTE = 3'd7;
// opcodes
localparam
R_TYPE = 6'b000000,
ADDI = 6'b001000,
BEQ = 6'b000100,
J = 6'b000010,
END = 6'b111111;
// ALUOps
localparam
R_OP = 2'b00,
ADD = 2'b01,
SUB = 2'b10;
reg [4:0] current_state, next_state;
initial begin
current_state = FETCH;
end
always@(*)
begin // state_table
case (current_state)
FETCH: next_state = DECODE;
DECODE: next_state = EXECUTE;
EXECUTE: begin
case (opCode)
J: next_state = FETCH;
BEQ: next_state = BRANCH;
END: next_state = END_STATE;
default: next_state = INCREMENT_PC;
endcase
end
INCREMENT_PC: next_state = INCREMENT_PC_EXECUTE;
BRANCH: next_state = BRANCH_EXECUTE;
BRANCH_EXECUTE: next_state = INCREMENT_PC;
END_STATE: next_state = END_STATE;
default: next_state = FETCH;
endcase
end
// assign output based on current state
always @(*)
begin: enable_signals
// By default make all our signals 0
ALUOp = 2'b00;
PCWriteCond = 1'b0;
PCWrite = 1'b0;
IorD = 1'b0;
MemWrite = 1'b0;
MemtoReg = 1'b0;
IRWrite = 1'b0;
ALUSrcB = 2'b0;
ALUSrcA = 1'b0;
RegWrite = 1'b0;
RegDst = 1'b0;
PCSource = 2'b00;
case (current_state)
FETCH: begin // get the instruction into the IR
IRWrite = 1'b1;
IorD = 1'b0;
end
DECODE: begin // decode the instruction and prepare the values
case(opCode)
R_TYPE: begin
ALUSrcA = 1'b1;
ALUSrcB = 2'b00;
RegDst = 1'b1;
ALUOp = R_OP;
end
ADDI: begin
ALUSrcA = 1'b1;
ALUSrcB = 2'b10;
RegDst = 1'b0;
ALUOp = ADD;
end
BEQ: begin
ALUSrcA = 1'b0;
ALUSrcB = 2'b10;
ALUOp = ADD;
end
J: begin
PCSource = 2'b10;
end
endcase
end
EXECUTE: begin // get the values to where they belong, ie. their correct registers
case(opCode)
R_TYPE: begin
ALUSrcA = 1'b1;
ALUSrcB = 2'b00;
RegDst = 1'b1;
ALUOp = R_OP;
RegWrite = 1'b1;
end
ADDI: begin
ALUSrcA = 1'b1;
ALUSrcB = 2'b10;
RegDst = 1'b0;
ALUOp = ADD;
RegWrite = 1'b1;
end
BEQ: begin
ALUSrcA = 1'b1;
ALUSrcB = 2'b00;
PCSource = 2'b01;
PCWriteCond = 1'b1;
ALUOp= SUB;
end
J: begin
PCSource = 2'b10;
PCWrite = 1'b1;
end
endcase
end
INCREMENT_PC: begin
ALUSrcA = 1'b0;
ALUSrcB = 2'b01;
ALUOp = ADD;
end
INCREMENT_PC_EXECUTE: begin
ALUSrcA = 1'b0;
ALUSrcB = 2'b01;
ALUOp = ADD;
PCWrite = 1'b1;
end
BRANCH: begin
ALUSrcA = 1'b1;
ALUSrcB = 2'b00;
PCSource = 2'b01;
ALUOp = SUB;
PCWriteCond = 1'b1;
end
BRANCH_EXECUTE: begin
ALUSrcA = 1'b1;
ALUSrcB = 2'b00;
PCSource = 2'b01;
PCWrite = 1'b0;
// PCWriteCond = 1'b1;
ALUOp = SUB;
end
// default: // don't need default since we already made sure all of our outputs were assigned a value at the start of the always block
endcase
end
always @(posedge clk)
begin
current_state <= next_state;
end
endmodule