Sub topic 16
Mealy and Moore Machines, State Diagrams and Tables
Prerequisites and working model
Assume you know Boolean logic, flip-flops, and clocked circuits. A finite state machine (FSM) is a sequential circuit that has a limited number of states. Its state records the relevant history of its inputs.
At each active clock edge, the FSM changes from its current state to a next state. Combinational logic calculates the next state and the output.
An FSM has:
- A finite set of states.
- Input signals.
- Output signals.
- A next-state function.
- An output function.
- A clock and usually a reset.
Mealy and Moore machines
A Mealy machine produces an output from the current state and the current input:
output = f(current state, input)
A Moore machine produces an output from the current state only:
output = f(current state)
| Property | Mealy machine | Moore machine |
|---|---|---|
| Output depends on | State and input | State only |
| Output changes | When state or input changes | When the state changes |
| Number of states | Often fewer | Often more |
| Output timing | Can react before the next clock edge | Usually changes after a clock edge |
| Common risk | Input glitches can affect output | Extra clock cycle or state may be needed |
A Mealy output can respond immediately to an input change. This can reduce the number of states, but it also makes the output more sensitive to glitches. A Moore output is easier to synchronize because it changes only with the state.
State diagrams and tables
In a state diagram, circles represent states and arrows represent transitions. Mealy transitions usually use the label input/output. Moore states usually contain the output value.
Consider a Moore machine that detects the overlapping input sequence 101. It asserts z for one clock cycle after the complete sequence has been received.
The corresponding state table is:
| Current state | Input x | Next state | Output z |
|---|---|---|---|
| S0 | 0 | S0 | 0 |
| S0 | 1 | S1 | 0 |
| S1 | 0 | S2 | 0 |
| S1 | 1 | S1 | 0 |
| S2 | 0 | S0 | 0 |
| S2 | 1 | S3 | 0 |
| S3 | 0 | S2 | 1 |
| S3 | 1 | S1 | 1 |
The transitions from S3 preserve overlap. For example, after detecting 101, a new input of 1 can begin another sequence.
Verilog implementation
module det101(
input clk,
input rst,
input x,
output z
);
localparam S0 = 2'd0, S1 = 2'd1;
localparam S2 = 2'd2, S3 = 2'd3;
reg [1:0] state, next;
always @(posedge clk) begin
if (rst)
state <= S0;
else
state <= next;
end
always @(*) begin
case (state)
S0: next = x ? S1 : S0;
S1: next = x ? S1 : S2;
S2: next = x ? S3 : S0;
S3: next = x ? S1 : S2;
default: next = S0;
endcase
end
assign z = (state == S3);
endmoduleCommon errors
- Confusing the current state with the next state.
- Forgetting a transition for one input value.
- Making a Moore output depend directly on an input.
- Losing overlap when designing a sequence detector.
- Omitting a default state assignment, which can create unintended storage in combinational logic.
Exercise
In the Moore detector above, what are the next state and output when the current state is S2 and x is 1?
Show answer
The next state is S3, and the current state's output is still z=0. After the clock edge, the machine enters S3, where z becomes 1.