Sub topic 16
Mealy and Moore machines, state diagrams and tables
This chapter assumes you know how D and JK flip-flops behave, how a clock edge updates them, and how to read a truth table. It also assumes you can write a next-state expression from a table. Here we define the two kinds of finite state machine (FSM) and draw them as diagrams and tables. We also compare their timing and implement both in Verilog.
What a synchronous FSM is
A finite state machine is a circuit with a fixed, finite set of states and a rule for moving between them. A synchronous FSM has three parts:
- State register. A group of flip-flops that holds the present state. With n flip-flops you can encode up to 2^n states.
- Next-state logic. A combinational block that computes the next state from the present state and the inputs.
- Output logic. A combinational block that computes the outputs.
The state only changes on the active clock edge. Between edges, the next-state logic settles to its answer, and the register ignores it until the next edge.
Moore versus Mealy
The two models differ only in what the output logic can see.
- A Moore machine has outputs that depend only on the present state:
z = g(state). - A Mealy machine has outputs that depend on the present state and the current inputs:
z = g(state, x).
| Property | Moore | Mealy |
|---|---|---|
| Output function | State only | State and input |
| Where output is written in a diagram | Inside the state | On the transition arrow |
| Output timing | Changes only after a clock edge | Can change as soon as the input changes |
| Glitches from input noise | Not passed to the output | Passed to the output |
| States for the same task | Usually more | Usually fewer |
| Reaction to an input | One clock cycle later | In the same cycle |
Mealy outputs react to inputs asynchronously. If the input changes in the middle of a clock cycle, the output changes in that cycle. This is why a Mealy output needs care when it feeds another clocked block.
State diagrams
A state diagram is a directed graph. Each node is a state, and each arrow is a transition labeled with the input condition that triggers it.
- In a Moore diagram, write the output inside the node, for example
S3 / z=1. Arrows carry only the input value. - In a Mealy diagram, write
input/outputon each arrow, for example1/1. Nodes carry only the state name.
Every state must have an outgoing arrow for every input combination. With one input bit, each state has exactly two arrows. A missing arrow is a common error. It means the next state is undefined.
Worked example: detecting the sequence 101
The task is to detect the pattern 101 in a serial input x, and overlapping matches count. In the stream 10101, the pattern occurs twice. Each state remembers how much of the pattern has been seen:
- S0: no useful progress.
- S1: the last bit was 1.
- S2: the last two bits were 10.
- S3 (Moore only): the last three bits were 101, so the output is 1.
The Moore diagram has four states.
The arrows leaving S3 show the overlap. After 101, a new 1 gives the partial match "1", so the machine goes to S1. A 0 gives "10", so it goes to S2. Going back to S0 would lose the overlapping match.
State tables
A state table lists the same information in rows. For the Moore machine, each row has a present state, the next state for each input, and the output.
| Present state | Next, x=0 | Next, x=1 | z |
|---|---|---|---|
| S0 | S0 | S1 | 0 |
| S1 | S2 | S1 | 0 |
| S2 | S0 | S3 | 0 |
| S3 | S2 | S1 | 1 |
A Mealy table puts the output next to each next state, as next/output. The Mealy detector needs only three states, because "just saw 101" is expressed as the transition S2 --1/1--> S1:
| Present state | x=0 | x=1 |
|---|---|---|
| S0 | S0 / 0 | S1 / 0 |
| S1 | S2 / 0 | S1 / 0 |
| S2 | S0 / 0 | S1 / 1 |
Moore implementation in Verilog
This is the Moore detector. It has a state register, a next-state block, and an output that is a function of the state only.
module det101(input clk, input rst, input x, output z);
localparam S0 = 2'd0, S1 = 2'd1, 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);
endmoduleThe default branch covers the unused encodings. With 2 bits and 4 states there are none here. In a 3-state design, one encoding is unused, and default sends the machine back to a safe state.
Mealy implementation in Verilog
The Mealy version has three states, and the output includes the input x:
module det101_mealy(input clk, input rst, input x, output z);
localparam S0 = 2'd0, S1 = 2'd1, S2 = 2'd2;
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 ? S1 : S0;
default: next = S0;
endcase
end
assign z = (state == S2) && x;
endmoduleOnly the last line and the state count differ from the Moore design.
Comparing the timing
Apply the input stream 1, 0, 1, 0, 1, 1, one bit per clock cycle, starting in S0. The table shows the values seen at each cycle.
| Cycle | Input x | Mealy state at cycle | Mealy z | Moore state at cycle | Moore z |
|---|---|---|---|---|---|
| 1 | 1 | S0 | 0 | S0 | 0 |
| 2 | 0 | S1 | 0 | S1 | 0 |
| 3 | 1 | S2 | 1 | S2 | 0 |
| 4 | 0 | S1 | 0 | S3 | 1 |
| 5 | 1 | S2 | 1 | S2 | 0 |
| 6 | 1 | S1 | 0 | S3 | 1 |
The Mealy output is 1 in cycle 3, the same cycle in which the last 1 of the pattern is applied. The Moore output is 1 in cycle 4, one cycle later, because the machine must first move into S3. Both machines find the same two matches. The Moore machine reports each one a clock cycle later.
Common errors
- Output in the wrong place. In a Moore diagram, labeling arrows with outputs describes a Mealy machine. In a Mealy diagram, putting outputs inside nodes does the opposite.
- Missing transitions. Check that each state has one arrow per input combination. For 2 inputs, that is 4 arrows per state.
- Forgetting overlap. Returning to S0 after a match discards partial matches. Ask what the last few bits are, and go to the state that records them.
- Reading the Mealy output after the edge. A Mealy output is valid before the clock edge that moves the state. After the edge, the state has already changed.
- Reset behavior. A synchronous reset takes effect only on a clock edge. Include it, or the initial state is unknown.
- Counting flip-flops. For N states you need at least ceil(log2 N) flip-flops. The Moore detector needs 2, and so does the Mealy one. A machine with 5 states needs 3.
GATE-style approach
For a question that gives a state diagram and asks for the output sequence, follow these steps:
- Identify the machine type from where the outputs are written.
- Write the initial state.
- For each input bit, record the output (Mealy: from the arrow; Moore: from the state you are in) and then the next state.
- Count carefully. A Moore machine on an n-bit input produces n+1 output values if the starting state's output is included. A Mealy machine produces n.
Exercise
A Mealy machine has states A and B, starting in A. From A: input 0 goes to A with output 0, and input 1 goes to B with output 0. From B: input 0 goes to A with output 1, and input 1 goes to B with output 0. What output sequence does it produce for the input 1 0 1 1 0?
Show answer
Start in A. Input 1: go to B, output 0. Input 0: go to A, output 1. Input 1: go to B, output 0. Input 1: stay in B, output 0. Input 0: go to A, output 1. The output sequence is 0 1 0 0 1.
Exercise
Modify the Moore state table for the overlapping 101 detector so that it detects the non-overlapping version, where the bits of one match cannot be reused. Which entries change?
Show answer
After a match, the machine must start fresh. In S3, input 1 goes to S0 instead of S1, because the final 1 of the match cannot start a new pattern. Input 0 also goes to S0 instead of S2, because the "10" would use the matched 1. So both S3 transitions become S0. All other entries stay the same.
Exercise
A Moore machine has 6 states and one input. How many flip-flops are needed, and how many rows does its state table have?
Show answer
ceil(log2 6) = 3 flip-flops. The table has one row per state, so 6 rows. Each row has two next-state entries (x=0 and x=1) and one output. If you count combined rows for the next-state logic (3 state bits plus 1 input), there are 2^4 = 16 input combinations, of which 12 are used and 4 are don't-cares.