0% found this document useful (0 votes)
4 views

Systemverilog-Module4-Dec2022

Uploaded by

1ds21ec025
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Systemverilog-Module4-Dec2022

Uploaded by

1ds21ec025
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Module4

Ch.4 Basic Object Oriented


Programming
Introduction
► Verilog and C both are procedural programming languages but there is a strong difference
between data structures and codes that uses them
► In Verilog there are no structures , only bit vectors and arrays.
► If we wanted to store information about a bus transaction, we need multiple arrays: one
for the address, one for the data, one for the command etc
► The arrays are all static, so if our test bench only allocated 100array entries, and the
current test needed 101, we would have to edit the source code to change the size and
recompile
► Object Oriented Programming lets us create complex data types and tie them together
with the routines that work with them
► We can create testbenches and system-level models at a more abstract level by calling
routines to perform an action rather than toggling bits
► This will make testbench more robust and easier to maintain and reuse on future projects
• Why OOP?
– Helps in creating and maintaining large testbenches:
• You can create complex data types and tie them together with the routines that work
with them
– Increases productivity:
• You can create testbenches and system-level models at a more abstract level by calling a
routine to perform an action rather than toggling bits
• You can work with transactions rather than signal transitions
– Allows the Testbench to be reused:
• OOP decouples the testbench from design details making it more robust and easier to
maintain and reuse

• How should the data and code be brought together


– The data that flows in and out of the design is grouped together in transactions so
the easiest way to organize the testbench is around the transactions and the
operations that you perform
OOP Basics: Terminology
Blueprint for
• Class
a house
- it is a data type( data and subroutines)
– Programming element “containing” related group of features and functionality
– Encapsulates functionality
– Provides a template for building objects
– Can be used as data structures
A complete • Object
house – An object is an instance of a class

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

Class Object Handle

Turn on/off switches Light switches

Methods Properties
5
OOP Basics: Terminology

HDL OOP
Verilog SystemVerilog
Block definition module class

Block instance instance object

Block name instance name handle

Data Types registers & wires Properties:


Variables
Executable Code behavioral blocks Methods: tasks
(always, initial), and functions
tasks, functions
Communication Ports or cross- calls,
between blocks module task calls mailboxes,
semaphores,
etc.

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;

bit [31:0] addr, crc, data[8]; Class properties

function calc_crc;
crc=addr^data.xor; methods
endfuntion: calc_crc

function display;
$display(“BusTran: %h”, addr); methods
endfuntion: display

endclass: BusTran

Simple BusTran Class


9
Creating New Objects
• Objects (Class Instance)
– An object is an instance of a class
Declare a handle that points to an object of the type
BusTran. When a handle is declared it is initialized to
null

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?

= 32+32+32*8= 320 bits of storage


10
Difference between New() and New[]
• New () function is called to construct a single
object. It can take arguments for setting object
values.
• New[] is used to set the size of dynamic arrays.
It builds an array with multiple elements. It
takes single value for the number of elements
in the array.
Custom Constructor
• You can define your own new() function to set
your own values. But you must not give a
return value type, it automatically returns a
handle to an object of the same type of the
class.
Handles
• Getting a handle on objects
– Three basic steps to using a class are
• Defining class
• Declaring a handle
• Constructing an object

BusTran b1,b2; Declare two handles


b1 = new(); Allocate BusTran object
b2 = b1; b1 & b2 point to it
b1 = new(); Allocate second BusTran object

First BusTran Object


b1 b2

Second BusTran Object

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

BusTran b; Declare a handle


b = new(); Allocate BusTran object
b = new(); Allocate another BusTran object and free the first
b = null; Deallocate the second BusTran Object

First BusTran Object


b

Second BusTran Object

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

BusTran b; Declare a handle to a BusTran


b= new; Construct a BusTran object
b.addr=32’h42; Set the value of a variable
b.display(); Call a routine

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

b2=new; //second instance, id=1 Second instance id=1, count=2


$display(b2.id, b2.count) 1,2
end

•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;

function time how_long;


how_long=stopT-startT;
ntrans++;
total_elapsed_time +=how_long;
endfunction

function void start;


startT=$time;
endfunction
endclass

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

BusTran b1,b2; Declare two handles


b1 = new; Allocate BusTran object
b2 = new b1; Shallow copy

First BusTran Object


Copy integers, strings,
b1 instance handles etc.
Second BusTran Object
b2

43
Handles
• Example: class A ;
class B ;
integer i = 1;
integer j = 5; A a = new;
endclass endclass

function integer test;


B b1 = new; // Create an object of class B
B b2 = new b1; // Create an object that is a copy of b1

b2.i = 10; // i is changed in b2, but not in b1

b2.a.j = 50; // change a.j, shared by both b1 and b2


test = b1.i; // test is set to 1 (b1.i has not changed)

test = b1.a.j; // test is set to 50 (a.j has changed)


endfunction

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

function integer test;


B b1 = new; // Create an object of class B
b1.a.j=10; // j is changed in b1
B b2 = new; // Create an object of class B
b2.copy(b1); // Deep copy.
b2.a.j = 50; // change b2.a.j,
test = b1.a.j; // test is set to 10 (b1.a.j has not changed)
test = b2.a.j; // test is set to 50 (b2.a.j has changed)
endfunction

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

data data=1 data data=2 data=1 data=5

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

class Transaction; Transaction


bit [31:0] src, dst, data[1024], crc;
endclass src dst
data crc
class BadTr extends Transaction;
bit bad_crc;
endclass
BadTr
bad_crc
BadTr bt;
bt = new;
bt.src = 42;
bt.bad_crc = 1;
BadTr =
Transaction +
bad_crc

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

class Transaction; Want to Transaction


bit [31:0] src, dst, data[1024], crc; access this src dst
function void calc_crc();
crc = src ^ dst ^ data.xor;
method
data crc
endfunction
endclass
calc_crc

class BadTr extends Transaction; BadTr


rand bit bad_crc;
function void calc_crc();
bad_crc
super.calc_crc();
if (bad_crc) crc = ~crc; calc_crc
endfunction
endclass

50

You might also like