COA LAB Merged
COA LAB Merged
INSTITUTE OF ENGINEERING
PURWANCHAL CAMPUS DHARAN
Department of Electronics and Computer Engineering
Program:
// design.sv
module UnsignedAdder(
input [4:0] A, // 5-bit input A
input [4:0] B, // 5-bit input B
output [4:0] Sum, // 5-bit output for the sum
output CarryOut // Output for the carry out
);
// Internal signals
reg [4:0] sum_temp; // Temporary sum to handle overflow
reg carry_temp; // Temporary carry
// Add A and B
always @(A,B) begin
{carry_temp, sum_temp} = A + B;
end
// Output signals
assign CarryOut = carry_temp; // Output the carry out
assign Sum = (carry_temp) ? {1'b1, sum_temp} : sum_temp; // Adjust sum based on carry
endmodule
// testbench.sv
module testbench;
// Inputs
reg [4:0] A;
reg [4:0] B;
// Outputs
wire [4:0] Sum;
wire CarryOut;
// Instantiate the UnsignedAdder module
UnsignedAdder dut (A,B,Sum,CarryOut);
// Stimulus generation
initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
// Test case 1: A = 8, B = 7
A = 5'b01000;
B = 5'b00111;
#10; // Wait for 10 time units
// Display simulation results
$display("A = %b, B = %b, Sum = %b, CarryOut = %b", A, B, Sum, CarryOut);
// Test case 2: A = 7, B = 31
A = 5'b00111;
B = 5'b11111;
#10; // Wait for 10 time units
// Display simulation results
$display("A = %b, B = %b, Sum = %b, CarryOut = %b", A, B, Sum, CarryOut);
end
endmodule
Output:
# KERNEL: A = 01000, B = 00111, Sum = 01111, CarryOut = 0
# KERNEL: A = 00111, B = 11111, Sum = 00110, CarryOut = 1
Program:
// design.sv
module BinarySubtractor4Bit(
input [3:0] A, // 4-bit input for the first binary number
input [3:0] B, // 4-bit input for the second binary number
output reg [3:0] diff, // 4-bit output for the difference
output reg borrow_out // Output for the borrow
);
always @* begin
// Initialize borrow_out to 0
borrow_out = 1'b0;
// Calculate the difference bit by bit, starting from the LSB
for (int i = 0; i < 4; i = i + 1) begin
// Calculate the difference of the current bits and the borrow
diff[i] = A[i] ^ B[i] ^ borrow_out;
// Update the borrow for the next bit subtraction
if (~A[i] & (B[i] | borrow_out))
borrow_out = 1'b1;
else
borrow_out = 1'b0;
end
end
endmodule
// testbench.sv
module testbench;
// Inputs
reg [3:0] A;
reg [3:0] B;
// Outputs
wire [3:0] diff;
wire borrow_out;
// Instantiate the UnsignedAdder module
BinarySubtractor4Bit dut (A,B,diff,borrow_out);
// Stimulus generation
initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
// Test case 1: A = 6, B = 4
A = 4'b0110;
B = 4'b0100;
#10; // Wait for 10 time units
// Display simulation results
$display("A = %b, B = %b, diff = %b, borrow_out = %b", A, B, diff, borrow_out);
// Test case 2: A = 7, B = 15
A = 4'b0111;
B = 4'b1111;
#10; // Wait for 10 time units
// Display simulation results
$display("A = %b, B = %b, diff = %b, borrow_out = %b", A, B, diff, borrow_out);
end
endmodule
Output:
Program:
// design.sv
module PartialProductMultiplication(
input [3:0] A,
input [3:0] B,
output reg [7:0] Product
);
reg [7:0] partial_products[3:0];
integer i;
always @(A, B) begin
// Initialize the product and partial products to 0
Product = 8'b0;
for (i = 0; i < 4; i = i + 1)
partial_products[i] = 8'b0;
// Generate partial products
for (i = 0; i < 4; i = i + 1) begin
if (B[i])
partial_products[i] = A << i;
end
// Sum the partial products
for (i = 0; i < 4; i = i + 1)
Product = Product + partial_products[i];
end
endmodule
// testbench.sv
module testbench;
reg [3:0] A;
reg [3:0] B;
wire [7:0] Product;
// Instantiate the multiplication module
PartialProductMultiplication dut (A, B, Product);
initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
// Test case 1
A = 4'b1010; // 10 in decimal
B = 4'b1100; // 12 in decimal
#10;
// Test case 2
A = 4'b1001; // 9 in decimal
B = 4'b0101; // 5 in decimal
#10;
// Test case 3
A = 4'b0110; // 6 in decimal
B = 4'b0011; // 3 in decimal
#10;
end
always @* begin
$display("A: %b, B: %b, Product: %b", A, B, Product);
end
endmodule
Output:
# KERNEL: A: 1010, B: 1100, Product: xxxxxxxx
# KERNEL: A: 1010, B: 1100, Product: 01111000
# KERNEL: A: 1001, B: 0101, Product: 01111000
# KERNEL: A: 1001, B: 0101, Product: 00101101
# KERNEL: A: 0110, B: 0011, Product: 00101101
# KERNEL: A: 0110, B: 0011, Product: 00010010