fsm.sv 745 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
`default_nettype none

module FSM(
    input logic a,
    output logic x,
    input logic clock, clear
);

    enum {S_A, S_B, S_C} currentState, nextState;

    always_ff @(posedge clock)
        if(clear) begin
            currentState <= S_A;
        end else begin
            currentState <= nextState;
        end

    always_comb begin
        nextState = currentState;
        unique case(currentState)
            S_A: nextState = a ? S_B : S_C;
            S_B: nextState = a ? S_A : S_B;
            S_C: nextState = S_A;
        endcase
    end

    always_comb begin
        x = 1'b0;
        unique case(currentState)
            S_A: x = ~a;
            S_B: x = 1'b1;
            S_C: x = 1'b0;
        endcase
    end

endmodule