Chapter-3 verilogHDLUPD
Chapter-3 verilogHDLUPD
It is standard
Everybody will interpret the schematic the same way
It is not proprietary
HDLs are not tool specific
It is machine readable
It is easier for computers to understand the circuit
Only later on additional benefits were discovered
Simulation and Synthesis
Two Hardware Description Languages
Verilog
developed in 1984 by Gateway Design Automation
became an IEEE standard (1364) in 1995
More popular in US
VHDL (VHSIC Hardware Description Language)
Developed in 1981 by the Department of Defense
Became an IEEE standard (1076) in 1987
More popular in Europe
In this course we will use Verilog
Introduction and Basic Concept
Whatis Verilog?
Hardware Description Language(HDL)
Why use Verilog?
Because 60% of US companies use it.
Verilog 8
8/31/2007
EECS150 Lab Lecture #1
What’s an HDL?
Textual description of a circuit.
Human and machine readable.
Hierarchical.
NOT A PROGRAM
Describe what the circuit IS.
Not what is DOES.
Why Verilog?
Why use an HDL?
Describe complex designs (millions of gates)
Input to synthesis tools (synthesizable subset)
Design exploration with simulation
Why not use a general purpose language
Support for structure and instantiation
Support for describing bit-level behavior
Support for timing
Support for concurrency
Verilog vs. VHDL
Verilog is relatively simple and close to C
VHDL is complex and close to Ada
Verilog has 60% of the world digital design market (larger share in US)
Why Verilog?
Verilog HDL and Verilog-XL
Verilog HDL
Hardware design language that allows you to
design circuit.
Verilog-XL
High speed, event-driven simulator that reads
Verilog HDL and simulates the behavior of
hardware.
What is Verilog HDL? 12
VHDL represents another high level language for digital system design.
In this course we study Verilog HDL
reason:
used more often in electronic and computer industry
programming style is very similar to C programming language
Basic Design Methodology
14
Requirements
Synthesize
Gate-level
Model Simulate Test Bench
Timing
Model Simulate
Modeling Digital Systems
15
• Verilog HDL is for writing models of a system
• Reasons for modeling
– requirements specification
– documentation
– testing using simulation
– formal verification
– synthesis
• Goal
– most reliable design process, with minimum cost and
time
– avoid design errors!
Domains and Levels of Modeling
16
Structural Functional
high level of
abstraction
low level of
abstraction
“Y-chart” due to
Gajski & Kahn
Geometric
Domains and Levels of Modeling
17
Structural Functional
Algorithm
(behavioral)
Register-Transfer
Language
Boolean Equation
Differential Equation
“Y-chart” due to
Geometric Gajski & Kahn
Domains and Levels of Modeling
18
Structural Functional
Processor-Memory
Switch
Register-Transfer
Gate
Transistor
“Y-chart” due to
Gajski & Kahn
Geometric
Domains and Levels of Modeling
19
Structural Functional
Polygons
Sticks
Standard Cells
Floor Plan
HDL model specifies the relationship between input signals and output signals
HDL uses special constructs to describe hardware concurrency, parallel activity
flow, time delays and waveforms
endmodule
Verilog Example: Structural Models
25
• Structural models
– Are built from gate primitives and/or other modules
– They describe the circuit using logic gates — much as
you would see in an implementation of a circuit.
• Identify
– Gate instances, wire names, delay from a or b to f.
optional “many”
and #delay instance-name (out, in1, in2, in3, …);
How to build and test a module 27
initial begin
//monitor and display
module test (q, r);
…
output q, r;
initial begin
//drive the outputs with signals
…
Another view of this
29
• 3 chunks of verilog, one for each of:
Another piece of
hardware, called Your hardware
TEST, to generate called
interesting inputs DESIGN
Verilog Examples 30
Module testAdd generated inputs for module halfAdd and displayed
changes. Module halfAdd was the design
module testAdd(a, b, sum, cOut);
module tBench;
input sum, cOut;
wire su, co, a, b;
output a, b;
reg a, b;
halfAdd ad(su, co, a, b);
testAddtb(a, b, su, co);
initial begin
endmodule
$monitor ($time,,
“a=%b, b=%b, sum=%b, cOut=%b”,
a, b, sum, cOut);
module halfAdd (sum, cOut, a, b); a = 0; b = 0;
output sum, cOut; #10 b = 1;
input a, b; #10 a = 1;
#10 b = 0;
xor #2 (sum, a, b); #10 $finish;
and #2 (cOut, a, b); end
endmodule endmodule
The test module
31
• It’s the test generator
• $monitor module testAdd(a, b, sum, cOut);
input sum, cOut;
– prints its string when output a, b;
executed. reg a, b;
– after that, the string is initial begin
printed when one of the $monitor ($time,,
listed values changes. “a=%b, b=%b, sum=%b, cOut=%b”,
a, b, sum, cOut);
– only one monitor can be a = 0; b = 0;
active at any time #10 b = 1;
#10 a = 1;
– prints at end of current #10 b = 0;
simulation time #10 $finish;
end
endmodule
The test module (continued)
32
• Function of this tester
– at time zero, print values module testAdd(a, b, sum, cOut);
input sum, cOut;
and set a=b=0 output a, b;
– after 10 time units, set b=1 reg a, b;
– after another 10, set a=1 initial begin
– after another 10 set b=0 $monitor ($time,,
“a=%b, b=%b, sum=%b, cOut=%b”,
– then another 10 and finish a, b, sum, cOut);
a = 0; b = 0;
#10 b = 1;
#10 a = 1;
#10 b = 0;
#10 $finish;
end
endmodule
Other things you can do
33
• More than modeling hardware
– $monitor — give it a list of variables. When one of them changes, it prints the
information. Can only have one of these active at a time.
e.g. …
• $monitor ($time,,, “a=%b, b=%b, sum=%b, cOut=%b”,a, b, sum, cOut);
newline
automatically
– $display() — sort of like printf()
included
• $display (“Hello, world — %h”, hexvalue)
• Behavioral model
– More like a procedure in a programming language
– Still specify a module in Verilog with inputs and outputs...
– ...but inside the module you write code to tell what you want to have
happen, NOT what gates to connect to make it happen
– i.e., you specify the behavior you want, not the structure to do it
Always Synthesis
inst1
inst2
inst3
n g
pi
ap
m
Place and
Route
clb 1
clb 2
Levels of design description
Algorithmic level
Level of description
Register Transfer Level
most suitable for
synthesis
Logic (gate) level
Combinational
Logic
Combinational …
Logic
Registers
What is an FPGA?
Configurable
Logic
Block RAMs
Block RAMs
Blocks
I/O
Blocks
Block
RAMs
Hardware Description Languages
The functionality of hardware
concurrency
timing controls
The implementation of hardware
structure
net-list
Different Levels of Abstraction
Algorithmic
the function of the system
RTL
the data flow
the control signals
the storage element and clock
Gate
gate-level net-list
Switch
transistor-level net-list
Verilog for Digital System Design
Structural description
net-list using primitive gates and switches
continuous assignment using Verilog operators
RTL
functional description
timing controls and concurrency specification
procedural blocks (always and initial)
registers and latches
C + timing controls + concurrency
Behavioral vs Structural 43
05/31/2024
Behavioral description describes functionality of design. It is independent of
implementation.
There is a one-to-many mapping between a behavioral module and a structural
module.
05/31/2024
Rule of thumb:
Behavioral doesn’t have sub-components
Structural has sub-components:
Instantiated Modules
Instantiated Gates
Instantiated Primitives
Most modules are mixed
Obviously this is the most flexible
Behavioral vs. Structural 45
05/31/2024
Structural
Structural
Structural Behavioral
Behavioral
Behavioral Primitive
Hierarchical structure and Modules
modules
the basic building blocks
ports
the I/O pins in hardware
input, output or inout
Event Driven Simulation
Verilog is really a language for modeling event-driven systems
Event : change in state
0 t t+1
Event ••• •••
queue
Events
Simulation starts at t = 0
Processing events generates new events
When all events at time t have been processed simulation time advances to t+1
Simulation stops when there are no more events in the queue
Modeling Structure: Modules
• •
• •
• •
endmodule endmodule
endmodule
Modeling Structure: Instances
Module instances
Verilog models consist of a hierarchy of module instances
In C++ speak: modules are classes and instances are objects
AND3
i0
i1
o
i2
wire temp;
X: unknown
Declaring a register
reg [<range>] <reg_name> [<reg_name>*];
Declaring memory
reg [<range>] <memory_name> [<start_addr> : <end_addr>];
Examples
reg r; // 1-bit reg variable
wire w1, w2; // 2 1-bit wire variable
reg [7:0] vreg; // 8-bit register
reg [7:0] memory [0:1023]; a 1 KB memory
Structural Modeling
Structural Verilog describes connections of modules (netlist)
endmodule c3 a3 a2 a1 a0 ci
Two Main Data Types
Verilog Simulator
Simulation Result
Sample Design
module fadder ( sum, cout, a, b , ci ); 1-bit full adder sum
// port declaration a
output sum, cout; b
ci
input a, b, ci;
reg sum, cout;
cout
// behavior description
always @( a or b or ci )
begin
sum = a ^ b ^ ci;
cout = ( a&b ) | ( b&ci ) | ( ci&a); Simpler than VHDL
end Only Syntactical Difference
endmodule
Basic Instructions
Reg and Parameters
Reg
variables used in RTL description
a wire, a storage device or a temporary variable
reg : unsigned integer variables of varying bit width
integer : 32-bit signed integer
real : signed floating-point
time : 64-bit unsigned integer
Parameters
run-time constants
Special Language Tokens
$<identifier>: System tasks and functions
$time
$stop
$finish
$monitor
$ps_waves
$gr_waves
$gr_regs
#<delay specification>
used in
gate instances and procedural statements
unnecessary in RTL specification
Modeling Structures
Net-list
structural description for the top level
Continuous assignments (combination circuits)
data flow specification for simple combinational
Verilog operators
Procedural blocks (RTL)
always and initial blocks
allow timing control and concurrency
C-like procedure statements
primitives (=truth table, state transition table)
function and task (»function and subroutine)
A full-adder
module add (co, s, a, b, c)
input a, b ,c ;
output co, s ;
xor (n1, a, b) ;
xor (s, n1, c) ;
nand (n2, a, b) ;
nand (n3,n1, c) ;
nand (co, n3,n2) ;
endmodule
Verilog Primitives
Conditional operator
assign z = ({s1,s0} == 2'b00) ? IA :
({s1,s0} == 2'b01) ? IB :
({s1,s0} == 2'b10) ? IC :
({s1,s0} == 2'b11) ? ID :
1'bx ;
Arithmetic Operators +, -, *, /, %
Relational Operators <, <=, >, >=
Equality Operators ==, !=, ===, !==
Logical Operators !, &&, ||
Bit-Wise Operators ~, &, |, ^, ~^
Unary Reduction &, ~&, |, ~|, ^, ~^
Shift Operators >>, <<
Conditional Operators ?:
Concatenations {}
Operator Precedence
[ ] bit-select or >, >=, <, <=
part-select relational
( ) parentheses ==, != logical equality
!, ~ logical and bit-wise & bit-wise AND
negation
^, ^~, ~^
&, |, ~&, ~|, ^, ~^, ^~
reduction operators bit-wise XOR and
XNOR
+, - unary arithmetic
| bit-wise OR
{ } concatenation
&& logical AND
*, /, % arithmetic
|| logical OR
+, - arithmetic
?: conditional
Operators
{} concatenation ~ bit-wise NOT
+ - * / & bit-wise AND
arithmetic | bit-wise OR
^ bit-wise XOR
% modulus
^~ ~^ bit-wise XNOR
> >= < <=
& reduction AND
relational
| reduction OR
! logical NOT
~& reduction NAND
&& logical AND ~| reduction NOR
|| logical OR ^ reduction XOR
== logical equality ~^ ^~ reduction XNOR
!= logical inequality << shift left
?: conditional >> shift right
Simple Behavioral Model: the always block
‘0’
‘X’
‘1’ ‘Z’
0
Major Data Type Class
Nets
Registers
Parameters
Nets
Net data type represent physical connections between
structural entities.
A net must be driven by a driver, such as a gate or a
continuous assignment.
Verilog automatically propagates new values onto a
net when the drivers change value.
Registers & Parameters
05/31/2024
Verilog Fragment Result
Clock
Blocking vs. Non-Blocking (2) 93
05/31/2024
Use Non-Blocking for FlipFlop Inference
posedge/negedge require Non-Blocking
Else simulation and synthesis wont match
Blocking vs. Non-Blocking (3) 94
05/31/2024
If you use blocking for FlipFlops:
a
Verilog
b y
Module
c
Defining a module
module example (a, b, c, y);
input a;
input b;
input c;
output y;
endmodule
a
Verilog
b y
Module
c
A question of style
The following two codes are identical
module test ( a, b, y ); module test ( input a,
input a; input b,
input b; output y );
output y;
endmodule
endmodule
What if we have busses?
You can also define multi-bit busses.
[ range_start : range_end ]
Example:
/* Multiline comments
are defined like this */
Good Practices
Structural
Describe how modules are interconnected
Each module contains other modules (instances)
… and interconnections between these modules
Describes a hierarchy
Behavioral
The module body contains functional description of the circuit
Contains logical and mathematical operators
// description of small
endmodule
endmodule
Structural HDL Example
Wire definitions
module top (A, SEL, C, Y);
input A, SEL, C;
output Y;
wire n1;
endmodule
Structural HDL Example
Instantiate first module
module top (A, SEL, C, Y);
input A, SEL, C;
output Y;
wire n1;
// description of small
endmodule
endmodule
Structural HDL Example
Instantiate second module
module top (A, SEL, C, Y);
input A, SEL, C;
output Y;
wire n1;
// alternative
small i_first ( A, SEL, n1 );
/* Shorter instantiation,
pin order very important */ module small (A, B, Y);
input A;
// any pin order, safer choice input B;
small i2 ( .B(C), output Y;
.Y(Y),
.A(n1) ); // description of small
endmodule endmodule
What Happens with HDL code?
endmodule
Behavioral HDL: Synthesis Results
b
c y
un5_y y
un8_y
Behavioral HDL: Simulating the Circuit
Bitwise Operators
module gates(input [3:0] a, b,
output [3:0] y1, y2, y3, y4, y5);
endmodule
Bitwise Operators: Synthesis Results
Reduction Operators
assign y = &a;
endmodule
Reduction Operators: assign y = &a;
Conditional Assignment
module mux2(input [3:0] d0, d1,
input s,
output [3:0] y);
assign y = s ? d1 : d0;
// if (s) then y=d1 else y=d0;
endmodule
endmodule
Even More Conditional Assignments
assign y = (s == 2’b11) ? d3 :
(s == 2’b10) ? d2 :
(s == 2’b01) ? d1 :
d0;
// if (s = “11” ) then y= d3
// else if (s = “10” ) then y= d2
// else if (s = “01” ) then y= d1
// else y= d0
endmodule
How to Express numbers ?
N’Bxx
8’b0000_0001
(N) Number of bits
Expresses how many bits will be used to store the value
(B) Base
Can be b (binary), h (hexadecimal), d (decimal), o (octal)
(xx) Number
The value expressed in base, apart from numbers it can also have X and Z as values.
Underscore _ can be used to improve readability
Number Representation in Verilog
Verilog Stored Number Verilog Stored Number
Highest ~ NOT
*, /, % mult, div, mod
+, - add,sub
<<, >> shift
endmodule endmodule
Example: Comparing Two Numbers
module compare (input a0, a1, a2, a3, b0, b1, b2, b3,
output eq);
wire c0, c1, c2, c3, c01, c23;
endmodule
Example: Comparing Two Numbers
module compare (input a0, a1, a2, a3, b0, b1, b2, b3,
output eq);
wire c0, c1, c2, c3, c01, c23;
endmodule
Example: Comparing Two Numbers
module compare (input a0, a1, a2, a3, b0, b1, b2, b3,
output eq);
wire c0, c1, c2, c3;
endmodule
Example: Comparing Two Numbers
module compare (input a0, a1, a2, a3, b0, b1, b2, b3,
output eq);
wire [3:0] c; // bus definition
endmodule
Example: Comparing Two Numbers
endmodule
Example: Comparing Two Numbers
output eq);
endmodule
Example: Comparing Two Numbers
endmodule
What is the BEST way of writing Verilog
assign y = s ? d1 : d0;
endmodule
assign y = s ? d1 : d0;
endmodule
// Concatenating is by {}
assign y = {a[2],a[1],a[0],a[0]};
y_1[3:0]
assign y = en ? a : 4'bz;
endmodule
Truth Table for AND with Z and X
A
&
0 1 Z X
0 0 0 0 0
1 0 1 X X
B
Z 0 X X X
X 0 X X X
What About Timing ?
It is possible to define timing relations in Verilog
These are ONLY for Simulation
They CAN NOT be synthesized
They are used for modeling delays in simulation
‘timescale 1ns/1ps
module simple (input a, output z1, z2);
endmodule