// Moore sequence detector for the bit pattern 1011 (overlapping matches). // Two-process style: one always_ff for the state register, one always_comb // for the next-state logic. The output is registered (look-ahead), so it // comes straight from a flip-flop and is high for one cycle per match. `timescale 1ns / 1ps module seq_detect_moore ( input logic clk, input logic rst, // synchronous, active high input logic din, // serial input, one bit per clock output logic detect // high for one cycle after 1011 has been received ); typedef enum logic [2:0] { S_IDLE = 3'd0, // no useful prefix seen S_1 = 3'd1, // seen 1 S_10 = 3'd2, // seen 10 S_101 = 3'd3, // seen 101 S_1011 = 3'd4 // seen 1011: output state } state_t; state_t state, state_next; // Process 1: state register always_ff @(posedge clk) begin if (rst) state <= S_IDLE; else state <= state_next; end // Process 2: next-state logic (pure combinational) always_comb begin state_next = state; // default: stay case (state) S_IDLE: if (din) state_next = S_1; else state_next = S_IDLE; S_1: if (din) state_next = S_1; else state_next = S_10; S_10: if (din) state_next = S_101; else state_next = S_IDLE; S_101: if (din) state_next = S_1011; else state_next = S_10; S_1011: if (din) state_next = S_1; else state_next = S_10; default: state_next = S_IDLE; // recover from unused codes endcase end // Registered Moore output: decode the next state, store it in a flop. // detect is then exactly (state == S_1011), but glitch-free. always_ff @(posedge clk) begin if (rst) detect <= 1'b0; else detect <= (state_next == S_1011); end endmodule