returning queue from function in systemverilog

e19293001 picture e19293001 · Dec 20, 2012 · Viewed 13.7k times · Source

I can't compile this code:

      function integer[$] get_register_name;
                integer ret[$];
                ret.push_back(1);
                ret.push_back(2);
                return ret;
      endfunction

Is it possible to return a queue from a function?

Answer

dwikle picture dwikle · Dec 20, 2012

Yes, you can return a queue from a function. But to do so you must define a new type using typedef and return that type.

typedef integer queue_of_int[$];

function queue_of_int get_register_name();
   queue_of_int ret;
   ret.push_back(1);
   ret.push_back(2);
   return ret;
endfunction

Note that in the typedef the [$] comes after the type name, while the queue element type is before the type name.