Video 4 of 4 · ~10 minutes
Dr. Mike Borowczak · Electrical & Computer Engineering · CECS · UCF
Every serial-protocol controller is a pattern detector for the protocol's start sequence. CAN bus, Ethernet MAC, PCIe link training, USB SOF detection — all start-of-frame detection is a pattern-matching FSM. Packet inspection hardware in network switches runs dozens of pattern-detector FSMs in parallel.
Your Day 11 UART RX detects the start bit using this pattern. Your Day 12 SPI detects chip select. Your capstone FSM will almost certainly be a pattern detector of some kind. Today's methodology is the template.
“I know the problem. I'll start writing Verilog, figure out states as I go, debug in simulation. The editor IS my thinking environment.”
Draw the state diagram on paper first. Enumerate states, transitions, outputs. Validate by walking through test inputs with a pencil. Only then open the editor, where you apply the 3-block template to your already-correct diagram. Debugging a diagram is seconds; debugging Verilog is hours.
Detect when the input bit stream contains 1011. Assert o_detected for one cycle when the last 4 bits form the pattern. Overlapping patterns allowed.
input: 0 1 0 1 1 0 1 1 0 1 1
output: 0 0 0 0 1 0 0 1 0 0 1
↑ ↑ ↑
match match match (overlapping)
S0: haven't matched anything yetS1: last bit was 1 (matched first bit of pattern)S10: last two bits were 10S101: last three bits were 101 — one more 1 completes the pattern
┌─── 0 ───┐
↓ │
┌─ S0 ────── 1 ──→ S1
│ ↑ │
│ │ │
0 0 0
│ │ ↓
│ └────────── S10
│ │
│ 1
│ ↓
└── 0 ──── S101 ──1──→ S101 (with o_detected=1)
(and can immediately match
again in overlapping case)
1 completes the pattern AND we're now seeing ...11, which could be the start of the next overlapping match. So from S101 + input=1, we go to S1 (not S0), because we just saw 1. This is what makes pattern detectors subtle.
Input stream: 1 0 1 1 1 0 1 1. What's the state trajectory and where does o_detected pulse?
Input: 1 0 1 1 1 0 1 1
State: S0 → S1 → S10→ S101→ ✓ → S10→ S101→ ✓
S1 S1
(overlapping match!)
o_det: 0 0 0 0 1 0 0 1
~6 minutes
▸ COMMANDS
cd labs/week2_day07/ex4_pattern/
cat pattern_1011.v # 3-block from diagram
make sim # covers overlap cases
make wave
make stat
▸ EXPECTED STDOUT
PASS: detects 1011
PASS: ignores 1010
PASS: detects overlapping 10110111
PASS: reset → S0
=== 16 passed, 0 failed ===
SB_DFF: 2
SB_LUT4: 4
▸ GTKWAVE
Signals: i_bit · r_state · o_detected. Watch state traverse S0→S1→S10→S101. Detection pulses are 1-cycle wide. The overlapping pattern exercises the S101→S1 transition explicitly.
always blocks (state reg, next-state, output)r_next = r_state;), default: case at enddefault: case at endlocalparam, never magic numbers<= in sequential (Block 1); blocking = in combinational (Blocks 2, 3)Ask AI: “Design an FSM that detects the pattern ‘110101’ in a serial bit stream. Include the state diagram description, 3-block Verilog, and a self-checking testbench with overlapping test cases.”
TASK
Ask AI for a complete FSM package.
BEFORE
Predict: 6 states + idle, 3-block Verilog, overlapping sequence tests.
AFTER
Strong AI handles overlaps. Weak AI only handles non-overlapping cases — subtle bug.
TAKEAWAY
Always test the overlap case. AI-written FSMs commonly miss it.
① Draw the state diagram before writing code.
② Walk test cases with a pencil. Catch bugs in diagrams, not in Verilog.
③ Pattern detector overlaps: new match can begin from a partial match.
④ Test every transition, including the edge cases.
Q1: Why is the state diagram drawn before the Verilog?
Q2: What transition happens when a pattern detector is in its near-final state and receives an input that completes an overlapping prefix?
Q3: For a 6-state FSM on iCE40, which encoding is likely more compact?
make stat.Q4: Name three cases your testbench must cover for a pattern detector.
🔗 End of Day 7
Day 8 · Hierarchy, Parameters, Generate
▸ WHY THIS MATTERS NEXT
You've built modules up to a few hundred lines. Real designs have thousands of lines, across dozens of files, arranged hierarchically. Day 8 teaches the three Verilog features that make this scale: module hierarchy, parameters for configurable IP, and generate blocks for array-style replication. By end of Day 8 your growing module library will become a reusable toolbox.