Draft preview. Lessons are being written and reviewed.

Digital Design / Module 6: Finite state machines

35. Detecting 1101, with overlap

Build the classic exercise and see your own code run.

Lecture coming soon

Lab: your code, animated

The idea

Detecting a bit pattern is the classic FSM exercise, because it teaches you what a state really is. To detect 1101, each state records how much of the pattern has matched so far.

StateMeaning
S0Nothing useful yet
S1Saw 1
S11Saw 11
S110Saw 110
S1101Saw 1101: output high

Building the transitions

When a new bit arrives, ask: "What is the longest ending of the bits so far that is also a beginning of the pattern?" That is your next state.

  • S0: 1 goes to S1; 0 stays.
  • S1: 1 goes to S11; 0 returns to S0.
  • S11: 1 stays in S11, because the latest two bits are still 11; 0 goes to S110.
  • S110: 1 goes to S1101; 0 returns to S0.
  • S1101: 1 goes to S11, because the final 1 plus the new 1 give 11; 0 returns to S0.

Overlap

The S1101 transition is where overlap lives. The final 1 of a match can also be the first 1 of the next. In the stream 1101101, matches end at positions 4 and 7, because position 4's 1 is shared.

Your own code, animated

In the lab above, write the detector yourself. It runs your actual code and builds the state diagram from what your design really did, then stops at the first wrong cycle. The starter contains a bug. Run it, read which cycle fails, and fix that transition.

Common mistake

The S11 case. Many people return to S1 on a further 1, and this loses the fact that the last two bits are still 11. Test with 11101: the detector must fire at the end of it.

Key ideas

  • State records how much of the pattern has been matched.
  • On overlap, the final 1 of a match can begin the next match.
  • After 1101, a 1 gives 11, while a 0 returns to the start.

Your exercise

Write the detector in the lab: it animates your code.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: The stream 1101 is detected, but 11101 is missed, even though it ends in 1101.

Interview questions

coreStream 1101101: how many overlapping matches?

coreMoore states needed?

basicIn a pattern detector for 1101, what does each state actually stand for?

basicA Moore detector for 1101 has five states. How many flops does it need with binary encoding and with one-hot encoding, and how many binary codes go unused?

coreThe machine is in S11 (it has just seen 11) and another 1 arrives. Which state should it go to, and why do many people get this wrong?

coreThis detector passes a test on the stream 1101101 but fails in the field. Find the bug and a short stream that shows it.

always @(posedge clk)
  if (rst) state <= S0;
  else case (state)
    S0:    state <= din ? S1    : S0;
    S1:    state <= din ? S11   : S0;
    S11:   state <= din ? S1    : S110;
    S110:  state <= din ? S1101 : S0;
    S1101: state <= din ? S11   : S0;
    default: state <= S0;
  endcase
assign detect = (state == S1101);

coreHow would you build the same 1101 detector as a Mealy machine, and how does its output timing differ from the Moore version?

assign detect = (state == S110) & din;

coreAn overlapping 1101 detector sees the stream 1101101101, first bit first. How many times does it fire, and at which bit positions do the matches end?

advancedHow would you convince yourself that your 1101 detector is correct on every possible input, not just the streams you thought of?

advancedFor an overlapping detector of 1011 (not 1101), where should the full-match state go on a 1 and on a 0?

Practise these 10 questions with animations