Different Array Types and Queues in System Verilog

Dynamic Array: 

  • Usage of dynamic array when user to allocate its size for storage during run time.
  • Dynamic array store a contiguous collection of data.
  • The array indexing should be always integer type.

To allocate the size of a dynamic array, we have to use a new[] operator.

Example:

int dyn_arr [];
initial
  begin
    dyn_arr = new[10];        //Allocated 10 elements 
  end

How to resize a dynamic array?

Example:

initial 
  begin 
    dyn_arr = new[25](dyn_arr);     //Resize the Array and Copy 
  end 

Dynamic Array Methods:

  • function int size()
    • Returns the current size of the array
  • function void delete()
    • Empties array contents and zero-sizes it
int my_addr[ ] = new[256];
initial begin
$display("Size of my_addr = %0d", my_addr.size() );
my_addr.delete();
$display("Size of my_addr (after delete) = %0d", my_addr.size());
end

Associative Array: 

  • It is also allocated during run time.
  • This is the array, user need when data is sparse.
  • It is used when we don’t have to allocate contiguous collection of data, or data in a proper sequence or index.
  • Indexing is not regular, can be accessed using indexing like integer or string type or any scalar. 

Example:

bit i_array[*]; // associative array of bits (unspecified index)
                     // unspecified index (*) implies any integral value
bit [7:0] age [string]; // associative array of 8-bit vectors, indexed by string
initial 
  begin
    string tom = “tom”;
    age [tom] = 21;
    age [“joe”] = 32;
    $display("%s is %d years of age ", tom, age[tom], "[%0d ages available]", age.num());
  end

Associative Array Methods:

  • function int num()
    • Returns the number of entries in the array, if empty returns 0
  • function void delete( [input index] )
    • Index is optional
    • If index is specified, deletes the item at the specified index
    • If index is not specified the all elements in the array are removed
  • function int exists ( input index );
    • Checks if an element exists at the specified index within the given array.
    • Returns 1 if the element exists, otherwise it returns 0
  • function int first( ref index )
    • Assigns to the given index variable the value of the first (smallest) index in the associative array
    • It returns 0 if the array is empty, and 1 otherwise
  • function int last( ref index )
    • Assigns to the given index variable the value of the last (largest) index in the associative array
    • It returns 0 if the array is empty, and 1 otherwise.
  • function int next( ref index );
    • finds the entry whose index is greater than the given index. If there is a next entry, the index variable is assigned the index of the next entry, and the function returns 1
    • Otherwise, index is unchanged, and the function returns 0
  • function int prev( ref index );
    • finds the entry whose index is smaller than the given index. If there is a previous entry, the index variable is assigned the index of the previous entry, and the function returns 1
    • Otherwise, the index is unchanged, and the function returns 0  

Queue:

  • Queue is a variable size, ordered collection of Homogenous Data.
  • It is flexible, as it is variable in size and analogous to an 1-dimensional Unpacked array that can shrink & grow automatically and can be of size zero.
  • The main advantage of queue over dynamic array is that, we don’t need new[] operator to allocate storage space for a queue.
  • The other advantages of queue over dynamic array is that we can manipulate the queue using various queue methods like: push, pop, delete, insert, size.

Example:

module test_example ; 
int my_queue[$] = { 1, 2, 3 }; 
string s_queue [$] = {"first","second","third","fourth"}; 
string store;
initial 
begin
// Use of the size() method/operator 
$display("\n size() operator used"); 
for (int i = 0 ; i < my_queue.size(); i++ ) 
$display (my_queue[i]); 
$display("\n\n Elements of s_queue is :"); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of insert() method/operator 
s_queue.insert(1,"next"); // Previous element 1 is now turned to element 2. 
s_queue.insert(2,"somewhere"); 
$display("\n\n insert() operator used"); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of delete() method/operator 
s_queue.delete(1); // delete the element 1 
s_queue.delete(3); // delete the element 3 
$display("\n\n delete() operator used"); 
for (int i = 0; i < s_queue.size; i++) 
$write(string_queue[i]," "); 
// Use of pop_front() method/operator (it deletes the front of the queue) 
store = s_queue.pop_front(); 
$display("\n\n pop_front() operator used"); 
$display(" %s",store); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of pop_back() method/operator (it deletes the back of the queue) 
store= s_queue.pop_back(); 
$display("\n\n pop_back() operator used"); 
$display(" %s",store); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," "); 
// Use of push_front() and push_back() method/operator 
s_queue.push_front("in-front"); 
s_queue.push_back("in-back"); 
$display("\n\n push_front() and push_back() operator used"); 
for (int i = 0; i < s_queue.size; i++) 
$write(s_queue[i]," \n "); 
end 
endmodule