Sie sind auf Seite 1von 25

INTRODUCTION Brief Introduction To OOP Unlike procedural programming, here in the OOP programming model programs are organized

around objects and data rather than actions and logic. Objects represent some concepts or things and like any other objects in the real Objects in programming language have certain behavior, properties, type, and identity. In OOP based language the principal aim is to find out the objects to manipulate and their relation between each other. OOP offers greater flexibility and compatibility then procedural language like verilog. Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your system, your desk, your chair. Real-world objects share two characteristics: They all have state and behavior. System have state (name, color) and behavior (playing music,switch off). Identifying the state and behavior for realworld objects is a great way to begin thinking in terms of object-oriented programming. SystemVerilog is a object oriented programming and to understand the functionality of OOP in SystemVerilog, we first need to understand several fundamentals related to objects. These include class, method, inheritance, encapsulation, abstraction, polymorphism etc. Class It is the central point of OOP and that contains data and codes with behavior. In SystemVerilog OOPS , everything happens within class and it describes a set of objects with common behavior. The class definition describes all the properties, behavior, and identity of objects present within that class.

Object Objects are the basic unit of object orientation with behavior, identity. As we mentioned above, these are part of a class but are not the same. An object is expressed by the variable and methods within the objects. Again these variables and methods are distinguished from each other as instant variables, instant methods and class variable and class methods.

Methods We know that a class can define both attributes and behaviors. Again attributes are defined by variables and behaviors are represented by methods. In other words, methods define the abilities of an object.

Inheritance This is the mechanism of organizing and structuring program. Though objects are distinguished from each other by some additional features but there are objects that share certain things common. In object oriented programming classes can inherit some common behavior and state from others. Inheritance in OOP allows to define a general class and later to organize some other classes simply adding some details with the old class definition. This saves work as the special class inherits all the properties of the old general class and as a programmer you only require the new features. This helps in a better data analysis, accurate coding and reduces development time. In Verilog , to write a new definition using the exesisting definitioan is done inserting `ifdef conpilation controls into the exesisting code.

Abstraction The process of abstraction in SystemVerilog is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object.

Encapsulation This is an important programming concept that assists in separating an object's state from its behavior. This helps in hiding an object's data describing its state from any further modification by external component. In SystemVerilog there are three different terms used for hiding data constructs and these are public, private and protected . As we know an object can associated with data with predefined classes and in any application an object can know about the data it needs to know about. So any unnecessary data are not required by an object can be hidden by this process. It can also be termed as information hiding that prohibits outsiders in seeing the inside of an object in which abstraction is implemented.

Polymorphism It describes the ability of the object in belonging to different types with specific behavior of each type. So by using this, one object can be treated like another and in this way it can create and define multiple level of interface. Here the programmers need not have to know the exact type of object in advance and this is being implemented at runtime. CLASS

A class is an actual representation of an Abstract data type. It therefore provides implementation details for the data structure used and operations. EXAMPLE:

class A ; // attributes: int i // methods: task print endclass Definition (Class) : A class is the implementation of an abstract data type . It defines attributes and methods which implement the data structure and operations of the abstract data type, respectively. Instances of classes are called objects. Consequently, classes define properties and behaviour of sets of objects. In SV classes also contain Constraints for randomization control.

Class Properties: Instance Variables (Non-Static Fields) : Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); Class Variables (Static Fields) : A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. Local Variables : Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared , which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. Parameters : The important thing to remember is that parameters are always classified as "variables" not "fields". Constants : Class properties can be made read-only by a const declaration like any other SystemVerilog variable.

OBJECT Objects are uniquely identifiable by a name. Therefore you could have two distinguishable objects with the same set of values. This is similar to traditional programming languages like verilog where you could have, say two integers i and j both of which equal to 2. Please notice the use of i and j in the last sentence to name the two integers. We refer to the set of values at a particular time as the state of the object. EXAMPLE: class simple ; int i; int j; task printf(); $display( i , j ); endtask endclass program main; initial begin simple obj_1; simple obj_2; obj_1 = new(); obj_2 = new(); obj_1.i = 2; obj_2.i = 4; obj_1.printf(); obj_2.printf(); end endprogram RESULT 2 0 4 0 Definition (Object) An object is an instance of a class. It can be uniquely identified by its name and it defines a state which is represented by the values of its attributes at a particular time. The state of the object changes according to the methods which are applied to it. We refer to these possible sequence of state changes as the behaviour of the object. Definition (Behaviour) The behaviour of an object is defined by the set of methods which can be applied on it. We now have two main concepts of object-orientation introduced, class and object. Object-oriented programming is therefore the implementation of abstract data types or, in more simple words, the

writing of classes. At runtime instances of these classes, the objects, achieve the goal of the program by changing their states. Consequently, you can think of your running program as a collection of objects.

Creating Objects As you know, a class provides the blueprint for objects; you create an object from a class. In the following statements ,program creates an object and assigns it to a variable: packet pkt = new(23, 94); driver drvr = new(pkt,intf); The first line creates an object of the packet class, and the second create an object of the driver class. Each of these statements has three parts (discussed in detail below): 1. Declaration: The code set in bold are all variable declarations that associate a variable name with an object type. 2. Instantiation: The new keyword is a SV operator that creates the object. 3. Initialization: The new operator is followed by a call to a constructor, which initializes the new object. Declaration: Declaring a Variable to Refer to an Object Previously, you learned that to declare a variable, you write: type name; This notifies the compiler that you will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. You can also declare a reference variable on its own line. For example: packet pkt; If you declare pkt like this, its value will be undetermined until an object is actually created and assigned to it using the new method. Simply declaring a reference variable does not create an object. For that, you need to use the new operator. You must assign an object to pkt before you use it in your code. Otherwise, you will get a compiler error. A variable in this state, which currently references no object, can be illustrated as follows (the variable name, pkt, plus a reference pointing to nothing): . During simulation, the tool will not allocate memory for this object and error is reported. There will not be any compilation error.

EXAMPLE: class packet ; int length = 0; function new (int l); length = l; endfunction endclass program main; initial begin packet pkt; pkt.length = 10; end endprogram RESULT Error: null object access Instantiating A Class: The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor. The new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type, like: packet pkt = new(10); Initializing An Object Here's the code for the packet class: EXAMPLE class packet ; int length = 0; //constructor function new (int l); length = l; endfunction endclass This class contains a single constructor. You can recognize a constructor because its declaration uses the same name as the class and it has no return type. The constructor in the packet class takes one

integer arguments, as declared by the code (int l). The following statement provides 10 as value for those arguments: packet pkt = new(10); If a class does not explicitly declare any, the SV compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's noargument constructor, or the Object constructor if the class has no other parent.

Constructor SystemVerilog does not require the complex memory allocation and deallocation of C++. Construction of an object is straightforward; and garbage collection, as in Java, is implicit and automatic. There can be no memory leaks or other subtle behavior that is so often the bane of C++ programmers. The new operation is defined as a function with no return type, and like any other function, it must be nonblocking. Even though new does not specify a return type, the left-hand side of the assignment determines the return type. THIS Using The This Keyword Within an instance method or a constructor, this is a reference to the current object , the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter. For example, in the packet class was written like this EXAMPLE class packet ; int length = 0; //constructor function new (int l); length = l; endfunction endclass but it could have been written like this: EXAMPLE:

class packet ; int length = 0; //constructor function new (int length); this.length = length; endfunction endclass program main; initial begin packet pkt; pkt =new(10); $display(pkt.length); end endprogram RESULT 10 Each argument to the second constructor shadows one of the object's fields inside the constructor "length" is a local copy of the constructor's first argument. To refer to the legnth field inside the object , the constructor must use "this.length". INHERITANCE

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Definition (Inheritance) Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say ``A inherits from B''. Objects of class A thus have access to attributes and methods of class B without the need to redefine them. The following definition defines two terms with which we are able to refer to participating classes when they use inheritance. Definition (Superclass) If class A inherits from class B, then B is called superclass of A. Definition (Subclass) If class A inherits from class B, then A is called subclass of 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 or extended classes or just derived classes . Of course, you can again inherit from a subclass, making this class the superclass of the new subclass. This leads to a hierarchy of superclass/subclass relationships.

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. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

What You Can Do In A Subclass: A subclass inherits all of the public and protected members of its parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members: The inherited fields can be used directly, just like any other fields. You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the subclass that are not in the superclass. The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. You can declare new methods in the subclass that are not in the superclass. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

Overriding Following example shows, adding new varibles, new methods, redefining existing methods.

EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS ");

endtask endclass class subclass extends parent; task printf(); $display(" THIS IS SUBCLASS "); endtask endclass program main; initial begin parent p; subclass s; p = new(); s = new(); p.printf(); s.printf(); end endprogram RESULT THIS IS PARENT CLASS THIS IS SUBCLASS

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. EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS "); endtask endclass class subclass extends parent; task printf(); super.printf(); endtask

endclass program main; initial begin subclass s; s = new(); s.printf(); end endprogram RESULT THIS IS PARENT CLASS

The member can be a member declared a level up or be inherited by the class one level up. There is no way to reach higher (for example, super.super.count is not allowed). Subclasses (or derived classes) are classes that are extensions of the current class whereas superclasses (parent classes or base classes) are classes from which the current class is extended, beginning with the original base class. NOTE: 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 does not provide an initialization, the compiler shall insert a call to super.new automatically.

Is Only Method Programmers can override the existing code/functionality before existing code and replaces with new code as shown in below example. EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS "); endtask endclass class subclass extends parent; task printf();

$display(" THIS IS SUBCLASS "); endtask endclass program main; initial begin subclass s; s = new(); s.printf(); end endprogram RESULT: THIS IS SUBCLASS

Is First Method Programmers can add new lines of code/functionality before existing code as shown in below example. EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS "); endtask endclass class subclass extends parent; task printf(); $display(" THIS IS SUBCLASS "); super.printf(); endtask endclass program main; initial begin subclass s; s = new(); s.printf();

end endprogram RESULT: THIS IS SUBCLASS THIS IS PARENT CLASS

Is Also Method Programmers can add new lines of code/functionality after the existing code as shown in below example. EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS "); endtask endclass class subclass extends parent; task printf(); super.printf(); $display(" THIS IS SUBCLASS "); endtask endclass program main; initial begin subclass s; s = new(); s.printf(); end endprogram RESULT: THIS IS PARENT CLASS THIS IS SUBCLASS

Overriding Constraints.

Programmers can override the existing constraint and replaces with new constraint as shown in below example. EXAMPLE: class Base; rand integer Var; constraint range { Var < 100 ; Var > 0 ;} endclass class Extended extends Base; constraint range { Var < 100 ; Var > 50 ;} // Overrighting the Base class constraints. endclass program inhe_31; Extended obj; initial begin obj = new(); for(int i=0 ; i < 100 ; i++) if(obj.randomize()) $display(" Randomization sucsessfull : Var = %0d ",obj.Var); else $display("Randomization failed"); end endprogram RESULT: Randomization sucsessfull : Var = 77 Randomization sucsessfull : Var = 86 Randomization sucsessfull : Var = 76 Randomization sucsessfull : Var = 89 Randomization sucsessfull : Var = 86 Randomization sucsessfull : Var = 76 Randomization sucsessfull : Var = 96

Overriding Datamembers Only virtual methods truly override methods in base classes. All other methods and properties do not override but provide name hiding.

EXAMPLE class base; int N = 3; function int get_N(); return N; endfunction endclass class ext extends base; int N = 4; function int get_N(); return N; endfunction function int get_N1(); return super.N; endfunction endclass program main; initial begin ext e = new; base b = e; // Note same object! $display(b.get_N()); // "3" $display(e.get_N()); // "4" $display(e.get_N1()); // "3" - super.N end endprogram RESULT 3 4 3 ENCAPSULATION

Encapsulation is a technique for minimizing interdependencies among modules by defining a strict external communication. This way, internal coding can be changed without affecting the communication, so long as the new implementation supports the same (or upwards compatible) external communication.

Encapsulation prevents a program from becoming so interdependent that a small change has massive ripple effects. The implementation of an object can be changed without affecting the application that uses it for: Improving performance, fix a bug, consolidate code or for porting. Access Specifiers: In SystemVerilog, unqualified class properties and methods are public, available to anyone who has access to the objects name. A member identified as local is available only to methods inside the class. Further, these local members are not visible within subclasses. Of course, nonlocal methods that access local class properties or methods can be inherited and work properly as methods of the subclass. EXAMPLE: local variblg error class base; local int i; endclass program main; initial begin base b = new(); b.i = 123; end endprogram RESULT: Local member 'i' of class 'base' is not accessible from scope 'main'

The above examples gives compilation error. EXAMPLE: local varible access using method class base; local int i; task set(int j); i = j; $display(i); endtask

endclass program main; initial begin base b = new(); b.set(123); end endprogram RESULT 123

EXAMPLE: local varible access in subclass class base; local int i; endclass class ext extends base; function new(); i = 10; endfunction endclass RESULT Local member 'i' of class 'base' is not accessible from scope 'ext'

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: protected varible class base; protected int i; endclass class ext extends base; function new(); i = 10; endfunction endclass

EXAMPLE: protected varible in 2 level of inheritence class base; local int i; endclass class ext extends base; protected int i; endclass class ext2 extends ext; function new(); i =10; endfunction endclass

In the above example, the varible i is overloaded in subclass with different qualifier. EXAMPLE: Error access to protected varible. class base; protected int i; endclass program main; initial begin base b = new(); b.i = 123; end endprogram RESULT Protected member 'i' of class 'base' is not accessible from scope 'main'

Within a class, a local method or class property of the same class can be referenced, even if it is in a different instance of the same class. EXAMPLE class Packet; local integer i;

function integer compare (Packet other); compare = (this.i == other.i); endfunction endclass A strict interpretation of encapsulation might say that other.i should not be visible inside of this packet because it is a local class property being referenced from outside its instance. Within the same class, however, these references are allowed. In this case, this.i shall be compared to other.i and the result of the logical comparison returned. POLYMORPHISM

Polymorphism allows an entity to take a variety of representations. Polymorphism means the ability to request that the same Operations be performed by a wide range of different types of things. Effectively, this means that you can ask many different objects to perform the same action. Override polymorphism is an override of existing code. Subclasses of existing classes are given a "replacement method" for methods in the superclass. Superclass objects may also use the replacement methods when dealing with objects of the subtype. The replacement method that a subclass provides has exactly the same signature as the original method in the superclass. 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.

EXAMPLE: without virtual class A ; task disp (); $display(" This is class A "); endtask endclass class EA extends A ; task disp (); $display(" This is Extended class A "); endtask endclass program main ; EA my_ea; A my_a; initial begin

my_a = new(); my_a.disp(); my_ea = new(); my_a = my_ea; my_a.disp(); end endprogram RESULTS This is class A This is class A

EXAMPLE: with virtual class A ; virtual task disp (); $display(" This is class A "); endtask endclass class EA extends A ; task disp (); $display(" This is Extended class A "); endtask endclass program main ; EA my_ea; A my_a; initial begin my_a = new(); my_a.disp(); my_ea = new(); my_a = my_ea; my_a.disp(); end endprogram RESULTS This is class A

This is Extended class A

Observe the above two outputs. Methods which are declared as virtual are executing the code in the object which is created.

The methods which are added in the subclasses which are not in the parent class canot be acessed using the parent class handle. This will result in a compilation error. The compiler check whether the method is exesting the parent class definition or not. EXAMPLE: class A ; endclass class EA extends A ; task disp (); $display(" This is Extended class A "); endtask endclass program main ; EA my_ea; A my_a; initial begin my_ea = new(); my_a = my_ea; my_ea.disp(); my_a.disp(); end endprogram RESULT: Member disp not found in class A

To access the varible or method which are only in the subclass and not in the parent class, revert back the object to the subclass handle. EXAMPLE:

class A ; endclass class EA extends A ; task disp (); $display(" This is Extended class A "); endtask endclass program main ; EA my_ea; A my_a; initial begin my_ea = new(); my_a = my_ea; just(my_a); end endprogram task just(A my_a); EA loc; $cast(loc,my_a); loc.disp(); endtask RESULT This is Extended class A

Let us see one more example, A parent class is extended and virtual method is redefined in the subclass as non virtual method. Now if furthur extention is done to the class, then the method is still considered as virtual method and Polymorphism can be achived still. It is advised to declare a method as virtual in all its subclass, if it is declared as virtual in baseclass , to avoid confusion to the end user who is extend the class. EXAMPLE: class A ; virtual task disp (); $display(" This is class A "); endtask endclass

class EA_1 extends A ; task disp (); $display(" This is Extended 1 class A "); endtask endclass class EA_2 extends EA_1 ; task disp (); $display(" This is Extended 2 class A "); endtask endclass program main ; EA_2 my_ea; EA_1 my_a; initial begin my_ea = new(); my_a = my_ea; my_a.disp(); just(my_a); end endprogram task just(A my_a); EA_1 loc; $cast(loc,my_a); loc.disp(); endtask RESULT This is Extended 2 class A This is Extended 2 class A ABSTRACT CLASSES With inheritance we are able to force a subclass to offer the same properties like their superclasses. Consequently, objects of a subclass behave like objects of their superclasses. Sometimes it make sense to only describe the properties of a set of objects without knowing the actual behaviour beforehand

Abstract classes are those which can be used for creation of handles. However their methods and constructors can be used by the child or extended class. The need for abstract classes is that you can generalize the super class from which child classes can share its methods. The subclass of an abstract class which can create an object is called as "concrete class". EXAMPLE: useing abstract class virtual class A ; virtual task disp (); $display(" This is class A "); endtask endclass class EA extends A ; task disp (); $display(" This is Extended class A "); endtask endclass program main ; EA my_ea; A my_a; initial begin my_ea = new(); my_a = my_ea; my_ea.disp(); my_a.disp(); end endprogram RESULT This is Extended class A This is Extended class A

EXAMPLE: creating object of virtual class virtual class A ; virtual task disp (); $display(" This is class A "); endtask endclass program main ;

A my_a; initial begin my_a = new(); my_a.disp(); end endprogram RESULT Abstract class A cannot be instantiated

Virtual keyword is used to express the fact that derived classes must redefine the properties to fulfill the desired functionality. Thus from the abstract class point of view, the properties are only specified but not fully defined. The full definition including the semantics of the properties must be provided by derived classes. Definition (Abstract Class) A class A is called abstract class if it is only used as a superclass for other classes. Class A only specifies properties. It is not used to create objects. Derived classes must define the properties of A.