Lecture 04-Hardware Modeling
Lecture 04-Hardware Modeling
Introduction to Verilog
1
The Verilog Language
• A hardware description language (HDL) used for
modeling, simulation and synthesis of digital
electronic systems
• One of the two most commonly-used languages in
digital hardware design (VHDL is the other)
• Virtually every chip (FPGA, ASIC, etc.) is designed in
part using one of these two languages
• Combines structural and behavioral modeling styles
• IEEE standard 1364-1995
• Verilog-2001 extends the initial Verilog-95 specification
2
Simulation
• Simulation is used to verify the functional
characteristics of models at any level of abstraction
• One of the main uses of Verilog/VHDL is simulation
• To test if the RTL code meets the functional
requirements of the specification, we must see if all
the RTL blocks are functionally correct.
• To achieve this we need to write a testbench, which
generates clk, reset and the required test vectors
• Simulation is needed after every refinement step
– Behavioral, zero-delay gate level, timing, post P&R
simulation
3
How Are Simulators Used?
• Testbench generates stimulus and checks
response
• Coupled to model of the system
• Pair is run concurrently
Stimulus
Response
Result
checker
4
Learn by Example: Combinational Logic
Adder: a circuit that does addition
Here’s an example of binary addition:
Adding two N-bit 1101
numbers produces an
(N+1)-bit result +0101
10010
“Ripple-
carry
adder”
5
1-bit Adder
Α Β Cin S Cout
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0 Sum: S A B C
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1 Carry Out:
1 1 0 0 1 CO A'BC AB'C ABC' ABC
1 1 1 1 1 (A' A)BC (B' B)AC AB(C' C)
BC AC AB
6
1-bit Adder
module: Basic unit of description One line comment
assign s = x ^ y ^ Cin;
assign Cout = (x & y) | (x & Cin) | (y & Cin);
endmodule 8
Alternative N-bit Adder
module addern #(parameter n=32) (carryin, X, Y, S, carryout);
input carryin;
input [n-1:0] X, Y; n=32 sets default value for parameter n
output [n-1:0] S;
output carryout; reg is a data type that indicates a
reg [n-1:0] S; driver that can store a value.
reg carryout; If a signal is not reg, it is a wire
reg [n:0] C; An always statement is only triggered at (@) the
integer k; condition in the parenthesis (sensitivity list).
We execute the always statement only when X or Y or
always @(X, Y, carryin) carryin change
begin
C[0] = carryin;
for (k = 0; k < n; k = k+1)
begin
S[k] = X[k] ^ Y[k] ^ C[k];
C[k+1] = (X[k] & Y[k]) | (X[k] & C[k]) | (Y[k] & C[k]);
end
carryout = C[n];
end
endmodule
Behavioral Description 9
Another one
module addern #(paramtere n=32) (carryin, X, Y, S, carryout, overflow);
input carryin;
input [n-1:0] X, Y;
output [n-1:0] S;
output carryout, overflow;
reg [n-1:0] S; S, X, Y are arrays
reg carryout, overflow;
endmodule
Behavioral Description
10
Language Elements and Expressions
Numbers in Verilog
• Integer numbers
– Simple decimal notation
• 32, -15
– Base format notation : [size in bits]’base value
– Base is one of d or D (decimal), o or O (octal), b or B (binary), h
or H (hex). Use s for signed.
• 5’O37 // 5 bit octal number 37
• 4’D2 // 4 bit decimal number 2
• 4’B1x_01 // 4 bit binary number 1x01.
• 7’Hx // 7 bit hex xxxxxxx
• 8’shFF // Signed hex equal to -1
• 4’d-4 // Not legal. The sign is before the size.
• -4’d4 // 4-bit binary number -4. The sign is before the size
• (2+3)d’10 // Not legal. Size cannot be an expression
– The underscore character (_) is legal anywhere in a number
except as the first character, where it is ignored
– When used in a number, the question mark (?) character is the
Verilog alternative for the z character.
12
Numbers in Verilog
• Real numbers
– Decimal notation
• 32.0, -11.06
• 2. // Not legal. Must have a digit on either side of
decimal
– Scientific notation
• 23.51e2 // 2351.0
• 23_5.1e1 // 2351.0
• 5E-4
13
Strings in Verilog
• A string is a sequence of chars within double
quotes
– “Verilog is cool!”
14
Four-valued Data
• Verilog’s nets and registers hold four-valued data
• 0, 1
– Obvious
• Z (high impedance)
– Output of an undriven tri-state driver
– Models case where nothing is setting a wire’s value
• X (unknown or undecided)
– Models when the simulator can’t decide the value
– Initial state of registers
– When a wire is being driven to 0 and 1 simultaneously
– Output of a gate with Z inputs
15
Two Main Data Types
• Wires represent connections between things
– Do not hold their value
– Take their value from a driver such as a gate or other module
– A signal is wire by default
16
Nets and Registers
• Wires and registers can be bits, vectors, and arrays
18
Operators
• Relational operators
– The result is 0 (false), 1 (true), or x (either operand
has an x or z bit)
• 23 > 45 // false (value 0)
• 52 < 8’hxFF // result is x
• ‘b1000 >= ‘b01110 // false. Smaller sized operand is
zero-filled
19
Operators
• Equality operators.
– 2’b10 == 4’b0010 // true
– ‘b11x0 == ‘b11x0 // unknown because there
is a bit in in either operand which is x (or z)
– ‘b11x0 === ‘b11x0 // true. In case equality, x
and z are compared strictly as values
20
Operators
• Logical operators.
– A && B // logical AND
– A || B // logical OR
– !A // logical NOT
21
Operators
• Bit-wise operators
– ~A (unary negation)
– & (binary and)
– | (binary or)
– ^ (binary xor)
– ~^ (binary XNOR)
0 1 X Z 0 1 X Z
0 0 0 0 0 0 0 1 X X
1 0 1 X X 1 1 1 1 1
X 0 X X X X X 1 X X
Z 0 X X X Z X 1 X X
22
Operators
• Reduction operators
• A = ‘b0110, B = ‘b0100, C = ‘b0x1
• &B is 0 // logical AND of all bits
• |B is 1 // logical OR of all bits
• ~&A is 1 // logical NAND of all bits
• ^B is 1 // logical XOR of all bits
• ~^B is 0 // logical XNOR of all bits
• &C is 0 // at least a bit is 0
• |C is 1 // at least a bit is 1
• ^C is x // if any bit is x or z, result is x
23
Operators
• Shift operators
– A >> B // logical right shift
– A << B // logical left shift
– A >>> B // right shift for signed numbers (Only
Verilog 2001)
– A <<< B // left shift for signed numbers (Only
Verilog 2001)
• Conditional operator
– cond_expr? expr1 : expr2
24
Operators
• Concatenation and Replication operators
– wire [7:0] Dbus
– wire [11:0] Abus
– assign Dbus = {Dbus[3:0], Dbus[7:4]} //
concatenation: swap nibbles
– Abus ={3{4’b1011}} // 12’b1011_1011_1011
25
Two ways to express hardware in
Verilog:
Structural and Behavioral Models
Two Main Components of Verilog
• Concurrent, event-triggered processes (behavioral)
– Initial and Always blocks
– Imperative code that can perform standard data
manipulation tasks (assignment, if-then, case)
– Processes run until they delay for a period of time or wait
for a triggering event
• Structure (structural)
– Verilog program built from modules with I/O interfaces
– Modules may contain instances of other modules
– Modules contain local signals, etc.
– Module configuration is static and all run concurrently
• High level modules typically structural
• Low level modules (leaves) typically behavioral
27
Structural Modeling
• When Verilog was first developed (1984) most
logic simulators operated on netlists
• Netlist: list of gates and how they’re
connected
• A natural representation of a digital logic
circuit
• Not the most convenient way to express test
benches
28
Behavioral Modeling
• A much easier way to write testbenches
• Also good for more abstract models of circuits
– Easier to write
– Simulates faster
• More flexible
• Provides sequencing
• Verilog succeeded in part because it allowed both
the model and the testbench to be described
together
29
Multiplexer Built From Primitives
Identifiers not
module mux(f, a, b, sel); explicitly defined
default to wires
output f;
input a, b, sel; Module may contain
Predefined module types structure: instances of
primitives and other
and g1(f1, a, nsel), modules
g2(f2, b, sel);
or g3(f, f1, f2); a f1
nsel g1
not g4(nsel, sel); g4
g3 f
b
endmodule sel
g2
f2
30
Multiplexer Built With Always
Modules may contain one or
more always blocks
module mux(f, a, b, sel);
output f;
input a, b, sel; Sensitivity list
reg f; contains signals
whose change
triggers the execution
always @(a, b, sel) of the block
if (sel)
f = b;
a
else
f = a; f
b
endmodule sel
31
Mux with Continuous Assignment
LHS is always set to the value on
the RHS
module mux(f, a, b, sel); Any change on the right causes
reevaluation
output f;
input a, b, sel;
assign f = sel ? b : a;
a
endmodule
f
b
sel
32
Structural Modeling in more
detail
Modules and Instances
• Basic structure of a Verilog module:
…
endmodule
Οργάνωση και Σχεδίαση Η/Y (ΗΥ232) 35
Instantiating a Module
• Instances of
• look like
e.g.
#2 input = 4’b0101; // wait for 2 units, and then make
the assignment
input = #1 x // assign to input the value that x will have after
1 time unit. Different than #1 input = x
initial begin
#10 a = 1; b = 0;
#10 a = 0; b = 1;
end
sum = a + b + cin;
case (op)
2’b00: y = a + b;
2’b01: y = a – b;
2’b10: y = a ^ b;
default: y = ‘hxxxx;
endcase
i = 0;
while (i <= 15) begin
output = i;
#10 i = i + 1;
end
• Fundamental problem:
– In a synchronous system, all flip-flops sample
simultaneously
– In Verilog, always @(posedge clk) blocks run in
some undefined sequence
1 a
a <= 1;
b <= a;
c <= b; b
New
Memory
State
Device Current
State Combinational
LOAD Logic
Input Output
CLK
t
≤ PD
t
≥ CD
CLK
n n
Q D
Registers
CLK
next
state S'D
inputs Q
Comb. Outputs
x0...xn Comb. Registers
Logic Logic yk = fk(S)
CLK
n
present state S
•Mealy FSM:
next
state S'D
inputs Q
Comb. Outputs
x0...xn Comb. Registers
Logic Logic yk = fk(S, x0...xn)
CLK
n
present state S
Level to
L Pulse P
Whenever input L goes Converter ...output P produces a single
from low to high... pulse, one clock period wide.
CLK
CLK
+
S0 = L
// Clock generator
always begin
#5 clock = ~clock; // Toggle clock every 5 ticks
end
• #42
– Schedule process to resume 42 time units from now
• wait(cf & of)
– Resume when expression “cf & of” becomes true
• @(a or b or y)
– Resume when a, b, or y changes
• @(posedge clk)
– Resume when clk changes from 0 to 1
while (~ready)
count = count + 1;
• Instead, use
wait(ready);
• Major vendors
– Synopsys Design Compiler, FPGA Express
– Cadence BuildGates
– Synplicity (FPGAs)
– Exemplar (FPGAs)
• Academic tools
– SIS (UC Berkeley)
• Typical operations
– Constant propagation
– Common subexpression elimination
– Function factoring
• Time-consuming operation
– Can take hours for large chips
• Rule:
• Combinational if outputs always depend
exclusively on sensitivity list
• Sequential if outputs may also depend on
previous values
always @(a or b)
case ({a, b})
2’b00 : f = 0;
f is not assigned when {a,b} = 2b’11
2’b01 : f = 1;
2’b10 : f = 1;
endcase
Οργάνωση και Σχεδίαση Η/Y (ΗΥ232) 97
Register Inference
• The solution is to always have a default case
always @(a or b)
case ({a, b}) f is always assigned
2’b00: f = 0;
2’b01: f = 1;
2’b10: f = 1;
default: f = 0;
endcase
- 1
FPGA fabric
• A generic island-style
FPGA fabric
• Configurable Logic Blocks
(CLB) and Programmable
Switch Matrices (PSM)
• Bitstream configures
functionality of each CLB
and interconnection
between logic blocks
- 3
TheDetailed
Xilinx Slice
Structure
• Xilinx slice features
– LUTs
– MUXF5, MUXF6,
MUXF7, MUXF8
(only the F5 and
F6 MUX are shown
in this diagram)
– Carry Logic
– MULT_ANDs
– Sequential Elements
Basic
- Architecture 4
Slice Logic
• Look-Up Tables
– An N-input LUT to implement
any combinational boolean
function with N inputs
– Coarser-grained than logic
gates
– Less area efficient than fixed
logic gates (e.g. 4-input AND
gate)
– Very powerful concept for
implementation of bit-level
random digital logic.
– Typical values (N=4,5, or 6)
- 5
Example 2-input LUT
0 0 0 1
a 0001 0 1 0 0
out
LUT
b
1001 1 0 0 0
configuration 1 1 1 1
input
Cout= 𝐴𝐵 +
SLICE
S0
(𝐶𝑖𝑛(𝐴 𝑥𝑜𝑟 𝐵 )) CIN CIN CLB
F8
MUXF7 outputs (from the
F5
CLB above or below)
Slice S3
MUXF6 combines slices S2
F6
and S3
F5
Slice S2
MUXF6 outputs
Slice S1
F5
Slice S0
F5
CE435
Basic- Embedded
Architecture
Systems
11
Programmable wiring
• Organized into channels.
– Many wires per channel.
• Connections between wires made at
programmable interconnection points.
• Must choose:
– Channels from source to destination.
– Wires within the channels.
• Routing area typically much larger than logic
area
CE435 - Embedded Systems 12
Programmable interconnect
D Q
channel
channel channel
channel
LE
LE
optimized
System Logic
Performance