program tb_top;
// here is a shape class, defining that there will be
// some shapes to draw, and they all must have
// a shape method to be used by the "BFM"
virtual
class Shapes;
// every shape must have a total size, and a start coordinate
int total_size;
int start_x, start_y;
bit[7:0] color;
// every shape must implement draw and color actions
virtual task draw();
endtask
// by defining code here, it becomes optional for
// derived methods to define it there.
// because it is virtual, the derived object method
// will override this call, unless super is used by
// the derived object to call this one
virtual task setcolor (bit [7:0] color);
this.color = color;
endtask
endclass
class Circle extends Shapes;
bit [30:0] circle_d_data;
function new(int size = 4);
total_size = size * 3;
endfunction
// here is how I would draw a circle
// I keep it virtual so that one someone
// can create a 3D circle and build upon
// this example
virtual task draw();
// do the actual draw here
$display("drawing a circle...\n");
endtask
endclass
class Square extends Shapes;
int drawn = 0;
function new(int sizex = 1, int sizey = 1);
total_size = sizex * sizey;
endfunction
virtual task draw();
// do the actual draw here
$display("drawing a square...\n");
drawn = 1;
endtask
virtual task setcolor(bit [7:0] color);
// here I need to do the base class stuff, but then
// the work of updating the color of the square
super.setcolor(color);
if (drawn == 1) begin
$display("updating drawing...\n");
end
endtask
0��)V 0��)V ���)V ��)V ���)V P��)V � P��)V n-bottom: 0">
endclass
Shapes s[2];
Circle c1;
Square s1;
initial
begin
// initialize the objects, let base class handles
// point to them also
c1 = new(); // create a circle object
s[0] = c1; // s[0] also points to that circle
s1 = new(); // create a square object
s[1] = s1; // watch out for sharp edges
//s0.draw(); is the same as calling c1.draw(), but
// the neat thing here is that a BFM only needs the
// base class handle, and doesn't need to be modified
// if the functionality or data features change!!
s[0].draw();
s[0].setcolor(22);
// is the same as
c1.draw();
c1.setcolor(22);
// but the base methods didn't need to be changed
end
endprogram |