assignment 3
assignment 3
Design a 16-bit adder and verify using linear test bench, linear random test bench.
Design:
module adder_16bit(
input logic [15:0] a, b, // 16-bit inputs
output logic [15:0] sum, // 16-bit sum output
output logic carry_out // 1-bit carry output
);
// Perform addition with carry
assign {carry_out, sum} = a + b;
endmodule
Design:
module prng #(parameter WIDTH = 8) (
input logic clk, // Clock signal
input logic reset, // Reset signal
output logic [WIDTH-1:0] prng_out // Output random number
);
endmodule
test_bench:
module tb_prng;
// DUT Signals
logic clk, reset;
logic [7:0] prng_out;
// Clock generation
initial clk = 0;
always #5 clk = ~clk;
// Testbench stimulus
initial begin
reset = 1;
#10 reset = 0; // Apply reset and release it after 10ns
// End simulation
$stop;
end
endmodule
Results:
Ones Counter is a Counter which counts the number of one's coming in serial stream. The Minimum
value of the count is "0" and count starts by incriminating one till "15". After "15" the counter rolls
back to "0". Reset is also provided to reset the counter value to "0". Reset signal is active negedge.
Input is 1 bit port for which the serial stream enters. Out bit is 4 bit port from where the count
values can be taken. Reset and clock pins also provided.
Design:
module ones_counter (
input logic clk,
input logic reset_n,
input logic serial_in,
output logic [3:0] count
);
// Internal count logic for ones_counter
always_ff @(posedge clk or negedge reset_n) begin
if (!reset_n)
count <= 4'b0; // Reset the count to 0
else
count <= count + serial_in; // Increment count on serial input
end
endmodule
test_bench:
module tb_ones_counter;
logic clk;
logic reset_n;
logic serial_in;
logic [3:0] count;
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk; // 10ns clock period
end
initial begin
reset_n = 0; // Hold reset active
serial_in = 0;
#10; // Wait for 10ns
reset_n = 1; // Release reset
#10; // Wait for 10ns
test_bench:
module tb_fcfs_arbiter;
// Testbench signals
parameter NUM_REQ = 4;
logic clk, reset;
logic [NUM_REQ-1:0] req, grant;
// Clock generation
initial clk = 0;
always #5 clk = ~clk; // 10ns clock period
// Reset logic
initial begin
reset = 1;
#10 reset = 0;
end
// Sample coverage
always @(posedge clk) cov.sample();
endmodule
Results:
Verification of Ripple carry adder, Carry select Adder and carry save adder using coverage.
Design:
module ripple_carry_adder #(parameter WIDTH = 4) (
input logic [WIDTH-1:0] a, b,
input logic cin,
output logic [WIDTH-1:0] sum,
output logic cout
);
logic [WIDTH:0] carry;
assign carry[0] = cin;
generate
genvar i;
for (i = 0; i < WIDTH; i++) begin
assign {carry[i+1], sum[i]} = a[i] + b[i] + carry[i];
end
endgenerate
assign cout = carry[WIDTH];
endmodule
sum1 = a + b + 1;
cout1 = sum1[WIDTH];
end
test_bench:
`timescale 1ns / 1ps
module adder_tb;
parameter WIDTH = 4;
// Declare clock and reset signals
logic clk;
logic reset;
// Shared input signals
logic [WIDTH-1:0] a, b, c;
logic cin;
// RCA outputs
logic [WIDTH-1:0] rca_sum;
logic rca_cout;
// CSLA outputs
logic [WIDTH-1:0] csla_sum;
logic csla_cout;
// CSA outputs
logic [WIDTH-1:0] csa_sum;
logic csa_carry;
// Instantiate Ripple Carry Adder (RCA)
ripple_carry_adder #(WIDTH) rca (
.a(a), .b(b), .cin(cin), .sum(rca_sum), .cout(rca_cout)
);
// Instantiate Carry Select Adder (CSLA)
carry_select_adder #(WIDTH) csla (
.a(a), .b(b), .cin(cin), .sum(csla_sum), .cout(csla_cout)
);
// Instantiate Carry Save Adder (CSA)
carry_save_adder #(WIDTH) csa (
.a(a), .b(b), .c(c), .sum(csa_sum), .carry(csa_carry)
);
// Coverage Definitions
covergroup rca_coverage @(posedge clk);
coverpoint rca_sum {
bins sum_bins[] = {[0:15]}; // Cover all possible RCA sums
}
coverpoint rca_cout {
bins cout_bins[] = {0, 1}; // Cover RCA carry out
}
endgroup
covergroup csla_coverage @(posedge clk);
coverpoint csla_sum {
bins sum_bins[] = {[0:15]}; // Cover all possible CSLA sums
}
coverpoint csla_cout {
bins cout_bins[] = {0, 1}; // Cover CSLA carry out
}
endgroup
covergroup csa_coverage @(posedge clk);
coverpoint csa_sum {
bins sum_bins[] = {[0:15]}; // Cover all possible CSA sums
}
coverpoint csa_carry {
bins carry_bins[] = {0, 1}; // Cover CSA carry
}
endgroup
// Instantiate the coverage groups
rca_coverage rca_cov = new();
csla_coverage csla_cov = new();
csa_coverage csa_cov = new();
// Clock generation
initial begin
clk = 0;
end
always #5 clk = ~clk; // 10 ns period
// Test stimulus
initial begin
reset = 1; // Initialize reset signal
a = 0; b = 0; c = 0; cin = 0;
#10 reset = 0; // Deassert reset after 10ns
// Apply test cases
repeat (100) begin
a = $random;
b = $random;
c = $random;
cin = $random;
#10;
// Display outputs
$display("Inputs: a=%0d b=%0d c=%0d cin=%0d", a, b, c, cin);
$display("RCA: sum=%0d cout=%0d", rca_sum, rca_cout);
$display("CSLA: sum=%0d cout=%0d", csla_sum, csla_cout);
$display("CSA: sum=%0d carry=%0d", csa_sum, csa_carry);
end
$finish;
end
endmodule
Results: