DEV Community

Open Row
Open Row

Posted on Fully Autonomous

The 1011 Sequence Detector: The FSM Question Every RTL Interview Asks

"Design an FSM that outputs 1 when it sees 1011 on a serial input" is probably the most common RTL whiteboard question there is. It looks easy, and most of the grade comes from one detail: what happens when the pattern overlaps.

The trick: name states by the prefix you've matched

Call each state by the longest prefix of 1011 you've seen so far: S0 (nothing), S1, S10, S101, and S1011 (match found). On a matching bit you move forward. On a mismatch you don't always go back to S0. You go to the longest suffix of what you've seen that is still a prefix of 1011.

That rule gives every transition:

State x = 0 x = 1
S0 S0 S1
S1 S10 S1
S10 S0 S101
S101 S10 S1011
S1011 S10 S1

The two that trip people up: from S101, a 0 gives "1010", whose suffix "10" is still a valid prefix, so you go to S10, not S0. From S1011, a 1 gives "10111", which leaves just "1", so S1. That's what makes the input 1011011 fire twice.

The SystemVerilog

// Moore FSM: y=1 after seeing 1011, overlapping allowed
module seq1011 (
  input  logic clk, rst_n, x,
  output logic y
);
  typedef enum logic [2:0] {S0, S1, S10, S101, S1011} state_t;
  state_t s, ns;
  always_ff @(posedge clk or negedge rst_n)
    if (!rst_n) s <= S0; else s <= ns;
  always_comb begin
    ns = s;
    case (s)
      S0:    ns = x ? S1    : S0;
      S1:    ns = x ? S1    : S10;
      S10:   ns = x ? S101  : S0;
      S101:  ns = x ? S1011 : S10;
      S1011: ns = x ? S1    : S10;
      default: ns = S0;
    endcase
  end
  assign y = (s == S1011);
endmodule
Enter fullscreen mode Exit fullscreen mode

Three things interviewers look for in that code: the state register and next-state logic are separate (nonblocking in always_ff, blocking in always_comb), ns has a default so no latch is inferred, and there's a default branch so unused encodings recover.

The follow-ups

  • "Make it Mealy." You drop S1011: the output becomes (s == S101) && x, so it fires in the same cycle as the last bit, one cycle earlier, with four states. The trade-off is that the output now depends combinationally on the input.
  • "Non-overlapping?" After a match, return to S0 instead of continuing from the suffix.
  • "How would you verify it?" A reference model that slides a 4-bit window over random input, compared every cycle, plus directed tests for 1011011 and 10101011.

Want 19 more like this?

The RTL Design Interview Problem Pack has 20 classic whiteboard problems (FIFOs, clock domain crossing, arbiters, LFSRs, handshakes, setup/hold math), each with what the interviewer is testing, a synthesizable SystemVerilog solution, and the follow-ups they ask.

This article was generated with AI from the author's study notes; the code was checked by synthesizing it with Yosys.

Top comments (0)