Latest
VLSI · 7 min read

Finite State Machines in Verilog: Moore vs Mealy, Worked Through a Sequence Detector

How Moore and Mealy state machines differ, how to code both cleanly in Verilog and SystemVerilog, and how state encoding (binary vs one-hot) changes flip-flop count and timing.

Almost every chip is full of state machines. A UART receiver, a DMA engine, a cache controller and a bus protocol handler are all, at their core, a small register that remembers “where we are” plus some logic that decides “where we go next”. If you can write a clean finite state machine (FSM) in Verilog, you can build most control logic. This article works through one example, a 101 sequence detector, as both a Moore and a Mealy machine, and then looks at how state encoding affects the hardware.

What an FSM is made of

Every synchronous FSM has three parts:

  1. A state register: a bank of flip-flops that holds the current state and updates on the clock edge.
  2. Next-state logic: combinational logic that looks at the current state and the inputs and decides the next state.
  3. Output logic: combinational logic that produces the outputs.

The difference between the two classic styles is only in that third part:

  • In a Moore machine, outputs depend on the current state only.
  • In a Mealy machine, outputs depend on the current state and the current inputs.

That one difference changes the number of states, the timing of the output, and where glitches can appear.

The example: detect 101 on a serial input

The input x delivers one bit per clock. The output y must be 1 whenever the last three bits received were 1, 0, 1. We allow overlap, so the stream 10101 contains two matches: the final 1 of the first match is the first 1 of the next.

Moore version: four states

A Moore machine must have a state that means “I have just seen 101”, because the output is attached to states. We name each state after the useful suffix of the input seen so far:

State Meaning y Next if x=0 Next if x=1
S0 nothing useful yet 0 S0 S1
S1 seen 1 0 S10 S1
S10 seen 10 0 S0 S101
S101 seen 101 1 S10 S1

Two transitions are worth checking by hand. From S1, another 1 keeps us in S1, because 11 still ends in a useful 1. From S101, a 0 takes us to S10, not S0, because the input now ends in ...1010 whose useful suffix is 10. That transition is what gives us overlap.

Mealy version: three states

A Mealy machine can raise the output on the transition out of S10 when x=1, so it does not need a separate “matched” state:

State Next / y if x=0 Next / y if x=1
S0 S0 / 0 S1 / 0
S1 S10 / 0 S1 / 0
S10 S0 / 0 S1 / 1

After a match, the machine goes to S1, since the last bit received was a 1 that can start the next match. One fewer state, and the output appears earlier, as the timing section shows.

Coding the Moore machine

The cleanest style keeps the state register, next-state logic and output logic in separate blocks. That keeps sequential and combinational code apart, which is exactly the rule from blocking vs non-blocking assignments: <= in the clocked block, = in the combinational ones.

module det101_moore (
  input  logic clk, rst_n, x,
  output logic y
);
  typedef enum logic [1:0] {S0, S1, S10, S101} state_t;
  state_t state, next;

  // 1. State register
  always_ff @(posedge clk or negedge rst_n)
    if (!rst_n) state <= S0;
    else        state <= next;

  // 2. Next-state logic
  always_comb begin
    next = state;              // default avoids latches
    case (state)
      S0:   next = x ? S1   : S0;
      S1:   next = x ? S1   : S10;
      S10:  next = x ? S101 : S0;
      S101: next = x ? S1   : S10;
      default: next = S0;
    endcase
  end

  // 3. Output logic: depends on state only
  assign y = (state == S101);
endmodule

Note the default assignment next = state; at the top of the combinational block. Without it, a missing case branch would make synthesis infer a latch. The default branch also gives a defined recovery path if the register ever lands in an unused code.

Coding the Mealy machine

module det101_mealy (
  input  logic clk, rst_n, x,
  output logic y
);
  typedef enum logic [1:0] {S0, S1, S10} state_t;
  state_t state, next;

  always_ff @(posedge clk or negedge rst_n)
    if (!rst_n) state <= S0;
    else        state <= next;

  always_comb begin
    next = state;
    y    = 1'b0;
    case (state)
      S0:  next = x ? S1 : S0;
      S1:  next = x ? S1 : S10;
      S10: begin
             next = x ? S1 : S0;
             y    = x;         // output depends on the input
           end
      default: next = S0;
    endcase
  end
endmodule

Here y is assigned inside the combinational block alongside next, and it has its own default of 0 so that every path assigns it.

Timing: when does the output appear?

Feed both machines the stream x = 1, 0, 1, 0, 1, one bit per clock, starting from reset. Let cycle \(k\) be the cycle in which the \(k\)-th bit is present on x.

Cycle x Moore state Moore y Mealy state Mealy y
1 1 S0 0 S0 0
2 0 S1 0 S1 0
3 1 S10 0 S10 1
4 0 S101 1 S1 0
5 1 S10 0 S10 1
6 – S101 1 S1 0

Both machines find two matches, which confirms the overlap handling. The difference is timing:

  • The Mealy output rises in the same cycle as the final 1, because y is a combinational function of x.
  • The Moore output rises one cycle later, once the machine has actually entered S101.

The Mealy machine is faster, but its output now has a purely combinational path from input x to output y. If x glitches, y can glitch, and in a larger design that path adds to the timing of whatever block consumes y. A Moore output, by contrast, changes only after a clock edge. A common compromise is to register a Mealy output, which brings back the one-cycle delay but removes the combinational path and its glitches.

State encoding: binary vs one-hot

The enum names are only labels. Synthesis has to pick actual bit patterns, and the choice matters.

Binary encoding uses the minimum number of flip-flops. For \(N\) states it needs

\[n_{FF} = \lceil \log_2 N \rceil\]

so our 4-state Moore machine needs 2 flip-flops and a 10-state machine needs 4.

One-hot encoding uses one flip-flop per state, with exactly one bit set at a time:

\[n_{FF} = N\]

so the 10-state machine needs 10 flip-flops instead of 4. That sounds wasteful, but the next-state and output logic usually become simpler: checking “am I in state 7?” is a single bit rather than a 4-bit compare. Shallow logic means a shorter critical path, which is why one-hot is popular in FPGAs, where flip-flops are plentiful.

Gray encoding changes only one bit between consecutive states. It suits machines that mostly move in a fixed sequence (like counters), and it reduces switching activity.

A quick comparison for \(N = 10\):

Encoding Flip-flops Unused codes Typical decode cost
Binary 4 \(2^4 - 10 = 6\) multi-bit compare
One-hot 10 \(2^{10} - 10 = 1014\) single bit
Gray 4 6 multi-bit compare

Those unused codes are the reason for the default branch. If a soft error or a reset problem puts the register into an illegal pattern, the machine should return to a known state rather than lock up. Most synthesis tools can also be told to build a “safe” FSM that recovers from illegal states automatically.

A few habits that prevent bugs

  • Reset to a known state, always.
  • Give every combinational output a default at the top of the block.
  • Keep the state register in its own always_ff block, with only <=.
  • Use typedef enum in SystemVerilog, so waveforms show state names and the tools can choose the encoding.
  • Register outputs that leave the block, unless you really need the Mealy cycle of advantage.

Key takeaways

  • An FSM is a state register plus next-state logic plus output logic.
  • Moore outputs depend only on the state; Mealy outputs also depend on the inputs.
  • Mealy machines often need fewer states and respond one cycle sooner, but create a combinational input-to-output path that can glitch.
  • Binary encoding needs \(\lceil \log_2 N \rceil\) flip-flops; one-hot needs \(N\) but often gives faster logic.
  • Defaults in combinational blocks and a default case branch prevent latches and lock-ups.

#verilog#systemverilog#fsm#rtl#digital-design

LearnSilicon

Clear, practical articles on VLSI, chip design and electronics, from first principles. About LearnSilicon →