是否必须使用 'new' 在 systemverilog 的 class 中运行?
Is this mandantory to use 'new' to function in the class of systemverilog?
现在正在研究systemverilog的clss
从许多 class 的例子中,我发现 'new' 有两种类型。
- 'new' 的情况在 class 中存在。
- 初始存在'new'的情况
这些构造函数的实现有什么区别吗?
还有一个,new()函数里有什么?
我不确定函数 new()
的目的是什么
更新
例如1是。
Classxxx
...
函数新();
...
结束函数
结束class
示例 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 中有什么?我不确定是否必须初始化变量?
更新3
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
调用 class 的 new() 方法是构造 class 对象的唯一方法。您可能想要定义一个 class 并且从不对其调用 new() 方法的原因有几个,但是您应该在对 SystemVerilog 有更好的理解之后再问这个问题。
更新
我想您可能会问 class 与 class
中声明的函数 new() 之间的区别是什么
class xxxx;
int x;
function new;
...
endfucntion
endclass
与没有它的class相比
class xxxx;
int x;
endclass
如果您没有在 class 中声明函数 new(),SystemVerilog 会为您定义一个隐式函数。您可能想要在 class 中声明一个新函数的原因是,如果您想将参数传递给构造函数,或者您有一些需要更复杂的过程代码来初始化的东西。
现在正在研究systemverilog的clss
从许多 class 的例子中,我发现 'new' 有两种类型。
- 'new' 的情况在 class 中存在。
- 初始存在'new'的情况
这些构造函数的实现有什么区别吗?
还有一个,new()函数里有什么? 我不确定函数 new()
的目的是什么更新
例如1是。 Classxxx ... 函数新(); ... 结束函数
结束class
示例 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 中有什么?我不确定是否必须初始化变量?
更新3
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
调用 class 的 new() 方法是构造 class 对象的唯一方法。您可能想要定义一个 class 并且从不对其调用 new() 方法的原因有几个,但是您应该在对 SystemVerilog 有更好的理解之后再问这个问题。
更新
我想您可能会问 class 与 class
中声明的函数 new() 之间的区别是什么class xxxx;
int x;
function new;
...
endfucntion
endclass
与没有它的class相比
class xxxx;
int x;
endclass
如果您没有在 class 中声明函数 new(),SystemVerilog 会为您定义一个隐式函数。您可能想要在 class 中声明一个新函数的原因是,如果您想将参数传递给构造函数,或者您有一些需要更复杂的过程代码来初始化的东西。