2017-06-14 267 views
2

我遇到问题,找出从函数返回动态结构数组的语法。我有以下小例子:如何从SystemVerilog中的某个函数返回一个动态结构数组

`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 []); 

然后它工作正常。是否有可能从函数返回一个动态结构数组?如果是的话,那么正确的语法是什么?

回答

2

当需要函数返回一个解压缩类型时,您需要一个typedef

typedef PersonType PersonType_da_t[]; 

function automatic PersonType_da_t getPeopleInfo(); 
+0

谢谢!这工作。我有一个关于“自动”的后续问题。我的理解是,自动强制变量在堆栈中(而不是静态的)。但是当返回一个像上面例子那样的结构时,我很难概念化它的含义。或者不添加“自动”关键词的后果是什么。 – user3716072

+1

对于您的例子,这并不重要,但这是一个很好的习惯。将“自动”放在函数头中会使函数自动调用_all_变量:返回变量,参数和局部变量。请参阅https://verificationacademy.com/forums/systemverilog/what-exact-difference-between-static-tasks/functions-and-automatic-tasks/functions-please-explain-clear-example#reply-44935 –

相关问题