T=0T=0T=0T=1T=1T=1GREEN00YELLOW01RED1001WS1S00101NEXT00OUT0101
Lit wire with moving dashes: it carries a 1Lamps: a glowing lamp shows 1, a dark one 0State diagram: the glowing circle is the current statePuck and token: the next state moving in on a clock edgeA traffic light as a finite state machine. The state diagram (back) has one circle per state and one arrow per move; the circuit (front) stores the state in two flip-flops, works out the next state from the state and the timer input T, and moves only on a rising clock edge. Moore machine: the outputs depend only on the current state. Mealy machine: the outputs depend on the current state and the inputs. The names come from Edward F. Moore (1956) and George H. Mealy (1955). With n flip-flops a register can hold 2^n patterns, so two flip-flops are enough for three states. Here the unused pattern 11 is sent back to GREEN, a safe choice if it ever appears. Real traffic controllers time each light and also read sensors and push buttons; this machine keeps only the timer so the idea stays clear.

States and memory

  1. A finite state machine is a circuit that is always in one of a few states. This traffic light has three: GREEN, YELLOW and RED. A state diagram draws each state as a circle; the glowing one is the state right now.

Making a move

The big picture

Tables and code

;; step 1/9 · fsm-idea
;; about: State diagram
finite state machine (FSM):
  a fixed, small set of states
  exactly one is current at a time

states of this machine:
  GREEN   YELLOW   RED

state diagram:
  circle = a state
  arrow  = a move to another state

A finite state machine is a state register (flip-flops) plus two blocks of combinational logic: next-state logic and output logic (Harris and Harris, Digital Design and Computer Architecture, section 3.4). A D flip-flop copies its input to its output only on the rising edge of the clock and holds it at all other times. Texas Instruments' SN74HC74 holds two of them in one chip.