Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does system verilog max() and min() functions return a queue and not a single element?

I noticed this interesting thing about the max() and min() functions in SV LRM (1800-2012) 7.12 (Array manipulation methods). I tried out the max() and min() functions in a dummy SV file

int a[3] = {0,5,5};
int q[$];
int b;
q = a.max(); // legal
b = a.max(); // illegal

The illegal statement error was

Incompatible complex type assignment
Type of source expression is incompatible with type of target expression.
Mismatching types cannot be used in assignments, initializations and
instantiations. The type of the target is 'int', while the type of the
source is 'int$[$]'.

So I commented out the illegal statement and tested it. It compiled and ran fine but I was hoping to get some more insight as to why the function returns a queue and not a single element - I printed out the contents of q and the size, but the size is still 1 and 5 is being printed just once. Kind of redundant then to make the max() and min() functions return a queue ?

like image 857
Prashanth R Avatar asked Nov 16 '17 20:11

Prashanth R


People also ask

How do I return a queue in SystemVerilog?

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.

What is queue in SystemVerilog?

A SystemVerilog queue is a First In First Out scheme which can have a variable size to store elements of the same data type. It is similar to a one-dimensional unpacked array that grows and shrinks automatically. They can also be manipulated by indexing, concatenation and slicing operators.

What is the difference between dynamic array and queue in SystemVerilog?

Queue has a dynamic and fixed size. Array has a fixed size. Stack has a dynamic and fixed size. Queue can contain elements of different data type.

How do I declare a queue in Verilog?

Queue in SystemVerilog Queues are declared using the same syntax as unpacked arrays, but specifying $ as the array size. In queue 0 represents the first, and $ representing the last entries. A queue can be bounded or unbounded.


1 Answers

The "SystemVerilog for Verification" book by Chris Spear and Greg Tumbush has a good explanation on this topic in Chapter 2.6.2, which I am quoting below:

"The array locator methods find data in an unpacked array. At first you may wonder why these return a queue of values. After all, there is only one maximum value in an array. However, SystemVerilog needs a queue for the case when you ask for a value from an empty queue or dynamic array."

like image 109
AndresM Avatar answered Oct 02 '22 14:10

AndresM