verilog code and Test bench for
Basic Gate
half adder
full adder
half subtractor
full subtractor
mux
demux
encoder
decoder
BCD to E-3
E-3 to BCD
binary to BCD
BCD to binary
Ripple carry adder
2. Aim:
To design, simulate and implement basic gates circuit using schematic and
Verilog HDL.
Software Details:
For design Functional Simulation: Quartus II, ModelSim
For design Synthesis: Quartus II
For design Implementation: Quartus II
1. And gate
a) Verilog code
module and_gate(a,b,y);
input a,b;
output y;
assign y = a & b;
endmodule
Verilog Code (Gate Level):
module andgate(a,b,y);
input a,b;
output y;
wire y;
and(y,a,b);
endmodule
b) Output waveform:
Task: 1 Design and Implementation of Basic gates using Verilog HDL
coding
3. 2. OR gate
a) Verilog code:
module OR_gate(a,b,y);
input a,b;
output y;
assign y = a | b;
endmodule
Verilog Code(Gate Level):
module orgate(a,b,y);
input a,b;
output y;
wire y;
or(y,a,b);
endmodule
4. b) Output waveform:
3. XOR gate
a) Verilog code:
module XOR_gate(a,b,y);
input a,b;
output y;
assign y = a ^ b;
endmodule
Verilog code:
module XOR_gate(a,b,y);
input a,b;
output y;
xor (y , a , b);
endmodule
6. or o1( y ,~a , ~b);
endmodule
a) Output waveform:
5. NOR gate
a) Verilog code:
module nor_gate(a,b,y);
input a,b;
output y;
assign y = ~a & ~b;
endmodule
Verilog code:
module nor_gate(a,b,y);
7. input a,b;
output y;
and (y , ~a , ~b);
endmodule
a) Output waveform:
6. EXNOR gate
a) Verilog code:
module exnor_gate(a,b,y);
input a,b;
output y;
assign y = a ~^ b;
endmodule
Verilog code:
module exnor_gate(a,b,y);
input a,b;
8. output y;
xnor (y , a , b);
endmodule
a) Output waveform:
Common test bench:
module tb_and_gate;
reg A,B;
wire Y;
and_gate a1 (A,B,Y);
initial begin
A =0;
B= 0;
end
always #25 A =~A;
always #50 B =~B;
endmodule
9. Aim:
To design, simulate and implement combinational circuits using schematic and
Verilog HDL.
Software Details:
For design Functional Simulation: Quartus II, ModelSim
For design Synthesis: Quartus II
For design Implementation: Quartus II
1. Half subtractor
a) Verilog code:
module hsubb(a,b,d,bo);
input a,b;
output d,bo;
wire d,bo;
assign d = a ^ b;
assign bo = ~a & b;
endmodule
Verilog Code(Gate Level):
module half_sub(a,b,d,b0);
input a,b;
output d,b0;
wire d,b0;
xor (d,a,b);
and(b0,~a,b);
endmodule
Task: 2 Design and Implementation of combinational circuit using
Verilog HDL coding
10. b) Test bench
module hsubb_tb();
reg a1,b1;
wire d1,bo1;
hsubb h1(a1,b1,d1,bo1);
initial
begin
a1=0;
b1=0;
end
always #50 a1=~a1;
always #100 b1=~b1;
endmodule
c) Wave forms:
11. 2. Half adder
a) Verilog coding:
module hadd(a,b,s,co);
input a,b;
output s,co;
wire s,co;
assign s = a ^ b;
assign co = a & b;
endmodule
Verilog Code(Gate Level):
module hadd(a,b,s,c0);
input a,b;
output s,c0;
wire s,c0;
xor(s,a,b);
and(c0,a,b);
endmodule
b) Test-Bench
module hadd_tb();
reg a1,b1;
wire s1,co1;
hadd h1(a1,b1,s1,co1);
initial
19. 7. 2-bit Comparator
a) Verilog coding
module comparator(a,b,l,e,g);
input a,b;
output l,e,g;
wire l,e,g;
and a1(l, ~a, b);
xnor x1(e, a, b);
and a2(g, a, ~b);
endmodule
b) Test-Bench
20. module comparator_tb();
reg a1,b1;
wire l1,e1,g1;
comparator c1(a1,b1,l1,e1,g1);
initial
begin
a1=0;
b1=0;
end
always #100 a1=~a1;
always #50 b1=~b1;
endmodule
c) Output waveform:
38. c) Waveform:
3. Binary To Gray
a) Verilog code:
module B_TO_G(b,g);
output [3:0]g;
input [3:0]b;
xor x1(g[0],b[0],b[1]);
xor x2(g[1],b[1],b[2]);
xor x3(g[2],b[2],b[3]);
and a1(g[3],b[3],1'b1);
endmodule
b) Test bench:
47. Aim:
To design, simulate and implement Ripple carry adder using schematic and
Verilog HDL.
Software Details:
For design Functional Simulation: Quartus II, ModelSim
For design Synthesis: Quartus II
For design Implementation: Quartus II
a) RC_Verilog Code:
module RC_adder(a2,b2,cin2,s2,cout2);
input [3:0]a2,b2;
wire c1,c2,c3;
input cin2;
output [3:0]s2;
output cout2;
FA f1(a2[0],b2[0],cin2,s2[0],c1);
FA f2(a2[1],b2[1],c1,s2[1],c2);
FA f3(a2[2],b2[2],c2,s2[2],c3);
FA f4(a2[3],b2[3],c3,s2[3],cout2);
Endmodule
b) FA_Verilog code:
Task: 4 Design and Implementation of Combinational Circuits using
Verilog HDL coding
48. module FA(a,b,cin,s,cout);
input a,b,cin;
output s,cout;
wire s,cout;
wire w1,w2,w3,w4;
xor x1(w1,a,b);
xor x2(s,w1,cin);
and a1(w2,a,b);
and a2(w3,cin,b);
and a3(w4,a,cin);
or o1(cout,w2,w3,w4);
endmodule
c) Test bench:
module RC_FA_TB();
reg [3:0]a2,b2;
reg cin2;
wire [3:0]s2;
wire cout2;
RC_adder f11(a2,b2,cin2,s2,cout2);
initial
begin
cin2=0;
#10 a2=4'b1100; b2=4'b0001;
#10 a2=4'b1100; b2=4'b1001;
#10 $stop;
end
endmodule
d) Waveforms: