SourceSV Verification Directory

Class Methods

A class method is a task or a function defined inside the scope of the class. A method in a class uses automatic storage by default.

Example: Method in a Class

class Ex_class;
   // class properties or variables
  int a, b, s;

   // task method
  task sum (int a,b);
    s = a + b;
    $display("a = %0d , b = %0d, sum s = %0d", a, b ,s);
  endtask

endclass

Defining Methods Outside of Class

To keep the code short, and easy to read and understand, it is advisable to keep the class to one “page” or to the screen of the editor. In case, if a method takes more than a page, you can break the method into the prototype(method name and arguments) inside the class, and the body(the procedural code) outside the class. To create an out-of-block declaration,

  1. Copy the first line of the method, with the name and arguments, and add the extern keyword at the beginning.
  2. Place the entire method outside of the class, and add the class name and two colons (: : the scope operator) before the method name.

Example: Method Outside of Class

class Ex_class;
   // class properties or variables
   int a, b, s;
   extern task sum (int a,b);
endclass

// task method outside the class
task Ex_class::sum (int a,b);
        s = a + b;
        $display("a = %0d , b = %0d, sum s = %0d", a, b ,s);
endtask



module tb_method_outside_class;

  Ex_class cl_obj;      // declare a class handle of type cl_obj

   initial begin
     cl_obj = new( );   // construct an object
     cl_obj.sum(30,10); // access a method
     repeat(10)
        cl_obj.sum($urandom_range(1,50),$urandom_range(1,50));
   end

endmodule

Execute the code in EDA Playground

Points to Remember

  1. The prototype (header of the routine) should match with the out-of-body declaration with the prefix keyword extern, except for the class name and scope operator(: :).
  2. The class name along with the scope operator must be included in the out-of-body declaration.