SystemVerilog数组是通过值还是引用传递的?

时间:2014-04-30 18:53:45

标签: arrays system-verilog

默认情况下,SystemVerilog是按值还是引用传递数组?

例如:

int array[5] = '{0,1,2,3,4};
some_function(array);  // <-- value or reference?

1 个答案:

答案 0 :(得分:16)

默认情况下,SystemVerilog按值传递数组,复制整个数组。

出于性能原因,建议尽可能通过引用传递数组。

  • 如果您希望函数修改数组,请使用ref
  • 如果您希望函数读取数组,请使用const ref

示例:

  function void pass_by_value(int array[5], int queue[$], int assoc[int]);
    // Default.
    // A copy of the arrays is made in this function
  endfunction

  function void pass_by_ref(ref int array[5], ref int queue[$],
                            ref int assoc[int]);
    // Original arrays are being referenced
  endfunction

  function void pass_by_const_ref(const ref int array[5],
                                  const ref int queue[$],
                                  const ref int assoc[int]);
    // Original arrays are being referenced
    // And they can be read but cannot be modified in this function 
  endfunction

EDA游乐场示例:http://www.edaplayground.com/x/2m9