// Mealy sequence detector for the bit pattern 1011 (overlapping matches). // The output depends on the state AND the current input, so it rises in // the same cycle as the last 1 of the pattern, one cycle before the Moore // version. It needs one state fewer than the Moore machine. `timescale 1ns / 1ps module seq_detect_mealy ( input logic clk, input logic rst, // synchronous, active high input logic din, output logic detect // combinational: high while din completes 1011 ); typedef enum logic [1:0] { S_IDLE = 2'd0, S_1 = 2'd1, S_10 = 2'd2, S_101 = 2'd3 } state_t; state_t state, state_next; always_ff @(posedge clk) begin if (rst) state <= S_IDLE; else state <= state_next; end always_comb begin state_next = state; detect = 1'b0; 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: begin if (din) begin detect = 1'b1; // pattern complete on this input state_next = S_1; // the final 1 starts a new prefix end else begin state_next = S_10; end end default: state_next = S_IDLE; endcase end endmodule