Half Adder in Verilog
Introduction
A Half Adder is the simplest digital circuit that performs binary addition.
It takes two 1-bit inputs and produces two outputs:
- Sum → result of XOR operation
- Carry → result of AND operation
In this tutorial, we will describe how to design and test a Half Adder using RTL (Register Transfer Level) modeling in Verilog.
Step 1: Understand the Logic
The Half Adder works with the following logic equations:
-
Sum Equation:
Sum = A ⊕ B -
Carry Equation:
Carry = A · B
Truth Table
| a | b | sum | carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
Step 2: RTL Modeling Concept
In RTL style:
- Outputs are declared as reg because they are assigned inside an
alwaysblock. - The block
always @(*)is used for combinational logic. - Inside the block, we describe the equations for sum and carry.
Step 3: Writing the Testbench
A testbench is used to apply all input combinations and observe the outputs.
It should include:
- Declaration of input regs and output wires.
- Instantiation of the Half Adder module.
- An
initialblock where inputs are changed over time. - Simulation ending with
$finish.
Step 4: Simulation and Verification
- Compile the design and testbench with your simulator (e.g.,
iverilog). - Run the simulation executable.
- Open the waveform file (e.g., in GTKWave) to observe the signals.
- Compare results with the truth table to confirm correctness.
Common Mistakes to Avoid
- Forgetting to declare
sumandcarryas reg when using RTL modeling. - Not using
always @(*), which may cause incomplete sensitivity list issues. - Confusing the XOR (
^) with OR (|) operator when writing equations.
No Waveform Data
Submit your Verilog code using Cmd+Enter to see the simulation results and waveform visualization.