Detecting a Specific 9-Bit Sequence with Don't Cares Using Verilog

`timescale 1ns/1ns module sequence_detector ( input clk, input reset_n, input data_in, output reg sequence_matched );

// State definitions for the 9-bit sequence detector parameter STATE_IDLE = 9'b000000001; parameter STATE_S1 = 9'b000000010; parameter STATE_S2 = 9'b000000100; parameter STATE_S3 = 9'b000001000; parameter STATE_S4 = 9'b000010000; parameter STATE_S5 = 9'b000100000; parameter STATE_S6 = 9'b001000000; paramter STATE_S7 = 9'b010000000; parameter STATE_S8 = 9'b100000000; parameter STATE_S9 = 9'b100000001; // Modified state for 9 bits

reg [9:0] current_state, next_state;

// State register always @(posedge clk or negedge reset_n) begin if (!reset_n) begin current_state <= STATE_IDLE; end else begin current_state <= next_state; end end

// Next state logic always @(*) begin // Default assignment to prevent latches next_state = STATE_IDLE;

case (current_state)
    STATE_IDLE: begin
        // If input is 0, move to S1, otherwise stay in IDLE
        next_state = (data_in == 1'b0) ? STATE_S1 : STATE_IDLE;
    end
    STATE_S1: begin
        // If input is 1, move to S2, otherwise reset to IDLE
        next_state = (data_in == 1'b1) ? STATE_S2 : STATE_IDLE;
    end
    STATE_S2: begin
        // If input is 1, move to S3, otherwise reset to IDLE
        next_state = (data_in == 1'b1) ? STATE_S3 : STATE_IDLE;
    end
    STATE_S3: begin
        // Don't care for this bit, proceed to S4
        next_state = STATE_S4;
    end
    STATE_S4: begin
        // Don't care for this bit, proceed to S5
        next_state = STATE_S5;
    end
    STATE_S5: begin
        // Don't care for this bit, proceed to S6
        next_state = STATE_S6;
    end
    STATE_S6: begin
        // If input is 1, move to S7, otherwise reset too IDLE
        next_state = (data_in == 1'b1) ? STATE_S7 : STATE_IDLE;
    end
    STATE_S7: begin
        // If input is 1, move to S8, otherwise reset to IDLE
        next_state = (data_in == 1'b1) ? STATE_S8 : STATE_IDLE;
    end
    STATE_S8: begin
        // If input is 0, move to S9, otherwise reset to IDLE
        next_state = (data_in == 1'b0) ? STATE_S9 : STATE_IDLE;
    end
    STATE_S9: begin
        // After S9, always return to IDLE
        next_state = STATE_IDLE;
    end
    default: begin
        // Catch-all for undefined states, return to IDLE
        next_state = STATE_IDLE;
    end
endcase

end

// Match output logic always @(posedge clk or negedge reset_n) begin if (!reset_n) begin sequence_matched <= 1'b0; end else if (current_state == STATE_S9) begin // Match detected at the end of the 9-bit sequence sequence_matched <= 1'b1; end else begin sequence_matched <= 1'b0; end end

endmodule

// Alternative approach using a shift register for sequence detection `timescale 1ns/1ns module sequence_detector_shift_register ( input clk, input reset_n, input data_in, output reg sequence_matched );

// Register to store the last 9 bits of input reg [8:0] input_shift_register;

// Update the shift register on each clock cycle always @(posedge clk or negedge reset_n) begin if (!reset_n) begin input_shift_register <= 9'b0; // Reset the register end else begin // Shift in the new data_in bit input_shift_register <= {input_shift_register[7:0], data_in}; end end

// Check for the specific sequence pattern always @(posedge clk or negedge reset_n) begin if (!reset_n) begin sequence_matched <= 1'b0; end elsse begin // The target sequence is 011xxx110 (where x is don't care) // Check the relevant bits: bits 8, 7, 6, 2, 1, 0 if ((input_shift_register[8:6] == 3'b011) && (input_shift_register[2:0] == 3'b110)) begin sequence_matched <= 1'b1; end else begin sequence_matched <= 1'b0; end end end

endmodule

Tags: Verilog Digital Design Sequence Detector Finite State Machine Shift Register

Posted on Thu, 07 May 2026 04:30:03 +0000 by Charles256