// Exercise B8: a 4-bit adder, its inputs from buttons and its outputs on
// LEDs.
//
// Generic version that matches schematic 1: four full adders in a chain
// (a ripple-carry adder), not tied to any chip.  Synthesizable.

module adder_leds
(
    input  logic [3:0] a,      // The first number, from buttons A4-A1
    input  logic [3:0] b,      // The second number, from buttons B4-B1

    output logic [3:0] sum,    // a + b without the carry, on LEDs 4-1
    output logic       carry   // The fifth bit of the sum: 1 when a + b > 15
);

    // Each full adder adds one bit of a, one bit of b and the carry from
    // the bit below, and passes its own carry to the bit above

    logic [4:0] c;

    assign c [0] = 1'b0;  // The carry into bit 0, like CARRY IN of the 4008 tied to GND

    for (genvar i = 0; i < 4; i ++)
    begin : full_adder
        assign sum [i]   = a [i] ^ b [i] ^ c [i];
        assign c [i + 1] = a [i] & b [i] | (a [i] ^ b [i]) & c [i];
    end

    assign carry = c [4];

endmodule
