Uploaded by SRIRAM SESHAGIRI

course-systemverilog-oop-for-uvm-verification session1-classes drich

SystemVerilog OOP for UVM Verification
Classes
Dave Rich
Verification Architect
info@verificationacademy.com | www.verificationacademy.com
SV & OOP
• Object Oriented Programming in SystemVerilog is supported through
the class data type
• OOP enables the following concepts
•
•
•
•
•
Encapsulation
Inheritance (single inheritance model)
Data hiding
Generic programming(template/parameterization)
Polymorphism
• Classes can be used to model
• Reusable verification environments
• Abstract data & methods that operate on them
© Mentor Graphics Corporation, all rights reserved.
A History of Object Oriented Programming
• 1967 Simula becomes the first OOP Language
• 1983 C++ created by merging the concepts of Simula
with C
• 1991 Java begins at Sun as a highly reliable alternative
to C++; originally called C++ -• 1994 Sun R&D creates Vera as a testbench language for
Verilog designs
• 2002 Vera incorporated as part of SystemVerilog
• 2005 SystemVerilog becomes an IEEE standard
© Mentor Graphics Corporation, all rights reserved.
The SystemVerilog Class Terminology
• Class Types define a collection of Data and Methods
• Class Types Are Constructed Dynamically to Create
Class Objects
• Class Variables Store Class Handles
• Class Objects Are Accessed Via Class Handles
• The term “Class” is heavily overloaded
• Many people just use “Class” when they mean “Class
Type”, “Object”, “Handle”, or “Variable”
© Mentor Graphics Corporation, all rights reserved.
Class Basics
• A class is a data type
• Contains variables referred to as class properties
• Contains subroutines (task/functions) referred to as class methods
• Both properties & methods are members of the class
Note:
Class declaration does
not allocate any storage,
it only creates a new type
typedef enum {IDLE, RUN, ...} cmd_t;
class Packet;
cmd_t Command;
int Status;
logic [31:0] Data [0:255];
function int GetStatus();
return(Status);
endfunction : GetStatus
task SetCommand (input cmd_t a);
Command = a;
endtask : SetCommand
endclass : Packet
© Mentor Graphics Corporation, all rights reserved.
Instantiating Classes
• Classes are dynamically created objects (class instance)
• Every class type has a built-in method new() ,the constructor
• Can also create user defined constructor that overrides built-in
• Calling new() creates an instance & allocates memory
class Packet;
...
endclass
Packet myPkt = new;
class Packet;
...
function new();
Command = IDLE;
endfunction
endclass
Packet myPkt = new;
myPkt
class Packet;
...
function new(input int a);
Command = IDLE;
Status = a;
endfunction
endclass
Packet myPkt = new(5);
memory
Command
Status
IDLE
5
Data
© Mentor Graphics Corporation, all rights reserved.
Object Handles
• A class variable holds a handle referencing an object
Packet Pkt1_h = new();
Packet Pkt2_h;
Pkt2_h is initially
null
• Uninitialized variables have the special value null
task Send_Pkt (Packet P_h);
Can test object &
initialize if necessary
if (P_h == null) P_h = new();
• Object Destruction/De-allocation done automatically after
an object has no references
• NO memory allocation/de-allocation
• NO access to de-allocated objects
© Mentor Graphics Corporation, all rights reserved.
Automatic Memory Management
• Compiler traces all active object references
Packet Pkt1_h, Pkt2_h; // _h used to indicate a class variable
begin
Pkt1_h = new(); // 1st object constructed
Pkt2_h = new(); // 2nd object constructed
Pkt1_h = new(); // 3rd object – 1st object can be reclaimed
Pkt2_h = Pkt1_h; // 2nd object can be reclaimed
Pkt1_h = new(); // 4th object constructed
Pkt2_h = null;
// 3rd object can be reclaimed
end
• Object reclaimed when no class variables reference it
© Mentor Graphics Corporation, all rights reserved.
Class Properties
• The variables inside a class object are class properties
• Properties can be of any type including other class variables
• Class properties have dynamic lifetime – the life of the object
• Class properties are referenced using the object handle in a variable
Packet Pkt_h;
initial begin
Pkt_h = new();
Pkt_h.Command = Idle;
if (Pkt_h.Status == 3) Out = Pkt_h.Data[3];
© Mentor Graphics Corporation, all rights reserved.
Class Methods
• Tasks & functions in a class object are referred to as class
methods
• The methods of a class object are referenced in the same manner as
properties
int S;
Pkt_h.SetCommand(RUN);
S = Pkt_h.GetStatus();
• As with Verilog, tasks can block and consume time
• As with Verilog, functions must be non-blocking and can return values
© Mentor Graphics Corporation, all rights reserved.
this
• The this keyword is a implicit argument to a method
that refers to the current object
class example;
int X;
function void setX(int X);
this.X = X;
“this” keyword distinguishes the
class member X from the local
function argument X.
endfuction : setX
endclass : example
example C = new()
“this” is implicitly assigned to the
handle that C refers to
C.setX(256);
$display(“C.X = %0d”, C.X);
© Mentor Graphics Corporation, all rights reserved.
Copying Handles versus Copying Objects
Packet Pkt1_h, Pkt2_h;
begin
Pkt1_h = new();
Pkt2_h = Pkt1_h;
Copies class handle only Pkt1_h and Pkt2_h refer to
the same object
Pkt1_h.status = 5;
$display(Pkt2_h.status);
Pkt2_h = new() Pkt1_h;
Pkt2_h.status = 10;
Shallow Copies Object Pkt1_h and Pkt2_h refer to
the different object with
the property values copied
$display(Pkt1_h.status);
© Mentor Graphics Corporation, all rights reserved.
Shallow Copy Example
class A;
int j = 5;
endclass
class B;
int i = 1;
A a = new;
endclass
B b1, b2;
initial begin
b1 = new;
b2 = new b1;
b2.i = 10;
b2.a.j = 50;
end
Class declaration can contain variables
of other classes
Shallow copy of object b1 copies first level
of properties to b2
Assigns 10 only to variable “i” in object b2
Assigns 50 to variable “j” shared by both b1 & b2
b1.i = 1
b2.i = 10
b1.a.j = 50
b2.a.j = 50
© Mentor Graphics Corporation, all rights reserved.
Deep Copy Example
class A;
In order to do a “deep” copy, a custom
int j = 5;
function must be created
endclass
class B;
B b1=new, b2=new;
int i = 1;
initial begin
A a = new;
b2.copy(b1);
function void copy(B source);
b2.i = 10;
this.i = source.i;
b2.a.j = 50;
this.a = new source.a;
...
endfunction : copy
end
endclass
Deep copy: Complete copy
b1.i = 1
b2.i = 10
of entire class including all
contained objects.
b1.a.j = 5
b2.a.j = 50
© Mentor Graphics Corporation, all rights reserved.
Static Properties
•
•
•
Each class object has its own copy of the class properties
Static properties shared between all instances of class
Can refer to Static properties without creating an object
class Packet;
int id;
Allocated when
constructing object
static int Pkt_ID;
Global variable declared
inside a class
Packet::Pkt_ID
endclass : Packet
begin
Two objects created
Packet Pkt1_h = new(), Pkt2_h = new();
Pkt1_h.id = Packet::Pkt_ID++;
Pkt_ID = 1
Pkt2_h.id = Packet::Pkt_ID++;
Pkt_ID = 2
© Mentor Graphics Corporation, all rights reserved.
Static Methods
•
•
Static class methods can be called without a specific object
Cannot access non-static members (no this object)
class Packet;
int id;
static int Pkt_ID = 0;
static function int unique_id;
return Pkt_ID++;
endfunction : inc_ID
endclass : Packet
Two objects created
begin
Packet Pkt1_h = new(), Pkt2_h = new();
Pkt_ID = 1
Pkt1_h.id = Packet::unique_id;
Pkt2_h.id = Packet::unique_id;
Pkt_ID = 2
© Mentor Graphics Corporation, all rights reserved.
Static Lists
class Component;
Better to use a unique
int m_X;
name instead of this.X
static Component list[$];
function new (int X);
m_X = X;
queue of class handles of itself
list.push_back(this);
endfuction : new
static function void all();
foreach (Component::list[i])
Each construction adds a
$display(Components::list[i].m_X);
new object to the list
endfunction : all
endclass : Component
begin
Component h1 = new(7);
Component h2 = new(42);
Component::all;
© Mentor Graphics Corporation, all rights reserved.
SystemVerilog OOP for UVM Verification
Classes
Dave Rich
Verification Architect
info@verificationacademy.com | www.verificationacademy.com