---
title: Finite state machines
heading: A circuit that steps through states
eyebrow: C7 · Lesson 37 · Finite state machines
xp: 60
---

::: hook
A traffic light, a vending machine, a USB controller and the control unit of a processor have something in common: each is always in one of a fixed set of states, and on each clock tick it moves to a next state chosen by its current state and its inputs. That's a **finite state machine**. Designing one is a recipe: draw the states and arrows, encode the states in flip-flops, and write the next-state logic as Boolean equations. The rest of this course's tools then take over.

::: play
- A traffic light controller with three states: green, yellow, red. Two flip-flops hold the state; the lamps decode it.
- Watch the sequence on the timing diagram. Which state comes after red?
- Each state lasts one clock here. How would you make green last longer?
:::
:::

```sim logic
output G, Y, Rd
clock CLK 8
always @(posedge CLK) begin
  S1 <= S0;
  S0 <= ~S1 & ~S0;
end
assign G = ~S1 & ~S0
assign Y = S0
assign Rd = S1
wave: CLK, S1, S0, G, Y, Rd
time: 64
```

::: predict
## The controller above encodes green as S1 S0 = 00, yellow 01, red 10. Its next-state equations are S1 ← S0 and S0 ← ¬S1·¬S0. What happens after red?

- [x] Green
- [ ] Yellow
- [ ] Red again
- [ ] It sticks in an unused state

::: reveal
Green. From red (10): S1 ← S0 = 0 and S0 ← ¬1·¬0 = 0, giving 00. The cycle is 00 → 01 → 10 → 00. The fourth code, 11, is never used; a careful design checks that it leads back into the cycle too, in case a glitch or a power-up ever lands the machine there. (Here, 11 goes to 10, red, so it recovers.)
:::
:::

::: nameit
## Designing a state machine

1. **States and transitions.** Draw a circle for each state and an arrow for each transition, labeled with the input condition that causes it.
2. **Encoding.** Assign each state a binary code. N states need at least $\lceil\log_2 N\rceil$ flip-flops. **One-hot** encoding uses N flip-flops, one per state, which wastes flip-flops but makes the logic simple and fast.
3. **Next-state table.** For each current state and input, list the next state's code.
4. **Logic.** Each flip-flop's D input is a Boolean function of the current state bits and inputs: simplify with K-maps.
5. **Outputs.** In a **Moore** machine, outputs depend only on the state (glitch-free, one cycle behind). In a **Mealy** machine, outputs depend on the state and the inputs (faster to respond, but they can glitch with the inputs).

**Unused codes.** With 3 states in 2 flip-flops, one code is spare. Make sure every unused code leads back to a valid state.

**Timing.** For a state to last several clocks, add a counter: stay in the state until the counter reaches a set value, then move on and reset it.
:::


::: example
## A traffic light cycles green, yellow, red. How many flip-flops does a binary encoding need, and how many codes are unused?

::: solution
1. **Count the flip-flops.**
   $$\lceil\log_2 3\rceil = 2$$
2. **Count the unused codes.** Two flip-flops give 4 codes, leaving 1 unused: the reset logic must make sure the light never starts there.

**Answer:** 2 flip-flops, with 1 of the 4 codes unused.
:::
:::

::: example
## That light advances one state per clock, starting at green. Which color shows after 7 clocks?

::: solution
1. **Divide by the cycle length.** The cycle has length 3, and
   $$7 = 2 \times 3 + 1$$
2. **Step past green.** Two full cycles end back at green, so the light is one step past green: yellow.

**Answer:** yellow.
:::
:::

::: example
## A 2-bit up/down counter is a four-state machine: on U it adds 1, on D it subtracts 1, wrapping around. Starting at 00, it sees U, U, D, U, U, U. Where does it end?

::: solution
1. **Follow the inputs.** 00 → 01 → 10 → 01 → 10 → 11 → 00: the last U wraps from 11 back to 00.
2. **As a state machine.** Each state has two outgoing arrows, one for U and one for D, and the whole behavior is a table of 4 × 2 transitions.
3. **Designing it.** Writing out that table is the design; the logic follows from it mechanically.

**Answer:** it ends at 00.
:::
:::

::: check
given: N = 3 | 5 | 9 | 17
answer: ceil(log(N)/log(2))
tolerance: 0

## At least how many flip-flops does a binary-encoded state machine with {N} states need?

::: right
Right: $\lceil\log_2\given{N}\rceil$ = {answer}.
:::

::: misses
- [N] That's one-hot encoding. Binary needs fewer.
- [floor(log(N)/log(2))] Round up: you need enough codes for every state.
- [any] Not yet. Find the smallest n with $2^n \geq$ {N}.
:::
:::

::: check
given: N = 4 | 6 | 10
answer: N
tolerance: 0

## How many flip-flops does a one-hot state machine with {N} states use?

::: right
Right. One per state: {answer}.
:::

::: misses
- [ceil(log(N)/log(2))] That's binary encoding. One-hot uses one per state.
- [any] Not yet. One flip-flop per state.
:::
:::

::: check
kind: boolean
vars: S1, S0
answer: ~S1 & ~S0
ops: and, or, not

## A 3-state machine cycles 00 → 01 → 10 → 00 in its code S1 S0. Write the next-state equation for S0.

::: right
Right. S0 is next 1 only when leaving 00.
:::

::: misses
- [~S0] That would also send 10 to 11 and 11 to 10.
- [S1] S1 is 1 only in red (10), which goes to green (00): S0 should be 1 next only when leaving green.
- [any] Not yet. Which current state leads to a next state with S0 = 1?
:::
:::

::: check
kind: choice

## A vending machine's "dispense" output must react within the same clock cycle that the last coin is detected. Which kind of machine is that?

- [ ] Moore
- [x] Mealy
- [ ] Either
- [ ] Neither: it needs a counter

::: right
Right. A Mealy output depends on the inputs as well as the state, so it can respond before the next edge.
:::

::: misses
- [1] A Moore output depends only on the state, so it changes one cycle later.
- [3] Only Mealy outputs can react within the cycle.
- [4] A counter could help with timing, but the question is about the output type.
:::
:::

::: check
given: s = 0 | 1 | 2
answer: s + 1 - 3*floor((s + 1)/3)
tolerance: 0

## In the traffic light (0 green, 1 yellow, 2 red), the machine is in state {s}. Which state comes next?

::: right
Right: {answer}.
:::

::: misses
- [s] It moves on every clock.
- [any] Not yet. Green → yellow → red → green.
:::
:::

::: check
kind: parts
given: s = 5 | 7 | 12 | 20

## A state machine has {s} states.

::: part
answer: ceil(log(s)/log(2))
tolerance: 0

### How many flip-flops does a binary encoding need?

::: misses
- [s] That's one-hot.
- [any] $\lceil\log_2s\rceil$.
:::
:::

::: part
answer: 2^ceil(log(s)/log(2)) - s
tolerance: 0

### How many codes go unused?

::: misses
- [any] $2^{\lceil\log_2s\rceil} - s$.
:::
:::
:::

::: check
kind: derivation
vars: s = 2..1000, i = 1..8

## A state machine has s states and i input bits.

::: step
answer: ceil(log(s)/log(2))

### How many flip-flops does a binary encoding need?
:::

::: step
answer: s

### How many does one-hot need?
:::

::: step
answer: s*2^i

### How many transitions must the next-state table specify?
:::

::: right
Right. The table grows with the inputs exponentially, which is why real machines test only a few inputs per state.
:::
:::

::: explain
## Why does a state machine need flip-flops, rather than just combinational logic?

::: model
A state machine's behavior depends on where it is in a sequence, not just on its present inputs: the same input can lead to different outputs depending on what happened before. Combinational logic has no memory, so it can only respond to the present inputs. Flip-flops hold the current state from one clock cycle to the next, and the combinational logic computes the next state from that stored state and the inputs; the clock then moves the machine forward one step at a time.
:::
:::

::: practice

::: check
given: N = 2 | 4 | 8 | 16
answer: log(N)/log(2)
tolerance: 0.5%

## Binary-encoded flip-flops for exactly {N} states?

::: misses
- [N] That's one-hot.
- [any] $\log_2 N$.
:::
:::

::: check
given: n = 2 | 3 | 4
answer: 2^n
tolerance: 0

## How many distinct states can {n} flip-flops encode?

::: misses
- [any] $2^n$.
:::
:::

::: check
kind: choice

## A Moore machine's outputs depend on...

- [x] the current state only
- [ ] the inputs only
- [ ] the state and the inputs
- [ ] the clock

::: right
Right. That's what makes Moore outputs glitch-free.
:::

::: misses
- [2] Then it would be combinational.
- [3] That's Mealy.
- [4] The clock times the changes; the state determines the output.
:::
:::
:::
