-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.vhd
More file actions
294 lines (287 loc) · 6.92 KB
/
control.vhd
File metadata and controls
294 lines (287 loc) · 6.92 KB
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
library ieee;
use ieee.std_logic_1164.all;
entity control is
port( Clk, mClk : in std_logic;
enable : in std_logic;
statusC, statusZ : in std_logic;
inst : in std_logic_vector(31 downto 0);
A_Mux, B_Mux : out std_logic;
Im_Mux1, Reg_Mux : out std_logic;
Im_Mux2, Data_Mux : out std_logic_vector(1 downto 0);
ALU_op : out std_logic_vector(2 downto 0);
Clr_A, Ld_A : out std_logic;
Clr_B, Ld_B : out std_logic;
Clr_C, Ld_C : out std_logic;
Clr_Z, Ld_Z : out std_logic;
Inc_PC, Ld_PC : out std_logic;
Clr_IR, Ld_IR : out std_logic;
T : out std_logic_vector(2 downto 0);
wen, en : out std_logic);
end control;
architecture logic of control is
type state_type is (T0, T1, T2);
signal current_state, next_state : state_type;
begin
----------OPERATION DECODER----------
process(current_state, next_state, inst, statusC, statusZ, enable)
begin
Clr_A <= '0';
Ld_A <= '0';
Clr_B <= '0';
Ld_B <= '0';
Clr_C <= '0';
Ld_C <= '0';
Clr_Z <= '0';
Ld_Z <= '0';
Inc_PC <= '0';
Ld_PC <= '0';
Clr_IR <= '0';
Ld_IR <= '0';
A_Mux <= '0';
B_Mux <= '0';
Reg_Mux <= '0';
Im_Mux1 <= '0';
Im_Mux2 <= "00";
Data_Mux <= "00";
ALU_op <= "000";
if enable = '1' then
----------FETCH NEXT INSTRUCTION----------
if current_state = T0 then
Ld_IR <= '1';
----------INCREMENT PC----------
elsif current_state = T1 then
Inc_PC <= '1';
Ld_PC <= '1';
case inst(31 downto 28) is
when x"2" => -- STA
Reg_Mux <= '0'; -- REG A
Data_Mux <= "00";
when x"3" => -- STB
Reg_Mux <= '1'; -- REG B
Data_Mux <= "00";
when x"9" => -- LDA
Ld_A <= '1';
A_Mux <= '0'; -- REG A
Data_Mux <= "01";
when x"A" => -- LDB
Ld_B <= '1';
B_Mux <= '0'; -- REG B
Data_Mux <= "01";
when others =>
null;
end case;
elsif current_state = T2 then
case inst(31 downto 28) is
when x"0" => -- LDAI
Ld_A <= '1';
A_Mux <= '1';
when x"1" => -- LDBI
Ld_B <= '1';
B_Mux <= '1';
when x"2" => -- STA
A_Mux <= '0';
Reg_Mux <= '0';
when x"3" => -- STB
B_Mux <= '0';
Reg_Mux <= '1';
when x"9" => -- LDA
Ld_A <= '1';
A_Mux <= '0'; -- REG A
Data_Mux <= "01";
when x"A" => -- LDB
Ld_B <= '1';
B_Mux <= '0'; -- REG B
Data_Mux <= "01";
when x"4" => -- LUI
Ld_A <= '1';
Clr_B <= '1';
A_Mux <= '0';
Im_Mux1 <= '1';
ALU_op <= "001";
Data_Mux <= "10";
when x"5" => -- JMP
Ld_PC <= '1';
when x"6" => -- BEQ
Ld_PC <= '1';
when x"8" => -- BNE
Ld_PC <= '1';
when x"7" => -- ALU INSTRUCTIONS
case inst(27 downto 24) is
when x"0" => -- ADD
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
Data_Mux <= "10";
ALU_op <= "010";
when x"1" => -- ADDI
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
Data_Mux <= "10";
ALU_op <= "010";
Im_Mux2 <= "01";
when x"2" => -- SUB
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
Data_Mux <= "10";
ALU_op <= "110";
when x"3" => -- INCA
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
Data_Mux <= "10";
ALU_op <= "010";
Im_Mux2 <= "10";
when x"4" => -- ROL
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
ALU_op <= "100";
Data_Mux <= "10";
when x"5" => -- CLRA
Clr_A <= '1';
when x"6" => -- CLRB
Clr_B <= '1';
when x"7" => -- CLRC
Clr_C <= '1';
when x"8" => -- CLRZ
Clr_Z <= '1';
when x"9" => -- ANDI
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
Data_Mux <= "10";
ALU_op <= "000";
Im_Mux2 <= "01";
when x"A" => -- TSTZ
if statusZ = '1' then
Ld_PC <= '1';
Inc_PC <= '1';
end if;
when x"B" => -- AND
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
ALU_op <= "000";
Data_Mux <= "10";
when x"C" => -- TSTC
if statusC = '1' then
Ld_PC <= '1';
Inc_PC <= '1';
end if;
when x"D" => -- ORI
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
ALU_op <= "001";
Im_Mux2 <= "01";
Data_Mux <= "10";
when x"E" => -- DECA
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
ALU_op <= "110";
Im_Mux2 <= "10";
Data_Mux <= "10";
when x"F" => -- ROR
Ld_A <= '1';
Ld_C <= '1';
Ld_Z <= '1';
ALU_op <= "101";
Data_Mux <= "10";
end case;
when others =>
null;
end case;
end if;
end if;
end process;
------------STATE MACHINE------------
process(Clk, enable)
begin
case enable is
when '1' =>
if rising_edge(Clk) then
-- Note: If you are using Quartus II's simulation tool instead of modelsim
-- Remove the comment below and change the current_states to next_states in this process below
-- current_state <= next_state;
case current_state is
when T0 =>
current_state <= T1;
when T1 =>
current_state <= T2;
when T2 =>
current_state <= T0;
end case;
end if;
when '0' =>
current_state <= T0;
end case;
end process;
T <= "001" when current_state = T0 else
"010" when current_state = T1 else
"100" when current_state = T2 else
"000";
-------DATA MEMORY INSTRUCTIONS------
process(mClk, Clk, inst)
begin
if falling_edge(mClk) then
if (current_state = T1 and Clk = '0') then
case inst(31 downto 28) is
when x"2" => -- STA
en <= '1';
wen <= '1'; -- WRITE TO MEM
when x"3" => -- STB
en <= '1';
wen <= '1'; -- WRITE TO MEM
when x"9" => -- LDA
en <= '1';
wen <= '0'; -- READ FROM MEM
when x"A" => -- LDB
en <= '1';
wen <= '0'; -- READ FROM MEM
when others =>
en <= '0';
wen <= '0';
end case;
elsif (current_state = T2 and Clk = '1') then
case inst(31 downto 28) is
when x"2" => -- STA
en <= '0';
wen <= '0'; -- WRITE TO MEM
when x"3" => -- STB
en <= '0';
wen <= '0'; -- WRITE TO MEM
when x"9" => -- LDA
en <= '0';
wen <= '0'; -- READ FROM MEM
when x"A" => -- LDB
en <= '0';
wen <= '0'; -- READ FROM MEM
when others =>
en <= '0';
wen <= '0';
end case;
elsif (current_state = T1) then
case inst(31 downto 28) is
when x"2" => -- STA
en <= '1';
wen <= '1'; -- WRITE TO MEM
when x"3" => -- STB
en <= '1';
wen <= '1'; -- WRITE TO MEM
when x"9" => -- LDA
en <= '1';
wen <= '0'; -- READ FROM MEM
when x"A" => -- LDB
en <= '1';
wen <= '0'; -- READ FROM MEM
when others =>
en <= '0';
wen <= '0';
end case;
end if;
end if;
end process;
end logic;