Addc Lab
Addc Lab
Addc Lab
module bcd_tb();
reg clk,rst;
wire [3:0]count;
bcd dut(clk,rst,count);
initial
clk=1'b0;
always
#10 clk=~clk;
initial
begin
rst=1'b1;
#10 rst=1'b0;
#200 $finish;
end
endmodule
cla
module cla_tb();
reg [3:0]a,b;
reg cin;
wire [3:0]sum;
wire cout;
cla dut(a,b,cin,sum,cout);
initial
begin
a=4'b0010;b=4'b0001;cin=1'b0;
#10 a=4'b1010;b=4'b0101;cin=1'b0;
#10 a=4'b1010;b=4'b1111;cin=1'b0;
#10 a=4'b1111;b=4'b1111;cin=1'b0;
end
endmodule
counter
module counter_tb();
reg rst,clk;
wire [3:0]count;
counterdown dut(rst,clk,count);
initial
clk=1'b1;
always
#5 clk=~clk;
initial
begin
rst=1'b1;
#10 rst=1'b0;
#200 $finish;
end
endmodule
decoder 24
module decoder24_tb();
reg [1:0]i;
wire [3:0]y ;
decoder24 DUT(i,y);
initial
begin
#10 i=2'b00;
#10 i=2'b01;
#10 i=2'b10;
#10 i=2'b11;
end
endmodule
demux 14
module demux14_tb();
reg [0:0]i ;
reg [1:0]s;
wire [3:0]y;
demux14 DUT(i,s,y);
initial
begin
i=1'b1;
#10 s=2'b00;
#10 s=2'b01;
#10 s=2'b10;
#10 s=2'b11;
end
endmodule
d flip flop
module dflipflop_tb();
reg clk,rst,din;
wire q;
dflipflop dut(clk,rst,din,q);
initial
begin
clk=1'b0;
end
always
#5
clk=~clk;
initial
begin
rst=1'b1;
din=1'b0;
#10 rst=1'b0;
din=1'b1;
end
encoder 83
module encoder83_tb();
reg en;
reg [7:0]d;
wire [2:0]b;
encoder83 DUT(en,d,b);
initial
begin
en=1'b0;
#10 d=8'b00000001;
#10 d=8'b00000010;
#10 d=8'b00000100;
#10 d=8'b00001000;
#10 d=8'b00010000;
#10 d=8'b00100000;
#10 d=8'b01000000;
#10 d=8'b10000000;
end
endmodule
encoder 83 pri
module encoder83priority_tb();
reg en;
reg [7:0]d;
wire [2:0]b;
encoder83priority DUT(en,d,b);
initial
begin
en=1'b0;
#10 d=8'b00000001;
#10 d=8'b00000011;
#10 d=8'b00000100;
#10 d=8'b00001010;
#10 d=8'b00010110;
#10 d=8'b00110100;
#10 d=8'b01010110;
#10 d=8'b10101010;
end
endmodule
fuladder
module fulladder_tb();
reg a,b,cin;
wire sum,carry;
fulladder dut(a,b,cin,sum,carry);
initial
begin
a=1'b0;b=1'b0;cin=1'b0;
#10 a=1'b0;b=1'b0;cin=1'b1;
#10 a=1'b0;b=1'b1;cin=1'b0;
end
endmodule
half adder
module halfadder_tb1();
reg a,b;
wire sum,carry;
halfadder1 dut(sum,carry,a,b);
initial
begin
a=1'b1;b=1'b0;
#10 a=1'b0;b=1'b0;
#10 a=1'b1;b=1'b1;
end
endmodule
jk
module jkflipflop_tb();
reg clk,reset,preset;
reg[1:0]jk;
wire q;
jkflipflop dut(clk,reset,preset,jk,q);
initial
clk=1'b0;
always
#10 clk=~clk;
initial
begin
reset=1'b1;
#10 reset=1'b0;
#10 preset=1'b1;
#10 preset=1'b0;
#10 jk=2'b00;
#20 jk=2'b01;
#20 jk=2'b10;
#20 jk=2'b11;
end
endmodule
jhonson
module johnsoncounter_tb();
reg clk,rst;
wire [3:0]out;
johnsoncounter dut (.out(out),.rst(rst), .clk(clk));
always
#5clk=~clk;
initial begin
rst=1'b1;
clk=1'b0;
#20 rst=1'b0;
end
initial
begin
$monitor ($time,"clk+%b,out=%b,rst=%b",clk,out,rst);
#105 $stop;
end
endmodule
mux 81
module mux81_tb();
reg [7:0]i;
reg [2:0]s;
wire y;
mux81 DUT(i,s,y);
initial
begin
#10 i=8'b00000001;
#10 s=3'b100;
#10 i=8'b00101110;
#10 s=3'b111;
end
endmodule
rca
module rca_tb();
wire [3:0]sum;
wire cout;
reg [3:0]a,b;
reg cin ;
rca dut(sum,cout,a,b,cin);
initial
begin
a=4'b0010;b=4'b0001;cin=1'b0;
#10 a=4'b1010;b=4'b0101;cin=1'b0;
#10 a=4'b1010;b=4'b1111;cin=1'b0;
#10 a=4'b1111;b=4'b1111;cin=1'b0;
end
endmodule
tflipflop
module tflipflop_tb();
reg clk,reset,preset,t;
wire q;
tflipflop dut(clk,reset,preset,t,q);
initial
clk=1'b0;
always
#10 clk=~clk;
initial
begin
reset=1'b1;
#10 reset=1'b0;
#10 preset=1'b1;
#10 preset=1'b0;
#20 t=1'b0;
#20 t=1'b1;
end
endmodule
universal shifter
module universalshiftregister_tb();
reg [3:0]d;
reg sdl,sdr,clk,clr;
reg[1:0]s;
wire [3:0]q;
universalshiftregister dut(clk,clr,d,sdl,sdr,s,q);
initial
begin
clk=1'b0;
end
always
#10 clk=~clk;
initial begin
clr=1;
sdl=1'b1;
sdr=1'b1;
s=2'b11;
d=4'b1010;
#20 s=2'b01;
#20 s=2'b10;
#20 s=2'b00;
end
endmodule
counter
module counter(
input rst,
input clk,
output reg [3:0]count
);
always@(posedge clk)
if(rst)
count<=4'b0000;
else
count<=count+4'b0001;
endmodule
VERILOG CODE
module fulladder(A,B,cin,Sum,Carry);
input A;
input B;
input cin;
output Sum;
output Carry;
assign Sum = A^B^cin;
assign Carry = (A&B)|(A&cin)|(B&cin); endmodule
For Half Adder
module half_adder( output Sum,Carry, input A,B );
xor(Sum,A,B);
and(Carry,A,B);
endmodule
For 4-bit Ripple Carry Adder module ripple_adder_4bit(output [3:0] Sum, output Cout, input
[3:0] A,B, input Cin ); wire c1,c2,c3;
full_adder FA1(Sum[0],c1,A[0],B[0],Cin), FA2(Sum[1],c2,A[1],B[1],c1),
FA3(Sum[2],c3,A[2],B[2],c2), FA4(Sum[3],Cout,A[3],B[3],c3); endmodule
module carry_look_adder (input [3:0] a, b, input cin, output [3:0] sum, output cout ); wire
g0,p0,g1,p1,g2,p2,g3,p3;
wire c1,c2,c3;
assign g0 = a[0] & b[0];
assign p0 = a[0] ^ b[0];
assign sum[0] = a[0] ^ b[0] ^ cin;
assign c1 = g0 | (p0 & cin);
assign g1 = a[1] & b[1];
assign p1 = a[1] ^ b[1];
assign sum[1] = a[1] ^ b[1] ^ c1;
assign c2 = g1 | (p1 & c1);
assign g2 = a[2] & b[2];
assign p2 = a[2] ^ b[2];
assign sum[2] = a[2] ^ b[2] ^ c2;
assign c3 = g2 | (p2 & c2);
assign g3 = a[3] & b[3];
assign p3 = a[3] ^ b[3];
assign sum[3] = a[3] ^ b[3] ^ c3;
assign cout = g3 | (p3 & c3);
endmodule
module jkflop( input clk, input reset, input [1:0] jk, output reg Q );
always @ (negedge clk)
if(reset) Q<=1'b0;
else if (preset) Q<=1'b1;
else
case (jk)
2'b00: Q<=Q;
2'b01: Q<=1'b0;
2'b10: Q<=1'b1;
2'b11: Q<=~Q;
default: Q<=1'bZ;
endcase
endmodule