Sub topic 17
Designing Sequence Detectors and Minimizing States
This chapter assumes you can draw Mealy and Moore state diagrams and write state tables, and that you know the D flip-flop. It also assumes you can minimize logic with Karnaugh maps. The goal is a repeatable design procedure, followed by a way to shrink the machine before you build it.
The design procedure
- Read the specification: overlapping or non-overlapping, Mealy or Moore, and what the output means.
- Give each state a meaning, such as "the longest suffix of the input seen so far that is a prefix of the target".
- Draw the state diagram and write the state table.
- Minimize the number of states.
- Assign binary codes to the states.
- Derive the flip-flop input equations and the output equations.
- Check the circuit against a test input stream.
Steps 2 and 4 decide the cost. Everything after them is mechanical.
Building a 101 detector (overlapping, Moore)
Name each state after the progress made toward 101.
- S0: no useful progress.
- S1: the last bit was
1. - S2: the last two bits were
10. - S3: the last three bits were
101. The output is 1 here.
The key rule is for the overlapping case. From S3, the final 1 of 101 can start a new match, so S3 with input 1 goes to S1. The final 01 leaves the suffix 10, so S3 with input 0 goes to S2. A new match can start from either suffix, which is why overlap matters.
Verilog for this machine uses two always blocks: one for the state register and one for next-state logic.
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);
endmoduleMoore versus Mealy
A Mealy detector needs no S3. The output is 1 on the transition from S2 when x = 1, so three states suffice.
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;
endmoduleFor a target of length n, an overlapping or non-overlapping detector needs n + 1 states as a Moore machine and n states as a Mealy machine.
The input stream 1 0 1 0 1 1 shows the timing difference:
| Input bit | Moore state after the edge | Moore z after the edge | Mealy z during the input cycle |
|---|---|---|---|
| 1 | S1 | 0 | 0 |
| 0 | S2 | 0 | 0 |
| 1 | S3 | 1 | 1 |
| 0 | S2 | 0 | 0 |
| 1 | S3 | 1 | 1 |
| 1 | S1 | 0 | 0 |
The Mealy output depends on x and appears in the same cycle as the last bit of the pattern, so it can change if x changes mid-cycle. The Moore output appears one clock later and is stable for the whole cycle.
State assignment and flip-flop equations
Use the codes S0 = 00, S1 = 01, S2 = 10, S3 = 11, written Q1Q0, with D flip-flops.
| Present state | Q1Q0 | Next (x=0) | Next (x=1) | z |
|---|---|---|---|---|
| S0 | 00 | 00 | 01 | 0 |
| S1 | 01 | 10 | 01 | 0 |
| S2 | 10 | 00 | 11 | 0 |
| S3 | 11 | 10 | 01 | 1 |
With D flip-flops, each D input equals the next-state bit. Read the columns:
- D0 is 1 exactly when x = 1, in every row. So D0 = x.
- D1 is 1 for S1 with x = 0, S3 with x = 0, and S2 with x = 1. The first two combine into Q0·x'. So D1 = Q0·x' + Q1·Q0'·x.
- The output is z = Q1·Q0.
Check one row. In S3 (Q1 = 1, Q0 = 1) with x = 0, D1 = 1 and D0 = 0. The next state is 10, which is S2, as the table says.
Other encodings can give simpler logic, and one-hot encoding uses more flip-flops (one per state) to get simpler next-state logic. For GATE, the usual questions ask for the number of flip-flops, which is ⌈log2 n⌉ for n states, or for one specific equation.
Minimizing states
Two states are equivalent if no input sequence can tell them apart by their outputs. Equivalent states can be merged without changing behavior. Use partition refinement:
- Group states by output. For a Mealy machine, group by the output vector over all inputs.
- Split any group whose states send some input to different groups.
- Repeat until no group splits. Each remaining group becomes one state.
Worked example, a Mealy machine (next state/output):
| State | x=0 | x=1 |
|---|---|---|
| A | B/0 | C/0 |
| B | A/0 | D/1 |
| C | E/0 | A/0 |
| D | A/0 | B/1 |
| E | C/0 | A/0 |
- Round 0: outputs split the states into {A, C, E}, which output 0 on both inputs, and {B, D}, which output 0 and 1.
- Round 1: A goes to B (block {B, D}) on 0, but C and E go to block {A, C, E}. So A splits off, leaving {A}, {C, E}, {B, D}.
- Round 2: C goes to E and A. E goes to C and A. These are the same blocks, so C and E stay together. B goes to A and D, and D goes to A and B, so B and D stay together.
The result is three states, so two flip-flops instead of three.
| State | x=0 | x=1 |
|---|---|---|
| A | B/0 | C/0 |
| B | A/0 | B/1 |
| C | C/0 | A/0 |
Common errors
- Missing the overlap transition from the last state. Check where S3 goes on each input.
- Giving a Moore machine only n states. The output state needs a state of its own.
- Forgetting the reset state or leaving unused codes undefined. Send unused codes to S0, as
defaultdoes above. - Merging states that have equal outputs but different successor blocks. Equal outputs are only the first test.
Exercise
A Moore machine detects the overlapping sequence 1011. How many states does it need, and where does the last state go on input 1 and on input 0?
Show answer
It needs 5 states: S0 (nothing), S1 (1), S2 (10), S3 (101) and S4 (1011, output 1). From S4 on input 1, the suffix 1 is a prefix of the target, so it goes to S1. On input 0, the suffix 10 is a prefix, so it goes to S2. Five states need 3 flip-flops.
Exercise
A machine has 6 states, and partition refinement merges them into 3 equivalence classes. How many flip-flops does the minimized machine need, and how many did it need before?
Show answer
Before, ⌈log2 6⌉ = 3 flip-flops. After, ⌈log2 3⌉ = 2 flip-flops.