VHDL FSM
VHDL FSM
• Sequential circuit
1
– Output depends not just on present inputs (as in a 1
combinational circuit), but on past sequence of inputs 0 Combinational F
b digital circuit
• Stores bits, also known as having “state”
1
• In this chapter, we will: a
?
0 Sequential F
– Design a new building block, a flip-flop, that stores one bit b digital circuit
Digital Design
Copyright © 2006 2
Frank Vahid Note: Slides with animation are denoted with a small red "a" near the animated items
3.2
Example Needing Bit Storage
a
Need some form of “feedback” in the circuit
3. Cancel button pressed – light turns off
Digital Design
Copyright © 2006 3
Frank Vahid
Clocks
Digital Design
Copyright © 2006 4
Frank Vahid
3.3
Finite-State Machines (FSMs) and Controllers
clk
• Example: Laser timer
patient
– Push button: x=1 for 3 clock cycles
Digital Design
Copyright © 2006 5
Frank Vahid
Need a Better Way to Design Sequential Circuits
Digital Design
Copyright © 2006 6
Frank Vahid
Describing Behavior of Sequential Circuit: FSM
• Two states: “Off” (x=0), and “On” (x=1) cycle 1 cycle 2 cycle 3 cycle 4
clk
• Transition from Off to On, or On to Off,
on rising clock edge
state Off On Off On
Digital Design
Copyright © 2006 7
Frank Vahid
FSM Example: 0,1,1,1,repeat
Outputs: x
• Want 0, 1, 1, 1, 0, 1, 1, 1, ...
x=0 clk^ x=1 clk^ x=1 clk^ x=1
– Each value for one clock cycle
Off On1 On2 On3
• Can describe as FSM
– Four states clk^
clk
State Off On1On2On3 Off On1On2 On3 Off
Outputs:
x
Digital Design
Copyright © 2006 8
Frank Vahid
Extend FSM to Three-Cycles High Laser Timer
Inputs: b; Outputs: x
• Four states
x=0
clk^
Off b’*clk^
• Wait in “Off” state while b is 0 (b’)
b*clk^
x=1 clk^ x=1 clk^ x=1
• When b is 1 (and rising clock edge),
transition to On1 On1 On2 On3
– Sets x=1
Inputs:
• So x=1 for three cycles after button b
pressed
State Off Off Off Off Off On1 On2 On3 Off
Outputs:
x
Digital Design
Copyright © 2006 9
Frank Vahid
FSM Simplification: Rising Clock Edges Implicit
Inputs: b; Outputs: x
• Showing rising clock on every x=0
transition: cluttered and clk^
unnecessary Off b’ *clk^
– Set of transitions
• Describes next states We often draw FSM graphically,
• Eg: Has 5 transitions known as state diagram
– Set of actions
• Sets outputs while in states Can also use table (state table), or
textual languages
• Eg: x=0, x=1, x=1, and x=1
Digital Design
Copyright © 2006 11
Frank Vahid
FSM Example: Secure Car Key
a.k.a., A Sequencer
Digital Design
Copyright © 2006 12
Frank Vahid
FSM Example: Secure Car Key (cont.)
Inputs: a; Outputs: r
clk clk
Inputs Inputs
a
a
State Wait Wait K1 K2 K3 K4 Wait Wait State Wait Wait K1 K2 K3 K4 Wait
a
Outputs Output
r r
Digital Design
Copyright © 2006 13
Frank Vahid
FSM Example: Code Detector
Inputs: s,r,g,b,a;
Outputs: u
Wait
u=0 s s’ ar’ ab’ ag’ ar’ a
Start
a’
u=0
ar
ab ag ar
Red1 Blue Green Red2
a’ a’ a’
u=0 u=0 u=0 u=1 Note: small problem still
remains; we’ll discuss later
– Input b, output x
outputs
inputs
b x
FSM
FSM
– Known as controller
Combinational n1
logic
inputs
outputs
O
FSM
I FSM n0
Combinational
s1 s0
logic
S clk State register
a
m
m-bit m
clk
state register
Digital Design N
Copyright © 2006 16
Frank Vahid General version
State Diagram Example
Modify the laser pulse generator state diagram (below left), so that the new
FSM will only produce one pulse (3 clk cycles wide) for each actuation of ‘b’
... or stated another way, one pulse out for one pulse in.
b
x=1 x=1 x=1
On1 On2 On3
On1 On2 On3
Digital Design
Copyright © 2006 17
Frank Vahid
3.4
Controller Design
Digital Design
Copyright © 2006 18
Frank Vahid
Controller Design: Laser Timer Example
– Input b, output x
outputs
inputs
b x
FSM
FSM
– Next state signals n1, n0 Combinational n1
logic
n0
• Step 3: Encode the states s1 s0 a
Digital Design
Copyright © 2006 19
Frank Vahid
Controller Design: Laser Timer Example (cont)
outputs
inputs
x
FSM
b
FSM
Combinational n1
logic
n0
s1 s0
clk State register
Digital Design
Copyright © 2006 20
Frank Vahid
Controller Design: Laser Timer Example (cont)
• Step 5: Implement
outputs
inputs
x
FSM
b
FSM
combinational logic Combinational n1
logic
n0
a
s1 s0
clk State register
Digital Design
Copyright © 2006 21
Frank Vahid
Controller Design: Laser Timer Example (cont)
• Step 5: Implement b
Combinational Logic
x
outputs
combinational logic (cont)
inputs
x
FSM
b
FSM
Combinational n1
logic n1 a
n0
s1 s0
clk State register
n0
s1 s0
x = s1 + s0
n1 = s1’s0 + s1s0’
n0 = s1’s0’b + s1s0’
Digital Design
Copyright © 2006 22
Frank Vahid
Understanding the Controller’s Behavior
x=0 x=0 x=0
00 b’ 00 b’ 00 b’
Off Off Off
b b b
x=1 x=1 x=1 x=1 x=1 x=1 x=1 x=1 x=1
01 On1 10 On2 11 On3 01 On1 10 On2 11 On3 01 On1 10 On2 11 On3
b x b x b x
0 0 0
0 0 0 1 0 0 1 1 1
0 0 1
n1 n1 n1
0 0 1
0 0 0 a
0 1 0
n0 n0 n0
0 1 0
0 0 0
s1 s0 s1 s0 s1 s0
clk clk clk
0 0 0 0 0 1
0 0 0 1 1 0
Inputs:
b
Outputs:
x
Digital Design
Copyright © 2006 23
Frank Vahid
Controller Example:
Button Press Synchronizer
cycle1 cycle2 cycle3 cycle4
clk
Inputs:
bi
Button press
bi bo
synchronizer Outputs:
controller
bo
Digital Design
Copyright © 2006 24
Frank Vahid
Controller Example:
Button Press Synchronizer (cont)
outputs
inputs
bi bo Step 2: Create architecture
FSM
FSM
FSM inputs: bi; FSM outputs: bo
Combinational
bi’ logic n1
bi
bi’ n0
A bi B bi C bi’ s1 s0 n1 = s1’s0bi + s1s0bi a
n0 = s1’s0’bi
bo=0 bo=1 bo=0 State register bo = s1’s0bi’ + s1’s0bi = s1s0
clk
Combinational logic
Step 1: FSM bo
Combinational logic bi
Inputs Outputs
s1 s0 bi n1 n0 bo n1
FSM inputs: bi; FSM outputs: bo 0 0 0 0 0 0
A
0 0 1 0 1 0
bi’ 0 1 0 0 0 1 n0
bi B
bi’ 0 1 1 1 0 1
00 bi 01 bi 10 bi’ 1 0 0 0 0 0 s1 s0
C
bo=0 bo=1 bo=0 1 0 1 1 0 0
State register
1 1 0 0 0 0 clk
Step 3: Encode states unused
1 1 1 0 0 0
a
K1 K2 K3 K4
r=1 r=1 r=0 r=1
a r
Combinational
n2
Step 2
logic
n1
n0
s2 s1 s0
clk State register
Inputs: a; Outputs: r
000
r=0 a’
a
Step 3
states
n1=(s1 xor s0)x D C
x y
n0=(s1’*s0’)x
z Outputs:y, z
A B states
n1 yz=10 yz=10
with
D C outputs
n0 yz=00 yz=01
s1 s0
Inputs: x; Outputs:y, z
State register
clk x
x’ A B yz=10
yz=10 x’ x
Work backwards D x’ C yz=01
yz=00
Pick any state names you want x
states with
outputs and
Digital Design transitions
Copyright © 2006 28
Frank Vahid
Common Pitfalls Regarding Transition Properties
a
• Only one condition should be b
true ab=11 –
– For all transitions leaving a next state? a
state
Digital Design
Copyright © 2006 29
Frank Vahid
Defining the FSM using VHDL
• Using the model put forth by the author … there will be two process
statements
– Combinational logic (next state logic and output logic combined)
– State register
process (clk)
begin
if (clk’event and clk=‘1’) then
cs <= ns;
end if;
end process;
• Another benefit for using enumerated types for naming states is when designing the
combinatorial logic block
Digital Design
Copyright © 2006 32
Frank Vahid
FSM w/ VHDL (cont.)
process(i, cs)
begin
• Setup combinatorial logic block in a case cs is
process statement and a case when init =>
statement: if i='0' then ns <= init;
else ns <= rising;
end if;
zr <= ‘0’; zf <= ‘0’;
Inputs: i; Outputs: z = zr,zf when rising =>
if i='0' then ns <=falling;
z = 00 else ns <= wait4falling;
end if;
init i’ zr <= ‘1’; zf <= ‘0’;
when wait4falling =>
i if i='0' then ns <= falling;
z=10 i i' z=01
z=00 else ns <= wait4falling;
end if;
rising wait4falling falling zr <= ‘0’; zf <= ‘0’;
when falling =>
ns <= init;
i zr <= ‘0’; zf <= ‘1’;
when others =>
i' ns <= init;
zr <= ‘0’; zf <= ‘0’;
end case;
end process;
Digital Design
Copyright © 2006 33
Frank Vahid