05 FSM Notes
05 FSM Notes
Hakim Weatherspoon
CS 3410
Computer Science
Cornell University
Inputs Combinational
circuit Outputs
N M
Need:
• To record data
• To build stateful circuits
• A state-holding device
2
Goals for Today
• Finite State Machines (FSM)
• How do we design logic circuits with state?
• Types of FSMs: Mealy and Moore Machines
• Examples: Serial Adder and a Digital Door Lock
3
Next Goal
• How do we design logic circuits with state?
4
Finite State Machines
5
Finite State Machines
An electronic machine which has
• external inputs
• externally visible outputs
• internal state
6
Abstract Model of FSM
Machine is
M = (S, I, O, δ )
S: Finite set of states
I: Finite set of inputs
O: Finite set of outputs
δ: State transition function
7
Automata Model
Finite State Machine
Current
Registers
State Comb.
Output
Logic
Input Next State
8
FSM Example
down/on
input/output up/off down/on
A B
state start
state
9
FSM Example
1/1
i0i1i2…/o0o1o2… 0/0 1/1
00 01
S1S0 S1S0
10
Mealy Machine
General Case: Mealy Machine
Current
Registers
State Comb.
Output
Logic
Input Next State
11
Moore Machine
Special Case: Moore Machine
Current
Registers
Comb. Output
State Logic
Comb.
Input Logic Next State
12
Moore Machine FSM Example
down
input up down
A B
state start
off on
out out
Legend up up
up
C D
off off
down
Input: up or down down
Output: on or off
States: A, B, C, or D
13
Mealy Machine FSM Example
down/on
input/output up/off down/on
A B
state start
state
14
Activity#2: Create a Logic Circuit for a Serial Adder
15
Activity#2: Create a Logic Circuit for a Serial Adder
Carry-out
1
…10110 Sum: output
…00101
…01111
16
Activity#2: Create a Logic Circuit for a Serial Adder
Carry-in
1
…10110 Sum: output
…00101
…01111
17
Activity#2: Create a Logic Circuit for a Serial Adder
Carry-out
11
…10110 Sum: output
…00101
…01111
18
iClicker Question
Add two infinite input bit streams
• streams are sent with least-significant-bit (lsb) first
…10110
…00101
…01111
How many states are needed to represent FSM
a) 0
b) 1
c) 2
d) 3
e) 4 19
Strategy for Building an FSM
(1) Draw a state diagram (e.g. Mealy Machine)
(2) Write output and next-state tables
(3) Encode states, inputs, and outputs as bits
(4) Determine logic equations for next state
and outputs
(5) Draw the Circuit
20
FSM: State Diagram
…10110
…00101
…01111
a…10110
…00101 z
b …01111
__/_ S0 S1 __/_
__/_
00/0 S0 S1 11/1
00/1
00/0 S0 S1 11/1
00/1
25
iClicker Question
11/0
00/0 S0 S1 11/1
00/1
26
Serial Adder: State Table
11/0
00/0 S0 S1 11/1
00/1
27
Serial Adder: State Table
11/0
00/0 S0 S1 11/1
00/1
00/0 S0 S1 11/1
00/1
a b s z s'
0 0 0 0 0 (3) Encode states, inputs, and
0 1 0 1 0
outputs as bits
1 0 0 1 0
1 1 0 0 1
0 0 1 1 0
Two states, so 1-bit is sufficient
0 1 1 0 1
• A single flip-flop will encode the
1 0 1 0 1
state
1 1 1 1 1 29
Serial Adder: Circuit
Next Current
Output
State State
z
s' s
D Q
a
b Next State
Input s'
a b s z s'
0 0 0 0 0 (4) Determine logic equations for
0 1 0 1 0 next state and outputs
1 0 0 1 0
1 1 0 0 1 Combinational Logic Equations
0 0 1 1 0
0 1 1 0 1
z = a�b�s + abs + abs + abs
� + abs
s’ = ab�s + a� bs + abs
1 0 1 0 1
1 1 1 1 1 30
Serial Adder: Circuit
Next Current
Output
State State
z
s' s Comb.
D Q
Logic
a
b Next State
Input s'
a b s z s'
0 0 0 0 0 (4) Determine logic equations
0 1 0 1 0 for next state and outputs
1 0 0 1 0
1 1 0 0 1 Combinational Logic
0 0 1 1 0 Equations
z = a�bs̅ + abs + abs + abs
0 1 1 0 1 � + abs
s’ = abs̅ + a�bs + abs
1 0 1 0 1
1 1 1 1 1 31
Sequential Logic Circuits
Next Current
Output
State State
z
s' s Comb.
D Q
Logic
a
b Next State
Input s'
33
Which statement(s) is true
(A) In a Moore Machine output depends on
both current state and input
(B) In a Mealy Machine output depends on
both current state and input
(C) In a Mealy Machine output depends on
next state and input
(D) All the above are true
(E) None are true
34
Mealy Machine
General Case: Mealy Machine
Current
Registers
State Comb.
Output
Logic
Input Next State
35
Moore Machine
Special Case: Moore Machine
Current
Registers
Comb. Output
State Logic
Comb.
Input Logic Next State
36
Example: Digital Door Lock
Digital Door Lock
Inputs:
• keycodes from keypad
• clock
Outputs:
• “unlock” signal
• display how many keys pressed so
far
37
Door Lock: Inputs
Assumptions:
• signals are synchronized to
clock
• Password is B-A-B
K A B Meaning
K 0 0 0 Ø (no key)
A 1 1 0 ‘A’ pressed
B 1 0 1 ‘B’ pressed
38
Door Lock: Outputs
Assumptions:
• High pulse on U unlocks door
4 LED 8
D3D2D1D0 dec
U
Strategy:
(1) Draw a state diagram (e.g. Moore Machine)
(2) Write output and next-state tables
(3) Encode states, inputs, and outputs as bits
(4) Determine logic equations for next state and outputs
39
Door Lock: Simplified State Diagram
Idle
”0”
Ø else any
B1 else B2 else B3
”1” ”2” ”3”
Ø Ø
(1) Draw a state diagram (e.g. Moore Machine) 41
Door Lock: Simplified State Diagram
Ø Ø
G1 “A” G2 “B” G3
”1” ”2” ”3”, U
Idle
”0”
else
Ø else
B1 else B2
”1” ”2”
Ø Ø
(1) Draw a state diagram (e.g. Moore Machine) 42
Door Lock: Simplified State Diagram
Ø Ø
G1 “A” G2 “B” G3
”1” ”2” ”3”, U
dec
D3-0
3bit S2-0
Reg U
clk
S2-0
K S’2-0 S2 S1 S0 D3 D2 D1 D0 U
A 0 0 0 0 0 0 0 0
B 0 0 1 0 0 0 1 0
0 1 0 0 0 1 0 0
U = S�2S1S0 0 1 1 0 0 1 1 1
dec
D03-0 0 0 0 0 0 0 0 0
3bit S2-0 0 0 0 1 0 1 0 0 1
Reg U
0 0 0 1 1 0 1 0 0
0 0 1 0 0 0 0 0 1
clk
0 0 1 1 1 0 0 1 0
S2-0 0 0 1 1 0 1 1 0 1
K 0S’2-0
1 0 0 0 0 0 1 0
A 0 1 0 1 0 1 0 1 1
B 0 1 0 1 1 0 0 0 0
0 1 1 x x x 0 0 0
1 0 0 0 0 0 1 0 0
1 0 0 1 x x 1 0 1
1 0 1 0 0 0 1 0 1
S0’ = ? 1 0 1 1 x x 0 0 0
S1’ = ?
S2’ = S2S1S0KAB �B + S2S1S2KAB + S�2S1S0K + S2 S�1S0 KAB
� + S2S1S0KA 47
Door Lock: Implementation
4
dec
D3-0
3bit S2-0
Reg U
clk
S2-0
K S’2-0
A
B
Strategy:
(1) Draw a state diagram (e.g. Moore Machine)
(2) Write output and next-state tables
(3) Encode states, inputs, and outputs as bits
(4) Determine logic equations for next state and outputs48
Door Lock: Implementation
Current
Registers
Comb. Output
State Logic
Comb.
Input Logic Next State
Strategy:
(1) Draw a state diagram (e.g. Moore Machine)
(2) Write output and next-state tables
(3) Encode states, inputs, and outputs as bits
(4) Determine logic equations for next state and outputs49
Summary
We can now build interesting devices with
sensors
• Using combinational logic
50