// Exercise BB9: a register file of four 4-bit words with one write port and
// two read ports, like the 40108.
//
// Generic version that matches schematic 1, not tied to any chip.  While
// we (write enable) is 1, the rising edge of clk writes wdata into the word
// at waddr.  The two read ports show the words at raddr_a and raddr_b at
// any time, without the clock.  There is no reset: as in the 40108, the
// words hold anything until they are written.  Synthesizable.
//
// A processor keeps its registers in a file like this one, only larger:
// an instruction such as add x3, x1, x2 reads x1 and x2 through the two
// read ports and writes x3 through the write port.

module register_file
(
    input  logic       clk,

    input  logic       we,       // write enable
    input  logic [1:0] waddr,    // write address
    input  logic [3:0] wdata,

    input  logic [1:0] raddr_a,  // read port A
    output logic [3:0] rdata_a,

    input  logic [1:0] raddr_b,  // read port B
    output logic [3:0] rdata_b
);

    logic [3:0] word [0:3];

    always_ff @ (posedge clk)
        if (we)
            word [waddr] <= wdata;

    assign rdata_a = word [raddr_a];
    assign rdata_b = word [raddr_b];

endmodule
