Textbooks/Digital Logic and Computer Architecture/Chapter 6
6State machines and sequential logic
Version 1 · Technically checked (automated checks only) · 4 source lessons · 7 practice items
Introduction
Combinational logic forgets: its output depends only on the inputs right now. Real machines need memory. A finite-state machine remembers which state it is in and moves between states when events arrive. This chapter specifies state machines precisely, then builds the same machine twice: in hardware with flip-flops and a clock, and in software as a loop.
Explanation
This chapter is built from 4 School lessons. Each part below is the lesson's own explanation; open the lesson for its predict-and-discover activities.
6.1 States, events and guards
A delivery robot gets an order with 25 % battery left. Should it go, or charge first? Who decides, and where is that rule written?
The four ingredients
A state is a mode the robot can be in (IDLE, DRIVING). An event is something that happens (an order arrives, a bump sensor fires). A transition says: in this state, on this event, go to that state. A guard is a condition that must also be true, such as battery ≥ 30 %.
Guards split one event into cases
IDLE + order has two rules: battery ≥ 30 % → DRIVING, battery < 30 % → CHARGING. Together the guards cover every battery level exactly once. If they overlapped, two rules could fire and the result would depend on which is listed first; if they left a gap, some orders would vanish.
Ignored events
If no rule matches, the machine stays where it is. That is often right (a second order while driving), but it can hide a missing rule. Review each state and ask, for every event: should something happen here?
Common mix-ups
- Guards are just extra if-statements you can add anywhere.
✓ Guards on the same state and event must together cover every case exactly once, or behaviour becomes order-dependent or silently lost.
6.2 Specifying a state machine
A lock that opens only when you type 1-0-1 has to remember something between key presses. What is the least it needs to remember?
The ingredients
A finite-state machine has a set of states, a start state, inputs, outputs and a transition function next = f(state, input). In a Moore machine the output depends only on the state; in a Mealy machine it depends on the state and the current input (often fewer states, but outputs can glitch with the input).
The table is the design
A state-transition table lists, for every state and every input, the next state and the output. Every cell must be filled: an empty cell is an unhandled case, which in hardware means undefined behaviour. Name states by what they remember ('seen 10') rather than S2, and the table almost writes itself.
Overlaps and testing
In 1101, the pattern 101 ends at position 4 and its last 1 can start a new match. A correct detector, after finding 101, goes to the state that remembers '1' rather than back to the start. Because inputs are finite, short machines can be tested exhaustively: the designer checks all 510 strings up to 8 bits.
Common mix-ups
- After a match, a detector should always restart.
✓ If overlaps count, it must keep the useful suffix of what it has seen. - More states make a machine more powerful.
✓ Extra states that remember nothing new just waste flip-flops; aim for the minimal correct machine.
6.3 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?
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.
Common mix-ups
- The clock tells the gates when to compute.
✓ Gates compute continuously; the clock tells the flip-flops when to capture the result.
6.4 A state machine in software
The same traffic-light controller can be twelve gates and four flip-flops, or twenty lines of code in a loop. When would you choose each?
The super-loop pattern
One variable holds the state. A loop runs forever: read inputs, then switch (state); each case sets outputs, checks its exit conditions and, if one is true, assigns the next state and records the entry time. Nothing blocks, so the loop runs many times per second and every input is noticed promptly.
Timed states and guards
Most real machines leave a state on a timeout or an event. Store entered = now() when entering a state and test now() − entered ≥ duration. Guard conditions (only go to WALK if the button was pressed AND the all-red has finished) are the software version of AND gates in next-state logic.
Hardware or software?
Hardware FSMs react within one clock cycle and keep running if the processor crashes; they are fixed once built. Software FSMs are easy to change and log but react only as fast as the loop and depend on the code being correct. Safety systems often use both: software for normal control, a simple hardware or independent monitor that forces a safe state.
Common mix-ups
- A state machine in code needs a special library.
✓ A variable, a loop and a switch are all it takes.
Worked examples
States, events and guards: example 1
Battery 45 %. Events: order, arrived, done, order. Where does the robot end?
Solution
IDLE → (45 ≥ 30) DRIVING → DELIVERING → IDLE, battery 25 %. Next order: 25 < 30, so CHARGING. Final state: CHARGING.
Specifying a state machine: example 1
Fill in the transition table for 'output 1 after an odd number of 1s'.
Solution
Two states: EVEN (output 0) and ODD (output 1). From EVEN: 0 → EVEN, 1 → ODD. From ODD: 0 → ODD, 1 → EVEN. Start in EVEN.
A state machine in hardware: example 1
Verify Q0⁺ = x·¬Q1·¬Q0 and Q1⁺ = x·(Q1 + Q0) for the 11 detector with S0 = 00, S1 = 01, S2 = 10.
Solution
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 ✓.
A state machine in software: example 1
Sketch the super-loop for a two-state blinker: LED_ON for 200 ms, LED_OFF for 800 ms.
Solution
loop: if state == ON: led(1); if now − t0 ≥ 200: state = OFF; t0 = now. elif state == OFF: led(0); if now − t0 ≥ 800: state = ON; t0 = now. No delays, so a button could be read in the same loop.
Interactive figures
Interactive figure (online). Description: The state-machine runner shows a table of rules: IDLE plus order goes to DRIVING if battery is at least 30 %, else CHARGING; DRIVING plus arrived goes to DELIVERING; DRIVING plus bump goes to FAULT; DELIVERING plus done goes to IDLE and costs 20 % battery; CHARGING plus charged goes to IDLE at 100 %; FAULT plus reset goes to IDLE. Sending events steps through the table.
Interactive figure (online). Description: One correct 11 detector: S0 (output 0) goes to S0 on 0 and S1 on 1; S1 (output 0) goes to S0 on 0 and S2 on 1; S2 (output 1) goes to S0 on 0 and stays in S2 on 1. One correct 101 detector uses four states meaning: nothing useful, seen 1, seen 10, seen 101 (output 1, goes to 'seen 10' on 0 and 'seen 1' on 1).
Laboratory
Everything runs in your browser. No kit, nothing mains-powered. Simulations are simplified educational models.
Silicon Studio stage
Silicon Studio: State machines
Build a sequence detector and test it with an input that contains overlapping matches.
Open Silicon Studio: State machines →Nexus project
Nexus: Smart junction
Find the states and events in the junction controller, and check that no input sequence lights two conflicting greens.
Open Nexus: Smart junction →BUILD level
BUILD: Traffic system, Level 2
Write the controller's transition table, including what happens to events the current state ignores.
Open BUILD: Traffic system, Level 2 →
Guided practice
Answers are checked on the server; wrong answers get a hint and another try. Signed in, solved items also count towards the source lesson.
- Which pair of guards on IDLE + order is complete and non-overlapping?
- battery > 30 and battery < 30
- battery ≥ 30 and battery < 30
- battery ≥ 30 and battery ≤ 30
- Which of these is an event rather than a state?
- CHARGING
- bump sensor triggered
- FAULT
- In a Moore machine the output depends on…
- Only the current input
- Only the current state
- The state and the current input
- The clock frequency
- A state machine has 6 states. How many flip-flops does a binary state encoding need? (flip-flops)
- A controller has 9 states with binary encoding. How many flip-flops? (flip-flops)
- The same 9-state controller uses one-hot encoding. How many flip-flops? (flip-flops)
- Why avoid long blocking delays inside a state machine's loop?
- They use too much memory
- Nothing else, including inputs, is checked while the delay runs
- They make the code longer
- Delays are illegal in C
Engineering applications
Controllers you can test
Lifts, traffic lights, vending machines and communication protocols are written as state machines because a transition table can be checked arrow by arrow.
Hardware or firmware
The same state machine can live in flip-flops, reacting within one clock cycle, or in a software loop that is easier to change. The choice depends on how fast the machine must respond.
Watch
Optional verified videos. Nothing loads from YouTube until you press play.
- Computers Without Memory - Computerphile (Computerphile)
Chapter summary
- A state machine has states, events, transitions and outputs; guards split one event into cases.
- A Moore machine's outputs depend only on the state; a Mealy machine's also depend on the inputs.
- With binary encoding, n states need ceil(log2 n) flip-flops; one-hot encoding uses one flip-flop per state.
- A clock makes every flip-flop update at the same instant, so the next state is computed from a stable current state.
Glossary
- D flip-flop
- A D flip-flop stores one bit. On each rising clock edge it copies its D input to its Q output and holds that value until the next edge.
- Registers
- Registers are a small number of very fast storage locations inside the processor that hold the values it is working on right now.
- Clock speed, cycles and CPI
- Execution time equals the number of instructions times the average cycles per instruction (CPI) divided by the clock frequency.
- Propagation delay
- Propagation delay is the time a gate's output takes to respond to a change at its input. The slowest path between flip-flops, the critical path, sets the fastest safe clock.
Definitions from the Field Guide.
Project: A pedestrian crossing controller
Specify a controller for a single pedestrian crossing with a request button and a timer. Draw the states and transitions, list every event each state ignores, and describe how you would implement it in flip-flops and as a software loop.
Deliverables
- A state diagram and transition table
- A list of ignored events per state
- The number of flip-flops for binary and one-hot encodings
- A test sequence that visits every transition
Keep your work in your Engineering Journal; it stays private unless you choose to share it.
References
- David Money Harris and Sarah L. Harris (2012). Digital Design and Computer Architecture, 2nd edition. Morgan Kaufmann.
- MIT OpenCourseWare (2017). 6.004 Computation Structures (Spring 2017). Massachusetts Institute of Technology. Link ↗
Publication record
- Version
- 1
- Status
- Technically checked (automated checks only)
- Technical review
- Automated checks only, last run 2026-11-29. No human, educator or mentor review is claimed.src/content/textbooks/textbooks.test.ts: schema, every lesson, diagram, practice item, video, Field Guide entry, lab and reference resolves; figure numbers recomputed; no practice answers in client payloads.
- Written by
- Compiled and written by the Tactiverge team with AI assistance, from Tactiverge School lessons that were themselves written with AI assistance.
- Licence
- All rights reserved by the founders unless a licence is approved.
- Corrections
- None recorded yet.Spotted a mistake? Use “Report a problem” on any chapter, or see the corrections page.