// CD40108B: 4 x 4 multiport register, a register file of four 4-bit words
// with one write port and two read ports.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (12)
// and VDD (24), are left out.
//
// While WRITE ENABLE is 1, the rising edge of CLOCK writes D0-D3 into the
// word that WRITE 1 and WRITE 0 select; while it is 0 the clock does
// nothing.  Bus A shows the word that READ 1A and READ 0A select, bus B the
// word that READ 1B and READ 0B select, at any time, without the clock.
// 3-STATE A and B are active high: 0 turns the outputs of that bus off.
// The chip has no reset: after power-up the words hold anything.
//
// Synthesizable.

module cd40108
(
    output logic q3b,           // Pin 1
    output logic q2b,           // Pin 2
    input  logic enable_a,      // Pin 3:  3-STATE A, 0 = bus A off
    output logic q0a,           // Pin 4
    output logic q1a,           // Pin 5
    output logic q2a,           // Pin 6
    output logic q3a,           // Pin 7
    input  logic write0,        // Pin 8:  write address, bit 0
    input  logic write1,        // Pin 9:  write address, bit 1
    input  logic read1b,        // Pin 10: read address of bus B, bit 1
    input  logic read0b,        // Pin 11: read address of bus B, bit 0
                                // Pin 12: VSS
    input  logic read0a,        // Pin 13: read address of bus A, bit 0
    input  logic read1a,        // Pin 14: read address of bus A, bit 1
    input  logic write_enable,  // Pin 15: 1 = the clock writes
    input  logic clock,         // Pin 16
    input  logic d3,            // Pin 17
    input  logic d2,            // Pin 18
    input  logic d1,            // Pin 19
    input  logic d0,            // Pin 20
    input  logic enable_b,      // Pin 21: 3-STATE B, 0 = bus B off
    output logic q0b,           // Pin 22
    output logic q1b            // Pin 23
                                // Pin 24: VDD
);

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

    always_ff @ (posedge clock)
        if (write_enable)
            word [{ write1, write0 }] <= { d3, d2, d1, d0 };

    assign { q3a, q2a, q1a, q0a } = enable_a ? word [{ read1a, read0a }] : 4'bz;
    assign { q3b, q2b, q1b, q0b } = enable_b ? word [{ read1b, read0b }] : 4'bz;

endmodule
