是SystemVerilog的阵列通过值或引用传递? [英] Are SystemVerilog arrays passed by value or reference?
本文介绍了是SystemVerilog的阵列通过值或引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
默认情况下,值或引用则SystemVerilog的传递数组?
例如:
int数组[5] ='{0,1,2,3,4};
some_function(数组); //< - 数值或引用?
解决方案
默认情况下,SystemVerilog的按值传递数组,复制整个数组。
建议参照尽可能通过阵列性能方面的原因。
- 如果你想你的函数修改数组,使用
REF
。 - 如果你想你的函数读取数组,使用
REF常量
。
例如:
函数void pass_by_value(int数组[5],INT队列[$],INT ASSOC [INT]);
//默认。
//阵列的副本在这个函数是由
endfunction可 函数void pass_by_ref(参考int数组[5],文献INT队列[$]
REF INT ASSOC [INT]);
//原始阵列被引用
endfunction可 函数void pass_by_const_ref(常量参考int数组[5]
常量REF INT队列[$]
常量REF INT assoc命令[INT]);
//原始阵列被引用
//他们可以读,但不能在此函数修改
endfunction可
在EDA游乐场举例: http://www.edaplayground.com/x/2m9
By default, does SystemVerilog pass arrays by value or reference?
For example:
int array[5] = '{0,1,2,3,4};
some_function(array); // <-- value or reference?
解决方案
By default, SystemVerilog passes arrays by value, copying the entire array.
It is recommended to pass arrays by reference whenever possible for performance reasons.
- If you want your function to modify the array, use
ref
. - If you want your function to read the array, use
const ref
.
Example:
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
Example on EDA Playground: http://www.edaplayground.com/x/2m9
这篇关于是SystemVerilog的阵列通过值或引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文