了解系统verilog函数返回值

chi*_*nna 3 system-verilog

我试图从语言资源手册(第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
Run Code Online (Sandbox Code Playgroud)

谢谢

jcl*_*lin 6

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

function int myfunc1(int c, d);
  myfunc1 = c+d;
endfunction
Run Code Online (Sandbox Code Playgroud)

function int myfunc1(int c, d);
  return c+d;
endfunction
Run Code Online (Sandbox Code Playgroud)

因此,当函数声明为结构或联合类型时,以函数名称开头的分层名称也表示返回值的变量.但旧的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
Run Code Online (Sandbox Code Playgroud)

另一个使用包含函数名称的分层名称的有趣示例.

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
Run Code Online (Sandbox Code Playgroud)

函数调用x = myfunc1(3,2)构造一个调用框架myfunc1并传递值以进行评估.范围myfunc1top.myfunc1不同.以...开头的层次结构名称myfunc1是指函数的当前调用框架,同时top.myfunc1指的是模块内部声明的函数范围top.所以消息将是1 2 3.


Gre*_*reg 5

看起来您正在引用 LRM 的非常旧的版本。获取最新的官方版本IEEE Std 1800-2012。您需要查看 § 13.4.1返回值和 void 函数。引用的段落和引用的代码之间缺少一行:

函数可以声明为 void 类型,它没有返回值。函数调用可以用作表达式,除非类型为 void,它们是语句:

示例代码并不是指您的问题层次名称访问,它是void返回类型的示例。

下面的示例代码演示了使用结构/联合返回类型的分层名称访问。请阅读第 7.2 节和第 7.3 节中有关结构体和联合的内容。

function struct { byte a,b; } struct_func (byte a,b);
  byte c;
  c = a ^ b;
  struct_func.a = a; // <== hierarchical name used inside the function
  struct_func.b = ~b;
endfunction

initial begin
  // function name is used within a hierarchical name ( struct member )
  $display("%h", struct_func(8'h42,8'hFE).b ); // returns 01
  // function name is used within a hierarchical name ( function member )
  $display("%h", struct_func.b ); // returns fe (last value of input b)
  // function name is used within a hierarchical name ( function member )
  $display("%h", struct_func.c ); // returns bc (last value of variable c)
end
Run Code Online (Sandbox Code Playgroud)

大多数情况下,您希望重用结构/联合定义,并且应将其定义为typedef. 下面的函数与上面的初始块产生相同的结果。

typedef struct { byte a,b; } my_struct;

function my_struct struct_func (byte a,b);
  byte c;
  c = a ^ b;
  struct_func.a = a; // <== hierarchical name used inside the function
  struct_func.b = ~b;
endfunction
Run Code Online (Sandbox Code Playgroud)