Is this mandantory to use 'new' to function in the class of systemverilog?

自闭症网瘾萝莉.ら 提交于 2019-12-25 17:14:10

问题


Now I'm trying to study about clss of systemverilog.

From many class of example, I found the 'new' in 2 types.

  1. The case of the 'new' is existed in class.
  2. The case of the 'new' is existed in initial.

Is there any difference between those implementation of constructor?

One more, what is in the function new()? I'm not sure what purpose is in the function new()

update

For example 1 is. Class xxx ... Function new(); ... Endfunction

Endclass

Example2 is

program

class xxxx


 endclass

 Initial begin
 xxxx  x = new;
   end

endprogram

update 2

Thanks for let me know. I've got a question. What Is the difference between

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

And

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

But in this case to pass the value in the intial statement or tasks. What is in the function new() endfunction? I'm not sure should I have to initialize the variables?

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

This is what I've a code. you can see that,

two types declare of function block.

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:


Calling the new() method of a class is the only way to construct a class object. There are a few reasons you might want to define a class and never call the new() method on it, but you should ask that question later when you have a better understanding of SystemVerilog.

Update

I think you may be asking what is the difference between a class with declared function new() inside the class

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

versus a class without it

class xxxx;
  int x;
endclass

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.



来源:https://stackoverflow.com/questions/35215240/is-this-mandantory-to-use-new-to-function-in-the-class-of-systemverilog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!