SV Module - 1
SV Module - 1
SYSTEM VERILOG
SUBJECT CODE:BVL701
Dr Jalaja S
Associate Professor
Dept of VDT, BIT, Bangalore
COURSE LEARNING OBJECTIVES:
▪ Understand methods and techniques to verify the functionality of
digital electronic systems using System Verilog.
▪ Learn the System Verilog Language for Digital System Verification.
▪ To develop the ability to create structured and modular
testbenches for verifying digital designs
▪ Use Constrained Random Tests for Verification
▪ To understand and utilize the concepts of functional coverage
▪ Module-1
▪ Introduction to System Verilog:
▪ System Verilog standards, Key System Verilog enhancements for hardware design.
Advantages of System Verilog over Verilog, [Text2: 1.1.1, 1.1.2,1.2]
▪ Data Types: Verilog data types, System Verilog data types, 2 - State Data types, Bit,
byte, shortint, int, longint. 4 - State data types. Logic, Enumerated data types, User
Defined data types, Struct data types, Strings, Packages, Type Conversion: Dynamic
casting, Static Casting, Memories: Arrays, Dynamic Arrays, Multidimensional Arrays,
Packed Arrays, Associative Arrays, Queues, Array Methods. [Text2: 3.3.1, 3.3.2,
3.3.3, 3.3.4, 4.1,4.2, 5.1, 5.7, 2.1, 3.9.1.3.9.2] [Text 1: 2.4, 2.5,2.6, 2.8]
▪ Module-2
▪ Connecting the Test Bench and Design: Separating the Test Bench and Design,
The Interface Construct, Stimulus Timing, Interface Driving and Sampling, System
Verilog Assertions.
[Text 1: 5.2,5.3, 5.4,5.5, 5.9]
▪ Procedural Statements and Routines: Procedural Statements, Tasks, Functions
and Void Functions, Task and Function Overview, Routine Arguments, Returning
from a Routine, Local Data Storage, Time Values. [Text1: 3.2, 3.3, 3.4,3.5, 3.6, 3.7,3.8]
▪ Module-3
▪ Randomization: Introduction, Randomization in System Verilog, Constraint Details,
Solution Probabilities, Valid Constraints, InLine Constraints, Random Number
Functions, Common Randomization Problems, Random Control, Random Number
Generators. [Text1: 6.1,6.3,6.4, 6.5, 6.6, 6.7,6.8, 6.9,6.11, 6.14, 6.15]
▪ Module-4
▪ Functional Coverage: Introduction, Coverage Types, Functional Coverage
Strategies , Anatomy of a Cover Group, Triggering a Cover Group, Data Sampling,
Cross Coverage, Coverage Options, Parameterized Cover Groups
[Text1: 9.1,9.2, 9.3, 9.6, 9.7,9.8, 9.9,9.10]
▪ Module-5
▪ COMPLETE DESIGN MODEL USING SYSTEM VERILOG- CASE STUDY: System
Verilog ATM Example, Data Abstraction, Interface Encapsulation, Design Top Level
Squat, Receivers and Transmitters, Test Bench for ATM. [Text2: 11.1,11.2, 11.3, 11.4,
11.5, 11.6]
PRACTICAL COMPONENT OF IPCC
Sl.NO Experiments
1 Develop mux2x1 verilog code in design.sv and develop Test bench for mux2x1 in testbench.sv
with different inputs at various times.
2 Write a program to demonstrate two-state and four-state data types.
3 Write a program to demonstrate push_front, pop_front, push_back and pop_back with respect
to Queues.
4 Design a full adder using the interface construct to model inter-module communication.
5 Create a program to highlight the difference between rand and randc constructs in constrained
randomization.
6 Build a 4-bit adder and verify its functionality using a structured verification environment.
7 Design a UART module using SystemVerilog:
a. Develop the transmitter logic
b. Implement the receiver logic
c. Create a top-level testbench to verify UART operation
8 Design, simulate, and analyze functional coverage to ensure thorough verification of a given
design.
9 Design and simulate a verification test that exercises multiple sequences.
10 Create and simulate a configurable Universal Verification Methodology (UVM) test
environment.
COURSE OUTCOMES (COURSE SKILL SET):
▪ At the end of the course the student will be able to:
▪ Understand and explain the principles and methodologies used in verifying
the functionality of digital systems.
▪ Apply the System Verilog concepts to verify the complex digital circuits.
▪ Develop and implement constrained random testbenches using System
Verilog
▪ Analyze verification quality using functional coverage metrics and implement
cover groups
▪ Analyze real-world verification scenarios and apply System Verilog
methodologies
TEXT BOOKS:
▪ Chris Spear, “System Verilog for Verification – A guide
to learning the Test bench language features”, Springer
Publications Second Edition, 2010.
Data Types
logic (replaces reg and wire confusion)
User-defined typedef
▪ Interfaces
▪ Encapsulates multiple signals into a single block to simplify
module connections.
CONT..
▪ Enhanced Operators and Constructs
▪ Packed/unpacked arrays
▪ Multidimensional arrays
▪ Enhanced case statements (unique case, priority case)
▪ Parameterized classes and modules
▪ Assertions (SVA)
▪ Immediate and concurrent assertions for design verification.
▪ Randomization
▪ Built-in random number generation for constrained random testing.
ADVANTAGES OF SYSTEM VERILOG OVER VERILOG
Feature Verilog System Verilog
Rich types (logic, bit, enum, struct,
Data Types Limited (reg, wire)
etc.)
Procedural always_ff, always_comb,
always (ambiguous)
Blocks always_latch for clarity
Design &
Primarily design Unified design + verification
Verification
Interfaces Not supported Supported (simplifies connectivity)
OOP Support No Yes (class-based verification)
Assertions No Yes (SystemVerilog Assertions)
Yes (constrained random
Randomization No
generation)
Reusability Limited High (OOP, parameterization)
SCHEMATIC VS HDL
Schematic HDL
✓ Good for multiple data flow
✓ Flexible & parameterisable
✓ Give overview picture
✓ Excellent input to optimisation & synthesis
✓ Relate directly to hardware
✓ Direct mapping to algorithms
✓ Don’t need good programming skills
✓ Excellent for datapaths
✓ High information density
✓ Easy to handle electronically (only needing a
✓ Easy back annotations
text editor)
✓ Useful for mixed analogue/digital
synthesis
simulation
Behavioural Structural
▪ MODULE AND_TB;
▪ LOGIC A,B,C,Y;
▪ AND3 AN1(a,b,c,y);
▪ Initial begin
▪ A=1,B=1; #10;
▪ $FINISH;
▪ END
▪ ENDMODULE
▪ Step-by-Step Process
▪ 1. Compile the Design and Testbench
▪ Use ncvlog to compile .sv (SystemVerilog) files:
▪ bash
▪ ncvlog -sv design.sv -mess
▪ ncvlog -sv testbench.sv -mess
▪ -sv tells ncvlog that the files are SystemVerilog.
▪ You can add other flags like +access+r to enable full signal access during simulation.
▪ 2. Elaborate the Design
▪ Use ncelab to elaborate the top-level module (usually your testbench module):
▪ bash
▪ ncelab -access +r testbench
▪ Replace testbench with the name of your top-level module if different.
▪ 3. Simulate the Design
▪ Use ncsim to simulate the elaborated design:
▪ bash
▪ ncsim testbench -gui
SYSTEM VERILOG: BITWISE OPERATORS
Highest
Lowest
Based on: “Digital Design and Computer Architecture (RISC-V Edition)”
by Sarah Harris and David Harris (H&H),
SYSTEM VERILOG: NUMBER FORMAT
Format: N'Bvalue
N = number of bits, B = base
N'B is optional but recommended (default is decimal)
This hardware could be described with assign statements using fewer lines
of code, so it’s better to use assign statements in this case.
Based on: “Digital Design and Computer Architecture (RISC-V Edition)”
by Sarah Harris and David Harris (H&H),
What is System Verilog?
What is SystemVerilog?
55
WHAT IS VERIFICATION
▪ Process of demonstrating functional correctness of a design.
▪ Process that ensures specification is preserved in the
implementation.
▪ Different from validation and testing
– Validation ensures that product/design satisfied user needs
– Testing ensures that product is manufactured correctly
56
WHAT IF WE DON’T VERIFY?
▪ Is it possible to guarantee a design without verification?
– Incorrect/Insufficient Specifications
– Misinterpretation of specifications
– Misunderstanding between designers
– Missed cases
– Protocol non-conformance
▪ Implications on the final system
– Incorrect interaction between IPs/Cores
– Conflict in accessing shared resources
– Deadlock and arbitration
– Priority conflicts in exception handling
57
THE IMPORTANCE OF VERIFICATION
▪ 70% of design effort goes in verification
▪ Bug escapes to silicon can be costly including re-spin
– 50% reason for re-spins are because of bugs. Hence
the need to find all bugs early
▪ Ever increasing complexity of designs makes this
harder
▪ Hence Verification is always on critical path for any
product design
▪ Verification time can be reduced by
– Parallelism
– Abstraction
– Automation 58
VERIFICATION SPACE
▪ What to Verify?
– Functional Verification
– Timing Verification
– Performance Verification
▪ How to Verify?
– Simulation based Verification
– Emulation/FPGA based Verification
– Formal Verification
– Semi-Formal Verification
– HW/SW Co- Verification
59
WHAT IS A VERIFICATION PLAN?
▪ Verification plan is the specification document for verification
effort
– Design specification is the golden reference
– Mechanism to ensure all essential features are verified as
needed
▪ What to verify ?
– Features and under what conditions to verify them
▪ How to verify ?
– What methodologies to use
– Formal, Checking, Coverage etc.
– What should be - Stimulus, Checkers, Coverage
▪ Priority for features to be verified
60
– Schedules should be extractable from a detailed plan
VERIFICATION APPROACHES
▪ Black-Box
▪ Verification without knowledge of design implementation.
▪ Lack of visibility and observability.
▪ Tests are independent of implementation.
▪ Impractical in todays designs.
▪ White Box
▪ Intimate knowledge of design implementation
▪ Full visibility and observability
▪ Tests are tied to a specific implementation
▪ Grey Box
▪ Compromise between above approaches
61
LEVELS OF VERIFICATION
▪ Each level of Verification will be
suited for a specific objective
– Lower levels have better
controllability and visibility
▪ Block level Verification helps
designs to be verified
independently and in parallel
▪ System Level Verification
focuses more on interactions
62
DIRECTED VS RANDOM TESTING DIRECTED
▪ Can only cover Constrained Random
scenarios thought High ramp up time to build smart
through planning test generators
▪ High maintenance cost Need to identify and implement
▪ Works good when Functional Coverage
condition space is finite Deep User control. Complex Test
▪ No need of extensive generator
coverage coding Best balance between engineer
time and compute time
Can have static and dynamic
(during test run) randomness
63
DIRECTED VS. RANDOM TESTING
64
DIRECTED TESTING
▪Hardware specification and verification plan with a list of tests.
▪ „Functional coverage „
▪ Layered testbench using transactors „
▪ Common testbench for all tests
▪ „Test-specific code kept separate from testbench
VERIFICATION METHODOLOGY BASICS
▪ Random stimulus is crucial for exercising complex designs.
▪ A directed test finds the bugs you expect to be in the design, while a
random test can find bugs you never anticipated
CONSTRAINT RANDOM TEST PROGRESS
▪ When the simulator to generate the stimulus, don’t want
totally random values. use the System Verilog language to
describe the format of the stimulus (“address is 32-bits,
opcode is X, Y, or Z, length < 32 bytes”),
▪ The simulator picks values that meet the constraints.
▪ Constraining the random values to become relevant stimuli.
▪ These values are sent into the design, and also into a high-
level model that predicts what the result should be.
▪ The design’s actual output is compared with the predicted
output.
CONSTRAINT RANDOM TEST PROGRESS
CONSTRAINT RANDOM TEST PROGRESS
▪ Every test you create shares this common testbench, as
opposed to directed tests where each is written from
scratch.
▪ Each random test contains a few dozen lines of code to
constrain the stimulus in a certain direction and cause
any desired exceptions, such as creating a protocol
violation. The result is that your single constrained-
random testbench is now finding bugs faster than the
many directed ones.
▪ As the rate of discovery begins to drop off, you can
create new random constraints to explore new areas.
The last few bugs may only be found with directed
tests, but the vast majority of bugs will be found with
random tests.
CONSTRAINED –RANDOM STIMULUS
•Gains in productivity/maintainability/thoroughness
• Recall graphs of directed versus random testing.
•Automatic generation and direction of stimulus
•Functional Coverage
•Higher level RTL
•Required for complex designs
TOOLS SUPPORT FOR SYSTEM VERILOG
THE LOGIC DATA TYPE
In the above Example prints the first array element (binary 101),
its lowest bit (1), and the next two higher bits (binary 10).
ARRAY OPERATIONS EXERCISE
UNPACKED ARRAY EXAMPLE
PACKED ARRAYS
▪ For some data types, you may want both to access the
entire value and also divide it into smaller elements.
▪ For example, you may have a 32-bit register that
sometimes you want to treat as four 8-bit values and at
other times as a single.
▪ Unsigned value. A System Verilog packed array is
treated as both an array and a single value.
▪ It is stored as a contiguous set of bits with no unused
space, unlike an unpacked array.
PACKED ARRAYS
DECLARATION FOR MIXED PACKED/UNPACKED ARRAY
PACKED ARRAYS EXAMPLE
PACKED AND UNPACKED EXERCISE
PACKED AND UNPACKED EXERCISE
DYNAMIC ARRAYS
▪A dynamic array is declared with empty word
subscripts [].
▪ This means
▪ that you do not want to give an array size at compile
time; instead, you specify it at run-time.
▪ The array is initially empty, so you must call the new[]
operator to allocate space, passing in the number of
entries in the square brackets.
▪ If you pass the name of an array to the new[] operator,
the values are copied into the new elements.
DYNAMIC ARRAYS
DYNAMIC ARRAYS
DYNAMIC ARRAYS
USING DYNAMIC ARRAYS
ARRAY METHODS
ARRAY REDUCTION METHODS
ARRAY LOCATOR METHODS
ARRAY LOCATOR
ARRAY LOCATOR
ARRAY SORTING AND ORDERING
QUEUES IN SYSTEM VERILOG
QUEUE OPERATIONS
QUEUES EXERCISE
ASSOCIATIVE ARRAY
▪ Dynamic arrays are good if you want to occasionally
create a large array, but what if you want something
really huge? Perhaps you are modeling a processor that
has a multi-gigabyte address range. During a typical test,
▪ The processor may only touch a few hundred or
thousand memory locations containing executable code
and data.
▪ So allocating and initializing gigabytes of storage is
wasteful.
▪ System Verilog offers associative arrays that store entries
in a sparse matrix.
ASSOCIATIVE ARRAY
▪ This means that while you can address a very large address
space,
▪ System Verilog only allocates memory for an element when
you write to it.
▪ In the following picture, the associative array holds the
values 0:3, 42, 1000,
▪ 4521, and 200,000. The memory used to store these is far
less than would be
▪ needed to store a fixed or dynamic array with 200,000
entries.
ASSOCIATIVE ARRAY
ASSOCIATIVE ARRAY
ASSOCIATIVE ARRAY
ASSOCIATIVE ARRAY CONTD..
CHOOSING STORAGE TYPE
CHOOSING STORAGE TYPE
CHOOSING STORAGE TYPE
CREATING NEW TYPES WITH TYPEDEF
we are not really creating a new type; we are just performing text
substitution. In System Verilog you create a new type.
USER-DEFINED TYPE IN SYSTEM VERILOG
// New SystemVerilog style
parameter OPSIZE = 8;
typedef reg [OPSIZE-1:0] opreg_t;
opreg_t op_a, op_b;
Definition of uint
typedef bit [31:0] uint; // 32-bit unsigned 2-state
typedef int unsigned uint; // Equivalent definition
CREATING NEW TYPES WITH TYPEDEF
CREATING USER-DEFINED STRUCTURES
CREATING A SINGLE PIXEL TYPE
//declaration
enum { red, green, blue, yellow, white, black } Colors;
endmodule
ENUM METHODS
Method Description
returns the value of the first member of the
first()
enumeration
returns the value of the last member of the
last()
enumeration
returns the value of next member of the
next()
enumeration
returns the value of next Nth member of the
next(N)
enumeration
returns the value of previous member of the
prev()
enumeration
returns the value of previous Nth member of
prev(N)
the enumeration
returns the number of elements in the given
num()
enumeration
returns the string representation of the given
name()
enumeration value
module enum_datatype;
//declaration
enum { red=0, green, blue=4, yellow, white=10, black } Colors;
endmodule
module enum_datatype;
//declaration
enum { red=0, green=1, blue=4, yellow, white=5, black=6
} Colors;
initial begin
Colors = Colors.first;
for(int i=0;i<6;i++)
$display("Colors :: Value of %0s is =
%0d",Colors.name(),Colors);
end
endmodule
Example shows declaring new data types using the enumerated type
module enum_datatype;
//declaration
typedef enum {GOOD, BAD} pkt_type;
pkt_type pkt_a;
pkt_type pkt_b;
initial begin
pkt_a = GOOD;
pkt_b=BAD;
if(pkt_a == GOOD)
$display("pkt_a is GOOD packet");
else
$display("pkt_a is BAD packet");
if(pkt_b == GOOD)
$display("pkt_b is GOOD packet");
else
$display("pkt_b is BAD packet");
end
endmodule
ENUMERATED TYPES
ENUMERATED TYPES
ENUMERATED TYPES
ROUTINES FOR ENUMERATED TYPES
Static casting
Multidimensional array
d = Derived'(b); // static cast
int matrix[3][3];
Packed array – bits packed for slicing
int assoc_arr[string];
assoc_arr["Alice"] = 10;
int q[$];
q.push_back(5);
q.push_front(10);
int val = q.pop_front();