Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are SystemVerilog arrays passed by value or reference?

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?
like image 816
Victor Lyuboslavsky Avatar asked Apr 30 '14 18:04

Victor Lyuboslavsky


People also ask

Do arrays pass references?

Since an array is passed as a pointer, the array's memory is not copied. The function uses the memory of the same array that is passed to it, and can change what is in that memory. Because arrays are already pointers, there is usually no reason to pass an array explicitly by reference.

What is pass by value and pass by reference in SV?

Passing by reference means that the method which gets the parameter is able to change the variable such that the original variable is also changed. Passing by value means that a copy of the value is passed, and any change to that copy does not reflect back on the original variable.

How do you define an array in Systemverilog?

Static ArraysA static array is one whose size is known before compilation time. In the example shown below, a static array of 8-bit wide is declared, assigned some value and iterated over to print its value. Static arrays are further categorized into packed and unpacked arrays.

Can function return array Systemverilog?

Replies. Yes you can, but you must use a typedef to define the return type.


1 Answers

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

like image 75
Victor Lyuboslavsky Avatar answered Oct 14 '22 13:10

Victor Lyuboslavsky