Anda di halaman 1dari 13

SystemVerilog OOP for UVM Verification

OOP Pattern Examples


Dave Rich
Verification Architect

info@verificationacademy.com | www.verificationacademy.com
OOP & Design Patterns
• OOP patterns are a general reusable solution to commonly occurring
problems
• Some examples:
• Singleton Pattern - Restrict instantiation of a class to one object.
• Factory Pattern - Provide an interface for creating families of related or
dependent objects and specify a policy for how it creates
• Observer Pattern – When one object changes state, all its subscribers are
notified & updated automatically.
• Many, many more patterns exist

© Mentor Graphics Corporation, all rights reserved.


Parameterized Classes
• Generic Class to be Instantiated as Objects of different types or sizes
• Uses module-like parameter passing
class stack #(type T, int W = 5); // #(type T = int)
T items[$];
task push ( T a ); ... endtask
function try_push ( T a ); ... endfunction
bit [W:0] data;
endclass : stack
• A generic class & actual parameter values is called a specialization
stack #(bit[1:10]) S_bit; Stack of 10-bit vectors
stack #(real) S_real; Stack of reals
stack S_int; illegal without default type
© Mentor Graphics Corporation, all rights reserved.
Parameterized Classes with Static Properties
• Static properties do not get allocated until specialized
• Each specialization has unique set of static properties
class stack #(type T = int); These 2 specializations
static int counter=1; create 2 types and 2
int m_cnt; static properties
function new;
m_cnt = counter++; endfunction S1, S2 share same type
endclass : stack and share the same
typedef stack #(byte) stack_byte; static property counter
typedef stack#() stack_int; S3 uses the static property
stack_byte S1 = new(), S2 = new(); counter allocated by stack#(int)
stack S3 = new();
class stacked extends stack#(real); static property counter property
allocated by stack#(real)
© Mentor Graphics Corporation, all rights reserved.
Class Scope Operator
• Access static member properties and methods without instance
• className #(parameter overrides) :: memberName

begin shares the same static


$display( stack #(byte) :: counter ); variable counter
$display( stack_byte :: counter );
$display( stack#() :: counter ); uses the default int
$display( stack :: counter ); illegal without default
$display( stack#(bit):: counter );
creates a new specialization if
end one does not exist regardless of
these statements ever executing
© Mentor Graphics Corporation, all rights reserved.
Singleton with local qualifiers
• guarantees only one instance of a class
class Root extends Component;
local static Root m_root;
local function new ();
super.new(“Root”,null); local constructor requires
endfunction delegation to static method
function void print();
$display(“”);
endfunction
static function Root get();
if (m_root == null) m_root = new();
return m_root;
endfunction static method enforces policy
endclass : Root for construction

© Mentor Graphics Corporation, all rights reserved.


Singleton Example
• construct on first use
const Root my_root = new(); // illegal
Root my_root = Root::get();
MyComponent me = new(“me”, my_root ); //race
MyComponent me = new(“me”, Root::get() )

• Avoids static variable initialization ordering


• my_root
• me
© Mentor Graphics Corporation, all rights reserved.
Factory Pattern
class A; class B; class C;
B b_h = new; C c_h = new; …
endclass endclass endclass

Want extended class D without class D extends C;


modifying classes B or A …
endclass

• Polymorphic Construction
• Delegate construction to another object that can
decide what kind of object to create
• Decouples instantiating class type from the actual type
being constructed
© Mentor Graphics Corporation, all rights reserved.
Proxy Class
• Stand-in for the full object it represents
• Defers creating the actual object until it is requested.
virtual class Object;
Full objects derived from

abstract base class
endclass
virtual class ObjectProxy;
pure virtual function Object createObj();
endclass : ObjectProxy Proxy classes do only one
thing: construct full objects
ObjectProxy factory[string];
h = factory[“D”].createObj(); Factory has a database
of proxy objects
© Mentor Graphics Corporation, all rights reserved.
Proxy Class Example
ObjectProxy factory[string]; class C extends Object;
class ObjectWrapper #(type T) byte Payload1[];
extends ObjectProxy; byte Payload2[];
virtual function T createObj(); endclass : C
T obj; class D extends C;
obj = new(); byte Payload3[];
return obj; ObjectWrapper#(C) c = new; endclass : D
endfunction ObjectWrapper#(D) d = new;
endclass C c_h; Proxy objects
begin
factory[“C"]=c;
register in factory replaces new()
factory[“D"]=d;
$cast(c_h,factory[“C"].createObj());
factory[“C"]=d;
$cast(c_h,factory[“C"].createObj()); overrides C
end creation with D
© Mentor Graphics Corporation, all rights reserved.
Factory Registration
• Use static variable initialization to register proxies
ObjectProxy factory[ObjectProxy];
class objectRegistry#(type T) extends ObjectProxy;
virtual function Object createObj;
T obj = new; singleton proxy object
return obj; for each class type
endfunction
local static objectRegistry#(T) me = get();
static function objectRegistry#(T) get();
if (me == null) begin
me = new(); static initialization registers
factory[me] = me; proxy object with factory
end return me;
endfunction : get static method creates
static function T create();
T obj_h; requested type
$cast(obj_h, factory[get()].createObj());
return obj_h;
endfunction : create
endclass : objectRegistry
© Mentor Graphics Corporation, all rights reserved.
Factory Registration Example
class C extends Object;
typedef objectRegistry#(C) typeId; register C in factory
byte Payload1[];
byte Payload2[];
endclass : C
class D extends C;
typedef objectRegistry#(D) typeId; register D in factory
byte Payload3[];
endclass : D

C c_h;
begin
replaces new()
c_h = C::typeId::create(); overrides C proxy
factory[C::typeId::get()]=D::typeId::get() with D proxy
c_h = C::typeId::create();
end
© Mentor Graphics Corporation, all rights reserved.
SystemVerilog OOP for UVM Verification
OOP Pattern Examples
Dave Rich
Verification Architect

info@verificationacademy.com | www.verificationacademy.com

Anda mungkin juga menyukai