← ProblemsVerilog Language / Modules: Hierarchy

Adder-subtractor

36%hierarchyarithmetic

An adder becomes an adder–subtractor using the two's-complement identity a - b = a + ~b + 1: invert b and add 1 via the carry-in.

Build a 32-bit adder–subtractor from two of the provided add16 modules ({cout, sum} = a + b + cin) plus a little glue logic:

  1. XOR the 32-bit b with sub replicated 32 times ({32{sub}}). When sub = 0 this leaves b unchanged; when sub = 1 it inverts every bit.
  2. Feed the XOR result, in two 16-bit halves, into the same two-add16 ripple structure as before.
  3. Connect sub to the carry-in of the lower adder — that supplies the "+1".

So the circuit computes a + b when sub = 0 and a - b when sub = 1.