且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是“参考在systemverilog中是什么意思?

更新时间:2023-01-24 12:49:57

通常,声明为 input 的任务和函数参数在进入例程时按值复制,声明为 的参数output 在从例程返回时按值复制.inout 参数在进入和从例程返回时都被复制.使用 ref 声明的参数不会被复制,而是对调用例程时使用的实际参数的引用.使用 ref 参数时有更严格的数据类型兼容性规则.

Normally, task and function arguments declared as input are copied by value upon entry to the routine, and arguments declared as output are copied by value upon returning from the routine. inout arguments are copied both upon entry and return from the routine. Arguments declared with ref are not copied but instead are references to the actual arguments used when making the call to the routine. There are much stricter data type compatibility rules when using ref arguments.

在消耗时间的任务中,可以使用 ref 而不是 inout 来捕获在任务处于活动状态时发生的值更改.请记住,inout 参数在被调用时被复制到任务中,并在任务返回时被复制出来.这是您应该尝试的示例.

In a task that consumes time, a ref can be used instead of an inout to capture value changes that occur while the task is active. Remember that an inout argument is copied into the task when it is called, and copied out when the task returns. Here is an example that you should try.

module top;
logic A,B;
task automatic mytask(inout logic arg1, ref logic arg2);
  #0 $display("%m %t arg1 %b arg2 %b",$time,arg1,arg2);
  // actual arguments have been set to 0
  #5 $display("%m %t arg1 %b arg2 %b",$time,arg1,arg2);
  #0 arg1 = 1; arg2 = 1;
  #5 $display("%m %t arg1 %b arg2 %b",$time,arg1,arg2);
endtask
initial #1 mytask(A,B);
initial begin
       A = 'z; B ='z;
       #2 A = 0; B = 0; // after call 
       // arguments have been set to 1
       #5 $display("%m %t A %b B %b",$time,A ,B);
       #5 $display("%m %t A %b B %b",$time,A ,B);
end
endmodule

查看 inout 和 pass by ref 参数之间的区别.

See the difference between the inout and pass by ref arguments.

请注意,类变量已经是对类句柄的引用,因此通过引用传递类变量很少有任何好处.此外,在函数中,ref 参数的唯一好处可能是在传递大型数据结构(如数组)而不是使用 inputoutput时的性能代码>,或输入代码>.

Note that a class variable is a reference to a class handle already, so passing a class variable by reference rarely has any benefit. Also, in a function, the only benefit of a ref argument might be performance in passing large data structures like an array instead of using an input, output, or inout.