Lecture 8 FSM
Lecture 8 FSM
5
Programmable vs. Non-Programmable Controller
6
Finite State Machines
• Digital Systems and especially their Controllers
can be described as Finite State Machines
(FSMs)
• Finite State Machines can be represented using
• State Diagrams and State Tables - suitable
for simple digital systems with a relatively few
inputs and outputs
• Algorithmic State Machine (ASM) Charts -
suitable for complex digital systems with a
large number of inputs and outputs
• All these descriptions can be easily translated to
the corresponding synthesizable VHDL code
7
Hardware Design with RTL VHDL
Pseudocode Interface
Datapath Controller
Block Block State diagram
diagram diagram or ASM chart
Output Outputs
function
13
Mealy FSM
• Output Is a Function of a Present State and
Inputs
Inputs Next State
function
Next State Present State
clock Present State
reset register
Output Outputs
function
14
State Diagrams
transition
condition 1
state 1 / state 2 /
output 1 output 2
transition
condition 2
16
Mealy Machine
transition condition 1 /
output 1
state 1 state 2
transition condition 2 /
output 2
17
Moore vs. Mealy FSM (1)
18
Moore vs. Mealy FSM (2)
19
Moore FSM - Example 1
• Moore FSM that Recognizes Sequence “10”
0 1
0
1
S0 / 0 S1 / 0 1 S2 / 1
reset
0
S0: No S1: “1” S2: “10”
Meaning elements observed observed
of states: of the
sequence
observed
20
Mealy FSM - Example 1
• Mealy FSM that Recognizes Sequence “10”
0/0 1/0 1/0
S0 S1
reset 0/1
S0: No S1: “1”
Meaning elements observed
of states: of the
sequence
observed
21
Moore & Mealy FSMs – Example 1
clock
0 1 0 0 0
input
S0 S1 S2 S0 S0
Moore
S0 S1 S0 S0 S0
Mealy
22
Finite State Machines
in VHDL
25
Mealy FSM
process(clock, reset)
Inputs Next State
function
Next State Present State
clock Present State
reset Register
Output Outputs
concurrent function
statements
26
Moore FSM - Example 1
• Moore FSM that Recognizes Sequence “10”
0 1
0
1
S0 / 0 S1 / 0 1 S2 / 1
reset
0
27
Moore FSM in VHDL (1)
TYPE state IS (S0, S1, S2);
SIGNAL Moore_state: state;
28
Moore FSM in VHDL (2)
WHEN S1 =>
IF input = ‘0’ THEN
Moore_state <= S2;
ELSE
Moore_state <= S1;
END IF;
WHEN S2 =>
IF input = ‘0’ THEN
Moore_state <= S0;
ELSE
Moore_state <= S1;
END IF;
END CASE;
END IF;
END PROCESS;
29
Mealy FSM - Example 1
S0 S1
reset 0/1
30
Mealy FSM in VHDL (1)
TYPE state IS (S0, S1);
SIGNAL Mealy_state: state;
31
Mealy FSM in VHDL (2)
WHEN S1 =>
IF input = ‘0’ THEN
Mealy_state <= S0;
ELSE
Mealy_state <= S1;
END IF;
END CASE;
END IF;
END PROCESS;
Output <= ‘1’ WHEN (Mealy_state = S1 AND input = ‘0’) ELSE ‘0’;
32
Moore FSM – Example 2: State diagram
Reset
w = 1
w = 0 A z = 0 B z = 0
w = 0
w = 0 w = 1
C z = 1
w = 1
33
Moore FSM – Example 2: State table
A A B 0
B A C 0
C A C 1
34
Example 2: VHDL code (1)
USE ieee.std_logic_1164.all ;
ENTITY simple IS
PORT ( clock : IN STD_LOGIC ;
resetn : IN STD_LOGIC ;
w : IN STD_LOGIC ;
z : OUT STD_LOGIC ) ;
END simple ;
35
Example 2: VHDL code (2)
CASE y IS
WHEN A =>
IF w = '0' THEN
y <= A ;
ELSE
y <= B ;
END IF ;
WHEN B =>
IF w = '0' THEN
y <= A ;
ELSE
y <= C ;
END IF ;
WHEN C =>
IF w = '0' THEN
y <= A ;
ELSE
y <= C ;
END IF ;
END CASE ;
36
Example 2: VHDL code (3)
END IF ;
END PROCESS ;
END Behavior ;
37
Mealy FSM – Example 3: State diagram
Reset
w = 1 z = 0
w = 0 z = 0 A B w = 1 z = 1
w = 0 z = 0
38
Example 3: VHDL code (1)
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY Mealy IS
PORT ( clock : IN STD_LOGIC ;
resetn : IN STD_LOGIC ;
w : IN STD_LOGIC ;
z : OUT STD_LOGIC ) ;
END Mealy ;
39
Example 3: VHDL code (2)
CASE y IS
WHEN A =>
IF w = '0' THEN
y <= A ;
ELSE
y <= B ;
END IF ;
WHEN B =>
IF w = '0' THEN
y <= A ;
ELSE
y <= B ;
END IF ;
END CASE ;
40
Example 3: VHDL code (3)
END IF ;
END PROCESS ;
END Behavior ;
41