Is it possible to use interface classes as function arguments with ref direction?
My example (just code snippet):
//DECLARATIONS
---------------------------------------------------------
typedef logic [7:0] my_type;
interface class _word#(type symbol_type);
pure virtual function int size();
pure virtual function symbol_type get_symbol(int i);
endclass
---------------------------------------------------------
interface class my_word extends _word#(my_type);
endclass
---------------------------------------------------------
class testword implements my_word;
typedef logic [7:0] custom_my_type;
custom_symbol_t q[$];
virtual function int size();
return $size(q);
endfunction
virtual function custom_symbol_t get_symbol(int i);
return q[i];
endfunction
function new(int n);
for(int i=0;i<n;i++)
begin
q.push_back(0);
end
endfunction
endclass
class my_User;
function void use_interface(ref my_word word);
$display("word size=%0d",word.size());
endclass
//USAGE
program runtest();
initial
begin
testword _word =new(9);
my_User user;
user.use_interface(_word);
end
endprogram
If I delete “ref” in use_interface, everything works fine. But with ref I have an error in questasim 10.5c
(when trying to run simulation, compilation is ok):Actual argument expression for ref formal ‘word’ is not an equivalent type.
ref arguments require equivalent types between the formal and actual arguments types. formal type ‘word’ is of type my_word, and actual is of type testword.
In reply to avek:
ref arguments require equivalent types between the formal and actual arguments types. formal type ‘word’ is of type my_word, and actual is of type testword.
There is no need to use a ref argument here. Please see Const ref - SystemVerilog - Verification Academy
Thank you for fast reply!
Maybe it was a bad example by me, and when using functions for printing, there is of cause no need to use a ref argument. But if i want to change structures that i send to ‘my_user’ - for example elements of queue. If I don’t use ‘ref’, such elements would be changed only inside ‘my_user’ because of copying by value.
I have found the solution:
To use declaration of the interface class,and send it to ‘my_user’ instead of ‘testword’ object, such as:
What you did works, but is an extra step. When you pass a class handle by value, you are only coping the value of the handle, not the object. Once you have a handle to an object, you can modify any of its members from within the function.