Introduction To Verilog
Introduction To Verilog
Outline
• HDL Verilog Lexical elements
Data type representation
• Synthesis Verilog tutorial Structures and Hierarchy
Operators
• Synthesis coding Assignments
Control statements
guidelines Task and functions
Generate blocks
• Verilog - Test bench
• Fine State Machines
• References
HDL
VERILOG
What is HDL?
• Hard & Difficult Language?
– No, means Hardware Description Language
• High Level Language
– To describe the circuits by syntax and sentences
– As oppose to circuit described by schematics
• Widely used HDLs
– Verilog – Similar to C
– SystemVerilog – Similar to C++
– VHDL – Similar to PASCAL
Verilog
• Verilog was developed by Gateway Design
Automation as a proprietary language for logic
simulation in 1984.
• Gateway was acquired by Cadence in 1989
• Verilog was made an open standard in 1990 under the
control of Open Verilog International.
• The language became an IEEE standard in 1995
(IEEE STD 1364) and was updated in 2001 and
2005.
SystemVerilog
• SystemVerilog is the industry's first unified hardware
description and verification language
• Started with Superlog language to Accellera in 2002
• Verification functionality (base on OpenVera
language) came from Synopsys
• In 2005 SystemVerilog was adopted as IEEE
Standard (1800-2005). The current version is
1800-2009
IEEE-1364 / IEEE-1800
Verilog 2005 (IEEE
Standard 1364-2005)
consists of minor
corrections, spec
clarifications, and a few
new language features
SystemVerilog is a superset
of Verilog-2005, with many
new features and
capabilities to aid design-
verification and design-
modeling
Types of modeling
• Behavioral
– Models describe what a rst Counter
cnt
module does. clk
If (rst) cnt
= 0; else
cnt = cnt+1; [0:3]
– Use of assignment
statements, loops, if, else
kind of statements
• Structural
– Describes the structure of
the hardware components
– Interconnections of primitive
gates (AND, OR, NAND, NOR,
etc.) and other modules
Behavioral - Structural
Behavioral Structural
module c t e r ( module c t e r ( r s t , c l o c k , count ) ;
input r s t , clock, outp ut [ 1 : 0 ] count;
outp ut reg [ 1 : 0 ] count input r s t , clock;
); w ire N5, n1, n4, n5, n6;
always@(posedge c l o c k ) FFD U0 (.D(N5), .CP(cl oc k) ,
begin . Q ( c o u n t [ 0 ] ) , .QN(n6));
i f ( r s t ) count = 0 ; FFD U1 ( . D ( n 1 ) , .CP(cl o ck ) ,
else count = count +1; . Q ( c o u n t [ 1 ] ) , .QN(n5));
end MUX21U2 ( . A ( N 5 ) , . B ( n 4 ) ,
.S(n5), .Z(n1) ) ;
endmodule
NRU3 ( . A ( n 6 ) , . B ( r s t ) , . Z ( n 4 ) ) ;
NRU4 ( . A ( c o u n t [ 0 ] ) , . B ( r s t ) ,
.Z(N5));
endmodule
Simulation and Synthesis
• The two major purposes of HDLs are
logic simulation and synthesis
– During simulation, inputs are applied to a module,
and the outputs are checked to verify that the
module operates correctly
– During synthesis, the textual description of a
module is transformed into logic gates
• Circuit descriptions in HDL resemble code in
a programming language. But the code is
intended to represent hardware
Simulation and Synthesis
• Not all of the Verilog commands can
be synthesized into hardware
• Our primary interest is to build hardware, we will
emphasize a synthesizable subset of the
language
• Will divide HDL code into synthesizable
modules and a test bench (simulation).
– The synthesizable modules describe the hardware.
– The test bench checks whether the output results are
correct (only for simulation and cannot be
synthesized)
SYNTHESIS VERILOG
TUTORIAL
Outline
• Lexical elements
• Data type representation
• Structures and Hierarchy
• Operators
• Assignments
• Control statements
• Task and functions
Lexical elements
• Case sensitive - keywords are lower case
• Semicolons(;) are line terminators
• Comments:
– One line comments start with / / ...
– Multi-line comments start with /*and
end w i t h * /
• System tasks and functions start with a
dollar sign, ex $display, $signed
Lexical elements
• Variable names have to start with an
alphabetic character or underscore (_)
followed by alphanumeric or underscore
characters
• Escaped identifiers ( \ )
– Permit non alphanumeric characters in Verilog name
– The escaped name includes all the characters
following the backslash until the first white
space character
wire \fo+o=a ; / / Declare the va r a i b l e fo+o=a=a
wire \ f o +o =a ; / / Assign a t o wire fo+o
Compiler directives
• The directives start with
a grave accent ( ` ) `include “ f i l e 1 . v ”
/ / Used as `WORD_SIZE i n code
followed by some
` d e f i n e WORD_SIZE 32
keyword
`define module t e s t ( ) ;
Text-macro substitution ` i f d e f TEST
`ifdef, `ifndef, ` e ls e , / / A implementation
`endif `else
/ / B implementation
Conditional compilation
`endif
` i n c lu d e assi g n out = ` WORD_SIZ E{1 ’b 1 };
File inclusion endmodule
Reserved keywords
and always assign attribute begin buf bufif0 bufif1
case cmos deassign def ault defparam d i sa b l e else endattribute
end endcase endf unction e n d p r i mi ti ve endmodule endtable endtask event
for force f o r e ve r fork function highz0 highz1 if
initial inout input integer join large medium module
nand negedge nor not notif0 notif1 nmos or
output parameter pmos posedge primitive pulldown pullup pull0
pull1 rcmos reg release repeat rnmos rpmos rtran
rtranif0 rtranif1 scalared small sp e c i f y specparam strong0 strong1
supply0 supply1 table t a sk tran tranif0 tranif1 time
tri triand trior trireg tri0 tri1 vectored wait
wand weak0 weak1 w h i le wire wor
Outline
• Lexical elements
• Data type representation
• Structures and Hierarchy
• Operators
• Assignments
• Control statements
• Task and functions
Logical values
• A bit can have any of these values
– 0 representing logic low (false)
– 1 representing logic high (true)
– X representing either 0, 1, or Z
– Z representing high impedance for tri-
state (unconnected inputs are set to Z)
Logical values
• Logic with multilevel (0,1,X,Z) logic values
– Nand anything with 0 is 1
– Nand two get an X
• True tables define the how outputs are compute
| 0 1 X Z
& 0 1 X Z
0 0 1 X X
0 0 0 0 0
1 0 1 X X 1 1 1 1 1
X 0 X X X X X 1 X X
Z 0 X X X Z X 1 X X
Number representation
<size>'<base format><number>
• <size>:
– number of bits (optional)
• <base format>:
– It is a single character ' followed by one of the
following characters b, d, o and h, which stand for
binary, decimal, octal and hex, respectively.
• <number>
– Contains digits which are legal for the <base format>
– ‘_’ underscore can be use for readability
Number representation
• Negative numbers are store as 2’s
complement
• Extended number
– If MSB is 0, Xor Z number is extended to fill
MSBs with 0, X, Z respectively
3’b01=3’b001 3’bx1=3’bxx1 3’bz=3’bzz
always @(posedge Cl k)
i f (Read==1’b0) Storage[Adr] = I n b u s ;
else ObusReg = Sto r a g e [Adr ];
endmodule
Data storage and Verilog arrays
Counter
module c t e r ( i n p u t r s t , c l o c k , jmp,
i nput [ 7 : 0 ] jump,
output reg [ 7 : 0 ] count
);
always@(posedge c l o c k )
begin
if (rst) count = 8 ’h 0 0 ;
else i f ( jmp ) count = jump + c o u n t ;
else count = count + 8 ’ h 1 ;
end
endmodul e
Outline
• Lexical elements
• Data type representation
• Structures and Hierarchy
• Operators
• Assignments
• Control statements
• Task and functions
Structures and Hierarchy
• Hierarchical HDL structures are achieved
by defining modules and instantiating
modules
top.v TOP
co mp.v COMP
MUX
mux.v
Module declaration
module ALU ( o u t p u t [ 3 1 : 0 ] z ,
in put [ 1 5 : 0 ] A, B,
in put c lo c k , ena) ;
Port declaration
• i n p u t and i n o u t are declared as wires
• outputs port can be declared as reg
(holds a value)
• 2 flavors for port declaration:
module S1 ( a , b , c , d , e ) ; module S2 ( i n p u t [ 1 : 0 ] a , b ,
input [1 :0 ] a, b; input c,
input c ; output reg [ 1 : 0 ] d ,
output reg [ 1 : 0 ] d ; output e ) ;
output e ;
/ / Ve r i l o g 1995 St yl e //ANSI CSt yl e
endmodule endmodule
Parameters
• Parameters are means of giving
names to constant values
• The values can be overridden when the
design is compiled
• Parameters cannot be used as variables
• Syntax:
parameter <name> = <constant expression>;
Parameter declaration
• Default value need to be set at declaration
time
• 32 bit wide by default, but may be declared
of any width parameter [ 2 : 0 ] IDLE = 3 ’ d 0 ;
• 2 declaration flavors:
Inside a module In module header
module t e s t ( . . . I / O ’ s . . . ) module t e s t
parameter ASIZE = 32, BSIZE =16; #(parameter ASIZE = 32, BSIZE =16)
//… ( . . . I/O’s . . . ) ;
reg [ASIZE - 1 : 0 ] Abus, Zbus; //…
w ire [BSIZE-1:0] Bw ire; reg [ASIZE - 1 : 0 ] Abus, Zbus;
//… w ire [BSIZE-1:0] Bw ire;
endmodule //…
endmodule
Example
module Adder ( A , B, Cin, S,
A[N-1:0]
Cout, C l k ) ; Adder S[N-1:0]
parameter N=8; B[N-1:0]
N-
i n p u t [ N - 1 : 0 ] A , B;
i n p u t Cin; Cin bits Cout
i n p u t C l k ; outp ut (8 by
[ N - 1 : 0 ] S; outp ut default)
Cout; reg-ouputs
reg [ N - 1 : 0 ] S; Clk
reg Cout;
//module i n t e r n a l s module Adder #(parameter N=8)
endmodule ( i n p u t [ N - 1 : 0 ] A , B,
i n p u t Cin,
input Clk,
outp ut reg [ N - 1 : 0 ] S,
outp ut reg Cout
ANSI C style );
//module i n t e r n a l s
endmodule
Structures and Hierarchy
• Instance of a module
– Instantiation is the process of “calling” a module
– Create objects from a module template
<module name> #(<param l i s t > )
<instance name> ( < p o r t l i s t > ) ;
Where:
<module name> Module to be instantiated
<param l i s t > Parameters values passed to the instance
<instance name> Identifies the instance of the module
<port l i s t > Port list connection
Port list connections
• Ports
– Provide the interface by which a module
can communicate with the environment
– Port declarations ( input , output , inout )
input outp ut
net inout
net
Port connections/Parameter
overwrite
• Named connection
– Explicitly linking the 2 names for each side
of the connection
my_mod #(.W (1), . N ( 4 ) ) U1 ( . i n 1 ( a ) , . i n 2 ( b ) ,
.out(c));
• Order connection
– Expression shall be listed in the same order
as the port declaration
my_mod # ( 1 , 4 ) U2 ( a , b , c ) ;
Hierarchy example
% amodb = a % b / / a mod(b)
;
Logical operators
• Logical operators evaluate to a 1-bit value
&& a && b ; // is a and b true? returns 1-bit true/false
|| a || b ; // is a or b true? returns 1-bit true/false
! if (!a) c = b; // if a is not true assign b to c
/ * r e p l i c a t e a 3 t i me s,
{{}} b = {3{a}}
e q u i vale nt t o { a , a , a } * /
Operators precedence
Operator precedence
wire A;
assign A = (B|C)&D;
Continuous assignments
• Implicit continuous assignment
– Continuous assignment can be placed when
the net is declared
wire A = i 1 & i 2 ;
module AND( A , B, C) ;
i n p u t A, B;
output C;
A
C wire w;
/ / 2 NAND i n s t a n t i a t i o n s
B
NAND U1 ( A , B, w) ;
NAND U2 ( w, w, C) ;
endmodule
Procedural blocks
• Each procedural block represent a
separate activity flow in Verilog
• Procedural blocks
– always blocks
• To model a block of activity that is repeated continuously
– i n i t i a l blocks simulation only
• To model a block of activity that is executed at the
beginning
• Multiple behavioral statements can be
grouped using keywords begin and end
Procedural assignments
• Procedural assignment changes the state of a
reg
• Used for both combinational and
sequential logic inference
• All procedural statements must be within always
(or i n i t a l ) block
reg A;
always @ (B o r C)
begin
A = ~(B & C);
end
Always block – Event control (@)
• Always blocks model an activity that is
repeated continuously
• @can control the execution
– posdege or negedge make sensitive to edge
– @*/ @(*), are sensitive to any signal that may be
read in the statement group
– Use “ , ” / o r for multiple signals
Always block – Event control (@)
module M1( i n p u t B, C, c l k , r s t , output reg X, Y, Z ) ;
/ / c o n t r o l l e d by any value change i n B o r C
always @ (B o r C) X = B & C;
/ / Co n t r o lle d by p o s i t i v e edge o f c l k
always @(posedge c l k ) Y= B &
C;
R G
case (<expression>)
<alternative 1> : <statement 1>;
<alternative 2> : <statement 2>;
default : <default statement>;
endcase
Conditional statements (case)
always @(s, a, b, c, d)
case (s)
2'b00: out = a; always @*
2'b01: out = b; casex (state)
2'b10: out = c; /*
2'b11: out = d; during comparison : 3'b01z,
endcase 3'b01x, 3b'011 ... match case
3'b01x
*/
always @* 3'b01x: fsm = 0 ;
casez (state) 3'b0xx: fsm = 1 ;
// 3'b11z, 3'b1zz,... Match 3'b1?? default: fsm = 1 ;
3'b1??: fsm = 0; endcase
3'b01?: fsm = 1;
endcase
Example
module mux(a, b , c , d , s , o u t ) ;
input [ 7 : 0 ] a , b , c , d ;
a input [ 1 : 0 ] s ;
b output [ 7 : 0 ] o u t ;
8-bit out reg [ 7 : 0 ] out;
c 4-to- / / used i n procedural statement
1 always @ ( s o r a o r b o r c o r d )
d multiplexe case ( s )
r 2 ' b 00 : out = a ;
2 ' b0 1 : out = b ;
2 ' b1 0 : out = c ;
s[0 s[1 2 ' b 11 : out = d ;
] ] endcase
endmodul e
Latches / Muxes (Comb logic)
• Assuming only level sensitivity on a always block:
– A variable or signal when is fully specified (it is
assigned under all possible conditions) a mux
or combinational logic.
– If a variable or signal is not fully specified a latch
will be inferred
/ / Gener a l purpose lo op
interger i ;
always @*
f or ( i = 0 ; i < 7 ; i=i+1)
memory[i] = 0 ;
Loop statements (while)
• Loop execute until the expression is not true
always @*
wh i l e ( d e l a y)
/ / mu l t i p l e statement groups wi th begin-end
begin
ldlang = ol dl dl ang;
delay = delay – 1 ;
end
Loop statements (repeat)
• Repeat statement a specified number of times
• The number is evaluated only at the beginning
always @*
repeat(`BIT-WIDTH)
begin
i f ( a [ 0 ] ) out = b + o u t ;
a = a << 1 ;
end
Outline
• Lexical elements
• Data type representation
• Structures and Hierarchy
• Operators
• Assignments
• Control statements
• Task and functions
Tasks and Functions
Task and function serve the same purpose on
Verilog as subroutines do in C
Task: Functions:
• Declare with task and • Declare with f u n c t i o n
endtask and
• May have zero endfunction
arguments or more • Must have at lease one
arguments of type
input
input, output, i n o u t
• Do not return with a • Always return a single
value, can pass values value (cannot have
through output and output or i n o u t
i n o u t arguments arguments)
Tasks and Functions - example
module top (input a1, a2, output reg [1:0] b1, b2);
always @ (a1, a2)
begin
b1 = out (a1, a2); // function calling
out_task (a1, a2, b2); // task calling
end
function [1:0] out (input in1, in2); // Function Declaration
begin
if (in1) out = {in2,in1}; else out = {in1,in2};
end
endfunction
task out_task (input in1, in2, output [1:0] out); // Task Declaration
begin
if (in1) out = {in2,in1}; else out = {in1,in2};
end
endtask
endmodule
Task and Functions
• Functions are simpler
Function Task
• Data Sharing
– Functions and task could be declare as automatic
– A static function retains the value of all it's
internal variables between calls. An
automatic function re-initializes them each call
Tasks and Functions in Verilog
Why Tasks and Functions?