// Exercise P1: a bit-serial adder.  Two 4-bit numbers are turned into
// streams of bits, added one bit per clock with a stored carry, and the
// sum bits are collected back into a number.
//
// Generic version that matches schematic 1, not tied to any chip.
//
// load at a rising edge of clk puts a and b into the serializers and
// clears the carry.  At every later edge each serializer moves one bit out,
// lowest first, then zeros; the adder adds the two bits and the carry, and
// the deserializer takes the sum bit.  load also clears the deserializer
// at once.  After four edges sum holds a + b without the fifth bit, and
// later edges push it along.  Like the 4511, the decoders blank the codes
// 10-15.  Synthesizable.

module serial_adder
(
    input  logic       clk,
    input  logic       load,
    input  logic [3:0] a,
    input  logic [3:0] b,

    output logic       serial_a,     // The bit of a on its way, like Q8 of the 4014
    output logic       serial_b,
    output logic       serial_sum,   // The sum bit, like SUM1 of the 4032

    output logic [3:0] sum,          // The deserializer, newest bit in sum [3]

    output logic [6:0] abcdefg_a,    // Segments a-g of each display: abcdefg [6] is a
    output logic [6:0] abcdefg_b,
    output logic [6:0] abcdefg_sum
);

    //------------------------------------------------------------------------
    // The serializers: the bits still to send, the next one at the bottom

    logic [3:0] shift_a, shift_b;

    always_ff @ (posedge clk)
        if (load)
        begin
            shift_a <= a;
            shift_b <= b;
        end
        else
        begin
            shift_a <= shift_a >> 1;
            shift_b <= shift_b >> 1;
        end

    assign serial_a = shift_a [0];
    assign serial_b = shift_b [0];

    //------------------------------------------------------------------------
    // The serial adder: one full adder and a flip-flop for the carry

    logic carry;

    assign serial_sum = serial_a ^ serial_b ^ carry;

    always_ff @ (posedge clk)
        if (load)
            carry <= 1'b0;
        else
            carry <= serial_a & serial_b | (serial_a ^ serial_b) & carry;

    //------------------------------------------------------------------------
    // The deserializer

    always_ff @ (posedge clk or posedge load)
        if (load)
            sum <= '0;
        else
            sum <= { serial_sum, sum [3:1] };

    //------------------------------------------------------------------------
    // Three decoders, like the 4511: codes 10-15 blank the display

    function automatic logic [6:0] decode (input logic [3:0] code);
        case (code)
        4'd0:    decode = 7'b111_1110;
        4'd1:    decode = 7'b011_0000;
        4'd2:    decode = 7'b110_1101;
        4'd3:    decode = 7'b111_1001;
        4'd4:    decode = 7'b011_0011;
        4'd5:    decode = 7'b101_1011;
        4'd6:    decode = 7'b001_1111;
        4'd7:    decode = 7'b111_0000;
        4'd8:    decode = 7'b111_1111;
        4'd9:    decode = 7'b111_0011;
        default: decode = 7'b000_0000;
        endcase
    endfunction

    assign abcdefg_a   = decode (a);
    assign abcdefg_b   = decode (b);
    assign abcdefg_sum = decode (sum);

endmodule
