C737 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C7 · Lesson 37 · Finite state machines

A circuit that steps through states

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?

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?

Name it

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 ⌈log2⁡N⌉ 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.

Worked example

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

Show the solution
  1. Count the flip-flops. ⌈log2⁡3⌉=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.

Worked example

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

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

Answer yellow.

Worked 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?

Show the 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

At least how many flip-flops does a binary-encoded state machine with 5 states need?

Check

How many flip-flops does a one-hot state machine with 6 states use?

Check

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

Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.

Check

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?

Check

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

Check

A state machine has 7 states.

(a)

How many flip-flops does a binary encoding need?

(b)

How many codes go unused?

Check

A state machine has s states and i input bits.

Step 1

How many flip-flops does a binary encoding need?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

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

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.