/*
 * Exercise BB9 on Tiny Tapeout: a register file of four 4-bit words with
 * one write port and two read ports.  The 7-segment display of the demo
 * board shows read port A as a hexadecimal digit, and read port B goes to
 * the BIDIR Pmod.
 *
 * Add register_file.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-3 are the data and switches 4-5 the address: the word to write and
    // the word that read port A shows.  Switch 6 is WRITE: turn it on, wait
    // for a tick of the clock, and turn it off again.  BIDIR pins 0-1 are
    // the address of read port B, pins 4-7 show its word.  There is no
    // reset, as in the 40108.

    wire [3:0] rdata_a, rdata_b;

    register_file i_register_file (
        .clk     ( clk          ),
        .we      ( ui_in  [6]   ),
        .waddr   ( ui_in  [5:4] ),
        .wdata   ( ui_in  [3:0] ),
        .raddr_a ( ui_in  [5:4] ),
        .rdata_a ( rdata_a      ),
        .raddr_b ( uio_in [1:0] ),
        .rdata_b ( rdata_b      )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g.
    // Read port A as a hexadecimal digit: 0-9, then A, b, C, d, E, F.

    reg [6:0] gfedcba;

    always @(*)
        case (rdata_a)
        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  = { 1'b0, gfedcba };

    // Read port B on BIDIR pins 4-7; pins 0-3 are inputs

    assign uio_out = { rdata_b, 4'b0 };
    assign uio_oe  = 8'hf0;

    // List all unused inputs to prevent warnings

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

endmodule
