Course Systemverilog Oop For Uvm Verification Session3 Oop Design Pattern Examples Drich
Course Systemverilog Oop For Uvm Verification Session3 Oop Design Pattern Examples Drich
[email protected] | 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
• 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
[email protected] | www.verificationacademy.com