分类:
2010-12-02 14:38:10
Operators perform an operation on one or two operands:
Unary expressions |
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 | |
| highest precedence lowest precedence |
Syntax |
Explicit Continuous Assignment |
Implicit Continuous Assignment |
Explicit continuous assignments require two statements: one to declare the net, and one to continuously assign a value to it. | |
Implicit continuous assignments combine the net declaration and continuous assignment into one statement. | |
may be any of the net data types except trireg. | |
(optional) may only be specified when the continuous assignment is combined with a net declaration. The default strength is (strong1, strong0). | |
(optional) follows the same syntax as . The default is zero delay. | |
may include any data type, any operator, and calls to functions. | |
Continuous assignments model combinational logic. Each time a signal changes on the right-hand side, the right-hand side is re-evaluated, and the result is assigned to the net on the left-hand side. | |
Continuous assignments are declared outside of procedural blocks. They automatically become active at time zero, and are evaluated concurrently with procedural blocks, module instances, and primitive instances. |
Continuous Assignment Examples |
|
|
|
Syntax |
|
Tasks are subroutines.
May have any number of inputs, outputs or inouts. | |
May contain timing controls (#, @, or wait). |
Example of a Task |
|
|
Syntax |
|
Functions return the value that is assigned to the function name.
Must have at least one input; may not have outputs or inouts. | |
May not contain timing controls (#, @, or wait). | |
(optional) is the return bit range as [ 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,
...;
specparams are constants used to store delays, delay calculation factors, synthesis factors, etc. | |
value may be interger, real, delay, or quoted string. |
Timing constraint checks are system tasks that model restrictions on input changes, such as setup times and hold times.
Timing Check Syntax |
$setup( , , , ); $hold( , , , ); $setuphold( , , , , ); $skew( , , , ); $recovery( , , , ); $period( , , ); $width( , , , ); |
Timing checks may only be used in specify blocks. | |
The is and edge of an input signal that establishes a reference point for changes on the data event. | |
The is the input signal that is monitored for changes. | |
The data_event and reference_event signals must be module input ports. | |
The and are delay values and use the same syntax as single . | |
The (optional) is a reg variable used as a flag. When a timing violation occurs, the model functionality can use the notifier flag to modify the model outputs. |
:
| |
: |
(
input_port (
output_port :
)) = (
);
(optional) may be either posedge or negedge. If not specified, all input transitions are used. | |
(optional) is the input port or value the output will receive. The source is ignored by most logic simulators, but may be used by timing analyzers. |
if (
)
simple_or_edge-sensitive_path_delayif (
)
simple_or_edge-sensitive_path_delayifnone
simple_path_delay
Allows different delays for the same path to be specified, based on other input conditions. | |
may only be based on input ports. | |
Most operators may be used with the condition, but should resolve to true/false (a logic X or Z is considered true; if the condition resolves to a vector, only the lsb is used). | |
Each state-dependent delay for the same path must have a different condition or a different edge-sensitive edge. | |
The ifnone condition (optional) may only be a simple path delay, and serves as a default if no other condition evaluates as true. |
Full connection path delay indicates every input bit may have a delay path to every output bit. | |
Parallel connection path delay indicates each input bit is connected to its corresponding output bit (bit 0 to bit 0, bit 1 to bit 1, ...) |
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.
All terminals must be scalar (1-bit). | |
Only one output is allowed, which must be the first terminal. | |
The maximum number of inputs is at least 9 inputs for a sequential UDP and 10 inputs for a combinational UDP. | |
Logic levels of 0, 1, X and transitions between those values may be represented in the table. The logic value Z is not supported with UDPs. | |
reg declaration (optional) defines a sequential UDP by creating internal storage. Only the output may be a reg. | |
initial (optional) is used to define the initial (power-up) state for sequential UDP's. Only the logic values 0, 1, and X may be used. The default state is X. |
input_logic_values :
output_logic_value ;
Combinational logic table entry. Only logic level values may be specified (0, 1, X and don't care). |
input_logic_values :
previous_state :
output_logic_value ;
Sequential logic table entry. May only be used when the output is also declared as a reg data type. Both input logic level and input logic transition values may be specified. |
A white space must separate each input value in the table. | |
The input values in the table must be listed in the same order as the terminal list in the primitive statement. | |
Any combination of input values not specified in the table will result in a logic X (unknown) value on the output. | |
Only one signal may have an edge transition specified for each entry in the table. | |
If an edge transition is specified for one input, the UDP becomes sensitive to transitions on all inputs. Therefore, all other inputs must have table entries to cover transitions, or when the transition occurs the UDP will output an X. | |
Level sensitive table entries have precedence over edge sensitive table entries. |
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.
This list is not specific to any one tool - each synthesis tool supports a unique subset of the Verilog language. | |
The constructs listed in this section represent a subset of the Verilog HDL that are supported by most synthesis tools. | |
Verilog constructs not listed in this section may be supported by some synthesis tools - refer to the specific synthesis tool documentation. |
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.
System tasks and functions begin with the $ (dollar sign). | |
Users may define additional built-in tasks and functions using the Verilog Programming Language Interface (PLI). | |
A few of the most common system tasks and functions are listed in this section. |
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(
);
$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,
... );
$time
$stime
$realtime
$timeformat(
unit,
precision,
"
suffix",
min_field_width);
unit is the base that time is to be displayed in, from 0 to -15 | |
precision is the number of decimal points to display. | |
suffix is a string appended to the time, such as " ns". | |
min_field_width is the minimum number of characters that will be displayed. |
|
|
|
|
|
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.
Compiler directives begin with the grave accent character ( ` ). | |
Compiler directives are not Verilog HDL statements; there is no semi-colon at the end of compiler directives. | |
Compiler directives are not bound by modules or by files. When a tool encounters a compiler directive, the directive remains in effect until another compiler directive either modifies it or turns it off. | |
A few of the most common compiler directives are listed in this section. |
`reset_all
`timescale
time_unit base /
precision basetime_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).
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
text_string`define
macro_name (
arguments)
text_string (
arguments)
text_string will be substituted in place of the macro_name where ever the macro name is used. | |
text_string is terminated by a carriage return. The string must be on one line. | |
arguments are evaluated before text is substituted. | |
macro_name must also be preceded by the grave accent mark ( ` ) each time the macro name is used. | |
Comments may be used. They are not substituted into the place of the macro name. |
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
`else
`endif
Examples:
`ifdef RTL
wire y = a & b;
`else
and #1 (y,a,b);
`endif
`celldefine
`endcelldefine
`default_nettype
`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