4-bit Adder-Subtractor

Design a 4-bit adder-subtractor controlled by a single line: when sub is 0 the circuit computes a + b; when sub is 1 it computes a - b using two's complement, by XOR-ing each bit of b with sub and feeding sub in as the carry in. Inputs: a and b, 4-bit operands; sub, the mode (0 = add, 1 = subtract). Outputs: result, the 4-bit result; cout, the carry out of the adder (for subtraction this is 1 when a >= b, i.e. no borrow). The testbench applies all 512 combinations and compares {cout, result} with a + b (sub = 0) or a + ~b + 1 (sub = 1).

Compiler Output
Waveform Viewer