Verilog Quick Reference Card
Verilog Quick Reference Card
Cover design: Sam Starfas Printed by: Technical Printing, Inc. Santa Clara Copyright 1993, 94, 95 Automata Publishing Company
Preface
This is a brief summary of the syntax and semantics of the Verilog Hardware Description Language. The summary is not intended at being an exhaustive list of all the constructs and is not meant to be complete. This reference guide also lists constructs that can be synthesized. For any clarications and to resolve ambiguities please refer to the Verilog Language Referc ence Manual, Copyright 1993 by Open Verilog International, Inc. and synthesis vendors Verilog HDL Reference Manuals.
Quick Reference
for
UNIX is a registered trademark of AT&T Verilog is a registered trademark of Cadence Design Systems, Inc.
Verilog HDL
In addition to this book, the following HDL books are available from Automata Publishing Company: 1. 2. Digital Design and Synthesis with Verilog HDL Digital Design and Synthesis with VHDL
In addition to the OVI Language Reference Manual, for further examples and explanation of the Verilog HDL, the following text book is recommended: Digital Design and Synthesis With Verilog HDL, Eli Sternheim, Rajvir Singh, Rajeev Madhavan c and Yatin Trivedi, Copyright 1993 by Automata Publishing Company.
Rajeev Madhavan
For additional copies of this book or for the source code to the examples, see the order form on the last page of the book. This book may be reproduced or transmitted for distribution provided the copyright notices are retained on all copies. For all other rights please contact the publishers.
Automata Publishing Company 1072 S. Saratoga-Sunnyvale Rd, Bldg A107 San Jose, CA 95129 Phone: 408-255-0705 Fax: 408-253-7916
Released with permission from Automata Publishing Company San Jose, CA 95129
Printed in the United States of America 10 9 8 7 6 5 4 3 2 ISBN 0-9627488-4-6 Copyright 1993, 1994, 1995 Automata Publishing Company.
Quick Reference
for
Verilog HDL
1.0 Lexical Elements ....................................................................... 1 1.1 Integer Literals .............................................................. 1 1.2 Data Types..................................................................... 1 Registers and Nets ..................................................................... 2 Compiler Directives................................................................... 3 System Tasks and Functions...................................................... 4 Reserved Keywords................................................................... 5 Structures and Hierarchy ........................................................... 6 6.1 Module Declarations ..................................................... 6 6.2 UDP Declarations.......................................................... 7 Expressions and Operators ...................................................... 10 7.1 Parallel Expressions .................................................... 13 7.2 Conditional Statements ............................................... 13 7.3 Looping Statements..................................................... 15 Named Blocks, Disabling Blocks............................................ 16 Tasks and Functions................................................................. 16
Permission to use, copy and distribute this book for any purpose is hereby granted without fee, provided that (i) the above copyright notices and this permission notice appear in all copies, and (ii) the names of Rajeev Madhavan, Automata Publishing and AMBIT Design Systems may not be used in any advertising or publicity relating to this book without the specic, prior written permission of Rajeev Madhavan, Automata Publishing and AMBIT Design Systems. THE BOOK IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL RAJEEV MADHAVAN OR AUTOMATA PUBLISHING OR AMBIT DESIGN SYSTEMS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OF THIS BOOK.
7.0
8.0 9.0
10.0 Continous Assignments ........................................................... 18 11.0 Procedural Assignments .......................................................... 18 11.1 Blocking Assignment ................................................ 19 11.2 Non-Blocking Assignment ........................................ 19 12.0 Gate Types, MOS and Bidirectional Switches ........................ 19 12.1 Gate Delays ............................................................... 21 13.0 Specify Blocks......................................................................... 22 14.0 Verilog Synthesis Constructs ................................................... 23 14.1 Fully Supported Constructs....................................... 23 14.2 Partially Supported Constructs.................................. 24 14.3 Ignored Constructs .................................................... 25 14.4 Unsupported Constructs ............................................ 25 15.0 Index ........................................................................................ 27
All rights reserved. This document is intended as a quick R reference guide to the Verilog HDL. Verilog is a registered trademark of Cadence Design Systems, Inc.
Quick Reference for Verilog HDL wire resolves to x. A trireg net behaves like a wire except that when all the drivers of the net are in high impedance (z) state, then the net retains its last driven value. trireg s are used to model capaci-
tive networks.
wire net1 ; /* wire and tri have same functionality. tri is used for multiple drive internal wire */ trireg (medium) capacitor ; /* small, medium, weak are used for charge strength modeling */
Variable names have to start with an alphabetic character or underscore followed by alphanumeric or underscore characters. The only exception to this are the system tasks and functions which start with a dollar sign. Escaped identiers (identier whose rst characters is a backslash ( \ )) permit non alphanumeric characters in Verilog name. The escaped name includes all the characters following the backslash until the rst white space character.
A wand net or triand net operates as a wired and(wand), and a wor net or trior net operates as a wired or (wor), tri0 and tri1 nets model nets with resistive pulldown or pullup devices on them. When a tri0 net is not driven, then its value is 0. When a tri1 net is not driven, then its value is 1. supply0 and supply1 model nets that are connected to the ground or power supply.
wand net2 ; // wired-and wor net3 ; // wired-or triand [4:0] net4 ; // multiple drive wand trior net5 ; // multiple drive wor tri0 net6 ; tri1 net7 ; supply0 gnd ; // logic 0 supply wire supply1 vcc ; // logic 1 supply wire
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 (for example two gate outputs are tied together), then the net value is resolved according to its type. Net types
wire wand wor tri0 supply0 trireg tri triand trior tri1 supply1
Memories are declared using register statements with the address range specied as in the following example,
reg [15:0] mem16X512 [0:511]; // 16-bit by 512 word memory // mem16X512[4] addresses word 4 // the order lsb:msb or msb:lsb is not important
The keyword scalared allows access to bits and parts of a bus and vectored allows the vector to be modied only collectively.
wire /* a tri1 /* a vectored [5:0] 6-bit vectored vectored [5:0] 6-bit vectored neta; net */ netb; tri1 */
For a wire, if all the drivers have the same value then the wire resolves to this value. If all the drivers except one have a value of z then the wire resolves to the non z value. If two or more non z drivers have different drive strength, then the wire resolves to the stronger driver. If two drivers of equal strength have different values, then the 2
les. The directives start with a grave accent ( ) followed by some keyword. A directive takes effect from the point that it appears in the le until either the end of all the les, or until another directive that cancels the effect of the rst one is encountered. For example,
define OPCODEADD 00010
This denes a macro named OPCODEADD. When the text OPCODEADD appears in the text, then it is replaced by 00010. Verilog macros are simple text substitutions and do not permit arguments.
`ifdef SYNTH <Verilog code> endif
If SYNTH is a dened macro, then the Verilog code until endif is inserted for the next processing phase. If SYNTH is not dened macro then the code is discarded.
`include <Verilog file>
The code in <Verilog file> is inserted for the next processing phase. Other standard compiler directives are listed below:
resetall - resets all compiler directives to default values define - text-macro substitution timescale 1ns / 10ps - species time unit/precision ifdef, else, endif - conditional compilation include - le inclusion signed, unsigned - operator selection (OVI 2.0 only) celldefine, endcelldefine - library modules default_nettype wire - default net types unconnected_drive pull0|pull1, nounconnected_drive - pullup or down unconnected ports protect and endprotect - encryption capability protected and endprotected - encryption capability expand_vectornets, noexpand_vectornets, autoexpand_vectornets - vector expansion options remove_gatename, noremove_gatenames - remove gate names for more than one instance remove_netname, noremove_netnames - remove net names for more than one instance
// Net type declarations wire dl,dbl ; // parameter value assignment paramter delay1 = 3, delay2 = delay1 + 1; // delay2 // shows parameter dependance /* Hierarchy primitive instantiation, port connection in this section is by ordered list */ nand #delay1 n1(cf,dl,cbf), n2(cbf,clk,cf,rst); nand #delay2 n3(dl,d,dbl,rst), n4(dbl,dl,clk,cbf), n5(q,cbf,qb), n6(qb,dbl,q,rst); /***** for debuging model initial begin #500 force dff_lab.rst = 1 ; #550 release dff_lab.rst; // upward path referencing end ********/ endmodule
Abbrevation
? (xy)
// latch with async reset primitive latch (q, clock, reset, data); input clock, reset, data ; output q; reg q; initial q = 1b1; // initialization
R or r F or f P or p N or n * or (??) B or b
Any transition
binary dont care (0, 1)
endtable endprimitive
All operators associate left to right, except for the ternary operator ?: which associates from right to left. Relational Operators
Application
c = a ; // assign a to c c == a ; /* is c equal to a returns 1-bit true/false applies for 1 or 0, logic equality, using X or Z operands returns always false hx == h5 returns 0 */ c != a ; // is c not equal to // a, retruns 1-bit true/ // false logic equality a === b ; // is a identical to // b (includes 0, 1, x, z) / // hx === h5 returns 0 a !== b ; // is a not // identical to b returns 1// bit true/false
Operator
<
Application
a < b // is a less than b? // return 1-bit true/false a > b // is a greater than b? a >= b // is a greater than or // equal to b a <= b // is a less than or // equal to b
==
a = !b;
>
>= <=
!=
===
Arithmetic Operators Parentheses can be used to change the precedence of operators. For example, ((a+b) * c) Operator precedence Operator *
/
!==
Application
c = a * b ; // multiply a with b c = a / b ; // int divide a by b sum = a + b ; // add a and b diff = a - b ; // subtract b // from a amodb = a % b ; // a mod(b) &
Operator
+,-,!,~ (unary) *, / % +, - (binary) <<. >> <, < =, >, >= =, ==. != ===, !==
Precedence + Highest %
Application
Unary plus & arithmetic(binary) addition Unary negation & arithmetic (binary) subtraction b = &a ; // AND all bits of a b = |a ; // OR all bits b = ^a ; // Exclusive or all bits of a NAND, NOR, EX-NOR all bits to-gether c = ~& b ; d = ~| a; e = ^c ; bit-wise NOT, AND, OR, EX-OR b = ~a ; // invert a c = b & a ; // bitwise AND a,b e = b | a ; // bitwise OR f = b ^ a ; // bitwise EX-OR bit-wise NAND, NOR, EX-NOR c = a ~& b ; d = a ~| b ; e = a ~^ b ;
&, ~& ^, ^~ |, ~| ||
!
&& ||
~&, ~|, ~^
?:
Lowest
10
11
12
Quick Reference for Verilog HDL if .. else ...conditions example always @(rst)// simple if -else if (rst) // procedural assignment q = 0; else // remove the above continous assign deassign q; always @(WRITE or READ or STATUS) begin // if - else - if if (!WRITE) begin out = oldvalue ; end else if (!STATUS) begin q = newstatus ; STATUS = hold ; end else if (!READ) begin out = newvalue ; end end
Quick Reference for Verilog HDL casex statement example casex (state) // treats both x and z as dont care // during comparison : 3b01z, 3b01x, 3b011 // ... match case 3b01x 3b01x: fsm = 0 ; 3b0xx: fsm = 1 ; default: begin // default matches all other occurances fsm = 1 ; next_state = 3b011 ; end endcase
Application
a << 1 ; // shift left a by // 1-bit a >> 1 ; // shift right a by 1 c = sel ? a : b ; /* if sel is true c = a, else c = b , ?: ternary operator */ {co, sum } = a + b + ci ; /* add a, b, ci assign the overflow to co and the result to sum: operator is called concatenation */ b = {3{a}} /* replicate a 3 times, equivalent to {a, a, a} */
>> ?:
{}
casez statement example casez (state) // treats z as dont care during comparison : // 3b11z, 3b1zz, ... match 3b1??: fsm = 0 ; 3b1??: fsm = 0 ; // if MSB is 1, matches 3?b1?? 3b01?: fsm = 1 ; default: $display(wrong state) ; endcase
{{}}
end
13
14
15
Quick Reference for Verilog HDL task Example // task are declared within modules task recv ; output valid ; output [9:0] data ; begin valid = inreg ; if (valid) begin ackin = 1 ; data = qin ; wait(inreg) ; ackin = 0 ; end end // task instantiation always begin: MAIN //named definition if (!qfull) begin recv(new, newdata) ; // call task if (new) begin q[head] = newdata ; head = head + 1 ; end end else disable recv ; end // MAIN function Example
/* since only one net15 declaration exists in a given module only one such declarative continous assignment per signal is allowed */ wire #10 (atrong1, pull0) net15 = enable ; /* delay of 10 for continous assignment with strengths of logic 1 as strong1 and logic 0 as pull0 */
module foo2 (cs, in1, in2, ns); input [1:0] cs; input in1, in2; output [1:0] ns; function [1:0] generate_next_state; input[1:0] current_state ; input input1, input2 ; reg [1:0] next_state ; // input1 causes 0->1 transition // input2 causes 1->2 transition // 2->0 illegal and unknown states go to 0 begin case (current_state) 2h0 : next_state = input1 ? 2h1 : 2h0 ; 2h1 : next_state = input2 ? 2h2 : 2h1 ; 2h2 : next_state = 2h0 ; default: next_state = 2h0 ; endcase generate_next_state = next_state; end endfunction // generate_next_state assign ns = generate_next_state(cs, in1,in2) ; endmodule
16
17
18
Quick Reference for Verilog HDL force and release are also procedural assignments. However, they can force or release values on net data types and registers.
Gates Allows strengths Allows strengths No strengths No strengths, non resistive No strengths, resistive Allows strengths
and, nand, or, nor,xor, xnor buf, not buif0,bufif1 notif0,notif1 nmos,pmos,cmos, rnmos,rpmos,rcmos tran, tranif0, tranif1 rtran,rtranif0, rtranif1 pullup pulldown
The drive strengths for each of the output signals are Strength of an output signal with logic value 1
supply1, strong1, pull1, large1, weak1, highz1
Logic 0
supply0 strong0 pull0 large weak0 medium small highz0 Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0
Logic 1
supply1 strong1 pull1 large weak1 medium small highz1 Su1 St1 Pu1 La1 We1 Me1 Sm1 HiZ0
Strength
7 6 5 4 3 2 1 0
19
20
21
Verilog
Delay #(delay) #(delay, delay)
Model
min:typ:max delay rise-time delay, fall-time delay, each delay can be with min:typ:max rise-time delay, fall-time delay and turn-off delay, each min:typ:max
disable
Synthesis Constructs
The following is a set of Verilog constructs that are supported by most synthesis tools at the time of this writing. To prevent variations in supported synthesis constructs from tool to tool, this is the least common denominator of supported constructs. Tool reference guides cover specic constructs.
function, endfunction if, else, else if input, output, inout wire, wand, wor, tri integer, reg macromodule, module parameter supply0, supply1 task, endtask
For trireg , the decay of the capacitive network is modeled using the rise-time delay, fall-time delay and charge-decay. For example,
trireg (large) #(0,1,9) capacitor // charge strength is large // decay with tr=0, tf=1, tdecay=9
Construct
*, /, %
Constraints when both operands constants, or 2nd operand power of 2. only edge-triggered events. bounded by static variables: only use + or - to index. only with always @ . Combinational and edge-sensitive user dened primitives are often supported. limitations on usage with blocking assignment. gate types supported without X or Z constructs
always for
Unsupported constructs Constructs which if used, may cause the synthesis tool to not accept the Verilog input or may cause different results between synthesis and simulation.
22
23
24
- NOTES -
- NOTES -
All rights reserved. Please send any feedback to the author. Verilog R is a registered trademark of Cadence Design Systems, Inc.
25
26
26b
Symbols
$display, $write 5 $fdisplay, $fwrite 5 $finish 5 $getpattern 5 $history 5 $hold, $width 5 $monitor, $fmonitor 5 $readmemb, $readmemh 5 $save, $restart, $incsave 5 $scale 5 $scope, $showscopes 5 $setup, $setuphold 5 $showvars 5 $sreadmemb/$sreadmemh 5 $stop 5 $strobe, $fstrobe 5 $time, $realtime 5 /* */ 1 // 1 autoexpand_vectornets 4 celldefine, endcelldefine 4 default_nettype 4 define 4 expand_vectornets 4 noexpand_vectornets 4 ifdef, else, endif 4 include 4 nounconnected_drive 4 protect, endprotect 4 protected, endprotected 4 remove_gatename 4 noremove_gatenames 4 remove_netname 4 noremove_netnames 4 resetall 4 signed, unsigned 4 timescale 4 unconnected_drive 4
C
case 14 casex 14 casez 14 compiler directives 3 continous assignments 18
O
Operator precedence 10
V
vectored 3
P
Partially Supported Synthesis Constructs 24 procedural assignments 18 pulldown 3 pullup 3
W
wait 16 wand 3 while 15 wire 2 wor 3
D
delays 21 disable 16
R
reg, register 2 Relational Operators 11 repeat 15 reserved words 5
X
x, X 1
E
Equality Operators 12 Escaped identifiers 1 Expressions 10
Z
z, Z 1
F
for 15 forever 15 fork ... join 13 Fully Supported Synthesis Constructs 23 function 16
S
scalared 3 Sequential edge sensitive UDP 9 Sequential level sensitive UDP 9 Shift, other Operators 13 specify block 22 specparam 22 String symbols 1 supply0 3 supply1 3 switch types 20 Synthesis Constructs 23 Synthesis Ignored Constructs 25 Synthesis Unsupported Constructs 25
G
Gate declaration 19 gate-types 19
I
if, if ... else 13 Integer literals 1 Identity Operators 12
P.O Number if any: _____________________________________ Charge my Visa/MC/AmExp. # ___________________________ Expires on: _____________________________________
Publication Qty-Price/copy 1-4 5-9 10-19 20- 44 45 - 99 100 - 500 1 (US$) 59.95 54.95 49.95 44.95 39.95 34.95 3.00 2 (US$) 65.95 60.95 54.95 49.95 44.45 39.00 3.00
T
task 16 tri0 3 tri1 3 triand 3 trior 3 trireg 3
L
Logical Operators 11
A
Arithmetic Operators 11
M
Memories 3 module 6
U
UDP 7 Unary Expression 10 Unary, Bitwise and Reduction Operators 12
B
Binary Expressions 10 blocking assignment 19
N
Named blocks 16 Nets 2 non-blocking assignments 19 27
Shipping/copy
28