Verilog Tutorial
Verilog Tutorial
Verilog Tutorial
James Barnes
([email protected])
Outline
HDL Overview
Hierarchical Flattening
Boolean Flattening
Boolean Structuring
Mapping to Technology
FPGA synthesis
Hierarchical advantages
Manage complexity
Promote design reuse
Allow parallel development
Hierarchical features in verilog
modules
ports (connection to modules)
Structure of a module
module full_adder(ci,a,b,sum,cout);
// port declarations
input a,b,ci;
output sum,cout;
// type declarations.
wire a, b, ci, sum,cout;
// assignments
assign sum = a ^ b ^ ci;
assign cout = (a & b) | (a & ci) | (b & ci);
endmodule
Syntax notes
Statements end with ;
Compound statements (see later) are delimited by begin end (like { } in C).
Port directionality and width declared.
Variable types must be declared, as in other programming languages.
How to test?
top
Checker
module
Stimulus
block
Inputs
Device
under
test
(adder4)
Verification components
can be written in behavioral
verilog, systemC, Verisity
E language
adder4 must be written in
synthesizable verilog
(RTL)
Outputs
adder4 checker
`timescale 1ns/1ps
module check_adder(xci, xa, xb, xcout, xsum);
input [3:0] xa, xb;
input
xci;
input [3:0] xsum;
input
xcout;
wire [4:0]
wire
reg [4:0]
reg [4:0]
reg
Number representations
Sized numbers
1b1, 4b1010, 4b0x0z binary
X=unknown, z=high impedance
Other radixes
3o4 octal
4hE - hex
4d11 - decimal
The number in front of the represents the bit width of number
when expressed as a binary, regardless of the radix used
Verilog performs arithmetic on sized numbers using 2s
complement arithmetic.
If size parameter is omitted, defaults to max width (>-32b wide)
Integers
At least 32b wide (simulator-dependent)
Signed, i.e. can write -10
Real
27.3, 4.02e+03
Internal rep as 64b integer
Operators
Type
Width of
Result
Symbol
Arithmetic
>Ops width
+ - * / %
Logical
1 bit
! && ||
Bitwise
Ops width
~ & | ^ ~^
Relational
1 bit
Equality
1 bit
== != === !==
Reduction
1 bit
& ~& | ~| ^ ~^
Shift
Ops width
>> <<
Concatenate,
replicate
{ , , }
Conditional
Ops width
? :
4b0110 4b0111
yields 5b11111
Note: vacant
positions zerofilled.
{{ }}
wire
Used to represent connections between blocks
No memory value assigned in continuous assignment statement.
Right-hand side can be of reg (see below) or net type.
wire is most commonly used member of net class
reg
Scope
10
11
out1
sel
out
outbar
selb
b
out2
Operator Examples
wire [3:0] a = 4b0110;
wire [3:0] b = 4b0101;
t = a && b;
wire
wire
wire
w = &a; // Reduction op & yields w = 1b0; This tests for all ones
wire
12
Procedural Assignments
always
@() block
13
clk;
initial
begin
clk = 0;
forever
clk <= #clk_half_period ~clk; // This loop continues until something
// terminates the simulation
end
initial
begin
$monitor ($time, " clk=%b",clk);
#1000 $finish; // OK, enough already. Stop the simulation
end
endmodule // clk_gen
within always block are executed sequentially. In this case, because the
case conditions are mutually exclusive, order doesnt matter
14
No new value for sel=2b11. Simulation will use previous value and synthesis
tool will interpret that as an inferred latch. Some lint tools will catch this
Since out is a reg type, it will hold its previous value when ind changes.
Because this is such a common error, later versions of verilog allow a shorthand
notation always @(*) which will be filled in with the appropriate variables
15
out
16
reg out;
wire ina, inb, a, b;
always @(ina or inb or a or b)
begin
// if statements are not mutually exclusive
if (a) out = ina;
else if (b) out = inb;
else out = 1'b0;
end
ina
inb
1
out
1
1b0
0
0
1b0
ina
inb
inc
3
2
out
0
2
sel
17
18
Inferred D-flop
Synchronous reset
// Infer an 8b wide D-flop bank w/ synchronous rst
always @(posedge clk)
out <= rst ? 8'h00 : din;
Asynchronous reset
// Infer an 8b wide D-flop bank w/ asynchronous rst
always @(posedge clk or posedge rst)
if (rst)
out <= 8h00;
else
out <= din;
19
Each Verilog simulation time step is divided into a number of queues, which are
evaluated in order. The important ones are:
Time 0:
Q1 (in any order) :
Evaluate RHS of all non-blocking assignments
Evaluate RHS and change LHS of all blocking assignments
Evaluate RHS and change LHS of all continuous assignments
Evaluate inputs and change outputs of all primitives
Evaluate and print output from $display and $write
Q2 (in any order) :
Change LHS of all non-blocking assignments
(Remaining queues)
Evaluate and print output from $monitor and $strobe
Time 1:
...
Ref: Sutherland, 1996 International Cadence Users Conference.
20
clk
D
Q
clk
Combinational
Logic cloud
Q
clk
Rule:
code combinational logic with
continuous assignments
(wire) or blocking procedural
assignments (=)
code sequential logic using
non-blocking procedural
assignment (<=)
If the assignment rules are
followed, on the rising clk edge the
old values from the upper flops will
be propagated through the combo
logic cloud to the input to the lower
flop. Only then will the flops be
evaluated. NO delays need (or
should) be put into the modules.
21
22
23
$display, $monitor
$display prints one line per call. $monitor prints any time a variable
in its list changes.
$stop, $finish
$stop stops sim, but it can be restarted from that point.
$finish ends sim. In many cases, the sim will stop on its own
without a $finish, but if it contains an infinite loop (such as a clock
generator), $finish is needed.
Generic IO tasks $fopen, $fwrite, Used for saving simulation
data.The development environment usually handles this behind the
scenes.
$random generates a random number.
$readmemb, $readmemh
For initializing memory arrays from a file
24
parameter example
25
wire a, b;
reg gate_out;
always @(a or b)
gate_out <= #5 ~(a & b); // gate_out will change 5 units after a or b
26
27