On-line Verilog HDL Quick Reference
Guide
by Stuart Sutherland of Sutherland HDL, Inc. - Portland, Oregon, USA
copyrighted material - do not reproduce
any portion by any means
professionally printed reference guides are available - see
www.sutherland.com for details
Verilog HDL constructs that represent hierarchy scope are:
begin
- end
or fork
- join
) Each scope has its own name space. An identifier name defined within a scope is unique to that scope. References to an identifier name will search first in the local scope, and then search upward through the scope hierarchy up to a module boundary.
The following Verilog HDL constructs are independent processes that are evaluated
concurrently in simulation time:
always and assign attribute begin buf bufif0 bufif1 case casex casez cmos deassign default defparam disable edge else end endattribute endcase endfunction |
endmodule endprimitive endspecify endtable endtask event for force forever fork function highz0 highz1 if ifnone initial inout input integer join medium module |
large macromodule nand negedge nmos nor not notif0 notif1 or output parameter pmos posedge primitive pull0 pull1 pulldown pullup rcmos real realtime |
reg release repeat rnmos rpmos rtran rtranif0 rtranif1 scalared signed small specify specparam strength strong0 strong1 supply0 supply1 table task time tran |
tranif0 tranif1 tri tri0 tri1 triand trior trireg unsigned vectored wait wand weak0 weak1 while wire wor xnor xor |
blanks, tabs, newlines (carriage return), formfeeds and EOF (end-of-file).
//
begins a single line comment, terminated
by a newline.
/*
begins a multi-line comment, terminated by
a */
.
Verilog is case sensitive.
a
-z A
-Z _
a
-z A
-Z 0
-9 _
and $
\
)
at the beginning of the identifier, and terminating with a white space.Examples |
Notes |
|
legal identifier name |
|
uppercase identifier is unique from xor keyword |
|
an escaped identifier (must be followed by a white space) |
The Verilog HDL has 4 logic values.
Logic Value |
Description |
|
zero, low, or false |
|
one, high, or true |
|
high impedance (tri-stated or floating) |
|
unknown or uninitialized |
The Verilog HDL has 8 logic strengths: 4 driving, 3 capacitive, and high impedance (no strength).
Strength Level |
Strength Name |
Specification Keyword |
Display Mnemonic |
||
7 |
Supply Drive |
|
|
|
|
6 |
Strong Drive |
|
|
|
|
5 |
Pull Drive |
|
|
|
|
4 |
Large Capacitive |
|
|
|
|
3 |
Weak Drive |
|
|
|
|
2 |
Med. Capacitive |
|
|
|
|
1 |
Small Capacitive |
|
|
|
|
0 |
High Impedance |
|
|
|
|
Syntax |
|
size' base value |
Sized integer in a specific radix (base) |
'
base
(optional) represents the radix. The default base is decimal.Base |
Symbol |
Legal Values |
binary |
or B |
0, 1, x, X, z, Z, ?, _ |
octal |
or O |
0-7, x, X, z, Z, ?, _ |
decimal |
or D |
0-9, _ |
hexadecimal |
or H |
0-9, a-f, A-F, x, X, z, Z, ?, _ |
?
is another way of representing the Z
logic value._
(underscore) is ignored (used to enhance
readability).Examples |
Size |
Base |
Binary Equivalent |
|
unsized |
decimal |
0...01010 (32-bits) |
|
unsized |
octal |
0...00111 (32-bits) |
|
1 bit |
binary |
1 |
|
8 bits |
hex |
11000101 |
|
6 bits |
hex |
110000 (truncated) |
|
6 bits |
hex |
001111 (zero filled) |
|
6 bits |
hex |
ZZZZZZ (Z filled) |
Syntax |
|
value. value |
decimal notation |
baseE exponent |
scientific notation (the E is
not case sensitive) |
Examples |
Notes |
|
must have value on both sides of decimal point |
|
3 times 104 (30000) |
|
5.8 times 10-3 (0.0058) |
Verilog HDL models are represented as modules.
Syntax |
Implicit Internal Connection module_name ( port_name, port_name, ... );
|
Explicit Internal Connection module_name
(. port_name ( signal_name ), . port_name ( signal_name ), ... );
|
macromodule
is a synonym for module
. Some EDA tools may optimize tool execution
performance by flattening macromodule hierarchy. Syntax |
port_direction [ port_size] port_name, port_name, ... ; |
port_direction is
declared as:
input
for scalar or vector input ports.output
for scalar or vector output ports.inout
for scalar or vector bi-directional
ports.port_size is a range from [
msb :
lsb ]
(most-significant-bit to least-significant-bit).
Examples |
Notes |
input a,b,sel; |
3 scalar ports |
output [7:0] result; |
little endian convention |
inout [0:15] data_bus; |
big endian convention |
input [15:12] addr; |
msb:lsb may be any integer |
parameter word = 32; |
constant expressions may be used |
Syntax |
register_type [ size] variable_name
, variable_name
, ... ; |
register_type [ size] memory_name
[ array_size]; |
net_type [ size] #( delay) net_name , net_name , ... ; |
net_type ( drive_strength) [ size] #( delay) net_name = continuous_assignment
; |
trireg ( capacitance_strength) [size] #( delay, decay_time) net_name, net_name, ... ; |
parameter constant_name = value, constant_name = value, ... ; |
specparam constant_name = value, constant_name = value, ... ; |
event event_name, event_name, ... ; |
delay (optional) may only be specified on net data types. The syntax is the same as primitive delays.
size is a range from [
msb :
lsb]
(most-significant-bit to least-significant-bit).
array_size is from [
first_address :
last_address]
.
first_address and last_address
must be integers, integer parameters, or an expression that resolves to integer.
strength (optional) is
specified as (
strength1,
strength0)
or (
strength0,
strength1)
. See Logic Strengths for keywords.
decay_time (optional)
specifies the amount of time a trireg net will store a charge after all drivers turn-off,
before decaying to logic X. The syntax is (
rise_delay,
fall_delay,
decay_time)
. The
default decay is infinite.
Keyword |
Functionality |
|
unsigned variable of any bit size |
|
signed 32-bit variable |
|
unsigned 64-bit variable |
|
double-precision floating point variable |
Register data types are used as variables in procedural blocks.
Keyword |
Functionality |
wire or tri |
Simple interconnecting wire |
wor or trior |
Wired outputs OR together |
wand or triand |
Wired outputs AND together |
tri0 |
Pulls down when tri-stated |
tri1 |
Pulls up when tri-stated |
supply0 |
Constant logic 0 (supply strength) |
supply1 |
Constant logic 1 (supply strength) |
trireg |
Stores last value when tri-stated (capacitance strength) |
Net data types connect structural components together.
Other Types |
Functionality |
|
Run-time constant for storing integers, real numbers, time, delays, or ASCII strings. Parameters may be redefined for each instance of a module. |
|
Specify block constant for storing integers, real numbers, time, delays or ASCII strings |
|
A momentary flag with no logic value or data storage. Often used for synchronizing concurrent activities within a module. |
Data Type Examples |
Notes |
wire a, b, c; |
3 scalar nets |
tri1 [7:0] data_bus; |
8-bit net, pulls-up when tri-stated |
reg [1:8] result; |
an 8-bit unsigned variable |
reg [7:0] RAM [0:1023]; |
a memory array; 8-bits wide, with 1K of addresses |
wire #(2.4,1.8) carry; |
a net with rise, fall delays |
wire (strong1,pull0) sum = a+b; |
net with drive strength and a continuous assignment |
trireg (small) #(0,0,35) ram_bit; |
net with small capacitance and 35 time unit decay time |
Syntax |
Port Order Connections module_name instance_name
|
Port Name Connections module_name instance_name
|
Explicit Parameter Redefinition
|
Implicit Parameter Redefinition module_name |
A module may be instantiated using port order or port names.
instance_name (required) is used to make multiple instances of the same module unique from one another.
instance_array_range
(optional) instantiates multiple modules, each instance connected to separate bits of a
vector.
[
lhi:
rhi]
(left-hand-index to right-hand-index).Parameters in a module may be redefined for each instance.
defparam
statement with the parameter's hierarchical name.#
token as part
of the module instantiation. Parameters must be redefined in the same order they are
declared within the module.
Module Instance Examples |
|
|
Array of Instances Example |
|
|
Syntax |
gate_type ( drive_strength) #( delay) instance_name [ instance_array_range] ( terminal, terminal, ... ); |
switch_type #( delay) instance_name [ instance_array_range]
( terminal, terminal, ... ); |
Terminal Order |
||
and |
nand xnor |
(1_output, 1-or-more_inputs) |
buf |
not |
(1-or-more_outputs, 1_input) |
bufif0 |
notif0 |
(1_output, 1_input, 1_control) |
pullup |
pulldown |
(1_output) |
user-defined-primitives | (1_output, 1-or-more_inputs) |
Terminal Order |
||
pmos |
rpmos |
(1_output, 1_input, 1_control) |
cmos |
rcmos |
(1_output, 1_input, n_control, p_control) |
tran |
rtran |
(2_bidirectional-inouts) |
tranif0 |
rtranif0 |
(2_bidirectional-inouts, 1_control) |
# delay or #( delay) Single delay for all output transitions |
#( delay, delay) Separate delays for (rising, falling) transitions |
#( delay, delay,
delay) Separate delays for (rising, falling, turn-off) transitions |
#( min_delay: typ_delay: max_delay) Minimum to maximum range of delays for all transitions |
#( min_delay: typ_delay: max_delay, min_delay: typ_delay: max_delay) Min. to max. range of delays for (rising, falling) transitions |
#( min_delay: typ_delay: max_delay, min_delay: typ_delay: max_delay, min_delay: typ_delay: max_delay) Min. to max. range of delays for (rising, falling, turn-off) transitions |
delay (optional) represents the propagation delay through a primitive. The default delay is zero. Integers or real numbers may be used.
strength (optional) is
specified as (
strength1,
strength0)
or (
strength0,
strength1)
Refer
to Logic Strengths for strength keywords.
instance_name (optional) may used to reference specific primitives in debugging tools, schematics, etc.
instance_array_range
(optional) instantiates multiple primitives, each instance connected to separate bits of a
vector.
[
lhi:
rhi]
(left-hand-index to right-hand-index).
Primitive Instance Examples |
Notes |
and i1 (out,in1,in2); |
zero delay gate primitive |
and #5 (o,i1,i2,i3,i4); |
same delay for all transitions |
not #(2,3) u7(out,in); |
separate rise & fall delays |
buf (pull0,strong1)(y,a); |
output drive strengths model ECL |
wire [31:0] y, a; |
array of 32 buffers |
Syntax |
type_of_block |
type_of_block is
either initial
or always
initial
procedural blocks process statements
one time.always
procedural blocks process statements
repeatedly.sensitivity_list (optional) is an event timing control that controls when all statements in the procedural block will start to be evaluated. The sensitivity list is used to model combinational and sequential logic behavior.
statement_group--end_of_statement_group is used to group two
or more procedural statements together and control the execution order.
begin
--end
groups two or more statements together sequentially, so that statements are evaluated in
the order they are listed. Each timing control is relative to the previous statement.fork
--join
groups two or more statements together in parallel, so that all statements are evaluated
concurrently. Each timing control is absolute to when the group started.group_name (optional)
creates a local scope in a statement group. Named groups may have local variables, and may
be disabled with the disable
keyword.
local_variable_declarations (optional) must be a register data type (may only be declared in named statement groups).
timing_control is used to control when statements in a procedural block are executed. Refer to Procedural Timing
procedural_statement is a procedural assignment to a register variable or a programming statement.
Procedural Block Examples |
Notes |
|
initial procedure executes statements one time; The fork--join group places statements in parallel. |
|
always procedure executes statements repeatedly. |
|
a statement group is not required when there is only one statement |
#
delay@(
edge signal or
edge signal or
... )
posedge
or negedge
. If no edge is specified, then any
logic transition is used.or
is used to specify events on any of several
signals.wait (
expression)
=
expression;begin--end
sequential
statement group, execution of the next statement is blocked until the assignment is
complete. In the sequence begin m=n; n=m; end
,
the 1st assignment changes m before the 2nd assignment evaluates m.<=
expression;begin--end
sequential statement group, execution of
the next statement is not blocked; and may be evaluated before the assignment is complete.
In the sequence begin m<=n; n<=m; end
, both
assignments will be evaluated before m or n changes.=
expression;<=
expression;=
timing_control expression;<=
timing_control expression;assign
register_data_type
=
expression;deassign
register_data_type;force
net_or_register_data_type
=
expression;release
net_or_register_data_type;if (
expression)
statement or statement_group
if (
expression) statement or statement_groupelse
statement or statement_group
case (
net_or_register_or_literal) case_match1: statement or statement_group case_match2, case_match3: statement or statement_groupdefault:
statement or statement_groupendcase
casez (
net_or_register_or_literal)casex (
net_or_register_or_literal)forever
statement or statement_grouprepeat (
number) statement or statement_groupwhile (
expression) statement or statement_groupfor (
initial_assignment; expression; step_assignment) statement or statement_groupdisable
group_name;Procedural Statement Examples |
|
|
|
|
Operators perform an operation on one or two operands:
operator
operand
operand
operator
operand
Usage |
Description |
|
Arithmetic Operators |
||
|
m + n |
Add n to m |
|
m - n |
Subtract n from m |
|
-m |
Negate m (2's complement) |
|
m * n |
Multiply m by n |
|
m / n |
Divide m by n |
|
m % n |
Modulus of m / n |
Bitwise Operators |
||
|
~m |
Invert each bit of m |
|
m & n |
AND each bit of m with each bit of n |
|
m | n |
OR each bit of m with each bit of n |
|
m ^ n |
Exclusive OR each bit of m with n |
|
m ~^ n |
Exclusive NOR each bit of m with n |
Unary Reduction Operators |
||
|
&m |
AND all bits in m together (1-bit result) |
|
~&m |
NAND all bits in m together (1-bit result) |
|
|m |
OR all bits in m together (1-bit result) |
|
~|m |
NOR all bits in m together (1-bit result) |
|
^m |
Exclusive OR all bits in m (1-bit result) |
|
~^m |
Exclusive NOR all bits in m (1-bit result) |
Logical Operators |
||
|
!m |
Is m not true? (1-bit True/False result) |
|
m && n |
Are both m and n true? (1-bit True/False result) |
|
m || n |
Are either m or n true? (1-bit True/False result) |
Equality Operators (compares logic values of 0 and 1 |
||
|
m == n |
Is m equal to n? (1-bit True/False result) |
|
m != n |
Is m not equal to n? (1-bit True/False result) |
Identity Operators (compares logic values of 0, 1, X and Z |
||
|
m === n |
Is m identical to n? (1-bit True/False results) |
|
m !== n |
Is m not identical to n? (1-bit True/False result) |
Relational Operators |
||
|
m < n |
Is m less than n? (1-bit True/False result) |
|
m > n |
Is m greater than n? (1-bit True/False result) |
|
m <= n |
Is m less than or equal to n? (True/False result) |
|
m >= n |
Is m greater than or equal to n? (True/False result) |
Logical Shift Operators |
||
|
m << n |
Shift m left n-times |
|
m >> n |
Shift m right n-times |
Miscellaneous Operators |
||
|
sel?m:n |
If sel is true, select m: else select n |
|
{m,n} |
Concatenate m to n, creating larger vector |
|
{n{m}} |
Replicate m n-times |
|
-> m |
Trigger an event on an event data type |
Operator Precedence |
|
(unary)
* / %
+ - (binary)
<< >>
< <= > >=
== != === !==
& ~&
^ ~^
| ~|
&&
||
?:
|
highest precedence lowest precedence |
Syntax |
Explicit Continuous Assignment net_type |
Implicit Continuous Assignment |
Continuous Assignment Examples |
|
|
|
Syntax |
|
Tasks are subroutines.
Example of a Task |
|
|
Syntax |
|
Functions return the value that is assigned to the function name.
[
msb:
lsb]
,
or the keywords integer or real. The default size is 1-bit.
Example of a Function |
|
|
Syntax |
|
specparam
param_name
=
value,
param_name =
value,
...;
Timing constraint checks are system tasks that model restrictions on input changes, such as setup times and hold times.
Timing Check Syntax |
$setup( data_event, reference_event, setup_limit, notifier); $hold( reference_event, data_event, hold_limit, notifier); $setuphold( reference_event, data_event, setup_limit, hold_limit, notifier); $skew( reference_event, data_event, skew_limit, notifier); $recovery( reference_event, data_event, limit,
notifier); $period( reference_event, period_limit, notifier); $width( reference_event, width_limit, width_threshold, notifier); |
(
input_port
polarity:
path_token output_port) = (
delay);
(
edge input_port path_token (
output_port polarity:
source)) = (
delay);
if (
first_condition)
simple_or_edge-sensitive_path_delayif (
next_condition)
simple_or_edge-sensitive_path_delayifnone
simple_path_delay
Delays |
Transitions represented (in order) |
1 |
all output transitions |
2 |
rise, fall output transitions |
3 |
rise, fall, turn-off output transitions |
6 |
rise, fall, 0->Z, Z->1, 1->Z, Z->0 |
12 |
rise, fall, 0->Z, Z->1, 1->Z, Z->0, 0->X, X->1, 1->X, X->0, X->Z, Z->X |
Specify Block Examples |
Notes |
(a => b) = 1.8; |
parallel connection path; one delay for all output transitions |
(a -*> b) = 2:3:4; |
full connection path; one min:typ:max delay range for all output transitions; b receives the inverted value of a |
|
different path delays for rise, fall transitions |
(a *> y1,y2) = (2,3,4,3,4,3); |
different delays for 6 output transitions |
(posedge clk => (qb -: d)) = (2.6, 1.8); |
edge-sensitive path delay; timing path is positive edge of clock to qb; qb receives the inverted value of data |
if (rst && pst) (posedge clk=>(q +:
d))=2; |
state-dependent edge sensitive path delay |
|
state-dependent path delays; an ALU with different delays for certain operations (default delay has no condition) |
Syntax |
|
User Defined Primitives define new primitives, which are used exactly the same as
built-in primitives.
input_logic_values :
output_logic_value ;
input_logic_values :
previous_state :
output_logic_value ;
Truth Table Symbol |
Definition |
|
logic 0 on input or output |
|
logic 1 on input or output |
|
unknown on input or output |
|
no change on output (may only be used with sequential UDPs) |
|
don't care if an input is 0, 1, or X |
|
don't care if and input is 0 or 1 |
|
input transition from logic v to
logic w e.g.: (01) represents transition from 0 to 1 |
|
rising input transition: same as (01) |
|
falling input transition: same as (10) |
|
positive input transition: (01) ,
(0X) or (X1) |
|
negative input transition: (10) ,
(1X) or (X0) |
|
any possible input transition: same as (??) |
UDP Examples |
|
|
Following is a list of Verilog HDL constructs supported by most synthesis tools.
Verilog HDL Constructs |
Notes |
module declarations | both module and macromodule keywords fully supported |
port declarationsinput output inout |
fully supported; any vector size supported |
net data typeswire wand wor |
scalars and vectors fully supported |
register data typesreg integer |
register variables:
|
parameter constants | limited to integers; parameter redefinition may not be supported |
module instances | fully supported; both port order and port name instantiation supported |
primitive instancesand nand or nor |
only gate primitives are supported |
assign continuous assignment |
fully supported; both explicit and implicit forms supported |
function definitions | may only use supported constructs; must be defined before being referenced |
task definitions | may only use supported constructs; must be defined before being referenced |
always procedural block |
must have a sensitivity list |
begin--end statement groups |
fully supported; named and unnamed blocks supported |
disable statement group |
must be used within the same named block that is being disabled |
= blocking procedural assignment<= non-blocking procedural assignment |
fully supported; may be restricted to using only one type of assignment for all assignments to the same register variable |
assign procedural continuous
assignment |
fully supported; the deassign keyword may not be supported |
integer values | fully supported; all sizes and bases |
if if-else decision statements |
logic X and Z only supported as don't care bits |
for loops |
the step assignment must be an increment or decrement (+ -) |
while loopsforever loops |
loop must take one clock cycle for each loop cycle (i.e.: an @(posedge clk) or @(negedge clk) must be within the loop) |
operators& ~& | ~| ^ ^~ ~^ |
operands may be:
|
vector bit selects vector part selects |
fully supported on the right-hand side of an assignment; restricted to constant bit or part selects on the left-hand side of an assignment |
EDA tool vendors and tool users may define tasks and functions specific to their tool,
such as text output or waveform displays.
Text Formatting Codes |
|||
%b %o %d %h %e %f %t |
binary values octal values decimal values hex values real values-exponential real values-decimal formatted time values |
%s %m \t \n \" \\ %% |
character strings hierarchical names print a tab print a newline print a quote print a backslash print a percent sign |
A zero in format codes (e.g.: %0d) displays the value using the minimum field width required. The %e and %f may specify the field width for both sides of the decimal point (e.g.: %5.2f) |
$monitor("
text_with_format_specifiers",
signal,
signal,
... );
$display("
text_with_format_specifiers",
signal,
signal,
... );
$write("
text_with_format_specifiers",
signal,
signal,
... );
$strobe("
text_with_format_specifiers",
signal,
signal,
... );
=
$fopen("
file_name");
$fclose(
mcd);
$monitor(
mcd,
"
text_with_format_specifiers",
signal,
signal,
... );
$display(
mcd,
"
text_with_format_specifiers",
signal,
signal,
... );
$write(
mcd,
"
text_with_format_specifiers",
signal,
signal,
... );
$strobe(
mcd,
"
text_with_format_specifiers",
signal,
signal,
... );
$time
$stime
$realtime
$timeformat(
unit,
precision,
"
suffix",
min_field_width);
|
|
|
|
|
Example: $timeformat(-9, 2, " ns", 10);
$printtimescale(
module_hierarchical_name);
$random(
seed);
$readmemb("
file_name",
register_array,
start,
end);
$readmemh("
file_name",
register_array,
start,
end);
$finish;
$stop;
Compiler directives provide a method for EDA tool vendors to control how their tool
will interpret Verilog HDL models.
`reset_all
`timescale
time_unit
base /
precision
base1 10
or 100
s ms us ns ps
or fs
Example: `timescale 1 ns / 10 ps
Indicates delays are in 1 nanosecond units with 2 decimal points of precision (10 ps is
.01 ns).
Note: There is no default timescale in Verilog; delays are simply relative numbers until a timescale directive declares the units and base the numbers represent.
`define
macro_name text_string`define
macro_name
(
arguments)
text_string (
arguments)
`
) each time the macro name is used.
Examples:
`define cycle 20 //clock period
always #(`cycle/2) clk = ~clk;
`define NAND(dval) nand #(dval)
`NAND(3) i1 (y,a,b);
`NAND(3:4:5) i2 (o,c,d);
`include "
file_name"
`ifdef
macro_name`else
`endif
Examples:
`ifdef RTL
wire y = a & b;
`else
and #1 (y,a,b);
`endif
`celldefine
`endcelldefine
`default_nettype
net_data_type`unconnected_drive pull1
`unconnected_drive pull0
`nounconnected_drive
`delay_mode_zero
`delay_mode_unit
`delay_mode_path
`delay_mode_distributed
`uselib file=
file
dir=
directory
libext=
extensionExamples:
`uselib file=/models/rtl_lib
ALU i1 (y1,a,b,op); //RTL model
`uselib dir=/models/gate_lib libext=.v
ALU i2 (y2,a,b,op); //Gate model
`uselib //turn off `uselib searching
On-line Verilog HDL Quick Reference
Guide
by Stuart Sutherland of Sutherland HDL, Inc. - Portland, Oregon, USA
copyrighted material - do not reproduce
any portion by any means
professionally printed reference guides are available - see
www.sutherland.com for details