You are on page 1of 14

Testbench Architecture & Implementation with SystemVerilog Module #3: Classes

Michael A. Warner Worldwide Consulting Manager


1 SystemVerilog Training Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

Agenda
Verification Planning Testbench Architecture Testbench Implementation
SystemVerilog Basics OOP with SystemVerilog OVM Introduction

2 Lexmark, June 25, 2007

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

Module #3: Classes


Class Basics Inheritance Polymorphism

3 SystemVerilog Training

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

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)

Classes can be used to model


Reusable verification environments Abstract data & methods that operate on them

4 SystemVerilog Training

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

A History of Object Oriented Programming


1967 Simula becomes the first OOP Language
Created as a discrete event simulation language

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

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

The SystemVerilog Class


Class Definitions Contain Data and Methods Classes Are Instantiated Dynamically to Create Objects
Static members create a single element shared by all objects of a particular class type

Objects Are Accessed Via Handles


Safe Pointers, Like Java

Classes Can Inherit Properties and Methods From Other Classes Classes Can Be Parameterized

6 SystemVerilog Training

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

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

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

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

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

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

Uninitialized objects are set to null


task Send_Pkt (Packet P); if (P == null) P = new(); Can test object & initialize if necessary

Object Destruction/De-allocation done automatically when an object is no longer being referenced


NO destructors NO memory leaks NO unexpected side effects
Send_Pkt( Pkt1 ); ... Send_Pkt( Pkt2 ); ... Pkt1 = null; Pkt2 = null;
Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

9 SystemVerilog Training

Working with Object Handles


Packet Pkt1 = new(); Packet Pkt2; Pkt2 = Pkt1; Create & construct an instance of Packet called Pkt1 Create another instance of Packet called Pkt2 There is still only ONE object but it now has two names Pkt1 & Pkt2 Packet Pkt1 = new(); Packet Pkt2 = new Pkt1; This time we call new a second time and construct Pkt2

Shallow copy: All variables are copied to Pkt2. However, objects are NOT copied. Only their handles are copied.

Deep copy: Complete copy of entire class including all objects.

10 SystemVerilog Training

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

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

Constraints are also class properties

11 SystemVerilog Training

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

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();

Class methods have automatic lifetime by default


Can be declared as static

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

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

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

Property declared as static Multiple instances created, Pkt3 is not initialized

Pkt_ID = 1 Pkt_ID = 2 Pkt_ID = 3 even though Pkt3 is null


Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

Static Methods
Static class methods can be called from outside class
Can be used without creating object

Cannot access nonstatic members (properties or methods)


class Packet; static int Pkt_ID = 0; static task inc_ID; Pkt_ID++; endtask : inc_ID endclass : Packet Packet Pkt; Pkt.inc_ID();
14 SystemVerilog Training

Static method can only operate on static properties

Class object does not have to be initialized before method is called

Pkt_ID = 1
Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

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 this keyword is need to identify which X we are referring

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

Stack of 10-bit vectors Stack of reals


Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

Parameterized Classes cont.


A generic class declaration is NOT a type
Only its concrete specialization is a type Each specialization has its own unique copy of static variables If parameter types & sizes are the same, those specializations share static variables
class stack #(parameter type T = int); static int count; ... endclass : stack typedef stack #(bit[7:0]) stack_byte; typedef stack stack_int; stack_byte S1 = new(), S2 = new(); stack_int S3 = new();
17 SystemVerilog Training

stack is not a type

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

S3 get its own variable count

Declaring Methods Externally


Sometimes it is more convenient to move method declarations out of the class body
Inside class body declare method prototype, including class Packet; extern keyword bit [3:0] cmd; extern task set_cmd (input bit [3:0] a); endclass : Packet

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

Copyright 2010 Mentor Graphics Corp.

Module #2: Classes


Class Basics Inheritance Polymorphism

19 SystemVerilog Training

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

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

Copyright 2010 Mentor Graphics Corp.

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

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

Module #2: Classes


Class Basics Inheritance Polymorphism

22 SystemVerilog Training

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

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

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

Abstract Class example


Abstract class declared with virtual keyword virtual class BasePacket; int id; pure virtual task send(bit[0:56] data); endclass Task declared with pure class ATMPacket extends BasePacket; virtual keyword providing bit header[0:5]; basic polymorphic behavior task send (bit[0:56] data); // implmentation described here ... Since task is declared as pure endtask virtual, an implementation must endclass be provided in the extended class

24 SystemVerilog Training

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

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

The task draw becomes polymorphic because of the virtual keyword

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

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

13

27 SystemVerilog Training

Copyright 2001-2003, Copyright 1999-2009, Model Technology Mentor Graphics Corporation

03/04/2010

Copyright 2010 Mentor Graphics Corp.

14

You might also like