CS 316 Systems Programming

Finite State Machines
Prof. Sirer
COMP 303
Koç University
Finite State Machines
An electronic machine which has

external inputs

externally visible outputs

internal state
Output and next state depend on

inputs

current state
Designing an FSM
Draw a state diagram
Write down state transition table
Assign numbers to states
Determine logic equations for all flip-flops and
outputs
A Simple Example
o1
o2
o0
o3
o4
o6
o5
Goal: flash hello on LEDs
Inputs: clock
Outputs: Just one
7-segment LED
Flash “h” then “e” then “l”
then “l” then “o”




h = <0011101>
e = <0111110>
l = <0010110>
o = <1110111>
HELLObox: State Diagram
S0
S1
S2
S3
S4
<0011101> <0111110> <0010110> < 0010110 ><1110111>
H
E
L
L
O
Determine the transitions


label all edges (transitions) with the inputs that cause them,
unlabeled edges are unconditional transitions
show start state
HELLObox: State Table
Build state table

rote encoding of the
state diagram
Current Next
State
State
Output
S0
S1
0011101
S1
S2
0111110
S2
S3
0010110
S3
S4
0010110
S4
S0
1110111
HELLObox: State Assignment 1
Assign bit patterns to
states that would
make the resulting
device simple

One option is shown
Determine logic
equations for every bit
of output and next
state, for every flipflop and output
Current
Output
State
Next
State
000
001
0011101
001
010
0111110
010
011
0010110
011
100
0010110
100
000
1110111
HELLObox #1
combinational
circuit
o1o
o2
0
o
o4 3o6
o5
flip-flops
10 bits of information
(7 outputs + 3 bits of
next state) are computed
by the combinatorial
circuit
All 10 bits have nontrivial logic equations
HELLObox: State Assignment 2
Assign bit
patterns to states
that would make
the resulting
device simple
Here, we use far
more bits than
necessary

to simplify the
combinatorial
circuit
Current
State
Next
State
Output
00111010 01111100
0011101
01111100 00101100
0111110
00101100 00101101
0010110
00101101 11101110
0010110
11101110 00111010
1110111
HELLObox #2
combinational
circuit
o1o
o2
0
o
o4 3o6
o5
15 bits of information
(7 outputs + 8 bits of
next state) are computed
by the combinatorial
circuit
8 bits have non-trivial
logic equations

flip-flops
all 7 outputs are simple
pass-throughs
A Traffic Light
There is a large avenue and
a Hospital ER on a side
street
a
H
The avenue traffic light
should be green at all times
Except when an ambulance
is waiting on the side street

A sensor detects a waiting
ambulance
Design the light controller
A Traffic Light
H
There is a large avenue
and a Hospital ER on a
side street
The avenue traffic light
should be green at all
times
Except when an
ambulance is waiting on
the side street
A Traffic Light
H
There is a large avenue
and a Hospital ER on a
side street
The avenue traffic light
should be green at all
times
Except when an
ambulance is waiting on
the side street
A Traffic Light
H
There is a large avenue
and a Hospital ER on a
side street
The avenue traffic light
should be green at all
times
Except when an
ambulance is waiting on
the side street
A Traffic Light
H
There is a large avenue
and a Hospital ER on a
side street
The avenue traffic light
should be green at all
times
Except when an
ambulance is waiting on
the side street
A Traffic Light
H
There is a large avenue
and a Hospital ER on a
side street
The avenue traffic light
should be green at all
times
Except when an
ambulance is waiting on
the side street
A Traffic Light
H
There is a large avenue
and a Hospital ER on a
side street
The avenue traffic light
should be green at all
times
Except when an
ambulance is waiting on
the side street
A Traffic Light
H
There is a large avenue
and a Hospital ER on a
side street
The avenue traffic light
should be green at all
times
Except when an
ambulance is waiting on
the side street
A Traffic Light
One input a
Six outputs (AR, AO, AG, HR, HO, HG)
0
S0
001100
1
1
S1 0 & 1 S2 0 & 1 S3
010100
100110
0&1
0
S4 0 & 1 S5
100001 100010
110100
FSM: Serial Adder
Add two input bit streams

streams are sent with least-significant-bit (lsb) first
…10110
…00101
…01111
FSM: State Diagram
00/1
00/0
S0
S1
11/1
11/0
10/1
01/1
10/0
01/0
Two states: S0 (no carry), S1 (carry in hand)
Inputs: a and b
Output: z

Arcs labelled with input bits a and b, and output z
Serial Adder: State Table
a
b
state z next
state
0
0
S0
0
S0
0
1
S0
1
S0
1
0
S0
1
S0
1
1
S0
0
S1
0
0
S1
1
S0
0
1
S1
0
S1
1
0
S1
0
S1
1
1
S1
1
S1
Write down all input
and state
combinations
Serial Adder: State Assignment
a
b
s
z s’
0
0
0
0
0
0
1
0
1
0
1
0
0
1
0
1
1
0
0
1
0
0
1
1
0
0
1
1
0
1
1
0
1
0
1
1
1
1
1
1
Two states, so 1-bit is
sufficient

A single flip-flop will
encode the state
Serial Adder: Circuit
a
z
combinational
logic
b
s
clk
D
z = abs + abs + abs + abs
s’ = abs + abs + abs + abs
s’
Q
Equations