TactivergeLite

Lite / Engineering Complex Systems / Week 24

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?

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

Why should I care?

Protocol decoders, vending machines, traffic lights and every processor's control unit are finite-state machines. Writing the table first, before any code or gates, is how engineers make sure every case is handled.

Predict

A Moore machine detects the pattern 11 (output 1 when the last two inputs were 1). How many states does it need at minimum?

  1. 1
  2. 2
  3. 3
  4. 4
Guess first, then reveal

Three: 'last bit not 1', 'last bit was 1' and 'last two bits were 1' (which outputs 1). In a Moore machine the output belongs to the state, so the 'found it' moment needs its own state.

Discover

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.

What the interactive shows (text version)

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

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

Watch (optional)

Watch for: Listen for what a state 'means': it summarises everything about the past that matters for the future.

Introduces machines that run on states and transitions, the same idea a traffic-light controller uses.

“Computers Without Memory - Computerphile” by Computerphile (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

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.

Explain it another way

Intuitive

A state is a sticky note saying exactly what you need to remember, no more. The table says, for each note and each new event, which note to write next.

Technical

Minimal state count follows from which input histories must be distinguished (Myhill–Nerode). Equivalent states, those with identical outputs and transitions, can be merged.

Mathematical

Moore machine M = (S, s₀, Σ, Λ, δ, λ) with δ: S × Σ → S and λ: S → Λ. Output after input u is λ(δ*(s₀, u)).

Applied

Serial receivers (UARTs) use an FSM to find a start bit, sample eight data bits and check the stop bit; USB and Ethernet controllers contain dozens of such machines.

Worked example 1

Fill in the transition table for 'output 1 after an odd number of 1s'.

Two states: EVEN (output 0) and ODD (output 1). From EVEN: 0 → EVEN, 1 → ODD. From ODD: 0 → ODD, 1 → EVEN. Start in EVEN.

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. In a Moore machine the output depends on…

Practice 3. A 101 detector has just output 1. The next input is 0. Where should it go?

flip-flops

Apply it

Design a Moore machine for a turnstile: states LOCKED and UNLOCKED, inputs COIN and PUSH. Write its full table, then add a third state ALARM for 'pushed while locked three times in a row' and say what resets it.

Reflect

What did each state in your 101 detector 'mean' in words?

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

← Where a chip's power goesA state machine in hardware →