bural
bural

Reputation: 61

What is the purpose the 'new' and 'virtual' in systemverilog?

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

Upvotes: 1

Views: 7746

Answers (1)

Greg
Greg

Reputation: 19096

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

Upvotes: 1

Related Questions