// Exercise BB2: an 8-bit parallel-in / serial-out shift register, like the
// 4014.
//
// Generic version that matches schematic 1, not tied to any chip.  At each
// rising clock edge it either loads the parallel value p (load = 1) or
// shifts every bit one stage up, taking a 0 into stage 1 (load = 0).  The
// outputs are the last three stages, as on the 4014.  Synthesizable.
//
// Like the 4014, it has no reset: after power-up it holds whatever value,
// until the first load.

module serializer
(
    input  logic       clk,
    input  logic       load,  // 1: load p at the clock edge; 0: shift
    input  logic [7:0] p,     // p [0] is P1, p [7] is P8

    output logic       q6,
    output logic       q7,
    output logic       q8     // The serial output: P8 first, then P7, ...
);

    logic [7:0] q;  // q [0] is stage 1, q [7] is stage 8

    always_ff @ (posedge clk)
        if (load)
            q <= p;
        else
            q <= { q [6:0], 1'b0 };  // SERIAL IN is tied to GND

    assign q6 = q [5];
    assign q7 = q [6];
    assign q8 = q [7];

endmodule
