Title: State Machine
1State Machine Timing Design
2?? ??
- Finite State Machine(FSM)
- Mealy Machine
- Moore Machine
- FSM in VHDL
- More VHDL codes for FSMs
- Techniques for simple sequential logic design
3Finite State Machines (FSMs)
- Any circuit with memory is a finite state machine
(FSM?? ?? ??) - Even computers can be viewed as huge FSMs
- Design of FSMs involves
- Defining states
- Defining transitions between states
- Optimization / minimization
- Manual optimization/minimization is practical for
small FSMs only
4Moore FSM
- Output is a function of a present state only
5Moore Machine
transition condition 1
state 2 / output 2
state 1 / output 1
transition condition 2
6Mealy FSM
- Output is a function of a present state and inputs
7Mealy Machine
transition condition 1 / output 1
state 2
state 1
transition condition 2 / output 2
8Moore vs. Mealy FSM (1)
- Moore and Mealy FSMs can be functionally
equivalent - Equivalent Mealy FSM can be derived from Moore
FSM and vice versa - Mealy FSM has richer description and usually
requires smaller number of states - Smaller circuit area
9Moore vs. Mealy FSM (2)
- Mealy FSM computes outputs as soon as inputs
change - Mealy FSM responds one clock cycle sooner than
equivalent Moore FSM - Moore FSM has no combinational path between
inputs and outputs - Moore FSM is more likely to have a shorter
critical path
10Moore FSM - Example 1
- Moore FSM that recognizes sequence 10
11Mealy FSM - Example 1
- Mealy FSM that recognizes sequence 10
12Moore Mealy FSMs Example 1
13FSMs in VHDL
- Finite state machines can be easily described
with processes - Synthesis tools understand FSM description if
certain rules are followed - State transitions should be described in a
process sensitive to clock and asynchronous reset
signals only - Outputs described as concurrent statements
outside the process
14State Machine - Mealy Machine
- Mealy Machine
- ??? ??(Current State)? ??? ??(Inputs)? ?? ??? ???
15Mealy FSM ? ?? State diagram
?? 1. WindowAct??? 0?? 1? ??? S1 state?? ??, ? ?
output RiseShot? 1?, 2. WindowAct??? 1?? 0?? ???
S0 state?? ??, FallShot? 1? ???? ?. 3. State ???
??? output?? ?? 0
16Mealy Machine ?? Process 2???
Library ieee Use ieee.std_logic_1164.all ENTITY
RiseFallShot IS PORT( clk IN STD_LOGIC res
et IN STD_LOGIC WindowAct
IN STD_LOGIC RiseShot, FallShot
OUT STD_LOGIC) END RiseFallShot ARCHITECTURE a
OF RiseFallShot IS TYPE STATE_TYPE IS (s0,
s1) SIGNAL state STATE_TYPE BEGIN PROCESS
(clk, reset) BEGIN IF reset '0' THEN
state lt s0 ELSIF clk'EVENT AND clk
'1' THEN CASE state IS WHEN s0 gt
IF WindowAct'1' THEN state lt s1
ELSE state lt s0 END
IF WHEN others gt IF WindowAct'0'
THEN state lt s0 ELSE state
lt s1 END IF END CASE END
IF END PROCESS
??? Data type STATE_TYPE ??
?? ??
Current State
Combinational Logic
F/F
Next State
Inputs
Combinational Logic
Outputs
17Mealy Machine ?? Process 2? ??
PROCESS(state, WindowAct) BEGIN if(
state s0 and WindowAct'1') then
RiseShot lt'1' else RiseShot
lt'0' end if if( state s1 and
WindowAct'0') then FallShot lt'1'
else FallShot lt'0' end
if END PROCESS END a
Current State
Combinational Logic
F/F
Next State
Inputs
Combinational Logic
Outputs
?? ??
18Mealy Machine ?? Process 3? ??
library ieee Use ieee.std_logic_1164.all ENTITY
RiseFallShot_v2 IS PORT( clk
IN STD_LOGIC reset IN STD_LOGIC WindowA
ct IN STD_LOGIC RiseShot, FallShot
OUT STD_LOGIC) END RiseFallShot_v2 ARCHITECTURE
a OF RiseFallShot_v2 IS TYPE STATE_TYPE IS (s0,
s1) SIGNAL State, NextState STATE_TYPE BEGIN
PROCESS (State, WindowAct) BEGIN CASE State
IS WHEN s0 gt IF WindowAct'1'
THEN NextState lt s1 ELSE NextState
lt s0 END IF WHEN others gt IF
WindowAct'0' THEN NextState lt
s0 ELSE NextState lt s1 END
IF END CASE END PROCESS
?? ??
Current State
Combinational Logic
F/F
Next State
Inputs
Combinational Logic
Outputs
19Mealy Machine ?? Process 3? ??
PROCESS(reset,clk) BEGIN IF reset '0'
THEN State lt s0 ELSIF clk'EVENT AND clk
'1' THEN State lt NextState END IF END
PROCESS process(State,WindowAct) begin
if( State s0 and WindowAct'1') then
RiseShot lt'1' else
RiseShot lt'0' end if if(
State s1 and WindowAct'0') then
FallShot lt'1' else
FallShot lt'0' end if end
process
?? ??
Current State
Combinational Logic
F/F
Next State
Inputs
Combinational Logic
Outputs
?? ??
20State Machine - Moore Machine
- Moore Machine
- ??? ??(Current State)?? ?? ??(Outputs)? ???
- Moore is less
Current State
Combinational Logic
F/F
Next State
Inputs
Combinational Logic
Outputs
21Moore Machine ?? State diagram
?? ??
?? WindowAct ?? y(20)
?? 1. WindowAct??? 0? ?? State? ??? ???, 1? ??
state? ??? S0-gtS1-gtS2-gtS0? ????. 2. ???? y(20)?
??? S0? ?? 000? S1? ???? 010? S2? ???? 101?
????.
22Moore Machine ?? Process 2? ??
- Library ieee Use ieee.std_logic_1164.all
- ENTITY MooreMachine IS
- PORT( clk, reset, WindowAct IN STD_LOGIC
- y OUT STD_LOGIC_vector(2 downto 0))
- END MooreMachine
- ARCHITECTURE a OF MooreMachine IS
- TYPE STATE_TYPE IS (s0, s1,s2)
- SIGNAL state STATE_TYPE
- BEGIN
- PROCESS (clk, reset)
- BEGIN
- IF reset '0' THEN
- state lt s0
- ELSIF clk'EVENT AND clk '1' THEN
- CASE state IS
- WHEN s0 gt
- IF WindowAct'1' THEN state lt s1
?? ??
Current State
Combinational Logic
F/F
Next State
Inputs
Combinational Logic
Outputs
23Moore Machine ?? Process 2? ??
- PROCESS(state)
- BEGIN
- CASE state IS
- WHEN s0 gt
- y lt "000"
- WHEN s1 gt
- y lt "010"
- WHEN others gt
- y lt "101"
- END CASE
- END PROCESS
- END a
Current State
Combinational Logic
F/F
Next State
Inputs
Combinational Logic
Outputs
?? ??
24Moore Machine ?? Process 3? ??
- Library ieee Use ieee.std_logic_1164.all
- ENTITY MooreMachine_v3 IS
- PORT( clk, reset, WindowAct IN STD_LOGIC
- y OUT STD_LOGIC_vector(2 downto 0))
- END MooreMachine_v3
- ARCHITECTURE a OF MooreMachine_v3 IS
- TYPE STATE_TYPE IS (s0, s1,s2)
- SIGNAL state, NextState STATE_TYPE
- BEGIN
- PROCESS ( State, WindowAct)
- BEGIN
- CASE State IS
- WHEN s0 gt
- IF WindowAct'1' THEN NextState lt s1
- ELSE NextState lt s0
- END IF
- WHEN s1 gt
?? ??
Current State
Combinational Logic
F/F
Next State
Inputs
Combinational Logic
Outputs
25Moore Machine ?? Process 3? ??
?? ??
- PROCESS (clk, reset)
- BEGIN
- IF reset '0' THEN
- state lt s0
- ELSIF clk'EVENT AND clk '1' THEN
- state lt NextState
- END IF
- END PROCESS
- PROCESS(state)
- BEGIN
- CASE state IS
- WHEN s0 gt
- y lt "000"
- WHEN s1 gt
- y lt "010"
- WHEN others gt
- y lt "101"
Current State
Combinational Logic
F/F
Next State
Inputs
Combinational Logic
Outputs
?? ??
26Moore FSM
process(clock, reset)
Next State function
Inputs
Next State
Present StateRegister
clock
Present State
reset
concurrent statements
Output function
Outputs
27Mealy FSM
process(clock, reset)
Next State function
Inputs
Next State
Present State
Present StateRegister
clock
reset
Output function
Outputs
concurrent statements
28Moore FSM - Example 1
- Moore FSM that Recognizes Sequence 10
29Moore FSM in VHDL (1)
TYPE state IS (S0, S1, S2) SIGNAL Moore_state
state U_Moore PROCESS (clock,
reset) BEGIN IF(reset 1) THEN Moore_state
lt S0 ELSIF (clock 1 AND clockevent)
THEN CASE Moore_state IS WHEN S0 gt IF
input 1 THEN Moore_state
lt S1 ELSE
Moore_state lt S0 END IF
30Moore FSM in VHDL (2)
- WHEN S1 gt
- IF input 0 THEN
- Moore_state
lt S2 - ELSE
- Moore_state
lt S1 - END IF
- WHEN S2 gt
- IF input 0 THEN
- Moore_state
lt S0 - ELSE
- Moore_state
lt S1 - END IF
- END CASE
- END IF
- END PROCESS
- Output lt 1 WHEN Moore_state S2 ELSE 0
31Mealy FSM - Example 1
- Mealy FSM that Recognizes Sequence 10
32Mealy FSM in VHDL (1)
TYPE state IS (S0, S1) SIGNAL Mealy_state
state U_Mealy PROCESS(clock,
reset) BEGIN IF(reset 1) THEN Mealy_state
lt S0 ELSIF (clock 1 AND clockevent)
THEN CASE Mealy_state IS WHEN S0 gt
IF input 1 THEN
Mealy_state lt S1 ELSE
Mealy_state lt S0
END IF
33Mealy FSM in VHDL (2)
- WHEN S1 gt
- IF input 0 THEN
- Mealy_state
lt S0 - ELSE
- Mealy_state
lt S1 - END IF
- END CASE
- END IF
- END PROCESS
- Output lt 1 WHEN (Mealy_state S1 AND input
0) ELSE 0
34Moore FSM Example 2 State diagram
35Moore FSM Example 2 State table
36Moore FSM with 2s Processes
process(clock, reset)
Input w
Next State function
Next State
Present StateRegister
Present State y
clock
resetn
Output z
concurrent statements
Output function
37Moore FSM 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 ARCHITECTURE
Behavior OF simple IS TYPE State_type IS (A, B,
C) SIGNAL y State_type BEGIN PROCESS (
resetn, clock ) BEGIN IF resetn '0'
THEN y lt A ELSIF (Clock'EVENT AND Clock
'1') THEN
38Moore FSM Example 2 VHDL code (2)
CASE y IS WHEN A gt IF w '0' THEN
y lt A ELSE y lt B
END IF WHEN B gt IF w '0'
THEN y lt A ELSE y lt C
END IF WHEN C gt IF w '0'
THEN y lt A ELSE y lt C
END IF END CASE
39Moore FSM Example 2 VHDL code (3)
- END IF
- END PROCESS
-
- z lt '1' WHEN y C ELSE '0'
- END Behavior
40Moore FSM with 3s Processes
process (w, y_present)
Input w
Next State function
Next State y_next
process (clock, resetn)
Present StateRegister
Present State y_present
clock
resetn
Output z
concurrent statements
Output function
41Alternative VHDL code (1)
ARCHITECTURE Behavior OF simple IS TYPE
State_type IS (A, B, C) SIGNAL y_present,
y_next State_type BEGIN PROCESS ( w,
y_present ) BEGIN CASE y_present IS WHEN A
gt IF w '0' THEN y_next lt A
ELSE y_next lt B END IF
WHEN B gt IF w '0' THEN y_next lt
A ELSE y_next lt C END IF
42Alternative VHDL code (2)
WHEN C gt IF w '0' THEN y_next lt A
ELSE y_next lt C END IF END
CASE END PROCESS PROCESS (clock,
resetn) BEGIN IF resetn '0'
THEN y_present lt A ELSIF (clock'EVENT AND
clock '1') THEN y_present lt y_next END
IF END PROCESS z lt '1' WHEN y_present C
ELSE '0' END Behavior
43Mealy FSM Example 2 State diagram
44Mealy FSM Example 2 State table
45Mealy FSM with 2s Processes
process(clock, reset)
Input w
Next State function
Next State
Present State y
Present StateRegister
clock
resetn
Output z
Output function
concurrent statements
46Mealy FSM Example 2 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 ARCHITECTURE Behavior OF Mealy IS TYPE
State_type IS (A, B) SIGNAL y State_type
BEGIN PROCESS ( resetn, clock ) BEGIN IF
resetn '0' THEN y lt A ELSIF
(clock'EVENT AND clock '1') THEN
47Mealy FSM Example 2 VHDL code (2)
- CASE y IS
- WHEN A gt
- IF w '0' THEN
-
y lt A - ELSE
-
y lt B - END IF
- WHEN B gt
- IF w '0' THEN
-
y lt A - ELSE
-
y lt B - END IF
- END CASE
48Mealy FSM Example 2 VHDL code (3)
- END IF
- END PROCESS
- WITH y SELECT
- z lt w WHEN B,
- z lt 0 WHEN others
- END Behavior
49Timing Design - ????
- State Machine ??
- Shift Register ??
- Counter ??
??? ??????? ??? ???? ??? ??? ??? ?? ??? ?? ????.
?? ????? ??? ??? ???? ??? ?? ??? ?? ?? ??? ???
????.
50Timing Design State Machine Application (1)
1. ??? ?? Timing ??? ??? ?? ??? ?????
- ?? WindowAct??? 0?? 1? ??? ???? ?? clock?
rising edge ?? RiseShot? 1? ??? - WindowAct??? 1?? 0?? ??? ???? ?? clock? rising
edge ?? FallShot? 1? ???? ?.
51Timing Design State Machine Application (2)
2.Excercise Mealy-machine state diagram? ????
WindowAct / RiseShot, FallShot
?? / ??1, ??2
52Timing Design State Machine Application (3)
????? ??? ?? ??? ???? ??
53Timing Design State Machine Application (4)
????? ??? ?? ??? ???? ??
Note The outputs react to input asynchronously.
54Timing Design State Machine Application (5)
55Timing Design State Machine Application (6)
3. ???? ???? ?? ?? ??? ? ? Timing ?? ??? ??
1
3
4
2
1,3 ?? RiseShot WindowAct and Q
Q? WindowAct? D F/F?? ???? ??
2,4 ?? FallShot WindowAct and Q
56Timing Design State Machine Application (7)
3. Timing ?? ??? ???? - BDF
Q? WindowAct? D FF?? ???? ??
WindowAct and Q
not Q
not WindowAct
WindowAct and Q
57Timing Design State Machine Application (8)
library ieee use ieee.std_logic_1164.all use
ieee.std_logic_unsigned.all entity
RiseFallShot_time is port( WindowAct in
std_logic clk,nclr in std_logic
RiseShot,FallShot out std_logic ) end
RiseFallShot_time architecture a of
RiseFallShot_time is signal q
std_logic signal RisingShotPules
std_logic begin -- shift register 1bits
process(nclr,clk) begin if( nclr'0')
then q lt'0' elsif(clk'event and clk'1')
then q lt WindowAct end if end
process -- rising shot pulse gen. RiseShot lt
WindowAct and not q FallShot lt not WindowAct
and q end a
3. Timing ?? ??? ???? - VHDL
?? ??
?? ??
58Timing Design Shift Register Application (1)
- ??? ?? Timing ??? ??? ?? ??? ?????.
- ???? reset, clk, WindowAct
- ???? y0, y1, y2, y3, y4, y5, y6
59Timing Design Shift Register Application (2)
1. ?? ??? ??? ?????. ???? reset, clk,
WindowAct ???? y0
?? ???? ???? ???? ? 1???? ??? ? ?? ??? Shift
Register? ???? ??
60Timing Design Shift Register Application (3)
???? y0? Q1? 1? ?? ??? Q2? 0? ?? 700-900ns???? 1?
??. Y0Q1 and Q2
??? ?? Shift Register? ???? ?? Q0, Q1, Q2? ????
??? ? ??.
Y0 Q1 and not Q2
61Timing Design Shift Register Application (4)
2. ???? y1? ?????.
62Timing Design Shift Register Application (5)
Y0? clk? falling edge? ???? shift?? ??? shift?
Y1? ?? ? ??.
63Timing Design Shift Register Application (6)
3. ???? y2? ?????.
64Timing Design Shift Register Application (7)
Y2? Y0? Y1? OR ? ??? ? ? ??. Y2 Y0 or Y1
Y2 Y0 or Y1
65Timing Design Shift Register Application (8)
4. ???? y3? ?????.
66Timing Design Shift Register Application (9)
11 bits Shift Register
Y3? 11?? shift register??? Q9? 1?? Q10? 0? ??? 1?
???? ??.
67Timing Design Shift Register Application (10)
Y3? 11?? shift register??? Q10? Q9? ??? ???. Y3
Q9 and Q10
68Timing Design Shift Register Application (11)
5. ???? y4? ?????.
69Timing Design Shift Register Application (12)
Y4? Y1? Y3? OR ? ??? ? ? ??. Y4 Y1 or Y3
70Timing Design Shift Register Application (13)
6. ???? y5? ?????.
71Timing Design Shift Register Application (14)
Y5? Q2? 1?? Q10? 0? ??? 1? ??.
72Timing Design Shift Register Application (15)
7. ???? y6? ?????.
73Timing Design Shift Register Application (16)
Y6p? Q1? 1?? Q9? 0? ??? 1? ??.
Y6? Y6p? Clk? Falling Edge? ???? ? ?? ??? ???.
74Timing Design Shift Register Application (17)
- library ieee
- use ieee.std_logic_1164.all
- use ieee.std_logic_unsigned.all
- entity shift_app2 is
- port( clk,nclr,WindowAct in std_logic
- y buffer std_logic_vector(0 to 6))
- end shift_app2
- architecture a of shift_app2 is
- signal q std_logic_vector(0 to 10)
- signal y6p std_logic
- begin
- ShiftRegster
- process(nclr,clk)
- begin
- if( nclr'0') then
- qlt"00000000000"
- elsif(clk'event and clk'1') then
- q(0)lt WindowAct
- for i in 0 to 9 loop
????
????
75Timing Design Shift Register Application (18)
????
- process(nclr,clk)
- begin
- if( nclr'0') then
- y(1)lt'0'
- elsif(clk'event and clk'0') then
- y(1)lty(0)
- end if
- end process
- y(2) lt y(0) or y(1)
- y(3) lt q(9) and not q(10)
- y(4) lt y(1) or y(3)
- y(5) lt q(2) and not q(10)
- y6p lt q(1) and not q(9)
- process(nclr,clk)
- begin
- if( nclr'0') then
- y(6)lt'0'
????
????
76Timing Design Result (1)
77Timing Design Result (2)
78Timing Design Resource Usage
79Timing Design Counter Application (1)
- ??? ?? Timing ??? ??? ?? ??? Shift Register? ??
?? ??(Counter??)?? ?????. - ???? reset, clk, WindowAct
- ???? y0, y1, y2, y3, y4, y5, y6
80Timing Design Counter Application (2)
1. ???? ???? cnt3..0? ?? ? ????
81Timing Design Counter Application (3)
? ???? WindowAct? 1? ?? ????, WindowAct? 0? ??
0?? ??.
process(nclr,clk) begin if( nclr'0')
then cntlt"0000" elsif(clk'event and
clk'1') then if(WindowAct'0')
then cntlt"0000" else cnt lt
cnt1 end if end if end process
library ieee use ieee.std_logic_1164.all use
ieee.std_logic_unsigned.all entity cnt_app2 is
port( clk,nclr,WindowAct in std_logic y
buffer std_logic_vector(0 to 6)) end
cnt_app2 architecture a of cnt_app2 is signal
cnt std_logic_vector(3 downto 0) begin
Cnt ??
Cnt3..0? ??
82Timing Design Counter Application (4)
2. ???? ?? cnt3..0? ?? Y0, Y3? ?? ? ????
83Timing Design Counter Application (5)
Y0 ???
Y3 ???
process(cnt) begin if( cnt2)
then y(0)lt'1' else y(0)lt'0' end
if end process
process(cnt) begin if( cnt10)
then y(3)lt'1' else y(3)lt'0' end
if end process
84Timing Design Counter Application (6)
3. Y1,Y2,Y4? ????
85Timing Design Counter Application (7)
- process(nclr,clk)
- begin
- if( nclr'0') then
- y(1)lt'0'
- elsif(clk'event and clk'0') then
- y(1)lty(0)
- end if
- end process
- y(2) lt y(0) or y(1)
- y(4) lt y(1) or y(3)
Y0? clk? falling edge? ???? ????? ??? ????Y1? ??
? ??.
Y2? Y0? Y1? OR ? ??? ? ? ??. Y2 Y0 or Y1
Y4? Y1? Y3? OR ? ??? ? ? ??. Y2 Y0 or Y1
Y2,Y4???
86Timing Design Counter Application (8)
4. Y5,Y6? ????
87Timing Design Counter Application (9)
- process(nclr,clk)
- begin
- if( nclr'0') then
- y(5)lt'0'
- elsif(clk'event and clk'1') then
- if(cnt2) then
- y(5)lt'1'
- elsif(cnt10) then
- y(5)lt'0'
- else
- y(5)lty(5)
- end if
- end if
- end process
Y5??? Cnt2? ? 1? ??? Cnt0? ? 0?? ???. Clk?
rising Edge??
process(nclr,clk) begin if( nclr'0')
then y(6)lt'0' elsif(clk'event and clk'0')
then if(cnt2) then y(6)lt'1' elsif(cnt
10) then y(6)lt'0' else y(6)lty(6)
end if end if end process end a
Y6??? Cnt2? ? 1? ??? Cnt0? ? 0?? ???. Clk?
Falling Edge??
88Timing Design Result (1)
89Timing Design Result (2)
90Timing Design Result (3)