Skip to content
Tactiverge

School/□ Systems Engineer/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

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

  • ◇ Specify a Moore finite-state machine with a state-transition table.
  • ◇ Design sequence detectors, including overlapping matches.
  • ◇ Test a design exhaustively on short input strings.

2.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?

Guess first. Being wrong here is how the surprise works.

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

Loading the interactive…

Can't use the interactive? Read a description instead

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

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

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?

5.Watch

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

8 min 51 s · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

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

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.

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

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

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A Moore machine outputs 1 for one clock whenever the last 2 input bits were 11 (overlaps allowed). Input, first bit first: 011001011100. How many clocks have output 1?

Practice 4

A state machine has 6 states. How many flip-flops does a binary state encoding need?

flip-flops

Practice 5 · a fresh version every time

A Moore machine outputs 1 for one clock whenever the last 3 input bits were 110 (overlaps allowed). Input, first bit first: 110011111111. How many clocks have output 1?

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

9.Reflect

Concepts: Super-loops and finite state machines