Finite State Machines Course For Website
Finite State Machines Course For Website
PDH305
2 Hours
S0
S̀0 S1
S̀0 S1
S0 S1
0 1
State Diagram
A state diagram describes a state machine using a graphical representation.
1
0 1
S0 S1
State Table
A state transition table (or state table) describes a state machine in a tabular format.
This simple model exemplifies a door lock that embodies two states: LOCKED (S1) and
UNLOCKED (S0) and two possible inputs: LOCK (1) and UNLOCK (0). If the door is in the
UNLOCKED state and an input of LOCK is presented, the state machine progresses to the
LOCKED state. If an input of LOCK is presented to the machine in the LOCKED state the
machine stays in the LOCKED state.
where
S0 is unlocked (state)
S1 is locked (state)
x=0 to unlock (input)
x=1 to lock (input)
To summarize, a state machine can be described as:
• A set of possible input events
• A set of possible output events
• A set of states
• An initial state
• A state transition function that maps the current state and input to the next state
• A function that maps states and input to output
Each bubble in a state diagram represents a state, and each arrow represents a transition from one
state to another. Inputs are shown next to each transition arrow and outputs are shown under the
inputs on the transitions or inside the state bubble.
Block Diagram
Memory is used to store the current state of the state machine. When developing a machine using
a hardware architecture, flip-flops are used as the memory device. The number of flip-flops
required is proportional to the number of possible states in the state machine.
# of states ≤ 2x
(where x is the number of flip-flops required for the state machine)
or
x ≥ ln (# of states) / ln 2
Now, round x up to the nearest integer.
A state machine can be viewed generally as consisting of the following elements: combinational
logic, memory (flip-flops or registers), inputs and outputs.
Output(s)
Memory is used to store the state of the system. The combinational logic can be viewed as two
distinct functional blocks: a next state decoder and an output decoder [4].
Figure 8 - block diagram of a state machine showing the next state and output decoders
The next state decoder computes the machine’s next state and the output decoder computes the
output.
S̀0 S1
1/1
0/0 0/0
S2
Using Karnaugh maps to reduce the minterms and simplify the equations:
Q1Q2
x 00 01 11 10
0 0 1 x 0
D1 D1 = x'Q2
1 0 0 x 0
Q1Q2
x 00 01 11 10
0 0 0 x 0
D2 D2 = x
1 1 1 x 1
Q1Q2
x 00 01 11 10
0 0 0 x 0
Z Z = xQ1
1 0 0 x 1
Q2
D Q
CLK
Moore Machine
The advantage of a Moore machine is a simplification of behavior. The output of a Moore
machine depends only on the current state. Therefore, the output is coupled with a state and is
depicted within the bubble of the state as shown in Figure 4. The following example is the same
{1 0 1} sequence detector as shown above but implemented here as a Moore machine.
1
0 1
S0 S1
0 0
0
1 0
0
S3 S2
1 0
As in the other example consider a hardware implementation using combinational logic and D
flip-flops. Here there are four states: S0, S1, S2 and S3, and still only 2 bits are needed to define
all of the states. This will require two D flip-flops. The present state will be represented as Q1Q2
and the next state will be represented by the flip-flop equations D1D2. The output will be
represented by the variable Z. The state transition table now encoded in binary becomes
Q1Q2 D1D2 Z
x=0 x=1
00 00 01 0
01 10 01 0
10 00 11 0
11 10 00 1
(where x is the input,
Q1Q2 is the present state,
D1D2 is the next state,
and Z is the output)
Figure 18 - state table encoded in binary
or simply
Q1Q2x D1 D2 Z
000 0 0 0
001 0 1 0
010 1 0 0
011 0 1 0
100 0 0 0
101 1 1 0
110 1 0 1
111 0 0 1
0 0 1 1 0
D1 D1 = x'Q2 + xQ1Q2'
1 0 0 0 1
Q1Q2
x 00 01 11 10
0 0 0 0 0
D2 D2 = xQ1' + xQ2'
1 1 1 0 1
Q1Q2
x 00 01 11 10
0 0 0 1 0
Z Z = Q1Q2
1 0 0 1 0
Q1
D Q
Q2
D Q
CLK
Implementation
Hardware Implementation
A disadvantage of the pure hardware implementation of the state machine using hardwired gates
and flip-flops is that the design is difficult to modify once it is committed to copper. Another
drawback of the discrete hardware implementation is that it requires significant circuit board
area. It is also difficult to debug if there is anomalous behavior. The greatest advantage of a pure
hardware implementation is that a hardware realization is very fast compared to a software
implementation.
Another form of hardware implementation uses a schematic capture program or a Verilog
implementation to produce a binary file which is loaded onto a field programmable gate array
(FPGA). This architecture typically requires less board space. However, the schematic capture
architecture is sometimes difficult to debug. It is easier to modify the design after it has been
committed to copper. To modify, the schematic or the Verilog firmware is modified and rebuilt
and the FPGA is reprogrammed.
The following state table is the Mealy implementation of the {1 0 1} sequence detector
Present State Next State Output
x=0 x=1 x=0 x=1
S0 S0 S1 0 0
S1 S2 S1 0 0
S2 S0 S1 0 1
(where x is the input)
Figure 23 - {1 0 1} sequence detector example
input clk;
input reset;
output out;
parameter S0 = 2'd0,
S1 = 2'd1,
S2 = 2'd2;
endmodule
Software Implementation
State machines may also be implemented in software using the C programming language. The
code is compiled with a compiler resulting in a binary file which is loaded onto a microprocessor
or microcontroller.
A state machine implementation using a software architecture is significantly easier to debug
than a hardware implementation using discrete flip-flops and combinational logic. Software is
easier to modify than hardware. Simply modify the code, recompile and reload the binary on the
microprocessor. Also, the software implementation may be more flexible than the hardware
design; it may be ported to different hardware platforms.
The main disadvantage of the software implementation is that it may be slower than the
hardware implementation.
The following state table is the Mealy implementation of the same {1 0 1} sequence detector
shown above.
Present State Next State Output
x=0 x=1 x=0 x=1
S0 S0 S1 0 0
S1 S2 S1 0 0
S2 S0 S1 0 1
(where x is the input)
Figure 24 - {1 0 1} sequence detector example
switch (*state)
{
case S0:
if (x == 0)
{
*state = S0;
*out = 0;
}
else
{
*state = S1;
*out = 0;
}
break;
case S1:
if (x == 0)
{
*state = S2;
*out = 0;
}
else
{
*state = S1;
*out = 0;
}
break;
case S2:
if (x == 0)
{
*state = S0;
*out = 0;
}
else
{
*state = S1;
*out = 1;
}
break;
default:
*state = S0;
*out = 0;
break;
}
}
Summary
A state machine is a model used to describe the behavior of a real world system. State machines
are used to solve a large number of problems. They are used to model the behavior of various
types of devices such as electronic control devices, parsing of communications protocols and
programs that perform text or pattern searches.
State machines may be described using a state diagram and a state table. A state diagram is
composed of states, inputs, outputs and transitions between states. A state table describes a state
machine with the present state and input on the left and the next state and output on the right.
State machines may be implemented using either a hardware architecture or a software
architecture. The advantage of a hardware implementation is that it operates very fast, but it is
difficult to modify and usually requires more circuit board space. The advantage of a software
implementation is that it is easier to design and modify, but can be slower than the hardware
equivalent.
References
1. “A New Paradigm for Synchronous State Machine Design in Verilog.” visited 1
November 2010 <http://ideaconsulting.com/smv.pdf>
2. “Finite State Machine – National Institute of Standards and Technology.” 12 May 2008
<http://xw2k.nist.gov/dads/HTML/finiteStateMachine.html>
1. A state machine is used for all of the following purposes except ____________.
a. to describe the behavior of a real world system
b. to model a random event
c. to describe the relationship between the inputs and outputs of a system
d. to ensure the predictability of a control system
4. Two state machine architectures, Mealy and Moore machines, are differentiated by
____________.
a. their output dependencies
b. the number of inputs
c. the number of states
d. their initial state
5. The architecture whose output depends both on input and current state is ____________.
a. Mealy machine
b. Moore machine
c. both Mealy and Moore machines
d. neither Mealy nor Moore machines
1
1/0
0/0
1/0
S̀0 S1
0/0
0/1 0/0
1/0
S3 S2
1/0
7. For the state machine described by the above state diagram, if the current state is S2, an
input of 1 would generate a next state of ____________.
a. S0
b. S1
c. S2
d. S3
8. For the state machine described by the above state diagram, if the current state is S3, an
input of 1 would generate a next state ____________ and output of ____________.
a. next state S0, output 1
b. next state S2, output 0
c. next state S0, output 0
d. next state S1, output 1
2
11. A disadvantage of a hardware implementation of a state machine using flip-flops and
discrete logic is ____________.
a. that the implementation requires significant circuit board space
b. that the design is difficult to modify
c. the design is difficult to debug
d. all of the above
12. A pure hardware implementation of a state machine is often slower than a software
implementation.
a. True
b. False
13. The main advantage of a software implementation of a state machine over a hardware
implementation is ____________.
a. the software implementation is always faster than a hardware implementation
b. the software implementation requires fewer states than a hardware
implementation
c. the software implementation is more versatile, i.e., easier to modify than a
hardware implementation
d. all of the above
14. The component of a state machine, regardless of implementation, that stores the current
state of the system is ____________.
a. the state diagram
b. memory
c. combinational logic
d. a transition
15. A state machine would best be used to describe all of the following except
____________.
a. a household appliance controller
b. a communications protocol that parses symbols as they are received
c. a fractal geometry algorithm
d. an electronic garage door opener