Verilog
Verilog
SUBMITTED BY
SHAHANA S
M S ALEENA FATHIMA
ARAVIND S G
SAJISHNU M S
2
RULES & SYNTAX
• Verilog, as a hardware description language, has a unique set of rules and syntax
elements that help describe digital circuits accurately and efficiently.
• The basic lexical conventions used in Verilog HDL are similar to those in the C
programming language.
Whitespace:
Whitespace can contain the characters for tabs, blanks, and newlines. These
characters are ignored except when they serve to separate other tokens. However, blanks and tabs
are significant in strings.
Comments:
Comments can be inserted in the code for readability and documentation. There are 2
ways to write comments in Verilog,
• A single-line comment starts with // and tells the Verilog compiler to treat everything after this point
to the end of the line as a comment.
• A multiple-line comment starts with /* and ends with */.
/*This is a multiple-line
4
comment */
Operators:
Operators are special characters used to set conditions or operate on variables. There
are three types of operators: unary, binary, and ternary.
• Numbers written without a <size> specification have a default no. of bits depending on the type of
simulator and machine (at least 32 bits).
• Keywords are special identifiers reserved to define the language constructs. They are in
lowercase.
• Identifiers are names given to objects so that they can be referenced in the design.
• Identifiers include alphanumeric characters, underscore and dollar signs. They are case-sensitive.
reg value; // reg is a keyword; value is an identifier
input clk; // input is a keyword; clk is an identifier 8
MODULES
• A module is a basic building block in Verilog.
• A module is a block of Verilog code that implements certain functionality.
• Modules can be embedded within other modules, and a higher-level module can
communicate with its lower-level modules using their input and output ports.
• The name of the module should be given right after the module keyword, and an
optional list of ports may be declared as well.
9
COMPONENTS OF A VERILOG MODULE
10
PORTS
• Port is an essential component of the Verilog module.
• Ports are used to communicate for a module with the external world through
input and output.
• Every port in the port list must be declared as input, output or input.
• Ports, also referred to as pins or terminals, are used when wiring the module to
other modules.
• If the module does not exchange any signals with the environment, there are no
ports in the list.
11
TYPES OF PORTS
PORT DESCRIPTION
The design module can only receive values from outside using its
Input input ports.
The design module can only send values to the outside using
Output its output ports.
12
VARIABLES
In Verilog, variables are used to store and manipulate data within hardware descriptions.
Here’s a breakdown of the variables used in Verilog:
Wire
Reg
Integer
Parameter
Real
Time
Logic
Genvar
Each type of variable serves different purposes in Verilog, whether modelling combinational
logic, storing values across clock cycles, or defining constants. 13
Wire:
Reg:
Used inside procedural blocks like always and initial. It holds values between clock
cycles and is commonly used in sequential logic.
Integer:
Store integers and is often used for loop counters or procedural assignments. It is 32-
bit wide by default.
14
Parameter:
Used to define constants that can be used throughout a module, often for setting
fixed values like bus widths or time delays.
Real:
Stores real numbers. Rarely used in synthesizable code, typically for testbenches.
Time:
Often used to store simulation time values in testbenches. It is 64-bit wide by default.
15
Logic:
A multi-driven signal used for both combinational and sequential logic. It replaces
wire and reg in many cases in SystemVerilog.
Genvar:
16
DATA TYPES
The primary intent of data types in the Verilog language is to represent the data storage
elements like bits in flip-flops and transmission elements like wires that connect
between logic gates and sequential structures.
VALUE DESCRIPTION
x Unknown value
z High impedance
18
Nets:
• Nets are used to connect between hardware entities like logic gates and hence do not
store any value.
• The net variables represent the physical connection between structural entities such
as logic gates.
• These variables do not store values except trireg. These variables have the value of
their drivers, which changes continuously by the driving circuit.
• Some net data types are wire, tri, wor, trior, wand, triand, tri0, tri1, supply0,
supply1, and trireg.
2. Wand (wired-AND)
The value of a wand depends on the logical AND of all the drivers connected to it.
3. Wor (wired-OR)
The value of wor depends on the logical OR of all the drivers connected to it.
4. Tri (three-state)
All drivers connected to a tri must be z, except one that determines the tri's value.
Vectors:
21
Integer, Real and Time Register Data Types:
1. Integer:
• It is a general-purpose register data type used for manipulating quantities.
• Integers are declared by the keyword integer.
• Default width – 32 bits
2. Real:
• Real number constants and real register data types are declared with the keyword
real.
• When a real value is assigned to an integer, the real number is rounded off to the
nearest integer.
• Default value – 0
3. Time:
• Time for storing simulation times in test benches.
• It can be used in conjunction with the $time system task to hold simulation time.
• Default width – 64 bits
22
Arrays:
• Arrays are multiple elements that are 1-bit or n-bis wide.
• Arrays are allowed in Verilog for reg, integer, time and vector register data types.
• Arrays are not allowed for real variables.
• Arrays are accessed by <array_name>[<subscript>].
Memories:
• Memories are modelled in Verilog as an array of registers.
• Each element of the array is known as a word.
• Each word can be one or more bits.
Parameters:
• Verilog allows constants to be defined by the keyword parameters.
• Module behavior can be altered by simply changing the value of a parameter. 23
Strings:
• If the width of the register is greater than the size of the string, Verilog fills
bits to the left of the string with zeroes.
• If the width of the register is smaller than the size of the string, Verilog
truncates the leftmost bits of the string.
24
OPERATORS
Operators act on the operands to produce desired results. Each operator is denoted by
a symbol.
Arithmetic
Logical
Relational
Bitwise
Equality
Reduction
Shift
Concatenation
Conditional
25
26
27
ASSIGNMENT
Placing values onto variables and nets is called assignments.
1. Procedural
2. Continuous
3. Procedural continuous
28
LEGAL LHS VALUES
An assignment has two parts, the right-hand side (RHS) and left-hand side (LHS)
with an equal symbol (=) or a less-than-equal symbol (<=) in between.
The value will be placed onto the variable when the simulation executes this
statement during simulation time. This can be modified and controlled the way we
want by using control flow statements such as if-else-if, looping, and case
statement mechanisms.
o An initial value can be placed onto a variable at the time of its declaration.
o The assignment does not have a duration and holds the value until the next
assignment to the same variable happens.
This is used to assign values to scalar and vector nets. And it happens whenever there is
a change in the RHS.
o This allows us to place a continuous assignment on the same statement that declares
the net.
o Only one declaration assignment is possible because a net can be declared only once.
31
Procedural Continuous Assignments:
These are procedural statements that allow expressions to be continuously assigned to variables or nets.
And these are the two types.
1. Assign deassign:
• It will override all procedural assignments to a variable and deactivate it using the same signal
with deassign.
• The value of the variable will remain the same until the variable gets a new value through a procedural or
procedural continuous assignment.
• The LHS of an assign statement cannot be a part-select, bit-select, or an array reference, but it can be a
variable or a combination of the variables.
2. Force release:
• These are similar to the assign deassign statements but can also be applied to nets and variables.
• The LHS can be a bit-select of a net, part-select of a net, variable, or a net but cannot be the reference
to an array and bit or part-select of a variable.
• The force statement will override all other assignments made to the variable until it is released using
the release keyword. 32
VERILOG TESTBENCH
The testbench is written to check the functional correctness based on
design behavior.
Since the behavior of the design has to be tested, the design module is
known to be “Design Under Test” (DUT).
33
34
MEMORY
Memories are digital storage elements that help store data
and information in digital circuits.
38
REFERENCE
Verilog HDL: A Guide to Digital Design and Synthesis - SAMIR PALNITKAR
https://www.javatpoint.com/verilog
https://www.chipverify.com/tutorials/verilog
https://vlsiverify.com/verilog/writing-a-testbench-in-verilog/
https://www.slideshare.net/slideshow/verilog-operatorspptx/251696451
39
40