// Exercise M12: a binary counter and a Johnson counter, both 4 bits wide,
// run from one clock and compared, as in Maya Grigoryeva's project.
//
// Generic version that matches schematic 1, not tied to any chip.  The
// binary counter steps through 0 ... 15, the Johnson counter through its
// eight codes 0000, 0001, 0011, 0111, 1111, 1110, 1100, 1000.  A comparator
// says whether the binary value is greater than, equal to or less than the
// Johnson code read as a number.  rst clears both counters at once, without
// the clock.  Synthesizable.

module counter_race
(
    input  logic       clk,
    input  logic       rst,

    output logic [3:0] binary,
    output logic [3:0] johnson,

    output logic       a_gt_b,   // binary >  johnson
    output logic       a_eq_b,   // binary == johnson
    output logic       a_lt_b    // binary <  johnson
);

    always_ff @ (posedge clk or posedge rst)
        if (rst)
        begin
            binary  <= '0;
            johnson <= '0;
        end
        else
        begin
            binary  <= binary + 4'd1;
            johnson <= { johnson [2:0], ~ johnson [3] };
        end

    assign a_gt_b = binary >  johnson;
    assign a_eq_b = binary == johnson;
    assign a_lt_b = binary <  johnson;

endmodule
