Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 83

CLASSES IN SYSTEM

VERILOG
Presented by:
Rajesh Panda
Design Trainee
Tessolve Semiconductor
CONTENTS
• Overview of CLASS • Data hiding and encapsulation
• Objects • Constant class properties
• Object methods • Abstract classes and virtual
• Constructors methods
• Static class properties • Polymorphism: dynamic method
• Static methods lookup
• Class scope resolution operator
• This
• Out of block declarations
• Assignment, re-naming and copying
• Parameterized classes
• Inheritance and subclasses
• Typedef class
• Overridden members
• Classes and structures
• Super
• Memory management
• Casting
• Chaining constructors
OVERVIEW OF CLASS

• A class defines a data type .


• A class is a type that includes data and subroutines (functions and tasks) that operate on that data.
• A class’s data is referred to as class properties, and its subroutines are called methods, both are members
of the class.
• Classes allow objects to be dynamically created, deleted, assigned, and accessed via object handles.
• Classes offer inheritance and abstract type modeling, which brings the advantages of C function pointers
thus, bringing true polymorphism into Verilog.
• A common convention is to capitalize the first letter of the class name, so that it is easy to recognize class
declarations

• Syntax: [ virtual ] class [ lifetime ] class_identifier [ parameter_port_list ]


[ extends class_type [ ( list_of_arguments ) ] ];
{ class_item }
endclass [ : class_identifier]
OBJECTS
• An object is an instance of that class.
• An object is used by first declaring a variable of that class type.
• Accessing non-static members or virtual via a null object handle is illegal.
• System Verilog objects are referenced using an object handle.
• Any data-type can be declared as a class property, except for net types since they are
incompatible with dynamically allocated data.
• Uninitialized object handles are set by default to the special value null.
Example:
class obj_example;
int x;
function print();
….
endfunction
endclass
  obj_example myexample;//handle
myexample.x=5;
myexample.print();
OBJECT METHODS

• An object’s methods can be accessed using the same syntax used to access class
properties.
• Class properties are freely and broadly available to the methods of the class, but
each method only accesses the properties associated with its object, i.e., its
instance.
CONSTRUCTORS
• System Verilog does not require the complex memory allocation and deallocation of C++.
• Construction of an object is straightforward and garbage collection is implicit and
automatic.
• Every class has a default (built-in) new method.
• The default constructor first calls its parent class constructor (super.new ()) and then
proceeds to initialize each member of the current object to its default (or uninitialized
value).
• new is now being used in two very different contexts with very different semantics.
Example : Packet p1;
p1=new;
EXAMPLE OF CONSTRUCTOR
OUTPUT
OUTPUT
STATIC CLASS PROPERTIES
• Sometimes only one version of a
variable is required to be shared by all
instances. These class properties are
created using the keyword static.
• Static class properties can be used
without creating an object of that type
STATIC METHODS
• Methods can be declared as static.
• A static method is subject to all the class scoping and access rules, but behaves like a
regular subroutine that can be called outside the class, even with no class instantiation.
• A static method has no access to non-static members (class properties or methods), but it
can directly access static class properties or call static methods of the same class.
• Access to non-static members or to the special this handle within the body of a static
method is illegal and results in a compiler error.
• Static methods cannot be virtual.
• A static method is different from a method with static lifetime.
WHAT IS OUTPUT ?
Scope resolution operator
OUTPUT
THIS

• The ‘this’ keyword is used to unambiguously refer to class properties or


methods of the current instance.
• The ‘this’ keyword shall only be used within non-static class methods,
otherwise an error shall be issued.
ASSIGNMENT, RE-NAMING AND COPYING
• Declaring a class variable only creates the name by which the object is known
that can hold the handle of an object of class Packet
Example Packet p1;
•  The object does not exist, and p1 does not contain an actual handle, until an instance of type Packet is created
Example p1 = new;
• If another variable is declared and assigned the old handle, p1, to the new one , then there is still only one object,
which can be referred to with either the name p1 or p2.
  For example Packet p2;
p2 = p1;
  Packet p1;
Packet p2;
p1 = new;
p2 = new p1; // class properties are copied from p1
Shallow copy, only does not copy objects.
Object assignment
O/P
Syllow copy
Deep copy
INHERITANCE AND SUBCLASSES
• Inheritance is the mechanism which allows a class A to inherit properties of a class B.
• Objects of a subclass can be used where objects of the corresponding superclass are
expected.
• This is due to the fact that objects of the subclass share the same behaviour as objects of
the superclass.
• Superclasses are also called parent classes. Subclasses may also be called child classes
• The idea of inheritance is simple but powerful: When you want to create a new class
and there is already a class that includes some of the code that you want, you can
derive your new class from the existing class.
• A subclass inherits all the members (fields, methods, and nested classes) from its
superclass.
• You can declare new fields in the subclass that are not in the superclass.
INHERITANCE (continued…..)
SUPER
• The super keyword is used from within a derived class to refer to
members of the parent class.
• It is necessary to use super to access members of a parent class when
those members are overridden by the derived class.
• When using the super within new, super.new shall be the first
statement executed in the constructor.
• This is because the superclass must be initialized before the current
class and if the user code doesn’t provide an initialization, the compiler
shall insert a call to super.new automatically.
CASTING

• It is always legal to assign a subclass variable to a variable of a class


higher in the inheritance tree.(p=c)
• It is never legal to directly assign a superclass variable to a variable of
one of its subclasses.(c=p[not possible] )
• However, it is legal to assign a superclass handle to a subclass variable
if the superclass handle refers to an object of the given subclass
• To check if the assignment is legal, the dynamic cast function $cast()
is used.
CASTING(Continued….)
CASTING(Continued….)
1. class B;
11. class E_2 extends B;
2. virtual task print(); 12. virtual task print();
3. $display(" CLASS B 13. $display(" CLASS E_2 ");
"); 14. endtask
4. endtask 15. endclass
16. program main;
5. endclass
17. initial
18. begin
6. class E_1 extends B; 19. B b;
7. virtual task print(); 20. E_1 e1;
21. E_2 e2;
8. $display(" CLASS
22. e1 = new();
E_1 ");
23. $cast(e2,e1);
9. endtask 24. end
10. endclass 25. endprogram
CHAINING CONSTRUCTORS
 
• When a subclass is instantiated, the class method new()is invoked.
• The first action new() takes, before any code defined in the function is evaluated, i.e is to
invoke the new() method of its superclass, and so on up the inheritance hierarchy.
• Thus, all the constructors are called, in the proper order, beginning with the root base
class and ending with the current class.
DATA HIDING AND ENCAPSULATION

• It is desirable to restrict access to class properties and methods from outside the class by
hiding their names.
• This keeps other programmers from relying on a specific implementation, and it also protects
against accidental modifications to class properties that are internal to the class.
• A member identified as local is available only to methods inside the class. Further, these local
members are not visible within subclasses.
• A protected class property or method has all of the characteristics of a local member, except
that it can be inherited; it is visible to subclasses.
Example for local type
How to access local member by method??
Protected
CONSTANT CLASS PROPERTIES
• Class properties can be made read-only by a 1. Class Jumbo_Packet;
const declaration like any other SystemVerilog 2. const int max_size = 9 * 1024; // global constant
variable. 3. byte payload [];
• Because of class objects are dynamic objects, 4. function new( int size );
5. payload = new[ size > max_size ? max_size : size ];
class properties allow two forms of read-only
6. endfunction
variables: global constants and instance
7. endclass
constants.
• Global constant class properties are those that 8. class Big_Packet;
include an initial value as part of their 9. const int size; // instance constant
declaration. They are similar to other const 10. byte payload [];
variables in that they cannot be assigned a
value anywhere other than in the 11. function new();
declaration. //can be used as static 12. size = $random % 4096; //one assignment in new
• Instance constants do not include an initial 13. payload = new[ size ];
value in their declaration, only the const 14. endfunction
qualifier. This type of constant can be assigned 15. endclass
a value at run-time, but the assignment can only
be done once in the corresponding class
constructor
ABSTRACT CLASSES AND VIRTUAL
METHODS
• A base class sets out the prototype for the subclasses. Since the base class is not intended
to be instantiated, it can be made abstract by specifying the class to be virtual.
• Abstract/virtual classes can also have virtual methods.
• A virtual method overrides a method in all the base classes, whereas a normal method
only overrides a method in that class and its descendants.
• An abstract class can contain methods for which there is only a prototype and no
implementation.
• An abstract/virtual class cannot be instantiated, it can only be derived.
ABSTRACT CLASSES AND VIRTUAL METHODS
(continued………..)
VIRTUAL METHODS (continued………..)
POLYMORPHISM
• Polymorphism allows the use of a variable in the superclass to hold subclass objects, and
to reference the methods of those subclasses directly from the superclass variable.
• Polymorphism means the ability to request that the same Operations be performed by a
wide range of different types of things.
• Polymorphism allows the redefining of methods for derived classes while enforcing a
common interface.
• To achieve polymorphism the 'virtual' identifier must be used when defining the base
class and method(s) within that class.
CLASS SCOPE RESOLUTION
OPERATOR (::)
• In addition, to disambiguating class scope identifiers, the :: operator also allows access to
static members from outside the class
• Scope resolution operator :: can be used to access to public or protected elements of a
superclass from within the derived classes.
The class scope resolution operator enables the following:
Access to type declarations and enumeration named constants declared inside the
class from outside the class hierarchy or from within derived classes.
CLASS SCOPE RESOLUTION OPERATOR
OUT OF BLOCK DECLARATIONS
• It is convenient to be able to move method definitions out
of the body of the class declaration. Class Packet;
• This is done in two steps. Declare, within the class body, Packet next;
the method prototypes and the full argument specification function Packet get_next();
plus the extern qualifier. get_next = next;
• The extern qualifier indicates that the body of the method endfunction
is to be found outside the declaration. Then, outside the
class declaration, declare the full method—like the extern protected virtual function int send(int value);
prototype but without the qualifiers—and, to tie the endclass
method back to its class, qualify the method name with the
class name and a pair of colons
• The out of block method declaration must match the function int Packet::send(int value);
prototype declaration exactly; the only syntactical ………// body of method
difference is that the method name is preceded by the class ...
name and scope operator (::). endfunction
PARAMETERIZED CLASSES
• It is often useful to define a generic class whose objects can be instantiated to have
different array sizes or data types. This avoids writing similar code for each size or type,
and allows a single specification to be used for objects that are fundamentally different.
• Instances of this class can then be instantiated like modules or interfaces

Example: class vector #(int size = 1);


vector #(10) vten; // object with vector of size 10
vector #(.size(2)) vtwo; // object with vector of size 2
typedef vector#(4) Vfour; // Class with vector of size 4
PARAMETERIZED
CLASSES(Continued…)
1. class Base#(int size = 3);
2. bit [size:0] a; 13. B1 = new();
14. B2 = new();
15. B3 = new();
3. task disp(); 16. B1.disp();
4. $display(" Size of the vector a is %d ",$size(a)); 17. B2.disp();
18. B3.disp();
5. endtask 19. end
6. endclass 20. endprogram
7. program main();
8. initial
9. begin
10. Base B1;
11. Base#(4) B2;
12. Base#(5) B3;
TYPEDEF CLASS
• Sometimes a class variable needs to be declared before the class itself has been
declared. For example, if two classes each need a handle to the other.
• When, in the course of processing the declaration for the first class, the compiler
encounters the reference to the second class, that reference is undefined and the compiler
flags it as an error.
MEMORY MANAGEMENT
• Memory for objects, strings, and dynamic and associative arrays is allocated dynamically.
• When an object is no longer needed, SystemVerilog automatically reclaims the memory,
making it available for re-use.
• Not a single process has enough information to determine when it is safe to free the object.
• Only two options available to the user are (1) play it safe and never reclaim the object, or (2)
add some form of reference count that can be used to determine when it might be safe to
reclaim the object
• Adopting the first option can cause the system to quickly run out of memory.
• The second option places a large burden on users
• To avoid these shortcomings, SystemVerilog manages all dynamic memory automatically.
THANK YOU

You might also like