UNIT-I part 2
Introduction to Verilog
CH JAYAPRAKASH,
ASSISTANT PROFESSOR
Dept of ECE
DIGITAL SYSTEM DESIGN Using hdl
SIR CRREDDY COLLEGE OF ENGG
Source :
DIGITAL System Design with FPGA implementation using Verilog & VHDL – CemUnsalan, BoraTar McGraw-Hill Education,2017.
Chapter 5�Introduction to Verilog �
Digital System Design with FPGA: Implementation using Verilog and VHDL
Layout
Digital System Design with FPGA: Implementation using Verilog and VHDL
Introduction
Hardware description languages (HDL) help us formalizing and representing a digital system at hand.
Hence, it can be implemented on a target FPGA platform.
Two popular HDLs are Verilog and VHDL.
We will explore these HDLs in detail in representing digital systems in the following chapters.
Digital System Design with FPGA: Implementation using Verilog and VHDL
Verilog Fundamentals
A digital system can be represented as a module with the following structure in Verilog.
Module Representation
Module {module_name} (port_list );
// Port definitions:
Input,output,inout.
//Declaration :
Wire,reg,parameters,
Functions,task....
statement :
Initial statement
always statement
Module instantiations
Gate instantiations
Continuous assignment
...
endmodule
Digital System Design with FPGA: Implementation using Verilog and VHDL
Verilog Fundamentals
.
Module Representation : example
Module {module_name} (port_list );
// Port definitions:
Input,output,inout.
//Declaration :
Wire,reg,parameters,
Functions,task....
statement :
Initial statement
always statement
Module instantiations
Gate instantiations
Continuous assignment
...
endmodule
All ports are of size 1-bit since no range is Specified. Also four ports are of NET data type since no declaration has specified.
Digital System Design with FPGA: Implementation using Verilog and VHDL
Timing and Delays in Modeling
Verilog Fundamentals
Digital System Design with FPGA: Implementation using Verilog and VHDL
assign output = function of inputs.
Dataflow Modeling
Digital System Design with FPGA: Implementation using Verilog and VHDL
9
Dataflow Modeling: Continuous Assignment
A continuous assignment assigns a value to a net
The target in a continuous assignment can be as follows
10
Dataflow Modeling: Continuous Assignment
Example 1:
Example 2:
Target is a concatenation of a scalar net and vector net
Dataflow Modeling: example 2 : 4 Decoder
En | A | B | Z[0] | Z[1] | Z[2] | Z[3] |
0 | 0 | 0 | 1 | 1 | 1 | 1 |
1 | 0 | 0 | 0 | 1 | 1 | 1 |
1 | 0 | 1 | 1 | 0 | 1 | 1 |
1 | 1 | 0 | 1 | 1 | 0 | 1 |
1 | 1 | 1 | 1 | 1 | 1 | 0 |
Digital System Design with FPGA: Implementation using Verilog and VHDL
12
Dataflow Modeling: example FULL ADDER
gate_keyword instance_name (port_list).
Structural Modeling
Verilog Fundamentals
Digital System Design with FPGA: Implementation using Verilog and VHDL
Structural modeling is described using
Structural Modeling
Module definition
Module instantiation :
Port association can be by Position or By Name instantiation
Digital System Design with FPGA: Implementation using Verilog and VHDL
Where port_expr can be
Structural Modeling
Digital System Design with FPGA: Implementation using Verilog and VHDL
Write verilog code for full adder using structural modeling
Structural Modeling
Sol:
Digital System Design with FPGA: Implementation using Verilog and VHDL
Write verilog code for full adder using structural modeling
Structural Modeling
1. Module: half adder(hf)
module HF (A,B,S,C);
input A,B;
output Sum, Carry;
assign S = A ^ B;
assign C= A & B;
endmodule
2. Module: or gate(or_2)
module or_2 (X,Y,Z);
input X,Y;
output Z;
assign Z = X & Y;
endmodule
Digital System Design with FPGA: Implementation using Verilog and VHDL
Write verilog code for full adder using structural modeling
Structural Modeling
3 TOP Module: FULL ADDER (FA)
module FA (P,Q,Cin,Sum,Cout);
input P,Q,Cin;
output Sum, Cout;
wire S1,C1,C2;
// two module instantiation:
// associating by position.
Hf h1 (P,Q,S1,C1) ;
// associating by name.
Hf h2 (.A(Cin), .B(S1), .S(Sum), .C(C2)) ;
Or_2 O1(C1,C2,Cout);
endmodule
Digital System Design with FPGA: Implementation using Verilog and VHDL
Write verilog code for full adder using structural modeling
Structural Modeling
Digital System Design with FPGA: Implementation using Verilog and VHDL
Write verilog code for full adder using structural modeling
Structural Modeling
Digital System Design with FPGA: Implementation using Verilog and VHDL
Verilog Fundamentals
Behavioral Modeling:
Digital System Design with FPGA: Implementation using Verilog and VHDL
Behavioral Modeling
Digital System Design with FPGA: Implementation using Verilog and VHDL
Behavioral Modeling
Digital System Design with FPGA: Implementation using Verilog and VHDL
Syntax for this representation becomes
always @ (sensitivity_list)
begin
// behavioral description
statement 1
statement 2
statement 3
...
end
Behavioral Modeling
The syntax for always statement is
Digital System Design with FPGA: Implementation using Verilog and VHDL
Behavioral Modeling
Digital System Design with FPGA: Implementation using Verilog and VHDL
Blocking statements
Behavioral Modeling
// Blocking Assignment
Initial
begin
// here, the begin-end clause is used because there are more than one statements in the initial block
#10 a = 0; // 10 time units delay has been given a with value 0
#11 a = 1; // 11 time units delay to a variable with value 1
#12 a = 0; // 12 time units delay to a with value 0
#13 a = 1; // 13 time units delay to a with value 1
end
Digital System Design with FPGA: Implementation using Verilog and VHDL
Behavioral Modeling
// Non-blocking Assignment
initial
begin
// These statements will get executed without the intervention of the other statements
// In other words, their execution will not be blocked by the other ones
#10 b <= 0; // A delay of 10 time units has been given to the variable b with 0 value.
#11 b <= 1; // 11 time units delay to b with value 1
#12 b <= 0; // 12 time units delay to b with value 0
#13 b <= 1; // 13 time units delay to b with value 1
end
Digital System Design with FPGA: Implementation using Verilog and VHDL
There are different ways we can proceed with Verilog coding for full adder:
Behavioral Modeling
Digital System Design with FPGA: Implementation using Verilog and VHDL
Using always statement:
Behavioral Modeling
// Procedural statements inside this always block gets executed once there’s any change in event A, B OR Cin.
`timescale 1ns / 1ps
module full_adder( A, B, Cin, S, Cout);
input A, B, Cin;
Output S, Cout;
always @(A or B or Cin) // or keyword is not performing logical-or
begin
S = A ^ B ^ Cin;
Cout = A&B | (A^B) & Cin;
end
endmodule
Digital System Design with FPGA: Implementation using Verilog and VHDL
Using case statement :
Behavioral Modeling
case (case_expression)
case_item1: procedural_expression;
case_item2: begin procedural_statements;
end .... default: expression;
endcase
Digital System Design with FPGA: Implementation using Verilog and VHDL
Behavioral Modeling
`timescale 1ns / 1ps
module full_adder( A, B, Cin, S, Cout);
input A, B, Cin;
Output S, Cout;
always @(A or B or Cin) // or keyword is not performing logical-or
begin
case (A | B | Cin)
3'b000: begin S = 0; Cout = 0; end
3'b001: begin S = 1; Cout = 0; end
3'b010: begin S = 1; Cout = 0; end
3'b011: begin S = 0; Cout = 1; end
3'b100: begin S = 1; Cout = 0; end
3'b101: begin S = 0; Cout = 1; end
3'b110: begin S = 0; Cout = 1; end
3'b111: begin S = 1; Cout = 1; end
endcase
endmodule
Digital System Design with FPGA: Implementation using Verilog and VHDL
Using if-else statement:
Behavioral Modeling
if(condition_1)
procedural_statement_1;
else if(condition_2)
procedural_statement_2;
else procedural_statement_3;
Digital System Design with FPGA: Implementation using Verilog and VHDL
Behavioral Modeling
`timescale 1ns / 1ps
module full_adder( A, B, Cin, S, Cout);
input A, B, Cin;
Output S, Cout;
always @(A or B or Cin) // or keyword is not performing logical-or
Begin
if(A==0 && B==0 && Cin==0)
begin
S=0; Cout=0;
end
else if(A==0 && B==0 && Cin==1)
begin
S=1; Cout=0;
end
else if(A==0 && B==1 && Cin==0)
begin
S=1; Cout=0;
end
else if(A==0 && B==1 && Cin==1)
begin
S=0; Cout=1;
end
else if(A==1 && B==0 && Cin==0)
begin
S=1; Cout=0;
end
else if(A==1 && B==0 && Cin==1)
begin
S=0; Cout=1;
end
else if(A==1 && B==1 && Cin==0)
begin
S=0; Cout=1;
end
else if(A==1 && B==1 && Cin==1)
begin
S=1; Cout=1;
end
end
endmodule
Digital System Design with FPGA: Implementation using Verilog and VHDL
One other important Verilog keyword for behavioral modeling is initial. this keyword, an initial block can be formed which is executed at time zero. Syntax of the initial block is
initial
begin
statements
end
Behavioral Modeling
Digital System Design with FPGA: Implementation using Verilog and VHDL
Hierarchical Module Representation
Each module represented as a black box
Each module represented as it is
Digital System Design with FPGA: Implementation using Verilog and VHDL
Hierarchical Module Representation
Each module represented as a black box
Each module represented as it is
Digital System Design with FPGA: Implementation using Verilog and VHDL
Testbench Formation in Verilog
Characteristics of a digital system can be analyzed in Vivado by using a testbench.
A Verilog testbench file is composed of five parts as follows.
Structure of a Verilog Testbench File
Digital System Design with FPGA: Implementation using Verilog and VHDL
1.Testbench module declaration.
Testbench Formation in Verilog
Digital System Design with FPGA: Implementation using Verilog and VHDL
Verilog Description of the First System-the Top Module
Testbench Formation in Verilog
2.Input/output port declaration
Digital System Design with FPGA: Implementation using Verilog and VHDL
Verilog Description of the First System–the Supplement File
Testbench Formation in Verilog
2.Input/output port declaration
Digital System Design with FPGA: Implementation using Verilog and VHDL
Testbench Formation in Verilog
3.Instantiation of the unit under test (UUT)
Digital System Design with FPGA: Implementation using Verilog and VHDL
Testbench Formation in Verilog
4. Providing input to the UUT.
Digital System Design with FPGA: Implementation using Verilog and VHDL
#100 { in1t, in2t } = {in1t, in2t } + 1’b1.
Testbench Formation in Verilog
4. Providing input to the UUT.
Digital System Design with FPGA: Implementation using Verilog and VHDL
There are two options to observe simulation results in Vivado.
5.Displaying Test Results
Digital System Design with FPGA: Implementation using Verilog and VHDL