Overlapping Sequence Detector (1011)

Medium · Finite State Machines

Design a finite-state machine that watches a serial bit stream and reports every occurrence of the pattern 1011, including overlapping occurrences.

Ports

PortDirWidthMeaning
clkinput1Clock.
rstinput1Synchronous, active-high reset.
dininput1Serial input: one bit is sampled at every rising edge of clk where rst = 0.
detectedoutput1Registered output: high for one cycle after the final 1 of 1011 is sampled.

Behavior

Let b[1], b[2], ... be the bits sampled at the rising edges since the last reset (bits are in time order; b[1] is sampled at the first edge with rst = 0). At the rising edge that samples b[n], the FSM sets

detected = (n >= 4) && b[n-3] == 1 && b[n-2] == 0 && b[n-1] == 1 && b[n] == 1

and keeps that value until the next rising edge. In words: detected is 1 during the clock cycle immediately after the edge that samples the last 1 of the pattern (Moore-style timing: detected is driven by a flip-flop / depends only on the state, never combinationally on din).

1011011 contains two matches (bits 1–4 and bits 4–7), so detected pulses twice.

bits sampled before (or at) a reset edge can never be part of a match.

Example

Input 1 0 1 1 0 1 1 0 0 1 0 1 1 (sampled at edges 1..13, edge 0 is a reset edge):

edge12345678910111213
din1011011001011
detected after the edge0001001000001

Sign up free to solve Overlapping Sequence Detector (1011) in your browser.

More Finite State Machines problems