首页 > 代码库 > systemverilog之OOP
systemverilog之OOP
what is oop
terminology
an example class
default methods for classes
static attibute
assigment and copying
inheritance
polymorphism
why oop?
1. helps in creating and maintaining large testbench:
You can creat complex data types and tie them together with
the routines that work with them
2.increases productivity:
You can creat testbenches and system-level models at a more
abstract level by calling a routine to perform an action rather than toggling bits
You can work with transactions rather than signals transitions
3.Allows the Testbench to be reused
OOP decouples the testbench from design details make it more rebust and easier
to maintain and reuse
How should the data and code be brought tegother?
transactions
OOP Basics:Terminology
Blueprint for a house ===> Class
A compelet house ====> Object
House Address ====> Handle
Turn on/off swithes =====> Methods
Light swithes ======> Properties
verilog VS OOP
Block definition module class
Block instance instance object
Block name instance name handle
Data Type reg & wire properties & variables
Executable Code behavioral blocks Methods:functions
(always / initial) and tasks
tasks / functions
communication ports or cross-module mailboxes/semaphores/calls
between blocks task calls ……..
OOP:Your First Class
略。。。
BusTran b; <—Declare a handle that points to an object of the type BusTran .When a handle is declared it is initialized to null
b= new();<- call the new function to construct the BusTran object.
when you call new you are allocating a new block of
memory to store variable for that object
1.new allocates space for ButTran
2.Initialize the variable to their default value( 0 for 2state
and x for 4-state variables)
3.Return the address where the object is stored
how gettint a handle on objects?
how deallocating a handle on objects?
BusTran b1,b2; <-----declare a handle
b1 = new(); <-----allocate bustran object
b2 = b1; <------
b1 = new(); <------allocate another buttran object
b2 = null; <-------deallocate the second bustran object
How do I create a variable shared by all objects of a class,but not make it global?
static variable
1.The static variable is associted with the class definition , not the instantiated object
2.It is often used to store meta-data,such as number of instances construncted
3.It is shared by all objects of that class
Using the id field can help keep track of transactions as flow through test
class routines : tasks and functions
Using one class inside another class
mainly use a handle to an object
using hierarchical syntax
done for reuse and controll complexity
copy:object copy
1.shallow copy ===> b2 = new()b1 copy的只是句柄,但b1 & b2 指向同一个空间
2.deep copy ====b2 = copy(b1) copy之后,b1 & b2有独立的空间
Inheritance
1.易于debug
2.在以前的基础进行代码扩展,而不是修改以前的代码.
super.parent_method/property
Inheritance chain
1.base class can hold extended class handle
2.extended class cannot hold base handle,this fails because the base object is missing properties(variable)
that only exist in the extended class,such as bad_src in the following example
$cast(badtr,tr) ===>必须badtr tr的类型一致
Abstract Classes and Virtual Methods
1.virtual class
2.polymorphism
the oop term for multiple routines sharing a common name is “polymorphism”
polymorphism is realized by virtual method
always declare routines inside a class as virtual so that they can be redefine
in an extended class.This applies to all tasks and functions ,except the new
function.which is called when the object is constructed, and there is no way to extend it
virtual fucntion在run时实现,compile并不关注 (有绑定的意思)
new在compile时实现
parameterized classes
It is often useful to define a generic class whose objects can be
instantiated to have different array sizes or data types
1.The normal verilog parameter machanism is used to parameterize the calss
class vector #(int size = 1)
bit [size-1:0] a;
endclass
2.Instance of this class can then be instantiated like modules or interfaces
vecor #(.size(2)) vtwo;
Initializing class properties (new)
How does systemverilog know which new function to call?
it does that by looking at the type of handle(有的概念根据句柄所指向的对象类型比如通过句柄调用method)
systemverilog之OOP