Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning queue from function in systemverilog

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?

like image 786
e19293001 Avatar asked Dec 20 '12 01:12

e19293001


People also ask

How do I return a queue from a function?

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. Note that in the typedef the [$] comes after the type name, while the queue element type is before the type name. Save this answer.

How do you return an array from a function in SystemVerilog?

If you want to return the dynamic array using return in your function, then you need a typedef. Typedef is needed when you want a function to return an unpacked type.

How do I copy a queue in SystemVerilog?

You can use Q2 = Q1; it will construct and copy the aggragate array. Q2 may have data already. So Q2 = Q1 will overwrite existing entries.

How do you reverse a queue in SystemVerilog?

you can simply use q. reverse() to reverse entire queue.


2 Answers

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.

like image 54
dwikle Avatar answered Sep 28 '22 06:09

dwikle


Down there is the example of code that worked for me....

typedef bit[31:0] bitQueue[$];

//Merges first and second queue, //with second queue being added to the end of first

function bitQueue   mergeEnd(bit[31:0] queue_1[$], bit[31:0] queue_2[$]);
  foreach (queue_2[i]) queue_1.push_back(queue_2[i]);
  return queue_1;
endfunction
like image 26
Miroslav Gasic Avatar answered Sep 28 '22 07:09

Miroslav Gasic