Professional Documents
Culture Documents
Agenda
Verification Planning Testbench Architecture Testbench Implementation
SystemVerilog Basics OOP with SystemVerilog OVM Introduction
03/04/2010
3 SystemVerilog Training
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 Class parameterization (similar to C++ template class) Polymorphism Constraints (for random stimulus generation)
4 SystemVerilog Training
03/04/2010
1983 C++ created by merging the concepts of Simula with C 1991 Java begins at Sun as a higher 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
5 SystemVerilog Training
Classes Can Inherit Properties and Methods From Other Classes Classes Can Be Parameterized
6 SystemVerilog Training
03/04/2010
Class Basics
A class is a type
Contains data 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
7 SystemVerilog Training
Instantiating Classes
Classes are dynamically created objects (class instance)
Every object has a built-in method new() called the constructor Can also create user defined constructor that overloads built-in Calling new() creates an instance & allocates memory
class Packet; ... endclass Packet myPkt = new;
myPkt
memory
Command Status Data
IDLE IDLE 5
class Packet; ... function new(); Command = IDLE; endfunction endclass Packet myPkt = new;
class Packet; ... function new(input int a); Command = IDLE; Status = a; endfunction endclass Packet myPkt = new(5);
8 SystemVerilog Training
03/04/2010
Object Handles
The variable name holds an object handle to that class
Packet Pkt1 = new(); Packet Pkt2; Pkt2 is uninitialized so it is set to null
9 SystemVerilog Training
Shallow copy: All variables are copied to Pkt2. However, objects are NOT copied. Only their handles are copied.
10 SystemVerilog Training
03/04/2010
Class Properties
The data fields of a class object are called properties
Class properties are referenced using the object handle
Packet Pkt = new(); initial begin Pkt.Command = Idle; if (Pkt.Status == 3) Out = Pkt.Data[3];
No restrictions on data types of class properties Class properties have automatic lifetime by default
Can be declared as static
11 SystemVerilog Training
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
Pkt.SetCommand(RUN); int S; S = Pkt.GetStatus();
As with Verilog, tasks can block & have event control As with Verilog, functions must be nonblocking Several build-in methods include new() & randomize() Methods can be declared external to the class body
12 SystemVerilog Training
03/04/2010
Static Properties
Unless declared static, each class object has its own copy of the class properties Static properties shared between all instances of class Static properties can be used without creating object
class Packet; static int Pkt_ID = 0; endclass : Packet Packet Pkt1 = new(), Pkt2 = new(), Pkt3; Pkt1.Pkt_ID++; Pkt2.Pkt_ID++; Pkt3.Pkt_ID++;
13 SystemVerilog Training
Static Methods
Static class methods can be called from outside class
Can be used without creating object
Pkt_ID = 1
Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation
03/04/2010
this
The this keyword is used to unambiguously refer to properties or methods of the current instance
Can only be used with non-static class members
class this_example; int X; function new (int X); this.X = X; endfuction : new endclass : this_example this_example C = new(256); $display(C.X = %0d, C.X);
15 SystemVerilog Training
The value 256 is passed to the constructor when the class instance is created The variable X local to class instance C is now equal to 256
Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation
Parameterized Classes
Allows Generic Class to be Instantiated as Objects of different types or sizes
Uses module-like parameter passing
class stack #(parameter type T = int); T items[$]; task push ( T a ); ... endtask function T pop (); ... endtask endclass : stack Keyword parameter is optional
A generic class & actual parameter values is called a specialization Default: stack of ints
stack S_int; stack #(bit[1:10]) S_bit; stack #(real) S_real;
16 SystemVerilog Training
03/04/2010
These specializations create types S1 & S2 are the same type & share the same static varible count
Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation
task Packet::set_cmd (input bit [3:0] a); cmd = a; Tie method back to its class using endtask class name & scope operator
Limitations
Out-of-block method declaration must match the prototype declaration exactly Out-of-block declarations must be declared in the same scope as the class declaration.
18 SystemVerilog Training Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation
03/04/2010
19 SystemVerilog Training
Extending SV Classes
Classes inherit properties & methods from other classes
Called a derived class Subclasses can redefine the parents methods explicitly Allows customization without breaking or rewriting known-good functionality in the parent class Packet:
class ErrPkt extends Packet; bit Error; function bit ShowError(); return(Error); endfunction task SetCommand (input cmd_t a); Command = a + 1; endtask endclass
Command Status Data
SetCommand Command = a; GetStatus
ErrPkt:
Command Status Data Error
GetStatus ShowError SetCommand Command = a+1;
Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation
20 SystemVerilog Training
03/04/2010
10
Interaction of SV Classes
local members are only available to methods inside the class
Not visible in subclasses
class Base; local int i; int a,d; protected task Set_i(input int i); this.i = i; endtask function Put();...endfunction endclass class Sub extends Base; int a; function new(); // prepend super.Put(); // append endfunction task Set_a(input int c); a = c; super.a = c+1; Set_i(c); endtask endclass
protected members are also only available to methods inside the class
Can be inherited
this pointer refers to current instance super pointer refers to parent class
Sub S = new; initial begin S.i = 4; // S.Set_i(4);// S.a = 5; // // S.set_a(5);// S.d = 3; // // end 21
illegal i local to Base illegal Set_i protected legal Base::a hidden by Sub::a legal Set_a unprotected legal d inherited from Base
SystemVerilog Training
22 SystemVerilog Training
03/04/2010
11
Abstract Classes
Prototypes that are not instantiated, only derived
Declared with virtual keyword
Used when several sub-classes are based on the same master class Methods can also be virtual, providing basic polymorphic behavior
An abstract class can contain methods for which there is only a prototype and no implementation An abstract class cannot be instantiated, it can only be derived IEEE 1800-2009 adds pure keyword to indicate methods that have no implementation & MUST be overridden
23 SystemVerilog Training
24 SystemVerilog Training
03/04/2010
12
Polymorphism
Polymorphism
Use a variable in the super-class to hold subclass objects Reference the methods of those subclasses directly from the super-class variable
class shape; virtual task draw (); ... endtask endclass : shape class circle extends shape; task draw (); ... endtask endclass : circle class triangle extends shape; task draw (); ... endtask endclass : triangle
25 SystemVerilog Training Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation
Polymorphism cont.
shape S [100]; circle C = new(); triangle T = new(); rectangle R = new(); initial begin S[0] = C; S[1] = T; S[2] = R; S[0].draw; S[1].draw; S[2].draw; end
26 SystemVerilog Training
Declare an array of the base type to hold all shapes Declare & construct objects of the derived types
Store derived objects in array NOTE: This works because derived classes are type compatible with their parent Draw each shape stored in the array NOTE: This works because the draw task is virtual. Without this polymorphic behavior, you would execute the parents draw task
03/04/2010
13
27 SystemVerilog Training
03/04/2010
14