Lec 8 FSMachine
Lec 8 FSMachine
Digital Systems
Lecture 8:
Sequential Networks and Finite State
Machines
CK Cheng
Dept. of Computer Science and Engineering
University of California, San Diego
1
3. Implementation
• Specification:
– Finite State Machine:
• Behavior, State Diagram, State Table
• Register Transfer Level Description
• Design Flow
• Excitation Table
• Mealy and Moore Machines
• Examples
2
Specification
• Combinational Logic
– Truth Table
– Boolean Expression
– Logic Diagram (No feedback loops)
• Sequential Networks: Finite State Machines
– State Diagram, State Assignment, State Table
– Excitation Table and Characteristic Expression
– Register-Transfer Level Description
– Logic Diagram (FFs and feedback loops)
3
FINITE STATE MACHINE: OVERVIEW USING AN EXAMPLE
Not all sequences are created equal: A coin has two faces.
A toss of the coin lands either Head or Tail with equal
probability. A series of tossing creates a sequence. Given
two patterns, say HHH and HTT, the pattern appears first
wins.
H H H T H H
A. The two patterns have an
equal chance to win
B. HHH wins more times
C. HTT wins more times
D. None of the above
FINITE STATE MACHINE: NOT ALL SEQUENCES ARE CREATED EQUAL
A B C D
Combinational
X
S(t)
CLK CLK
RTL: Register-Transfer Level
Description
CLK
• VHDL
• Verilog Conceptually, we can
• BVS align all registers into one
single column
6
Design Flow
• Input Output Relation
• State Diagram (Transition of states)
• State Assignment (Map states into binary
code)
– Binary code, Gray encoding, One hot
encoding, Coding optimization
• State Table (Truth table of states)
• Excitation Table (Truth table of FF inputs)
– K Map, Minimal Expression
– Logic Diagram
• Verification: Simulation
7
Design Flow: Examples
• Example 1: a counter from description, state
diagram, state table, netlist, to I/O behavior.
• Example 2: a pattern recognizer from state
diagram to netlist to demonstrate the
difference of two machines.
8
Example: A Counter
Behavior over time
Symbol/ Circuit
CLK
Q1
𝑄 𝑡 + 1 = (𝑄 𝑡 + 1) 𝑚𝑜𝑑 4
9
Counter: Input-output relation ⇒ State diagram
2 bit Counter
10
Counter: State diagram ⇒ State assignment
State Diagram State Table: Symbol
S0
Current state Next State
S0 S1
S3 S1
S1 S2
S2 S3
S2 S3 S0
State
Assignment
State Q1 Q0 Q1(t) Q0(t) Q1(t+1) Q0(t+1)
S0 0 0
S1 0 1
S2 1 0
S3 1 1 State Table: Binary
11
Counter: State assignment ⇒ Excitation table
State Table 12
Counter: Excitation table ⇒ K map ⇒ Logic Diagram
Q0(t)
Q
Combinational D
Q’
circuit Q1(t)
D Q
Q’
CLK
CLK
Q1(t)
14
Counter: Verification (Simulation)
Q
D
Q’
Q0(t)
Current state Next State Q
D
S0 S1 Q’
S1 S2
S2 S3 CLK
Q1(t)
S3 S0
Time 0 1 2 3 4 5
𝑄1 0
𝑄0 0
15
Example: Simulation from state table/state diagram
Time 0 1 2 3 4 5
Input 0 1 1 0 1 -
State S0
Output
16
Example: Simulation from state table/state diagram
Time 0 1 2 3 4 5
Input 0 1 1 0 1 -
State S0 S0 S2 S1 S3 S0
Output 0 0 0 0 1 0
17
Example: Life on Mars?
Mars rover has a binary input x. When it receives the input sequence
x(t-2, t) = 001 from its life detection sensors, it means that the it has
detected life on Mars ☺ and the output y(t) = 1, otherwise y(t) = 0 (no
life on Mars ).
18
Mars Life Recognizer FSM
Which of the following diagrams is a correct Mealy solution
for the 001 pattern recognizer on the Mars rover?
1/1
A.
0/0 0/0
1/0 S0 S1 S2
0/0
1/0
B. 0/0
0/0 1/1 Input/output
1/0 S0 S1 S2
1/0
0/0
C. Both A and B are correct
D. None of the above 19
Mars Life Recognizer FFs
Mealy Machine
Pattern Recognizer ‘001’
x(t)
1/0 1/1
C1 C2 y(t)
0/0 0/0
S0 S1 S2
CLK
1/0 0/0 S(t)
20
State Diagram => State Table with State Assignment
1/1 x(t)
C1 C2 y(t)
0/0 0/0
1/0 S0 S1 S2 0/0
CLK
S(t)
1/0
Mealy Machine
21
State Diagram => State Table => Excitation Table =>
Circuit
Q1(t) Q0(t)\x 0 1 id Q1Q0x D1 D0 y
00 01,0 00,0 0 000 0 1 0
01 10,0 00,0
1 001 0 0 0
10 10,0 00,1
2 010 1 0 0
x(t) 3 011 0 0 0
C1 C2 y(t) 4 100 1 0 0
5 101 0 0 1
CLK 6 110
S(t)
Mealy Machine 7 111
22
State Diagram => State Table => Excitation Table =>
Circuit id Q1Q0x D1 D0 y
Q1(t) Q0(t)\x 0 1
0 000 0 1 0
00 01,0 00,0
01 10,0 00,0
1 001 0 0 0
10 10,0 00,1 2 010 1 0 0
x(t) 3 011 0 0 0
C1 C2 y(t) 4 100 1 0 0
5 101 0 0 1
CLK 6 110
S(t)
Mealy Machine 7 111
Example: What to fill in rows 6 and 7 of excitation table?
A. All 0s
B. All 1s
C. All Don’t Cares 23
State Diagram => State Table => Excitation Table => Circuit
Q0
id Q1Q0x D1 D0 y D1(t):
0 2 6 4
0 000 0 1 0 0 1 X 1
1 3 7 5
1 001 0 0 0 x(t) 0 0 X 0
2 010 1 0 0 Q1
3 011 0 0 0
4 100 1 0 0 D1(t) = x’Q0 + x’Q1
D0 (t)= Q’1Q’0 x’
5 101 0 0 1
y= Q1x
6 110 X X X
7 111 X X X
24
State Diagram => State Table => Excitation Table => Circuit
Q’1 D0 Q0
Q
Q’0 D
x’ Q’
x’ Q1 y
D1 Q
D
Q0 Q’
Q1
x
x(t)
C1 C2 y(t)
D1(t) = x’Q0 + x’Q1
D0 (t)= Q’1Q’0 x’
y= Q1x CLK
S(t)
Mealy Machine 25
Mealy and Moore Machines: Canonical Form
x(t) y(t)
Combinational
Logic
CLK
x(t) C2 y(t)
x(t)
C1 C2 y(t)
C1
CLK
CLK
26
Mealy and Moore Machines: Canonical Form
27
Mealy and Moore Machines: Canonical Form
Mealy Machine: yi(t) = fi(X(t), S(t))
Moore Machine: yi(t) = fi(S(t))
si(t+1) = gi(X(t), S(t))
Mealy Machine Moore Machine
x(t) x(t) y(t)
C1 C2
y(t)
C1 C2
CLK
S(t)
CLK S(t)
Input Input
PS NS, output PS NS Output
input/output input
Si
S Sj Si
S Sj
output output
28
Mealy and Moore Machines: Mars Life Recognizer
Which of the following diagrams is a correct Moore solution to the ‘001’
pattern recognizer? input/output
1/1
A.
1/0 0/0 0/0
S0 S1 S2
0/0
1/0
B. 1 0
1
0 0 S2
1 S3
S0 S1 1
0 input 0
1 0
output
C. Both A and B are correct
29
D. None of the above
Mealy and Moore Machines: Mars Life Recognizer
Pattern Recognizer ‘001’
x(t)
C1 S(t) C2 y(t)
1
1 0
0 0 S2 1 S3
S0 S1
0 0 1 CLK
1 0
Moore Machine
What does state table need to show to design controls of C2?
A.(current input x(t), current state S(t) vs. next state, S(t+1))
B.(current input, current state vs. current output y(t))
C.(current state vs. current output y(t) and next state)
D.(current state vs. current output y(t) )
E. None of the above
30
Mealy and Moore Machines: Mars Life Recognizer
1
1 0
S2 1 S3
S0 0 S1 0 1
0 0
1
0
ID Q1Q0x D1 D0 y
S(t)\x 0 1
0 000 0 1 0
S0 S1,0 S0,0
S1 S2,0 S0,0 1 001 0 0 0
S2 S2,0 S3,0 2 010 1 0 0
S3 S1,1 S0,1
3 011 0 0 0
Q1Q0\x 0 1 4 100 1 0 0
00 01,0 00,0
5 101 1 1 0
01 10,0 00,0
10 10,0 11,0 6 110 0 1 1
11 01,1 00,1 7 111 0 0 1
Q1(t+1)Q0(t+1), y
Mealy and Moore Machines: Mars Life Recognizer
Q0
D1(t):
0 2 6 4
0 1 0 1
1 3 7 5
x(t) 0 0 0 1
id Q1Q0x D1 D0 y
Q1
0 000 0 1 0
Q0
1 001 0 0 0 D0(t):
0 2 6 4
1 0 1 0
2 010 1 0 0 1 3 7 5
011 0 0 0 1
3 0 0 0 x(t)
Q1
4 100 1 0 0
Q0
y(t):
5 101 1 1 0 0 2 6 4
0 0 1 0
6 110 0 1 1 1 3 7 5
x(t) 0 0 1 0
7 111 0 0 1
Q1
Mealy and Moore Machines: Mars Life Recognizer
State Diagram => State Table => Excitation Table => Circuit
D0 Q0
Q y
D
Q’
Q1
D1 Q
D
Q’
x(t) y(t)
C1 C2
D1(t)= Q1(t)Q0(t)’+Q1(t)’Q0(t) x(t)
D0(t)= Q1(t)’Q0(t)’x(t)’+
Q1(t)Q0(t) x(t)’+Q1(t)Q0(t)’ x(t) CLK
y(t)= Q1(t)Q0(t) S(t)
Moore Machine 33
Conversion from Mealy to Moore Machine
1/1
1
1 0
S 1 S
S0 0 S1 0
0/0 0/0 0 0 2 3
S1 1
1/0 S0 S 0/ 1 0
2
0 0
1/
0
S(t)\x 0 1 S(t)\x 0 1
S0 S1,0 S0,0 S0 S1 S0 0
S1 S2,0 S0,0 S1 S2 S0 0
S2 S2,0 S0,1 S2 S2 S3 0
S3
Algorithm
1. Identify distinct (NS, y) pair
2. Replace each distinct (NS, y) pair with distinct new states
3. Insert rows of present state = new states
Conversion from Mealy to Moore Machine
Mealy S(t)\x 0 1 S(t)\x 0 1 y Moore
S0 S1,0 S0,0 S0 S1 S0 0
S1 S2,0 S0,0 S1 S2 S0 0
S2 S2,0 S0,1 S2 S2 S3 0
S3
1. Find distinct NS, y
2. Add new states to represent distinct NS, y
iClicker
For the above Moore machine, what are the next states with
respect to present state S3?
A. S2, S3, 1
B. S2, S0, 1
C. S1, S0, 1
D. S1, S0. 0
E. None of the above.
Conversion from Mealy to Moore Machine
1
1/1 1 0
S 1 S
S0 0 S1 0 3
0 0 2
0/0 0/0 1 0 1
1/0 S0 S1 S2 0/ 0
0
1/0 S(t)\x 0 1
S(t)\x 0 1 S0 S1,0 S0,0
S0 S1,0 S0,0 S1 S2,0 S0,0
S1 S2,0 S0,0 S2 S2,0 S3,0
S2 S2,0 S0,1 S3 S1,1 S0,1
Time 0 1 2 3 4 5 6 7 8
x 0 1 0 0 1 1 0 0 1
Smealy S0 S1 S0 S1 S2 S0 S0 S1 S2
ymealy
Smoore S0
ymoore
Conversion from Mealy to Moore Machine
1
1/1 1 0
S 1 S
S0 0 S1 0 3
S0 0/0 S1 0/0 0 0 2
1/0 S2 0/ 1 0 1
0 0
1/0 S(t)\x 0 1
S(t)\x 0 1
S0 S1,0 S0,0
S0 S1,0 S0,0
S1 S2,0 S0,0
S1 S2,0 S0,0
S2 S2,0 S3,0
S2 S2,0 S0,1 S3 S1,1 S0,1
0 0
1/0 S(t)\x 0 1
S(t)\x 0 1
S0 S1,0 S0,0
S0 S1,0 S0,0
S1 S2,0 S0,0
S1 S2,0 S0,0
S2 S2,0 S3,0
S2 S2,0 S0,1 S3 S1,1 S0,1
Algorithm
1. Identify distinct (NS, y) pair
2. Replace each distinct (NS, y) pair with distinct new states
3. Insert rows of present state = new states
4. Append each present state with its output y
Example: Traffic Light Controller
– Traffic sensors: TA, TB (TRUE when there’s traffic)
– Lights: LA, LB
Bravado
Dining
Hall
LB
LA TB
LA
Academic TA TA Ave.
Labs TB LB Dorms
Blvd.
Fields
41
Traffic Light Controller: FSM Black Box
CLK
TA Traffic LA
Light
TB Controller LB
Reset
42
FSM State Transition Diagram
• Moore FSM: outputs labeled in each state
• States: Circles
• Transitions: Arcs
Reset
S0
LA: green
LB: red
43
FSM State Transition Diagram
• Moore FSM: outputs labeled in each state
• States: Circles
• Transitions: Arcs T
Reset A
S0 TA S1
LA: green LA: yellow
LB: red LB: red
S3 S2
LA: red LA: red
LB: yellow LB: green
TB
TB
44
FSM State Transition Table
PS Inputs NS
TA TB
S0 0 X S1
S0 1 X S0
S1 X X S2
S2 X 0 S3
S2 X 1 S2
S3 X X S0 45
State Transition Table
PS Inputs NS
Q1(t) Q0(t) TA TB Q1(t +1) Q0(t +1)
0 0 0 X 0 1
State Encoding
0 0 1 X 0 0
S0 00
0 1 X X 1 0
S1 01
1 0 X 0 1 1 S2 10
1 0 X 1 1 0 S3 11
1 1 X X 0 0
Q1(t+1)= Q1(t)Q0(t)
Q0(t+1)= Q’1(t)Q’0(t)T’A + Q1(t)Q’0(t)T’B
46
Traffic Light Controller: FSM Output Table
PS Outputs Output Encoding
Q1 Q0 LA1 LA0 LB1 LB0 green 00
0 0 0 0 1 0
yellow 01
0 1 0 1 1 0
red 10
1 0 1 0 0 0
1 1 1 0 0 1 LA1 = Q1
LA0 = Q’1Q0
LB1 = Q’1
LB0 = Q1Q0
47
Traffic Light Controller: State Register
CLK
S'1 S1
S'0 S0
r
Reset
state register
48
Traffic Light Controller: Logic Diagram
CLK
S'1 S1
TA S'0 S0
r
TB Reset
S1 S0
49
Traffic Light Controller: Output Logic
CLK LA1
S'1 S1
LA0
TA S'0 S0
LB1
r
TB Reset
S1 S0 LB0
50
Summary: Implementation
• Set up canonical form
• Mealy or Moore machine
• Identify the next states
• state diagram ⇨ state table
• state assignment
• Derive excitation table
• Inputs of flip flops
• Design the combinational logic
• don’t care set utilization
51