1

I'm trying to learn about SystemVerilog. While reading about it, I came across the following code, which I cannot fully understand:

Test1.

class A ; 
  task disp();
    $display(" This is class A "); 
  endtask 
endclass 

class EA extends A ; 
  task disp (); 
    $display(" This is Extended class A "); 
  endtask 
endclass 

program main ; 
  EA my_ea; 
  A my_a; 

  initial 
  begin 
    my_a.disp(); 
    my_a = my_ea; 
    my_a.disp(); 
  end 
endprogram 

Test2.

class A ; 
  virtual task disp (); 
    $display(" This is class A "); 
  endtask 
endclass 
 
class EA extends A ; 
  task disp (); 
    $display(" This is Extended class A "); 
  endtask 
endclass 

program main ; 
  EA my_ea; 
  A my_a; 

  initial 
  begin 
    my_a = new(); 
    my_a.disp(); 

    my_ea = new(); 
    my_a = my_ea; 
    my_a.disp(); 
  end 
endprogram 

I have some questions about the test1 code above. There is a call to some 'new' function, but the implementation of that is not provided anywhere. How can this code compile and run then?

Also in the test2, you can see the 'virtual' keyword. I do not understand the reason behind using 'virtual'. Can you please explain why do we have to use 'virtual' in this context?

update

I'd like to implement the example code from Greg. But I've got some problem as the below

                         Chronologic VCS (TM)
         Version J-2014.12-SP1-1 -- Wed Aug  8 08:33:23 2018
               Copyright (c) 1991-2014 by Synopsys Inc.
                         ALL RIGHTS RESERVED

This program is proprietary and confidential information of Synopsys Inc.
and may be used and disclosed only as authorized in a license agreement
controlling such use and disclosure.

Parsing design file 'design.sv'
Parsing design file 'testbench.sv'

Error-[SE] Syntax error
  Following verilog source has syntax error :
  "testbench.sv", 21: token is '('
    function(A a);
             ^

1 error
CPU time: .073 seconds to compile
Exit code expected: 0, received: 1
Done
Community
  • 1
  • 1
bural
  • 61
  • 2
  • 11

1 Answers1

1

The new keyword is a constructor, it creates the object. Since new is not defined it is inferring the default constructor:

function new();
endfunction

Objects must be constructed before you call any of there methods. Test1 should through a null pointer error because you call an object's method that hasn't been constructed.

The virtual keyword and concept is the same in C++, Java, etc. There are plenty of explanations of this already answered on the virtual topic and polymorphism, such as : Why do we need virtual functions in C++?

In a nutshell a parent handle pointing to a child object can execute the object's method if it is virtual. Best way understant this is the create a class and child class that has both a virtual and non-virtual methods. Example:

module main ; 
  class A ; 
    function void disp (); 
      $display(" Non-Virtual from A "); 
    endfunction
    virtual function void vdisp (); 
      $display(" Virtual from A "); 
    endfunction
  endclass 

  class EA extends A ;
    function void disp (); 
      $display(" Non-Virtual from EA "); 
    endfunction
    virtual function void vdisp (); 
      $display(" Virtual from EA "); 
    endfunction
  endclass 

  function void disp(A a);
    a.disp();
    a.vdisp();
  endfunction

  EA my_ea; 
  A my_a; 

  initial 
  begin 
    my_a = new(); 
    my_ea = new(); 
    disp(my_a); 
    disp(my_ea); 
    my_a = my_ea; 
    disp(my_a); 
  end 
endmodule
bural
  • 61
  • 2
  • 11
Greg
  • 18,111
  • 5
  • 46
  • 68
  • Thanks Greg, but test1 compiled and elab and simulation well. I don't knownthe reason.. test1's output display like this this is class A ...this is class A. Is this something wrong that I can't see? – bural Feb 12 '16 at 02:41
  • There is nothing wrong with test1 and as the `virtual` keyword is not used in disp() method of class A, it doesn't allow EA to override it and hence second time also it prints "this is class A". – H.Modh Feb 12 '16 at 03:23
  • You can refer 5th chapter of "systemverilog for verification chris spear 3rd edition" for quick reference and then look into LRM for more detailed explanation. – H.Modh Feb 12 '16 at 03:26
  • @H.Modh Thanks, When I use virtual keyword to task of the first class A, then i got an error. E.TRNULLID: NULL pointer dereference. – bural Feb 12 '16 at 04:22
  • [IEEE Std 1800-2012](http://standards.ieee.org/getieee/1800/download/1800-2012.pdf) section 8.4 "Uninitialized object handles are set by default to the special value null. An uninitialized object can be detected by comparing its handle with null. ... Accessing non-static members (see 8.9) or virtual methods (see 8.20) via a null object handle is illegal. The result of an illegal access via a null object is indeterminate, and implementations may issue an error." – Greg Feb 12 '16 at 04:44
  • 1
    You haven't created an object for class EA. – H.Modh Feb 12 '16 at 05:11
  • @Greg I've got some error when I run your code in https://www.edaplayground.com Error-[SE] Syntax error Following verilog source has syntax error : "testbench.sv", 21: token is '(' function(A a); ^ 1 error CPU time: .073 seconds to compile Exit code expected: 0, received: 1 Done – bural Aug 08 '18 at 08:38
  • @bural I fixed the 2+ year old types – Greg Aug 08 '18 at 14:33
  • @Greg Would you please help me for understanding about the purpose "virtual" keyword? I want to know why when do we need the "virtual" keyword. – bural Aug 09 '18 at 00:42