Verilog Program Examples Using Iverilog and Gtkwave: September 2021
Verilog Program Examples Using Iverilog and Gtkwave: September 2021
net/publication/354381091
CITATIONS READS
0 386
1 author:
Guru Prasad
Manipal Academy of Higher Education
23 PUBLICATIONS 6 CITATIONS
SEE PROFILE
All content following this page was uploaded by Guru Prasad on 16 September 2021.
GURUPRASAD
A SSISTANT P ROFESSOR
D EPT. OF E LECTRONICS & C OMMUNICATION
M ANIPAL I NSTITUTE OF T ECHNOLOGY
M ANIPAL , INDIA
Published in September, 2021.
3 Structural Modeling 14
1. 4 bit binary ripple adder using full adders . . . . . . . . . . . . . . . . . . . . . . 14
2. BCD adder using 4 bit ripple adders . . . . . . . . . . . . . . . . . . . . . . . . . 16
3. Master slave JK flip flop using SR latch . . . . . . . . . . . . . . . . . . . . . . . 18
4. Negative edge triggered ripple decade counter using JK flip-flops . . . . . . . . . 20
5. 3 bit synchronous counter using T flip-flops . . . . . . . . . . . . . . . . . . . . . 22
References 44
i
List of Figures
ii
6.5 Structure of model used for FSM-3 . . . . . . . . . . . . . . . . . . . . . . . . 41
6.6 Simulation result of Program-3 in Chapter-6 (Sequence detector) . . . . . . . . 43
iii
Chapter 1
1. Write a behavioral verilog code for a full adder using assign statements.
assign sum = a ^ b ^ c;
assign carry = (a&b) | (a&c) | (c&b);
endmodule
Testbench
module full_adder_test ;
reg a,b,c;
wire sum,carry;
// Instanstiation
full_adder FA(a,b,c,sum,carry);
initial
begin
$dumpfile("full_adder.vcd");
$dumpvars(0,full_adder_test);
$monitor($time,"a =%b, b=%b, c=%b, sum=%b, carry=%b",a,b,c,sum,
carry);
a=0; b=0; c=0;
#5 c=1;
#5 b=1;
#5 a=1;
#5 $finish;
end
endmodule
Result
1
Figure 1.1: Simulation result of Program-1 in Chapter-1 (Full adder)
2. Write a behavioral verilog code for a gated D latch using conditional assignment
statement.
assign Q = En ? D : Q ;
// D latch using dataflow and blocking assignment.
// Initial value for Q can not be assigned during the simulation
endmodule
Testbench
module Dff_conditional_test ;
reg D,En;
wire Q;
// Instanstiation
Dff_conditional G0 (D, En, Q);
initial
begin
$dumpfile("Dff_conditional.vcd");
$dumpvars(0,Dff_conditional_test);
$monitor($time," D=%b, En=%b, Q=%b",D,En,Q);
D=1;En=0;
#5 En=1;
#5 D=0;
#5 $finish;
end
endmodule
Result
2
3. Write a verilog code for 2 to 1 Multiplexer in data flow style.
assign out=in[sel];
// note index number of ‘in’ variable. The value of sel is converted to
// decimal automatically.
endmodule
Testbench
module mux_2to1_test ;
reg [1:0] in;
reg sel;
wire out;
// Instanstiation
mux_2to1 MUX (in,sel,out);
initial
begin
$dumpfile("mux_2to1.vcd");
$dumpvars(0,mux_2to1_test);
$monitor($time," in=%2b, sel=%b, out=%b",in,sel,out);
in=2’b01;sel=0;
#5 sel=1;
#5 in=2’b11;
#5 sel=0;
#5 $finish;
end
endmodule
Result
3
Chapter 2
1. Write a behavioral verilog code for 2 to 4 decoder with active low output and active
high input.
always @ (*)
begin
if (enable)
case (din)
0 : dout = 4’b1110;
1 : dout = 4’b1101;
2 : dout = 4’b1011;
3 : dout = 4’b0111;
default: dout =4’b1111;
endcase
else
dout = 4’b1111;
end
endmodule
Testbench
module decoder_2to4_test ;
reg [1:0] din;
reg enable;
wire [3:0] dout;
// Instanstiation
decoder_2to4 DECODER (din, enable, dout);
initial
begin
4
$dumpfile("decoder_2to4.vcd");
$dumpvars(0,decoder_2to4_test);
$monitor($time," din=%2b, enable=%b, dout=%4b",din,enable,dout);
#10 enable=1;
#20 din=2’b11;
#20 din=2’b01;
#20 din=2’b10;
end
initial
begin
din=2’b00;
enable=0;
#100 $finish;
end
endmodule
Result
2. Write a behavioral verilog code for 8 to 3 priority encoder with highest priority
assigned for lower bits.
always @ (*)
begin
if (enable)
casez (din) // Casez is used for handling dont cares.
8’b???????1 : dout=3’b000;
8’b??????1? : dout=3’b001;
8’b?????1?? : dout=3’b010;
8’b????1??? : dout=3’b011;
8’b???1???? : dout=3’b100;
8’b??1????? : dout=3’b101;
8’b?1?????? : dout=3’b110;
8’b1??????? : dout=3’b111;
default: dout =3’bzzz;
endcase
5
else
dout = 3’bzzz;
end
endmodule
Testbench
module priority_encoder_test ;
reg [7:0] din;
reg enable;
wire [2:0] dout;
// Instanstiation
priority_encoder ENCODER (din, enable, dout);
initial
begin
$dumpfile("priority_encoder.vcd");
$dumpvars(0,priority_encoder_test);
$monitor($time," din=%8b, enable=%b, dout=%3b",din,enable,dout);
#10 enable=1;
#20 din=8’b11100011;
#20 din=8’b10000000;
#20 din=8’b11110000;
#20 din=8’b00000000;
end
initial
begin
din=8’b00000001;
enable=0;
#100 $finish;
end
endmodule
Result
3. Write a verilog code for edge triggered D flip-flop with asynchronous set and reset.
6
assign Qbar= ~Q;
// Instanstiation
Dff_edge G0 (D, reset, set, clk, Q, Qbar );
//Clock generation
always #5 clk=~clk ;
// Initialisation
initial
begin
D=1; reset=0;
set=1;
clk=0;
#100 $finish;
end
7
Result
// Instanstiation
ring_counter G0 (clk, init, count);
// Clock generation
always #5 clk=~clk ;
// Initialization
8
initial
begin
init=1;
clk=0;
#100 $finish;
end
5. Write a verilog code for a 4 bit ripple up counter with asynchronous clear.
9
begin
if (reset)
Q2 <= 0;
else
Q2 <= ~Q2;
end
// Instanstiation
ripple_counter G0 (clk, reset, Q0, Q1, Q2, Q3);
// Clock generation
always #5 clk=~clk;
// Initialisation
initial
begin
reset=1;
clk=0;
#200 $finish;
end
endmodule
10
Result
// Index [0:7] makes data storage as 0th bit at left most and 7th
// bit as right most. If din<=00001011 then display will be 0B.
// However 0th bit is 0 and 7th bit is 1. Addition is din+1=0C.
always @ (posedge clk)
if (reset == 1) // reset has the highest priority
count <= 0;
else if (load == 1) // if load is kept high, it will not count
count <= din;
else if ( mode )
count <= count+1;
else
count <= count-1;
endmodule
Testbench
module counter_sync_test ;
reg mode, load, reset, clk;
reg [0:7] din;
wire [0:7] count;
// Instanstiation
counter_sync G0 (mode, load, clk, reset, din, count);
// Clock generation
always #5 clk=~clk ;
// Initialisation
initial
begin
reset=1;
load=0;
mode=1;
11
clk=0;
din=8’b00000001;
#150 $finish;
end
7. Write a verilog code using sequential statements for a bidirectional shift register.
12
Q2 <= Q3;
Q3 <= lin;
end
end
endmodule
Testbench
module bidir_shiftreg_test ;
reg rin, lin, mode, clk ;
wire Q0,Q1,Q2,Q3;
// Clock generation
always #5 clk=~clk ;
initial
begin
mode=1;
clk=1;
rin=0;
lin=1;
#100 $finish;
end
initial
begin
$dumpfile("bidir_shiftreg.vcd");
$dumpvars(0,bidir_shiftreg_test);
$monitor($time," rin=%b, lin=%b, mode=%b, Q0=%b, Q1=%b, Q2=%b, Q3
=%b", rin, lin, mode, Q0,Q1,Q2,Q3);
#45 mode=0;
end
endmodule
Result
13
Chapter 3
Structural Modeling
1. Write a verilog code in structural style for a 4 bit binary ripple adder using full
adders.
a w1 s
b
c
w2
w5
w3 cout
w4
14
wire w1, w2, w3, w4, w5;
//Instanstiation
ripple_adder G0 (a, b, cin, s, cout);
initial
begin
a=4’b0000;
b=4’b0000;
cin=0;
#50 $finish;
end
initial
begin
$dumpfile("ripple_adder.vcd");
15
$dumpvars(0,ripple_adder_test);
$monitor($time," a=%4b, b=%4b, cin=%b, s=%4b, cout=%b",a, b, cin,
s, cout );
#5 a=4’b1111;
#5 cin=1;
#5 b=4’b1111;
#5 cin=0;
#5 a=4’b1010;
end
endmodule
Result
2. Write a verilog code in structural style for a BCD adder using 4 bit ripple adders.
a[3;0] b[3;0]
RA0
p[3;0]
w1 cout
p3
w3
w2 p1
p3
p2
0
CY RA1
NC s[3;0]
16
// Module for full adder using primitive gates
module full_adder (a,b,c,s,cout);
input a, b, c;
output s,cout;
wire w1, w2, w3, w4, w5;
17
// Note CY is input and as well as output
endmodule
Testbench
module bcd_adder_test ;
reg [3:0] a, b;
wire [3:0] s;
wire CY;
// Instanstiation
bcd_adder G0 ( a, b, s, CY);
initial
begin
a=4’b0000;
b=4’b0000;
#50 $finish;
end
initial
begin
$dumpfile("bcd_adder.vcd");
$dumpvars(0,bcd_adder_test);
$monitor($time,"a=%4b, b=%4b, s=%4b, CY=%b",a, b, s, CY );
#5 a=4’b0110;
#5 b=4’b0011;
#5 b=4’b0111;
#5 a=4’b1001;
#5 b=4’b1001;
end
endmodule
Result
3. Write a verilog code in structural style for a master slave JK flip flop using SR latch.
18
s1
J s2
S Q S Q Q
clk
K R Qbar R Qbar Qbar
r2
r1
clkbar
always @ (S,R,enable)
casez ({S,R,enable})
3’b000: Q = Q;
3’b001: Q = Q;
3’b010: Q = Q;
3’b011: Q = 0;
3’b100: Q = Q;
3’b101: Q = 1;
3’b110: Q = Q;
3’b111: Q = 0;
default: Q = Q;
endcase
endmodule
19
reg J, K, clk;
wire Q, Qbar;
// Instanstiation
master_slave_JK G0 (J, K, clk, Q, Qbar);
always #5 clk=~clk;
initial
begin
clk=1;
J=1;
K=0;
#60 $finish;
end
initial
begin
$dumpfile("master_slave_JK.vcd");
$dumpvars(0,master_slave_JK_test);
$monitor($time," J=%b, K=%b, Q=%b, Qbar=%b", J, K, Q, Qbar );
#12 J=0;
#10 K=1;
#10 K=0;
#10 J=1;
end
endmodule
Result
4. Write a verilog code in structural style for a negative edge triggered ripple decade
counter using JK flip-flops.
20
Q0 Q1 Q2 Q3 (MSB)
1 1 1 1
J Q J Q J Q J Q
clk
K clr K clr K clr K clr w1
R
reset
begin
if (clr == 0)
Q <= 0;
else
case({J,K})
2’b00 : Q<=Q;
2’b01 : Q<=0;
2’b10 : Q<=1;
2’b11 : Q<=~Q;
default: Q<=Q;
endcase
end
endmodule
// Instanstiation
21
ripple_decade_counter G0 (clk, reset, Q0, Q1, Q2, Q3);
always #5 clk=~clk;
initial
begin
clk=0;
reset=0;
#200 $finish;
end
initial
begin
$dumpfile("ripple_decade_counter.vcd");
$dumpvars(0,ripple_decade_counter_test);
$monitor($time," reset=%b, Q0=%b, Q1=%b, Q2=%b, Q3=%b",reset, Q0,
Q1, Q2, Q3 );
#34 reset=1;
end
endmodule
Result
5. Write a verilog code in structural style for a 3 bit synchronous counter using T
flip-flops.
w3
T Q T Q T Q
1 1
clear
clk
22
// Module for positive edge triggered T FF
module tff (Q, T, clear, clk);
input T,clear, clk;
output reg Q;
// Instanstiation
synchronous_counter G0 (clear, clk, Q);
always #5 clk=~clk;
initial
begin
clear=0;
clk=1;
#150 $finish;
end
initial
begin
23
$dumpfile("synchronous_counter.vcd");
$dumpvars(0,synchronous_counter_test);
$monitor($time, " clear=%b, Q=%3b", clear, Q );
#12 clear =1;
end
endmodule
Result
24
Chapter 4
1. Write a verilog code for implementing an 4 bit ALU block using ‘functions’ which
performs the following operations - addition, subtraction, multiplication, exclusive-OR.
25
exclusive[7:4]=4’b0000;
end
endfunction
always@(a,b,sel)
case(sel)
2’b00: z=add(a,b);
2’b01: z=sub(a,b);
2’b10: z=mul(a,b);
2’b11: z=exclusive(a,b);
default: z=8’b00000000;
endcase
endmodule
Testbench
module alu_function_test ;
reg [3:0] a,b;
reg [1:0] sel;
wire [7:0] z;
alu_function G0 (a,b,sel,z);
initial
begin
a=4’b0000;
b=4’b0000;
sel=2’b00;
#50 $finish;
end
initial
begin
$dumpfile("alu_function.vcd");
$dumpvars(0,alu_function_test);
$monitor($time," a=%4b, b=%4b, sel=%b, z=%8b", a,b,sel,z );
#5 a=4’b1111;b=4’b1110;
#10 sel=2’b01;
#10 sel=2’b10;
#10 sel=2’b11;
end
endmodule
Result
26
2. Write a verilog code for a implementing f = ∑ m(1, 3, 4, 6) using 2:4 decoders with
active low outputs. Use tasks for 2:4 decoders and NAND gates.
always@(x,y,z)
begin
// Order of statement is important; executes sequentially
decoder (y,z,~x,r1[0],r1[1],r1[2],r1[3]);
decoder (y,z,x, r2[0],r2[1],r2[2],r2[3]);
nand4 (r1[1],r1[3],r2[0],r2[2],f);
end
endmodule
27
Testbench
module decoder_ckt_test ;
reg x,y,z;
wire f;
// Instanstiation
decoder_ckt G0 (x,y,z,f);
initial
begin
x=0;
y=0;
z=0;
#50 $finish;
end
initial
begin
$dumpfile("decoder_ckt.vcd");
$dumpvars(0,decoder_ckt_test);
$monitor($time," x=%b, y=%b, z=%b, f=%b", x, y, z, f );
#5 x=1;
#5 y=1;
#5 z=1;
#5 x=0;
#5 z=0;
end
endmodule
Result
28
3. Write a verilog code for a 8:1 multiplexer implemented using 4:1 and 2:1 multiplexers
as UDPs.
29
reg [2:0] s;
integer p;
wire f;
// Instanstiation
mux_ckt_udp G0 (i, s, f);
initial
begin
i=8’b11010011;
for (p=0; p<8; p=p+1)
begin
s=p; #5;
$display ("T=%2d, s[2]=%b, s[1]=%b, s[0]=%b, f=%b",
$time, s[2], s[1], s[0], f);
end
end
initial
begin
$dumpfile("mux_ckt_udp.vcd");
$dumpvars(0,mux_ckt_udp_test);
#50 $finish;
end
endmodule
Result
Figure 4.3: Simulation result of Program-3 in Chapter-4 (8:1 Mux using UDPs)
4. Write a verilog code for a 3 bit ripple down counter using positive edge triggered T
flip-flops. Use UDP for T flip-flop.
30
initial
Q=0;
table
// T clear clk : Q : Qnew
? 0 ? : ? : 0; // Clear
? (01) ? : ? : -; // Ignore transition of
clear
0 1 (01) : ? : -; // No toggle
1 1 (01) : 0 : 1; // Toggle
1 1 (01) : 1 : 0;
? 1 (?0) : ? : -; // Ignore -ve edge
? 1 (0x) : ? : -;
? 1 (1x) : ? : -;
(??) ? ? : ? : -; // Ignore change of T on
steady clk
endtable
endprimitive
// Instanstiation
counter_udp G0 (clear, clk, Q);
always #5 clk=~clk;
initial
begin
clear=0;
clk=1;
#150 $finish;
end
31
initial
begin
$dumpfile("counter_udp.vcd");
$dumpvars(0,counter_udp_test);
$monitor($time, " clear=%b, Q=%3b", clear, Q );
#12 clear =1;
end
endmodule
Result
Figure 4.4: Simulation result of Program-4 in Chapter-4 (Ripple counter using UDPs)
32
Chapter 5
Vdd
p1
x f
n1
endmodule
Testbench
module inverter_switch_test ;
reg x;
wire f;
// Instanstiation
inverter_switch G0 (x,f);
33
initial
begin
x=0;
#30 $finish;
end
initial
begin
$dumpfile("inverter_switch.vcd");
$dumpvars(0,inverter_switch_test);
$monitor($time,"x = %b, f = %b",x,f );
#5 x=1;
#10 x=0;
#5 x=1;
end
endmodule
Result
2. Write a switch level verilog code for a two input NAND gate.
nand_switch G0 (x,y,f);
34
Vdd
x p1
y p2
f
n1
n2
initial
begin
x=0; y=0;
#20 $finish;
end
initial
begin
$dumpfile("nand_switch.vcd");
$dumpvars(0,nand_switch_test);
$monitor($time, " x = %b, y = %b, f = %b",x,y,f );
for (i=0; i<4; i=i+1)
begin
{x,y}=i;
#5;
end
end
endmodule
Result
35
2. Write a switch level verilog code for a two input NOR gate.
Vdd
x p1
y p2
n1
n2
endmodule
Testbench
module nor_switch_test ;
reg x,y;
wire f;
integer i;
36
// Instanstiation
nor_switch G0 (x,y,f);
initial
begin
x=0; y=0;
#20 $finish;
end
initial
begin
$dumpfile("nor_switch.vcd");
$dumpvars(0,nor_switch_test);
$monitor($time, " x = %b, y = %b, f = %b",x,y,f );
for (i=0; i<4; i=i+1)
begin
{x,y}=i;
#5;
end
end
endmodule
Result
37
Chapter 6
1. Write a verilog code for a FSM which is defined as following - The has three lamps,
RED, GREEN and YELLOW, that should glow cyclically with a fixed interval (say, 1
second).
absent state
light
I/P NS NS PS Output
FF O/P
Logic Logic
clk
always@(posedge clk) always @(PS)
38
case(state)
S0: light = RED;
S1: light = GREEN;
S2: light = YELLOW;
default: light = RED;
endcase
endmodule
Testbench
module FSM_1_test ;
reg clk;
wire [2:0] light;
// Instanstiation
FSM_1 G0 (clk, light);
always #5 clk=~clk;
initial
begin
clk=0;
#50 $finish;
end
initial
begin
$dumpfile("FSM_1.vcd");
$dumpvars(0,FSM_1_test);
$monitor($time," LIGHT =%3b",light );
end
endmodule
Result
2. Write a verilog code for a synchronous binary parity checker which produces output
high whenever it receives even number of 1s in binary input stream.
39
x state
z
I/P NS NS PS Output
FF O/P
Logic Logic
clk
always@(posedge clk) always @(PS)
EVEN/1
1 ODD/0
0 0
1
Figure 6.3: Structure of model used for FSM-2
// It is a Moore model.
always @(posedge clk) // Always block for NS logic and FF
case(state)
EVEN: state <= x ? ODD : EVEN;
ODD: state <= x ? EVEN: ODD;
default: state <= EVEN;
endcase
//Instanstiation
FSM_2 G0 (x, clk, z);
always #5 clk=~clk;
initial
begin
clk=0;
x=0;
#70 $finish;
40
end
initial
begin
$dumpfile("FSM_2.vcd");
$dumpvars(0,FSM_2_test);
$monitor($time," x = %b, z = %b ",x,z );
#12 x=1;
#10 x=0;
#10 x=0;
#10 x=1;
end
endmodule
Result
3. Write a verilog code for a synchronous overlapping sequence detector which detects
‘0110’ in a binary data.
always@(PS,x)
x state
z
I/P NS NS PS Output
FF O/P
PS Logic Logic
clk
always@(posedge clk)
1/0
S0 0/0 S1 1/0 S2 1/0 S3
1/0
0/0
0/0 0/1
41
output reg z;
parameter S0=0, S1=1, S2=2, S3=3;
reg [1:0] PS, NS;
// It is a Mealy model.
always @(posedge clk) // Always block for FF
PS <= NS;
// Instanstiation
FSM_3 G0 (x, clk, z);
always #5 clk=~clk;
42
initial
begin
clk=0;
x=0;
#150 $finish;
end
initial
begin
$dumpfile("FSM_3.vcd");
$dumpvars(0,FSM_3_test);
$monitor($time," x = %b, z = %b ",x,z );
#12 x=0; #10 x=0; #10 x=1; #10 x=1;
#10 x=0; #10 x=1; #10 x=1; #10 x=0;
#10 x=1; #10 x=0;
end
endmodule
Result
43
References
[1] NPTEL video lecture series by Prof. Indranil Sengupta, IIT Kharagpur, https://nptel.
ac.in/courses/106/105/106105165/#
[2] Samir Palnitkar, “Verilog HDL: A Guide to Digital Design and Synthesis”, Prentice Hall
PTR, 2003.
44