// Exercise P2: the bit-serial adder of P1, with the 4032 replaced by a full
// adder from gates and a D flip-flop for the carry.
//
// 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.  At every
// later edge each serializer moves one bit out, lowest first, then zeros;
// the full adder adds the two bits and the carry, and the deserializer
// takes the sum bit.  load clears the carry flip-flop and the deserializer
// at once, as RESET of the 4013 and of the 4015 do.  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_gates
(
    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
    output logic       carry,        // The carry flip-flop, like Q1 of the 4013

    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 full adder of exercise M2 and a flip-flop for the carry

    wire p = serial_a ^ serial_b;   // 4070
    wire g = serial_a & serial_b;   // 4081
    wire t = p & carry;             // 4081

    assign serial_sum = p ^ carry;  // 4070

    always_ff @ (posedge clk or posedge load)
        if (load)
            carry <= 1'b0;
        else
            carry <= g | t;         // 4071, into D1 of the 4013

    //------------------------------------------------------------------------
    // 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
