Systemverilog中的“新”功能的目的是什么?

时间:2018-08-06 07:49:21

标签: system-verilog

我试图了解systemverilog,所以我指的是this网站。 但是我在下面的代码中混淆了“ new”的用法。

class packet;
  //class properties
  bit [31:0] addr;
  bit [31:0] data;
  bit        write;
  string     pkt_type;

  //constructor
  function new(bit [31:0] addr,data,bit write,string pkt_type);
    addr  = addr;
    data  = data;
    write = write;
    pkt_type = pkt_type;
  endfunction

  //method to display class prperties
  function void display();
    $display("---------------------------------------------------------");
    $display("\t addr  = %0h",addr);
    $display("\t data  = %0h",data);
    $display("\t write = %0h",write);
    $display("\t pkt_type  = %0s",pkt_type);
    $display("---------------------------------------------------------");
  endfunction
endclass

module sv_constructor;
  packet pkt;
  initial begin
    pkt = new(32'h10,32'hFF,1,"GOOD_PKT");
    pkt.display();
  end
endmodule

特别是,您可以看到

function new(bit [31:0] addr,data,bit write,string pkt_type);
        addr  = addr;
        data  = data;
        write = write;
        pkt_type = pkt_type;
      endfunction

此代码使用“ new”。 我很困惑功能〜终端功能中的“新”是什么意思? 功能块中的“新”用途是什么?

您可以看到上面的代码中的另一个块,

function void display();
    $display("---------------------------------------------------------");
    $display("\t addr  = %0h",addr);
    $display("\t data  = %0h",data);
    $display("\t write = %0h",write);
    $display("\t pkt_type  = %0s",pkt_type);
    $display("---------------------------------------------------------");
  endfunction

功能块中没有使用“新”。

您能帮我让我知道是什么意思吗?

更新

我看到了另一个问题的副本。 但是我有一个关于戴夫答案的查询。

If you do not declare a function new() inside your class, SystemVerilog defines an implicit one for you. The reason you might want to declare a function new inside your class is if you want to pass in arguments to the constructor, or you have something that requires more complex procedural code to initialize.

特别是

  

如果您未在类内声明函数new(),则SystemVerilog会为您定义一个隐式函数。

您能通过任何简单的示例帮助我理解“为您隐性的”是什么吗?

我还有一个如下的实验,

class packet;

  //class properties
  bit [31:0] addr;
  bit [31:0] data;
  bit        write;
  string     pkt_type;


  //constructor
  function temp(bit [31:0] addr1,data1,bit write1,string pkt_type1);
    addr  = addr1;
    data  = data1;
    write = write1;
    pkt_type = pkt_type1;
  endfunction


  //method to display class prperties
  function void display();
    $display("---------------------------------------------------------");
    $display("\t addr  = %0h",addr);
    $display("\t data  = %0h",data);
    $display("\t write = %0h",write);
    $display("\t pkt_type  = %0s",pkt_type);
    $display("---------------------------------------------------------");
  endfunction

endclass

module sv_constructor;


  initial begin
    packet pkt;
    packet temp;

    pkt = new();//32'h10,32'hFF,1,"GOOD_PKT");
    temp = new();


    pkt.addr = 1;
    pkt.display();



  end

endmodule

如您所见,代码在temp函数中没有“ new”。所以我很困惑我在哪种情况下需要功能“新”?

更新

如果我要进行如下所示的自定义功能,

class MyClass;
   int number;
   function custom_function1();
       number = 0;
   endfunction
   function custom_function2 (int num);
       number = num;
   endfunction

endclass

我不能只有这样吗?

更新2

class MyClass;
   int number;
   function new();
       number = 0;
   endfunction
     function new (int num);
       number = num;
    endfunction

     function new (int num);
        number = num;
     endfunction

     function new (int temp);
        number = temp;
     endfunction

    endfunction

  endclass

如果我们在MyClass之上,那么我们如何知道调用哪个函数?

2 个答案:

答案 0 :(得分:1)

new()是SystemVerilog中构造对象的特殊功能,也称为构造函数。它没有任何返回类型。

答案 1 :(得分:1)

系统Verilog测试平台实现了面向对象的编程模型。 class是对象的定义。

class MyClass;
   members....
   function new();
      // do something
   endfunction
endclass

该类中的new函数是一个构造函数,该构造函数在对象实例化时被调用。

可以通过以下方式实例化一个类:

MyClass a_class = new();

在这种情况下,对new()函数的调用将创建MyClass类型的对象,并调用其成员函数new并使a_class成为对新创建对象的引用。

系统Verilog不支持函数重载,因此只能使用单个构造函数。但是,可以使用默认参数值来增加灵活性

class MyClass;
   int number;
   function new (int num = 0);
       number = num;
   endfunction

endclass

在上述情况下,您可以使用构造函数创建您感兴趣的对象的版本:

 MyClass a_class = new(); // will assign '0' to the 'number'
 MyClass b_class = new(3); // will call the second constructor and assign '3'