// Synthesizable, zero-delay generic circuit. Segment order: { g, f, e, d, c, b, a }.

module basic_counter
(
    input              clk,
    input              rst,
    input              inhibit,
    input              up,
    input              binary,
    output logic [3:0] q,
    output             co_n
);

    wire [3:0] limit = binary ? 4'd15 : 4'd9;

    // Reset before changing binary/decimal mode. Decimal states 10..15
    // are outside this lesson's contract; there is no dynamic JAM loading.
    always_ff @ (posedge clk or posedge rst)
        if (rst)
            q <= '0;
        else if (~ inhibit)
        begin
            if (up)
                q <= q == limit ? 4'd0 : q + 1'b1;
            else
                q <= q == 4'd0 ? limit : q - 1'b1;
        end

    assign co_n = inhibit | ~ (up ? q == limit : q == 4'd0);

endmodule
