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 flip-flops (up to states), a next-state function of the present state and input , 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: . 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: . In a diagram each edge is labelled input/output.
| Moore | Mealy | |
|---|---|---|
| output function | state only | state and input |
| states for the same behaviour | usually more | usually fewer |
| output changes | right after a clock edge | whenever the input changes |
| response to an input | one clock later | in the same cycle |
| glitches on the output | only from the logic | also 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.
- Understand the specification and draw a state diagram (decide Moore or Mealy).
- Make the state table.
- Reduce the number of states (merge equivalent states).
- Choose a state assignment (binary codes for the states).
- 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.
- 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 states you need 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:
where is the clock-to-output delay, the delay of the slowest path of the next-state logic and the setup time. The hold condition says the fastest path must not be too fast: . 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 typedMealy machine: edges are labelled input/output.
Run so far
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.
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 typedMoore machine: the output is written inside the state (state/output); edges are labelled with the input.
Run so far
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.
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 typedDashed 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.
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 turns the value into , so the new remainder is .
Is the binary number read so far divisible by 3? (most significant bit first): start
Simulated, not typedMoore machine: the output is written inside the state (state/output); edges are labelled with the input.
Run so far
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).
Worked examples
EX 1Both detectors, as tables and as gates
EasyThe 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 MHzThe 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: .
EX 2Design the Mealy detector with D flip-flops
MediumState 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 : S0 = 00, S1 = 01, S2 = 10; the code 11 is unused (don't care).
Equations (D flip-flops, so ). Reading the table with the minimiser: (go to S2 only from S1 on input 0), (every transition on input 1 goes to S1, which has ) and the output (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
MediumStates 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. (the next state is B or D, both with , exactly when the input is 1). (from B or D on input 0, or from C on input 1). The output is (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
MediumA 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). (output 0) and (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 and . 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: , and , 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 levelA circuit has one D flip-flop with and output . Present state , input : next state . State table: (=0,=0) goes to 0; (0,1) to 1; (1,0) to 1; (1,1) to 0. With the output depends on the state only: a Moore machine. Behaviour: it toggles when and holds when , a T flip-flop. If the output were it would be a Mealy machine.
EX 6Number of flip-flops and states
GATE level(a) A machine with 11 states needs 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 bits with overlaps needs exactly states as a Mealy machine (0 to bits matched) and 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 levelThe next-state logic has a critical path of 5 ns, the flip-flops have ns and ns (the program computes this as 8 ns). Then ns and 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 ( smaller than plus skew) is a hold violation: it fails at any clock frequency.
Cheat box
Cheat box
- Moore: . Mealy: . Mealy needs fewer states; its output appears a clock earlier and follows input glitches. Moore's output is synchronous.
- Number of flip-flops: (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 (, , with S0=00, S1=01, S2=10); Moore 4 states.
- Divisible by 3, MSB first: state = remainder, .
- ; 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
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
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 __________.
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 answerHide
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 answerHide
in binary, and 9 in one-hot.
Question 3
A circuit has as its output. Moore or Mealy?
Show answerHide
Mealy: the output contains the input .
Question 4
Using the divisible-by-3 machine, what is the state after reading 10110 (22) and is the number divisible by 3?
Show answerHide
, 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. flip-flops.
- Equivalence: same outputs, equivalent next states; partition refinement.
- 101 overlap: Mealy 3 states, Moore 4. Mod-3 detector: state = remainder.
- ; 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
My notes (saved in this browser)