DLD Lab Manual KMG Version 24-08-2024
DLD Lab Manual KMG Version 24-08-2024
Prepared By
Mission
Inculcate Academic Excellence through innovative teaching and learning processes and
espousing appropriate pedagogical parameters.
Reinforce the Students with desired technical aptitude, entrepreneurial and leadership skill
sets enabling them to face the challenges of globalization and technological sophistication.
COURSE OUTCOMES:
Bloom’s
Course
Description Taxonomy
Outcome
Level
At the end of the course the student will be able to:
Lab Descriptions
The labs for this course provide a practical foundation for creating synthesizable RTL code. All aspects of
the design flow are covered in the labs. You will write, synthesize, simulate, and implement all the labs.
The focus of the labs is to write code that will optimally infer reliable and high-performance circuits.
Tool Overview
Getting Started with Vivado Design Suite for Spartan 7 FPGA kit
This tutorial explains the step-by-step procedure to create a Vivado project, create source files, synthesize
the design, Implement the design and finally verify the functionality in FPGA using the Spartan 7 board.
Step 7: Now the Vivado tool opens with Flow Navigator on left and Project Manager on right. Flow
Navigator display the list of process involved from HDL input creation to bit file output generation. Project
manager consist of Source, Workspace and Report Window.
Source Window displays Design Sources (VHDL, Verilog), Constraints (XDC) and Simulation sources.
Workspace Window is used to create and view HDL/XDC files. Report Window consist of TCL console,
Messages, Logs, Reports and Design Rules
Step 21: Once the Program Succeeds, Done LED light up on Spartan 7 FPGA kit. Observe the output by
varying the different combination of inputs.
Experiment -1
Adder – Half Adder using data flow, Structural and behaviour modelling styles
AIM: To design half adder using dataflow modeling, Gate level Modelling and Behavioral Modelling style
and verify the functionalities along with their synthesis and simulation results and also implement of FPGA
kits.
module half_adder (
input a,
input b,
output sum,
output carry);
assign sum = a ^ b;
assign carry = a & b;
endmodule
endmodule
module HA_Test( );
reg a, b;
wire sum, carry;
half_adder g1(a,b, sum,carry);
initial
begin
#0 a=1'b0; b=1'b0;
#30 a=1'b0; b=1'b1;
#30 a=1'b1; b=1'b0;
#30 a=1'b1; b=1'b1;
end
initial
$monitor ( $time, "a = %b, b = %b, sum = %b, carry = %b", a, b, sum, carry);
initial
#100 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
Conclusion: The half adder has been implemented in different modelling styles and verified on FPGA kit
Experiment -1
Adder – Full Adder using data flow, Structural and behaviour modelling styles
AIM: To design full adder using dataflow modeling, Gate level Modelling and Behavioral Modelling style
and verify the functionalities along with their synthesis and simulation results and also implement of FPGA
kits.
SIMULATION RESULTS:
SYNTHESIS RESULTS:
VERILOG CODE USING GATE LEVEL MODELLING FOR FULL ADDER USING 2 HALF
ADDERS
module FA_behavioral(a,b,cin,s,c);
input a,b,cin;
output reg s,c;
always @(a or b or cin)
begin
if(a==0 & b==0 & cin==0)
begin
s=0;
c=0;
end
else if(a==0 & b==0 & cin==1)
begin
s=1;
c=0;
end
else if(a==0 & b==1 & cin==0)
begin
s=1;
c=0;
end
else if(a==0 & b==1 & cin==1)
begin
s=0;
c=1;
end
else if(a==1 & b==0 & cin==0)
begin
s=1;
c=0;
end
else if(a==1 & b==0 & cin==1)
begin
s=0;
c=1;
end
else if(a==1 & b==1 & cin==0)
begin
s=0;
c=1;
end
else if(a==1 & b==1 & cin==1)
begin
s=1;
c=1;
end
end
endmodule
VERILOG CODE FOR FULL ADDER TESTBENCH FOR ALL 3 MODELLING STYLES
module FA_Test( );
reg a, b, cin;
wire sum, carry;
FA_behave g1(a,b,cin, sum,carry);
initial
begin
#0 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;
#10 a=1'b0; b=1'b1; cin=1’b1;
#10 a=1'b1; b=1'b0; cin=1’b0;
#10 a=1'b1; b=1'b0; cin=1’b1;
#10 a=1'b1; b=1'b1; cin=1’b0;
#10 a=1'b1; b=1'b1; cin=1’b1;
end
initial
$monitor ( $time, "a = %b, b = %b, cin=%b, sum = %b, carry = %b", a, b, cin, sum, carry);
initial
#100 $finish;
endmodule
Conclusion: The Verilog code for different modelling styles’ has been simulated and implemented on
FPGA Kits
Experiment -3
Combinational designs – I
a. Multiplexer: 4:1, 8:1 MUX.
b. De Multiplexer: 1:4, 1:8 DEMUX.
AIM: To design Mux and Demux using if else and case statements and verify the functionalities along with
their synthesis and simulation results and also implement of FPGA kits.
2:1 Mux (NOT IN SYLLABUS but can spend 20 mins on this if time is available for better
understanding of Conditional operator)
Out=(in1*select) + (in2*~select)
module mux2to1_tb( );
reg in1,in2,sel;
wire out;
mux2to1 g1(in1, in2, sel, out);
initial
begin
#0 sel=1’b0; in1=1’b1; in2=1’b1;
#10 sel=1’b1; in1=1’b1; in2=1’b1;
end
initial
$monitor ( $time, "in1 = %b, in2 = %b, sel=%b, out=%b", in1, in2, sel, out);
initial
#100 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
4:1 MUX
SYNTHESIS RESULTS:
8:1 MUX
VERILOG CODE USING CASE STATEMENT
module mu8to1(i, s, y);
input [7:0] i;
input [2:0] s;
output reg y;
always @ (*)
begin
case (s)
3’b000: y=i[0];
3’b001: y=i[1];
3’b010: y=i[2];
3’b011: y=i[3];
3’b100: y=i[4];
3’b101: y=i[5];
3’b110: y=i[6];
3’b111: y=i[7];
default: y=8’b00000000;
endcase
end
endmodule
SYNTHESIS RESULTS:
1:4 DEMUX
module demux1to4_tb( );
reg d;
reg [1:0] s;
wire [0:3] y;
demux1to4 g1(d,s,y);
initial
begin
#0 d=1’b0;
#10 d=1’b1; s=2’b00;
#10 s=2’b01;
#10 s=2’b10;
#10 s=2’b11;
End
initial
$monitor ($time, "d = %b, s=%b, y=%b", d, s, y);
initial
#100 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
1:8 DEMUX
Y0 = A (S2)'(S1)'(S0)’
Y1 = A (S2)'(S1)'(S0)
Y2 = A (S2)'(S1)(S0)’
Y3 = A (S2)'(S1)(S0)
Y4 = A (S2)(S1)'(S0)’
Y5 = A (S2)(S1)'(S0)
Y6 = A (S2)(S1)(S0)’
Y7 = A (S2)(S1)(S0)
VERILOG CODE FOR 1:8 DEMUX USING CASE STATEMENT
module demux1to8(y,s,a);
output reg [0:7]y;
input [2:0]s;
input a;
always @(*)
begin
case(s)
0: y ={a,7'b0000000};
1: y ={1'b0,a,6'b000000};
2: y ={2'b00,a,5'b00000};
3: y ={3'b000,a,4'b0000};
4: y={4'b0000,a,3'b000};
5: y={5'b00000,a,2'b00};
6: y={6'b000000,a,1'b0};
7: y={7'b0000000,a};
default: y=8'b00000000;
endcase
end
endmodule
SYNTHESIS RESULTS:
Experiment -4
Combinational designs – II (different types of case statements)
c. Encoder with and without Priority: 8:3 and 4:2.
d. Decoder: 3:8 and 2:4.
AIM: To design encoder with and without priority and decoder using Behavioral Modelling style with
different case statements and verify the functionalities along with their synthesis and simulation results and
also implement of FPGA kits.
encwtoutprio u1 (a,en,y);
initial
begin
#0
en=1'b0;
a= 8'b00000001;
#30 a=8'b00000010;
#30 a=8'b00000100;
#30 a=8'b00001000;
#30 a=8'b00010000;
#30 a=8'b00100000;
#30 a=8'b01000000;
#30 a=8'b10000000;
#30 en=1'b1;
a= 8'b00000001;
#30 a=8'b00000010;
#30 a=8'b00000100;
#30 a=8'b00001000;
#30 a=8'b00010000;
#30 a=8'b00100000;
#30 a=8'b01000000;
#30 a=8'b10000000;
end
initial
$monitor ( $time, "a= %b, en= %b, y = %b", a, en,y);
initial
#1000 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
module encwtprio(a,en,y);
input [7:0] a;
input en;
output reg [2:0] y;
always@(a, en)
begin
if(en==1)
y=3'bzzz;
else
casex(a)
8'b00000001:y=3'b000;
8'b0000001x:y=3'b001;
8'b000001xx:y=3'b010;
8'b00001xxx:y=3'b011;
8'b0001xxxx:y=3'b100;
8'b001xxxxx:y=3'b101;
8'b01xxxxxx:y=3'b110;
8'b1xxxxxxx:y=3'b111;
endcase
end
endmodule
TESTBENCH FOR 8:3 PRIORITY ENCODER
module prienc_tb( );
reg [7:0] a;
reg en;
wire [2:0] y;
encwtprio u1 (a,en,y);
initial
begin
#0
en=1'b0;
a= 8'b00000001;
#30 a=8'b10000010;
#30 a=8'b00000101;
#30 a=8'b00001011;
#30 a=8'b0001xxxx;
#30 a=8'b00101000;
#30 a=8'b01001000;
#30 a=8'b10001011;
#30 en=1'b1;
a= 8'b00000001;
#30 a=8'b00000010;
#30 a=8'b00000101;
#30 a=8'b00001011;
#30 a=8'b0001xxxx;
#30 a=8'b0010xxxx;
#30 a=8'b01001000;
#30 a=8'b10001011;
end
initial
$monitor ( $time, "a= %b, en= %b, y = %b", a, en, y);
initial
#1000 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
case (din)
1 : dout[0] = 0;
2 : dout[1] = 1;
4 : dout[2] = 2;
8 : dout[3] = 3;
default : dout = 2’bxx;
endcase
endmodule
module enc4_2_tb( );
reg [3:0] din;
wire [1:0] dout;
enc4_2 g1 (din, dout);
initial
begin
din = 0;
#10; din=1;
#10; din=2;
#10; din=4;
#10; din=8;
end
initial
begin
$monitor($time, “din=%b, dout=%b”, din, dout);
end
initial
#150 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
VERILOG CODE FOR 4:2 PRIORITY ENCODER
module priority_encoderbehave(A, Y);
input [3:0]Y;
output reg [1:0]A;
always@(Y)
begin
casex(Y)
4'b0001:A = 2'b00;
4'b001x:A = 2'b01;
4'b01xx:A = 2'b10;
4'b1xxx:A = 2'b11;
default:$display("Error!");
endcase
end
endmodule
module PriorityEncoder_Test ( ) ;
reg [3:0] Y;
wire [1:0] A;
priority_encoderbehave g1 (.Y(Y), .A(A));
initial
begin
#0 Y = 0;
#10 Y = 4'b0000;
#10 Y = 4'b1000;
#10 Y = 4'b0100;
#10 Y = 4'b0010;
#10 Y = 4'b0001;
#10 Y = 4'b1010;
#10 Y = 4'b1111;
end
initial
begin
$monitor("time=",$time, "A=%b : Y=%b",A,Y);
end
initial
#100 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
VERILOG CODE FOR DECODER 2:4
module dec2to4_tb( );
reg [1:0] din;
wire [3:0] dout;
dec2to4 g1 (din, dout);
initial
begin
#0 din = 0;
#10; din=0;
#10; din=1;
#10; din=2;
#10; din=3;
end
initial
begin
$monitor($time, “din=%b, dout=%b”, din, dout);
end
initial
#100 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
VERILOG CODE FOR DECODER 3: 8
always @(Data_in)
case (Data_in)
3'b000 : Data_out = 8'b00000001;
3'b001 : Data_out = 8'b00000010;
3'b010 : Data_out = 8'b00000100;
3'b011 : Data_out = 8'b00001000;
3'b100 : Data_out = 8'b00010000;
3'b101 : Data_out = 8'b00100000;
3'b110 : Data_out = 8'b01000000;
3'b111 : Data_out = 8'b10000000;
default : Data_out = 8'b00000000;
endcase
endmodule
module tb_decoder ( );
reg [2:0] Data_in;
wire [7:0] Data_out;
decoder3to8 g1 (Data_in, Data_out );
initial
begin
#0 Data_in = 3'b000;
#10 Data_in = 3'b001;
#10 Data_in = 3'b010;
#10 Data_in = 3'b011;
#10 Data_in = 3'b100;
#10 Data_in = 3'b101;
#10 Data_in = 3'b110;
#10 Data_in = 3'b111;
end
initial
begin
$monitor($time, “Data_in=%b, Data_out=%b”, Data_in, Data_out);
end
initial
#100 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
Experiment-5
Aim: To design the 4 bit ALU for the following functions and verify its functationality with test bench and
implement on FPGA kit.
module alu_tb ( );
reg [2:0] S;
reg [3:0] A,B;
wire [3:0] F;
alu g1 (S,A,B, F);
initial
begin
#0 A=4'b0101; B=4'b1001; S=3'b000;
#10 S=3'b001;
#10 S=3'b010;
#10 S=3'b011;
#10 S=3'b100;
#10 S=3'b101;
#10 S=3'b110;
#10 S=3'b111;
SIMULATION RESULTS:
SYNTHESIS RESULTS:
Experiment: 6
Aim: To design different flipflop such as DFF, TFF and JK FF with various combinations of Reset and
verify its functionality with testbench and implement on FPGA kit.
D Filp flop
SIMULATION RESULTS:
SYNTHESIS RESULTS:
J K Flip Flop
always@(posedge clk)
begin // for synchronous reset
if(!rst_n)
{q,q_bar} <= {1'b0,1'b1};
else
begin
case({j,k})
2'b00: {q,q_bar} <= {q,q_bar}; // No change
2'b01: {q,q_bar} <= {1'b0,1'b1}; // reset
2'b10: {q,q_bar} <= {1'b1,1'b0}; // set
2'b11: {q,q_bar} <= {q_bar,q}; // Toggle
endcase
end
end
endmodule
initial
begin
clk=1'b0;
forever #5 clk=~clk;
end
initial
begin
#0 rst_n =0;
#20 j=0; k=0;
#20 j=0; k=1;
#20 j=1;k=0;
#20 j=1;k=1;
#20 rst_n =1;
#20 j=0; k=0;
#20 j=0; k=1;
#20 j=1;k=0;
#20 j=1;k=1;
end
initial
begin
$monitor($time, "clk=%b, rst_n=%b, j=%b, k=%b, q=%b, q_bar=%b", clk, rst_n,j,k,q,q_bar);
end
initial
#200 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
T Flip Flop
module tff_tb ( );
reg clk;
reg rstn;
reg t;
wire q;
initial
begin
clk=1'b0;
forever #20 clk=~clk;
end
initial
begin
#0 rstn =0; t=0;
#30 t=1;
#30 rstn=1; t=0;
#30 t=1;
end
initial
begin
$monitor($time “clk=%b, rstnt=%b, t=%b, q=%b”, clk, rstn,t,q);
end
initial
#500 $finish;
endmodule
SIMULATION RESULTS:
SYNTHESIS RESULTS:
Experiment 8
The “parameter N=5 and width =3” can be changed for different value of modulus
VERILOG CODE MOD 5 UPDOWN COUNTER SYNCHRONOUS RESET
module counter_syn(clk,reset,up_down,count);
parameter N=5;
parameter width=3;
input clk,reset,up_down;
output reg [width-1:0]count;
endmodule
module counter_tb();
parameter N=5,width=3;
reg clk,reset,up_down;
wire [width-1:0]count;
counter_syn G1(clk,reset,up_down,count);
initial
begin
clk=1'b0;
forever #10 clk=~clk;
end
initial
begin
#0 clk=0;reset=1;
#100 reset=0;up_down=1;
#430 up_down=0;
end
initial
#1000 $finish;
endmodule
SIMULATION RESULTS:
Simulation result for mod 5 counter
module counter_asyn(clk,reset,up_down,count);
parameter N=5;
parameter width=3;
input clk,reset,up_down;
output reg [width-1:0]count;
endmodule
module counter_tb();
parameter N=5,width=3;
reg clk,reset,up_down;
wire [width-1:0]count;
counter_asyn G1(clk,reset,up_down,count);
initial
begin
clk=1'b0;
forever #10 clk=~clk;
end
initial
begin
#0 clk=0;reset=1;
#100 reset=0;up_down=1;
#430 up_down=0;
end
initial
#1000 $finish;
endmodule
SIMULATION RESULTS:
Simulation result for mod 5 counter
6. What is Verilog?
Verilog is a Hardware Description Language (HDL) used for describing a digital system such as a
network switch, a microprocessor, a memory, or a flip-flop. Verilog is mainly used to verify analog
circuits, mixed-signal circuits, and the design of genetic circuits. It is also used in the design and
verification of digital circuits at the register-transfer level of abstraction.
7. Who is the founder of the Verilog programming language?
Verilog was introduced by Prabhu Goel, Phil Moorby, Chi-Lai Huang, and Douglas Warmke
between late 1983 and early 1984.
8. What is the difference between == and === in Verilog?
10. What are the main differences between Wire and Reg?
12. What are casex and casez statements used for in Verilog?
Casez allows matching one case item to multiple case expressions by not taking into consideration
the Z bit. It is written as Z or ?.
For a case expression 2’1b0, 2’1b1, 2’1bX, or 2’1bZ will be matched to an item2’1bZ
Whenever comparing the case item and case expression, the don’t care bit (represented by Z or ?)
will be ignored completely irrespective of its value.
Casex is absolutely similar to casez with the addition of ‘X’ to the don’t care bit. So here in casex
‘Z’, ‘?’ and ‘X’ bits are ignored in case item and/or case expression while comparing.