// Exercise P5: a Moore finite state machine that recognizes the sequence
// 0, 1 on its input a, the "snail" of Harris & Harris, Digital Design and
// Computer Architecture.
//
// Generic version that matches schematic 1, not tied to any chip.  The
// two state bits are written with the equations of the book:
//
//     state [0]' = ~ a                 the last sample of a was 0
//     state [1]' = state [0] & a       the last two samples were 0, then 1
//
// so the three states are 00 (S0: nothing seen), 01 (S1: a 0 seen) and
// 10 (S2: 0, 1 seen).  The output y of the machine is state [1]: in a
// Moore machine the output depends only on the state.  rst clears both
// bits at once, without the clock.  Synthesizable.

module snail_fsm
(
    input  logic       clk,
    input  logic       rst,
    input  logic       a,
    output logic [1:0] state  // y = state [1]
);

    always_ff @ (posedge clk or posedge rst)
        if (rst)
            state <= '0;
        else
            state <= { state [0] & a, ~ a };

endmodule
