Uploaded by kavanab506

Verilog HDL Introduction: Structure, Operators, Data Types

advertisement
MODULE-4
INTRODUCTION TO VERILOG
• Introduction to VERILOG: Structure of Verilog Module,
Operators, Data Types, Styles of description
• VERILOG Data Flow Description: Highlights of Data Flow
description, Structure of Data Flow Description
Why HDL?
What is Hardware description language (HDL):
HDL is a computer aided design (CAD) tool for the modern digital
design and synthesis of digital systems.
Need for HDL
• The advancement in the semiconductor technology, the power and
complexity of digital systems has increased. Due to this, such digital
systems cannot be realized using discrete integrated circuits (IC’s).
• Complex digital systems can be realized using high-density
programmable chips such as Application Specific Integrated Circuits
(ASIC’s) and Field Programmable Gate Arrays (FPGA’s). To design
such systems, we require sophisticated CAD tool such as HDL.
• HDL is used by designer to describe the system in a computer
language that is similar to other software Language like C.
Debugging the design is easy, since HDL package implement
simulators and test benches. The two widely used Hardware
description languages are
– VHDL and Verilog
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
2
A Brief History of Verilog
Evolution of Verilog
• In 1983, a company called Gateway design
Automation developed a hardware-description
language for its newly introduced logic simulator
verilog_XL
• Gateway was bought by cadence in 1989 & cadence
made Verilog available as public domain.
• In December 1995, Verilog HDL became IEEE standard
1364-1995.
• The language presently is maintained by the Open
Verilog international (OVI) organization.
• Verilog code structure is based on C software language.
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
3
Structure of Verilog Module
• The Verilog module has a declaration and a
body.
• In the declaration, name, input and outputs of
the modules are listed.
• The body shows the relationship between the
input and the outputs with help of signal
assignment statements.
• endmodule
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
4
The syntax of the Verilog module is shown below
module name of module(port_list);
// declaration:
input , output, reg, wire, parameter, inout;
functions, tasks;
// statements or body
Initial statement
always statement
module instantiation
continuous assignment
endmodule
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
5
The example verilog program: halfadder
module halfadder (a,b,sum,carry);
input a;
input b;
output sum;
output carry;
assign sum=a ^b; // statement 1
assign carry=a &b; // statement2
endmodule
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
6
• Verilog is case sensitive. Halfadder and halfadder are two
different modules in verilog. The declaration starts with
predefined word module.
• The name of the module should start with alphabetical
letter and can include special character underscore (_). It is
user selected.
• Semicolon (;) is a line separator. The order in which the
inputs & outputs and their declarations are written is
irrelevant.
• “=” is assignment operator, and symbols ^ and & are used
for: “xor” and “and” respectively.
• The doubles slashes (//) signal a comment command. If the
comment takes more than one line, new double slashes can
be used or the pair /*…………*/ can be used to write a
comment of any length.
• The program ends with predefined word endmodule
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
7
Verilog ports
Ports are the set of signals or pins that acts as input
or output to a particular module.
Verilog ports can be one of the following three
modes:
• input: the port is only an input port. In any assignment
statement, the port should appear only on the right
hand side of the assignment statement.(i.e., port is
read.)
• output: the port is an output port. In contrast to VHDL,
the Verilog output port can appear on either side of
the assignment statement.
• inout: this port can be used as both an input and
output. The inout port represents a bidirectional bus.
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
8
Operators
HDL has a extensive list of operators. Operator
performs a wide variety of functions.
These Functions can be classified as
1. Logical operators such as and, or, nand, nor, xor,
xnor and not
2. Relational operators: to express the relation
between objects. The operators include =, /=, <, <=,
> and >=.
3. Arithmetic operators: such as +, -, * and /.
4. Shifts operators: To move the bits of an objects
in a certain direction such as right or left sll, srl, sla,
sra, rol and ror .
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
9
1. Logical operators
• These operator performs Logical operations,
such as and, or, nand, nor, xor, xnor, and not.
The operation can be on two operands or on a
single operand. The operand can be single bit
or multiple bits.
• Verilog logical operator can be classified as
-Bitwise Logical operators
-Boolean logical operators and
-Reduction logical operators.
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
10
Bitwise Logical Operators:
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
11
• The bitwise logical operators operate on the
corresponding bits of two operands.
• Example Z= x & y, if x=1011 and y=1010 are 4bit signals then z=1010 is logical and
operation of x and y.
Boolean logical operators:
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
12
• Boolean operators operate on the two operands. The
result is Boolean true (1) or false (0).
• Examples
1. z= x && y, if x=1011 and y=0001 then Z=1
2. z= x && y, x=0000 and y=0101 then z=0;
3. with unknowns: if a = 2’b0x; b = 2’b10;
Then (a && b) evaluates to x
4. With expressions: (a == 2) && (b == 3) evaluates to 1 only if
both comparisons are true
5. z=!x , where ! Is the negation operator. if x=1111 then z=0;
6. If a = 3 and b = 0, then...
Z=(a && b) evaluates to zero
Z=(b || a) evaluates to one
Z=(!a) evaluates to 0
Z=(!b) evaluates to 1
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
13
Reduction operators:
• These operators operate on a single operand. The
result is Boolean value 0 or 1.
• Example y=&x, if x=1010 then y= (1&0&1&0)=0
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
14
2. Relational operators
• These are implemented to compare the values of two objects.
The result is false (0) or true (1).
• The result can also be of type unknown (X) when any of the
operand include don’t care or unknown (X) or high
impedance.
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
15
Example:
1. if (A==B), if the values of A or B contains one or more don’t
care or Z bits. The value of the expression is unknown.
• If A is equal to B, then result of the expression (A==B) is true
(1).
• If A is not equal to B, then result of the expression (A==B) is
false (0).
2. If (A===B), if A=10x1 and B=10x1 then result is true(1)
if A=10x1 and B=1011 then result is false(0)
inclusive including x and z also
3. Arithmetic operators
•
Arithmetic operators can perform a wide variety of
operation, such as addition, subtraction, multiplication and
division.
• Example y :=(A*B) calculates the values of y as the product of
A times B
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
16
•
Verilog arithmetic operator are shown in table
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
17
4. Verilog Shift operators
• It has basic shift operators. These are unary operators i.e.,
operate on single operand. Example if A=1110, is a 4 bit
vectors table shows the Verilog shift operators as they apply
to operand A
2/13/2025
18
Dr. Prakash K M. Dept. of E&CE, BIET
Data types
• The data or operands used in the language must have
several types to match the need for describing the
hardware.
Verilog Data types
• There are different types of Verilog data types. Namely
1. Nets
2. Registers
3. Vectors
4. Integer
5. Real
6. Parameters
7. Array
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
19
1. Nets
• These are declared by the predefined word “wire”.
• Nets values are changed continuously by the circuits that are
driving them.
• A wire represents a physical wire in a circuit and is used to
connect gates or modules.
• The value of a wire can be read, but not assigned to, in a
function or block. Verilog supports 4 values for nets.
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
20
Example:
• Wire sum; // statement declares a net by name sum.
• Wire s1=1’b0; // this statement declares a net by the
name of s1; it is initial value is 1 bit with value 0.
2. Registers
• Registers store values until they are updated.
• They are data storage elements.
• Declared by the predefined word “reg”
• Verilog supports 4 values for registers. As shown
in above table.
Example: reg sum_total; // declares a register by
the name sum_total.
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
21
3. Vectors
• These are multiple bits.
• A reg or net can be declared as a vector.
• Vectors are declared by brackets [].
Example: wire [3:0] a;
wire [3:0] a=4’b1010
reg [7:0] total =8’d12;
4. Integer
• Declared by the predefined word “integer”.
• Integers are general-purpose variables.
Example: Integer no_bits;//The above statement declares
no_bits as an integer.
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
22
5. Real
• Real (floating point) numbers are declared with the predefined
word “real”. Examples of real values are 2.4, 56.3 5e12.
Example: real weight; // the statement declares the register weight as
real.
6. Parameters
• It represents global constants.
• Declared by the predefined word “parameter”
Example of Implementing parameters:
module comp_genr (x, y, xgty, xlty, xeqy);
parameter N=3;
input [N:0] x, y; // x and y are 4 bit size
output xgty, xlty, xeqy;
wire [N:0] sum, xb; // nets sum and xb are 4 bit
• To change x, y, sum and xb to 8 bits change the value of N as
parameter=7
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
23
7. Array
• There is no predefined word “array”. Registers and integers
can be used as arrays.
Example:
parameter N=4;
parameter M=3;
reg signed [M: 0] carry [0:N]
reg [M: 0] b [0: N];
integer sum [0: N];
• The above statement declares an array by the name sum. It
has 5 elements, and each element is an integer type.
• array carry has 5 elements, and each elements is 4bits.
They are in 2’S complement form
• The array b has 5 elements, each element is 4 bits. The
value of each bit can be 0, 1, X or Z;
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
24
Style (Types) of Descriptions
– Behavioral Descriptions
– Structural Descriptions
– Dataflow Descriptions
– Switch level Descriptions
– Mixed-type Descriptions
– Mixed-language Descriptions
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
25
1. Behavioral Descriptions
• This models the system as to how the outputs behave with
inputs.
• Module includes the predefined word always or initial.
Example:
module half_adder (a, b, sum, carry);
input a, b;
output sum, carry;
reg sum, carry;
always @ (a, b)
begin
# 10 sum = a ^ b; // after 10 ns
# 10 carry = a & b; // after 10 ns
end
endmodule
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
26
2. Structural Descriptions
• Models the system as components or gates.
• Uses gates construct such as “and”, “or”, or “not” in the
module.
Example:
module half_adder (a, b, sum, carry);
input a, b;
output sum, carry;
xor x1 (sum, a, b);
and a1 (carry, a, b) ;
endmodule
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
27
3. Dataflow Descriptions
• It describes how the systems signals flow from the input to the
output.
• The description is done by writing the Boolean function of the
outputs.
• The dataflow statements are concurrent(occur at the same time);
their execution is controlled by events.
Example:
module half_adder (a, b, sum, carry);
input a, b;
output sum, carry;
assign sum = a ^ b;
assign carry = a & b ;
endmodule
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
28
4. Switch level Descriptions
• It is the lowest level of description.
• The system is described using switches or transistors.
• Verilog keywords nmos, pmos, cmos, tran,
tranif0(bidirectional switches) describe the system.
or
Example of CMOS Inverter
module cmos_inv (a, f);
input a;
output y;
supply1 vdd;
supply2 gnd;
pmos p1 (f, vdd, a);
nmos n1 (f, gnd, a);
endmodule
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
29
5. Mixed-type Descriptions
• It uses more than one type or style.
• Here we may describe some parts of the system using one type and other
parts using another type.
Example:
module ALU_mixed(a, b, cin, opc, z);
………………..
wire [2:0] g, p;
wire c0, c1;
//the following is the data flow description
assign g[0]=a[0] & b[0];
assign g[1]=a[1] & b[1];
assign g[2]=a[2] & b[2];
assign p[0]=a[0] | b[0];
assign g[1]=a[1] | b[1];
assign g[2]=a[2]| b[2];
………………………..
//the following is the behavioral description
always @ (a, b, cin, opc, temp1)
begin
case(opc)
……………………………………..
endmodule
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
30
6. Mixed-language Descriptions
• It is newly added tool for HDL descriptions. The user can write a module in
one language (VHDL or Verilog) and invoke or import a construct (entity or
module) written in the other language.
Example:
module full_adder (a, b, cin, sum, carry);
input a, b, cin;
output sum, carry;
wire c0, c1, s0;
HA H1 (b, cin, s0, c0); // Description of HA is written in VHDL in the entity HA
………………………..
endmodule
// half adder entity written in VHDL
Library IEEE;
use ieee.std_logic_1164.all
entity HA is
Port (a, b : in std_logic; s, c: out std_logic);
end HA
architecture HA_Dtflw of HA is
begin
s<=a xor b;
c<=a and b;
end HA_Dtflw;
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
31
DATA FLOW DESCRIPTION
Highlights of Data Flow description:
•
•
•
Shows how signal flows from system inputs to outputs.
Signal flow shown by Boolean function of the output or logical structure of the system.
At any signal time, all signal assignment statements that have an event are executed
concurrently.
Structure of Data-flow Description:
Consider an example of Verilog dataflow description
module system ( I1, I2, O1,O2 );
input I1, I2;
output O1, O2;
assign O1 = I1 & I2; //st1
assign O2 = I1 ^ I2; //st2
end module
•
•
•
Module name is system
st1 and st2 are used to assign a value to outputs O1 and O2
Predefined word assign is used to assign a value to the left hand side of signal
assignment statement
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
32
Signal declaration and assignment statements:
•
Consider AND-OR Circuit shown in figure
a
s1
b
c
d
s2
• Signals a, b, c and d are inputs and Signal y is the output
• Inputs and outputs are declared in the module as ports
• Signals are to be declared before it can be used
• Signal s1 and s2 are intermediates and are defined by predefined word wire
i.e. wire s1, s2;
• The value of the wire continuously changing with changes in the device that is
driving it. For example s1 is the output of AND gate and s1 may change as a or b
changes.
• Another type of declaration is the reg(register) which is used when a value need to
be stored
• Predefined word assign is used to assign a value to the left hand side of signal
assignment statement
• assign O1 = I1 & I2;
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
33
• Execution of signal assignment statement has two phases.
– Calculation and
– Assignment
•
In the example of system,
module system ( I1, I2, O1,O2 );
input I1, I2;
output O1, O2;
assign O1 = I1 & I2; //st1
assign O2 = I1 ^ I2; //st2
end module
• Assume that an event at T0 occurs on either signal I1 or I2. This event
changes the value of I1 from 0 to 1 and also the value of I2 from 0 to 1.
1. Calculation: The value of O1 is calculated using the current values of I1 and
I2 at time T0. The value 1 and 1=1 is calculated. This is not yet assigned to O1.
2. Assignment: The calculated value 1 is assigned to O1 after a delay time.
The delay time can be implicitly or explicitly specified. If no delay time is
specified, the HDL uses a default, small delay of Δ (delta) seconds.
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
34
Concurrent Signal-Assignment Statements:
• In the above example st1 and st2 are
concurrent statements.
• For execution of any concurrent statement to
start, an event on the right hand side of the
statement has to occur.
• An event is a change in the value of signal
such as change from 0 to 1 or 1 to 0
• If an event occur on more than one
statements, regardless of their order, they are
executed concurrently i.e. simultaneously
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
35
Constant Declaration and Assignment Statements:
• The value of a constant is constant within the segment of the
program where it is visible. A constant in Verilog is declared by
its type, such as time or integer.
Example: time period ; //Declares period as constant type time
• To assign a value to the constant we use assignment operator
= in verilog.
Example: Period = 100;
• In verilog there are no explicit units of time. 100 means 100
simulation screen time units.
• The declaration and assignment can be combined in one
statement as:
time period = 100 ;
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
36
Example 1: Data-Flow Description of Half Adder:
a
2/13/2025
b
module half_adder (a, b, s, c);
input a;
Input b;
output s;
output c;
assign s = a ^ b;
assign c = a & b ;
endmodule
a
b
Dr. Prakash K M. Dept. of E&CE, BIET
37
Assigning a Delay Time to the Signal-Assignment Statements:
• To assign Delay Time to the Signal-Assignment Statements a
predefined symbol # is used
Example: assign #10 sum = a ^ b ;
Example: 2×1 Multiplexer with Active Low Enable
Truth Table
Input
SEL
Gbar
x
1
0
0
1
0
output
y
0
A
B
Fig. Logic Diagram
Fig. Logic Symbol
Boolean expression for y is
Y=πΊπ‘π‘Žπ‘Ÿ 𝑆𝐸𝐿 A) + πΊπ‘π‘Žπ‘Ÿ S𝐸𝐿 B)
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
38
• Assume propagation delay time for all gate is 7ns (7 time unit
delay).
• Since it is data flow description, the ordering of statements is
irrelevant.
module mux2x1 (A, B, SEL, Gbar, y);
input A, B, SEL, Gbar;
output y;
wires s1, s2, s3, s4, s5;
assign #7 y = s4 | s5;
// st1
assign #7 s4 = A & s2 & s1; // st2
assign #7 s5 = B & s3 & s1; // st3
assign #7 s2 = ~ SEL;
// st4
assign #7 s3 = ~ s2;
// st5
assign #7 s1 = ~ Gbar;
// st6
endmodule
Alternatively st1 to st6 can be replaced by
assign # 21 y = ((~Gbar) & (~SEL) & A) | ((~Gbar) & SEL & B);
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
39
2/13/2025
Dr. Prakash K M. Dept. of E&CE, BIET
40
Download