Draft preview. Lessons are being written and reviewed.

Digital Design / Module 6: Finite state machines

32. States, transitions and outputs

Translate behaviour into a state diagram.

Lecture coming soon

See it move

The idea

A finite state machine is a design that remembers where it is in a process, and decides what to do next. Protocol controllers, bus interfaces and CPU control units are all FSMs.

It has three parts:

  • A state register: flops holding the current state.
  • Next-state logic: combinational logic using the state and the inputs.
  • Output logic: combinational logic producing the outputs.

How to choose the states

Ask what the machine must remember in order to decide its future. A state should hold exactly that history and nothing more. Two situations that behave identically for all future inputs can share a state.

From description to diagram

For a door controller:

  • List the situations: closed, opening, open, closing.
  • For each state, consider every input. What if "open" is pressed during closing?
  • Decide the output in each state.
  • Choose the reset state. Closed is the safe choice here.

The design discipline

Check every state against every input. The missed combination, not the main path, is where most FSM bugs live. Ask "what happens if this arrives now?" for each one. The animation above lets you press the buttons in any order and see each transition.

Safe behaviour

Reset should lead to a harmless state: motors off, doors shut, outputs inactive. If a state could lead to physical danger or corrupted data, design a way out.

Common mistake

Creating a state for every value of something that is really a number. If the machine waits 100 cycles, that shouldn't be 100 states. Use one wait state plus a counter. This idea, separating control from data, appears in the Traffic light lesson.

Key ideas

  • List what must be remembered: that is your state set.
  • Every state defines every input's transition.
  • Define safe reset and unused-state behaviour.

Your exercise

Draw a diagram for a two-button door controller.

Interview questions

coreWhat belongs in a state?

basicWhat are the three parts of a finite state machine in hardware?

basicHow do you choose the reset state of a state machine that controls something physical, such as a motor?

coreA door controller has states CLOSED, OPENING, OPEN and CLOSING, and buttons for open and close. Which question must you answer for every state, and what is a sensible answer for open being pressed while CLOSING?

coreA controller must wait 100 cycles between two actions. Someone draws 100 wait states. Does a counter save flops, and why is it still the better design?

coreWhen can two states of an FSM be merged into one?

coreIn this next-state logic, both buttons are pressed at once in the CLOSED state. Which state does the machine go to, and why is this code risky?

always @(*) begin
  next = state;
  if (open_btn)  next = OPENING;
  if (close_btn) next = CLOSING;
end

advancedDesign the control for a serial transmitter that sends a start bit, 8 data bits and a stop bit. How would you structure the states?

advancedA one-hot state machine takes an unsynchronised input straight into its next-state logic. Occasionally it ends up with two hot bits or none. How can that happen?

Practise these 9 questions with animations