Thursday, June 10, 2010

virtual things

Virtual class
If a base class is not intended to be instantiated, it can be made abstract by specifying the class to be virtual.
An abstract class cannot be instantiated; it can only be derived.
Abstract classes can also have virtual methods.

Virtual method(function)
 Virtual methods are a basic polymorphic construct. A virtual method overrides a method in all the base classes, whereas a normal method only overrides a method in that class and its descendants.(only a virtual method can be overrided.) One way to view this is that there is only one implementation of a virtual method per class hierarchy, and it is always the one in the latest derived class. When subclasses override virtual methods, they must follow the prototype exactly.

example:
virtual class BasePacket;
virtual function integer send(bit[31:0] data);
endfunction
endclass
class EtherPacket extends BasePacket;
function integer send(bit[31:0] data);
// body of the function
...
endfunction
endclass


Virtual interface
Virtual interfaces provide a mechanism for separating abstract models and test programs from the actual signals that make up the design. A virtual interface allows the same subprogram to operate on different portions of a design and to dynamically control the set of signals associated with the subprogram. Instead of referring to the actual set of signals directly, users are able to manipulate a set of virtual signals. Changes to the underlying design do not require the code using virtual interfaces to be rewritten. By abstracting the connectivity and functionality of a set of blocks, virtual interfaces promote code reuse.
A virtual interface is a variable that represents an interface instance.

Virtual interface variables can be passed as arguments to tasks, functions, or methods. A single virtual interface variable can thus represent different interface instances at different times throughout the simulation. A virtual  interface  must  be  initialized  before  it  can  be  used;  it  has  the  value  null  before  it  is  initialized.

A virtual  interface  must  be  initialized  before  it  can  be  used;  it  has  the  value  null  before  it  is  initialized.

Once  a  virtual  interface  has  been  initialized,  all  the components  of  the  underlying  interface  instance  are directly  available  to  the  virtual  interface  via  the  dot  notation. 
Virtual interfaces can be declared as class properties, which can be initialized procedurally or by an argument to new().

example:
interface SBus; // A Simple bus interface
logic req, grant;
logic [7:0] addr, data;
endinterface

class SBusTransctor; // SBus transactor class
  virtual SBus bus; // virtual interface of type Sbus
  function new( virtual SBus s );
    bus = s;  // initialize the virtual interface
  endfunction
endclass

module devA( Sbus s ) ... endmodule   // devices that use SBus

module top;
  SBus s[1:4] (); // instantiate 4 interfaces
  devA a1( s[1] ); // instantiate 4 devices
  ...
  initial begin
    SbusTransactor t[1:4];          // create 4 bus-transactors and bind
    t[1] = new( s[1] );
    ...
  end
endmodule

In the preceding example, the transaction class SbusTransctor is a simple reusable component. It is written without any global or hierarchical references and is unaware of the particular device with which it will interact. Nevertheless, the class can interact with any number of devices (four in the example) that adhere to the interface’s protocol.

(设想一下,在SBusTransctor中如果没有virtual修饰 SBus s, s就成为一个实在的interface, 所有对s的操作都在局限在SBusTransctor中,"到此为止"。有了virtual修饰,在SBusTransctor梨花的时候,通过把virtual interface和外部实际interface连接,相应的操作就能传递到理想的real DUT上。 所以virtual interface 常用于 ovm driver 设计中。

No comments: