1. Three basic characteristics of object-oriented
Object: everything is an object
Class: a class is an abstract collection of objects with the same properties and behavior
Instance: an object is an instance of a class
Encapsulation can hide the implementation details and make the code modular; Inheritance can extend existing code modules, both for code reuse, and polymorphism for another purpose - Interface reuse.
1.1 difference between Class 1 and structure
A structure can define a type, and a class is also a type. The similarities and differences between that class and a structure are as follows:
- Both can define data members themselves
- Class needs to be built after variable declaration to create object entity; struct has opened up memory when variables are declared
- Classes can declare methods and tasks, but structs cannot
- Fundamentally, struct is a data structure, while class contains data members and operation methods for members
Encapsulation for information hiding, using abstract data types to encapsulate data and data-based operations, hide some internal details as much as possible, and only retain some external interfaces to make them contact with the outside. Data encapsulation, method encapsulation.
When new() is constructed, it does not need to return a value, and the function will implicitly return the instantiated object pointer.
//Declaration class class Transaction; bit[31:0] addr,crc; function new(); ... endfunction endclass:Transaction //Declare a handle Transaction tr,tr1,tr2;//The pointer is null and does not point to any object Transaction tr = new();//Declare handle + create object and assign an address to the object //create object tr1 = new(); //Assign handle to tr1 tr2 = tr1;//Assign the value of tr1 to tr2, that is, tr1 and tr2 point to the same object tr2 = new();//Assign handle to tr2
Note: the variables defined in the class cannot be wire and reg, nor initial and always, that is, parts of the hardware world cannot be defined in the software world.
When new() is called, the system steps are as follows:
1. System development space;
2. Allocate the opened space to the variable and enter the new() function to initialize the variable
3. When exiting new(), return the handle of the current object
Note: when the new() function creates an object, it assigns an address to the object and initializes the variables in the class (the binary variable is 0 and the Quaternary variable is x).
System Verilog automatically reclaims space. When an object is no longer needed anywhere in the whole program, it will be "destroyed", that is, reclaim its space.
Variables declared in a class are dynamic (as opposed to module), but static variables can be declared with static.
To declare a pointer to an interface in a class, you must add virtual
class TB; static int count = 0;//Declare a static variable local virtual interface_test it;//Declare a pointer endclass
Subclasses inherit all member methods and properties of the parent class, and can have their own characteristics. Implement inheritance through the keyword extends to solve the reusability of code.
- Subclasses have non private properties and methods of the parent class.
- Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.
- Subclasses can implement the methods of their parent classes in their own way.
3.1 member access rights
- By default, both subclasses and external can be accessed
- protected only this class and subclasses can be accessed, but not externally
- local is accessible only by this class, but not by subclasses or outside
3.2 difference between this and super
- this will search in the current scope first. If it is not in the current scope, it will search in the previous scope until the variable is found
- super will first find the parent class of this class
class Father string name = "father"; function new(string name); this.name = name; endfunction endclass class Son extends Father; int son_age = 5; function new(string name); super.new(name); name = "son"; son_age = 15; endfunction endclass
3.3 method of class
Class method -- that is, the internal task or function defined by the scope of the class
class Transaction; bit [31:0] addr,crc,data; function void display(); $diaplay("@%0t:TR addr = %h,crc = %h",$time,addr,crc); $write("\tdata[0-7]="); foreach(data[i]) $write(data[i]); $display(); endfunction endclass class PCI_tran; bit [31:0] addr,data; function void display(); $display("@a%0t:PCI:addr = %h",$time,addr,data); endfunction endclass Transaction t;//Declaration handle PCI_Tran pc; initial begin t=new();//Create a Transaction object t.display();//Call the method of the transfer pc=new();//Create a PCI transaction pc.display();//Method of calling PCI transaction end
"One interface, multiple methods". The same operation acts on different objects, can have different interpretations and produce different execution results.
Three conditions for polymorphism:
- Existence of inheritance
- Subclasses override methods of the parent class
- The parent class reference variable points to the child class object
overload and override are two main ways to realize polymorphism.