/*
 * Exercise M12 on Tiny Tapeout: a binary counter against a Johnson counter.
 * The 7-segment display of the demo board is a small traffic light, and
 * the two counts go to the BIDIR Pmod.
 *
 * Add counter_race.sv to source_files in info.yaml next to project.v.
 * Rename the module to tt_um_<your GitHub name>_<project name> and use the
 * same name as top_module in info.yaml.
 */

`default_nettype none

module tt_um_example (
    input  wire [7:0] ui_in,    // DIP switches, INPUT Pmod, RP2350 GPIO17-24
    output wire [7:0] uo_out,   // 7-segment display, OUTPUT Pmod, RP2350 GPIO33-40
    input  wire [7:0] uio_in,   // BIDIR Pmod, RP2350 GPIO25-32
    output wire [7:0] uio_out,  //   (same pins)
    output wire [7:0] uio_oe,   //   1 = your design drives the pin
    input  wire       ena,      // set by the on-chip project mux, not a board pin
    input  wire       clk,      // RP2350 GPIO16
    input  wire       rst_n     // RP2350 GPIO14
);

    // The demo board makes the project clock; set it to 1 Hz.  The reset
    // button of the board resets both counters.

    wire [3:0] binary, johnson;
    wire       a_gt_b, a_eq_b, a_lt_b;

    counter_race i_counter_race (
        .clk     ( clk     ),
        .rst     ( ~ rst_n ),
        .binary  ( binary  ),
        .johnson ( johnson ),
        .a_gt_b  ( a_gt_b  ),
        .a_eq_b  ( a_eq_b  ),
        .a_lt_b  ( a_lt_b  )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g.
    // Segment a (top) is red for binary < Johnson, g (middle) yellow for
    // equal, d (bottom) green for binary > Johnson.

    assign uo_out  = { 1'b0, a_eq_b, 2'b0, a_gt_b, 2'b0, a_lt_b };

    // Both counts on the BIDIR Pmod, for LEDs or a logic analyzer

    assign uio_out = { johnson, binary };
    assign uio_oe  = 8'hff;

    // List all unused inputs to prevent warnings

    wire _unused = & { ena, ui_in, uio_in, 1'b0 };

endmodule
