/*
 * Exercise P3 on Tiny Tapeout: the FIFO adder.  Numbers pushed into FIFO A
 * and FIFO B are added in pairs into FIFO C.  The 7-segment display of the
 * demo board shows the head of C in hexadecimal.
 *
 * Add fifo_adder.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.  DIP switches
    // 0-2 are the data, switch 3 is PUSH A, switch 4 PUSH B and switch 5
    // POP C: turn the switch on, wait for a tick of the clock, and turn it
    // off again.  The reset button of the board empties the FIFOs.

    wire [2:0] head_a, head_b;
    wire [3:0] head_c;
    wire       dor_a, dor_b, dor_c, dir_c, transfer;

    fifo_adder i_fifo_adder (
        .clk      ( clk         ),
        .rst      ( ~ rst_n     ),
        .push_a   ( ui_in [3]   ),
        .push_b   ( ui_in [4]   ),
        .pop_c    ( ui_in [5]   ),
        .data     ( ui_in [2:0] ),
        .head_a   ( head_a      ),
        .dor_a    ( dor_a       ),
        .head_b   ( head_b      ),
        .dor_b    ( dor_b       ),
        .head_c   ( head_c      ),
        .dor_c    ( dor_c       ),
        .dir_c    ( dir_c       ),
        .transfer ( transfer    )
    );

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

    reg [6:0] gfedcba;

    always @(*)
        if (~ dor_c)
            gfedcba = 7'b0000000;
        else
            case (head_c)
            4'h0: gfedcba = 7'b0111111;
            4'h1: gfedcba = 7'b0000110;
            4'h2: gfedcba = 7'b1011011;
            4'h3: gfedcba = 7'b1001111;
            4'h4: gfedcba = 7'b1100110;
            4'h5: gfedcba = 7'b1101101;
            4'h6: gfedcba = 7'b1111101;
            4'h7: gfedcba = 7'b0000111;
            4'h8: gfedcba = 7'b1111111;
            4'h9: gfedcba = 7'b1101111;
            4'ha: gfedcba = 7'b1110111;
            4'hb: gfedcba = 7'b1111100;
            4'hc: gfedcba = 7'b0111001;
            4'hd: gfedcba = 7'b1011110;
            4'he: gfedcba = 7'b1111001;
            4'hf: gfedcba = 7'b1110001;
            endcase

    assign uo_out  = { ~ dir_c, gfedcba };

    // The flags and the head of A on the BIDIR Pmod

    assign uio_out = { head_a, transfer, dor_c, dor_b, dor_a, 1'b0 };
    assign uio_oe  = 8'hfe;

    // List all unused inputs to prevent warnings

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

endmodule
