EE 322 · Week 1

Finite State Machines in VHDL

A circuit can react to the present and remember just enough of the past to choose its next action.

  1. remember
  2. cycle
  3. structure
  4. detect
  5. compare

By the end: read an FSM as a diagram, table, timing trace, and VHDL implementation; explain what each state remembers; and predict the different output timing of Moore and Mealy detectors.

This lesson supports EE 322 CLOs 1.3 and 2.2. The checks are formative, with suggested teaching alignment; they are not formal outcome evidence. Teach from the slide deck →

00 — Why an FSM?

What must this circuit remember?

Combinational logic produces an output from the inputs that exist now. A digital system also needs to respond to a sequence: what happened on earlier clock cycles can change what it should do next. That is sequential logic.

Current state+Inputs→Next state

The state register holds that memory. At a clock edge, it loads the next state calculated from the current state and inputs. Reset places it in a known starting state.

State memory

Flip-flops retain the current state between clock edges.

Next-state logic

Combinational logic computes the state to load at the next edge.

Output logic

Combinational logic produces the visible output from state, or state and input.

Basic finite state machine architecture Inputs and the current state feed next-state logic. Its result enters a state register on a clock edge. Reset initializes the register. Current state feeds the output logic; a Mealy output can also depend on input. Next-statelogic State registerclocked memory Output logic Inputs nextstate state Output current state feedback input to Mealy output logic clock ↑ · reset
The register changes state at an edge; the two logic blocks are combinational. A Moore output uses state only. A Mealy output also uses the present input.

Moore

y = f(state). Output changes when state changes, normally after a clock edge or reset.

Mealy

y = f(state, input). Output can respond to an input change between clock edges.

01 — Example 1 · divideby3FSM

Three states make a repeating pulse

Start with the smallest possible transition problem: there is no data input. Every rising edge advances one place around a three-state cycle. Reset selects S0.

S0→S1→S2→S0→ …
Divide-by-3 state diagram On successive rising edges S0 changes to S1, S1 changes to S2, and S2 returns to S0. Output y is one in S0 and zero in S1 and S2. S0y = 1 S1y = 0 S2y = 0 next rising edge
Each arrow means “load the next state at a rising edge.” The state label also tells you the output during that state.
Current stateNext stateOutput y
S0S11
S1S20
S2S00

Read each row as an edge-to-edge rule. In S1, for instance, y is low now and the next rising edge loads S2.

State declaration · 0430-divideby3FSM.vhdDownload teaching copy ↓
type statetype is (S0, S1, S2);
signal state, nextstate: statetype;

statetype defines three symbolic values. state is the value held in the register; nextstate is the value waiting to be loaded.

State register · standard VHDL teaching copy
process(clk, reset) begin
  if reset = '1' then state <= S0;
  elsif rising_edge(clk) then
    state <= nextstate;
  end if;
end process;
Next-state and output logic · source excerpt
nextstate <= S1 when state = S0 else
             S2 when state = S1 else
             S0;

y <= '1' when state = S0 else '0';

The code follows the same three objects as the diagram and table: the register stores the state, the transition expression selects the next one, and the output expression marks S0 with a high pulse.

Read the timing at rising edges

Starting from reset in S0, each rising edge loads the next state. The output is high only while S0 is held:

Keep the three views together: the state diagram shows the loop, the table lists each transition, and the VHDL implements exactly those rows.

02 — Reusable implementation structure

Find the three pieces in every FSM

The next two designs add an input, but keep the same architecture. When reading FSM VHDL, identify these parts before following individual conditions.

1 · State memory

A clocked process loads nextstate into state; reset initializes it.

2 · Next-state logic

nextstate = f(state, inputs). Every state and input case must lead to a defined next state.

3 · Output logic

Moore: y = f(state). Mealy: y = f(state, inputs).

03 — Example 2 · patternMoore

Detect 01 by remembering a useful prefix

Now a serial input a supplies one bit per clock. We want a pulse when the most recently sampled two bits are 01. Before looking at VHDL, ask: What does the circuit need to remember about the previous input bits? After a 0, it needs to remember that a 1 next would complete the pattern.

Start at S0, with no useful trailing zero. A sampled 0 moves to S1. In S1, another 0 keeps the same useful memory, while a sampled 1 completes 01 and moves to S2. The remaining arrows prepare for the next match.

StateWhat it remembersa=0 →a=1 →y
S0No useful trailing 0S1S00
S1Last sampled bit was 0S1S20
S2Last two bits were 01S1S01
Moore 01 detector: S0 on 0 goes to S1, S1 on 1 goes to detection state S2, and y is 1 only in S2. Other arrows preserve useful trailing input history.
Moore outputs are written inside states. S2 is the extra detection state with y = 1.

In S1, another 0 keeps a useful trailing zero. A 1 takes the machine to S2, where the Moore output is high. From S2, a new 0 starts the next possible match. Thus 0101 produces two detections; no bit must be discarded between them.

State register and transition logic · teaching copy of 0431-patternMoore.vhdDownload teaching copy ↓
type statetype is (S0, S1, S2);
signal state, nextstate: statetype;

process(clk, reset) begin
  if reset = '1' then         state <= S0;
  elsif rising_edge(clk) then state <= nextstate;
end process;

process(all) begin
  case state is
    when S0 =>
      if a = '1' then nextstate <= S0;
      else      nextstate <= S1;
      end if;
    when S1 =>
      if a = '1' then nextstate <= S2;
      else      nextstate <= S1;
      end if;
    when S2 =>
      if a = '1' then nextstate <= S0;
      else      nextstate <= S1;
      end if;
    when others => nextstate <= S0;
  end case;
end process;
Output logic · source excerpt
y <= '1' when state = S2 else '0';

The output expression mentions state only. If a changes while the state stays S1, y remains low. The detecting 1 must be sampled at a rising edge to enter S2; then y goes high after the state updates.

Trace four sampled bits from reset. Each row shows the state and Moore output after the edge:

Rising edgeSampled aState after edgey after edge
10S10
21S21
30S10
41S21

04 — Example 3 · patternMealy

Detect the same 01 with the input on the output path

Can we recognize the same event without creating a dedicated detection state? The Mealy design needs only two states. S0 means there is no useful trailing zero; S1 means the last sampled bit was zero. When the circuit is in S1 and a is currently 1, the pattern is present at the input and y asserts directly.

Current stateInput aNext stateOutput y
S00S10
S01S00
S10S10
S11S01
Mealy 01 detector: two states S0 and S1; the S1 to S0 transition on input 1 has output 1, shown as 1 slash 1.
Mealy outputs are written on transitions as input/output. The green 1/1 arrow detects 01; there is no S2.

For a Mealy diagram, label an edge with input/output. The detecting transition is S1 — 1/1 → S0. The output belongs to the transition condition, not to a separate detected state.

Transition logic · teaching copy of 0432-patternMealy.vhdDownload teaching copy ↓
type statetype is (S0, S1);
signal state, nextstate: statetype;

process(all) begin
  case state is
    when S0 =>
      if a = '1' then nextstate <= S0;
      else      nextstate <= S1;
      end if;
    when S1 =>
      if a = '1' then nextstate <= S0;
      else      nextstate <= S1;
      end if;
    when others => nextstate <= S0;
  end case;
end process;
Output logic · source excerpt
y <= '1' when (a = '1' and state = S1) else '0';

Its state register is the same clocked pattern as the Moore design: reset sets S0, and each rising edge loads nextstate. The key difference is visible in the output expression: a appears beside state.

For 0, 1, 0, 1, observe the Mealy output just before each sampling edge, while the old state still holds:

EdgeInput before edgeState before edgey before edgeState after edge
10S00S1
21S11S0
30S00S1
41S11S0

05 — Put the detectors side by side

Same pattern, different output timing

PropertyMoore exampleMealy example
Output depends onStateState and present input
Output associated withA stateA transition condition
States hereThree: S0, S1, S2Two: S0, S1
Response to inputAfter a state updateCan respond within the current state
Timing considerationInput changes do not directly change yInput changes can propagate through the combinational output path

Neither architecture is universally preferable. The Mealy version saves a state and can respond earlier; the Moore version keeps the output independent of direct input changes. Choose according to the interface and timing your system needs.

For a stable input sequence 0, 1, 0, 1, both machines recognize two 01 occurrences. The table below names exactly which side of each edge is being observed.

Input sampled at edgeMoore state after edgeMoore y after edgeMealy y before edge
0S100
1S211
0S100
1S211

The matching values in different columns are not simultaneous output waveforms: the Mealy assertion occurs before the detecting edge; the Moore assertion follows that edge.

Step through the bitstream

Choose the bit present before the next rising edge. Watch the Mealy output in the current state, then clock the bit and see the Moore state and output update.

Input a
Moore, after last edgeState S0 · y = 0
Mealy, before next edgeState S0 · y = 0 for selected a
Clocked input history
EdgeInputMoore state afterMoore y afterMealy y beforeMealy state after
No bits clocked yet. Try 0, then 1.

06 — Check your understanding

Predict before you simulate

After reset, the divide-by-3 machine is S0. What are its state and output after two rising edges?

The Moore detector is in S1 and a changes from 0 to 1 before a clock edge. What does y do at once?

Why does the Mealy 01 detector need a state meaning “last sampled bit was 0”?

Discuss a possible answer

If overlaps are allowed, 10101 contains two matches. A Moore design can use four states: no useful prefix, trailing 1, trailing 10, and detected 101. Only the last state's output is high. The key transitions are: trailing 1 + 0 → trailing 10; trailing 10 + 1 → detected 101; detected 101 + 0 → trailing 10. A Mealy design can use the first three states and assert its output on the 1 transition from “trailing 10”; afterward it remembers the final 1 as a possible start of the next match.

07 — Design method

A repeatable way to design an FSM

Work from required behavior toward code. Each step should agree with the diagram and table before simulation.

  1. Understand the required behavior.
  2. Identify what the circuit must remember from the past.
  3. Define states for that information.
  4. Draw the state diagram.
  5. Build the transition table.
  6. Choose Moore or Mealy outputs.
  7. Write the clocked state register.
  8. Write the combinational next-state logic.
  9. Write the output logic.
  10. Simulate and verify reset, transitions, output timing, and overlaps.

08 — Week summary

State is the useful part of the past

Cycle

In the three-state machine, state is a position in a repeating clock sequence.

Detect

In the pattern detectors, state records a useful suffix of sampled input history.

Implement

Find the state register, next-state logic, and output logic in every VHDL FSM.

Time

Moore output follows state; Mealy output can respond to the current input before an edge.

The original source files remain available: divide-by-3, Moore detector, and Mealy detector. Use the linked teaching copies above when compiling with VHDL-2008.

Back to EE 322