gpt4 book ai didi

system-verilog - 了解函数返回值

转载 作者:行者123 更新时间:2023-12-01 07:56:26 46 4
gpt4 key购买 nike

我想了解 SystemVerilog function从语言资源手册(第 10.3.1 节)返回值,但我在掌握以下部分时遇到了困难。谁能帮我解释一下?我尝试查看不同的站点,但信息并不那么深入。

In SystemVerilog, a function return can be a structure or union. In this case, a hierarchical name used inside the function and beginning with the function name is interpreted as a member of the return value. If the function name is used outside the function, the name indicates the scope of the whole function. If the function name is used within a hierarchical name, it also indicates the scope of the whole function.

a = b + myfunc1(c, d); //call myfunc1 (defined above) as an expression
myprint(a); //call myprint (defined below) as a statement
function void myprint (int a);
...
endfunction

最佳答案

您可以使用两种不同的方式从函数返回值。例如如下,

function int myfunc1(int c, d);
myfunc1 = c+d;
endfunction


function int myfunc1(int c, d);
return c+d;
endfunction

所以当函数被声明为结构或联合类型时,以函数名开头的层次名也意味着返回值的变量。
但是旧的 LRM 描述现在并不正确和精确,因为现在分层名称也可能是函数范围,而不是返回值。例如,
typedef struct { int c, d, n; } ST;

function ST myfunc1(int c, d);
static int x = 1;
myfunc1.c = c; // myfunc1 refers to the return structure
myfunc1.d = d; // myfunc1 refers to the return structure
myfunc1.n = c + d + // myfunc1 refers to the return structure
myfunc1.x; // myfunc1 refers to function scope
endfunction

使用包含函数名称的分层名称的另一个有趣示例。
typedef struct { int c, d; } ST ;

module top;
function ST myfunc1(int c,d);
top.myfunc1.c = c;
myfunc1.c = 1;
myfunc1.d = d;
endfunction

ST x;
initial begin
x = myfunc1(3,2);
#1 $display("%d %d %d", x.c, x.d, top.myfunc1.c);
end
endmodule
x = myfunc1(3,2)的函数调用构造 myfunc1 的调用框架并传递值进行评估。 myfunc1的范围和 top.myfunc1是不同的。以 myfunc1 开头的层次名称指函数的当前调用帧,而 top.myfunc1指模块内声明的函数范围 top .所以消息将是 1 2 3 .

关于system-verilog - 了解函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25396647/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com