/*
 * Exercise BB8 on Tiny Tapeout: a FIFO of sixteen 3-bit words with a push
 * and a pop input.  The 7-segment display of the demo board shows the head
 * of the queue, and DOR, DIR and the head go to the BIDIR Pmod.
 *
 * Add fifo_demo.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 empties the FIFO.  DIP switches 0-2 are the data,
    // switch 3 is PUSH and switch 4 is POP: turn the switch on, wait for a
    // tick of the clock, and turn it off again.

    wire [2:0] head;
    wire       dor, dir;

    fifo_demo i_fifo_demo (
        .clk  ( clk         ),
        .rst  ( ~ rst_n     ),
        .push ( ui_in [3]   ),
        .pop  ( ui_in [4]   ),
        .data ( ui_in [2:0] ),
        .head ( head        ),
        .dor  ( dor         ),
        .dir  ( dir         )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g,
    // uo_out [7] is the dot.  The head as a digit, dark while the FIFO is
    // empty; the dot lights while the FIFO is full.

    reg [6:0] gfedcba;

    always @(*)
        if (~ dor)
            gfedcba = 7'b0000000;
        else
            case (head)
            3'd0: gfedcba = 7'b0111111;
            3'd1: gfedcba = 7'b0000110;
            3'd2: gfedcba = 7'b1011011;
            3'd3: gfedcba = 7'b1001111;
            3'd4: gfedcba = 7'b1100110;
            3'd5: gfedcba = 7'b1101101;
            3'd6: gfedcba = 7'b1111101;
            3'd7: gfedcba = 7'b0000111;
            endcase

    assign uo_out  = { ~ dir, gfedcba };

    // DIR, DOR and the head on the BIDIR Pmod, for LEDs or a logic analyzer

    assign uio_out = { dir, 3'b0, dor, head };
    assign uio_oe  = 8'hff;

    // List all unused inputs to prevent warnings

    wire _unused = & { ena, ui_in [7:5], uio_in, 1'b0 };

endmodule
