// Exercise M10: a 5-stage Johnson counter.
//
// Generic version that matches schematic 1, not tied to any chip.  Five
// D flip-flops form a shift register; the inverted last stage goes back
// into the first.  From 00000 the register fills with ones from the bottom
// and then with zeros: ten states, then it repeats.  The 4017 of exercise
// BB7 has the same counter inside.  rst clears all stages at once, without
// the clock.  Synthesizable.

module johnson_counter
(
    input  logic       clk,
    input  logic       rst,

    output logic [4:0] q     // q [0] is stage 1, q [4] is stage 5
);

    always_ff @ (posedge clk or posedge rst)
        if (rst)
            q <= '0;
        else
            q <= { q [3:0], ~ q [4] };

endmodule
