我试图从《语言》资源手册(第10.3.1节)中了解系统Verilog函数的返回值,但是我在理解以下部分时遇到了困难。有人可以帮我解释它吗?没那么深。
在SystemVerilog中,函数返回可以是结构或联合。在这种情况下,在函数内部使用并以函数名称开头的层次结构名称将被解释为返回值的成员。如果在函数外部使用函数名称,则该名称表示整个函数的范围。如果在层次结构名称中使用功能名称,则它还指示整个功能的范围。
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
。