Level 34: FullAdder (Verilog)
Task
Write a full adder in Verilog. It adds three bits: a, b, and carry-in c_in. Outputs are sum and carry-out c_out.
You may use the HalfAdder module from the previous level — a great chance to practice module reuse.
Remember: Verilog is not a program — it describes a circuit. All lines run simultaneously.
Related Materials
- Full Adder — three-bit addition
- Half Adder — two-bit addition
Solution
A full adder is easily built from two half adders (module HalfAdder from level 33) and one OR gate.
Port order of HalfAdder: (sum, carry, a, b).
- Declare three internal wires:
wire sum1, carry1, carry2; - First half adder adds
aandb:HalfAdder ha1(sum1, carry1, a, b); - Second half adder adds
sum1andc_in:HalfAdder ha2(sum, carry2, sum1, c_in); - Combine both carries through OR:
or g1(c_out, carry1, carry2);
The first half adder produces an intermediate sum sum1 and a first carry carry1. The second gives the final sum sum and a second carry carry2. The two carries are never both 1 — OR safely combines them.
Alternative: build FullAdder from 5 primitives (2×XOR, 2×AND, 1×OR) — try both approaches!