// CD4015B: dual 4-stage shift register, serial input, parallel output.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (8)
// and VDD (16), are left out.  Synthesizable.
//
// Each register shifts on the rising edge of its clock: DATA goes into Q1,
// Q1 into Q2, Q2 into Q3, Q3 into Q4.  RESET is active high and
// asynchronous: it clears all four stages at once, without the clock.
// Connect Q4A to DATA B to make one 8-stage register.

module cd4015
(
    input  logic clock_b,  // Pin 1
    output logic q4b,      // Pin 2
    output logic q3a,      // Pin 3
    output logic q2a,      // Pin 4
    output logic q1a,      // Pin 5
    input  logic reset_a,  // Pin 6:  1 = Q1A-Q4A go to 0 at once
    input  logic data_a,   // Pin 7
                           // Pin 8:  VSS
    input  logic clock_a,  // Pin 9
    output logic q4a,      // Pin 10
    output logic q3b,      // Pin 11
    output logic q2b,      // Pin 12
    output logic q1b,      // Pin 13
    input  logic reset_b,  // Pin 14: 1 = Q1B-Q4B go to 0 at once
    input  logic data_b    // Pin 15
                           // Pin 16: VDD
);

    logic [4:1] ra, rb;  // ra [1] is Q1A, ra [4] is Q4A

    always_ff @ (posedge clock_a or posedge reset_a)
        if (reset_a)
            ra <= '0;
        else
            ra <= { ra [3:1], data_a };

    always_ff @ (posedge clock_b or posedge reset_b)
        if (reset_b)
            rb <= '0;
        else
            rb <= { rb [3:1], data_b };

    assign { q4a, q3a, q2a, q1a } = ra;
    assign { q4b, q3b, q2b, q1b } = rb;

endmodule
