首页 文章

这个要求使用'new'在systemverilog类中起作用吗?

提问于
浏览
-1

现在我正在尝试研究systemverilog的clss .

从许多类的例子中,我发现了2种类型的“新” .

  • 'new'的情况在课堂上存在 .

  • 'new'的情况初始存在 .

这些构造函数的实现有什么区别吗?

还有,函数new()中有什么?我不确定函数new()的用途是什么

更新

例如1是 . 类xxx ...函数new(); ......功能

Endclass

例2是

program

class xxxx


 endclass

 Initial begin
 xxxx  x = new;
   end

endprogram

更新2

谢谢你让我知道 . 我有一个问题 . 有什么区别

Class xxx
...
Function new();
(Variable initialization)
...
Endfunction
Endclass

Class xxx
...
Function new();
(Nothing variable initialization)
Endfunction
Endclass

但在这种情况下,要传递初始语句或任务中的值 . 函数new()endfunction是什么?我不确定是否应该初始化变量?

update3

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

这就是我的代码 . 你可以看到,

两种类型声明功能块 .

1. is with new

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


2. is without new.

  //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

1 回答

  • 0

    调用类的new()方法是构造类对象的唯一方法 . 您可能想要定义一个类并且从不在其上调用new()方法有几个原因,但是当您对SystemVerilog有更好的理解时,您应该稍后提出这个问题 .

    更新

    我想你可能会问在类中声明函数new()的类之间有什么区别

    class xxxx;
      int x;
      function new;
      ...
      endfucntion
    endclass
    

    而没有它的课程

    class xxxx;
      int x;
    endclass
    

    如果您未在类中声明函数new(),则SystemVerilog会为您定义一个隐式函数 . 您可能希望在类中声明一个新函数的原因是,如果要将参数传递给构造函数,或者您需要更复杂的过程代码来初始化 .

相关问题