Finite State Machines
Hakim Weatherspoon
CS 3410
Computer Science
Cornell University
The slides are the product of many rounds of teaching CS 3410
by Professors Weatherspoon, Bala, Bracy, and Sirer.
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
Finite State Machines
Next Goal
How do we design logic circuits with state?
Finite State Machines
An electronic machine which has
• external inputs
• externally visible outputs
• internal state
Output and next state depend on
• inputs
• current state
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
Next state depends on present input and
present state
Automata Model
Finite State Machine
Current
Output
Registers
State Comb.
Logic
Input Next State
• inputs from external world
• outputs to external world
• internal state
• combinational logic
FSM Example
down/on
input/output up/off down/on
A B
state start
state
Legend up/off up/off
up/off
C D
down/off
Input: up or down down/off
Output: on or off
States: A, B, C, or D
FSM Example
down/on
input/output up/off down/on
A B
state start
state
Legend up/off up/off
up/off
C D
down/off
Input: = up or = down down/off
Output: = on or = off
States: = A, = B, = C, or = D
FSM Example
1/1
i0i1i2…/o0o1o2… 0/0 1/1
00 01
S 1S 0 S 1S 0
Legend 0/0 0/0
0/0
10 11
1/0
Input: 0=up or 1=down 1/0
Output: 1=on or 0=off
States: 00=A, 01=B, 10=C, or 11=D
Mealy Machine
General Case: Mealy Machine
Current
Registers
State Comb. Output
Logic
Input Next State
Outputs and next state depend on both
current state and input
Moore Machine
Special Case: Moore Machine
Current Comb.
Registers
State Logic Output
Comb.
Input Logic Next State
Outputs depend only on current state
Moore Machine FSM Example
down
input up down
A B
state start off on
out out
Legend up down
up
C D
off off
up
Input: up or down down
Output: on or off
States: A, B, C, or D
Mealy Machine FSM Example
down/on
input/output up/off down/on
A B
state start
state
Legend up/off down/off
up/off
C D
up/off
Input: up or down down/off
Output: on or off
States: A, B, C, or D
Activity#2: Create a Logic Circuit for a Serial Adder
Add two infinite input bit streams
• streams are sent with least-significant-bit (lsb) first
• How many states are needed to represent FSM?
• Draw and Fill in FSM diagram
…10110 Sum: output
…00101
…01111
Strategy:
(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
FSM: State Diagram
…10110
…00101
…01111
states:
Inputs: ??? and ???
Output: ???
FSM: State Diagram
__/_
__/_ S0 S1 __/_
__/_
__/_ __/_ __/_ __/_
…10110
…00101
…01111
states:
Inputs: ??? and ???
Output: ???
FSM: State Diagram
(2) Write down all input and __/_
state combinations
__/_ S0 S1 __/_
__/_
__/_ __/_ __/_ __/_
Current State Input (A B) Next State Output (S)
S0 (Carry=0) 00 S0 0
S0 01 or 10 S0 1
S0 11 S1 0
S1 (Carry=1) 00 S0 1
S1 01 or 10 S1 0
S1 11 S1 1