Systemverilog-Module4-Dec2022
Systemverilog-Module4-Dec2022
Address of • Handle
a house – Type-safe pointer to an object – can not be corrupted
Light • Properties
switches – Variables contained in the instance of the class
• Methods
Turn on/off – Tasks/functions (algorithms) that operate on the properties in this instance of the
switches class
4
OOP Basics: Terminology
Blueprint for a house A complete house House Address
123 Elm Street
Methods Properties
5
OOP Basics: Terminology
HDL OOP
Verilog SystemVerilog
Block definition module class
6
OOP Basics: Advantages
• Traditional programming deals with data structures and algorithms
separately
• OOP organizes transactions and transactors better through
encapsulation
– Classes encapsulate (group) data and algorithms together logically
– Objects are just an instance of a class
– Classes are composed of ”members”.
– Members are either properties (data/variables) or methods
(functions/tasks).
• OOP allows for characteristics & functionality of existing classes to be
extended - inheritance
• OOP enables binding of data with functions at runtime - polymorphism
7
Where to Define a Class
► We can define a class in System Verilog in a program, module,
package, or outside of any of these.
► Classes can be used in programs and modules.
► We can think of a program block as a module that holds our test code
► The program holds a single test and contains the objects that
comprise the testbench, and the initial blocks to create, initialize, and
run the test
► Many verification teams put either a standalone class or a group of
closely related classes in a file
► Bundle the group of classes with a System Verilog package
► Now we can compile the package separately from the test of the
system.
► Unrelated classes, such as those for transactions, score-boards, or
different protocols, should go into separate files.
OOP: Your First Class
• Your first class
– A class encapsulates the data together with the routines that manipulate it
• A class’s data is referred to as class properties
• Subroutines of a class are called methods
class BusTran;
function calc_crc;
crc=addr^data.xor; methods
endfuntion: calc_crc
function display;
$display(“BusTran: %h”, addr); methods
endfuntion: display
endclass: BusTran
BusTran b;
b= new();
Call the new function to construct the BusTran object. When
Declaring and using a handle
you call new you are allocating a new block of memory to
store variables for that object.
1. new allocates space for BusTran
2. Initializes the variables to their default value (0 for 2 state
and x for 4-state variables)
3. Returns the address where the object is stored
BusTran has two 32-bit registers (addr and crc) and an array with 8, 32 bit entries.
How much space would new allocate for an object of BusTran?
14
Object Deallocation
• Deallocating a handle on objects
– SystemVerilog deallocates an object if there are no handles pointing to it
– SystemVerilog does not deallocate an object that is being referenced by a handle
– Need to manually clear all handles by setting them to null
15
Using Objects
• Refer to variables and routines using the .
notation
class BusTran;
bit [31:0] addr, crc, data[8];
function calc_crc;
crc=addr^data;
endfuntion: calc_crc
function display;
$display(“BusTran: %h”, addr);
endfuntion: display
endclass: BusTran
16
Class Routines
• A routine in a class is a function or a task defined inside the scope of a
class
class BusTran;
bit [31:0] addr, crc, data[8];
function void display;
$display(“BusTran”,addr,crc); BusTran b;
endfuntion: display PCITran pc;
endclass: BusTran initial begin
b=new();
b.display();
pc=new();
pc.display();
end
class PCITran;
bit [31:0] addr, data[8];
function void display;
$display(“PCITran: %h”, addr, data);
endfuntion: display
endclass: PCITran
17
Class methods(task /function)
Summary- Class data type
Defining Methods outside of the class( using
“extern”and ::scope operator)
Static variables
• A class can have static properties and static
methods (functions and tasks).
• When a variable inside a class is declared as
static ,that variable will be a shared copy in all
class instances.
• Class members with the keyword static are
called as static class members.
• static <data_type> <property_name>;
Example code without static variables
Static Variables
Static Variables
• How do I create a variable shared by all objects of a class, but not
make it global?
– SystemVerilog allows you to create a static variable inside a class
• The static variable is associated with the class definition, not the instantiated
object
• It is often used to store meta-data, such as number of instances constructed
• It is shared by all objects of that class
class Transaction;
static int count = 0; Number of objects created
int id; Unique instance id
function new();
id = count++; Set id and bump count
endfunction
endclass
Using a id field can help keep track of transactions as they flow through test
26
Static Variables
Transaction b1,b2;
initial begin
b1=new; //first instance, id=0
$display(b1.id, b1.count) First instance id=0, count=1
•There is only one copy of the static variable count regardless of how many BusTran
objects are created
•The variable id is not static so every BusTran has its own copy
27
Static methods
• Static methods are the same as static
properties,
• A static method can access only static
properties of the class and access to the non-
static properties is illegal and lead to a
compilation error.
static task/function <method_name>;
Example code-static method
STATIC METHOD TRYING TO ACCESS A NON-STATIC VARIABLE
Static methods
Using One Class Inside Another
• A class can contain an instance of another class, using a handle to an
object
– This is similar to Verilog’s concept of instantiation
– Done for reuse and controlling complexity
class BusTran;
bit [31:0] addr, src, data[1024], crc;
Statistics stats;
endclass
class Statistics;
time startT, stopT;
static int ntrans=0;
static time total_elapsed_time;
endclass
32
Using One Class Inside Another
• Statistics class example
class Statistics;
time startT, stopT; //Transaction time
static int ntrans=0; //Transaction count
static time total_elapsed_time=0;
33
Using One Class Inside Another
• Using hierarchical syntax
class BusTran;
bit [31:0] addr, src, data[1024], crc;
Statistics stats;
function new();
stats=new(); Instantiate the object
endfunction
task create_packet();
// Fill packet with data
stats.start(); Use hierarchical syntax
// Transmit packet
endtask
endclass
34
Copying Objects
• Types:
Shallow Copy: Objects will not be copied, only
their handles will be copied.
Deep Copy: All the class members and its
nested class members are copied.
Copying Objects
Copying Objects
Copying Objects
Copying Objects
Deep copy
Deep Copy
• SystemVerilog’s ‘deep copy’ copies all the class
members and its nested class members.
In shallow copy, Objects will not be copied,
only their handles will be copied.
• To perform a full or deep copy, the custom
method needs to be added
• In the custom method, a new object is created,
all the class properties are copied to a new
handle and the new handle will be returned
Deep Copy example
Handles
• Getting a handle on objects
– Shallow copy
43
Handles
• Example: class A ;
class B ;
integer i = 1;
integer j = 5; A a = new;
endclass endclass
a a
j=5 j=50
b1 b2 b2 b2
i=1; i=1; i=10; i=10;
a a a a test=1 test=50
B b1 = new; B b2 = new b1; b2.i = 10; b2.a.j = 50; test = b1.i; test = b1.a.j;
44
Handles
• Getting a handle on objects
– Class properties and instantiated objects can be initialized directly in a class
declaration
– Shallow copy does not copy objects
– Instance qualifications can be chained as needed to reach through objects
b1.a.j
– Can write custom code to do a full copy of everything including nested objects
BusTran b1,b2;
b1 = new;
b2 = new;
b2.copy(b1); Deep copy
45
Handles
• Deepclass
CopyA ;
class B ;
integer i = 1;
integer j = 5;
A a = new;
endclass
endclass
a a a a
j=5 j=10 j=10 j=50
b1 b1 b2 b2
i=1; i=1; i=1; i=1;
a a a a test=10 test=50
B b1 = new; b1.j = 10; B b2=new; b2.a.j = 50; test = b1.a.j;
test = b2.a.j;
b2.copy(b1)
46
Exercise
•Write code for the sequence of handles and operations shown
t1 t1 t1 t2 t2 t2 t2
t2 t1
t1
class Thing;
int data;
endclass
…
Thing t1, t2; // Two handles
initial begin
t1 = new(); // Construct first thing
t1.data = 1;
t2 = new(); // Construct second
t2.data = 2;
t2 = t1; // Second thing is lost
t2.data = 5; // Modifies first thing
$display(t1.data); // Displays “5”
end
47
Inheritance
• How do I share code between classes?
– Instantiate a class within another class
– Inherit from one class to another (inheritance/derivation)
• Inheritance allows you to ‘add’ extra:
– Add extra Properties (data members)
– Add extra Methods
– Change the behavior of a method
• Common code can be grouped into a base class
– Additions and changes can go into the derived class
• Advantages:
– Reuse existing classes from previous projects with less debug
48
Inheritance
Add additional functionality to an existing class
49
Inheritance
• Change the current functionality of a class: Single Inheritance
– super keyword is used from within the extended class to refer to members of the
parent class
– It is necessary to use super to access members of a parent class when those
members are overridden by the derived class
50