Lecture 2 - Verilog 2022
Lecture 2 - Verilog 2022
Lecture 2: Verilog
Semester B, 2021 - 22
Lecturer: Mr. Deivis Hanan
Synopsys Confidential Information © 2020 Synopsys, Inc. 2
Primary Markets Served
FY21
Employees: Patents: Offices:
$ Revenue:
16,360+ 3,445+ 125
~$4.2B
#1 electronic design
Russia automation
Canada Armenia
Europe
China
S. Korea tools & services
USA Japan
Israel
India Taiwan Broadest IP portfolio and
Southeast Asia
Chile
#1 interface, foundation &
physical IP
DFF
DFF q
At every positive edge of clock:
d If reset is not low:
set q to the value of d
set qb to inverse of d
reset
HDL- Example
DFF
DFF q module DFF ();
d
reg q, qb;
At every positive edge of clock:
If reset is not low:
set q to the value of d
set qb to inverse of d
clock qb
Whenever reset goes low:
set q to 0
set qb to 1
endmodule
reset
HDL- Example
DFF
DFF q module DFF (clock, reset, d, q, qb);
d
input d, clock, reset;
output q, qb;
reg q, qb;
At every positive edge of clock:
If reset is not low: always @(posedge clock)
set q to the value of d if (~reset) begin
set qb to inverse of d q <= d;
qb <= ~d;
clock qb
Whenever reset goes low:
set q to 0
set qb to 1
endmodule
reset
HDL- Example
DFF
DFF q module DFF (clock, reset, d, q, qb);
d
input d, clock, reset;
output q, qb;
reg q, qb;
At every positive edge of clock:
If reset is not low: always @(posedge clock or negedge reset)
set q to the value of d if (~reset) begin
set qb to inverse of d q <= d;
qb <= ~d;
clock qb else begin
Whenever reset goes low: q <= 0;
set q to 0 qb <= 1;
set qb to 1 end
endmodule
reset
Simulation Algorithms
Simulation Algorithms
Different Level of Abstractions
• Architectural/Algorithmic
• A system is described in terms of the algorithms it performs
• Behavioral
• A system is described by the flow of data between its functional blocks
• Structural/Gate-Level
• Components are modeled by connecting primitives (or gates) for greater accuracy,
especially in timing
• Switch
• Components are modeled by the logic behavior of their transistor circuits for maximum
accuracy
Lexical Convention - Comments
Comments can be specified in two ways (exactly the same way as in C/C++):
➢ Begin the comment with double slashes (//). All text between these characters and the end of the line will be
ignored by the Verilog compiler.
➢ Enclose comments between the characters /* and */. Using this method allows you to continue comments on
more than one line. This is good for “commenting out” many lines code, or for very brief in-line comments.
Example
assign y = z;
a = c + d; // this is a simple comment
/* however, this comment continues more
than one line */
assign x = w /* plus its compliment*/ + t
Lexical Elements - Identifiers
➢ Identifiers are user-defined words for variables, function names, module names, block
names and instance names.
➢ Identifiers begin with a letter or underscore (Not with a number or $) and can include
any number of letters, digits and underscores. Identifiers in Verilog are case-sensitive.
Lexical Elements - Literals
Literals
Literals are constant-valued operands that can be used in Verilog expressions. The two common Verilog literals are:
(a) String: A string literal is a one-dimensional array of characters enclosed in double quotes (“ “) and cannot span
multiple lines.
(b) Numeric: constant numbers specified in binary, octal, decimal or hexadecimal.
Numeric literals can have underscores embedded in them for improved readability.
For example, Decimal literal 24_000
Lexical Elements - Reserved Keywords
always endmodule large reg
and endprimitive macromodule release
tranif0
assign endspecify nand repeat
tranif1
attribute endtable negedge rnmos
tri
begin endtask nmos rpmos
tri0
buf event nor rtran
tri1
bufif0 for not rtranif0
triand
bufif1 force notif0 rtranif1
trior
case forever notif1 scalared
trireg
casex fork or signed
unsigned
casez function output small
vectored
cmos highz0 parameter specify
wait
deassign highz1 pmos specparam
wand
default if posedge strength
weak0
defparam ifnone primitive strong0
weak1
disable initial pull0 strong1
while
edge inout pull1 supply0
wire
else input pulldown supply1
wor
end integer pullup table
xnor
endattribute join rcmos task
xor
endcase medium real time
endfunction module realtime tran
Hierarchical Modeling Concepts
Value Description
A Register stores its value from one assignment to the next and is used to model data storage elements.
Nets correspond to physical wires that connect instances. The default range of a wire or reg is one bit. Nets do not store
values and have to be continuously driven.
If a net has multiple drivers (i.e.: two gate outputs are tied together), then the net value is resolved according to its type.
Choosing the Correct Data Type
Module Boundary
Input Port Output Port
net
Inputs
Internally, input port must always be of the type net.
Externally, the inputs can be connected to a variable which is a reg or a net.
Outputs
Internally, output port must be of the type net or reg.
Externally, the outputs must always be connected to a net. They cannot be connected to a reg.
Inouts
Internally, inout port must always be of the type net.
Externally, the outputs must always be connected to a net. They cannot be connected to a reg.
Data Types – Net Types
Keyword Functionality
Pull1
a
Strong0
y
Strong0
b
Supply1 Supply1
Large0
Data Types – Register Types
Keyword Functionality
Integer
Integers are general-purpose variables. For synthesis they are mainly used for loops-indices, parameters, and
constants.
They are of implicitly of type reg.
However, they store data as signed numbers whereas explicitly declared reg types store them as unsigned.
If they hold numbers which are not defined at compile time, their size will default to 32-bits.
If they hold constants, the synthesizer adjusts them to the minimum width needed at compilation.
Real
They can be specified in decimal notation (e.g., 3.14) or in scientific notation (e.g., 3e6 ).
Their default value is 0. When a real value is assigned to an integer, the real number is rounded off to the nearest integer.
Time
Time is a 64-bit quantity that can be used in conjunction with the $time system task to hold simulation time.
Time is not supported for synthesis and hence is used only for simulation purposes.
Arrays are allowed in Verilog for reg, integer, time and vector register data types. Not allowed for real types
It is important not to confuse arrays with net or register vectors. A vector is a single element that is n-bits wide.
On the other hand, arrays are multiple elements that are 1-bit or n-bits wide.
reg [4:0] port_id [0:7]; // Array of 8 port_ids, each port_id is 5 bits wide
A parameter defines a constant that can be set when you instantiate a module. parameter end_bit = 1;
This allows customization of a module during instantiation. parameter delay = 5;
output [end_bit:0] q;
input [end_bit:0] data;
endmodule
Explicit Implicit
defparam stam1.delay = 3;
===
defparam stam1.end_bit = 2;
endmodule endmodule
Operators
• Unary expressions
➢ operator operand
• Binary expressions
➢ operand operator operand
•Ternary expressions
➢ operand operator operand operator operand
* Relational operators evaluate to a logical value of 1 if the expression is true and 0 if the expression is false
If there are any unknown or z bits in the operands, the expression takes a value x, except for === and !==
Assignments - Continuous Assignment
The continuous assignment is used to assign a value onto a wire in a module.
Continuous assignment is done with an explicit assign statement or by assigning a value to a wire during its declaration.
Note that continuous assignment statements are concurrent and are continuously executed during simulation.
The order of assign statements does not matter.
Any change in any of the right-hand-side inputs will immediately change a left-hand-side output.
Assignments - Regular Assignment Delay
The first method is to assign a delay value in a continuous assignment statement.
The delay value is specified after the keyword assign.
Any change in values in in1 or in2 will result in a delay of 10-time units before recomputation of the
expression in1&in2, and the result will be assigned to out.
in1
Procedural assignments are assignment statements used within Verilog procedures (always and initial blocks).
Only reg variables and integers (and their bit/part-selects and concatenations) can be placed left of the “=” in procedures.
The right-hand side of the assignment is an expression which may use any of the operator types.
Verilog Processes – Always and Initial
Always Block
Like the continuous assignment, it is a concurrent statement that is continuously executed during simulation.
This also means that all always blocks in a module execute simultaneously.
The always block can be used to imply latches, flip-flops or combinational logic.
If the statements in the always block are enclosed within begin ... end, the statements are executed sequentially.
If enclosed within the fork... join, they are executed concurrently (simulation only).
The always block is triggered to execute by the level, positive edge or negative edge of one or more signals
(separate signals by the keyword or).
A double-edge trigger is implied if you include a signal in the event list of the module DFF (clock, reset, d, q, qb);
always statement.
input d, clock, reset;
The single edge-triggers are specified by posedge and negedge keywords. output q, qb;
reg q, qb;
Blocking Assignments
Blocking assignments (=) are done sequentially in the order the statements are written.
A second assignment is not started until the preceding one is complete.
Delay in Assignment
In a delayed assignment Dt time units pass before the statement is executed and the left-hand assignment is made.
With intra-assignment delay, the right side is evaluated immediately but there is a delay of Dt before the result is place
in the left-hand assignment. If another procedure changes a right-hand side signal during Dt, it does not affect the
output.
Example
sum [7] = b[7] ^ c[7]; // executes now.
#10 d = e&f; /* 10-time units after ‘sum’ changes,
e&f is evaluated and ‘d’ changes. */
a = #15 b&c; /* b&c is evaluated now; ‘a’ changed
after 15-time units. */
Behavior Modeling – Procedural Assignments
Nonblocking Assignments
Nonblocking assignments (<=), which follow each other in the code, are done in parallel.
The right-hand side of nonblocking assignments is evaluated starting from the completion of the last
blocking assignment or if none, the start of the procedure.
b <= #3 a; //
x <= #6 b + c; /*
Behavior Modeling – Procedural Assignments
Application of Nonblocking Assignments
Nonblocking assignments are important in digital design and used as a method to model several concurrent
data transfers that take place after a common event.
Consider the following example where three concurrent data transfers take place at the positive edge of clock
If you really understood this example, what value reg1 gets? The newer or the older?
Behavior Modeling – Procedural Assignments
A good example
Just understand it, never mix blocking and non-blocking in the same procedure!
for Loops
Similar to for loops in C/C++, they are used to repeatedly execute a statement or block of statements.
If the loop contains only one statement, the begin ... end statements may be omitted.
Behavior Modeling – Loops
while Loops
The while loop repeatedly executes a statement or block of statements until the expression in the while
statement evaluates to false.
To avoid combinational feedback during synthesis, a while loop must be broken with an
@(posedge/negedge clock) statement. For simulation, a delay inside the loop will suffice.
Behavior Modeling – Loops
forever Loops
The forever statement executes an infinite loop of a statement or block of statements.
To avoid combinational feedback during synthesis, a forever loop must be broken with an
@(posedge/negedge clock) statement. For simulation, a delay inside the loop will suffice.
Behavior Modeling – Disable
disable
Execution of a disable statement terminates a block and passes control to the next statement after the
block.
It is like the C break statement except it can terminate any loop, not just the one in which it appears.
Disable statements can only be used with named blocks.
Behavior Modeling – Branching
if ... else if ... else
The if ... else if ... else statements execute a statement or block of statements depending on the result of the
expression following the if.
Both the
else if and else
statements are
optional.
Behavior Modeling – Branching
case
The case statement allows a multipath branch based on comparing the expression with a list of case choices.
Statements in the default block executes when none of the case choice comparisons are true.
Case choices may be a simple constant or expression, or a comma-separated list of same.
Behavior Modeling – Branching
casex
In casex the case choices in the statement may contain z, x or ? which are used as don’t cares for comparison. With
case the corresponding simulation variable would have to match a tri-state, unknown, or either signal. In short, case
uses x to compare with an unknown signal. casex uses x as a don’t care which can be used to minimize logic.
casez
casez is the same as casex except only ? and z (not x) are used in the case choice constants as don’t cares.
casez is favored over casex since in simulation, an inadvertent x signal, will not be matched by a 0 or 1 in the case choice.
Behavior Modeling – Timing Control
Delay Control
This specifies the delay time units before a statement is executed during simulation.
A delay time of zero can also be specified to force the statement to the end of the list of statements to be
evaluated at the current simulation time, this is used to eliminate race conditions.
Intra-Assignment Delay
This delay #D is placed after the equal sign. The left-hand assignment is delayed by the specified time
units, but the right-hand side of the assignment is evaluated before the delay instead of after the delay.
This is important when a variable may be changed in a concurrent procedure.
Behavior Modeling – Timing Control
Event Control, @
This causes a statement or begin-end block to be executed only after specified events occur. An event is a change in
a variable, and the change may be: a positive edge, a negative edge, or either (a level change), and is specified by the
keyword posedge, negedge, or no keyword respectively. Several events can be combined with the or keyword.
Event specification begins with the character @ and are usually used in always statements.
Behavior Modeling – Timing Control
Wait Statement
The wait statement makes the simulator wait to execute the statement(s) following the wait
until the specified condition evaluates to true.
In a continuous assignment, they are evaluated when any of its declared inputs change.
In a procedure, they are evaluated when invoked.
Functions are a good way to reuse procedural code, since modules cannot be invoked from a procedure.
Function Declaration
A function declaration specifies the name of the function, the width of the function return value, the function input
arguments, the variables (reg) used within the function, and the function local parameters and integers.
{temp, i[2:0]};
Tasks and Functions - Functions
Function Return Value
When you declare a function, a variable is also implicitly declared with the same name as the function name,
and with the width specified for the function name (The default width is 1-bit).
At least one statement in the function must assign the function return value to this variable.
Function Call
A function call must specify in its terminal list all the input parameters.
Function Rules
The following are some of the general rules for functions:
- Functions must contain at least one input argument.
- Functions cannot contain an inout or output declaration.
- Functions cannot contain time-controlled statements (#, @, wait).
- Functions cannot enable tasks.
- Functions must contain a statement that assigns the return value to the implicit function name register.
Tasks and Functions - Functions
Function Example
Tasks and Functions - Functions
A Function has only one output.
If more than one return value is required, the outputs should be concatenated
into one vector before assigning it to the function name.
Example
module simple_processor (instruction, outp);
input [31:0] instruction;
reg header;
reg [7:0] op1, op2;
function [16:0] decode_add;
input [31:0] instr;
reg add_header;
reg [7:0] opcode, opr1, opr2;
decode_add = {add_header, opr2, opr1}; //concatenated into 17-bits
endfunction
always @(instruction)
{header, op2, op1} = decode_add (instruction);
endmodule
Tasks and Functions - Tasks
A task is similar to a function, but unlike a function it has both
input and output ports. Therefore, tasks do not return values.
Tasks are similar to procedures in most programming
languages.
The syntax and statements allowed in tasks are those
specified for functions
Tasks and Functions – Tasks Vs. Functions
Tasks and Functions – Force and Release
The format string is like that in C/C++ and may contain format characters. Format characters include %d
(decimal), %h (hexadecimal), %b (binary), %c (character), %s (string) and %t (time). Append b, h, o to the task
name to change default format to binary, octal or hexadecimal.
$random
$random generates a random integer every time it is called.
If the sequence is to be repeatable, the first time one invokes random give it a numerical argument (a seed).
Otherwise, the seed is derived from the computer clock.
Compiler Directives
time_unit is the amount of time a delay of 1 represents. The time unit must be 1 10 or 100
base is the time base for each unit, ranging from seconds to femtoseconds, and must be: s ms us ns ps or fs
precision and base represent how many decimal points of precision to use relative to the time units.
Example: `timescale 1 ns / 10 ps
Indicates delays are in 1 nanosecond units with 2 decimal points of precision (10 ps is .01 ns).
Compiler Directives - `define
Conditional compilation.
`ifdef RTL
wire y = a & b;
`else
and #1 (y,a,b);
`endif
Design Validation
How to Build and Test a Module
How to Build and Test a Module
VCS® – Industry’s Best-in-Class Simulation Solution
20 years of Innovation and Leadership
Schematic Views
Source Code - Hierarchical
Views - Flattened
- Source code - Browse
folding - Editable
- Macro expansion
- Browse
- FSM extraction
- Tracing
Advanced Backtrace
- Driver/load
- Fan-in/out cone
- Connectivity
- Two-points
Waveform Views - Active trace
- Collapsible range - Auto-trace X
- Waveform
compare
- Switching analysis
- Annotations
- Memory values
- Tracing