-
Notifications
You must be signed in to change notification settings - Fork 0
/
State_Machine_Example.vhd
149 lines (107 loc) · 2.58 KB
/
State_Machine_Example.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
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
Entity State_Machine_Example IS Port
(
clk_input, reset, I0, I1, I2 : IN std_logic;
output1, output2 : OUT std_logic
);
END ENTITY;
Architecture SM of State_Machine_Example is
TYPE STATE_NAMES IS (S0, S1, S2, S3, S4, S5, S6, S7); -- list all the STATE_NAMES values
SIGNAL current_state, next_state : STATE_NAMES; -- signals of type STATE_NAMES
BEGIN
-------------------------------------------------------------------------------
--State Machine:
-------------------------------------------------------------------------------
-- REGISTER_LOGIC PROCESS EXAMPLE
Register_Section: PROCESS (clk_input) -- this process updates with a clock
BEGIN
IF(rising_edge(clk_input)) THEN
IF (reset = '1') THEN
current_state <= S0;
ELSIF (reset = '0') THEN
current_state <= next_State;
END IF;
END IF;
END PROCESS;
-- TRANSITION LOGIC PROCESS EXAMPLE
Transition_Section: PROCESS (I0, I1, I2, current_state)
BEGIN
CASE current_state IS
WHEN S0 =>
IF(I0='1') THEN
next_state <= S1;
ELSE
next_state <= S0;
END IF;
WHEN S1 =>
next_state <= S2;
WHEN S2 =>
IF ((I0='1')) THEN
next_state <= S6;
ELSIF(I1='1') THEN
next_state <= S3;
ELSE
next_state <= S2;
END IF;
WHEN S3 =>
IF(I0='1') THEN
next_state <= S4;
ELSE
next_state <= S3;
END IF;
WHEN S4 =>
next_state <= S5;
WHEN S5 =>
next_state <= S6;
WHEN S6 =>
IF(I0='1') THEN
next_state <= S7;
ELSE
next_state <= S6;
END IF;
WHEN S7 =>
IF(I2='1') THEN
next_state <= S0;
ELSE
next_state <= S7;
END IF;
WHEN OTHERS =>
next_state <= S0;
END CASE;
END PROCESS;
-- DECODER SECTION PROCESS EXAMPLE (MOORE FORM SHOWN)
Decoder_Section: PROCESS (current_state)
BEGIN
CASE current_state IS
WHEN S0 =>
output1 <= '1';
output2 <= '0';
WHEN S1 =>
output1 <= '0';
output2 <= '0';
WHEN S2 =>
output1 <= '0';
output2 <= '0';
WHEN S3 =>
output1 <= '0';
output2 <= '0';
WHEN S4 =>
output1 <= '0';
output2 <= '0';
WHEN S5 =>
output1 <= '0';
output2 <= '0';
WHEN S6 =>
output1 <= '0';
output2 <= '1';
WHEN S7 =>
output1 <= '0';
output2 <= '0';
WHEN others =>
output1 <= '0';
output2 <= '0';
END CASE;
END PROCESS;
END ARCHITECTURE SM;