Q-Write RTL Description and Test Bench For Full Adder Circuit Using Half Adder and OR Gate
Q-Write RTL Description and Test Bench For Full Adder Circuit Using Half Adder and OR Gate
Q-Write RTL Description and Test Bench For Full Adder Circuit Using Half Adder and OR Gate
Q- Write RTL description and test bench for full adder circuit using half adder and OR gate.
Source code-
Full adder:
module full_adder_tb();
//Step1 : Instantiate the full adder with order based port mapping
full_adder uut (a,b,c,sum,carry);
//Process to initialize the variables at 0ns
initial
begin
a = 1'b0;
b = 1'b0;
c = 1'b0;
end
Half adder:
assign sum = a ^ b;
assign carry = a & b;
endmodule
Test bench:
module full_adder_tb();
//Step1 : Instantiate the full adder with order based port mapping
full_adder uut (a,b,c,sum,carry);
//Process to initialize the variables at 0ns
initial
begin
a = 1'b0;
b = 1'b0;
c = 1'b0;
end
Output
Waveform:
EX-1 Write RTL description & test bench for the 4-bit ripple carry adder using 1-bit full adder
Source code-
module ripple4(a,b,cin,s,carry);
input [3:0]a,b;
input cin;
output [3:0]s;
output carry;
wire w1,w2,w3;
full_adder fa1(.a(a[0]), .b(b[0]), .c(cin), .sum(s[0]), .carry(w1));
full_adder fa2(.a(a[1]), .b(b[1]), .c(w1), .sum(s[1]), .carry(w2));
full_adder fa3(.a(a[2]), .b(b[2]), .c(w2), .sum(s[2]), .carry(w3));
full_adder fa4(.a(a[3]), .b(b[3]), .c(w3), .sum(s[3]), .carry(carry));
endmodule
Test bench-
module full_adder_tb();
reg [3:0]a,b;
reg c;
wire [3:0]sum;
wire carry;
integer i;
ripple4 uut (a,b,cin,s,carry);
initial
begin
a=4'b0;
b=4'b0;
c=1'b1;
end
initial
begin
for(i=0;i<512;i=i+13)
begin
{a,b,c}=i;
#5;
end
end
initial
$monitor( "$time ,"input a[%d]=%b ,b[%d]=%b ,c[%d]=%b , sum[%d]=%b,
carry[%d]=%b",i,a,i,b,i,c,i,sum,i, carry);
initial #200
$finish;
endmodule
Output :
Source Code-
4:1 MUX-
2:1 MUX-
Test bench-
module mux4_1_tb();
reg a,b,c,d,s0,s1;
wire out;
integer i;
mux4_1 DUT(a,b,c,d,s0,s1,out);
initial begin
a=1'b1;
b=1'b0;
c=1'b1;
d=1'b0;
s0=1'b0;
s1=1'b0;
end
initial begin
for(i=0;i<4;i=i+1)
begin
{s0,s1}=i;
#10;
end
end
initial #100
$finish;
endmodule
WAVEFORM
Output :
# 0a=1,b=0,c=1,d=0 s0=0,s1=0,out=1
# 10a=1,b=0,c=1,d=0 s0=0,s1=1,out=0
# 20a=1,b=0,c=1,d=0 s0=1,s1=0,out=1
# 30a=1,b=0,c=1,d=0 s0=1,s1=1,out=0
# Time: 100 ps Iteration: 0 Instance: /mux4_1_tb
A1- Write RTL for 2x4 decoder using dataflow abstraction
Source code-
module decoder2_4(a,b,e,y);
input a,b,e;
output [3:0]y;
assign y[0]=(~a)*(~b)*e;
assign y[1]=(~a)*(b)*e;
assign y[2]=(a)*(~b)*e;
assign y[3]=(a)*(b)*e;
endmodule
Test bench-
module decoder2_4_tb();
reg a,b,en;
wire [3:0]y;
integer i;
decoder2_4 UUT(a,b,en,y);
initial begin
a=1'b0;
b=1'b0;
en=1'b1;
end
initial begin
for(i=0;i<4;i=i+1)
begin
en=1'b1;
{a,b}=i;
#10;
en=1'b0;
a=1'b1;
b=1'b0;
#10
end end
initial #100
$finish;
Endmodule
Waveform
Output
# 0 a=0.b=0,en=1,y[0]=0001
# 10 a=0.b=1,en=1,y[ 1]=0010
# 20 a=1.b=0,en=1,y[ 2]=0100
# 30 a=1.b=1,en=1,y[ 3]=1000
# 40 a=1.b=1,en=1,y[ 4]=1000
A2- Write RTL for 8x3 priority encoder using structural model
Source code-
module priority(y,i,idle);
input [7:0]i;
output [2:0]y;
wire [7:0]h;
output idle;
assign h[7]=i[7];
assign h[6]=i[6]*(~i[7]);
assign h[5]=(i[5])*(~i[6])*(~i[7]);
assign h[4]=(i[4])*(~i[5])*(~i[6])*(~i[7]);
assign h[3]=(i[3])*(~i[4])*(~i[5])*(~i[6])*(~i[7]);
assign h[2]=(i[2])*(~i[3])*(~i[4])*(~i[5])*(~i[6])*(~i[7]);
assign h[1]=(i[1])*(~i[2])*(~i[3])*(~i[4])*(~i[5])*(~i[6])*(~i[7]);
assign h[0]=(i[0])*(~i[1])*(~i[2])*(~i[3])*(~i[4])*(~i[5])*(~i[6])*(~i[7]);
assign idle=(~i[0])*(~i[1])*(~i[2])*(~i[3])*(~i[4])*(~i[5])*(~i[6])*(~i[7]);
assign y[0]=i[1]+i[3]+i[5]+i[7];
assign y[1]=i[2]+i[3]+i[6]+i[7];
assign y[2]=i[4]+i[5]+i[+6]+i[7];
assign id=idle;
Endmodule
Test bench-
module prioritytb();
reg [7:0]i;
wire idle;
wire [2:0]y;
integer a;
task initialize;
begin
{i}=0;
end
endtask
initialize;
for (a=0;a<256;a=a+1)
begin
inputs(a);
end
end
initial
$monitor ($time ," data=%b,output=%b,idle=%b ",i,y,idle);
initial
begin
#250 $finish;
end
endmodule
Waveform
Output:
0 data=00000000,output=000,idle=1
# 10 data=00000001,output=000,idle=0
# 20 data=00000010,output=001,idle=0
# 30 data=00000011,output=001,idle=0
# 40 data=00000100,output=010,idle=0
# 50 data=00000101,output=010,idle=0
# 60 data=00000110,output=010,idle=0
# 70 data=00000111,output=010,idle=0
# 80 data=00001000,output=011,idle=0
# 90 data=00001001,output=011,idle=0
# 100 data=00001010,output=011,idle=0
# 110 data=00001011,output=011,idle=0
# 120 data=00001100,output=011,idle=0
# 130 data=00001101,output=011,idle=0
# 140 data=00001110,output=011,idle=0
# 150 data=00001111,output=011,idle=0
# 160 data=00010000,output=100,idle=0
# 170 data=00010001,output=100,idle=0
# 180 data=00010010,output=100,idle=0
# 190 data=00010011,output=100,idle=0
# 200 data=00010100,output=100,idle=0
# 210 data=00010101,output=100,idle=0
# 220 data=00010110,output=100,idle=0
# 230 data=00010111,output=100,idle=0
# 240 data=00011000,output=100,idle=0
A3- Write RTL for 4X1 MUX using decoder & tri-state buffer and verify the same using a test bench
Source code-
4X1 MUX-
decoder2_4 d1(s0,s1,en,w);
bufif1 b1(y, a, w[0]);
bufif1 b2(y, b, w[1]);
bufif1 b3(y, c, w[2]);
bufif1 b4(y, d, w[3]);
Endmodule
Decoder-
module decoder2_4(a,b,e,y);
input a,b,e;
output [3:0]y;
assign y[0]=(~a)*(~b)*e;
assign y[1]=(~a)*(b)*e;
assign y[2]=(a)*(~b)*e;
assign y[3]=(a)*(b)*e;
Endmodule
Test bench-
module mux4_dec_tb();
reg a,b,c,d,s0,s1,e;
wire out;
integer i;
mux4_dec DUT(a,b,c,d,s0,s1,e,y);
initial begin
a=1'b1;
b=1'b0;
c=1'b1;
d=1'b0;
s0=1'b0;
s1=1'b0;
e=1'b0;
end
initial begin
for(i=0;i<8;i=i+1)
begin
{e,s0,s1}=i;
#10;
end
end
initial #100
$finish;
endmodule
Waveform:
Output:
# 0a=1,b=0.c=1,d=0,s0=0,s1=0,e=0,out=z
# 10a=1,b=0.c=1,d=0,s0=0,s1=1,e=0,out=z
# 20a=1,b=0.c=1,d=0,s0=1,s1=0,e=0,out=z
# 30a=1,b=0.c=1,d=0,s0=1,s1=1,e=0,out=z
# 40a=1,b=0.c=1,d=0,s0=0,s1=0,e=1,out=1
# 50a=1,b=0.c=1,d=0,s0=0,s1=1,e=1,out=0
# 60a=1,b=0.c=1,d=0,s0=1,s1=0,e=1,out=1
# 70a=1,b=0.c=1,d=0,s0=1,s1=1,e=1,out=0
A4- Write RTL to design bidirectional buffer and verify the same using test bench
Source code-
module bi_buffer(a,b,c);
inout a,b;
input c;
bufif1 (b,a,c);
bufif0 (a,b,c);
endmodule
Test bench-
module bi_buffer_tb();
wire a,b;
reg c;
integer i,d;
bi_buffer uut(a,b,c);
initial begin
c=1'bx;
#10;
end
initial begin
for(i=0;i<8;i=i+1)
begin
{d,c}=i;
#10;
end
end
initial
$monitor( $time,"a=%b,b=%b,c=%b",a,b,c);
Endmodule
Waveform:
Output:
# 0a=x,b=x,c=0
# 10a=0,b=0,c=1
# 20a=x,b=x,c=0
# 30a=1,b=1,c=1
# 40a=x,b=x,c=0
# 50a=0,b=0,c=1
# 60a=x,b=x,c=0
# 70a=1,b=1,c=1
LAB 2-
Write RTL description and test bench for an arithmetic logic unit using arithmetic and logical
operations.
Source code-
module operators();
reg [3:0]a,b,c,d;
reg [3:0]e,f,g,h;
reg signed x,y,z;
initial begin
a=5;
b=9;
c=4'b1010;
d=3'bx;
x=a&&b;
y=!a;
z=a||b;
$display ($time ,"x=%b,y=%b,z=%b",x,y,z);
#10
e=a&b;
f=a^b;
g=a|b;
$display ($time ,"e=%b,f=%b,g=%b",e,f,g);
#10;
e=^a;
f=~&(a);
g=a|b;
Test bench-
module alu_tb();
reg [7:0]a,b;
reg [3:0]command;
reg enable;
wire [15:0]out;
integer m,n,o;
reg [4*8:0]string_cmd;
alu DUT (a,b,command,enable,out);
task initialize;
begin
{a,b,command,enable} = 0;
end
endtask
task delay();
begin
#10;
end
endtask
always@(command)
begin
case(command)
ADD : string_cmd = "ADD";
INC : string_cmd = "INC";
SUB : string_cmd = "SUB";
DEC : string_cmd = "DEC";
MUL : string_cmd = "MUL";
DIV : string_cmd = "DIV";
SHL : string_cmd = "SHL";
SHR : string_cmd = "SHR";
INV : string_cmd = "INV";
AND : string_cmd = "AND";
OR : string_cmd = "OR";
NAND : string_cmd = "NAND";
NOR : string_cmd = "NOR";
XOR : string_cmd = "XOR";
XNOR : string_cmd = "XNOR";
BUF : string_cmd = "BUF";
endcase
end
initial
begin
initialize;
en_oe(1'b1);
for (m=0;m<256;m=m+17)
begin
for (n=0;n<256;n=n+19)
begin
inputs(m,n);
for (o=0;o<8;o=o+1)
begin
command=o;
delay;
end
end
end
en_oe(0);
inputs(8'd20,8'd10);
cmd(ADD);
#10;
en_oe(1);
inputs(8'd25,8'd17);
cmd(ADD);
delay;
$finish;
end
initial
$monitor("Input oe=%b, a=%b, b=%b, command=%s, Output out=%b",enable,a,b,string_cmd,out);
Endmodule
Waveform:
Output:
Source code :
module operators();
reg [3:0]a,b,c,d;
reg [3:0]e,f,g,h;
reg signed x,y,z;
integer j;
initial begin
a=5;
b=4'b1011;
c=4'b1010;
d=3'bx;
x=a&&b;
y=!a;
z=a||b;
$display ($time ,"x=%b,y=%b,z=%b",x,y,z);
#10
e=a&b;
f=a^b;
g=a|b;
$display ($time ,"e=%b,f=%b,g=%b",e,f,g);
#10;
e=^a;
f=~&(a);
g=a|b;
x=(a>=b);
y=(c<=a);
$display ($time ,"x=%b,y=%b",x,y);
#10
x=(a==b);
y=(a!=b);
end
Endmodule
Output
0x=1,y=0,z=1
# 10e=0001,f=1110,g=1111
# 20e=0000,f=0001,g=1111
# initial valuesof a=0101,b=1011,c=1010,d=0xxx
# 30a=1010,b=0010,c=0101,
# 40x=1,y=1
LAB 3-
E1- Write RTL (Behavioral) description and test bench for a 4:1 multiplexer circuit.
Source code-
module mux4_1(data,sel,out);
input [3:0]data;
input [1:0]sel;
output reg out;
always@(data,sel)
begin
case(sel)
2'b00 : out=data[0];
2'b01 : out=data[1];
2'b10 : out=data[2];
2'b11 : out=data[3];
default :out=0;
endcase
end
endmodule
Test bench-
module mux4_1_tb();
reg [3:0]d;
reg [1:0]sel;
wire y;
mux4_1 DUT(d,sel,y);
task initialize;
begin
{d,sel} = 0;
end
endtask
initial
begin
initialize;
stimulus(4'd5,2'd2);
stimulus(4'd15,2'd3);
stimulus(4'd6,2'd1);
end
initial
begin
$monitor("Values of data=%b,sel=%b,Output=%b",d,sel,y);
end
initial
begin
100 $finish;
end
endmodule
Output:
# Values of data=0000,sel=00,Output=0
# Values of data=0101,sel=10,Output=1
# Values of data=1111,sel=11,Output=1
# Values of data=0110,sel=01,Output=1
# ** Note: $finish
# Time: 100 ps Iteration: 0 Instance: /mux4_1_tb
E2- Write RTL description and test bench for 3:8 decoder
Source code-
module decoder3_8(data,en,out);
input [2:0]data;
input en;
output reg [7:0]out;
always@(data)
begin
if(en)
begin
case(data)
3'b000 : out=8'b00000001;
3'b001 : out=8'b00000010;
3'b010 : out=8'b00000100;
3'b011 : out=8'b00001000;
3'b100 : out=8'b00010000;
3'b101 : out=8'b00100000;
3'b110 : out=8'b01000000;
3'b111 : out=8'b10000000;
default : out=8'b00000000;
endcase
end
else
out=8'bzzzzzzzz;
end
endmodule
Test bench-
module decoder3_8_tb();
reg [2:0]data;
reg en;
wire [7:0]out;
integer a;
decoder3_8 uut(data,en,out);
task initialize ;
begin
{en,data}=0;
end
endtask
data=i;
end
endtask
initial
begin
#10;
en=1'b1;
end
initial
begin
initialize;
inputs(3'b000);
inputs(3'b001);
inputs(3'b010);
inputs(3'b011);
inputs(3'b100);
inputs(3'b101);
inputs(3'b110);
inputs(3'b111);
inputs(3'b000);
end
initial
$monitor ($time ," data =%b,en=%b,output=%b",data,en,out);
Endmodule
Output :
# 0 data =000,en=0,output=zzzzzzzz
# 10 data =000,en=1,output=zzzzzzzz
# 20 data =001,en=1,output=00000010
# 30 data =010,en=1,output=00000100
# 40 data =011,en=1,output=00001000
# 50 data =100,en=1,output=00010000
# 60 data =101,en=1,output=00100000
# 70 data =110,en=1,output=01000000
# 80 data =111,en=1,output=10000000
# 90 data =000,en=1,output=00000001
E3- Write RTL description and test bench for 8:3 priority encoder
Source code-
always@(data,en)
begin
if (en==1'b1)
casex(data)
8'b00000000 :out=3'bxxx;
8'b00000001 :out=3'b000;
8'b0000001x :out=3'b001;
8'b000001xx :out=3'b010;
8'b00001xxx :out=3'b011;
8'b0001xxxx :out=3'b100;
8'b001xxxxx :out=3'b101;
8'b01xxxxxx :out=3'b110;
8'b1xxxxxxx :out=3'b111;
default :out=3'b000;
endcase
end
endmodule
Test bench-
module priority8_3_tb();
reg [7:0]data;
reg en;
wire [2:0]out;
integer a;
task initialize;
begin
{data,en}=0;
end
endtask
initial
begin
initialize;
enable(1'b1);
for (a=0;a<256;a=a+1)
begin
inputs(a);
end
end
initial
$monitor ($time ," data=%b, en=%b,output=%b",data,en,out);
initial
begin
#250 $finish;
end
endmodule
Output:
0 data=00000000, en=1,output=xxx
# 10 data=00000001, en=1,output=000
# 20 data=00000010, en=1,output=001
# 30 data=00000011, en=1,output=001
# 40 data=00000100, en=1,output=010
# 50 data=00000101, en=1,output=010
# 60 data=00000110, en=1,output=010
# 70 data=00000111, en=1,output=010
# 80 data=00001000, en=1,output=011
# 90 data=00001001, en=1,output=011
# 100 data=00001010, en=1,output=011
# 110 data=00001011, en=1,output=011
# 120 data=00001100, en=1,output=011
# 130 data=00001101, en=1,output=011
# 140 data=00001110, en=1,output=011
# 150 data=00001111, en=1,output=011
# 160 data=00010000, en=1,output=100
# 170 data=00010001, en=1,output=100
# 180 data=00010010, en=1,output=100
# 190 data=00010011, en=1,output=100
# 200 data=00010100, en=1,output=100
# 210 data=00010101, en=1,output=100
# 220 data=00010110, en=1,output=100
# 230 data=00010111, en=1,output=100
# 240 data=00011000, en=1,output=100