-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSM.vhd
125 lines (112 loc) · 4.25 KB
/
FSM.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
LIBRARY ieee;
USE ieee.numeric_std.ALL;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_unsigned.ALL;
ENTITY fsm IS
PORT (
clk : IN STD_LOGIC;
reset : IN STD_LOGIC;
flag_zero : IN STD_LOGIC;
flag_negative : IN STD_LOGIC;
decoder : IN STD_LOGIC_VECTOR(3 DOWNTO 0);
count_load : OUT STD_LOGIC;
enable_ula : OUT STD_LOGIC
);
END ENTITY fsm;
ARCHITECTURE behavior OF fsm IS
-- declara os estados e inicia no inicial
TYPE states IS (inicial, jumps, opera);
SIGNAL state : states;
SIGNAL next_state : states := inicial;
BEGIN
PROCESS (clk, reset)
BEGIN
IF reset = '1' THEN
state <= inicial;
ELSIF rising_edge(clk) THEN
state <= next_state;
END IF;
END PROCESS;
-- transições da fsm
PROCESS (clk)
BEGIN
CASE state IS
WHEN inicial =>
-- se o decoder for 0000, 0001, 0010 ou 0011, vai para o estado de opera
IF decoder = "0000" OR decoder = "0001" OR decoder = "0010" OR decoder = "0011" THEN
next_state <= opera;
----------------------------------------
-- jumps
----------------------------------------
-- se o decoder for 0100, jump normal (flag zero = 0 e flag negative = 0)
ELSIF decoder = "0100" THEN
next_state <= jumps;
-- se o decoder for 0101, jump N, flag zero = 1
ELSIF decoder = "0101" THEN
-- se flag negativo = 1, vai para o estado de jumps se nao fica no estado inicial
IF flag_negative = '1' THEN
next_state <= jumps;
ELSE
next_state <= inicial;
END IF;
ELSIF decoder = "0110" THEN
-- se flag zero = 0, vai para o estado de jumps se nao fica no estado inicial
IF flag_zero = '1' THEN
next_state <= jumps;
ELSE
next_state <= inicial;
END IF;
ELSE
next_state <= inicial;
END IF;
WHEN opera =>
-- se o decoder for 0000, 0001, 0010 ou 0011, vai para o estado de opera
IF decoder = "0000" OR decoder = "0001" OR decoder = "0010" OR decoder = "0011" THEN
next_state <= opera;
----------------------------------------
-- jumps
----------------------------------------
-- se o decoder for 0100, jump normal (flag zero = 0 e flag negative = 0)
ELSIF decoder = "0100" THEN
next_state <= jumps;
-- se o decoder for 0101, jump N, flag zero = 1
ELSIF decoder = "0101" THEN
-- se flag negativo = 1, vai para o estado de jumps se nao fica no estado inicial
IF flag_negative = '1' THEN
next_state <= jumps;
ELSE
next_state <= inicial;
END IF;
ELSIF decoder = "0110" THEN
-- se flag zero = 0, vai para o estado de jumps se nao fica no estado inicial
IF flag_zero = '1' THEN
next_state <= jumps;
ELSE
next_state <= inicial;
END IF;
ELSE
next_state <= inicial;
END IF;
WHEN OTHERS =>
next_state <= inicial;
END CASE;
END PROCESS;
-- saidas da fsm
PROCESS (state)
BEGIN
CASE state IS
WHEN inicial =>
count_load <= '1';
enable_ula <= '0';
WHEN opera =>
count_load <= '1';
enable_ula <= '1';
WHEN jumps =>
count_load <= '0';
enable_ula <= '0';
WHEN OTHERS =>
count_load <= '0';
enable_ula <= '0';
END CASE;
END PROCESS;
END ARCHITECTURE behavior;