Sub topic 17
Designing Sequence Detectors and Minimizing States
Assume that you know Boolean algebra, flip-flops, and the basic idea of a finite state machine (FSM). This chapter focuses on converting a detection rule into states, handling overlapping patterns, and removing states that do not affect the circuit's behavior.
Sequence detectors
A sequence detector is an FSM that examines one input bit per clock cycle and asserts an output when a target pattern has been recognized. For example, a detector for 101 must remember enough of the recent input history to know how close it is to completing the pattern.
A state should represent the longest suffix of the input seen so far that is also a prefix of the target pattern:
S0: no useful prefix has been matchedS1: the suffix1has been matchedS2: the suffix10has been matchedS3: the complete pattern101has been matched
A Moore machine produces its output from the current state. Therefore, S3 has output 1, and all other states have output 0.
| Current state | Input `0` | Input `1` | Output |
|---|---|---|---|
| S0 | S0 | S1 | 0 |
| S1 | S2 | S1 | 0 |
| S2 | S0 | S3 | 0 |
| S3 | S2 | S1 | 1 |
The transition from S3 supports overlapping detection. After recognizing 101, a new 1 leaves the suffix 1, so the machine moves to S1. A new 0 leaves the suffix 10, so it moves to S2.
A Mealy machine places the output on a transition instead of a state. It can assert the detection output on the same clock transition that receives the final 1. A Moore machine usually needs an extra state or one-cycle later output, but its output changes only when the state changes.
State minimization
State minimization reduces hardware by merging equivalent states. Two states are equivalent when:
- They produce the same output.
- For every possible input, their next states are equivalent.
Start by grouping states with equal outputs. Then repeatedly split a group when its states transition to different groups for some input. Continue until no group can be split.
For example, if states A and B both output 0, and both transition to the same equivalence groups for inputs 0 and 1, they can be replaced by one state. If a later transition distinguishes them, they must remain separate.
After minimization, assign binary codes to the remaining states. With n states, use at least ceil(log2 n) flip-flops. Unused encodings need a defined recovery transition, usually to the reset state.
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);
endmoduleExercise
In a Moore detector for 110, what should happen after the pattern is detected and the next input is 1?
Show answer
The machine should retain the suffix 1 if that suffix is a prefix of the target pattern. This supports overlapping detection instead of restarting unconditionally at the initial state.