Solution
************************Design under test(DUT)******************************
module rca4(
//interfacing ports are define
input [3:0] a,
input [3:0] b,
input cin,
output reg [3:0] s,
output reg cout);
// temporary variables are defined here
reg [4:0] t ;
integer i;
//Body of DUT
always@(a,b,cin)
begin
t[0]= cin; //carry stored to reg t[0]
for( i=0;i<4;i=i+1)
begin
full_adder (a[i],b[i],t[i],s[i],t[i+1]); // full adder task instance
end
cout = t[4]; // cout stored to t[4]
end
// define full adder in task
task full_adder;
//interfacing ports of the task
input a,b,c;
output s,co;
// task body
begin
s = a^b^c ;
co = a&b | b&c | a&c;
end
endtask
endmodule
//***************Test bench****************************8
module tb();
// test bench interface through reg and wire
reg [3:0] a;
reg [3:0] b;
reg cin;
wire [3:0] s;
wire cout;
rca4 i(a,b,cin,s,cout); //ripple carry adder design instance
initial begin // remove for the other simulatores
$dumpfile("ripple.vcd"); // remove for the other simulatores
$dumpvars(-1,i); // remove for the other simulatores
end // remove for the other simulatores
initial begin
a = 4'b0000; b=4'b0000;cin=1'b0;// initial value
end
always begin
//assign random values to test the corner cases
#10;
a=$random;b=$random;cin=$random;
#10;
a=$random;b=$random;cin=$random;
#10;
a=$random;b=$random;cin=$random;
#10;
a=$random;b=$random;cin=$random;
#10;
a=$random;b=$random;cin=$random;
#10;
a=$random;b=$random;cin=$random;
#10;
$finish;
end
endmodule
Execution steps
iverilog rca4.v
iverilog -o rca4 rca4.v
iverilog -o rca4.vvp -tvvp rca4.v
vvp rca4
gtkwave ripple.vcd
waveform:
No comments:
Post a Comment