G27
Self-checkedTier 1 16 min readdl.state-machines

FSM design: Mealy and Moore

Finite state machines as sequential circuits: Moore and Mealy outputs and their timing, state diagrams and tables, analysis and design procedures, state reduction, state assignment, and the clock period limit.

Why it exists

Counters have a fixed sequence of states. Most real controllers, however, must react to inputs: a vending machine, a traffic-light controller, a protocol decoder, the control unit of a processor. A finite state machine (FSM) is the general model: a set of states, an input, and rules that give the next state and the output. In hardware it is flip-flops (the state) plus combinational logic (the next-state and output functions), all on a clock. This is the last link between the memory elements of this subject and the machines of the theory of computation. GATE asks you to analyse a given circuit, to design a detector, to say whether a machine is Moore or Mealy, to reduce the number of states, and to compute the clock period.

Prerequisites

You need flip-flop excitation from flip-flop conversion, and minimisation from Karnaugh maps.

Precise definitions

Finite state machine (synchronous sequential circuit). State register of nn flip-flops (up to 2n2^n states), a next-state function Q+=δ(Q,X)Q^+ = \delta(Q, X) of the present state QQ and input XX, and an output function. All flip-flops share one clock, so the state changes only at clock edges.

Moore machine. The output depends only on the present state: Z=λ(Q)Z = \lambda(Q). In a diagram the output is written inside the state and the edges carry the input.

Mealy machine. The output depends on the present state and the present input: Z=λ(Q,X)Z = \lambda(Q, X). In a diagram each edge is labelled input/output.

MooreMealy
output functionstate onlystate and input
states for the same behaviourusually moreusually fewer
output changesright after a clock edgewhenever the input changes
response to an inputone clock laterin the same cycle
glitches on the outputonly from the logicalso follow input glitches

State diagram and state table. The diagram draws states as circles and transitions as arrows. The state table lists, for every present state and input, the next state and output. They contain the same information.

Analysis procedure. From a circuit: (1) write the flip-flop input equations and the output equations; (2) use the characteristic equations of the flip-flops to get the next-state equations; (3) build the state table; (4) draw the diagram; (5) describe what the machine does.

Design procedure.

  1. Understand the specification and draw a state diagram (decide Moore or Mealy).
  2. Make the state table.
  3. Reduce the number of states (merge equivalent states).
  4. Choose a state assignment (binary codes for the states).
  5. Choose the flip-flop type and use its excitation table to get the input equations; get the output equations; unused codes are don't cares.
  6. Minimise, draw the circuit and check the unused states.

State reduction (equivalent states). Two states are equivalent if no input sequence can tell them apart: they have the same output (Moore) or the same outputs on each input (Mealy), and their next states are equivalent for every input. Reduction is by partition refinement: start with the states grouped by output, then split a group whenever two of its states go to different groups on some input, until nothing splits. The groups that remain are the states of the minimal machine. For kk states you need ⌈log⁡2k⌉\lceil \log_2 k \rceil flip-flops, so fewer states can save flip-flops.

State assignment. Any distinct binary codes work, but the codes affect the cost of the equations. Common choices are binary (fewest flip-flops), Gray-like (adjacent states differ in one bit) and one-hot (one flip-flop per state, simplest logic).

Clock period. In a single-clock design the signal launched by one flip-flop must reach the next one before the next edge:

Tclk≥tcq+tlogic+tsu  (+  skew),fmax=1/Tclk,T_{clk} \ge t_{cq} + t_{logic} + t_{su} \;(+\; \text{skew}),\qquad f_{max} = 1/T_{clk},

where tcqt_{cq} is the clock-to-output delay, tlogict_{logic} the delay of the slowest path of the next-state logic and tsut_{su} the setup time. The hold condition says the fastest path must not be too fast: tcq,min+tlogic,min≥thold+skewt_{cq,min} + t_{logic,min} \ge t_{hold} + \text{skew}. A hold violation cannot be repaired by slowing the clock.

How it works

Step through a Mealy detector for the pattern 101 (overlapping occurrences allowed). The output edge is labelled input/output; the output is 1 on the transition that completes the pattern.

Mealy detector for the pattern 101 (overlap allowed): start

Simulated, not typed
0/01/00/01/00/01/1S0S1S2

Mealy machine: edges are labelled input/output.

Run so far

input (none)
output (none)

Step 1

S0: no useful progress. S1: the last bit was 1 (a possible start of 101). S2: the last two bits were 10. The output is 1 on the edge that completes 101.

1 / 9
computed by script: discrete-time simulation of the circuit (gate-level unit delays for latches, ideal edges for flip-flops). python3 scripts/gen-dl-seq-traces.py

The Moore machine needs one more state, because the output belongs to the state: the state D means "101 has just been seen".

Moore detector for the pattern 101 (overlap allowed): start

Simulated, not typed
01010101A/0B/0C/0D/1

Moore machine: the output is written inside the state (state/output); edges are labelled with the input.

Run so far

input (none)
output 0

Step 1

A: no progress. B: last bit 1. C: last two bits 10. D: just saw 101, and the output of D is 1. The output belongs to the state, so it appears one step after the last input bit has been read, in the next state.

1 / 9
computed by script: discrete-time simulation of the circuit (gate-level unit delays for latches, ideal edges for flip-flops). python3 scripts/gen-dl-seq-traces.py

The time difference between them is the main practical point: with the same input the Mealy output appears in the cycle in which the last bit is applied, while the Moore output appears one clock edge later.

Mealy and Moore detectors for 101 in time: Start

Simulated, not typed
CLKXMealy stateS0S1S2S1S2S1S2S1S2S0Z MealyMoore stateABCDCDBCDCAZ Mooret = 0

Dashed lines mark rising clock edges. Green is an output, blue a stored state, black an input, grey the clock. One tick = one time unit.

Step 1

The same input stream (1 0 1 0 1 1 0 1 0) feeds a Mealy and a Moore detector for the pattern 101. The input changes just after each rising clock edge and the state changes at the rising edges.

1 / 5
computed by script: discrete-time simulation of the circuit (gate-level unit delays for latches, ideal edges for flip-flops). python3 scripts/gen-dl-seq-traces.py

A machine that is not a detector: read a binary number most significant bit first and say whether it is divisible by 3. The state is the remainder, and reading a bit bb turns the value vv into 2v+b2v + b, so the new remainder is (2r+b) mod 3(2r + b) \bmod 3.

Is the binary number read so far divisible by 3? (most significant bit first): start

Simulated, not typed
010101R0/1R1/0R2/0

Moore machine: the output is written inside the state (state/output); edges are labelled with the input.

Run so far

input (none)
output 1

Step 1

The state is the remainder of the number read so far, modulo 3. Reading a bit b turns a value v into 2v + b, so the new remainder is (2r + b) mod 3. The output is 1 in state R0 (divisible by 3).

1 / 8
computed by script: discrete-time simulation of the circuit (gate-level unit delays for latches, ideal edges for flip-flops). python3 scripts/gen-dl-seq-traces.py

Worked examples

EX 1Both detectors, as tables and as gates

Easy

The program runs the Mealy table, the Mealy circuit (below) and the Moore table on the same input and checks all three against a direct search for 101 on every bit string of length 1 to 10. It also runs the divisible-by-3 machine.

#include <stdio.h>
#include <string.h>
 
/* Mealy detector for 101 (overlap), states S0=0 S1=1 S2=2: returns output, updates state */
static int mealy_step(int *s, int x) {
    int z = (*s == 2 && x == 1);
    static const int next[3][2] = { { 0, 1 }, { 2, 1 }, { 0, 1 } };
    *s = next[*s][x];
    return z;
}
 
/* the same machine as a circuit: state bits P Q, D1 = X'Q, D0 = X, Z = X P */
static int mealy_circuit(int *p, int *q, int x) {
    int z = x && *p;
    int d1 = !x && *q, d0 = x;
    *p = d1;
    *q = d0;
    return z;
}
 
/* Moore detector: A=0 B=1 C=2 D=3, output 1 only in D */
static int moore_step(int *s, int x) {
    static const int next[4][2] = { { 0, 1 }, { 2, 1 }, { 0, 3 }, { 2, 1 } };
    *s = next[*s][x];
    return *s == 3;
}
 
/* divisible by 3: state = remainder */
static int div3_step(int *r, int x) {
    *r = (2 * *r + x) % 3;
    return *r == 0;
}
 
int main(void) {
    const char *in = "10101101";
    int s = 0, p = 0, q = 0, m = 0;
    char a[16], b[16], c[16];
    for (size_t i = 0; i < strlen(in); i++) {
        int x = in[i] - '0';
        a[i] = (char)('0' + mealy_step(&s, x));
        b[i] = (char)('0' + mealy_circuit(&p, &q, x));
        c[i] = (char)('0' + moore_step(&m, x));
    }
    a[strlen(in)] = b[strlen(in)] = c[strlen(in)] = 0;
    printf("input        %s\nMealy table  %s\nMealy gates  %s\nMoore        %s\n", in, a, b, c);
    int bad = 0, strings = 0;
    for (int len = 1; len <= 10; len++)
        for (int v = 0; v < (1 << len); v++) {
            int s1 = 0, p1 = 0, q1 = 0, m1 = 0, r = 0;
            int last3 = 0;
            for (int i = len - 1; i >= 0; i--) {
                int x = (v >> i) & 1;
                int z1 = mealy_step(&s1, x), z2 = mealy_circuit(&p1, &q1, x), z3 = moore_step(&m1, x);
                last3 = ((last3 << 1) | x) & 7;
                int seen = (len - i >= 3) && last3 == 5;
                bad += z1 != seen || z2 != seen || z3 != seen;
                div3_step(&r, x);
            }
            bad += r != v % 3;
            strings++;
        }
    printf("machines wrong on %d of %d strings\n", bad, strings);
    int r = 0;
    printf("divisible by 3 on 1100101:");
    int val = 0;
    for (const char *t = "1100101"; *t; t++) {
        val = 2 * val + (*t - '0');
        printf(" %d%s", val, div3_step(&r, *t - '0') ? "*" : "");
    }
    printf("\n");
    double tcq = 2, tlogic = 5, tsu = 1;
    printf("clock period at least %.0f ns, frequency at most %.0f MHz\n", tcq + tlogic + tsu, 1000 / (tcq + tlogic + tsu));
    return 0;
}
input        10101101
Mealy table  00101001
Mealy gates  00101001
Moore        00101001
machines wrong on 0 of 2046 strings
divisible by 3 on 1100101: 1 3* 6* 12* 25 50 101
clock period at least 8 ns, frequency at most 125 MHz

The three detectors give the same output string for the same input, 00101001: the pattern 101 is complete at the third, fifth and eighth bit (the occurrence ending at the fifth bit overlaps the first one). The divisible-by-3 line marks (with a star) the prefixes 3, 6 and 12 of 1100101: 1,3,6,12,25,50,1011, 3, 6, 12, 25, 50, 101.

EX 2Design the Mealy detector with D flip-flops

Medium

State diagram. S0: no progress. S1: the last bit was 1. S2: the last two bits were 10. Transitions: S0 on 0 stays S0 (output 0), S0 on 1 goes to S1; S1 on 0 goes to S2, S1 on 1 stays S1; S2 on 0 goes to S0, S2 on 1 goes to S1 with output 1 (the pattern 101 is complete, and the final 1 can start another occurrence).

State assignment. 3 states need 2 flip-flops Q1Q0Q_1 Q_0: S0 = 00, S1 = 01, S2 = 10; the code 11 is unused (don't care).

Equations (D flip-flops, so D=Q+D = Q^+). Reading the table with the minimiser: D1=X′Q0D_1 = X' Q_0 (go to S2 only from S1 on input 0), D0=XD_0 = X (every transition on input 1 goes to S1, which has Q0=1Q_0 = 1) and the output Z=XQ1Z = X Q_1 (input 1 while in S2). That is 2 D flip-flops and 3 gates (one inverter, two ANDs). The program above simulates exactly these equations (the line labelled "Mealy gates") and it agrees with the table.

EX 3Design the Moore detector

Medium

States A (no progress), B (last bit 1), C (last two bits 10), D (101 seen). Codes A = 00, B = 01, C = 10, D = 11. Next-state rule: A: 0 stays, 1 to B. B: 0 to C, 1 stays. C: 0 to A, 1 to D. D: 0 to C, 1 to B.

Equations. D0=XD_0 = X (the next state is B or D, both with Q0=1Q_0 = 1, exactly when the input is 1). D1=X′Q0+XQ1Q0′D_1 = X'Q_0 + XQ_1Q_0' (from B or D on input 0, or from C on input 1). The output is Z=Q1Q0Z = Q_1 Q_0 (state D only). Cost: 2 flip-flops, a handful of gates; the output is a function of the state only.

EX 4State reduction by partition refinement

Medium

A Moore machine with five states: S1 (output 0), S2 (1), S3 (1), S4 (0), S5 (0), and next states: S1: 0 to S2, 1 to S3. S2: 0 to S1, 1 to S4. S3: 0 to S1, 1 to S5. S4 and S5: both inputs go to S1.

Round 1 (group by output). {S1,S4,S5}\{S1, S4, S5\} (output 0) and {S2,S3}\{S2, S3\} (output 1).

Round 2. In the first group, S1 goes to the second group on both inputs, while S4 and S5 go to S1 (the first group): split into {S1}\{S1\} and {S4,S5}\{S4, S5\}. In the second group S2 goes to S4 and S3 goes to S5 on input 1, and S4, S5 are in the same group, so S2 and S3 stay together.

Round 3. Nothing splits. The minimal machine has 3 states: {S1}\{S1\}, {S2,S3}\{S2, S3\} and {S4,S5}\{S4, S5\}, so 2 flip-flops instead of 3. The script runs the five-state and the three-state machine on all bit strings up to length 11 and finds identical output.

EX 5Analyse a circuit

GATE level

A circuit has one D flip-flop with D=X⊕QD = X \oplus Q and output Z=QZ = Q. Present state QQ, input XX: next state Q+=X⊕QQ^+ = X \oplus Q. State table: (QQ=0,XX=0) goes to 0; (0,1) to 1; (1,0) to 1; (1,1) to 0. With Z=QZ = Q the output depends on the state only: a Moore machine. Behaviour: it toggles when X=1X = 1 and holds when X=0X = 0, a T flip-flop. If the output were Z=QXZ = Q X it would be a Mealy machine.

EX 6Number of flip-flops and states

GATE level

(a) A machine with 11 states needs ⌈log⁡211⌉=4\lceil \log_2 11 \rceil = 4 flip-flops (one-hot would need 11). (b) The Mealy detector for 101 has 3 states, the Moore detector 4; both need 2 flip-flops. (c) The divisible-by-3 machine has 3 states (remainders 0, 1, 2) and needs 2 flip-flops. (d) A detector for a pattern of LL bits with overlaps needs exactly LL states as a Mealy machine (0 to L−1L - 1 bits matched) and L+1L + 1 as a Moore machine (the extra state means "the whole pattern has just been matched"). This was checked for every pattern of length 2 to 5.

EX 7Clock frequency

GATE level

The next-state logic has a critical path of 5 ns, the flip-flops have tcq=2t_{cq} = 2 ns and tsu=1t_{su} = 1 ns (the program computes this as 8 ns). Then Tclk≥2+5+1=8T_{clk} \ge 2 + 5 + 1 = 8 ns and fmax=125f_{max} = 125 MHz. If the clock skew between two flip-flops is 0.5 ns in the unfavourable direction the period must be 8.5 ns. A shorter minimum path (tcq,min+tlogic,mint_{cq,min} + t_{logic,min} smaller than tholdt_{hold} plus skew) is a hold violation: it fails at any clock frequency.

Cheat box

Cheat box

  • Moore: Z=λ(Q)Z = \lambda(Q). Mealy: Z=λ(Q,X)Z = \lambda(Q, X). Mealy needs fewer states; its output appears a clock earlier and follows input glitches. Moore's output is synchronous.
  • Number of flip-flops: ⌈log⁡2(states)⌉\lceil \log_2 (\text{states}) \rceil (binary) or one per state (one-hot).
  • Design: diagram, table, reduce, assign, excitation equations (unused codes are don't cares), outputs, circuit.
  • Equivalent states: same output and equivalent next states. Reduce by partition refinement.
  • 101 detector, overlap: Mealy 3 states (D1=X′Q0D_1 = X'Q_0, D0=XD_0 = X, Z=XQ1Z = XQ_1 with S0=00, S1=01, S2=10); Moore 4 states.
  • Divisible by 3, MSB first: state = remainder, r+=(2r+b) mod 3r^+ = (2r + b) \bmod 3.
  • Tclk≥tcq+tlogic+tsu+skewT_{clk} \ge t_{cq} + t_{logic} + t_{su} + \text{skew}; hold violations cannot be cured by a slower clock.

Traps and GATE patterns

Connections

A counter is a state machine without inputs, and a shift register with a combinational function is a feedback machine. The memory elements are the flip-flops, and the next-state logic is designed with the excitation tables of the previous page. In Theory of Computation the same object appears as a DFA, with a Moore machine as a DFA with outputs, and the control unit of the processor is an FSM that steps through the instruction cycle.

PYQs

3 previous year questions from your book. I re-solved each one myself (3 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · Set 1 · MCQ · 2 marksQ1 · page 184 of the book
Consider a 2-bit saturating up/down counter that
performs the saturating up count when the input P
is 0, and the saturating down count when P is 1. The
Next State table of the counter is as shown. The
counter is built as a synchronous sequential circuit
using D flip-flops.

Input
Current
State
Next
State
P
Q1
Q1
+
Q0
+
0
0
0
0
1
0
0
1
1
0
0
1
0
1
1
0
1
1
1
1
1
0
0
0
0
1
0
1
0
0
1
1
0
0
1
1
1
1
1
0
  • (a)D1 = PQ1 + P ̅Q0 + Q1Q0 D0 = PQ0 + P ̅Q1 + Q1Q0
  • (b)D1 = P̅Q1 + P̅Q0 + Q1Q0 D0 = P̅Q0̅̅̅̅ + P̅Q1 + Q1Q0
  • (c)D1 = P ̅Q ̅1 + P ̅Q0 + Q1Q0 D0 = P ̅Q0 + P ̅Q1 + Q1Q ̅0
  • (d)D1 = PQ1 + P ̅Q0 + Q1Q0 D0 = PQ0 + P ̅Q1 + Q1Q0
GATE 2016 · Set 1 · NAT · 1 markQ11 · page 186 of the book
We want to design a synchronous counter that
counts the sequence 0-1-0-2-0-3 and then repeats.
The minimum number of J-K flip-flops required to
implement this counter is __________.
GATE 2005 · MCQ · 1 markQ23 · page 188 of the book
Consider the following circuit

The flip-flops are positive edge triggered s. Each
state is designated as a two-bit string . Let the initial
state be The state transition sequence is
  • (a)
  • (b)
  • (c)
  • (d)DIGITAL LOGIC

Self-check

Question 1

A sequence detector for 11 (overlap allowed) as a Mealy machine: how many states and what does it output on the input 0111?

Show answer

2 states: S0 (last bit not 1) and S1 (last bit 1). The output is 1 on the edge S1 on 1. For 0111: bit 1 (0) gives 0, bit 2 (1) moves to S1 with output 0, bit 3 (1) gives 1, bit 4 (1) gives 1 (the overlap). Output 0011.

Question 2

How many flip-flops does a Moore machine with 9 states need in binary encoding, and in one-hot encoding?

Show answer

⌈log⁡29⌉=4\lceil \log_2 9 \rceil = 4 in binary, and 9 in one-hot.

Question 3

A circuit has Z=Q1X+Q0Z = Q_1 X + Q_0 as its output. Moore or Mealy?

Show answer

Mealy: the output contains the input XX.

Question 4

Using the divisible-by-3 machine, what is the state after reading 10110 (22) and is the number divisible by 3?

Show answer

22 mod 3=122 \bmod 3 = 1, so the state is R1 and the output is 0: not divisible. Step by step, the bits 1, 0, 1, 1, 0 give the values 1, 2, 5, 11, 22, with remainders 1, 2, 2, 2, 1.

Revision card

One-screen revision

  • Moore output = function of the state; Mealy = function of state and input (fewer states, earlier output).
  • Design: diagram, table, reduce, assign, excitation equations, outputs. ⌈log⁡2k⌉\lceil \log_2 k \rceil flip-flops.
  • Equivalence: same outputs, equivalent next states; partition refinement.
  • 101 overlap: Mealy 3 states, Moore 4. Mod-3 detector: state = remainder.
  • Tclk≥tcq+tlogic+tsuT_{clk} \ge t_{cq} + t_{logic} + t_{su}; hold violations are independent of frequency.

References

  • ·Mano and Ciletti, Digital Design, 5th ed., Chapter 5 Synchronous Sequential Logic (state diagrams, state tables, design procedure, state reduction and assignment)
  • ·Kohavi and Jha, Switching and Finite Automata Theory, Chapters on finite-state machines and state minimisation

Verification

The three state-diagram animations and the Mealy-versus-Moore timing waveform come from simulations (scripts/gen-dl-seq-traces.py) that assert the machine outputs against a direct search for the pattern (all bit strings up to length 12) and are re-simulated independently in tests/dl-seq-traces.test.ts. The synthesised D flip-flop equations for both detectors are produced by the exact minimiser and simulated against the reference on all strings up to length 10, the state-reduction example is run through partition refinement and compared with its reduced machine on all strings up to length 11, and the divisible-by-3 machine is checked for all strings up to length 12, all in scripts/enum-checks/dl_seq.py. The C program is compiled and run by npm run verify:c. The clock-period formula uses the standard single-clock model and the numbers are illustrative. Textbook chapter titles are from memory; the independent second pass has not been run, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (conversions, signed ranges and flags, IEEE 754 fields and custom formats, Boolean identities and counts, K-map groups, adder carries, BCD correction, flip-flop conversions, counter sequences and T equations, state reduction, Quine-McCluskey rounds and covers, floating-point alignment and rounding); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never