/*
 * Exercise BB6 on Tiny Tapeout: an 8-to-3 priority encoder like the 4532,
 * with the DIP switches as inputs and the 7-segment display of the demo
 * board showing the number of the highest switch that is on.
 *
 * Add priority_encoder.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
);

    // Switches 7-0 are D7-D0

    wire [2:0] q;
    wire       gs, eo;
    wire [6:0] abcdefg;

    priority_encoder i_priority_encoder (
        .d       ( ui_in   ),
        .q       ( q       ),
        .gs      ( gs      ),
        .eo      ( eo      ),
        .abcdefg ( abcdefg )
    );

    // uo_out [0] is segment a, ..., uo_out [6] is g; the dot, uo_out [7],
    // is GS.  Q and EO go to the BIDIR Pmod, for LEDs or a logic analyzer.

    assign uo_out  = { gs, abcdefg [0], abcdefg [1], abcdefg [2], abcdefg [3],
                           abcdefg [4], abcdefg [5], abcdefg [6] };

    assign uio_out = { 4'b0, eo, q };
    assign uio_oe  = 8'b0000_1111;

    // The encoder needs no clock or reset.
    // List all unused inputs to prevent warnings

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

endmodule
