Draft preview. Lessons are being written and reviewed.

Digital Design / Module 6: Finite state machines

37. Traffic light and vending machine

Design control logic from a real specification.

Lecture coming soon

See it move

The idea

Real controllers rarely need many states, as long as you separate control from data. An FSM decides what to do next, while counters and registers hold the numbers.

Vending machine

An item costs 15, and the machine accepts coins of 5 and 10. The only history that matters is the credit so far, so three states are enough: 0, 5 and 10.

StateCoin 5Coin 10
Credit 0Credit 5Credit 10
Credit 5Credit 10Vend, credit 0
Credit 10Vend, credit 0Vend, return 5, credit 0

If prices or coin values grow, the state count explodes. The better design then holds the credit in a register, with a small FSM controlling it.

Traffic light

The sequence is north-south green, north-south yellow, east-west green, east-west yellow. Each phase lasts some number of cycles. Without a timer, a 60-cycle green requires 60 states. With a counter, the FSM has just four:

always_ff @(posedge clk)
  if (rst) begin
    state <= NS_GREEN;
    timer <= GREEN - 1;
  end else if (timer == 0) begin
    case (state)
      NS_GREEN:  begin state <= NS_YELLOW; timer <= YELLOW - 1; end
      NS_YELLOW: begin state <= EW_GREEN;  timer <= GREEN - 1;  end
      EW_GREEN:  begin state <= EW_YELLOW; timer <= YELLOW - 1; end
      default:   begin state <= NS_GREEN;  timer <= GREEN - 1;  end
    endcase
  end else
    timer <= timer - 1'b1;

Changing the timing now means changing a number, not redesigning the machine.

Common mistake

Missing cases that a real user would hit. What if a 10 coin is inserted during vending? What happens at the instant of reset: are both directions red? Walk through odd sequences, not just the expected one. Designs fail on the sequences their authors never imagined.

One more to try

Key ideas

  • Timers pair with FSMs through a counter.
  • Accumulated value can be separate data rather than extra states.
  • Separating control and datapath keeps state count low.

Your exercise

Build a vending FSM accepting 5 and 10 for a 15-cost item.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: Coins of 10 + 10 vend correctly, but 5 + 10 and 5 + 5 + 5 keep the money and vend nothing.

Interview questions

coreWhen should you add a datapath counter instead of states?

basicA traffic light has a 60-cycle green and a 5-cycle yellow for each direction. How many states does it need without a timer, and how many with one?

basicA vending machine sells a 15-unit item and takes coins of 5 and 10. Why are three states enough?

coreA phase controller loads its timer with GREEN and counts down, changing phase when the timer reaches 0. The green phase comes out one cycle too long. Why?

NS_GREEN: begin state <= NS_YELLOW; timer <= YELLOW; end
NS_YELLOW: begin state <= EW_GREEN; timer <= GREEN; end
// timer counts down; phase changes when timer == 0

coreIf the vending item cost 40 instead of 15, with the same 5 and 10 coins, how many credit states would a pure FSM need? What is the better design?

coreShould the traffic light outputs (the lamp drives) be Moore or Mealy outputs? Why?

coreThe traffic controller's reset puts it straight into north-south green. What would a careful reviewer ask about this?

advancedA pedestrian button gives a one-cycle pulse at any time, and the controller should serve it at the end of the current green. How do you make sure the request is never lost?

advancedThis register-based vending logic loses money in one case. Find it.

always @(posedge clk)
  if (rst)               credit <= 0;
  else if (credit >= 15) credit <= credit - 15;  // vend
  else if (coin5)        credit <= credit + 5;
  else if (coin10)       credit <= credit + 10;
assign item = (credit >= 15);

Practise these 9 questions with animations