Finite State Machines
A finite state machine remembers just enough about the past to decide what to do next: a handful of states, plus a rule that picks the next state from the current one and the latest input. Protocol decoders, traffic lights, vending machines and button debouncers all work this way.
What the symbols mean
- State
What the machine remembers after k inputs.
- Input
The next input symbol, such as a bit.
- Transition function
The table of next states.
- Output function
What the machine outputs in each state (a Moore machine).
What the symbols mean
- Number of states
How many situations the machine has to tell apart.
Explanation
A finite state machine is simply a way to describe what state something is in right now (a traffic light is green, yellow or red) and what makes it switch to another. It's called finite because there's a fixed, limited set of states, and it's always in exactly one of them at a time. That makes it great for computing: one simple description of where things stand is all the machine has to remember. The formula looks abstract, but it's just three pieces: (the state right now), (the next input that comes in) and (a lookup table saying which state comes next), and just reads the output off the current state. It's a fancy way to write a table; bear with the symbols.
Many machines only need to remember a little about the past: a traffic light which color it's showing, a serial receiver which bit comes next, a vending machine how much money has gone in. A finite state machine packages exactly that: a finite set of states, a start state, and a transition function that picks the next state from the current state and the input. Outputs come from the state alone (a Moore machine) or from the state and input together (a Mealy machine, which can react one step sooner).
Example: spot 101 in a bit stream. What does the machine actually need to remember? Only how much of the pattern it has just seen, so that's what the states record:
| state | meaning | on 0 | on 1 | output |
|---|---|---|---|---|
| nothing useful | 0 | |||
| saw 1 | 0 | |||
| saw 10 | 0 | |||
| saw 101 | 1 |
A common mistake. After a match, it's tempting to send the machine back to to start fresh, since the pattern is "done." It isn't done: from , a 0 leaves "10" at the end of the stream, which is exactly , and that's how overlapping matches like 10101 get counted twice. The same goes for on another 1: the stream still ends in a "1", so it stays in . Always ask what's left over at the end of the stream.
In hardware the state lives in flip-flops, and states need of them with binary encoding. On each clock tick, Boolean next-state logic, built with the tools of the logic unit (K-maps included), computes the next state from the current state bits and the input. FPGA tools often use one-hot encoding instead, one flip-flop per state, because the logic gets simpler.
In firmware it's a switch inside a loop or an interrupt handler:
enum { S0, S1, S2, S3 } s = S0;
void on_bit(int b) { // called for each received bit
switch (s) {
case S0: s = b ? S1 : S0; break;
case S1: s = b ? S1 : S2; break;
case S2: s = b ? S3 : S0; break;
case S3: s = b ? S1 : S2; break;
}
if (s == S3) pattern_found();
}
A state diagram is a directed graph, the next unit's subject, with one arrow per state and input. There's an animation further down that runs the 101 detector on its diagram. What a finite state machine can't do is count without limit. A machine with states that reads open parentheses must revisit some state (the pigeonhole principle), so it can't tell those two counts apart, and it can't check that the closing parentheses match. That needs memory that grows, like a stack.
See it
Worked examples
Trace the 101 detector on the input 0110101, starting in .
- 1
The first three bits: .
How many flip-flops does a 6-state machine need?
- 1
With binary encoding, , since .
Practice
Type your answer and check it. Solve them all to complete the lesson.
How many flip-flops does a 10-state machine need with binary state encoding?
Feed 1, 0, 1, 0, 1 into the 101 detector, starting in . How many times does it output 1?
In the 101 detector, where does go on input 0?
A parking meter takes dimes (10¢) and quarters (25¢) and prints a ticket once at least 50¢ has gone in, with no change given. Its controller is a state machine with one state for each amount that can have been paid so far, plus a state for printing the ticket. How many states does it need?
A state machine reads a binary number one bit at a time, most significant bit first, and remembers only the value so far mod 3. Reading bit takes state to . Starting from , which state is it in after reading 1101?
How many different transition tables does a 3-state machine with a 1-bit input have?
Which of these can no finite state machine do?
The hardest one here. Get it, and you've got this lesson.
Design a Moore machine that reads a binary number, most significant bit first, and outputs 1 exactly when the number read so far is divisible by 5. What is the fewest states it can have, and which state (as a remainder) is it in after reading 11011? Enter both.
Where it's used
Every protocol controller on a chip (UART, I²C, USB, Ethernet) is a finite state machine, and so is the main loop of most embedded firmware. Hardware description languages like Verilog have a standard pattern for them, and synthesis tools turn the transition table into flip-flops and gates.