Verilog
Verilog
Outline
§ HDL Languages and Design Flow
§ Connectivity in Verilog
§ Race conditions
§ UDPs
§ Electrical Properties
§ Introduction to PLI
HDL Languages and Design Flow
HDLs – WHAT-WHY-HOW
§ WHAT is a HDL?
§ The designers felt need for a flexible language that may help
the design process by giving a complete framework for design.
HDLs – WHAT-WHY-HOW
§ Behavioral/Architectural Design
§ Logic Design
§ Circuit Design
§ Physical Design
§ Manufacturing
System Specification
Functional (Architectural
Design)
Logic Design
Logic (Gate-level)
Representation Logic Verification
Circuit Design
Physical Design
Behavioral level
... CPU SubSystem
RTL
Gate level
Treansistor level
Implementation
adder subtr
Hardware Design Flow
§ HDLs and CAD tools are used to describe hardware for:
§ Simulation
§ Synthesis
§ Testing
§ Documentation
Verilog HDL Introduction
Verilog HDL - History
§ Invented by Phil Moorby & Prabhu Goel at Gateway Design
Automation Systems in 1983/84.
§ “Synthesis subset”
§ Can be translated using Synopsys’ Design Compiler or others
into a netlist.
§ Design written in Verilog.
§ Simulated to check functionality.
§ Synthesized (netlist generated).
§ Static timing analysis.
Levels of Abstraction
§ Verilog supports a design at 4
different levels of abstraction. Behavioral
Highest Abstraction
Level
§ Behavioral Level
§ Dataflow Level Dataflow
§ Gate Level
§ Switch level Gate Level
§ The <port list> is a list of input, inout and output ports which are
used to connect to other modules.
§ Identifier
§ A letter or _ can be followed by letters, digits, $ and _
§ Max 1024 characters
§ Numbers
§ [<sign>] [<size>] <base> <num>
§ e.g.- 549, ‘h8ff, ‘o765, 4’b11,3’b10x, -4’b11
Verilog Comments
Verilog supports 2 type of comment syntaxes
§ Single line comment start with //, and end with newline.
§ Block comment, start with /*, and end with */. Block comment
cannot be nested.
Example
/* Copyright Kacper Technologies Pvt Ltd, 2009
No unauthorized copying is allowed.
*/
input status; // 0:ready, 1:not ready
output data; // sync with clock mClock
Verilog Number Specifications
§ Two representations: sized & unsized
§ Format:<number of bits><base><number>
§ Different types: wire, wand, wor, tri, triand, trior, trireg, etc.
1 2 3 4 5 6 7 8 && A B C D E F G H
Examples
1 2 3 4 5 6 7 8 & A B C D E F G H
1 2 3 4 5 6 7 8
& & & & & & & &
A B C D E F G H
Reduction operators
§ Key symbols: &, ~&, |, ~|, ^, ~^, ^~.
§ The reduction operators are and, nand, or, nor, xor, xnor and an
alternative xnor. They take one operand and perform a bit-by-
next-bit operation, starting with the two leftmost bits, giving a 1-
bit result.
initial begin
a = 4'b1111;
b = 4'b0101;
c = 4'b0011;
$displayb(& a); // bitwise and, (same as 1&1&1&1)
// evaluates to 1
$displayb(| b); // bitwise or (evaluates to 1)
end
Reduction operation
& 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
§ The shift operators are shift left and shift right. The shift operator
takes a vector and a number indicating the shift.
module shiftTest;
reg [3:0] a;
initial begin
a = 4'b1010;
$displayb(a << 1); // shift left by 1, displays 0100
$displayb(a >> 2); //shift right by 2, displays 0010
end
endmodule // shiftTest
Conditional Operator
§ cond_expr ? true_expr : false_expr
§ A ternary operator
§ Acts like a 2-to-1 mux.
A
1
Y
Y = (sel)? A : B;
B 0
Y = A if sel is ‘1’
sel B if sel is 0
Concatenation Operator
§ {op1, op2, ..} → concatenates op1, op2, .. to single number.
§ Operands must be sized !!
…
reg a;
reg [2:0] b,c;
a = 1’b1,b = 3’b 010, c = 3’b 101;
catx = {a, b, c}; // catx = 1_010_101
caty = {b, 2’b11, a}; // caty = 010_11_1
catz = {b, 1}; // WRONG !!
…
Replication Operator
§ <no> { <variable/sized_number> }
§ <no> is an integer.
…
reg a;
reg [2:0] b,c;
a = 1’b1,b = 3’b 010, c = 3’b 101;
catr = {4{a}, b, 2{c}}; // catr = 1111_010_101101
…
Relational & Equality Operators
§ > → greater than
§ < → less than
§ >= → greater or equal than
§ <= → less or equal than
§ Result is one bit value: 0, 1 or x
§ == → logical equality
§ != → logical inequality Return 0, 1 or x
§ === → case equality
§ !== → case inequality Return 0 or 1
Logical && ||
Conditional ?: Lowest
Vectors
§ Vectors have multiple bits and are often used to represent buses.
§ Slice management
…
reg [2:0] bus_A;
reg [0:2] bus_B; bus_A[2] = bus_B[0];
initial begin = bus_A[1] = bus_B[1];
bus_A = bus_B; bus_A[0] = bus_B[2];
end
…
Vectors (Contd..)
§ Variable Vector Part Select
§ [<starting_bit>+ : <width>]
§ [<starting_bit>- : <width>]
…
reg [8*13:1] string_val; // can hold up to 13 chars
...
string_val = “Hello Verilog”;
string_val = “hello”; // MS Bytes are filled with 0
string_val = “I am overflowed”; // “I ” is truncated
…
Arrays
//Multi-dimensional arrays
§ Declaration:
reg <vector_width> <ary_name> <no_of_locations>;
…
reg [7:0] string_val [99:0]; // a memory with 100 elements
// each of 1 byte
reg [7:0] ray2d [4:0] [49:0]; // 2-dimentional array
reg [31:0] mem32 [`DEPTH-1:0]; // 32-bit memory
…
Connectivity in Verilog
Port assignments
§ Modules contain functional descriptions and have input, output,
and inout (bidirectional ports) for interfaces.
TB_TOP
dff
nand1 nand2
Hierarchical Naming (Contd..)
TB_TOP
dff1
Q, QB
nand1 nand2 D (Signals)
TB_TOP TB_TOP.nand1.o1
TB_TOP.dff1.Q TB_TOP.dff1.QB
TB_TOP.nand1
Modeling using Verilog
Gate Level Modeling
§ These are rarely used for in design work, but are used in post
synthesis world for modeling the ASIC/FPGA cells. These cells are
then used for gate level simulation or what is called as SDF
simulation.
§ The gate delay can be specified with only one delay time or rise
and fall times or with all 3 delay values.
Gate Delays (Contd..)
endmodule
Gate Level Modeling Examples (Contd..)
endmodule
Dataflow Modeling
§ The data flow between registers and the way that data gets
processes is modeled using dataflow modeling.
§ Syntax:
// 2:1 Multiplexer
module mux21(op, a, b, sel );
input [3:0] a, b;
input sel;
output op;
assign op = sel ? a : b;
endmodule
Behavioral Modeling
§ In RTL and Gate level implementation, the details of the
handshake mechanism between different processes are implied.
The states are cycle-to-cycle accurate.
overflow
acknowledge data
Behavioral Modeling (Contd..)
§ The behavioral model provides the ability to describe design
functionality in an algorithmic manner or in higher level of
modeling where behavior of logic is modeled.
initial begin
#10 a = 1; b = 0;
#10 a = 0; b = 1;
end
§ initial block
§ Executes only once.
§ always block
§ Executes repeatedly. It must have timing control,
otherwise it become INFINITE LOOPS
Procedural Blocks (Contd..)
§ Syntax:
type_of_block @(sensitivity_list)
statement_group: group_name
local_variable_declarations
timing_control procedural_statements
end_of_statement_group
module flawed_sr;
reg d1, d2, d3, d4;
always @(posedge clk) d2 = d1;
always @(posedge clk) d3 = d2;
always @(posedge clk) d4 = d3;
endmodule
1 a
a <= 1;
b <= a; “ b ”
c <= b;
c
Looping Flow Control
Verilog supports “for”, “while” and “repeat” and “forever” loop
§ The code that initiated a task has to wait for that task to
complete or disabled before continuing execution.
§ A task can have inputs and outputs, whereas function must have
at least one input and only one output. (which is the name of the
function itself)
Example of a task
task task_example;
input [1:0] in1, in2;
output [1:0] out1, out2;
#1 out1 = in1 & in2;
#1 out2 = in1 | in2;
endtask
Example of a function
function [1:0] function_example;
input [1:0] in1, in2;
function_example = in1 & in2;
endfunction
System Tasks
§ Verilog has some of the inbuilt task as part of the language itself.
module abc();
reg [3:0] a;
initial begin
a = 3;
$display(“Initializing….\nA = %d”, a);
monitor($time, “ \tA = %d”, a);
#300 $finish;
end
always #25 a = $random;
endmodule
Synchronization
Verilog support the following type of process synchronization
§ event
§ fork and join
§ disable
Synchronization - Event
§ # <expression>
suspends execution of the process for a fixed time period
§ @event-expression
suspends the execution of the process until the specified event
occurs
§ wait (expression)
suspends the execution of the process until the expression become
true
Synchronization (Contd..)
§ Verilog supports “event” data type.
module event_example;
event e1, e2;
…
endmodule
always begin
…
fork <statement 1> to <statement N>
<statement 1>; are executed in parallel
<statement 2>;
…
<statement N>;
join
<statement S>; <statement S> is executed only
end when <statement 1> to
<statement N> are completed
endmodule
Synchronization – disable
disable <block_name>
§ remove pending events from <block_name>
§ will not continue to execute the rest of the <block_name>
§ if <block_name> is in the always block, execution continues
from the start of the always block
…
always begin : write_block
if “write_through”is true,
<statement 1>;
all pending events (e.g.
…
out=#10 ram[index] from the
if ( write_through )
previous cycle) will be
disable write_block;
removed. Execution start from
out = #10 ram[index];
<statement 1>
end
…
Forever Statement
§ This loop executes continuously and never completes.
parameter byte_size = 8;
reg[byte_size-1:0] A;
§ Sequential UDPs take the value of the current inputs and the
current output to determine the value of the next output. The
value of the output is also the internal state of the UDP. Good
examples of sequential UDPs are latches and flip-flops.
Rules to Define UDP
§ UDPs can take only scalar input terminals (1 bit). Multiple input
terminals are permitted.
Clock
FSM Classification
Moore:
Output is a function of present state only that are synchronized
with the clock.
Input
Next state Output Output
Decoder Memory Decoder
Clock
Input
Next state
Decoder Output
Memory
Clock
FSM encoding
§ Binary
The number of storage devices (Flip-flops) is minimum.
§ Gray
If it is gray encoded, there will be only one switching
between
adjacent states. This reduces glitches at the outputs due
to
unequal delays of storage devices.
FSM encoding
§ One-hot
§ only one of the state variables will be ‘1’ and all others
will be ‘0’s for a state.
§ Complexity of Next state Decoder and Output Decoder is
reduced
§ Due to reduced complexity of Decoders , the speed of the
FSM (Max.clock frequency) is not limited by the
combinational logic. Hence Faster FSM.
§ Use “casex” for output and next state decoder
Modeling Mealy FSM
synthesis
RTL
Synthesis
§ Cell Name
Operating Condition § Cell Type
§ Cell Function
§ Cell Area
§ Cell Timing
Design Constraint § Cell Power
§ Cell Pin
§ Area § Cell Pin Loading
§ Timing § Cell design rule
§ Power § Wire Load Table
§ Design Rule Gate
§ DFT
Synthesis Process
§ Not all Verilog commands synthesize easily.
Example 1 Example 2
Only Put Latches If Necessary
§ Every time one executes a procedure all of the variables
defined anywhere in the procedure must be calculated.
§ If the procedure has several paths, every path must evaluate all
outputs. Else synthesis will insert latches to hold the old value
of those unevaluated outputs.
Method 1:
Set all outputs to some value at the start of the procedure.
Later on different values can overwrite those values.
always @(. . .
begin
x=0; y=0; z=0;
if (a) x=2; elseif (b) y=3; else z=4;
end
Method 2
Be sure every branch of every if and case generate every output.
always @(. . .
begin
if (a)
begin
x=2; y=0; z=0;
end
elseif (b)
begin
x=0; y=3; z=0;
end
else
begin x=0; y=0; z=4; end
end
end
Procedural synthesis
Logic Inference
Deciding what logic to synthesize from code is called inference.
always @
Can infer: flip-flops, latches, and/or combinational logic.
§ Operator reordering.
M=A.B
Y1=A.B.C+A.B.D
N=C+D
Y2=A.B+C+D
Y1=M.N
factorize
Y2=M+N
Gate Level Optimization
§ Gate level optimization consists of
§ Combinational mapping
§ Sequential mapping
§ It then looks at another local area with an overlap with the first
local area. If the optimization effort is increased then the
optimizer will look at a slightly larger area each time.
Mapped circuit
before gate level
optimization.
After gate level optimization
3 cells
14 transistors
3.5 equivalent gates
Verilog Coding Guidelines
Verilog Coding Guidelines
Guideline #1: When modeling sequential logic, use nonblocking
assignments.
Guideline #7: Use $strobe to display values that have been assigned
using nonblocking assignments.
0
1 1
Z X Z Z
1
X X
Signals (cont)
0
0 S0 Z
X
S1
1 SEL
X
X
S0 S1 SEL Z
1 X X 0 S0
1
X X X 1 S1
0 0 X 0
0 1 1 X 1
X
X
Signal Strength (cont)
Logic 0 strength Logic 1 strength
7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7
Su0 St Pu0 La0 We0 Me0 Sm HiZ HiZ Sm1 Me1 We La1 Pu1 St Su1
0 0 0 1 1 1
Su1(7)
Su1(7)
La0(4)
Verilog Syntax for Strength
Note:
Valid strength keywords are supply1, strong1, pull1, weak1,
highz1, supply0, strong0, pull0, weak0, highz0.
Support for transmission gate
rcmos, rpmos, rnmos, rtran, rtranif0,rtrainif1 reduce the output
strength as follow:
Back-annotation
Delay
Verilog SDF
Calculation
Physical
Information
Timing Delay Calculation
RC
network
Output Table
Delay skew Look-up
Load Load
Cell
Library Input SDF
Skew skew
Timing Delay - SDF Back-Annotation
Note:
$sdf_annotate() can be placed anywhere in the HDL code.
However, it make no sense to put it after time 0
Macros,
Conditional Compilation
&
Naming Convention
Macro
§ Macro names and the names used in the design are different
§ Macro definition is global. There is not scope restriction.
§ Macro can be re-define. The last definition read is used.
§ Keyword cannot be used as macro name. (It does not make sense).
§ One line comment (//) will be excluded in the text substituted.
§ Macro can also be defined in the command line. These macro has
the highest priority.
(e.g. verilog design.v +define+regressionSuit=“4” )
Macro Cont..
‘define WORD 8 //word size
‘define CLOCKMUX ssmux8
module register(…)
reg [‘WORD-1 : 0] cpu_reg;
…
‘CLOCKMUX hand_clockMux (…);
…
endmodule
Conditional Compilation
§ ‘ifdef, ‘else and ‘endif can be nested
§ Syntax for the ignored Verilog texts are not checked.
§ Verilog simulator does not keep record of the ignored text. Thus
any output (e.g. profiling and encryption) from the simulation does
not retain the full Verilog input.
module adder(a, b, c);
output [8:0] a; input [7:0] b, c;
reg [8:0] a;
always @ (b or c) begin
‘ifdef BEHAVIOURAL
a = b + c;
‘else
gateAdder #(8, 7, 7)i0 (a, b, c);
‘endif
end
endmodule
Good Naming Convention
Input Waveform
command
line
Simulation
Testbench Log
Key
Design
Log
File Output
$display(), $write() and $monitor() have a counterpart to write to a
specific file, beside the log file.
integer fileID;
initial begin
fileID = $fopen(“capture.dat”);
if (fileID == 0)
$finish;
$fdisplay(fileID, “Start Simulation %s”,
$pli_currentDateTime() );
end
Test bench
§ The following things are essentially required to be
provisioned a test bench.
§ timing control
§ input stimulus
§ device under test
§ reference model
§ diagnostic logging
§ assertion checking
Test bench – timing control
initial begin
#10 inputA = 1’b1;
#12 inputB = 1’b1; inputC=1’b0; inputD=1’b1;
#9 {inputA, inputB} = 2’b00;
end
initial begin
clock = 1’b0;
forever
#10 clock = !clock;
end
Waveform Probing using VCD
$dumpvars(<level>
Specify dump variable
<,<module or variable>>*);
$dumpfile(1, top.mod1);
§ Dump all variables in the module top.mod1.
$dumpfile(0, top.mod1);
§ Dump all variables in the module top.mod1, and in all module
instances below top.mod1 in the design hierarchy.
module top(reset);
input reset;
reg a;
always begin
if ( !reset ) begin // what happen when
#10 a = 0; // reset is 1
#10 a = 1;
end
end
endmodule
A more complicated zero delay loop
…
reg var;
parameter delay=10 Accelerated
assign #delay a = b; continuous
assign #(delay + 1) c = d; assignment
assign e = f & g;
clk1
clk2
unSyncD
clk2
syncD
The timing violation can “kill” the simulation. The “X”state get
propagated to the rest of the circuit, and the simulation
becomes meaningless.
unSyncD
sel
clk1
clk2
Introduction to PLI
Where PLI is Used
§ The Programming Language Interface (PLI) provides a set of
interface routines to read internal data representation, write to
internal data representation, and extract information about the
simulation environment. User-defined system tasks and
functions can be created with this predefined set of PLI interface
routines.
§ PLI is used to customize the capability of the Verilog language
by defining their own system tasks and functions for which
designers need to interact with the internal representation of
the design and the simulation environment in the Verilog
simulator.
Simulation Flow Using PLI routines
Generations of Verilog PLI
§ Task/Function (tf_) routines make up the first generation PLI.
These routines are primarily used for operations involving user-
defined task/function arguments, utility functions, callback
mechanism, and writing data to output devices.
§ Once the user-defined task has been linked into the Verilog
simulator, it can be invoked like any Verilog system task by
the keyword $hello_verilog. A Verilog module hello_top, which
calls the task $hello_verilog, is defined in file hello.v as shown
below:
module hello_top;
initial
$hello_verilog; //Invoke the user-defined task $hello_verilog
endmodule
§ Gate-level primitives
Boolean logic gates
§ User-defined primitives
Gates and sequential elements defined with truth tables
§ Continuous assignment
Modeling combinational logic with expressions
§ Logic synthesis
§ Translating Verilog (structural and behavioral) into netlists.
§ Register inference: whether output is always updated.
§ Logic optimization for cleaning up the result.
Little-used Language Features
§ Switch-level modeling
§ Much slower than gate or behavioral-level models.
§ Insufficient detail for modeling most electrical problems.
§ Delicate electrical problems simulated with a SPICE-like
differential equation simulator.
§ Delays
§ Simulating circuits with delays does not improve confidence
enough.
§ Hard to get timing models accurate enough.
§ Never sure you’ve simulated the worst case.
§ Static timing analysis has taken its place.
Verilog Strengths and Weaknesses
§ Verilog is widely used because it solves a problem
§ Good simulation speed that continues to improve.
§ Designers use a well-behaved subset of the language.
§ Makes a reasonable specification language for logic synthesis.
§ Logic synthesis one of the great design automation success
stories.
§ http://www.asic-world.com/verilog/.
§ Palnitkar, Samir, Verilog HDL: A Guide to Design and Synthesis
§ www.sunburst-design.com.
§ IEEE Standard Hardware Description Language Based on the
Verilog Hardware Description Language, IEEE Computer
Society, IEEE Std 1364-1995.
ThanQ