Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 37 ยท Finite State Machines

The Two-Process FSM Style

Write a clean VHDL state machine with an enumeration type, a clocked state register process and a combinational next-state process, plus a traffic light example.

There are many ways to write an FSM in VHDL, and some of them synthesize badly. The most widely taught and safest layout is the two-process style: one clocked process for the state register and one combinational process for the next-state and output logic. It mirrors the block diagram from the previous lesson exactly, which makes it easy to read, easy to simulate and hard to get wrong.

As elsewhere in this track, the VHDL below is read-only (no VHDL simulator runs on this site), and the C programs are stand-ins that model the same structure so you can run and change them.

The skeleton

VHDL
library ieee;
use ieee.std_logic_1164.all;

entity traffic is
  port (
    clk, rst : in  std_logic;
    car_wait : in  std_logic;
    red, yellow, green : out std_logic
  );
end entity;

architecture rtl of traffic is
  type state_t is (S_RED, S_GREEN, S_YELLOW);
  signal state, next_state : state_t;
begin

  -- Process 1: the state register (clocked)
  state_reg : process (clk)
  begin
    if rising_edge(clk) then
      if rst = '1' then
        state <= S_RED;
      else
        state <= next_state;
      end if;
    end if;
  end process;

  -- Process 2: next-state and output logic (combinational)
  comb : process (state, car_wait)
  begin
    next_state <= state;          -- default: stay
    red <= '0'; yellow <= '0'; green <= '0';   -- default outputs
    case state is
      when S_RED =>
        red <= '1';
        if car_wait = '1' then next_state <= S_GREEN; end if;
      when S_GREEN =>
        green <= '1';
        next_state <= S_YELLOW;
      when S_YELLOW =>
        yellow <= '1';
        next_state <= S_RED;
    end case;
  end process;

end architecture;

Each piece has a job.

The type and the two signals

state holds the present state; next_state holds the value the register will take on the next clock edge. Keeping them separate is what lets the combinational process compute the future without touching the flip-flops.

The register process

Its sensitivity list holds only clk, because a register only reacts to the clock. With a synchronous reset (checked after the clock edge test) the reset takes effect on the next edge. An asynchronous reset would check rst before the edge test; on FPGAs the synchronous form is usually preferred. Chapter 7 returns to this choice.

The combinational process

Its sensitivity list must contain every signal it reads: state and car_wait. If you forget one, simulation will not match the hardware that gets synthesized. (VHDL-2008 allows process (all), which removes this class of mistake.)

Defaults prevent latches

The two lines at the top of the combinational process assign a default to next_state and to every output. That guarantees every signal is assigned on every path through the process. If a signal is left unassigned on some branch, VHDL means "keep the old value", and hardware must then remember it, so synthesis inserts a latch. Latches in an FSM are almost always a bug. Defaults at the top are the simplest cure; Inferred Hardware explains the full story.

Covering all cases

A case over an enumeration must cover every value, otherwise the compiler complains. For std_logic_vector states you would add when others =>. A when others branch that returns to a safe state also helps recovery from illegal states.

Do not put the reset in the combinational process. Reset belongs to the flip-flops, so it goes in the register process.

Mealy outputs in the same layout

Moore outputs read only state. For a Mealy output, you just read the input inside the case branch. For example, a "coin rejected" flag could be set in when S_GREEN => if coin = '1' then reject <= '1'; end if;. The input is then part of the sensitivity list. That is the entire difference.

A C stand-in with the same two blocks

The C code below keeps the structure literal: a comb function that computes the next state and outputs from state and inputs (no side effects), and an explicit "clock edge" that copies next_state into state, with a synchronous reset.

C C stand-in: two-process traffic light

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice that at edge 0 the machine is in an unknown state, yet reset forces it to RED for edge 1: exactly why every FSM needs a reset.

One-process and three-process styles

You will meet two relatives:

  • One-process FSM: state and outputs are both written inside the clocked process. Outputs become registered (no glitches, one cycle later). Shorter, but the code mixes "what is next" with "what happens", and mistakes show up as off-by-one-cycle behavior.
  • Three-process FSM: separate processes for the register, the next-state logic and the output logic. Handy when Moore and Mealy outputs are mixed, at the price of more code.

Stick with two processes until you have a reason not to.

C C stand-in: registered outputs (one extra flip-flop stage)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The registered output follows the combinational one exactly one clock later: that is the price of glitch-free outputs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the two-process style keep state and next_state as separate signals?

    next_state is the input of the state flip-flops; state is their output. Separating them mirrors the hardware.
  2. 2. What must the sensitivity list of the combinational process contain?

    Otherwise simulation will not match the synthesized logic. VHDL-2008 process(all) avoids the mistake.
  3. 3. What is the purpose of assigning defaults at the top of the combinational process?

    A signal left unassigned on some path implies memory, so synthesis builds a latch.
  4. 4. Where does the reset belong?

    Reset is a property of the flip-flops, so it lives in the register process.

Summary

  • Two-process FSM: a clocked process for the state register, a combinational process for next state and outputs.
  • Keep state and next_state as separate signals.
  • Assign defaults at the top of the combinational process to avoid latches.
  • Every signal read in the combinational process goes in its sensitivity list (or use process (all)).
  • Reset goes in the register process; registered outputs are glitch-free but one cycle later.

Next: State Encoding and Safe Machines