Moore vs Mealy State Machines, With Verilog for Each

In a Moore machine the outputs depend only on the current state, so they change only after a clock edge. In a Mealy machine the outputs depend on the current state and the current inputs, so they can change as soon as an input changes, which usually means fewer states and an answer one clock earlier, at the cost of a combinational path from input to output. The example below builds the same rising-edge detector both ways in Verilog and shows the one-cycle difference in simulation.

What you'll learn

  • The one rule that separates Moore from Mealy, and how it shows up in a state diagram
  • A rising-edge detector as a three-state Moore machine and a two-state Mealy machine, in Verilog
  • A simulation that shows the Mealy output one clock earlier than the Moore output
  • How to choose between them in real RTL, and what a registered Mealy output is

Conventions and assumptions

One clock, rising edge, synchronous active-high reset. Verilog-2001 syntax, valid in SystemVerilog. Both designs were simulated in Icarus Verilog 12 and linted with Verilator 5.036 with -Wall, which reports no warnings for either module. The scheduling and always-block rules come from IEEE 1800-2023.

What is the difference?

Both machines have a state register and next-state logic; the difference is only in the output logic. A Moore output is a function of the state alone, y = f(state), so it is stable for the whole clock cycle and changes just after an edge. A Mealy output is a function of the state and the inputs, y = f(state, x), so it follows the input within the cycle. On a state diagram this shows up in where the output is written: inside the circle for Moore, on the arrow next to the input for Mealy, as input / output.

The example: a rising-edge detector

The input x is a level that is already synchronized to the clock. The output y must be high for exactly one cycle each time x goes from 0 to 1. To know that x has just risen, the machine has to remember the previous value of x. The Moore machine also needs a separate state that means "x has just risen", because that is where its output lives, so it needs three states: LOW, EDGE and HIGH. The Mealy machine can compute the output on the arrow from LOW when x = 1, so two states, LOW and HIGH, are enough.

State diagrams of a rising-edge detector: a three-state Moore machine and a two-state Mealy machine
The same edge detector both ways. Moore: y = 1 only in EDGE, which lasts one cycle. Mealy: the output is on the arrows, and only LOW with x = 1 gives 1.

The Moore machine in Verilog

Three blocks: a clocked state register with <=, a combinational next-state block with a default so that no path infers a latch, and an output that decodes the state.

edge_moore: the output depends on the state only
module edge_moore (input wire clk, input wire rst, input wire x, output wire y);
  localparam [1:0] LOW = 2'd0, EDGE = 2'd1, HIGH = 2'd2;
  reg [1:0] state, next;

  always @(posedge clk) begin
    if (rst) state <= LOW;
    else     state <= next;
  end

  always @(*) begin
    next = LOW;
    case (state)
      LOW:     next = x ? EDGE : LOW;
      EDGE:    next = x ? HIGH : LOW;
      HIGH:    next = x ? HIGH : LOW;
      default: next = LOW;
    endcase
  end

  assign y = (state == EDGE);        // Moore: the state alone
endmodule

The Mealy machine in Verilog

Here the state is just the previous value of x, and the output combines it with the current x. The assign line is the whole difference: it reads x directly.

edge_mealy: the output depends on the state and the input
module edge_mealy (input wire clk, input wire rst, input wire x, output wire y);
  localparam LOW = 1'b0, HIGH = 1'b1;
  reg state;

  always @(posedge clk) begin
    if (rst) state <= LOW;
    else     state <= x ? HIGH : LOW;  // the state is just the last x
  end

  assign y = (state == LOW) && x;    // Mealy: the state and the input now
endmodule

Worked example: the same input into both

The testbench drives both modules with one input. The clock has a 10 ns period with rising edges at 5, 15, 25 ns and so on; reset is released after the first edge, and x for each cycle is set 2 ns after an edge, so it is stable well before the next edge samples it. The input pattern is 0, 1, 1, 1, 0, 1, 0, 0: a long high pulse and then a one-cycle pulse. The testbench prints every value in the middle of each cycle and counts the edges at which each output is high.

Testbench: one input into both machines
module tb;
  reg clk = 0, rst = 1, x = 0;
  wire y_moore, y_mealy;
  edge_moore M  (.clk(clk), .rst(rst), .x(x), .y(y_moore));
  edge_mealy ML (.clk(clk), .rst(rst), .x(x), .y(y_mealy));
  always #5 clk = ~clk;              // 10 ns clock, rising edges at 5, 15, 25, ...
  // x for each clock cycle, changed 2 ns after a rising edge.
  reg [0:7] pattern = 8'b0_1_1_1_0_1_0_0;
  integer k;
  // Count output pulses: each rising edge of x must give exactly one cycle of y in both designs.
  integer moore_cycles = 0, mealy_cycles = 0;
  always @(posedge clk) begin
    if (!rst && y_moore) moore_cycles <= moore_cycles + 1;
    if (!rst && y_mealy) mealy_cycles <= mealy_cycles + 1;
  end
  initial begin
    @(posedge clk); #2 rst = 0;      // reset released after the first edge
    for (k = 0; k < 8; k = k + 1) begin
      x = pattern[k];
      #1 $display("cycle %0d mid: x=%b mealy=%b moore=%b moore_state=%0d", k, x, y_mealy, y_moore, M.state);
      @(posedge clk); #2;
    end
    $display("high at a clock edge: mealy %0d cycles, moore %0d cycles", mealy_cycles, moore_cycles);
    $finish;
  end
endmodule
Icarus Verilog output, in the middle of each cycle. Both outputs are high for exactly 2 cycles in total.
CyclexMealy yMoore stateMoore y
000LOW0
111LOW0
210EDGE1
310HIGH0
400HIGH0
511LOW0
600EDGE1
700LOW0
Timing diagram where the Mealy output rises with the input and the Moore output one clock later
The simulation as a waveform. x rises at 17 ns and the Mealy output rises with it, inside cycle 1; the Moore machine only sees the rise at the 25 ns edge, so its output is high in cycle 2. The one-cycle pulse in cycle 5 behaves the same way.

Both machines detect both rising edges, and each output is high for exactly one cycle per edge. The Mealy output is one cycle earlier, and it starts 2 ns after the edge, when x changes, rather than at the edge. That second detail is the cost: whatever logic reads the Mealy output sees the input's timing, including any glitch on x, while the Moore output changes only when the state register does.

Which one should you use?

  • Use Moore when the output leaves the block, drives a clock enable or a chip pin, or crosses into another module whose timing you do not control. Its output comes from flip-flops through a small decoder, so it is clean and easy to time.
  • Use Mealy when one cycle of latency matters, for example a handshake that must answer in the same cycle as a valid signal, and the input is itself a clean, registered signal from the same clock domain.
  • A common compromise is a registered Mealy output: compute the Mealy expression and put a flip-flop on it. The output is then glitch-free, but it is back to the Moore timing, one cycle after the input.
  • Either way, the input must be synchronous to the clock. An asynchronous input needs a synchronizer first, or the state register can go metastable.

Common mistakes

  • Writing the output of a "Moore" machine with an input in its expression, such as assign y = (state == EDGE) && x. That is a Mealy output, with Mealy timing.
  • Expecting the Mealy output on the clock edge in a waveform. It changes when the input changes, so if the testbench drives x at the edge, the output can seem to appear at the edge before.
  • Leaving the next-state block without a default, so an unlisted state value holds the old next and infers a latch.

Interview questions

  • Q1. Can you turn any Mealy machine into a Moore machine, and what does it cost? Answer: yes. Split every state into one copy per output value of the arrows that enter it, and put that output inside the new state. The Moore machine has the same behavior with one cycle more latency, and it can need more states: 3 instead of 2 for this edge detector.
  • Q2. A Mealy output from block A drives an input of block B, and B's Mealy output feeds back into A on the same clock. What can go wrong? Answer: the two output expressions form a combinational loop through both blocks, which can oscillate in hardware and hang or race in simulation. Register one of the outputs, or make one of the blocks Moore.
  • Q3. Your Moore detector must react in the same cycle as the input. Do you need a new state machine? Answer: no state change is needed if you can accept a combinational path: compute the output from the next-state value or from the state and input, which makes it Mealy. If the path is too slow for the clock, the real fix is to get the input one cycle earlier.

Related

Sources

Last verified

Both modules and the testbench were simulated in Icarus Verilog 12 and linted with Verilator 5.036 on 25 November 2026. The table is copied from that run, and the waveform is drawn from the same values; lib/digitalDesign.test.js reruns the simulation when Icarus Verilog is installed and checks both.

Keep going

Practice this