-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.vhd
405 lines (373 loc) · 12 KB
/
states.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity FSM is
port(clk:in std_logic;
c_flag_d2,z_flag_d2,flag_carry,flag_zero,flag_equal: in std_logic;
mem_write,c_flag_d1,c_flag_write,z_flag_d1,z_flag_write,flag_op,rf_write,t1_write,t2_write,t3_write,t4_write,instr_write:out std_logic;
mem_d2,instr_d2,t1_d2,t2_d2,t3_d2,t4_d2,alu_c,rf_d1,rf_d2, sig_pc:in std_logic_vector(15 downto 0); -- outputs
mem_a1,mem_d1,instr_d1,t1_d1,t2_d1,t3_d1,t4_d1,alu_a,alu_b,rf_d3:out std_logic_vector(15 downto 0); -- inputs
rf_a1,rf_a2,rf_a3:out std_logic_vector(2 downto 0);
state: out std_logic_vector(4 downto 0));
end entity;
architecture fsm_arc of FSM is
constant OC_ADDR : std_logic_vector(3 downto 0) :="0000"; -- ADD,ADC,ADZ
constant OC_ADDI : std_logic_vector(3 downto 0) :="0001";
constant OC_NNDR : std_logic_vector(3 downto 0) :="0010"; -- NDU,NDC,NDZ
constant OC_LHI : std_logic_vector(3 downto 0) :="0011";
constant OC_LW : std_logic_vector(3 downto 0) :="0100";
constant OC_SW : std_logic_vector(3 downto 0) :="0101";
constant OC_LM : std_logic_vector(3 downto 0) :="0110";
constant OC_SM : std_logic_vector(3 downto 0) :="0111";
constant OC_JAL : std_logic_vector(3 downto 0) :="1000";
constant OC_JLR : std_logic_vector(3 downto 0) :="1001";
constant OC_BEQ : std_logic_vector(3 downto 0) :="1100";
begin
process(clk,c_flag_d2,z_flag_d2,flag_carry,flag_zero,flag_equal,mem_d2,instr_d2,t1_d2,t2_d2,t3_d2,t4_d2,alu_c,rf_d1,rf_d2,sig_pc)
variable nextstate:std_logic_vector(4 downto 0);
variable crntstate:std_logic_vector(4 downto 0):="00000";
variable opcode:std_logic_vector(3 downto 0);
begin
case crntstate is
----------------------------------------state0------------------------------
when "00000" =>
state<=crntstate;
mem_write<='0';
c_flag_write<='0';
z_flag_write<='0';
rf_write<='0';
t1_write<='0';
t2_write<='0';
t3_write<='0';
instr_write<='0';
nextstate:="00001";
-----------------------------------------state1--------------------------------
when "00001" =>
state<=crntstate;
rf_write<='0';
rf_a3 <= "111";
instr_write<='1';
mem_a1<=sig_pc;
instr_d1<=mem_d2;
opcode:=mem_d2(15 downto 12);
nextstate:="00010";
----------------------------------------state2----------------------------------
when "00010" =>
state<=crntstate;
instr_write<='0';
rf_write<='0';
rf_a3 <= "111";
alu_a<=sig_pc;
alu_b<="0000000000000001";
flag_op<='0';
nextstate:="00011";
----------------------------------------state3----------------------------------
when "00011" =>
rf_write<='1';
rf_a3 <= "111";
rf_d3<=alu_c;
if(opcode= OC_LHI) then
nextstate:="01000";
elsif(opcode= OC_LW) then
nextstate:="01001";
elsif(opcode= OC_SW or opcode= OC_BEQ) then
nextstate:="01011";
elsif(opcode= OC_JAL) then
nextstate:="10000";
elsif(opcode = OC_LM) then
nextState:="10101";
elsif(opcode = OC_SM) then
nextState:="10101";
else
nextstate:="00100";
end if;
----------------------------------------state4----------------------------------------
when "00100" =>
state<=crntstate;
instr_write<='0';
rf_write<='0';
t1_write<='1';
t2_write<='1';
rf_a1<=instr_d2(11 downto 9);
rf_a2<=instr_d2(8 downto 6);
t1_d1<=rf_d1;
t2_d1<=rf_d2;
if(opcode = OC_ADDR) then
nextState:="00101";
elsif(opcode = OC_ADDI) then
nextState:= "00111";
elsif(opcode = OC_NNDR) then
nextState:= "00101";
elsif(opcode = OC_JLR) then
nextState:= "10000" ;
end if;
----------------------------------------state5--------------------------------------------------
when "00101" =>
state<=crntstate;
t1_write<='0';
t2_write<='0';
t3_write<='1';
alu_a<=t1_d2;
alu_b<=t2_d2;
t3_d1<=alu_c;
if(opcode= OC_ADDR) then -- only writing carry and zero signals to flagreg
flag_op<='0';
c_flag_write<='1';
z_flag_write<='1';
c_flag_d1<=flag_carry;
z_flag_d1<=flag_zero;
nextstate:="00110";
elsif(opcode= OC_NNDR) then
flag_op<='1'; -------------nand
z_flag_write<='1';
z_flag_d1<=flag_zero; -------------checking for zero flag
nextstate:="00110";
end if;
nextstate:="00001";
----------------------------------------------state6------------------------------------------------
when "00110" =>
state<=crntstate;
t3_write<='0';
c_flag_write<='0';
z_flag_write<='0';
rf_write<='1';
if(opcode= OC_ADDR or opcode= OC_NNDR) then
case instr_d2(1 downto 0) is
when "00" =>
rf_d3<=t3_d2;
rf_a3<=instr_d2(5 downto 3);
when "01" =>
if(z_flag_d2 = '0') then
nextstate:="00001";
else
rf_d3<=t3_d2;
rf_a3<=instr_d2(5 downto 3);
end if;
when "10" =>
if(c_flag_d2 = '0') then
nextstate:="00001";
else
rf_d3<=t3_d2;
rf_a3<=instr_d2(5 downto 3);
end if;
when others =>
null;
end case;
end if;
nextstate:="00001";
--------------------------------------state7------------------------------------------
when "00111" =>
state<=crntstate;
t2_write<='0';
rf_write<='1';
alu_a<=t2_d2;
alu_b<="0000000000" & instr_d2(5 downto 0);
flag_op<='0';
rf_d3<= alu_c;
rf_a3<= instr_d2(8 downto 6);
nextstate:= "00010";
--------------------------------------state8-----------------------------------------
when "01000" =>
state<=crntstate;
rf_a3<=instr_d2(11 downto 9);
rf_d3<=instr_d2(8 downto 0) & "0000000";
rf_write<='1';
nextstate:="00001";
---------------------------------------state9------------------------------------------
when "01001" =>
state<=crntstate;
t1_write<='0';
t2_write<='1';
rf_write<='0';
rf_a2<=instr_d2(8 downto 6);
t2_d1<=rf_d2;
nextstate:="01010";
--------------------------------------state10--------------------------------------------
when "01010" =>
state<=crntstate;
t1_write<='0';
t2_write<='0';
rf_a3<=instr_d2(11 downto 9);
alu_b<="0000000000" & instr_d2(5 downto 0);
alu_a<=t2_d2;
flag_op<='0';
z_flag_write<='1'; ----------modifies zflag
z_flag_d1<=flag_zero;
mem_a1<=alu_c;
rf_d3<=mem_d2;
rf_write<='1';
nextstate:="00001";
---------------------------------------state11--------------------------------------------
when "01011" =>
state<=crntstate;
rf_write<='0';
t1_write<='1';
t2_write<='1';
rf_a1<=instr_d2(11 downto 9);
rf_a2<=instr_d2(8 downto 6);
t1_d1<=rf_d1;
t2_d1<=rf_d2;
if(opcode= OC_BEQ) then
nextstate:="01101";
elsif(opcode= OC_SW) then
nextstate:="01100";
end if;
--------------------------------------state12-----------------------------------------
when "01100" =>
state<=crntstate;
t1_write<='0';
t2_write<='0';
alu_b<="0000000000" & instr_d2(5 downto 0);
alu_a<=t2_d2;
flag_op<='0';
mem_write<='1';
mem_d1<=t1_d2;
mem_a1<=alu_c;
nextstate:= "00010";
--------------------------------------state13------------------------------------------
when "01101" =>
state<=crntstate;
t1_write<='0';
t2_write<='0';
alu_a<=t1_d2;
alu_b<=t2_d2;
if(flag_equal='1') then
nextstate:="01110";
else
nextstate:="00001";
end if;
-------------------------------------state14--------------------------------------------
when "01110" =>
state<=crntstate;
rf_write<='0';
rf_a3<="111";
alu_a<=sig_pc;
alu_b<="0000000000" & instr_d2(5 downto 0);
flag_op<='0';
nextstate:="01111";
-------------------------------------state15--------------------------------------------
when "01111" =>
rf_write<='1';
rf_a3<="111";
rf_d3<=alu_c;
nextstate:="00001";
--------------------------------------state16---------------------------------------------
when "10000" =>
state<=crntstate;
instr_write<='0';
t1_write<='1';
rf_write<='0';
rf_a1<=instr_d2(11 downto 9);
t1_d1<=rf_d1;
nextstate:="10001";
--------------------------------------state17----------------------------------------------
when "10001" =>
state<=crntstate;
t1_write<='0';
alu_a<=t1_d2;
alu_b<="0000000" & instr_d2(8 downto 0);
flag_op<='0';
rf_write<='1';
rf_a3<="111";
rf_d3<=alu_c;
nextstate:="00001";
--------------------------------------state18-----------------------------------------------
when "10010" =>
state<=crntstate;
rf_write<= '0';
rf_a3<="111";
t4_write<='1';
t4_d1<=sig_pc;
nextstate:="10011";
--------------------------------------state19-----------------------------------------------
when "10011" =>
state<=crntstate;
instr_write<='0';
t4_write<= '0';
rf_write<='1';
rf_a3<=instr_d2(11 downto 9);
rf_d3<=t4_d2;
nextstate:="10100";
--------------------------------------state20-----------------------------------------------
when "10100" =>
state<=crntstate;
rf_write<='1';
rf_a3<="111";
rf_d3<=t2_d2;
nextstate:= "00010";
------------------------------------------state21-------------------------------------------
when "10101" =>
state<=crntstate;
rf_a1<=instr_d2(11 downto 9);
t1_write<='1';
t1_d1<=rf_d1;
t2_write<='1';
t2_d1<="0000000000000000";
if(opcode=OC_LM) then
nextstate:="10110"; ------22 for lm
elsif(opcode=OC_SM) then
nextstate:="11001"; ------25 for sm
end if;
----------------------------------------------state22---------------------------------------
when "10110" =>
state<=crntstate;
t1_write<='0';
t2_write<='0';
rf_write<='1';
if(instr_d2(7 downto 0)(to_integer(unsigned(t2_d2)))='1') then
mem_a1<=t1_d2;
rf_d3<=mem_d2;
rf_a3<=t2_d2(2 downto 0);
end if;
nextstate:="10111";
--------------------------------------------state23------------------------------------------
when "10111" =>
state<=crntstate;
rf_write<='0';
t1_write<='1';
alu_a<=t1_d2;
alu_b<="0000000000000001";
flag_op<='0';
t1_d1<=alu_c;
nextstate:="11000";
-------------------------------------------state24-------------------------------------------
when "11000" =>
state<=crntstate;
t1_write<='0';
alu_a<=t2_d2;
alu_b<="0000000000000001";
flag_op<='0';
t2_write<='1';
t2_d1<=alu_c;
if(t2_d2="0000000000000111") then
nextstate:="00001";
else
if(opcode=OC_LM) then
nextstate:="10110";
elsif(opcode=OC_SM) then
nextstate:="11001";
end if;
end if;
------------------------------------------state25---------------------------------------------
when "11001" =>
state<=crntstate;
t1_write<='0';
t2_write<='0';
mem_write<='1';
if(instr_d2(7 downto 0)(7-to_integer(unsigned(t2_d2)))='1') then
rf_write<='0';
rf_a1<=t2_d2(2 downto 0);
mem_d1<=rf_d1;
mem_a1<=t1_d2;
end if;
nextstate:="10111";
-----------------------------------------------------------------------------------------------
when others =>
null;
end case;
if rising_edge(clk) then
crntstate := nextstate;
end if;
end process;
end architecture;