Sequential Logic & State Machines
Mealy vs Moore Machines
A finite state machine (FSM) is a sequential circuit that moves between a fixed number of states according to its inputs. There are two classic ways to define its outputs. In a Mealy machine the output depends on the present state and the present input. In a Moore machine the output depends only on the present state. This small difference changes the number of states, the timing of the outputs and the design of the circuit.
In this lesson you will build both kinds of machine for the same task, a sequence detector, and compare them. You should know flip-flops and the idea of a state.
Learning objectives
After this lesson you will be able to:
- Explain the difference between Mealy and Moore machines.
- Draw and read the state table of each type.
- Compare the number of states the two types need.
- Explain why a Mealy output reacts one clock earlier than a Moore output.
- Choose the right type for a design.
Mealy machines
In a Mealy machine, each transition carries an output. On a state diagram this is written as input / output on the arrow. Because the output depends on the input, it can change as soon as the input changes, without waiting for a clock edge. This is fast, and often needs fewer states, but the output can glitch if the input glitches.
Moore machines
In a Moore machine, the output is attached to the state itself, and it is written inside the state circle. The output changes only when the state changes, which happens at a clock edge, so Moore outputs are clean and stable. The price is that the machine often needs more states, and the response to an input appears one clock later than in a Mealy machine.
The task: detect the sequence 101
We want an output of 1 whenever the last three input bits were 1, 0, 1. The sequences may overlap: in 10101 the detector should fire twice. Both machines must remember how much of the pattern has been seen so far.
Example 1: the Mealy detector
Three states are enough. A means no useful progress, B means the last input was 1, and C means the last two inputs were 1, 0. Each entry shows next state / output.
| Present state | x = 0 | x = 1 |
|---|---|---|
| A (no progress) | A / 0 | B / 0 |
| B (seen 1) | C / 0 | B / 0 |
| C (seen 10) | A / 0 | B / 1 |
Check the overlapping case. In state C, an input of 1 completes the pattern, so the output is 1. The final 1 can also start a new pattern, so the next state is B.
Result: the Mealy detector needs 3 states, and its output is 1 on the transition C to B with x = 1.
Example 2: the Moore detector
The output must be stored in a state, so a fourth state is needed, one that means the pattern has just been completed.
| Present state | x = 0 | x = 1 | Output z |
|---|---|---|---|
| S0 (no progress) | S0 | S1 | 0 |
| S1 (seen 1) | S10 | S1 | 0 |
| S10 (seen 10) | S0 | S101 | 0 |
| S101 (seen 101) | S10 | S1 | 1 |
Result: the Moore detector needs 4 states, one more than the Mealy machine. In state S101 the output is 1, and the next input takes it on to S10 (if x = 0, because the pattern 10 has started) or S1 (if x = 1).
Example 3: comparing the timing
Feed both machines with the input stream 1, 0, 1, 0, 1, 1, 0, 1, one bit per clock cycle. The table shows the outputs seen in each cycle.
| Cycle | Input x | Mealy output | Moore output |
|---|---|---|---|
| 1 | 1 | 0 | 0 |
| 2 | 0 | 0 | 0 |
| 3 | 1 | 1 | 0 |
| 4 | 0 | 0 | 1 |
| 5 | 1 | 1 | 0 |
| 6 | 1 | 0 | 1 |
| 7 | 0 | 0 | 0 |
| 8 | 1 | 1 | 0 |
| 9 | - | - | 1 |
Result: the Mealy output is 1 in cycles 3, 5 and 8, in the same cycle in which the last bit of each pattern arrives. The Moore output is 1 in cycles 4, 6 and 9, exactly one cycle later, because the machine has to reach state S101 first and the output then follows. Both detect the same three patterns.
Which one should you use?
Use a Moore machine when stable, glitch-free outputs matter or when the outputs drive other clocked circuits, since the outputs change only after a clock edge. Use a Mealy machine when you want a faster response or fewer states. In practice many designers use Moore machines, or add a register after a Mealy output to get clean timing.
Common mistakes
- Putting the output on the wrong part of the diagram. In a Mealy machine it goes on the arrows, and in a Moore machine inside the states.
- Forgetting overlaps. After detecting 101 the last 1 may start a new pattern, so the next state is not the starting state.
- Expecting equal timing. A Moore output is always one cycle behind the equivalent Mealy output.
- Assuming the Mealy machine is always smaller. It usually is, but not in every case.
- Using Mealy outputs with a noisy input. The output follows the glitches on the input.
Practice problems
Check your logic with the truth table generator.
-
How many states do the Mealy and the Moore overlapping detectors for 101 need?
Answer: 3 states for the Mealy machine and 4 states for the Moore machine.
-
A machine's output depends only on its present state. Is it a Mealy or a Moore machine?
Answer: A Moore machine.
-
The input 0, 1, 0, 1 is applied to the Mealy detector of Example 1, starting in state A. What are the four outputs?
Answer: 0, 0, 0, 1.
-
Which type of machine reacts faster to an input change, and why?
Answer: The Mealy machine, because its output depends on the input directly and does not wait for the next state.
Frequently asked questions
What is the main difference between Mealy and Moore machines? In a Mealy machine the output is a function of the state and the input. In a Moore machine it is a function of the state alone. As a result, Mealy outputs can change between clock edges and Moore outputs change only at clock edges.
Can any Mealy machine be converted to a Moore machine? Yes. Both types describe the same class of behaviour. Converting a Mealy machine to a Moore machine usually needs extra states, and the output of the Moore version is delayed by one clock.
Which type is easier to design? Moore machines are simpler to reason about because the output is tied to the state, and the timing is cleaner. Mealy machines need fewer states, but their outputs need more care.
You can check your next-state logic with the Boolean calculator.
Next lesson: State Diagrams