02 Notes IntroductionToVerilog
02 Notes IntroductionToVerilog
Dr Alister Hamilton
2-1
Introduction
2-2
Computer-Aided Design (CAD)
2-3
CAD (continued)
Target technologies that are available:
2-4
Hardware Description Languages
(HDLs)
Many different HDLs used for a variety of purposes
Some suited to low level design (e.g. ABEL)
Two popular HDLs—VHDL and Verilog.
Verilog is an HDL used to describe the behavior and/or
structure of digital systems.
Verilog can describe a digital system at several
different levels—behavioral, data flow, and structural.
Verilog leads naturally to a top-down design
methodology.
Also system-level languages (hardware and software)
e.g. SystemC and System Verilog.
2-5
Verilog: The Module
Basic unit of hardware
Freestanding, cannot contain other module definitions.
A module may be instantiated within another module.
Basic mechanism for creating design hierarchy
2-6
A Simple Module
module Continuous (output StatOut, input StatIn);
endmodule
synthesis
9
Verilog AND-OR-INVERT module
// Note – Comments are written in the same
// style as C++ (block comments use /* */)
// Verilog description of a AND-OR-INVERT gate
assign W1 = A & B; A W1
assign W2 = C & D;
B
assign F = ~( W1 | W2); F
C
endmodule W2
D
endmodule
c_in
a
add4 sum
b
c_out
12
Design Hierarchy
modT
U1 out1
4
w1
in1 X Y
5 modA
K
F modB G out2
U3 E
U2
in2 M N
modC 8 w2
endmodule
Explicit association:
assign S = A^B^Ci;
assign Cy = (A&B) | (A&Ci) | (B&Ci);
endmodule
A FA
B Cy
Ci
S
endmodule
16
4-bit ripple carry adder schematic
17
4-bit adder test bench
A Add4
Ain S
initial B Sum
block Bin
C_in Co
Cin C_out
‘!=’
error
‘+’ check_sum
18
Test bench for 4-bit adder
‘timescale 1ns/1ns
module Test_Add4 (); // no ports needed
// input stimulus
reg [3:0] A, B;
reg C_in;
//output responses
wire [3:0] S;
wire C_out;
continued …
20
Test bench (continued)
endmodule
21
Verilog types
Nets (connections) Registers (storage)
wire
tri
supply0 reg
supply1 integer
wand real
wor time
tri0 realtime
tri1
triand
trireg
trior
module …
supply0 gnd;
supply1 vdd;
nand g1 (y, a, b, vdd); // tie one input of NAND high
endmodule
23
reg and integer
reg can be declared as a 1-bit object or as a
vector of any required size
i.e size not determined by host machine,
by default an unsigned quantity.
may be qualified as signed upon declaration e.g.
2-24
Verilog type reg and integer:
example
At the start of simulation, all regs assume the unknown state x, while all
wires assume the undriven state z. x is also used to represent the don’t
care condition in certain circumstances.
27
2-input AND module simulation
module valuedemo (output y, input a, b);
and g1(y, a, b);
endmodule
time (ns)
High impedance – dashed line
Unknown – filled rectangle 28
Numeric values in Verilog
Two types of number: sized and unsized.
Format of sized number:
<size> ’ <base> <number>
<size>: a decimal number, specifying number
length in binary bits.
If omitted, number is decimal format. Size implied by the
variable to which the number is being assigned.
The <base> can be one of the following:
Binary b or B 4’b0101 // 4-bit binary number
Hexadecimal h or H 12’hefd // 12-bit hex number
Decimal (default) d or D 16’d245 // 16-bit decimal no.
Octal o or O 9’o237 // 9-bit octal number
29
Primitive gates in Verilog
Basic logic gates
and, or, xor, not, nand, nor, xnor, buf
all have one output, but allow any number of inputs
Optional delays specifying propagation delay or rise-time
and fall-time may be included.
Three-state gates
bufif1, bufif0, notif1, notif0
all have one input, one control signal input and one tri-
state output.
2-30
Gate-level Verilog example
‘timescale 1 ns /1 ns
endmodule
Note that gate level delays are inertial i.e. input pulses with a duration shorter or
equal to the gate delay don’t produce a response at the gate output.
2-31
Verilog operators
continued …
32
Verilog operators (continued)
continued …
33
Verilog operators (continued)
Operator type Symbol Operation Operands
Bitwise ~ Bitwise NOT 1
& Bitwise AND 2
! Bitwise OR 2
^ Bitwise exclusive OR 2
^~ or ~^ Bitwise exclusive NOR 2
Reduction & Reduction AND 1
~& Reduction NAND 1
| Reduction OR 1
~| Reduction NOR 1
^ Reduction EXOR 1
^~ or ~^ Reduction EXNOR 1
continued …
34
Verilog operators (continued)
35
Verilog and C/C++ Differences
Verilog provides reduction operators that operate
on all the bits within a single word.
Additional ‘case’ equality/inequality operators are
used to handle high impedance (z) and unknown
values (x).
Curly braces ‘{‘ and ‘}’ used in concatenation and
replication operators instead of normal begin …
end block delimiters in Verilog.
36
Example continuous assignments
assign d = ~| a; // example #3
a[7]
a[3:0] c[3:0]
a[3] c[3] a[6]
c[2]
d a[5]
b[3]
a[2] a[4]
c[2] d
a[6]
b[2] a[3]
a[1]
c[1] Example #2 a[2]
a[1]
b[1]
a[0] a[0]
c[0]
Example #4
a[7:0] d
38
Behavioural Description: Sequential
Block
always block
Sequential statements that execute repetitively, usually in
response to trigger mechanism
Acts like a continuous loop that never terminates
Used to describe any type of digital hardware
initial block
Sequential statements that execute from beginning to end
once only
Commence at start of simulation at t=0
Used in test benches to provide test input stimuli
Does not describe synthesisable hardware
initial and always are parallel blocks
A module may contain any number of them
39
initial block clock generator
example
reg CLK;
initial
begin
CLK = 1’b0;
forever // an endless loop!
#(PERIOD/2) CLK = ~CLK;
end
2-40
always sequential block format
always @ (event_expression)
begin
// sequential statement 1
// sequential statement 2
…
end
Other forms:
always @ (input1 or input2 or input3) // or separated list
always @ (input1, input2, input3) // comma separated list
always @ (*) // wildcard event expression
42
Include all variables in
always statements
synthesis
47
RTL described in Verilog HDL
synthesis
Consider the following Verilog Model
combinational circuit:
A B C D Y1 Y2
Verilog HDL
description
Synthesis constraints:
Is speed or area
important for this
design?
FSM modelling with
Verilog-HDL:
Example: A circuit that could be in either a WRITE
state or READ state. If input SlowROM is received
from the ROM memory, then the circuit goes into a
wait state Delay.
The circuit has inputs Read and Write. While in the
READ state read is set to 1, whereas in WRITE state
write=1, otherwise they are set to zero.
2-52
Behavioral and Structural Verilog
(continued)
3 Model views:
Behavioral:
Specifies only the behavior at a higher level of abstraction.
Does not imply any particular structure or technology.
Structural:
Specifies more details.
Components used and the structure of the interconnection
between the components are clearly specified.
At a low level of abstraction.
Dataflow (Register Transfer Language [RTL]):
Data path and control signals are specified.
System is described in terms of the data transfer between
registers.
At an intermediate level of abstraction.
2-53
Synthesis
Synthesis is a set of transformations between two
model views (e.g. behavioural to structural):
Architectural synthesis
Logic synthesis
Circuit synthesis
2-54
Overview of Synthesis Levels
[Rabaey]
Architectural Level Logic Level Circuit Level
state
Behavioral View
0 a
(i: 1..16) :: b
sum = sum*z–1 +
2 1
coeff[i]*In*z–1 c x
3 tp
a
4
mem b
fsm x
c a 2
* 1 c
b 2
D
Architecture Synthesis
Identifying hardware resources (e.g., execution
units, memories, busses, controllers, etc.)
Binding the behavioral operations to resources
Scheduling the execution time of resources
2-57