TactivergeLite

Lite / Engineering Complex Systems / Week 24

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?

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

Why should I care?

Most embedded firmware is a handful of state machines running in a loop. Writing them explicitly, with one variable for the state and a table-like switch, is what keeps safety-critical controllers like traffic lights auditable.

Predict

A software traffic controller is in state GREEN_NS for 30 s, then must go to AMBER_NS. Where should the 30 s timer be checked?

  1. In a delay that blocks the whole loop
  2. Inside the GREEN_NS case, comparing elapsed time with 30 s
  3. In a separate program
  4. Nowhere: it happens automatically
Guess first, then reveal

Inside the case: each pass of the loop checks 'has 30 s elapsed since I entered this state?' A blocking delay would freeze every other job, including the pedestrian button.

Discover

Open the traffic system at Level 2 and build the junction controller as a cycle of timed states. Before running, write the state table on paper. Then run the safety audit and fix anything it flags.

What the interactive shows (text version)

The traffic build's Level 2 lets you list timed states (for example north–south green 30 s, amber 3 s, all-red 2 s, east–west green 30 s…) and audits them for conflicts, missing ambers and missing all-red gaps. The same list can be written as a switch statement in a loop.

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

Understand

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.

Explain it another way

Intuitive

The state variable is a bookmark. Each time round the loop you open the book at the bookmark, follow that page's instructions, and maybe move the bookmark.

Technical

A table-driven FSM stores transitions in a data structure instead of a switch, which makes it easier to verify automatically and to generate from a specification.

Mathematical

Worst-case reaction time = loop period. If one pass takes at most 2 ms, an event is noticed within 2 ms and acted on within 4 ms (it may arrive just after its check).

Applied

Railway and road signal controllers are specified as state machines with interlocking rules, then checked so that conflicting greens are unreachable from any state.

Worked example 1

Sketch the super-loop for a two-state blinker: LED_ON for 200 ms, LED_OFF for 800 ms.

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.

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. Why avoid long blocking delays inside a state machine's loop?

ms

Practice 3. Which is a strength of a hardware FSM over a software one?

Practice 5. A junction controller passes a safety audit 'no conflicting greens in any reachable state'. Why check every reachable state, not just a normal cycle?

Apply it

Write pseudocode for a pedestrian crossing with states GREEN_CARS, AMBER, RED_ALL, WALK, FLASHING. Include the button as an event and every timeout. Then list two input sequences you would test and the states each visits.

Reflect

What would you want a hardware safety monitor to check even if the software controller is correct?

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

← A state machine in hardwareThe contract between code and silicon →