SYSTEM VERILOG OOPs (Object-Oriented Programming )
SYSTEM VERILOG OOPs (Object-Oriented Programming )
Table of Contents
1. Class
2. Handle
3. Object
4. Property
5. Method
6. Prototype
7. Static, Local, and Global Variables
8. Shallow Copy and Deep Copy
9. Static Methods
10. Inheritance
11. Super Keyword
12. This Keyword
13. Polymorphism
14. Casting
15. Abstract Class
16. Extern Keyword
1. Class
A class is a blueprint or template for creating objects. It defines the properties (data members) and
methods (functions/tasks) that the objects of the class will have.
2. Handle
3. Object
An object is an instance of a class. It is created dynamically using the new() method. Each object has its
own set of properties and can call the methods defined in the class.
4. Property
A property is a data member of a class. It holds the state or attributes of an object. Properties can be
of any data type, such as int, string, or even another class.
OOPs PART-1 VIKRAM RENESAS
5. Method
A method is a function or task defined within a class. It defines the behavior or actions that an object
can perform.
6. Prototype
A prototype is a declaration of a method (function or task) within a class. It specifies the method's
name, return type (if any), and arguments (if any). The actual implementation of the method is defined
later.
In SystemVerilog, static, local, and global variables have specific scoping and lifetime rules.
1. Static Variable
2. Local Variable
3. Global Variable
In SystemVerilog, shallow copy and deep copy are two ways to copy objects. The key difference lies
in how they handle nested objects or dynamic data within the object being copied.
Shallow Copy
A shallow copy creates a new object and copies all the fields of the original object.
If the object contains references to other objects, the references are copied, but the
referenced objects themselves are not duplicated.
Both the original and the copied object will share the same nested objects.
OOPs PART-1 VIKRAM RENESAS
Deep Copy
A deep copy creates a new object and recursively copies all the fields of the original object,
including any nested objects.
If the object contains references to other objects, new instances of those objects are created,
and their contents are copied.
The original and the copied object will have independent copies of nested objects.
Static Methods
In SystemVerilog, a static method is a method that belongs to a class itself, rather than to an instance
of the class. This means that the method can be called without creating an object of the class.
Inheritance
In SystemVerilog, inheritance is a key feature of Object-Oriented Programming (OOP) that allows you
to create a new class (called the derived class or subclass) based on an existing class (called the base
class or superclass). The derived class inherits all the properties and methods of the base class and
can also add new properties and methods or override existing ones.
1. Base Class (Superclass): The original class from which properties and methods are inherited.
2. Derived Class (Subclass): The new class that inherits from the base class.
3. extends Keyword: Used to specify that a class inherits from another class.
4. super Keyword: Used to refer to the base class from within the derived class.
Polymorphism
Polymorphism is one of the core concepts of Object-Oriented Programming (OOP), and it allows for
the ability to use a single interface to represent different types of objects. In SystemVerilog,
polymorphism is primarily used with classes, and it enables you to write more flexible and reusable
code by allowing objects of different classes to be treated through a common interface.
OOPs PART-1 VIKRAM RENESAS
1. Base Class and Derived Classes: A base class defines a common interface (methods and
properties). Derived classes inherit from the base class and can override its methods to
provide specific implementations.
2. Virtual Methods: Methods in the base class must be declared as virtual to allow overriding in
derived classes.
3. Dynamic Method Dispatch: The actual method to be called is determined at runtime based
on the type of the object, not the type of the handle.
Casting
Dynamic Casting
Dynamic casting is used to safely cast a super-class pointer (reference) into a subclass pointer
(reference) in a class hierarchy.
Dynamic casting will be checked during runtime, and an attempt to cast an object to an
incompatible object will result in a runtime error.
Dynamic casting is done using the $cast(destination, source) method.
Abstract Class
SystemVerilog prohibits a class declared as virtual to be directly instantiated and is called an abstract
class.
A virtual method inside an abstract class can be declared with the keyword pure and is called
a pure virtual method.
Such methods only require a prototype to be specified within the abstract class, and the
implementation is left to be defined within the sub-classes.
Extern Keyword
The extern keyword is used to declare methods that are defined outside the class body. This allows
for separation of method declaration and implementation.
1. Class
A class is a blueprint or template for creating objects. It defines the properties (data members) and
methods (functions/tasks) that the objects of the class will have.
2. Handle
3. Object
An object is an instance of a class. It is created dynamically using the new() method. Each object has its
own set of properties and can call the methods defined in the class.
4. Property
A property is a data member of a class. It holds the state or attributes of an object. Properties can be
of any data type, such as int, string, or even another class.
OOPs PART-1 VIKRAM RENESAS
5. Method
A method is a function or task defined within a class. It defines the behavior or actions that an object
can perform.
6. Prototype
A prototype is a declaration of a method (function or task) within a class. It specifies the method's
name, return type (if any), and arguments (if any). The actual implementation of the method is defined
later.
OOPs PART-1 VIKRAM RENESAS
Complete code:
../simv up to date
CPU time: .460 seconds to compile + .382 seconds to elab + .390 seconds to link
Chronologic VCS simulator copyright 1991-2023
Contains Synopsys proprietary information.
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 5 10:23 2025
------------------------------->
Name: Dog, Age: 5
------------------------------->
V C S S i m u l a t i o n R e p o r t
OOPs PART-1 VIKRAM RENESAS
An array of instances of
a class with a specified
Creates One instance of a class. size.
MyClass arr[10] =
Example MyClass obj = new(); new[10];
Memory Allocates memory for a Allocates memory for
Allocation single object. an array of objects.
Allows dynamic size for
the array (can specify
Not applicable (single the number of
Dynamic Size object). elements).
Can pass arguments to
Can pass arguments to the constructor of each
the constructor of the object in the array (or
Arguments class. to the array itself).
MyClass obj = new(5); `MyClass arrct in the
Argument (if the constructor array needs an
Example takes an argument) argument)
time t = 100;
(Represents 100 time realtime rt = 0.01;
units in simulation (Represents 10
Example cycles). milliseconds).
Supports arithmetic
with fractions (e.g., 5.5
Cannot support
Arithmetic fractional values. or 0.1).
Can be converted
to/from real and int but Can be used in
not directly to/from expressions with
Conversion time. floating-point numbers.
The actual deallocation of memory is done using the delete operator. Setting an object to
null just nullifies the reference and marks it as no longer pointing to the object. The object
itself will be deallocated only if there are no other references to it and the delete operator
is used.
OOPs PART-1 VIKRAM RENESAS
#0 in test at testbench.sv:24
#1 in test
V C S S i m u l a t i o n R e p o r t
OOPs PART-1 VIKRAM RENESAS
1. Static Variable:
o A static variable retains its value between function/task calls.
o It is initialized only once and persists throughout the simulation.
o Declared using the static keyword.
2. Local Variable:
o A local variable is declared inside a function or task.
o It is created when the function/task is called and destroyed when the function/task
exits.
o Declared without any special keyword.
3. Global Variable:
o A global variable is declared outside any function, task, or class.
o It is accessible throughout the entire module or program.
o Declared at the module or program level.
EXAMPLE:
Use static variables when you need to retain values between function/task calls.
Use local variables for temporary storage within a function/task.
Use global variables for data that needs to be shared across multiple functions/tasks or
throughout the module.
EXAMPLE 2:
OOPs PART-1 VIKRAM RENESAS
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 5 12:30 2025
Object 1:
Static Var: 1, Non-Static Var: 1
Static Var: 2, Non-Static Var: 2
Object 2:
Static Var: 3, Non-Static Var: 1
Static Var: 4, Non-Static Var: 2
V C S S i m u l a t i o n R e p o r t
PROTECTED KEYWORD
The protected keyword is used to specify that a class member (variable or method) can be
accessed within the class itself and by derived (child) classes. However, it cannot be
accessed from outside the class hierarchy, i.e., it is not accessible by instances of the class or
non-derived classes.
Accessible anywhere within the scope of Accessible from anywhere within the
the module, class, or program in which it module, class, or program where it is
global is declared. declared.
Shallow Copy
A shallow copy creates a new object and copies all the fields of the original object.
If the object contains references to other objects (e.g., handles to class objects), the references
are copied, but the referenced objects themselves are not duplicated.
Both the original and the copied object will share the same nested objects.
Deep Copy
A deep copy creates a new object and recursively copies all the fields of the original object,
including any nested objects.
If the object contains references to other objects, new instances of those objects are created,
and their contents are copied.
The original and the copied object will have independent copies of nested objects.
STATIC
In SystemVerilog, a static method is a method that belongs to a class itself, rather than to an
instance of the class. This means that the method can be called without creating an object of
the class, which is particularly useful when you need to perform a task that does not depend
on any instance-specific data.
To define a static method, you use the static keyword before the function or task
keyword.
RESULT:
-------------------->
Counter incremented: 3
-------------------->
-------------------->
Counter incremented: 4
-------------------->
-------------------->
Current counter value (static): 4
Current counter2 value (non-static): 1
-------------------->
-------------------->
Current counter value (static): 4
Current counter2 value (non-static): 5
-------------------->
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.420 seconds; Data structure size: 0.0Mb
OOPs PART-1 VIKRAM RENESAS
ERROR:
Error-[SV-AMC] Non-static member access
testbench.sv, 13
$unit, "counter2"
Illegal access of non-static member 'counter2' from static method
'MyClass::increment_counter'.
OOPs PART-1 VIKRAM RENESAS
INHERETANCE
In SystemVerilog, inheritance is a key feature of Object-Oriented Programming (OOP) that allows you
to create a new class (called the derived class or subclass) based on an existing class (called the base
class or superclass). The derived class inherits all the properties and methods of the base class and
can also add new properties and methods or override existing ones.
EXAMPLE 1:
RESULT:
Contains Synopsys proprietary information.
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 5 12:51 2025
===========================>>>>>>>>>>>>
Value of addr = 10 data = 20
===========================>>>>>>>>>>>>
V C S S i m u l a t i o n R e p o r t
addr is a 32-bit wide bit (which is similar to a reg in Verilog but more suited for use
in classes). This variable is intended to hold an address.
child_class is another class, which extends the parent_class, meaning it inherits all the
properties and behaviors of parent_class.
The initial block is used for initialization in simulation. It runs once at the start of the
simulation.
SUPER KEYWORD
In SystemVerilog, the super keyword is used in the context of inheritance to refer to the base class
(superclass) from within a derived class (subclass). It is primarily used for two purposes:
===========================>>>>>>>>>>>>
THIS IS PARENT CLASS
Value of addr = 10 data = 20
===========================>>>>>>>>>>>>
V C S S i m u l a t i o n R e p o r t
EXAMPLE3
===========================>>>>>>>>>>>>
THIS IS CHILD CLASS
THIS IS PARENT CLASS
Value of addr = 10 data = 20
===========================>>>>>>>>>>>>
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.380 seconds; Data structure size: 0.0Mb
OOPs PART-1 VIKRAM RENESAS
EXAMPLE 4
RESULT:
Chronologic VCS simulator copyright 1991-2023
Contains Synopsys proprietary information.
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 5 13:19 2025
===========================>>>>>>>>>>>>
Value CHILD variable N = 4
Child get_N(): 4
===========================>>>>>>>>>>>>
Value PARENT variable N (via super) = 3
Child get_N1(): 3
===========================>>>>>>>>>>>>
V C S S i m u l a t i o n R e p o r t
OOPs PART-1 VIKRAM RENESAS
“THIS” KEYWORD
The keyword is used to refer to class properties, parameters and methods of the current instance. It
can only be used within non-static methods, constraints and covergroups. this is basically a pre-
defined object handle that refers to the object that was used to invoke the method in which this is
used.
This is used inside a class method to refer to the object that is currently calling the method.
It can be useful when you need to distinguish between the instance's own variables and
parameters that might be passed to a method with the same name.
It’s typically used in constructor methods, for chaining, or when you need to call another
method or access another member within the same class.
EXAMPLE:
CPU time: .406 seconds to compile + .426 seconds to elab + .485 seconds to link
Chronologic VCS simulator copyright 1991-2023
Contains Synopsys proprietary information.
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 5 13:24 2025
===========================>>>>>>>>>>>>
Value of x: 10
===========================>>>>>>>>>>>>
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.460 seconds; Data structure size: 0.0Mb
OOPs PART-1 VIKRAM RENESAS
POLYMORPHISM IN SYSTEMVERILOG
Polymorphism in SystemVerilog
This is achieved by having multiple methods in the same class with the same name but
different argument types or numbers of arguments. This allows the compiler to choose the
correct method at compile-time based on the method signature.
Example 1:
RESULT:
Chronologic VCS simulator copyright 1991-2023
Contains Synopsys proprietary information.
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 5 13:54 2025
Drawing a generic shape
Drawing a circle with radius 5
Drawing a rectangle with length 10 and width 20
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.400 seconds; Data structure size: 0.0Mb
OOPs PART-1 VIKRAM RENESAS
In this example, the draw() method is overloaded in the derived classes (Circle and Rectangle)
with different parameters. The method that gets called is determined at compile-time based on the
arguments passed.
Runtime polymorphism is achieved through method overriding. In this case, a derived class
provides its specific implementation of a method that was declared in the base class. The
method to be called is determined at runtime based on the actual object type (i.e., whether it's
a Circle, Rectangle, or a Shape).
RESULT:
Contains Synopsys proprietary information.
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 8 04:28 2025
-------------------->
Inside parent class
Inside child class 1
-------------------->
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.340 seconds; Data structure size: 0.0Mb
Sat Feb 8 04:28:58 2025
OOPs PART-1 VIKRAM RENESAS
RESULT :
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 8 04:35 2025
-------------------->
Inside child class 1
-------------------->
Inside child class 1
-------------------->
Inside parent class 2
-------------------->
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.320 seconds; Data structure size: 0.0Mb
OOPs PART-1 VIKRAM RENESAS
Explanation:
The display() function is designed to print "Inside parent class", and display1()
prints "Inside parent class 2".
1. Polymorphism:
o The line p = c1; demonstrates polymorphism. Here, the parent class handle
p is assigned an instance of child_1. Even though p is of type parent, it
points to an object of child_1. This allows us to call the display() method
in a polymorphic way.
o When p.display() is called, the overridden version of display() in
child_1 is executed, not the one in parent. This is run-time polymorphism.
o The p.display1() call will invoke the display1() method from the parent
class, because display1() is not virtual and cannot be overridden in
child_1.
2. Method Overriding:
o The display() method in child_1 overrides the display() method in
parent. This is done by providing a new implementation for display() in the
child_1 class.
o When p.display() is called (even though p is a parent type handle), the
method in child_1 is executed because display() is virtual.
3. Base Class Handle:
o The parent class handle p can point to an object of any class that extends
parent, like child_1. This allows accessing methods from the derived class
through the base class reference.
o c1.display() is a direct call to the display() method of child_1, and it
also calls the overridden display() in the child_1 class.
OOPs PART-1 VIKRAM RENESAS
OVERRIDE
-------------------->
Inside parent class 2
Addr = 0
-------------------->
Inside child class 1
Data = 60
Addr = 50
-------------------->
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.350 seconds; Data structure size: 0.0Mb
Sat Feb 8 05:00:34 2025
OOPs PART-1 VIKRAM RENESAS
EXAMPLE 2
OOPs PART-1 VIKRAM RENESAS
Contains Synopsys proprietary information.
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 8 05:10 2025
-------------------->
Inside child class 1
-------------------->
-------------------->
Inside child class 2
-------------------->
-------------------->
Inside child class 3
-------------------->
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.380 seconds; Data structure size: 0.0Mb
Sat Feb 8 05:10:06 2025
OOPs PART-1 VIKRAM RENESAS
IMPORTANT:
Here child can get all property from parents ex father took 2bh property it will
go to child
Parents cannot get child property ex child took 2bhk that cannot be in
herniated to parents
Another way father has cricket or singing or blue eye pr sugar it will be
inherited to child but child skills will not in to parent if child learn cooking or
any other it will not got to father
CASTING
DYNAMIC CASTING
Dynamic casting is used to, safely cast a super-class pointer (reference) into a subclass pointer
(reference) in a class hierarchy
Dynamic casting will be checked during run time, an attempt to cast an object to an incompatible
object will result in a run-time error
Dynamic casting is done using the $cast(destination, source) method
With $cast compatibility of the assignment will not be checked during compile time, it will be checked
during run-time
It is never legal to directly assign a super-class (parent class) variable to a variable of one of its
subclasses (child class).
child_class = parent_class; //not-allowed
However, it is legal to assign a super-class (parent class) handle to a subclass (child class)
variable if the super-class (parent class) handle refers to an object of the given subclass(child
class).
parent_class = child_class ;
child_class = parent_class; //allowed because parent_class is pointing to
child_class.
ALLOWED
RESULT
Contains Synopsys proprietary information.
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 8 05:18 2025
OOPs PART-1 VIKRAM RENESAS
Addr = 10
Data = 20
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.580 seconds; Data structure size: 0.0Mb
Sat Feb 8 05:18:20 202
NOT ALLOWED
ERROR
Error-[SV-ICA] Illegal class assignment
testbench.sv, 22
"c = p;"
Expression 'p' on rhs is not a class or a compatible class and hence cannot
be assigned to a class handle on lhs.
Source type: class $unit::parent_class
Target type: class $unit::child_class
Please make sure that the lhs and rhs expressions are compatible.
OOPs PART-1 VIKRAM RENESAS
OOPs PART-1 VIKRAM RENESAS
With the use of $cast(), type check during compile time can be skipped.
ABSTRACT CLASS
SystemVerilog prohibits a class declared as virtual to be directly instantiated
and is called an abstract class
EXAMPLE NORMAL CLASS
RESULT
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 8 05:50 2025
-------------------->
Addr = 10
-------------------->
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.370 seconds; Data structure size: 0.0Mb
OOPs PART-1 VIKRAM RENESAS
Let us declare the class base as virtual to make it an abstract class and see what
happens.
A virtual method inside an abstract class can be declared with the keyword pure and is
called a pure virtual method. Such methods only require a prototype to be specified within the
abstract class and the implementation is left to defined within the sub-classes.
CPU time: .376 seconds to compile + .360 seconds to elab + .393 seconds to link
Chronologic VCS simulator copyright 1991-2023
Contains Synopsys proprietary information.
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Feb 8 05:29 2025
Addr = 10
Data = 20
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.380 seconds; Data structure size: 0.0Mb
Sat Feb 8 05:29:25 2025
OOPs PART-1 VIKRAM RENESAS
EXTERN KEYWORD
-------------------->
INSIDE PARENT CLASS: addr = 10
-------------------->
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.420 seconds; Data structure size: 0.0Mb
OOPs PART-1 VIKRAM RENESAS
Error