Verilog
Verilog
SYLLABUS:
A Brief History of HDL, Structure of HDL Module, Comparison of VHDL
and Verilog
Introduction to Simulation and Synthesis Tools, Test Benches, Verilog
Modules, Delays, data flow style, behavioral style, structural style, mixed
design style, simulating design
Introduction to Language Elements: Keywords, Identifiers, White Space
Characters, Comments, Format, Integers, Reals and Strings, Logic Values,
Data Types-net types, undeclared nets, scalars and vector nets, Register
type, Parameters, Expressions, Operands, Operators, types of Expressions
Why Verilog?
Why use an HDL?
Describe complex designs (millions of gates)
Verilog has 60% of the world digital design market (larger share in US)
The three common HDLs are Verilog, VHDL, and System C. Of these, System C is the
newest. The HDLs will allow fast design and better verification. In most of the industries,
Verilog and VHDL are common. Verilog, one of the main Hardware Description Language
standardized as IEEE 1364 is used for designing all types of circuits. It consists of modules
and the language allows Behavioral, Dataflow and Structural Description. VHDL (Very
High Speed Integrated Circuit Hardware Description Language) is standardized by
IEEE1164. The design is composed of entities consisting of multiple architectures. System
C is a language that consist a set of C++ classes and macros. It allows electronic system
level and transaction modeling.
All the goals like power, throughput, latency (delay), test coverage, functionality and area
consumption required for a design can be known by using HDL. As a result, the designer
can make the necessary engineering tradeoffs and can develop the design in a better and
efficient way. Simple syntax, expressions, statements, concurrent and sequential
programming is also necessary while describing the electronics circuits. All these features
can be obtained by using a hardware description language. Now while comparing HDL and
C languages, the major difference is that HDL provides the timing information of a design.
Verilog
OR GATE EXAMPLE
Using Structural Modeling: In this model, the component represents another design
module.
endmodule
What is VHDL?
VHDL is an HDL that helps to describe circuits in digital systems. A hardware module in
VHDL is called an entity. The syntax is as follows. The entity starts with “entity” and ends
with “end” keyword.
entity <entity_name> is
port declaration;
end entity_name;
Definition
Verilog is an HDL used to model electronic systems while VHDL is an HDL used
in electronic design automation to describe digital and mixed-signal systems such
as field programmable gate arrays and integrated circuits.
Base Language
The main difference between Verilog and VHDL is that Verilog is based on C
language while VHDL is based on Ada and Pascal languages.
Case Sensitive
Moreover, one other difference between Verilog and VHDL is that Verilog is case
-sensitive while VHDL is not case sensitive.
Xilinx Tools is a suite of software tools used for the design of digital circuits
implemented using Xilinx Field Programmable Gate Array (FPGA) or Complex
Programmable Logic Device (CPLD). The design procedure consists of
(a) Design entry
(b) Synthesis and implementation of the design
(c) Functional simulation
(d) Testing and verification.
Digital designs can be entered in various ways using the above CAD tools: using a
schematic entry tool, using a hardware description language (HDL) – Verilog or VHDL
or a combination of both. In this lab we will only use the design flow that involves the
use of Verilog HDL.
The CAD tools enable you to design combinational and sequential circuits starting with
Verilog HDL design specifications.
The steps of this design procedure are listed below:
1. Create Verilog design input file(s) using template driven editor.
2. Compile and implement the Verilog design file(s).
3. Create the test-vectors and simulate the design (functional simulation)
without using a PLD (FPGA or CPLD).
4. Assign input/output pins to implement the design on a target device.
5. Download bit-stream to an FPGA or CPLD device.
6. Test design on FPGA/CPLD device
A Verilog input file in the Xilinx software environment consists of the following
segments:
Header: module name, list of input and output ports.
Declarations: input and output ports, registers and wires.
Logic Descriptions: equations, state machines and logic functions.
End: endmodule
All designs for this lab must be specified in the above Verilog input format. Note that
the State diagram segment does not exist for combinational logic designs.
TEST BENCH
Xilinx Test bench Waveform Editor- Ideal for less complex simulation tasks,
recommended for use by comparatively new users.
VHDL: This language has its roots in Ada programming language. VHDL stands for
VHSIC (Very High Speed Integrated Circuit) Hardware Description Language used
primarily in electronic design automation. This is a strongly typed language.
Verilog: This is again an HDL that is used in digital electronic systems, analog circuits
and mixed signal circuits. Verilog is a loosely typed language but has efficient notation.
Device under Test (DUT): A device under test can be simply thought of as a replica of
the actual design or something that is a behavioral representation of a design.
Stimulus Only - Contains only the driver containing the stimulus driver and the Design
under test but does not contain any result verification.
Full Test bench -This test bench contains the stimulus driver, the correct results and
results for comparison.
Simulator specific - The name suggests that the test bench is written in a simulator specific
format.
Hybrid test bench -This is a blend of techniques from more than one test bench style.
Fast test bench -It is to optimize the speed of a test bench. This is written in a way so as
to provide the best speed from a simulation.
Module Declaration
The first line of a module declaration specifies the name and port list (arguments).
The next few lines specify the input type and width of each port. The default port
width is 1 bit.
Then the port variables must be declared wire, wand, . . . , reg. The default is wire.
Typically inputs are wire since their data is latched outside the module. Outputs
are type reg if their signals were stored inside an always or initial block
Syntax
Example
module add_sub (add, in1, in2, oot);
reg oot;
... statements ...
endmodule
2. Lumped Delay
Lumped delays are specified on a per module basis. They can be specified as a single
delay on the output gate of the module. The cumulative delay of all paths is lumped at one
location.
//Lumped Delay Model
module M (out, a, b, c, d);
output out;
input a, b, c, d;
wire e, f;
and a1(e, a, b);
and a2(f, c, d);
and #11 a3(out, e, f);//delay only on the output gate
endmodule
Lumped delays models are easy to model compared with distributed delays.
3. Pin-to-Pin Delays
Another method of delay specification for a module is pin-to-pin timing. Delays are
assigned individually to paths from each input to each output. Thus, delays can be
separatelyspecified for each input/output path. Pin-to-pin delays are also known as path
delays.
Specify Blocks
A delay between a source (input or inout) pin and a destination (output or inout) pin of
a module is called a module path delay. Path delays are assigned in Verilog within the
keywords specify and endspecify. The statements within these keywords constitute a
specify block.
//Pin-to-pin delays
module M (out, a, b, c, d);
output out;
input a, b, c, d;
The specify block is a separate block in the module and does not appear under any
otherblock, such as initial or always.
Edge-Sensitive Paths
An edge-sensitive path construct is used to model the timing of input to output delays, which
occurs only when a specified edge occurs at the source signal.
In this example, at the positive edge of clock, a module path extends from clock signal to
out signal using a rise delay of 10 and a fall delay of 8. The data path is from in to out, and
the in signal is not inverted as it propagates to the out signal.
The delays declaration can contain up to three values, such as rise, fall, and turn-off delays.
The time taken for the output of a gate to change from some value to 1 is called a
rise delay.
The time taken for the output of a gate to change form some value to 0 is called a
fall delay.
The time taken for the output of a gate to change from some value to high
impedance is called turn-off delay.
// Single delay specified - used for all three types of transition delays
or # (<delay>) o_1 (out, a, b);
// Two delays specified - used for Rise and fall transitions
or # (<rise>, <fall>) o_1 (out, a, b);
// Three delays specified - used for Rise, fall and Turn-off transitions
or # (<rise>, <fall>, <turn_off>) o_1 (out, a, b);
Every digital gate and transistor cell has a minimum, typical, and maximum delay
For example,
assign out1 = in1 & in2; // perform and function on in1 and in2 and assign the result to
out1assign out2 = ~ in1;
assign #2 z[0] = ~(ABAR & BBAR & EN); //perform the desired function and assign
theresult
For example, to describe an AND gate using dataflow, the code will look something like
this:
BEHAVIORAL MODELING
Behavioral modeling is used to describe complex circuits. It is primarily used to model
sequential circuits, but can also be used to model pure combinatorial circuits. The
mechanisms (statements) for modeling the behavior of a design are:
initial Statements
always Statements
A module may contain an arbitrary number of initial or always statements and may
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
containone or more procedural statements within them. They are executed concurrently
(i.e. to model parallelism such that the order in which statements appear in the model
does not matter) with respect to each other whereas the procedural statements are
executed sequentially (i.e. the order in which they appear does matter). Both initial and
always statements are executed at time=0 and then only always statements are executed
during therest of the time.
Syntax
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
if (sel ==
1) sel out
out =b; 0 a
1 b
else
out = a;
end
endmodule
In Verilog, most of the digital designs are done at a higher level of abstraction
like RTL. However, it becomes natural to build smaller deterministic circuits at
a lower level by using combinational elements such as AND and OR.
Modeling done at this level is called gate-level modeling as it involves gates and
hasa one to one relationship between a hardware schematic and the Verilog code.
The multiple-input gates are and, nand, or, nor, xor, and xnor whose
number ofinputs are two or more, and has only one output.
The multiple-output gates are buf and not whose output is one or more and has
onlyone input.
The language also supports the modeling of tri- state gates, including bufif0,
bufif1,notif0, and notif1. These gates have one input, one control signal, and
one output.
Syntax
Following is the basic syntax for each type of gates with zero delays, such as:
and | nand | or | nor | xor | xnor [instance name] (out, in1, ..., inN); // [ ] is optional
and |is selection
One can also have multiple instances of the same type of gate in one
constructseparated by a comma:
and [inst1] (out11, in11, in12), [inst2] (out21, in21, in22, in23), [inst3] (out31, in31,
in32,in33);
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
multiplexer, adder, encoder and decoder.
endmodule
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
INTRODUCTION TO LANGUAGE ELEMENTS:
Keywords are special identifiers reserved to define the language constructs. Keywords are
in lowercase.
Identifiers are names given to objects so that they can be referenced in the design.
Identifiers are made up of alphanumeric characters, the underscore (_), or the dollar sign
($). Identifiers are case sensitive. Identifiers start with an alphabetic character or an
underscore. They cannot start with a digit or a $ sign (The $ sign as the first character is
reserved for system tasks.
reg value; // reg is a keyword; value is an identifier
input clk; // input is a keyword, clk is an identifier
Blank spaces (\b), tabs (\t) and newlines (\n) comprise the whitespace. Whitespace is
ignored by Verilog except when it separates tokens. Whitespace is not ignored in strings.
Blank space (\b)
Tab (\t)
Newline (\n)
Comments
Comments can be inserted in the code for readability and documentation. There are two
ways to write comments. A one-line comment starts with "//". Verilog skips from that point
to the end of line. A multiple-line comment starts with "/*" and ends with "*/". Multiple-
line comments cannot be nested. However, one-line comments can be embedded in
multiple-line comments.
a = b && c; //This is a one-line comment
/* This is a multiple line comment
………………….
…………………..*/
/* This is /* an illegal */ comment */
/* This is //a legal comment */
Format
Sized numbers
General syntax: <size>’<base><number>
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
<size> number of bits (in decimal)
<number> is the number in radix <base>
<base> :
d or D for decimal (radix 10)
b or B for binary (radix 2)
o or O for octal (radix 8)
h or H for hexadecimal (radix 16)
Examples:
4’b1111
12’Habc
16’d255
Negative numbers
Put the sign before the <size>
Examples:
-6’d3 // legal format
4’d-2 // illegal
Two’s complement is used to store the value
Integer
An integer is a general purpose register data type used for manipulating quantities. Integers
are declared by the keyword integer. Although it is possible to use reg as a general-purpose
variable, it is more convenient to declare an integer variable for purposes such as counting.
The default width for an integer is the host-machine word size, which is implementation-
specific but is at least 32 bits. Registers declared as data type reg store values as unsigned
quantities, whereas integers store values as signed quantities.
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
delta = 2.13; // delta is assigned a value 2.13
end
integer i; // Define an integer i initial
i = delta; // i gets the value 2 (rounded value of 2.13)
Strings
A string is a sequence of characters that are enclosed by double quotes. The restriction on
a string is that it must be contained on a single line, that is, without a carriage return. It
cannot be on multiple lines. Strings are treated as a sequence of one-byte ASCII values.
"Hello Verilog World" // is a string
"a / b" // is a string
Logic Values
DATA TYPES:
NETS TYPES –
Represent connections between hardware components.
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
wor (wired-OR); // the value of a wor depend on logical OR of all the drivers connected
to it.
tri (three-state); // all drivers connected to a tri must be z, except one (which determines
the value of the tri).
Syntax
wire [msb:lsb] wire_variable_list;
Nets and reg data types can be declared as vectors (multiple-bit data) or as scalars (single-
bit data). If the width of a data variable is not declared, scalar is assumed.
Vector and scalar declarations-
Vectors can be specified by declaring the range of bit numbers with the variable name. The
form of the declaration is:
[<high#> : <low#>] <variable> ;
or [<low#>: <high#>] <variable> ;
Example
wire [7:0] BYTE; // declare 8-bit data.
reg [15:0] INFO; // declare 16-bit register.
reg [0:11] DATA; // declare 12-bit register
Note: The bit numbers can run in either direction but the left-hand number of the pair in
the brackets is always the most significant bit of the vector.
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
When a list of variables is given in a declaration, Verilog assumes that each variable is
sized according to the last size specification seen as that line is scanned. This allows a set
of same-sized vectors to be declared without explicitly specifying the size of each one.
PARAMETER
Parameters allow constants like word length to be defined symbolically in one place.
This makes it easy to change the word length later, by change only the parameter.
Syntax
parameter par_1 = value,
Example
parameter add = 2’b00, sub = 3’b111;
parameter n = 4;
...
Expression
An expression calculates a value from a set of operators and operands.
For its use in expressions, data type integer shares the same traits as the data type reg.
Descriptions of register usage apply to integers as well.
Syntax:
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
operand | operator operand | operand operator operand
Description:
number
net
register, integer
net bit-select
register bit-select
net part-select
register part-select
memory element
Operators
Operator Type
{ } concatenation
+ - * / arithmetic
% modulus
! logical negation
|| logical or
== logical equality
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
!= logical inequality
~ bit-wise negation
| bit-wise inclusive or
^ bit-wise exclusive or
^~ or ~^ bit-wise equivalence
| reduction or
~| reduction nor
^ reduction xor
~^ or ^~ reduction xnor
? : conditional
Example:
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
assign carry = in0 & in1;
endmodule
OPERATORS:
Unary ….. a = ~b;
Binary……a = b && c;
1. Arithmetic Operators
These perform arithmetic operations. The + and - can be used as either unary (-z) or
binary(x-y) operators.
Operators
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
2. Relational Operators
Relational operators compare two operands and return a single bit 1or 0. These
operators synthesize into comparators. Wire and reg variables are positive Thus (-
3’b001) = = 3’b111.However for integers -1< 6.
Operators
< (less than)
== (equal to)
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
!= (not equal to)
3. Bit-wise Operators
Bit-wise operators do a bit-by-bit comparison between two operands.
Operators
~ (bitwise NOT)
| (bitwise OR)
^ (bitwise XOR)
~^ or ^~ (bitwise XNOR)
4. Logical Operators
Logical operators return a single bit 1 or 0. They are the same as bit-wise operators only
forsingle bit operands. They can work on expressions, integers or groups of bits, and
treat all values that are nonzero as “1”. Logical operators are typically used in conditional
(if ... else)statements since they work with expressions.
Operators
! (Logical NOT)
|| (logical OR)
5. Reduction Operators
Reduction operators operate on all the bits of an operand vector and return a single-bit
value.These are the unary (one argument) form of the bit-wise operators above.
Operators
& (reduction AND)
| (reduction OR)
~| (reduction NOR)
^ (reduction XOR)
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
~^ or ^~ (reduction XNOR)
6. Shift Operators
Shift operators shift the first operand by the number of bits specified by the second
operand.Vacated positions are filled with zeros for both left and right shifts (There is
no sign extension).
Operators
<< (shift left)
7. Concatenation Operator
The concatenation operator combines two or more operands to form a larger vector.
Operators
{ } (concatenation)
8. Replication Operator
The replication operator makes multiple copies of an item.
Operators
{n{item}} (n fold replication of an item)
Operators
(cond) ? (result if cond true): (result if cond false)
Operator Precedence
Table shows the precedence of operators from highest to lowest. Operators on the same
level evaluate from left to right. It is strongly recommended to use parentheses to define
order of precedence and improve the readability of your code.
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade
T. Y. BSc. (SEM - V) EL 351: Paper I: UNIT 1: INTRODUCTION TO VERILOG, Notes by Mrs. M. E. Niphade