Writing a 4-bit Adder in Verilog
๐ก Concept Overview
A 4-bit adder performs binary addition of two 4-bit numbers (A and B) and generates a 4-bit sum along with a carry-out bit.
It’s a combination of logic gates that models how addition works at the hardware level.
๐ป Verilog Code: 4-Bit Adder
// 4-Bit Adder in Verilog
module four_bit_adder (
input [3:0] A, // 4-bit input A
input [3:0] B, // 4-bit input B
output [3:0] SUM, // 4-bit sum output
output CARRY // Carry-out
);
assign {CARRY, SUM} = A + B; // Perform addition
endmodule
⚙️ Explanation
input [3:0] → Defines 4-bit input signals for A and B.
output [3:0] → Defines a 4-bit output for the SUM.
assign → Used for continuous assignment; the addition happens automatically when inputs change.
{CARRY, SUM} → Concatenation; stores the carry and the sum result together.
๐งช Testbench Example
You can simulate this using a testbench to verify the adder’s behavior.
module test_four_bit_adder;
reg [3:0] A, B;
wire [3:0] SUM;
wire CARRY;
four_bit_adder uut (A, B, SUM, CARRY);
initial begin
A = 4'b0101; B = 4'b0011; #10; // 5 + 3
$display("A=%b B=%b | SUM=%b CARRY=%b", A, B, SUM, CARRY);
A = 4'b1111; B = 4'b0001; #10; // 15 + 1
$display("A=%b B=%b | SUM=%b CARRY=%b", A, B, SUM, CARRY);
$finish;
end
endmodule
▶️ Simulation Output
A=0101 B=0011 | SUM=1000 CARRY=0
A=1111 B=0001 | SUM=0000 CARRY=1
Learn VLSI Course in Hyderabad
Read More
First Verilog Code: Hello World
๐ป HDL Programming (Verilog/VHDL)
Glitches in Digital Circuits: Causes and Fixes
Introduction to Synchronous Design
Comments
Post a Comment