Lec9-11 IntroVerilog
Lec9-11 IntroVerilog
Verilog HDL
Email: [email protected]
HDLs’ History
• 1971-ISP
– Introduce the concept of Register Transfer Level (RTL)
– no synthesis
• 1985-Verilog
– Develop by Gateway(acquired by Cadence)
– Similar to C language
– IEEE standard
• 1987-VHDL
– Request from U.S. Department of Defence
– More structural than Verilog but verbose
– IEEE standard
• Now
– Verilog-95, Verilog-2001, System Verilog
– VHDL-1987, … VHDL 2008
• Ref
– https://en.wikipedia.org/wiki/Verilog
– https://en.wikipedia.org/wiki/VHDL
Email: [email protected] 1
Design Methodology
• Structural and behavioral Inputs
• Simulation
Functional verification HDL description
• Synthesis Timing verification
Netlist (gate level) verification
• Place and Route Simulation
Fab
Email: [email protected] 2
Verilog Language
• One of the two most commonly-used HDL
in digital design
• Using text rather than pictures to describe
a circuit or
Email: [email protected] 3
Verilog module
• A Verilog description is included in a
module
;
Email: [email protected] 4
Two Main Data Types
• wire (refer to combinational logic)
– Represent electrical connections between blocks, using assign
– Cannot be assigned in an initial or always block
– Used for left hand side
• reg (refer to sequential logic and combinational logic)
– Behave like memory, DFF in a computer
– Hold the value until explicitly assigned in an initial or always
– Synthesized to latches, flip-flops, but not necessary
– Used for left hand side(LHS)
Verilog 2001
Email: [email protected] 5
Cont.
• Integer
– Converted to right number of bits automatically if stored in a scalar or vector
• Scalar
– A single wire or reg, holding 1 bit
• Vector
– A wire or reg with more than 1 bit
• Array
– A 2-dimentional array of wire or reg
– Often used in a testbench
• A test bench is a source file that is used to instantiate the designed module, generate test vectors,
and collect the output puts from the module
– Not preferred for synthesis
Email: [email protected] 6
Verilog description
• Structural vs. Behavioral
– Structural Description
• Similar to the schematics but using text
• List of gates and their connections
• A natural representation of a digital logic circuit
• Hard to understand and to write
• Suitable for module connections
– Behavioral Description
• Describe what the function of the component is, like C
• Much easier to understand and to write
• Always preferred
• But not all behavior statements are synthesizable
Email: [email protected] 7
Verilog general
• Character sets
– 0-9, A-Z, a-z(case sensitive), _ $
– All keywords are in lower case
– Exp.:
• Comments
– Single line comments: //
– Multiple line comments: /* comment*/
– Exp.:
• White space
– Space(\b), tabs(\t), newlines(\n)
– Used in strings
Email: [email protected] 8
Data Values
• Constants
– Specified by number of bits and value
– Integer values are truncated to fit variable size
– Exp.: <size>'<radix><value>
• Parameters
– Given a constant value
– Can be overridden when a design is complied
– Exp.:
Email: [email protected] 9
Statements
• Statement delimiter
– End of statement is a “;”
– Returns do not affect the statements
– Multiple statements using the same form can be
grouped and distinguished by commas(not
recommended)
Email: [email protected] 10
Operators – Three Types
• Unary operators
– On the left of their operand
• Binary operators
– In the middle of their operands
• Ternary has three operands by two operators(conditional assignment)
Email: [email protected] 11
Operators Summary-1
Email: [email protected] 12
Operators Summary-2
Email: [email protected] 13
Four – Valued Logic
• Verilog logic values
– 1, 0, Z(high impedance), X(unknown)
– X: one of 1, 0 , Z or in the state of change(unknown).
It is not a real value, no real corresponding gate. It is
simply meaning “don’t care” whatever the value
– Z: the high impedance output of a tri-state gate
Truth table
NAND 0 1 X Z
0 1 1 1 1
1 1 0 X X
X 1 X X X
Z 1 X X X
Email: [email protected] 14
Structural Design
• Top module
– The top of the synthesized code
– Comprises of other modules with different
functions
• This is the file where structural description is required
– Define the chip I/O signals
• Sub-modules
– Function units, e.g. multiplier, adder, filter, etc.
– Memories, e.g. SRAM, register array, etc.
– Other IPs, e.g. decoder, encoder, etc.
Email: [email protected] 15
Design and Test
• Construct a “test bench” for the design
– Generate test vectors
– Monitor the outputs of the design under test
(DUT)
– Create a “testbench.v” file, which incorporate
vector generation, outputs monitor and DUT.
16
Email: [email protected]
Half Adder Example
Truth Table
A B S C
A B
0 0 0 0
C 0 1 1 0
1 0 1 0
1 1 0 1
S
S = A xor B
C = AB
Email: [email protected] 17
Half Adder Example (1)
Email: [email protected] 18
Half Adder Example (2)
Email: [email protected] 19
Primitives in Verilog
• Verilog supports basic logic gates as
primitives
– and, nand, or, nor, xor, xnor, not, buf
– Bufif1 and bufif2 are tri-state buffers
• Using Net(wire) represents connections of
primitives
sel out
0 b
Email: [email protected] 1 a 20
Continuous Assignment
• Key word: assign
• Represent combinational logic
• RHS* is continuously evaluated like data flow
• LHS* must be a scalar or vector net not reg
Email: [email protected] 22
Procedural with always
• High-level behavioral description of
combinational logic
Not a latch and DFF
Email: [email protected] 23
Cont.
• In a module, there can be more than one
always statements
• Two separate always cannot calculate the
same function
Not allowed
Email: [email protected] 24
Behavioral Statements
• if(condition is true) else
– Four value logic. 1 is “true”, 0, X, Z are “false”
• case is similar to if else but easy to read than a
long if else
Email: [email protected] 25
Combinational Circuits - 1
• Combinational: the output is only a function
of the inputs(not the previous value)
Wait for any change on a,
b, or sel, then execute the
begin-end block. Then
wait for another change.
Email: [email protected] 26
Combinational Circuits - 2
• A reg type variable is not a
register in comb. circuits
– The synthesis tool translates
it as a combinational circuit
– So, reg is artificial in this case
– Inside always, LHS must be
reg type
Email: [email protected] 27
Combinational Circuits Style
Email: [email protected] 28
Rules for Combinational Logic
• Every element of the input sets must be in the
sensitivity list, e.g @()
• The combinational output must be assigned in
every control path
Email: [email protected] 29
Incomplete Specification - 1
• Infer latches
a 00
b 01
c out
10
sel
Combinational logic
Correct?
Email: [email protected] 30
Incomplete Specification - 2
a 00 out
b 01 D Q
c 10
Incomplete
case list, missing
sel 2’b11
Sel0,1
If out is not assigned in this case, the
previous value will be retained, inferring
latches. Avoiding inferred latches all the
time
Email: [email protected] 31
Incomplete Specification - 3
• Solution
– Complete the all the cases, e.g. complete if…else and
case
– Given default value
Email: [email protected] 32
Incomplete Specification(4)
• One more example
BAD Good
BAD
No default values
Email: [email protected] 34
Incomplete Specification - 6
Good
Email: [email protected] 35
More Complex Examples
Email: [email protected] 36
A Full Adder
cin A B S cout
0 0 0 0 0
A B 0 0 1 1 0
COUT CIN 0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
S
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Email: [email protected] 37
A Full Adder Implementation – 1
• Behavioral implementation
• Module fulladder2bits
Email: [email protected] 38
A Full Adder Implementation - 2
• Structural Approach
– Design a Half Adder(we’ve learned)
– Cascade two Half Adders → 1 bit Full Adder
– Cascade n Full Adders → n bits Full Adder
1-bit Half Adder- module halfadder
Email: [email protected] 39
A Full Adder Testbench
• Instantiate two 3-
bits Full Adder
– Behavioral Verilog
Initialization(remove
– Structural Verilog these three
statements, see
– Compare the output again)
results
Email: [email protected] 40
A Full Adder Simulations
Q1: Which one is correct?
Q2: Why it happens?
Email: [email protected] 42
Multiplier Structure
A2 A1 A0
* B2 B1 B0
A2B0 A1B0 A0B0
+
A2B1 A1B1 A0B1
+
A2B2 A1B2 A0B2
P5 P4 P3 P2 P1 P0
Email: [email protected] 43
Multiplier Implementation(1)
• Unsigned multiplication
Nothing but *
Why this?
Email: [email protected] 44
Multiplier Implementation(2)
a b
• 110*011=110*(0*22+1*21+1*20) 20=no shift
21=shift left 1 bit
• 110*111=110*(1*22+1*21+1*20) 22=shift left 2 bits
23=shift left 3 bits
…
2N=shift left N bits
Email: [email protected] 45
Multiplier Testbench
Email: [email protected] 46
Multiplier Simulation
Email: [email protected] 47
Priority Logic (1)
• Beware of priority coding
– Different coding style infers different logic gates
– See example
A3 A2 A1 A0 O1 O0
0 0 0 1 0 0
0 0 1 0 0 1
0 1 0 0 1 0
1 0 0 0 1 1
others Don’t care
Email: [email protected] 48
Priority Logic (2)
We want We get
Email: [email protected] 50
Behavioral Statements-Loops
• for…end/while
– Synthesizable, but DO NOT USE IT
– Best applicable situation is to use it in a testbench,
not for a circuit design
• repeat/forever
– Not synthesizable
– @<edge-triggered event>
• For procedural code
• Process is triggered when the correct edge happens
Email: [email protected] 52
Verilog Timing (2)
• always and initial
– Initial starts at time=0
– always starts when
meet the condition
• always
– always: this will run
forever
– always @(a or b):
runs when a or b
changes
– always @(posedge
clk): runs at each
rising clock(synthesize
to flip-flops
Email: [email protected] 53
Verilog Timing (3)
• initial
– Initial is not
synthesizable
– initial is used for
test bench
– Initialize variables
Email: [email protected] 54
Sequential Logic
• Combinational logic circuits
– The output of a logic block is only the function of
the current input values
– No memory
– E.g. NAND, NOR and NOT
• Sequential logic circuits
– The output of a logic block is the function of current
inputs and preceding input values
– Has memory
– e.g. latches(level sensitive) and registers(edge
triggered)
Email: [email protected] 55
Delay of An Inverter
• tpdr: rising
Input of a inverter
propagation delay
– From input to rising
output crossing VDD/2
• tpdf: falling
output of a inverter
propagation delay
– From input to falling
output crossing VDD/2
• tpd: (tpdr+tpdf)/2
Email: [email protected] 56
Timing Metrics for Sequential Circuits
• Setup time: tsu
– The time that the data input(D) must be valid
before the clock transition (0→1 for positive edge
triggered register, 1→0 for negative edge triggered
register)
• Hold time: thold
– The time that the data input(D) must be retained
valid after the clock edge
• Propagation delay(clock to Q): tcq
– The time that the data at D input is transferred to
the Q output
Email: [email protected] 57
tsu,thold, tcq
register
D Q
CLK
Email: [email protected] 58
Setup and Hold Time Violation
• What happens if failure of setup and hold time occurs?
– The D flip-flop enters a state where its output is unpredictable,
e.g. not “1” or “0”
– Called metastability
– How long D flip-flop can be recovered from metastable states is
unknown
– It happens in asynchronized system→ avoid asynchronized Sys.
Email: [email protected] 59
Dealing with Metastability
• Sometimes asyncrhonized design cannot be
avoided, e.g. traffic light button
– Using double synchronizer to minimize the
probability of metastability of a DFF
Email: [email protected] 60
A Synchronized Circuits – One CLK
Chip Boundary
Comb:
sequential: adders sequential
reg multipliers comb
Q Q Q
tCLK>tcq+tpd_comb+tsu thold<tcq+tpd_comb
Determine minimum time
period of a clock
Exp:
tcq=200ps
tsu=100ps
tpd_comb=1000ps
tCLK_min=200+100+1000=1300ps
Email: [email protected] 61
Pipelining
td=(td1+td2+td3) < Tclk td1<Tclk td2<Tclk td3<Tclk
REG
REG
a a
REG
REG
REG
REG
CLK + log Out CLK + log Out
REG
REG
Latency:
3 clock cycles
Ref: [J.Rabaey02]
Email: [email protected] 62
Sequential Logic in Verilog
• always is often used to construct sequential circuits
– edge triggered circuits is for synchronized design
Email: [email protected] 63
Posedge/Negedge DFF
Email: [email protected] 67
Synchronized vs Asynchronized Reset
• Synchronized resets
– Ensure circuits are 100% synchronous
– Ensure reset can only occur at an active clock edge
– Remove any glitches on reset signal unless the
glitches are near the active clock edge
– Use more area
– May cause timing issue in data path
– Preferable technique in most of the designs
• Asynchronized resets(if in library)
– Better timing in data path
– Can reset circuits without clocks
– But it may cause metastability issue
Email: [email protected] 68
Blocking Assignment
• Two types assignments within always blocks, inferring
different behaviors
– “=“ and “<=“
• Blocking assignment “=“
– Evaluate and assign are happening immediately
– The next assignment awaits(is blocked) until the present
assignment is completed
Email: [email protected] 69
Nonblocking Assignment
• Nonblocking assignment “<=“
– All assignments are executed until right hand
sides have been evaluated
1. Evaluate a&b,
2. Evaluate a|b|c
3. Evaluate ~a
4. Assign x,y,z
Email: [email protected] 70
Shift Registers
• Are they same?
Email: [email protected] 71
Nonblocking for Sequential Circuits
out
testbench
Source code
Email: [email protected] 73
sim
Using Blocking and Nonblocking
• Use blocking, “=“, for combinational logic
circuits
• Use nonblocking, “<=“, for sequential logic
circuits
Email: [email protected] 74
S-to-P and P-to-S (1)
• Series to parallel conversion
• Parallel to series conversion
• Often used in a storage device in microprocessor or
ASIC
S-to-P
Email: [email protected] 75
S-to-P and P-to-S (2)
• Integrate S-to-P and P-to-S
• Signal SP determines the current status
– SP=0, it is in series to parallel conversion
– SP=1, it is in parallel to series conversion
Email: [email protected] 76
S-to-P and P-to-S (3)
• S-to-P • P-to-S
Email: [email protected] 77
S-to-P and P-to-S (4)
• P-to-S full example
Email: [email protected] 78
Finite State Machine(FSM)
• Most useful abstraction for sequential
circuits
• FSM is defined by its next-state
State graph
Email: [email protected] 79
Two Types of FSM
• Moor state machine: the outputs are only a
function of the present state
• Mealy state machine: one or more of the outputs
are a function of the present state and one or
more of the inputs
Email: [email protected] 80
Coding Styles of FSM
• One always
– State transition, next state generation and output
are in the same always blocks
– NOT recommended since it is hard to maintain
• Two/three always
– Easiest method to implement FSM
– Exactly same as the FSM diagram
– Distinguish clocked present state logic, next state
combinational logic and output combinational logic
– Must define very clear FSM state generation
Email: [email protected] 81
One always Block
BAD
Email: [email protected] 82
Three always Blocks(1)
Email: [email protected] 83
Three always Blocks(2)
Email: [email protected] 84
Common Errors in FSM
• No default state → inferring unintended
latches, e.g default next_state=2’b00
• Incomplete branches → inferring unintended
latches, e.g. if..else if ..
• Inappropriate sensitive list → incorrect
function
– (state or data_in): state is missing, resulting in
wrong state transition
– X = A+B: x is listed in ()
Email: [email protected] 85
A FSM Example – MIT Open Course
• Goal:
– Build an electronic combination lock with a
reset button, two number inputs buttons(0
and 1), and an unlock output. The key
password is 01011.
Email: [email protected] 86
Step 1: Block Diagram
• Synchronized design
• Asynchronized signals from external must be synchronized first
Email: [email protected] 87
Step 2: State Transition Diagram
Button input
Unlock indicator
Email: [email protected] 89
Step 3A: Synchronizer
Email: [email protected] 90
Step 3B: State Transition
Email: [email protected] 91
Email: [email protected] 92
Step 4: Testbench
Email: [email protected] 93
Step 5: Simulation Results
0 1 0 1 1 Unlock
indicator
Email: [email protected] 94
Common Rules for Verilog Coding(1)
• Have a block diagram ready before coding
• Have a timing schedule ready before coding
• Have a pin definitions ready before coding
• Keep same naming convection and coding style for the entire design
• Add comments for each statement and block
• Use lowercase letters for all signal, variable, and port, e.g. reg a, b ;
• Use descriptive names, e.g. fsm, clk, ram, etc.
• Use clk for clock signals, e.g. clk1, clk2, etc.
• Use rst for reset signals
• Active low signals postfix with _n
• Use convention bits order, e.g. [MSB:LSB]
• Each .v file should only contain one module/endmodule
• Pins’ order should follow input and output
• No glue logic in top level module, i.e. using hierarchy
Email: [email protected] 95
Common Rules for Verilog Coding(2)
• Continued code lines should be indented
• Do not mix rising edge triggered register with falling edge
triggered register in the same design
• Use behavioral description rather than structural description for
reuse purpose
• Use rational partition
• Avoid incomplete sensitivity list(you may use always @*, but not
preferred)
• Given default values for case/if statements
• Never use loop statements in your design
• Avoid using large FSM
• Use three always type FSM
• Use Moor rather than Mealy FSM
• Better to use registered output signal
Email: [email protected] 96
A FIR Filter Design
• Goal:
– Build an FIR filter with Fs(sampling
frequency)=10MHz, Fc(cut-off
frequency)=0.1MHz, hamming window, 8 order
– Use minimum resource to implement the FIR
filter, i.e. the less multipliers or adders, the
better
– Verify the RTL simulation results, comparing to
the Matlab data
FIR Order
FIR Window
Convert to binary
See next
Normalized
Decimal
2023-10-23 Email: [email protected] 100
FIR Architecture(1)
• Convolution
• Direct Form
7 Adders
4 Multipliers
4 Adders
1 Multipliers
Start
End