OOPs concept in SV
System Verilog introduced OOPs concept which was missing in verilog. As we have seen that System Verilog was introduced keeping in mind the needs of complex design verification. In complex test benches we make lots of components and using modules was not an efficient way to create components as the components defined as module would not have features like inheritance, polymorphism reducing the reusability. In this article we will focus more on the generic concepts of OOP which will help understand future articles.
Most of you might be familiar with OOPs already and they can skip this article. So, let’s get started with the introduction of OOPs.
OOP stands for object-oriented programming. This type of programming gets its inspiration from real word objects where every object has its own property, characteristics defined by a blueprint. As this programming model is inspired by reads-life objects writing code becomes more easier as we can relate it to real objects.
Some key features of OOP are:
- Data Abstraction
Some popular OOPs language is C++, C#, Java, etc.
System Verilog is an HDL, i.e., hardware define language and thus all features of OOP are not needed in SV. Some of the complex OOP feature are omitted in the latest version of OOP. Some of the supported features are:
- Single & multi level inheritance
- Function overriding
- Virtual classes and pure virtual functions
- Virtual functions
We will slowly understand all the features in upcoming articles.
OOPs as we have seen is based on real-life objects which are created from a blueprint. This blueprint is Kwan as class in OOP which defines the properties and behaviour of the object. Using this class multiple objects can be created from a single class each having unique name and properties.
Let us understand this with help of some real-life example. A car is an object which is created from the blueprint of a car. Blueprint will define the properties of the car, like it will have 4 wheels, steering wheel, breaks, design, etc. Now from this blueprint multiple cars (objects) can be created with some unique properties like chassis number. Also, different cars can have different colour, different configuration which may/may not be unique.
We will use similar real-life examples to understand distinct features of OOPs.
Class is a blueprint which defines the properties and behaviour of an object. In OOP classes are the entity which encapsulates all the data and methods.
Objects are the unique entity created from class. Objects are dynamic in nature, i.e, it is created dynamically during runtime.
This is the property of OOP by virtue of which a class can inherit properties and behaviour of another class called parent or base class. The child or derived class can add more property or behaviour on the base class.
Continuing the example of car, we can say that car is a child class of vehicle. Where vehicle would define properties like it should have brake, accelerator, tyres, etc. Car as a derived class of vehicle would have all the properties and add few other properties like it should have four tyres, steering wheel, parking brakes, etc. Similarly different car models can be child of car class.
Parent class can be known as base class or super class. Similarly, child class is known as derived class and sub class.
This is a property by which we can bundle all the data and methods into one unit. This also helps in black boxing a unit where users can focus more on using the unit without knowing the underlying process or complexity.
With our car analogy, we can see car as a black box where we as a user of car are interested in driving the car without knowing how the car works. What happens when we apply brake??
Poly mean many and morph means form. Thus, polymorphism is a concept in which same method can act differently in child class or when inputs are different. OOPs provide us two ways to enable polymorphism – function overloading and function overriding.
For example – Let’s suppose there is a method which calculates area. When there is 1 input, it would calculate area of square, when there are 2 inputs it will calculate area for rectangle and when 3 inputs are given it will calculate area for triangle. We see that the name of the method remains the same but the method perform different functionalities when number of inputs are different.
Data Abstraction means hiding unnecessary and representing only what is necessary for the user basically that particular use case.
We will use another example to understand this. Let us suppose there is a library in a school. It will keep the data of student who have borrowed books from the library. Now a student has a lot of properties such as roll number, name, address, grade, subject marks, extra-curricular activities, etc.
But for library not all the data are important. They need data like roll number, name, class, no of books borrowed, duration of borrowed book, etc. This is known as data abstraction where we focus on the data which is necessary for the use case and hide other data.
Data abstraction in OOP is implemented by virtual class, pure virtual functions.
Virtual class is kind of a framework which defines what type of data and methods are needed for some use case and depending on that different child classes can be created.
Please note we cannot create an object from a virtual class directly. We need to create a class from a virtual class and then only we can create object from it.
These were the four main concepts which build up the OOPs language.
We have seen that whenever we need to use class, we first need to create an object. So why creation of object is needed? We will explore the concept of static and dynamic nature of objects in this section.
When we define a class, its just a blueprint which defines the properties and behaviour of the object. But this does not require any memory. When we create an object, memory is allocated for all the variables defined inside the class.
Just like, blueprint of a car will not require space, but when car (object) is created from blueprint it will occupy space and will be a unique entity by itself. Thus, variables in different object will occupy different memory space and the values can be different in different instance of objects.
These are known dynamic objects as the objects are created dynamically during runtime as and when needed. Objects can be deleted once it is no longer needed, freeing up memory.
Static objects are the one which cannot have different instances. There will be only one instance globally. For these types of objects, the object creation is done during compilation only and thus it will live in the memory throughout the runtime. This is why it is of static nature.
The exact mechanism for dynamic and static objects are out of the scope of this article. The basic concept related to static and dynamic objects should be kept in mind as it is very crucial in writing test benches.
SV components like module, interface is all static in nature, but classes are dynamic in nature and thus, we must be careful about how we use these components in our code.
In this article we mainly focused on understanding the theoretical concept of OOPs. In next few articles we will see how to apply these concepts in System Verilog. We will also see why these concepts are really needed to make a scalable and reusable test bench.
OOP is one of the most common types of programming language used in modern days mainly because the concepts can be easily related to real life objects.