Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • RTL Foundations36
  • Combinational Design25
  • Sequential Design27
  • FSM & Control27
    • Controller and Datapath
    • Moore vs Mealy
      • Behavioral Difference
      • Side-by-Side: A Sequence Detector
      • Registered Mealy Outputs
      • Choosing and Verifying
    • State Encoding
    • Multi-Cycle Sequencers
    • UART Transmitter
    • Memory Controller
  • Timing & Clocking39
  • Reset & CDC34
  • Memory & Buses57
  • Advanced RTL28
  • Senior & Interview76
  • Glossary and Notation
Loading lesson

‹Moore vs Mealy
  1. Topics
  2. RTL Design
  3. FSM & Control
  4. Moore vs Mealy
  5. Side-by-Side: A Sequence Detector

Part 4 · FSM & Control · Intermediate

Side-by-Side: A Sequence Detector

A two-ones detector in Moore and Mealy styles, checked against one reference model.

On this page

    Why this matters

    The same spec, two ones in a row, takes two states as a Mealy machine and three as a Moore machine. Reading the output at the wrong place makes both look wrong.

    After this lesson you can: code the two-ones detector as Mealy and Moore, trace both on the same samples, and count the flip-flops each needs.

    Same spec, two machines

    Spec: y indicates that the last two input samples (the current one and the one before) were both 1. The two codings differ in the state meaning and in the output expression.

    Read the output from the right place

    Moore two-ones detector: reset selects S0. A one moves S0 to S1, then another one moves S1 to S11. Output is zero in S0 and S1, one in S11. A zero returns any state to S0; further ones keep S11 active for overlapping pairs.
    Moore: each state shows its y value. S0 means no trailing ones, S1 means one, and S11 means at least two. Each x-labelled transition occurs at a rising edge; reset selects S0 asynchronously.
    Mealy two-ones detector: reset selects S0. In S0, zero stays in S0 and one moves to S1, both with output zero. In S1, zero returns to S0 with output zero; one stays in S1 with output one, including overlapping pairs.
    Mealy: prev is the previous input sample (zero after reset), and each arc is x / y. Evaluate y using the state before the rising edge; the edge then commits the next state. Reset selects S0 asynchronously.

    Trace the same samples as the waveform

    Use the just-before-rising-edge observation point from the behavioral-difference waveform. The input has settled, but the state register has not yet accepted that sample. Both machines start in S0 after reset. The Mealy and Moore columns show state before / y before. Moore after is the state once the upcoming edge's update settles; its output then follows that new state.

    Cycle

    x

    Mealy

    Moore

    Moore after

    0

    0

    S0 / 0

    S0 / 0

    S0

    1

    0

    S0 / 0

    S0 / 0

    S0

    2

    1

    S0 / 0

    S0 / 0

    S1

    3

    1

    S1 / 1

    S1 / 0

    S11

    4

    0

    S1 / 0

    S11 / 1

    S0

    5

    1

    S0 / 0

    S0 / 0

    S1

    6

    1

    S1 / 1

    S1 / 0

    S11

    7

    1

    S1 / 1

    S11 / 1

    S11

    8

    0

    S1 / 0

    S11 / 1

    S0

    9

    0

    S0 / 0

    S0 / 0

    S0

    10

    0

    S0 / 0

    S0 / 0

    S0

    11

    0

    S0 / 0

    S0 / 0

    S0

    At cycle 3, Mealy sees S1 together with x=1 and asserts y before the edge. Moore still occupies S1 and reports 0; that edge enters S11, so the following observation reports 1. Cycles 6 and 7 detect overlapping pairs, producing the two consecutive Moore hits at cycles 7 and 8. The one-cycle relationship is for this implementation and observation convention, not a universal latency rule for every Moore/Mealy design.

    Common pitfall

    State-only does not guarantee a glitch-free decoded output: multiple state bits can settle at different times after an edge. A registered output gives a flop-driven boundary. Mealy additionally exposes changes on the input-to-output combinational path between edges.

    Check yourself At cycle 4 x is zero but Moore y is one. Is that a false detection?

    No. Before the cycle-4 edge, Moore still holds S11 from samples 2 and 3. Its y reports that earlier pair. The zero returns it to S0 at the upcoming edge; Mealy already sees the zero and reports no current pair.

    Moore

    Mealy

    States

    S0 (no ones), S1 (one), S11 (two or more)

    S0 (previous was 0), S1 (previous was 1)

    Output

    y = (state == S11)

    y = (state == S1) && x

    Flops

    2

    1

    Reading the output expressions

    y = (state == S1) && x has two parts. The first (state == S1) is memory (“the previous sample was 1”), and the second (x) is the live input.

    Remove && x and the output signals only “the previous sample was 1”, which is a different, wrong spec.

    Common mistakes

    Common pitfall

    A common mistake is to write a Mealy machine but sample its output with an input that changes after the clock edge in a testbench, or to compare it at the wrong time. In simulation, drive inputs away from the clock edge (as the testbench below does, at the falling edge) and compare after the combinational logic settles.

    Check yourself How many flip-flops does each version need, and why does the Moore one need more states?

    Moore: 3 states → 2 flip-flops. Mealy: 2 states → 1 flip-flop. The Moore machine needs a separate state to mean “two ones seen” so its output can depend on the state alone; the Mealy machine encodes that fact as “previous was 1 AND input is 1” at the output.

    Testbench

    The listing below checks both machines against one reference model.

    Tip

    The testbench states the relationship directly: Mealy = spec and Moore = spec delayed by one cycle. That is the cleanest definition of “equivalent but one cycle apart”.

    det_moore.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    module det_moore (input logic clk, rst_n, x, output logic y);
      typedef enum logic [1:0] {S0, S1, S11} state_t;
      state_t state, next;
    
      always_comb begin
        next = state;
        unique case (state)
          S0:      if (x)  next = S1;
          S1:      if (x)  next = S11; else next = S0;
          S11:     if (!x) next = S0;
          default: next = S0;
        endcase
      end
    
      always_ff @(posedge clk or negedge rst_n)
        if (!rst_n) state <= S0;
        else        state <= next;
    
      assign y = (state == S11);          // Moore: state only
    endmodule
    
    module det_mealy (input logic clk, rst_n, x, output logic y);
      typedef enum logic {S0, S1} state_t;  // S1 = the previous sample was 1
      state_t state;
    
      always_ff @(posedge clk or negedge rst_n)
        if (!rst_n)  state <= S0;
        else if (x)  state <= S1;
        else         state <= S0;
    
      assign y = (state == S1) && x;       // Mealy: state AND input
    endmodule
    

    Remember

    • Read the output just before the rising edge, where the input is settled and the state has not yet updated.

    • Mealy y = (state == S1) && x; drop the && x and it signals only that the previous sample was 1.

    • Moore needs a state for 'two ones seen', so it costs three states against two.

    Rule of thumb: the Moore output is the Mealy output delayed by one cycle.

    Keep going

    • Practice: Types of FSMs and their difference

    • Code lab: Sequence FSM

    • Interview: run a mock interview on this topic

    Related topics

    • Registered Mealy outputs

    Previous

    96 / 350

    Next: Registered Mealy Outputs

    Up next

    Up nextRegistered Mealy Outputs

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Choosing and Verifying