In SystemVerilog value can be passed via reference, which is indicated by ref keyword. But if a function has other parameters declared after the ref parameter they also seem to behave like passed-by-reference. Why it is so? (or what rule in LRM applies to that?)
My first thought to this counter-intuitive behavior was ‘it’s simulator bug’, so I checked other tools on edaplayground.com. The results were consistent, so it’s certainly intended behavior. In this case how to undo the ref if I really want to preserve the parameter order I like?
Code listing:
class Test;
function genArray(int count, ref byte arr[]);
arr = new[count];
for(int i=0; i<count; i++)
arr[i] = $urandom();
endfunction
function genArrayWrong(ref byte arr[], int count);
// ??
genArray(count, arr);
endfunction
endclass
module tb;
Test t;
byte randomStuff[];
initial begin
int myCount = 14;
t = new;
t.genArray(14, randomStuff); // ok
t.genArrayWrong(randomStuff, myCount); // ok
//t.genArrayWrong(randomStuff, 14); // won't compile
$display("random array: %p", randomStuff);
$finish;
end
endmodule