module seq_detector_1011 (
input wire clk,
input wire rst, // Active-high synchronous reset
input wire din, // Serial input
output reg dout // Output: 1 when 1011 is detected
);
// State encoding
localparam A = 2’b00, // Idle / initial state
B = 2’b01, // Received ‘1’
C = 2’b10, // Received ’10’
D = 2’b11; // Received ‘101’
reg [1:0] curr_state, next_state;
// ——————————————————–
// State Register (Sequential)
// ——————————————————–
always @(posedge clk) begin
if (rst)
curr_state <= A;
else
curr_state <= next_state;
end
// ——————————————————–
// Next-State Logic (Combinational)
// ——————————————————–
always @(*) begin
case (curr_state)
A: next_state = din ? B : A;
B: next_state = din ? B : C;
C: next_state = din ? D : A;
D: next_state = din ? A : C; // Non-overlapping → back to A on detect
default: next_state = A;
endcase
end
// ——————————————————–
// Output Logic (Mealy — depends on state + input)
// ——————————————————–
always @(*) begin
case (curr_state)
D: dout = din ? 1’b1 : 1’b0; // Output 1 only on 4th bit ‘1’
default: dout = 1’b0;
endcase
end
endmodule