如何从 SystemVerilog 中的函数 return 动态结构数组
How to return a dynamic struct array from a function in SystemVerilog
我在从函数中找出 returning 动态结构数组的语法时遇到问题。我有以下小例子:
`timescale 1ns/10ps
typedef struct{
string Name;
int Age;
} PersonType;
function PersonType A [] getPeopleInfo();
automatic string Name [50];//Max of 50 people
automatic int Age [50];
PersonType A [];
/*Turns out we only have 3 people->this may change at runtime*/
Name[0]="Jon";Age[0]=25;
Name[1]="Ana";Age[1]=32;
Name[2]="Ali";Age[2]=19;
A=new[3];/*This size may change at runtime*/
for(int idx=0;idx<3;idx++)
begin
A[idx].Name=Name[idx];
A[idx].Age=Age[idx];
end
return A;
endfunction // getPeopleInfo
module Test();
PersonType A [];
initial begin
A=getPeopleInfo();
for(int idx=0;idx<A.size();idx++)
begin
$display(A[idx].Name);
$display(A[idx].Age);
end
end
endmodule // Test
当我修改函数使其将动态结构数组作为参数传递时,即:
void getPeopleInfo(output PersonType A []);
然后它工作正常。是否可以从函数中 return 动态结构数组?如果可以,正确的语法是什么?
当您想要 return 解压类型的函数时,您需要 typedef
。
typedef PersonType PersonType_da_t[];
function automatic PersonType_da_t getPeopleInfo();
我在从函数中找出 returning 动态结构数组的语法时遇到问题。我有以下小例子:
`timescale 1ns/10ps
typedef struct{
string Name;
int Age;
} PersonType;
function PersonType A [] getPeopleInfo();
automatic string Name [50];//Max of 50 people
automatic int Age [50];
PersonType A [];
/*Turns out we only have 3 people->this may change at runtime*/
Name[0]="Jon";Age[0]=25;
Name[1]="Ana";Age[1]=32;
Name[2]="Ali";Age[2]=19;
A=new[3];/*This size may change at runtime*/
for(int idx=0;idx<3;idx++)
begin
A[idx].Name=Name[idx];
A[idx].Age=Age[idx];
end
return A;
endfunction // getPeopleInfo
module Test();
PersonType A [];
initial begin
A=getPeopleInfo();
for(int idx=0;idx<A.size();idx++)
begin
$display(A[idx].Name);
$display(A[idx].Age);
end
end
endmodule // Test
当我修改函数使其将动态结构数组作为参数传递时,即:
void getPeopleInfo(output PersonType A []);
然后它工作正常。是否可以从函数中 return 动态结构数组?如果可以,正确的语法是什么?
当您想要 return 解压类型的函数时,您需要 typedef
。
typedef PersonType PersonType_da_t[];
function automatic PersonType_da_t getPeopleInfo();