0
0
Verilogprogramming~20 mins

Sequence detector FSM in Verilog - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sequence Detector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple sequence detector FSM
What is the output sequence of this FSM when the input sequence is 1,0,1,1,0,1?
The FSM detects the sequence '101' and outputs 1 when detected, else 0.
Verilog
module seq_detector(input clk, input reset, input in, output reg out);
  typedef enum reg [1:0] {S0, S1, S2} state_t;
  state_t state, next_state;

  always @(posedge clk or posedge reset) begin
    if (reset) state <= S0;
    else state <= next_state;
  end

  always @(*) begin
    out = 0;
    case(state)
      S0: if (in) next_state = S1; else next_state = S0;
      S1: if (!in) next_state = S2; else next_state = S1;
      S2: begin
        if (in) begin
          next_state = S1;
          out = 1;
        end else next_state = S0;
      end
      default: next_state = S0;
    endcase
  end
endmodule
A[0,0,0,1,0,1]
B[0,0,0,0,0,1]
C[0,0,1,0,0,1]
D[0,1,0,1,0,0]
Attempts:
2 left
💡 Hint
Trace the FSM states step-by-step for each input bit.
🧠 Conceptual
intermediate
1:30remaining
Understanding overlapping sequences in FSM
Why is it important for a sequence detector FSM to handle overlapping sequences?
Consider detecting '101' in a stream like '10101'.
ATo allow the FSM to detect the sequence again immediately if it overlaps with the previous one.
BTo reset the FSM after each detection to avoid false positives.
CTo ignore repeated inputs and reduce output glitches.
DTo increase the clock frequency for faster detection.
Attempts:
2 left
💡 Hint
Think about sequences that share bits between detections.
🔧 Debug
advanced
2:30remaining
Identify the error in this sequence detector FSM code
This FSM is supposed to detect the sequence '110'. What error causes it to never output 1?
Verilog
module seq_detector(input clk, input reset, input in, output reg out);
  typedef enum reg [1:0] {S0, S1, S2} state_t;
  state_t state, next_state;

  always @(posedge clk or posedge reset) begin
    if (reset) state <= S0;
    else state <= next_state;
  end

  always @(*) begin
    out = 0;
    case(state)
      S0: if (in) next_state = S1; else next_state = S0;
      S1: if (in) next_state = S2; else next_state = S0;
      S2: if (!in) begin
            next_state = S0;
            out = 1;
          end else next_state = S2;
      default: next_state = S0;
    endcase
  end
endmodule
AThe FSM never resets to S0 on reset signal.
BThe output 'out' is assigned only when transitioning from S2 to S0, missing detection.
CThe output 'out' is assigned inside a combinational block causing glitches.
DThe FSM stays in S2 on input 1, missing the overlapping sequence detection.
Attempts:
2 left
💡 Hint
Check how the FSM transitions from S2 on input 1.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in this FSM Verilog code
Which option correctly fixes the syntax error in this FSM code snippet?
Verilog
always @ (posedge clk or posedge reset) begin
  if reset
    state <= S0;
  else
    state <= next_state;
end
AChange 'if reset' to 'if (reset)'
BAdd semicolon after 'if reset;'
CReplace 'posedge reset' with 'negedge reset'
DRemove 'or posedge reset' from sensitivity list
Attempts:
2 left
💡 Hint
Check the syntax for if statements in Verilog.
🚀 Application
expert
3:00remaining
Number of states in a sequence detector FSM
How many states are required in a Moore FSM to detect the sequence '1011' with overlapping allowed?
A4 states
B6 states
C5 states
D3 states
Attempts:
2 left
💡 Hint
Count states representing each prefix of the sequence including the empty prefix.