Verilog HDL Lab Codes
Verilog HDL Lab Codes
Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Testbench:
Module gates_tf(a,b,m,n,o,p,q,r,s);
reg a,b; //Input ports
wire m,n,o,p,q,r,s; //Output ports
gates uut(.a(a),.b(b),.m(m),.n(n),.o(o),.p(p),.q(q),.r(r),.s(s)); //Instantiating the module gates
initial begin
a=0;b=0;#20; //stimulus
a=0;b=1;#20;
a=1;b=0;#20;
a=1;b=1;#20;
$stop;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Experiment 2
Verilog Code for Combinational Circuits
Half Adder:
Main Code:
module ha(
input a,b, //input ports
output s,ca); //output ports
assign s=a^b; //expression for sum
assign ca=a&b; //expression for carry
endmodule
Testbench:
module ha_tf(a,b,s,ca);
reg a,b; //Input ports
wire s,ca; //Output ports
ha uut(.a(a),.b(b),.s(s),.ca(ca)); //Instantiating the module ha
initial begin
a=0;b=0;#20; //Stimulus
a=0;b=1;#20;
a=1;b=0;#20;
a=1;b=1;#20;
$stop;
end
endmodule
Half Subtractor:
Main Code:
module hs(
input a,b,
output d,br);
assign d=a^b; //expression for difference
assign br=~a&b; //expression for borrow
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Testbench:
module hs_tf(a,b,d,br);
reg a,b;
wire d,br;
hs uut(.a(a),.b(b),.d(d),.br(br));
initial begin
a=0;b=0;#20;
a=0;b=1;#20;
a=1;b=0;#20;
a=1;b=1;#20;
$stop;
end
endmodule
2:1 Multiplexer:
Main Code:
module mux(
input a,b,sel,
output y);
assign y=((~sel)&a)|(sel&b); //expression for multiplexer output
endmodule
Testbench:
module mux_tf(a,b,sel,y);
reg a,b,sel;
wire y;
mux uut(.a(a),.b(b),.sel(sel),.y(y));
initial begin
sel=0;a=0;b=0;#20;
sel=0;a=0;b=1;#20;
sel=0;a=1;b=0;#20;
sel=0;a=1;b=1;#20;
sel=1;a=0;b=0;#20;
sel=1;a=0;b=1;#20;
sel=1;a=1;b=0;#20;
sel=1;a=1;b=1;#20;
$stop;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
1:2 Demultiplexer:
Main Code:
module demux(
input a,sel,
output y1,y2);
assign y1=(~sel)&a; //expression for demultiplexer output
assign y2=sel&a; //expression for demultiplexer output
endmodule
Testbench:
module demux_tf(a,sel,y1,y2);
reg a,sel;
wire y1,y2;
demux uut(.a(a),.sel(sel),.y1(y1),.y2(y2));
initial begin
sel=0;a=0;#20;
sel=0;a=1;#20;
sel=1;a=0;#20;
sel=1;a=1;#20;
end
endmodule
4:2 Encoder:
Main code:
module enc_4to2(
input i3,i2,i1,i0,
output f1,f0);
assign f1=((~i3)&(i2)&(~i1)&(~i0))|((i3)&(~i2)&(~i1)&(~i0)); //expression for encoder output
assign f0=((~i3)&(~i2)&(i1)&(~i0))|((i3)&(~i2)&(~i1)&(~i0));//expression for encoder output
endmodule
Testbench:
module enc_4to2_tf(i3,i2,i1,i0,f1,f0);
reg i3,i2,i1,i0;
wire f1,f0;
enc_4to2 uut(.i3(i3),.i2(i2),.i1(i1),.i0(i0),.f1(f1),.f0(f0));
initial begin
i3=0;i2=0;i1=0;i0=1;#20;
i3=0;i2=0;i1=1;i0=0;#20;
i3=0;i2=1;i1=0;i0=0;#20;
i3=1;i2=0;i1=0;i0=0;#20;
$stop;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
2:4 Decoder:
Main Code:
module dec_2to4(
input i1,i0,
output f3,f2,f1,f0);
assign f0=(~i1)&(~i0);
assign f1=(~i1)&(i0);
assign f2=(i1)&(~i0);
assign f3=(i1)&(i0);
endmodule
Testbench:
module dec_2to4_tf(i1,i0,f3,f2,f1,f0);
reg i1,i0;
wire f3,f2,f1,f0;
dec_2to4 uut(.i1(i1),.i0(i0),.f3(f3),.f2(f2),.f1(f1),.f0(f0));
initial begin
i1=1;i0=1;#20;
i1=1;i0=0;#20;
i1=0;i0=1;#20;
i1=0;i0=0;#20;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Experiment 3
Verilog Code for 1-bit Full Adder and Full Subtractor in all 3 modeling
styles
Testbench:
module fa_df_tf(a,b,c,sum,carry);
reg a,b,c;
wire sum,carry;
fa_df uut(.a(a),.b(b),.c(c),.sum(sum),.carry(carry));
initial begin
a=0;b=0;c=0;#20;
a=0;b=0;c=1;#20;
a=0;b=1;c=0;#20;
a=0;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
end
endmodule
Testbench:
module fa_bh_tf(a,b,c,sum,carry);
reg a,b,c;
wire sum,carry;
fa_bh uut(.a(a),.b(b),.c(c),.sum(sum),.carry(carry));
initial begin
a=0;b=0;c=0;#20;
a=0;b=0;c=1;#20;
a=0;b=1;c=0;#20;
a=0;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
end
endmodule
Testbench:
module fa_st_tf(a,b,c,sum,carry);
reg a,b,c;
wire sum,carry;
fa_st uut(.a(a),.b(b),.c(c),.sum(sum),.carry(carry));
initial begin
a=0;b=0;c=0;#20;
a=0;b=0;c=1;#20;
a=0;b=1;c=0;#20;
a=0;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Testbench:
module fs_df_tf(a,b,c,sum,carry);
reg a,b,c;
wire sum,carry;
fs_df uut(.a(a),.b(b),.c(c),.diff(diff),.br(br));
initial begin
a=0;b=0;c=0;#20;
a=0;b=0;c=1;#20;
a=0;b=1;c=0;#20;
a=0;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
end
endmodule
Testbench:
module fs_bh_tf(a,b,c,sum,carry);
reg a,b,c;
wire sum,carry;
fs_bh uut(.a(a),.b(b),.c(c),.diff(diff),.br(br));
initial begin
a=0;b=0;c=0;#20;
a=0;b=0;c=1;#20;
a=0;b=1;c=0;#20;
a=0;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Testbench:
module fs_df_tf(a,b,c,sum,carry);
reg a,b,c;
wire sum,carry;
fs_df uut(.a(a),.b(b),.c(c),.diff(diff),.br(br));
initial begin
a=0;b=0;c=0;#20;
a=0;b=0;c=1;#20;
a=0;b=1;c=0;#20;
a=0;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
a=1;b=0;c=0;#20;
a=1;b=1;c=1;#20;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Experiment 4
module fa_4b(
input [3:0] a,b,
input c,
output [3:0] sum,
output carry);
wire c1,c2,c3;
fa fa0(.sum(sum[0]),.carry(c1),.a(a[0]),.b(b[0]),.c(c));
fa fa1(.sum(sum[1]),.carry(c2),.a(a[1]),.b(b[1]),.c(c1));
fa fa2(.sum(sum[2]),.carry(c3),.a(a[2]),.b(b[2]),.c(c2));
fa fa3(.sum(sum[3]),.carry(carry),.a(a[3]),.b(b[3]),.c(c3));
endmodule
Testbench:
module fa_4b_tf(a,b,c,sum,carry);
reg [3:0] a;
reg [3:0] b;
reg c;
wire [3:0] sum;
wire carry;
fa_4b uut(.sum(sum),.carry(carry),.a(a),.b(b),.c(c));
initial begin
a=1100;b=1010;c=0;
#100;
a=1001;b=0111;c=1;
#100;
$stop;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
4-bit Subtractor:
module fs(
input a,b,c,
output diff,br);
assign diff=a^b^c;
assign br=((~a)&b)|(~(a^b)&c);
endmodule
module fs_4b(
input [3:0] a,b,
input c,
output [3:0] diff,
output br);
wire b1,b2,b3;
fs fs0(.diff(diff[0]),.br(b1),.a(a[0]),.b(b[0]),.c(c));
fs fs1(.diff(diff[1]),.br(b2),.a(a[1]),.b(b[1]),.c(b1));
fs fs2(.diff(diff[2]),.br(b3),.a(a[2]),.b(b[2]),.c(b2));
fs fs3(.diff(diff[3]),.br(br),.a(a[3]),.b(b[3]),.c(b3));
endmodule
Testbench:
module fs_4b_tf(a,b,c,diff,br);
reg [3:0] a;
reg [3:0] b;
reg c;
wire [3:0] diff;
wire br;
fs_4b uut(.diff(diff),.br(br),.a(a),.b(b),.c(c));
initial begin
a=1100;b=1010;c=0;
#100;
a=1001;b=0111;c=1;
#100;
$stop;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Experiment 5
Verilog Code for 8-bit, 16-bit and 32-bit ALU
8-bit ALU:
module alu_8b(
input [7:0] a,b,
input [2:0] opc,
output reg [7:0] f);
always @(*)
begin
case(opc)
3’b000: begin f=a+b; $display("Addition"); end
3’b001: begin f=a-b; $display("Subtraction"); end
3’b010: begin f=a|b; $display("OR"); end
3’b011: begin f=a&b; $display("AND"); end
3’b100: begin f=a^b; $display(“XOR"); end
3’b101: begin f=~(a|b); $display("NOR"); end
3’b110: begin f=~(a&b); $display("NAND"); end
3’b111: begin f=~(a^b); $display("XNOR"); end
default:f=8’d0;
endcase
end
endmodule
Testbench:
module alu_8b_tf(a,b,opc,f);
reg [7:0]a,b;
reg [2:0] opc;
reg [2:0] count;
wire [7:0] f;
alu_8b uut(.a(a),.b(b),.opc(opc),.f(f));
initial begin
a=8’h50;b=8’h30;
for(count=0;count<4;count=count+1)
begin
opc=count;
#20;
end
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
16-bit ALU:
module alu_4b(
input [15:0] a,b,
input [2:0] opc,
output reg [15:0] f);
always @(*)
begin
case(opc)
3’b000: begin f=a+b; $display("Addition"); end
3’b001: begin f=a-b; $display("Subtraction"); end
3’b010: begin f=a|b; $display("OR"); end
3’b011: begin f=a&b; $display("AND"); end
3’b100: begin f=a^b; $display(“XOR"); end
3’b101: begin f=~(a|b); $display("NOR"); end
3’b110: begin f=~(a&b); $display("NAND"); end
3’b111: begin f=~(a^b); $display("XNOR"); end
default:f=16’d0;
endcase
end
endmodule
Testbench:
module alu_16b_tf(a,b,opc,f);
reg [15:0]a,b;
reg [2:0] opc;
reg [2:0] count;
wire [15:0] f;
alu_16b uut(.a(a),.b(b),.opc(opc),.f(f));
initial begin
a=16’hAA;b=16’h55;
for(count=0;count<4;count=count+1)
begin
opc=count;
#20;
end
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
32-bit ALU:
module alu_32b(
input [31:0] a,b,
input [2:0] opc,
output reg [31:0] f);
always @(*)
begin
case(opc)
3’b000: begin f=a+b; $display("Addition"); end
3’b001: begin f=a-b; $display("Subtraction"); end
3’b010: begin f=a|b; $display("OR"); end
3’b011: begin f=a&b; $display("AND"); end
3’b100: begin f=a^b; $display(“XOR"); end
3’b101: begin f=~(a|b); $display("NOR"); end
3’b110: begin f=~(a&b); $display("NAND"); end
3’b111: begin f=~(a^b); $display("XNOR"); end
default:f=32’d0;
endcase
end
endmodule
Testbench:
module alu_32b_tf(a,b,opc,f);
reg [31:0] a,b;
reg [2:0] opc;
reg [2:0] count;
wire [31:0] f;
alu_32b uut(.a(a),.b(b),.opc(opc),.f(f));
initial begin
a=32’hEF;b=32’hBB;
for(count=0;count<4;count=count+1)
begin
opc=count;
#20;
end
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Experiment 6
Verilog Code for design of SR, JK, D, T and master slave JK flip flop
SR Flip flop:
Main Code:
module srflipflop(
input s,r,clk,rst,
output reg q,qb
);
reg [1:0]sr;
always@(posedge clk,posedge rst)
begin
sr={s,r};
if(rst==0)
begin
case (sr)
2'd1:q=1'b0;
2'd2:q=1'b1;
2'd3:q=1'b1;
default: begin end
endcase
end
else
begin
q=1'b0;
end
qb=~q;
end
endmodule
Testbench:
module srff_tf;
// Inputs
reg s;
reg r;
reg clk;
reg rst;
// Outputs
wire q;
wire qb;
);
initial begin
$monitor(clk,s,r,q,qb,rst);
s = 1'b0;
r = 1'b0;
rst= 1;
clk=1;
#10
rst=0;
s=1'b1;
r=1'b0;
#100
rst=0;
s=1'b0;
r=1'b1;
#100
rst=0;
s=1'b1;
r=1'b1;
#100
rst=0;
s=1'b0;
r=1'b0;
#100
rst=1;
s=1'b1;
r=1'b0;
end
always #25 clk= ~clk;
endmodule
JK Flip Flop:
Main Code:
module jkflipflop(
input clk,j,k,rst,
output reg q,
output reg qbar
);
reg [1:0] jk;
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Testbench:
module jk_tf;
// Inputs
reg clk;
reg j;
reg k;
reg rst;
// Outputs
wire q;
wire qbar;
always #5 clk=~clk;
// Instantiate the Unit Under Test (UUT)
jkflipflop uut (
.clk(clk),
.j(j),
.k(k),
.rst(rst),
.q(q),
.qbar(qbar)
);
initial begin
// Initialize Inputs
$monitor(clk,j,k,q,qbar,rst);
j=0;
k=0;
rst=1;
clk=1;
#10;
rst=0;
j=1;
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
k=0;
#100;
rst=0;
j=0;
k=1;
#100;
rst=0;
j=0;
k=0;
#100;
rst=1;
j=1;
k=0;
end
endmodule
D- Flip Flop:
Main Code:
module dff(
input d,rst,clk,
output reg q,qbar
);
Testbench:
module d_tf;
// Inputs
reg d;
reg rst;
reg clk;
// Outputs
wire q;
wire qbar;
dff uut (
.d(d),
.rst(rst),
.clk(clk),
.q(q),
.qbar(qbar)
);
initial begin
// Initialize Inputs
d = 0;
rst = 0;
clk = 0;
#100;
rst=0;
d=0;
#100;
rst=0;
d=1;
#100;
rst=1;
d=0;
#100;
rst=1;
d=1;
#100;
$stop;
end
endmodule
T – Flip Flop:
Main Code:
module tff(
input t,clk,rst,
output reg q,qbar
);
initial q=0;
always @(posedge clk)
begin
if (rst==0)
begin
q=0;
qbar=1;
end
else
begin
if(t==1)
begin
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
q=~q;
qbar=~qbar;
end
else
begin
q=q;
qbar=qbar;
end
end
end
endmodule
Testbench:
module t_tf;
// Inputs
reg t;
reg clk;
reg rst;
// Outputs
wire q;
wire qbar;
// Instantiate the Unit Under Test (UUT)
tff uut (
.t(t),
.clk(clk),
.rst(rst),
.q(q),
.qbar(qbar)
);
initial begin
// Initialize Inputs
t = 0;
clk = 0;
rst=0;
#100;
rst=0;
t=0;
#100;
rst=0;
t=1;
#100;
rst=1;
t=1;
#100;
$stop;
end
always #10 clk=~clk;
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
module sr_latch(
output q,qbar,
input g,s,r);
wire s1,r1;
and(s1,g,s);
and(r1,g,r);
nor(qbar,s1,q);
nor(q,r1,qbar);
endmodule
Testbench:
module msjk_tf;
// Inputs
reg clk;
reg j;
reg k;
reg resetn;
// Outputs
wire q;
wire qbar;
// Instantiate the Unit Under Test (UUT)
masterslave uut (
.clk(clk),
.j(j),
.k(k),
.resetn(resetn),
.q(q),
.qbar(qbar)
);
initial begin
// Initialize Inputs
clk = 1;
j = 0;
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
k = 0;
resetn = 1;
#20;
j=0;
k=0;
resetn=0;
#20;
j=0;
k=0;
resetn=1;
#100;
j=0;
k=1;
resetn=1;
#100;
j=1;
k=0;
resetn=1;
#100;
j=1;
k=1;
resetn=1;
end
always #25 clk=~clk;
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Experiment 7
Verilog Code for design of code converter
Testbench:
module b2g_tf;
reg [3:0] bin;
wire [3:0] gr;
reg [3:0] i;
b2g uut(.bin(bin),.gr(gr));
initial begin
bin=4’b0;
#20;
for(i=0;i<=d15;i=i+1)
begin
bin=i;
end
end
endmodule
Testbench:
module g2b_tf;
reg [3:0] gr;
wire [3:0] bin;
reg [3:0] i;
g2b uut(.bin(bin),.gr(gr));
initial begin
gr=4’b0;
#20;
for(i=0;i<=d15;i=i+1)
begin
gr=i;
end
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Experiment 8
Verilog Code for design of 4-bit binary and BCD Counter
Testbench:
module ud_tf;
reg clk;
reg rst;
reg up_down;
wire [3:0] count;
ud_counter uut(.clk(clk),.rst(rst),.up_down(up_down),.count(count));
initial begin
clk=0;
rst=0;
up_down=0;
#20;
rst=1;
#200;
rst=0;
up_down=1;
#200;
rst=0;
up_down=0;
#200;
$stop;
end
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Testbench:
module bcd_tf;
reg clk;
reg rst;
wire [3:0] count;
bcd_count uut(.clk(clk),.rst(rst),.count(count));
initial begin
clk=0;
rst=0;
#20;
rst=0;
#20;
rst=1;
#200;
$stop;
end
always #10 clk=~clk;
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Testbench:
module tb;
parameter WIDTH = 4;
reg clk;
reg rstn;
wire [WIDTH-1:0] out;
ring_ctr uut (.clk (clk),.rstn (rstn),.out (out));
always #10 clk = ~clk;
initial begin
{clk, rstn} <= 0;
$monitor ("T=%0t out=%b", $time, out);
repeat (2) @(posedge clk);
rstn <= 1;
repeat (15) @(posedge clk);
$finish;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Testbench:
module tb;
parameter WIDTH = 4;
reg clk;
reg rstn;
wire [WIDTH-1:0] out;
johnson_ctr uut (.clk (clk),.rstn (rstn),.out (out));
always #10 clk = ~clk;
initial begin
{clk, rstn} <= 0;
$monitor ("T=%0t out=%b", $time, out);
repeat (2) @(posedge clk);
rstn <= 1;
repeat (15) @(posedge clk);
$finish;
end
endmodule
H. K. E. Society’s
PDA College of Engineering, Kalaburagi
Department of Electronics and Communication Engineering
Experiment 9
Verilog Code for design of 4-bit bidirectional shift register
Main Code:
module shift_reg #(parameter MSB=4) (
input d,clk,rstn,en,dir,
output reg [MSB-1:0] out);
always @ (posedge clk)
begin
if (!rstn)
out <= 0;
else
begin
if (en)
case (dir)
0: out <= {out[MSB-2:0], d};
1: out <= {d, out[MSB-1:1]};
endcase
else
out <= out;
end
end
endmodule
wire RxD_data_ready;
wire [7:0] RxD_data;
async_receiver
deserialer(.clk(clk), .RxD(RxD), .RxD_data_ready(RxD_data_ready), .RxD_data(
RxD_data));
assign LCD_RW = 0;
assign LCD_DataBus = RxD_data;
reg [2:0] count;
always @(posedge clk) if(Received_Data | (count!=0)) count <= count + 1;
reg LCD_instruction;
always @(posedge clk)
if(LCD_instruction==0)
LCD_instruction <= Received_Escape;
else
LCD_instruction <= (count!=7);
assign LCD_RS = ~LCD_instruction;
endmodule