2017-06-14 135 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を参照してください。 –

関連する問題