-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_unit.vhd
342 lines (309 loc) · 10.2 KB
/
control_unit.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
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
use ieee.std_logic_unsigned.all;
entity control_unit is
port(
clk : in std_logic;
rst : in std_logic;
CCR_Result : in std_logic_vector(3 downto 0);
IR : in std_logic_vector(7 downto 0);
-- Outputlar:
IR_Load : out std_logic; -- Komut register'i yükle kontrol
MAR_Load : out std_logic;
PC_Load : out std_logic;
PC_Inc : out std_logic;
A_Load : out std_logic;
B_Load : out std_logic;
ALU_Sel : out std_logic_vector(2 downto 0);
CCR_Load : out std_logic;
BUS1_Sel : out std_logic_vector(1 downto 0);
BUS2_Sel : out std_logic_vector(1 downto 0);
write_en : out std_logic
);
end control_unit;
architecture arch of control_unit is
type state_type is (
S_FETCH_0, S_FETCH_1, S_FETCH_2, S_DECODE_3,
S_LDA_IMM_4, S_LDA_IMM_5, S_LDA_IMM_6,
S_LDA_DIR_4, S_LDA_DIR_5, S_LDA_DIR_6, S_LDA_DIR_7, S_LDA_DIR_8,
S_LDB_IMM_4, S_LDB_IMM_5, S_LDB_IMM_6,
S_LDB_DIR_4, S_LDB_DIR_5, S_LDB_DIR_6, S_LDB_DIR_7, S_LDB_DIR_8,
S_STA_DIR_4, S_STA_DIR_5, S_STA_DIR_6, S_STA_DIR_7,
S_ADD_AB_4,
S_BRA_4, S_BRA_5, S_BRA_6,
S_BEQ_4, S_BEQ_5, S_BEQ_6, S_BEQ_7
);
signal current_state, next_state : state_type;
-- Tum komutlar:
-- Kaydet/Yukle komutlari
constant YUKLE_A_SBT :std_logic_vector(7 downto 0) := x"86";
constant YUKLE_A :std_logic_vector(7 downto 0) := x"87";
constant YUKLE_B_SBT :std_logic_vector(7 downto 0) := x"88";
constant YUKLE_B :std_logic_vector(7 downto 0) := x"89";
constant KAYDET_A :std_logic_vector(7 downto 0) := x"96";
constant KAYDET_B :std_logic_vector(7 downto 0) := x"97";
-- ALU Komutlari
constant TOPLA_AB :std_logic_vector(7 downto 0) := x"42";
constant CIKAR_AB :std_logic_vector(7 downto 0) := x"43";
constant AND_AB :std_logic_vector(7 downto 0) := x"44";
constant OR_AB :std_logic_vector(7 downto 0) := x"45";
constant ARTTIR_A :std_logic_vector(7 downto 0) := x"46";
constant ARTTIR_B :std_logic_vector(7 downto 0) := x"47";
constant DUSUR_A :std_logic_vector(7 downto 0) := x"48";
constant DUSUR_B :std_logic_vector(7 downto 0) := x"49";
-- Atlama komutlari (Kosullu/Kosulsuz)
constant ATLA :std_logic_vector(7 downto 0) := x"20";
constant ATLA_NEGATIFSE :std_logic_vector(7 downto 0) := x"21";
constant ATLA_POZITIFSE :std_logic_vector(7 downto 0) := x"22";
constant ATLA_ESITSE_SIFIR :std_logic_vector(7 downto 0) := x"23";
constant ATLA_DEGILSE_SIFIR :std_logic_vector(7 downto 0) := x"24";
constant ATLA_OVERFLOW_VARSA :std_logic_vector(7 downto 0) := x"25";
constant ATLA_OVERFLOW_YOKSA :std_logic_vector(7 downto 0) := x"26";
constant ATLA_ELDE_VARSA :std_logic_vector(7 downto 0) := x"27";
constant ATLA_ELDE_YOKSA :std_logic_vector(7 downto 0) := x"28";
begin
-- Current State Logic
process (clk, rst)
begin
if(rst = '1') then
current_state <= S_FETCH_0;
elsif(rising_edge(clk)) then
current_state <= next_state;
end if;
end process;
-- Next State Logic
process(current_state, IR, CCR_Result)
begin
case current_state is
when S_FETCH_0 =>
next_state <= S_FETCH_1;
when S_FETCH_1 =>
next_state <= S_FETCH_2;
when S_FETCH_2 =>
next_state <= S_DECODE_3;
when S_DECODE_3 =>
if(IR = YUKLE_A_SBT) then
next_state <= S_LDA_IMM_4;
elsif(IR = YUKLE_A) then
next_state <= S_LDA_DIR_4;
elsif(IR = YUKLE_B_SBT) then
next_state <= S_LDB_IMM_4;
elsif(IR = YUKLE_B) then
next_state <= S_LDB_DIR_4;
elsif(IR = KAYDET_A) then
next_state <= S_STA_DIR_4;
elsif(IR = TOPLA_AB) then
next_state <= S_ADD_AB_4;
elsif(IR = ATLA) then
next_state <= S_BRA_4;
elsif(IR = ATLA_ESITSE_SIFIR) then
if(CCR_Result(2) = '1') then --NZVC, Zero bilgisi 2. bitte
next_state <= S_BEQ_4;
else -- Z = '0'
next_state <= S_BEQ_7;
end if;
else
next_state <= S_FETCH_0;
end if;
---------------------------------------------------------------
when S_LDA_IMM_4 =>
next_state <= S_LDA_IMM_5;
when S_LDA_IMM_5 =>
next_state <= S_LDA_IMM_6;
when S_LDA_IMM_6 =>
next_state <= S_FETCH_0;
---------------------------------------------------------------
when S_LDA_DIR_4 =>
next_state <= S_LDA_DIR_5;
when S_LDA_DIR_5 =>
next_state <= S_LDA_DIR_6;
when S_LDA_DIR_6 =>
next_state <= S_LDA_DIR_7;
when S_LDA_DIR_7 =>
next_state <= S_LDA_DIR_8;
when S_LDA_DIR_8 =>
next_state <= S_FETCH_0;
---------------------------------------------------------------
when S_LDB_IMM_4 =>
next_state <= S_LDB_IMM_5;
when S_LDB_IMM_5 =>
next_state <= S_LDB_IMM_6;
when S_LDB_IMM_6 =>
next_state <= S_FETCH_0;
---------------------------------------------------------------
when S_LDB_DIR_4 =>
next_state <= S_LDB_DIR_5;
when S_LDB_DIR_5 =>
next_state <= S_LDB_DIR_6;
when S_LDB_DIR_6 =>
next_state <= S_LDB_DIR_7;
when S_LDB_DIR_7 =>
next_state <= S_LDB_DIR_8;
when S_LDB_DIR_8 =>
next_state <= S_FETCH_0;
---------------------------------------------------------------
when S_STA_DIR_4 =>
next_state <= S_STA_DIR_5;
when S_STA_DIR_5 =>
next_state <= S_STA_DIR_6;
when S_STA_DIR_6 =>
next_state <= S_STA_DIR_7;
when S_STA_DIR_7 =>
next_state <= S_FETCH_0;
---------------------------------------------------------------
when S_ADD_AB_4 =>
next_state <= S_FETCH_0;
---------------------------------------------------------------
when S_BRA_4 =>
next_state <= S_BRA_5;
when S_BRA_5 =>
next_state <= S_BRA_6;
when S_BRA_6 =>
next_state <= S_FETCH_0;
---------------------------------------------------------------
when S_BEQ_4 =>
next_state <= S_BEQ_5;
when S_BEQ_5 =>
next_state <= S_BEQ_6;
when S_BEQ_6 =>
next_state <= S_FETCH_0;
when S_BEQ_7 => -- Z = '0' durumu, komut bypass
next_state <= S_FETCH_0;
---------------------------------------------------------------
when others =>
next_state <= S_FETCH_0;
end case;
end process;
-- Output Logic--
process(current_state)
begin
IR_Load <= '0';
MAR_Load <= '0';
PC_Load <= '0';
PC_Inc <= '0';
A_Load <= '0';
B_Load <= '0';
ALU_Sel <= (others => '0');
CCR_Load <= '0';
BUS1_Sel <= (others => '0');
BUS2_Sel <= (others => '0');
write_en <= '0';
case current_state is
when S_FETCH_0 =>
BUS1_Sel <= "00"; -- PC
BUS2_Sel <= "01"; -- BUS1
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_FETCH_1 =>
PC_Inc <= '1';
when S_FETCH_2 =>
BUS2_Sel <= "10"; -- from memory
IR_Load <= '1';
when S_DECODE_3 =>
-- next state zaten guncellendi, ve ilgili dallanma saglandi
---------------------------------------------------------------
when S_LDA_IMM_4 =>
BUS1_Sel <= "00"; -- PC
BUS2_Sel <= "01"; -- BUS1
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_LDA_IMM_5 =>
PC_Inc <= '1';
when S_LDA_IMM_6 =>
BUS2_Sel <= "10"; -- from memory
A_Load <= '1';
---------------------------------------------------------------
when S_LDA_DIR_4 =>
BUS1_Sel <= "00"; -- PC
BUS2_Sel <= "01"; -- BUS1
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_LDA_DIR_5 =>
PC_Inc <= '1';
when S_LDA_DIR_6 =>
BUS2_Sel <= "10"; -- from memory
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_LDA_DIR_7 =>
-- BOS : Bellekten okuma yapilmasi bekleniyor.
when S_LDA_DIR_8 =>
BUS2_Sel <= "10"; -- from memory
A_Load <= '1';
---------------------------------------------------------------
when S_LDB_IMM_4 =>
BUS1_Sel <= "00"; -- PC
BUS2_Sel <= "01"; -- BUS1
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_LDB_IMM_5 =>
PC_Inc <= '1';
when S_LDB_IMM_6 =>
BUS2_Sel <= "10"; -- from memory
B_Load <= '1';
---------------------------------------------------------------
when S_LDB_DIR_4 =>
BUS1_Sel <= "00"; -- PC
BUS2_Sel <= "01"; -- BUS1
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_LDB_DIR_5 =>
PC_Inc <= '1';
when S_LDB_DIR_6 =>
BUS2_Sel <= "10"; -- from memory
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_LDB_DIR_7 =>
-- BOS : Bellekten okuma yapilmasi bekleniyor.
when S_LDB_DIR_8 =>
BUS2_Sel <= "10"; -- from memory
B_Load <= '1';
---------------------------------------------------------------
when S_STA_DIR_4 =>
BUS1_Sel <= "00"; -- PC
BUS2_Sel <= "01"; -- BUS1
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_STA_DIR_5 =>
PC_Inc <= '1';
when S_STA_DIR_6 =>
BUS2_Sel <= "10"; -- from memory
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_STA_DIR_7 =>
BUS1_Sel <= "01"; -- A_reg
write_en <= '1';
---------------------------------------------------------------
when S_ADD_AB_4 =>
BUS1_Sel <= "01"; -- A_reg
BUS2_Sel <= "00"; -- ALU result
ALU_Sel <= "000"; -- Toplama kodu ALU'daki
A_Load <= '1';
CCR_Load <= '1';
---------------------------------------------------------------
when S_BRA_4 =>
BUS1_Sel <= "00"; -- PC
BUS2_Sel <= "01"; -- BUS1
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_BRA_5 =>
-- BOS
when S_BRA_6 =>
BUS2_Sel <= "10"; -- from memory
PC_Load <= '1'; -- Program sayaci registerina BUS2 verisini al
---------------------------------------------------------------
when S_BEQ_4 =>
BUS1_Sel <= "00"; -- PC
BUS2_Sel <= "01"; -- BUS1
MAR_Load <= '1'; -- BUS2 daki program sayaci degeri MAR'a alindi
when S_BEQ_5 =>
when S_BEQ_6 =>
BUS2_Sel <= "10"; -- from memory
PC_Load <= '1'; -- Program sayaci registerina BUS2 verisini al
when S_BEQ_7 => -- Z = '0' durumu, komut bypass
PC_Inc <= '1';
---------------------------------------------------------------
when others =>
IR_Load <= '0';
MAR_Load <= '0';
PC_Load <= '0';
A_Load <= '0';
B_Load <= '0';
ALU_Sel <= (others => '0');
CCR_Load <= '0';
BUS1_Sel <= (others => '0');
BUS2_Sel <= (others => '0');
write_en <= '0';
end case;
end process;
end architecture;