-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.vhd
385 lines (344 loc) · 9.94 KB
/
controller.vhd
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
library IEEE;
use IEEE.std_logic_1164.all;
entity controller is
port (
-- databus : inout std_logic_vector(15 DOWNTO 0);
--au
ResetPC,
PCplusI,
PCplus1 ,
RplusI,
Rplus0,
EnablePC,
--ir
ir_load,
--wp
wp_add ,
wp_reset,
--flags
c_set ,
c_reset ,
z_set ,
z_reset ,
sr_load,
--tristates
rd_on_AddressUnitRSide ,
rs_on_AddressUnitRSide ,
address_on_databus ,
aluout_on_databus ,
--rf
rfl_write ,
rfh_write ,
Shadow : out std_logic;
--alu
alu_opcode : out std_logic_vector(3 downto 0);
--mem
memAddress : out std_logic_vector(15 DOWNTO 0);
--outputs
readmem,
writemem : out std_logic;
clk,
zin ,
cin,
rst : in std_logic;
ir_in : in std_logic_vector(15 downto 0)
);
end entity;
architecture rtl of controller is
type state is (reset_state, fetch_state, decode_state, decode_delay_state, no_operation_state, halt_state, set_zero_state, clear_zero_state, set_carry_state, clear_carry_state ,
clear_wp_state, move_reg_state, load_address_first_state, enable_pc_state, load_address_second_state, store_address_state, and_state, or_state, not_state, left_shift_state, right_shift_state,
add_state, sub_state, compare_state, move_immd_low_state, move_immd_high_state, save_pc_state, jump_address_state, jump_relative_state, branch_if_zero_state, branch_if_carry_state,
wp_add_state, fetch_delay_state);
signal current_state : state := reset_state;
signal next_state : state;
begin
process (clk, rst)
begin
if rst = '1' then
current_state <= reset_state;
elsif clk'event and clk = '1' then
current_state <= next_state;
end if;
end process;
process (current_state)
begin
--if (current_state /= after_exe) then
ResetPC <= '0';
PCplusI <= '0';
PCplus1 <= '0';
rplusI <= '0';
rplus0 <= '0';
EnablePC <= '0';
c_set <= '0';
c_reset <= '0';
z_set <= '0';
z_reset <= '0';
sr_load <= '0';
rfl_write <= '0';
rfh_write <= '0';
wp_add <= '0';
wp_reset <= '0';
ir_load <= '0';
address_on_databus <= '0';
aluout_on_databus <= '0';
rs_on_AddressUnitRSide <= '0';
rd_on_AddressUnitRSide <= '0';
ReadMem <= '0';
WriteMem <= '0';
alu_opcode <= "1111";
--end if ;
case current_state is
when reset_state =>
ResetPC <= '1';
EnablePC <= '1';
c_reset <= '1';
z_reset <= '1';
wp_reset <= '1';
next_state <= fetch_state;
when fetch_state =>
ReadMem <= '1';
ir_load <= '1';
next_state <= fetch_delay_state;
when fetch_delay_state =>
ReadMem <= '1';
ir_load <= '1';
next_state <= decode_state;
when decode_state =>
next_state <= decode_delay_state;
when decode_delay_state =>
if (ir_in(15 downto 8) = "00000000") then --nop
next_state <= no_operation_state;
elsif (ir_in(15 downto 8) = "00000001") then --hlt
next_state <= halt_state;
elsif (ir_in(15 downto 8) = "00000010") then --szf
next_state <= set_zero_state;
elsif (ir_in(15 downto 8) = "00000011") then --czf
next_state <= clear_zero_state;
elsif (ir_in(15 downto 8) = "00000100") then --scf
next_state <= set_carry_state;
elsif (ir_in(15 downto 8) = "00000101") then --ccf
next_state <= clear_carry_state;
elsif (ir_in(15 downto 8) = "00000110") then --cwp
next_state <= clear_wp_state;
elsif (ir_in(15 downto 8) = "00001010") then
next_state <= wp_add_state;--awp
elsif (ir_in(15 downto 12) = "0001") then
next_state <= move_reg_state;--mvr
elsif (ir_in(15 downto 12) = "0010") then
next_state <= load_address_first_state;--lda
elsif (ir_in(15 downto 12) = "0011") then
next_state <= store_address_state;--sta
elsif (ir_in(15 downto 12) = "0110") then --and
next_state <= and_state;
elsif (ir_in(15 downto 12) = "0111") then --orr
next_state <= or_state;
elsif (ir_in(15 downto 12) = "1000") then --not
next_state <= not_state;
elsif (ir_in(15 downto 12) = "1001") then --shl
next_state <= left_shift_state;
elsif (ir_in(15 downto 12) = "1010") then --shr
next_state <= right_shift_state;
elsif (ir_in(15 downto 12) = "1011") then --add
next_state <= add_state;
elsif (ir_in(15 downto 12) = "1100") then --sub
next_state <= sub_state;
elsif (ir_in(15 downto 12) = "1110") then --cmp
next_state <= compare_state;
elsif (ir_in(15 downto 12) = "1111" and ir_in(9 downto 8) = "00") then
next_state <= move_immd_low_state;--mil
elsif (ir_in(15 downto 12) = "1111" and ir_in(9 downto 8) = "01") then
next_state <= move_immd_high_state;--mih
elsif (ir_in(15 downto 12) = "1111" and ir_in(9 downto 8) = "10") then
next_state <= save_pc_state;--spc
elsif (ir_in(15 downto 12) = "1111" and ir_in(9 downto 8) = "11") then
next_state <= jump_address_state;--jpa
elsif (ir_in(15 downto 8) = "00000111") then
next_state <= jump_relative_state;--jpr
elsif (ir_in(15 downto 8) = "00001000") then
next_state <= branch_if_zero_state;--brz
elsif (ir_in(15 downto 8) = "00001001") then
next_state <= branch_if_carry_state;--brc
end if ;
when no_operation_state =>
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when halt_state =>
if (rst = '1') then
next_state <= fetch_state;
else
next_state <= halt_state;
end if;
when set_zero_state =>
z_set <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when clear_zero_state =>
z_reset <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when set_carry_state =>
c_set <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when clear_carry_state =>
c_reset <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when clear_wp_state =>
wp_reset <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when wp_add_state =>
wp_add <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when move_reg_state =>
alu_opcode <= "0000";
aluout_on_databus <= '1';
rfl_write <= '1';
rfh_write <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when load_address_first_state =>
rs_on_AddressUnitRSide <= '1';
rplus0 <= '1';
ReadMem <= '1';
next_state <= load_address_second_state;
when load_address_second_state =>
ReadMem <= '1';
rfl_write <= '1';
rfh_write <= '1';
next_state <= enable_pc_state;
when store_address_state =>
alu_opcode <= "0000";
aluout_on_databus <= '1';
rd_on_AddressUnitRSide <= '1';
rplus0 <= '1';
WriteMem <= '1';
next_state <= enable_pc_state;
when enable_pc_state =>
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when and_state =>
alu_opcode <= "0001";
aluout_on_databus <= '1';
rfl_write <= '1';
rfh_write <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when or_state =>
alu_opcode <= "0010";
aluout_on_databus <= '1';
rfl_write <= '1';
rfh_write <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when not_state =>
alu_opcode <= "1001";
aluout_on_databus <= '1';
rfl_write <= '1';
rfh_write <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when left_shift_state =>
alu_opcode <= "0100";
aluout_on_databus <= '1';
rfl_write <= '1';
rfh_write <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when right_shift_state =>
alu_opcode <= "0011";
aluout_on_databus <= '1';
rfl_write <= '1';
rfh_write <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when add_state =>
alu_opcode <= "0101";
aluout_on_databus <= '1';
sr_load <= '1';
rfl_write <= '1';
rfh_write <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when sub_state =>
alu_opcode <= "0110";
aluout_on_databus <= '1';
sr_load <= '1';
rfl_write <= '1';
rfh_write <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when compare_state =>
alu_opcode <= "0100";
sr_load <= '1';
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when move_immd_low_state =>
rplusI <= '1';
address_on_databus <= '1';
rfl_write <= '1';
next_state <= enable_pc_state;
when move_immd_high_state =>
rplusI <= '1';
address_on_databus <= '1';
rfh_write <= '1';
next_state <= enable_pc_state;
when save_pc_state =>
PCplusI <= '1';
address_on_databus <= '1';
rfh_write <= '1';
rfl_write <= '1';
next_state <= enable_pc_state;
when jump_address_state =>
rd_on_AddressUnitRSide <= '1';
rplusI <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when jump_relative_state =>
PCplusI <= '1';
EnablePC <= '1';
next_state <= fetch_state;
when branch_if_zero_state =>
if (Zin = '1') then
PCplusI <= '1';
EnablePC <= '1';
next_state <= fetch_state;
else
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
end if ;
when branch_if_carry_state =>
if (Cin = '1') then
PCplusI <= '1';
EnablePC <= '1';
next_state <= fetch_state;
else
PCplus1 <= '1';
EnablePC <= '1';
next_state <= fetch_state;
end if ;
when others =>
next_state <= fetch_state;
end case;
end process;
end architecture;