// CD4032B: triple serial adder.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (8)
// and VDD (16), are left out.
//
// Each adder takes two numbers one bit per clock, lowest bit first.  Its
// SUM output is A XOR B XOR the stored carry, at once, without the clock;
// the rising edge of CLOCK stores the carry for the next bit.  INVERT at 1
// inverts the sum.  The datasheet asks to clear the carries with CARRY
// RESET at 1 one bit before the first bit of the next word; the model
// clears them at the rising edge of CLOCK that sees CARRY RESET at 1.
// The three adders share CLOCK and CARRY RESET.  Synthesizable.

module cd4032
(
    output logic sum3,         // Pin 1
    input  logic invert3,      // Pin 2
    input  logic clock,        // Pin 3
    output logic sum2,         // Pin 4
    input  logic invert2,      // Pin 5
    input  logic carry_reset,  // Pin 6:  1 at a rising edge = carries 0
    input  logic invert1,      // Pin 7
                               // Pin 8:  VSS
    output logic sum1,         // Pin 9
    input  logic a1,           // Pin 10
    input  logic b1,           // Pin 11
    input  logic b2,           // Pin 12
    input  logic a2,           // Pin 13
    input  logic b3,           // Pin 14
    input  logic a3            // Pin 15
                               // Pin 16: VDD
);

    wire  [3:1] a   = { a3, a2, a1 };
    wire  [3:1] b   = { b3, b2, b1 };
    wire  [3:1] inv = { invert3, invert2, invert1 };

    logic [3:1] carry;

    always_ff @ (posedge clock)
        if (carry_reset)
            carry <= '0;
        else
            carry <= a & b | (a ^ b) & carry;

    assign { sum3, sum2, sum1 } = a ^ b ^ carry ^ inv;

endmodule
