Problem: Implement Half Subtractor in Verilog (gate level)
Objective
A Half Subtractor is a combinational circuit that subtracts one 1-bit binary input (b) from another (a).
It produces two outputs:
- Difference (D) → result of subtraction
- Borrow (B) → indicates when a borrow is required
In this tutorial, you will design a half subtractor using Verilog and test it with all input combinations.
Explanation (How it works)
The half subtractor handles subtraction of two bits only — it cannot handle borrow from a previous stage (that’s why it’s half).
-
Difference Equation:
D = a ⊕ b
-
Borrow Equation:
B = (~a) & b
(Borrow is needed whena = 0
andb = 1
.)
Requirements
- Create a Verilog module named
half_subtractor
with:- Inputs:
a
,b
- Outputs:
diff
,borrow
- Inputs:
- Implement using the logic equations:
assign diff = a ^ b;
assign borrow = (~a) & b;
- Write a
testbench
module to verify:- Apply all 4 input combinations (
00
,01
,10
,11
). - Observe difference and borrow.
- Apply all 4 input combinations (
Expected Truth Table
a | b | diff | borrow |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 1 | 1 |
1 | 0 | 1 | 0 |
1 | 1 | 0 | 0 |
Test Plan (What to do)
- Simulate
testbench
and inspect signalsa, b, diff, borrow
in waveforms. - Verify
diff = a ⊕ b
andborrow = (~a) & b
for all inputs. - Ensure truth table matches simulation results.
Common Pitfalls
- Forgetting to invert
a
in the borrow equation. - Using
a - b
directly in Verilog (not recommended for logic design tutorials). - Not testing all input cases.
Extension (Challenge)
- Compare the circuit with a Full Adder and notice the symmetry between addition and subtraction.
No Waveform Data
Submit your Verilog code using Cmd+Enter to see the simulation results and waveform visualization.