TactivergeLite

Lite / Engineering Complex Systems / Week 24

A state machine in hardware

Turn a state-transition table into a circuit and it is always the same shape: a register and a cloud of gates. Why that shape?

About 35 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Every synchronous digital design, from a traffic-light controller to a CPU's control unit, follows this same template. Once you can turn a table into equations you can build any controller from gates and flip-flops.

Predict

A toggle machine has one flip-flop Q and input T. Each clock, Q flips if T = 1 and stays if T = 0. What is the next-state equation?

  1. Q⁺ = T
  2. Q⁺ = Q AND T
  3. Q⁺ = Q XOR T
  4. Q⁺ = NOT Q
Guess first, then reveal

Q⁺ = Q ⊕ T: XOR outputs the opposite of Q exactly when T = 1. That one gate feeding a D flip-flop is a complete state machine: the parity checker from last lesson.

Discover

In the logic lab, build the next-state logic for the parity machine: inputs Q (current state) and T (new bit), output D = Q XOR T. Then build the 2:1 multiplexer preset and explain how a multiplexer could choose between 'hold' and 'load' for a register.

What the interactive shows (text version)

In the logic lab, D = Q XOR T gives D = 0, 1, 1, 0 for (Q, T) = 00, 01, 10, 11: the next state is the opposite of Q exactly when T = 1. The 2:1 multiplexer outputs input A when its select is 0 and input B when it is 1, which is how a register chooses between keeping its value and loading a new one.

You can also open the interactive on the full site when you have a faster connection.

Understand

The template

State register: n D flip-flops holding the current state code. Next-state logic: combinational gates computing the next code from the current code and the inputs. Output logic: gates computing outputs from the state (Moore) or state and inputs (Mealy). On each rising clock edge the register loads the next state.

From table to equations

Give each state a binary code (S0 = 00, S1 = 01, S2 = 10). Rewrite the table as a truth table with inputs (Q1, Q0, x) and outputs (Q1⁺, Q0⁺). Each column is a Boolean function you can simplify and build from gates. For the 11 detector with this encoding: Q0⁺ = x·¬Q1·¬Q0, Q1⁺ = x·(Q1 + Q0), output = Q1.

Why a clock?

Between edges the gates settle; at the edge, every flip-flop samples together. So the circuit only ever moves from one well-defined state to the next, no matter how messy the gate delays are, as long as the logic settles before the next edge. That timing condition comes back in RTL design.

Explain it another way

Intuitive

The register is the machine's short-term memory; the gates are its rulebook. The clock is a metronome telling it when to turn the page.

Technical

Encoding choice matters: binary uses ⌈log₂N⌉ flip-flops; one-hot uses N flip-flops but simpler, faster logic, popular on FPGAs.

Mathematical

Q⁺ = δ(Q, x), y = λ(Q). For the parity machine Q⁺ = Q ⊕ x, y = Q. Test by enumerating all (Q, x) rows against the table.

Applied

Simple controllers in washing machines and lifts were once exactly this: a few flip-flops and a programmable logic array implementing the next-state equations.

Worked example 1

Verify Q0⁺ = x·¬Q1·¬Q0 and Q1⁺ = x·(Q1 + Q0) for the 11 detector with S0 = 00, S1 = 01, S2 = 10.

x = 0: both are 0, so every state goes to S0. ✓. x = 1 from S0 (00): Q0⁺ = 1, Q1⁺ = 0 → S1 ✓. From S1 (01): Q0⁺ = 0, Q1⁺ = 1 → S2 ✓. From S2 (10): Q0⁺ = 0, Q1⁺ = 1 → S2 ✓. Output y = Q1 is 1 only in S2 ✓.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 1. Which part of a hardware FSM holds the current state?

flip-flops

flip-flops

Practice 5. Why does the state register load only on the clock edge?

Apply it

Write the next-state equations for the 'odd number of 1s' machine and for a 2-bit counter that counts 0, 1, 2, 3, 0… when its input E = 1 and holds when E = 0. Build the counter's Q0⁺ logic in the logic lab.

Reflect

What is the trade-off between binary and one-hot state encoding?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Specifying a state machineA state machine in software →