TactivergeLite

Lite / Textbooks / Digital Logic and Computer Architecture / Chapter 6

6. State machines and sequential logic

Version 1 · Technically checked (automated checks only) · 4 source lessons · Full interactive version

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.

Figure 6.1. A two-state turnstile (described in words)

Two states, Locked and Unlocked. From Locked, a coin event moves to Unlocked and a push leaves it Locked. From Unlocked, a push moves back to Locked and a coin leaves it Unlocked. An arrow marks Locked as the starting state.

Explanation

Built from 4 School lessons. Each part is the lesson's own explanation.

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

Open the Lite lesson: States, events and guards

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

Open the Lite lesson: Specifying a state 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

Open the Lite lesson: A state machine in hardware

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

Open the Lite lesson: A state machine in software

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, in words

Figure 6.2 from States, events and guards

Load the complete design. Set the battery to 25 % and send: order, charged, order, arrived, done. Write the state after each event before you press it. Then load each 'Bug' design and read what the checker says.

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.

Figure 6.3 from Specifying a state machine

Choose 'Detect 11' with 3 states and fill in the table until 'Check my machine' passes. Then try 'Detect 101' with 4 states. If a check fails, type the failing string into the input box and trace it.

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).

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. Which pair of guards on IDLE + order is complete and non-overlapping?
    Answer it in “States, events and guards”
  2. Which of these is an event rather than a state?
    Answer it in “States, events and guards”
  3. In a Moore machine the output depends on…
    Answer it in “Specifying a state machine”
  4. A state machine has 6 states. How many flip-flops does a binary state encoding need?
    Answer it in “Specifying a state machine”
  5. A controller has 9 states with binary encoding. How many flip-flops?
    Answer it in “A state machine in hardware”
  6. The same 9-state controller uses one-hot encoding. How many flip-flops?
    Answer it in “A state machine in hardware”
  7. Why avoid long blocking delays inside a state machine's loop?
    Answer it in “A state machine in software”

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)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

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. Field Guide
Registers
Registers are a small number of very fast storage locations inside the processor that hold the values it is working on right now. Field Guide
Clock speed, cycles and CPI
Execution time equals the number of instructions times the average cycles per instruction (CPI) divided by the clock frequency. Field Guide
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. 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

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

References

← 5. From silicon to switches7. Instructions and the datapath →