Sie sind auf Seite 1von 18

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
typedef enum {IDLE, RUN, ...} cmd_t;
class Packet;
Note: cmd_t Command;
int Status;
Class declaration does logic [31:0] Data [0:255];
function int GetStatus();
not allocate any storage, return(Status);
endfunction : GetStatus
it only creates a new type 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;
...
myPkt memory
endclass
Packet myPkt = new;
Command IDLE
class Packet; Status 5
...
class Packet;
... function new(input int a); Data
Command = IDLE;
function new();
Status = a;
Command = IDLE;
endfunction
endfunction
endclass
endclass
Packet myPkt = new(5);
Packet myPkt = new;
Mentor Graphics Corporation, all rights reserved.
Object Handles
A class variable holds a handle referencing an object
Packet Pkt1_h = new(); Pkt2_h is initially
Packet Pkt2_h; null
Uninitialized variables have the special value null
Can test object &
task Send_Pkt (Packet P_h); 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;
this keyword distinguishes the
function void setX(int X); class member X from the local
function argument X.
this.X = X;
endfuction : setX
endclass : example this is implicitly assigned to the
example C = new() 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
Copies class handle only -
Pkt1_h = new();
Pkt1_h and Pkt2_h refer to
Pkt2_h = Pkt1_h; the same object
Pkt1_h.status = 5;
$display(Pkt2_h.status); Shallow Copies Object -
Pkt1_h and Pkt2_h refer to
Pkt2_h = new() Pkt1_h;
the different object with
Pkt2_h.status = 10; the property values copied
$display(Pkt1_h.status);

Mentor Graphics Corporation, all rights reserved.


Shallow Copy Example
class A; Class declaration can contain variables
int j = 5; of other classes
endclass
class B; Shallow copy of object b1 copies first level
int i = 1; of properties to b2
A a = new;
endclass

B b1, b2; Assigns 10 only to variable i in object b2


initial begin
b1 = new;
Assigns 50 to variable j shared by both b1 & b2
b2 = new b1;
b2.i = 10; b1.i = 1 b2.i = 10
b2.a.j = 50;
end b1.a.j = 50 b2.a.j = 50
Mentor Graphics Corporation, all rights reserved.
Deep Copy Example
class A;
int j = 5; In order to do a deep copy, a custom
endclass function must be created
class B;
int i = 1; B b1=new, b2=new;
A a = new; initial begin
function void copy(B source); b2.copy(b1);
this.i = source.i; b2.i = 10;
this.a = new source.a; b2.a.j = 50;
endfunction : copy ...
endclass end
Deep copy: Complete copy
b1.i = 1 b2.i = 10 of entire class including all
b1.a.j = 5 b2.a.j = 50 contained objects.
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; Allocated when
constructing object Global variable declared
int id;
inside a class
static int Pkt_ID; Packet::Pkt_ID
endclass : Packet
begin Two objects created
Packet Pkt1_h = new(), Pkt2_h = new();
Pkt_ID = 1
Pkt1_h.id = Packet::Pkt_ID++;
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
Two objects created
endclass : Packet
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;
int m_X; Better to use a unique
static Component list[$]; name instead of this.X
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

Das könnte Ihnen auch gefallen