620 likes | 1.03k Views
IEP on Design Verification and Hardware Security NIT, Rourkela. System Verilog Object Oriented Programming and Classes. OUTLINE. Classes “this” keyword Constructors Class Assignment Shallow Copy and Deep Copy Inheritance Polymorphism Overriding Class members “super” keyword Casting
E N D
IEP on Design Verification and Hardware Security NIT, Rourkela System Verilog Object Oriented Programming and Classes
OUTLINE • Classes • “this” keyword • Constructors • Class Assignment • Shallow Copy and Deep Copy • Inheritance • Polymorphism • Overriding Class members • “super” keyword • Casting • Data hiding and Encapsulation • Virtual methods and Extern methods
Introduction to Classes • A class is a user-defined data type that includes data (class properties), functions and tasks that operate on data. • An object is used by first declaring a variable of that class type (that holds an object handle) and then creating an object of that class (using the new function) and assigning it to the variable. source: - www.verificationguide.com
Class Constructors • The new function is called as class constructor. • On calling the new method it allocates the memory and returns the address to the class handle. • Constructors should be function and not a task. • Constructors does not return values. • Constructors like any other function take input parameters. • There can be only one constructor per class. source: - www.verificationguide.com
“this” keyword • “this” keyword is used to refer to class properties. • “this” keyword is used to unambiguously refer to class properties or methods of the current instance. source: - www.verificationguide.com
Static Class members • Static class members : - Class members can be created with the keyword static. class members with the keyword static are called as static class members. The class can have static properties and static methods (functions and tasks). • Static Properties: - The class can have multiple instances, each instance of the class will be having its own copy of variables. Sometimes only one version of a variable is required to be shared by all instances. These class properties are created using the keyword static. • A static method can access only static properties of the class and access to the non-static properties is illegal and lead to a compilation error. source: - www.verificationguide.com
Class Assignment • Object will be created only after doing new to an class handle, • packet pkt_1;pkt_1 = new();packet pkt_2;pkt_2 = pkt_1; • In the above piece of code, an object is created only for pkt_1, pkt_2 is just a handle to the packet. • pkt_1 is assigned to the pkt_2. So only one object has been created, pkt_1 and pkt_2 are two handles both are pointing to the same object • As both the handles are pointing to the same object any changes made with respect to pkt_1 will reflect on pkt_2.
System Verilog Inheritance • Inheritance is an OOP concept that allows the user to create classes that are built upon existing classes. • Inheritance is about inheriting base class members to the extended class. • The new class will be with new properties and methods along with having access to all the properties and methods of the original class. source: - www.verificationguide.com
Overriding Class members • Base class or parent class properties and methods can be overridden in the child class . • Defining the class properties and methods with the same name as parent class in the child class will override the class members. source: - www.verificationguide.com
“Super” Keyword • When class members are overridden in the derived class, it is necessary to use the super keyword to access members of a parent class. • With super keyword, it is allowed to access the class members of parent class which is only one level up. source: - www.verificationguide.com
Data hiding and encapsulation • Data hiding and Encapsulation: The technique of hiding the data within the class and making it available only through the methods (i.e., by the methods of the class) is known as encapsulation. • Access Control:Access control rules that restrict the members of a class from being used outside the class. • This is achieved by prefixing the class members with the keywords: local and keyword. source: - www.verificationguide.com
Extern Functions • The definition of the method written outside the body of the class then the method is called an external method. • External functions need to declare the method (Function/Task) with an extern keyword. • The extern qualifier indicates that the body of the method (its implementation) is to be found outside the class declaration. source: - www.verificationguide.com
SV Interface source: - www.verificationguide.com
SV Interfaces source: - www.verificationguide.com
Ones Counter source: - www.testbench.in
Ones Counter_TestBench source: - www.testbench.in
Ones Counter_TestBench source: - www.testbench.in
Ones Counter_TestBench source: - www.testbench.in