Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

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

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("	 addr  = %0h",addr);
    $display("	 data  = %0h",data);
    $display("	 write = %0h",write);
    $display("	 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("	 addr  = %0h",addr);
    $display("	 data  = %0h",data);
    $display("	 write = %0h",write);
    $display("	 pkt_type  = %0s",pkt_type);
    $display("---------------------------------------------------------");
  endfunction
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...