【问题标题】:What is the purpose of "new" on the function in Systemverilog?Systemverilog中的功能“新”的目的是什么?
【发布时间】:2018-08-06 07:49:20
【问题描述】:

我正在尝试理解 systemverilog,所以我指的是this 站点。 但我对以下代码中“新”的用法感到困惑。

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

此代码使用“新”。 我很困惑,“新”在功能〜结束功能中是什么意思? 功能块中使用的“新”有何用途?

如你所见,上面代码中的另一个块,

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

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

更新

如果我想做如下自定义函数,

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,那我们怎么知道调用的是哪个函数呢?

【问题讨论】:

标签: system-verilog


【解决方案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' 

【讨论】:

  • 谢谢顺便说一句,我不能有自定义功能吗?不仅仅是多个新的。
  • 是的,您可以在一个类中拥有任意数量的自己的函数、任务和变量。
  • 我已经更新了问题,你能告诉我更新问题吗?
  • 错误-[IPD] 先前声明的标识符 先前声明为函数的标识符“新”。 "testbench.sv", 10 源信息:function new (int num);
  • 对不起,被 c++ 语义冲昏了头脑。系统 verilog 不支持函数重载,因此在一个类范围内只能存在一个函数名。因此,您仍然可以拥有任意数量的自定义函数,但它们必须以不同的方式命名。每个类只允许有一个构造函数。
【解决方案2】:

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

【讨论】:

  • 我想做一个自定义函数,但大多数函数名都是用 new() 声明的。我该怎么办?
  • “大多数函数名”是什么意思?只有一个构造函数称为 new() 并在创建对象时被调用。您可以创建自己的用户定义函数名称并在 new() 中调用它们。这就是你想要的吗?
  • 是的,你能告诉我更多关于自定义函数的例子吗?
  • 这取决于你想做什么。
  • 我是否应该在制作函数时必须制作一个对象?
猜你喜欢
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 2010-10-22
  • 2021-11-24
相关资源
最近更新 更多