Array Methods

Let's first talk about methods specific to static, dynamic, and associative arrays before we discuss queue methods.

Static Array Methods

As static arrays in SystemVerilog are of fixed size, they do not have built-in methods for resizing or adding elements. However, you can use built-in system functions like $size and $dimensions to retrieve information about the array.

Example:

integer my_array[5:10];
$display("$size = %0d, $dimensions = %0d", $size(my_array), $dimensions(my_array));

This will output the size of my_array (which is 6) and the number of dimensions (which is 1).

Dynamic Array Methods

Dynamic arrays in SystemVerilog, being resizable, have methods to manage their size. Here are a few examples:

  • new[n]: Allocates n new elements to the array. If n is less than the current size, the array size is reduced, and excess elements are deleted. If n is greater than the current size, new elements are added and initialized to their default values.

  • delete: Deletes all elements in the array, reducing its size to zero.

Example:

integer dynamic_array[];
dynamic_array = new[5]; // Allocates 5 elements to the array
$display("Size of array after new[5]: %0d", dynamic_array.size());
dynamic_array.delete(); // Deletes all elements
$display("Size of array after delete: %0d", dynamic_array.size());

This will first display the size of dynamic_array as 5, then 0 after deletion.

Associative Array Methods

Associative arrays have several useful methods:

  • num: Returns the number of entries in the associative array.

  • delete: Deletes all elements in the associative array, reducing its size to zero.

  • exists(key): Returns 1 if the key exists in the array, and 0 otherwise.

Example:

integer associative_array[string];
associative_array["Alice"] = 85;
$display("Number of entries after adding Alice: %0d", associative_array.num());
$display("Does key 'Alice' exist? %0d", associative_array.exists("Alice"));
associative_array.delete();
$display("Number of entries after delete: %0d", associative_array.num());

This will display the number of entries as 1 after adding Alice, then show that the key "Alice" does exist, and finally display the number of entries as 0 after deletion.

Queue Methods

Now that we've discussed methods specific to static, dynamic, and associative arrays, let's delve into the methods that are unique to SystemVerilog queues.

Insertion and Removal

  • push_front(element): Inserts the element at the front of the queue.

  • push_back(element): Inserts the element at the end of the queue.

  • pop_front(): Removes and returns the first element from the queue.

  • pop_back(): Removes and returns the last element from the queue.

integer my_queue[];
my_queue.push_front(5);
my_queue.push_back(10);
$display("First element: %0d, Last element: %0d", my_queue.pop_front(), my_queue.pop_back());

This will first push 5 to the front and 10 to the back of the queue. Then it will display 5 as the first element and 10 as the last element.

Size Management and Deletion

  • size(): Returns the current size of the queue.

  • delete(): Deletes all elements in the queue.

$display("Size of queue: %0d", my_queue.size());
my_queue.delete();
$display("Size of queue after deletion: %0d", my_queue.size());

This will first display the size of my_queue before and after the deletion operation.

Other Useful Methods

  • insert(index, element): Inserts an element at a specific position in the queue.

  • delete(index): Deletes an element at a specific position in the queue.

  • find_first_index(value): Returns the index of the first occurrence of the given value in the queue. If the value is not found, it returns -1.

my_queue.insert(1, 20); // Inserts 20 at position 1
$display("Index of 20: %0d", my_queue.find_first_index(20));
my_queue.delete(1); // Deletes element at position 1
$display("Index of 20 after deletion: %0d", my_queue.find_first_index(20));

In this example, it will first insert 20 at index 1, display the index of 20, delete the element at index 1, and then display the index of 20 after deletion, which should be -1.

These methods allow you to manipulate queues in SystemVerilog with great flexibility, further enhancing their versatility and utility in complex verification environments.

Have a Question?

Feel free to ask your question in the comments below.

Please Login to ask a question.

Login Now