Professional Documents
Culture Documents
Verilog HDL Part II
Verilog HDL Part II
Verilog HDL Part II
FALL 2010
1
Hardware Description Language
2
A simple Verilog Example
// A simple example a
comment line c
b
module and2 (a, b ,c); module name
port list
input
p a,, b;;
port declarations
output c;
3
Module definition
port declarations
— Interface: consisting of port and parameter declarations
parameter declarations
`include directives
— optional add-ons
variable declarations
— body: specification of internal assignments
i t
part of the module low-level module instantiation
initial and always blocks
task and function
endmodule 4
Port declaration
Truth table
AND 1 0 X Z
a
b c 1 1 0 X X
0 0 0 0 0
X X 0 X X
Z X 0 X X
6
Signal Classification
7
Net declaration
A net declaration starts with keyword wire
…… addr
wire r_w; // scalar signal
Proceessor
Mem
wire [7:0] data; // vector signal data
mory
wire [9:0] addr; // vector signal
r_w
……
Proccessor
Meemory
wire r_w;
_ data[7:0]
[ ]
wire [7:0] data; status[3:0]
wire [9:0] addr; r_w
…… i_o[7:0]
endmodule
9
Register declaration
A register declaration starts with keyword reg
……
reg done; // scalar signal
reg [7:0] count; // vector signal
……
Registers can be used to describe the behavior of sequential circuits
10
Defining memory
A memory component can be defined using reg variables
Example:
……
reg [7:0] myMem [3:0]; // It defines a memory with 4 locations and each
// location contains an 8-bit data
Bit 7 6 5 4 3 2 1 0
myMem[0]
myMem[1]
myMem[2]
myMem[3]
11
Using parameters
The use of parameters make code easy to read and modify
Example:
……
parameter bussize = 8;
reg [bussize-1 : 0] databus1;
reg [bussize-1 : 0] databus2;
……
12
Predefined gate primitives
Verilog offers predefined gate primitives
— Multiple-input
p p ggates: and,, nand,, or,, xor,, xor,, xnor
e.g. a
b d
and (d, a, b, c)
c
e.g.
a
not (a, b, c) c
b
13
Predefined gate primitives
— tri-state gates: bufif1, bufif0, notif1, notif0
e.g. c
bufif1 (a, b, c) b a
c
e.g.
notif0 (a, b, c) a
b
14
Example of structural Verilog code
Example of using predefined gate primitives
15
User defined primitives
Verilog allows users to defined their own primitive
components, referred to as User defined primitives (UDPs)
16
Combinational UDPs
Example: 2-to-1 multiplexer
18
Sequential UDPs
Example: D Flip-Flop
Defining UDPs
— Cannot be defined
within modules.
modules
— Can be defined after
or before the module in
the same file.
— Can be defined
in a separate file
and use include
directive to include
to the code.
inout net
output net
21
Module instantiation
by name
25
Integer constants
Un-sized integer example
— 12 // decimal number 12
— `h12 // hex number 12 (18 decimal number)
—`o12 // octal number 12 (10 decimal number)
—`b1001
b1001 // binary number 1001 (9 decimal number)
26
Integer constants
Negative numbers
— Negative
N i numbers
b are represented
d iin 2’s
2’ complement
l form
f
— - 8`d12 // stored as 11110100
Use of ?, X, Z, _ characters
— 8`h1? // 0001ZZZZ
— 2`b1? // 1Z
— 4`b10XX // 10XX
— 4`b100Z // 100Z
— 8`b1010_0011 // 10100011
27
Arithmetic operators
Available operators: +, -, *, /, % (modulo)
28
Relation and equality operators
Available relational operators: <, <=, >, >=
— If any bit off an operandd is
i X or Z,
Z the
th result
lt will
ill be
b X
Example
Left Op.
Op Right Op.
Op === !== == !=
0110 0110 1 0 1 0
0110 0XX0 0 1 X X
0XX0 0XX0 1 0 X X
29
Logic operators
Logic operators:
— && (logic and), || (logic or), ! (logic not)
Operand A Operand B A&B A|B !A !B
1010 00 0 1 0 1
1010 011 1 1 0 0
Reducation
R d ti operators:
t
— & (and), ~& (nand), | (or), ~| (nor), ^ (xor), ~^ (xnor)
Operand A &A ~&A |A ~|A ^A ~^A
1010 0 1 1 0 0 1
30
Shifter operators
<< : shift left
reg [3:0]
[3 0] A
A;
1 1 0 1 A << 2 0 1 0 0
reg [3:0] A;
1 1 0 1 A >> 2 0 0 1 1
31
Concatenation operators
Example
reg [7:0] A, B, Data;
reg c;
……
A = 10101101
10101101; BB= 00110011
00110011;
c = 0;
Data = {A[3:0], B[7:6], c, c}; // Data = 11010000
Data 1 1 0 1 0 0 0 0
c c
A[3:0] B[7:6]
32
Continuous assignment
Continuous assignment starts with keyword assign.
OR o
c
34
Adding
g delay
y to continuous assignment
g
Delay is added by # t after keyword assign, t is the number
of delayed
y time unit.
Time unit is defined by `timescale
Example
E l
`timescale 10ns/1ns // <ref_time_unit>/<time_precision>
module buf1 (o,
(o i);
output o;
input i;
assign
i #3 o = 1;
1 // delay
d l for
f 3 time
i unit
i
endmodule
35
Behavioral blocks
In additional to assignment, other functional description codes are
included in two-type behavioral blocks:
initial blocks and always blocks
A module can have multiple blocks, but blocks cannot be nested.
When a block has multiple statements, they must be grouped using
begin and end (for sequential statements) or fork and join (for
concurrent statements)
statements).
36
Procedural assignment
Procedural assignment is used to assign value to variables.
37
Procedural assignment examples
38
Delay in procedural assignments
Delay specified in front of procedural assignment statements
((e.g.
g #3 a = b&c;)
;) delayy the execution of the entire statement.
Module delayTest;
integer a, b, c;
initial begin
a = 2; b = 3; Change a from 2 to 4
end after 3 time unit
initial #3 a = 4;
initial #5 c = a+b; Execution order:
endmodule 1. delayy
2. evaluation
3. assignment
Result: cc=77
39
Delay in procedural assignments
Delay specified right after = in procedural assignment statements
(e.g. a = #3 b&c;) just delay the assignment operation. The evaluation
off the
h right
i h hand
h d side
id expression
i isi executedd without
ih delay.
d l
Module delayTest;
integer a,
a b,
b c;
initial begin
a = 2; b = 3; Change a from 2 to 4
end after 3 time unit
initial #3 a = 4;
initial c = #5 a+b; Execution order:
endmodule 1 evaluation
1. l ti
2. delay
3. assignment
Result: c=5
40
Blocking assignments v.s. Non-blocking assignments
Blocking assignments use = as assignment symbol (previously discussed
procedural assignments). Assignments are performed sequentially.
initial begin
a = #1 1; // assignment at time 1
b = #3 0; // assignment at time 4 (3+1)
c = #6 1; // assignment at time 10 (6+3+1)
end
Non-blocking
Non blocking assignments use <= as assignment symbol
symbol. Non
Non-blocking
blocking
assignments are performed concurrently.
initial begin
#1 a < = 1;1 // assignment
i t att time
ti 1
#3 b <= 0; // assignment at time 3
#6 c <= 1; // assignment at time 6
end
41
Parallel blocks
Parallel block is a more flexible method to write concurrent statements.
It uses fork and join, instead of begin and end, in block description.
Parallel
ll l block
bl k
42
Event control statements
An event occurs when a net or register changes it value. The event can be
further specified
p as a rising
g edge
g ((by
ypposedge)
g ) or fallingg edge
g (by
( y negedge)
g g )
of a signal.
An event control statement always starts with symbol @
@ (posedge
( d clk)
lk) Q = D;
D // assignment will be performed whenever
signal clk has a rising edge (0Æ1, 0ÆX,
0ÆZ, XÆ1, ZÆ1)
43
Sensitivity list
Sensitivity list specifies events on which signals activating always blocks
46
(from ALDEC tutorial)
Multiple choice statements
Multiple choice statement starts with keyword case. It offers a more
readable alternative to nested if-else statements.
50
An FSM example
read_1_zero
Moor-type machine 0
0 1 read_2_zero 0
0
1
start_state 0 1 0
1 0
1
0 1 read_2_one 1
read_1_one
module moore_explicit
moore explicit (clock,
(clock reset,
reset in_bit,
in bit out_bit);
out bit);
input clock, reset, in_bit;
output out_bit;
reg [2:0] state_reg, next_state;
read_1_zero:
if (in_bit == 0) next_state <= read_2_zero; else
if (in_bit == 1) next_state <= read_1_one;
read_2_zero:
if (in_bit == 0) next_state <= read_2_zero; else
if (in_bit == 1) next_state <= read_1_one;
52
An FSM example
read_1_one:
if (in_bit == 0) next_state <= read_1_zero; else
if (in_bit == 1) next_state <= read_2_one;
read_2_one:
read 2 one:
if (in_bit == 0) next_state <= read_1_zero; else
if (in_bit == 1) next_state <= read_2_one;
assign out
out_bit
bit =((state
((state_reg
reg == read_2_zero)
read 2 zero) || (state
(state_reg
reg ==read
read_2_one))
2 one)) ? 1 : 0;
endmodule
53
Synthesizing registers
Assignment inside a clocked always block will be synthesized as DFFs.
— Latch-prone
L t h code
d — Latch-free code
55
Other synthesis tips
Nested if-else leads to lengthy mux-chain, which is normally slow. Using
case instead. However,, case results in mux with multiple
p inputs,
p , which
makes routing more difficult.
Assign values to all outputs in all cases (to avoid unwanted latches).
56
Verilog subroutines
57
Task example
Task declaration
task name
argument declaration
local variable
task body
function name
input declaration
local variable
Function call
reg [31:0]
[31 0] result;
lt
reg [3:0] data;
……
result = Factorial (data);
59
Creating testbench
Testbench is used to verify the designed circuit.
Simulation result
61