Arrays

We earlier studied the data types of System Verilog, but now we will study arrays of System Verilog, which is also a basic foundation of it.

This blog introduces the language’s arrays. Packed, unpacked, associative, and dynamic arrays are specifically explored. There is discussion of array assignment, indexing, slicing, array manipulation methods, and array ordering methods.

So first we will know what the basic array means. An array is a collection of variables of the same type that are all accessible using the same name plus one or more indices.

There are several sorts of arrays; a few array declaration examples are provided below which will be discussed further.

bit uP [3:0]; 					//1-D unpacked
logic myArray[ integer ]; 			//Associative array

Fixed-size arrays

In SystemVerilog, fixed-size arrays are also known as static arrays. When we define a static array, we allocate a set amount of memory to the array at compilation time. As a result, we can’t add new entries or resize the array while the simulation is running.

In SystemVerilog, static arrays are the most basic type of array to deal with. Static arrays are used in the same way as an array is used in Verilog.

Vector width and dimensions declared before the object name are referred to as “packed arrays” in SystemVerilog, whereas array size and dimensions stated after the object name are referred to as “unpacked arrays.”

The code below demonstrates the general syntax for declaring a static array in SystemVerilog.

<type> <size> <variable_name> <elements>;             //general syntax

Fixed arrays are further subdivided into packed arrays and unpacked arrays.

Packed and Unpacked Arrays

The major difference between an unpacked array and a packed array is that an unpacked array cannot be represented as a contiguous set of bits, but a packed array can be represented as a contiguous set of bits.

Another distinction is that when a packed array appears as a primary, it is regarded as a single vector.

Unpacked array

The dimensions stated following the data identifier name are referred to as an unpacked array. Any data type, including other packed or unpacked arrays, can be used to create an unpacked array.

This unpacked array can be expressed as follows (depending on the compiler):

unused uP0
unused uP1
unused uP2
unused uP3

As you can see, up0 through up3 are spread throughout many words. In other words, they are not connected or packed together.

Example

bit [8:0] array4[3:0];

The figure1 below depicts storing an unpacked array as a non-contiguous collection of bits.

Fig 1: Unpacked array example

Packed array

The dimensions provided before the data identifier name are referred to as a “packed array”. A packed array, in other words, is a means for subdividing a vector into subfelds that may be accessed as array elements.

It is important to note that packed arrays may only be composed of single-bit data types (bit, logic, reg), enumerated types, and recursively other packed arrays and packed structures.

This packed array is expressed as follows.

unused P3 P2 P1 P0

The packed array is written as follows. As you can see, p3 to p0 are continuous, or packed.

Example

bit [1:0] [8:0] array5;

The figure 2 below depicts storing an unpacked array as a contiguous collection of bits.

Fig 2: Packed array example

1-D  array

int array1 [6];                            //Compact declaration

int array2 [5:0];                          // Verbose declaration

2-D  array

int arr[2][3];                             //array has 2*3 =6 element

3-D  array

int arr[2][2][2];                          //array has 2*2*2=8 element

Example

This example demonstrates array declaration and manipulation using the for and foreach loops.

module fixedsize_array;
  //declaration of array’s
  int array1[6];               				//single dimension array
  int array2[5:0];             				//single dimension array
  int array3[2:0][3:0];        			//multi dimension array
  int array4[4:0];
 
  initial begin
    						//array initialization
    array1 = '{0,1,2,3,4,5};
    array2 = '{0,1,2,3,4,5};
    array3 = '{'{0,1,2,3},'{4,5,6,7},'{8,9,10,11}};
 
    						//displaying array elements
    $display("-------displaying array1-------");
    foreach(array1[i]) $display("\t array1[%0d] = %0d",i,array1[i]);
 
    $display("-------displaying array2-------");
    for(int i=0;i<6;i++) $display("\t array2[%0d] = %0d",i,array2[i]);
 
    $display("-------displaying array3-------");
    foreach(array3[i,j]) $display("\t array3[%0d][%0d] = %0d",i,j,array3[i][j]);
 
    $display("-------displaying uninitialized array4-------");
    for(int i=0;i<5;i++) $display("\t array4[%0d] = %0d",i,array4[i]);
  end
endmodule

Dynamic Array

A dynamic array is an unpacked array whose size may be set or altered while the programme is running.

You must use the “new” operator to create a dynamic array. The size of a dynamic array is set at instantiation. In the array declaration, dynamic array dimensions are represented by []. Syntax is as follows:

data_type array_name [ ];

There are some methods of dynamic arrays are as follow:

ew[] -> This function allocates storage.
size() -> returns the dynamic array’s current size.
delete() -> removes all elements from the array, resulting in a zero-sized array.

Resize

d_array1 = new[10];                        //dynamic array of 10 elements

The following syntax assigns d_array1 10 new memory places and deletes the previous values of d_array1. By expanding the current array with the following syntax, the old values of d_array1 members can be maintained.

d_array1 = new[10](d_array1);

Delete

d_array1.delete;                    //delete array

This method will delete the array1.

Example

Declaration, Allocation, and Initialization of a Dynamic Array

module dynamic_array;
  							//dynamic array declaration
  bit [7:0] d_array1[];
  int       d_array2[];
 
  initial begin
    $display("Before Memory Allocation");
    $display("\tSize of d_array1 %0d",d_array1.size());
    $display("\tSize of d_array2 %0d",d_array2.size());
 
   							 //memory allocation
    d_array1 = new[4];
    d_array2 = new[6];
     
    $display("After Memory Allocation");
    $display("\tSize of d_array1 %0d",d_array1.size());
    $display("\tSize of d_array2 %0d",d_array2.size());
     
    							//array initialization
    d_array1 = {0,1,2,3};
    foreach(d_array2[j])  d_array2[j] = j;
 
    $display("--- d_array1 Values are ---");
    foreach(d_array1[i])   $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);
    $display("---------------------------------");
 
    $display("--- d_array2 Values are ---");
    foreach(d_array2[i])   $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);
    $display("---------------------------------");
  end
 
endmodule

Output is shown in Figure 3 of the above dynamic array.

Fig 3: Dynamic array output

 

Associative array

As above demonstrated, dynamic arrays are handy when dealing with a group of contiguous variables whose quantity might vary dynamically (resize, allocate new elements, etc.).

An associative array, on the other hand, is similar to a lookup table. Memory is not allocated until it is needed, and data is kept in what are known as indices.

You may find data by utilising an index, which serves as a key to that place. This is analogous to a tag memory in a cache subsystem, where cache lines are sparsely distributed at indices known as tags.

You save data at a certain key (index) and retrieve it using the same key. This allows you to make use of limited sparse memory.

When the size of the variable collection is uncertain or the data space is limited, an associative array is preferable to a dynamic array. Associative array items are allocated dynamically. The items of an associative array are unpacked.

The associative array keeps track of the items that have been assigned values as well as their relative order based on the index data type. An unlawful index type is one that contains “real” or “shortreal” data types, or one that contains a real or a shortreal.

The syntax is as follows:

data_type array_id [index_type];

where

  1. data type: the array items’ data type
  2. array name: the associative array’s name.
  3. index type: the data type to be used as an index, or *.
  4. * specifies that the array is indexed by any integral expression of any length.

Method

Method Meaning
num() yields the number of associative array entries
delete(index) The entry at the specified is removed index.exa array.delete(index)
exists(index) If an element exists at the provided index, it returns 1, else it returns 0.
first(var) assigns the variable var the value of the first index
last(var) assigns the variable var the value of the last index
next(var) assigns the variable var the value of the next index
prev(var) assigns the variable var the value of the previous index

Example

module associative_array;
  							//array declaration
  int a_array[*]; 
  int index;
   
  initial begin
    							//allocation
    repeat(3) begin
      a_array[index] = index*2;
      index=index+4;
    end
 
    									//num() 
    $display("\tNumber of entries in a_array is %0d",a_array.num());
    $display("--- Associative array a_array entries and Values are ---");
    foreach(a_array[i]) $display("\ta_array[%0d] \t = %0d",i,a_array[i]);
    $display("--------------------------------------------------------");
     
    									//first()
    a_array.first(index);	
    $display("\First entry is \t a_array[%0d] = %0d",index,a_array[index]);
 
    									//last()
    a_array.last(index);
    $display("\Last entry is \t a_array[%0d] = %0d",index,a_array[index]);
  end
endmodule

Output is shown in Figure 4 of the above associative array.

Fig 4: Associative array output

Queue

SystemVerilog includes numerous data structures for storing a collection of things. A queue is one such data structure. A queue is a variable-size, ordered collection of homogenous (same type) entries.

A queue allows access to all of its parts as well as insertion and removal at the beginning or end of the queue.

The first element in the queue is represented by the 0th position, while the last element is represented by $.

A queue is a one-dimensional unpacked array. Queues are declared using the same syntax as unpacked arrays.

The main difference is that a queue may expand and shrink on its own. Queues, like arrays, may be handled using the indexing, concatenation, slicing operator syntax, and equality operators.

The distinction between an array and a queue is that an array is a non-variable-size ordered collection of homogenous things, whereas a queue is a variable-size ordered collection of heterogeneous objects.

Queues can be used to describe buffers that are last-in, first-out (LIFO) or first-in, first-out (FIFO). A queue’s length can be configurable, even zero.

As a result, a queue is an excellent option for a storage element that may shrink or expand as elements are deleted or added to it without imposing an artificial upper limit on its size as a regular fixed-size array would.

Queues are declared using the same syntax as unpacked arrays, but with the array size specified as “$.”

The syntax is as follows:

data_type queue_name [$ : ];

where data_type denotes the data type of the queue items and queue_name denotes the queue’s name.

Methods

Methods Meaning
size() returns the number of queue items.
insert() inserts the provided item at the supplied index position.
delete() the item at the provided index position is deleted.
push front() inserts the provided element at the front of the queue.
push back() inserts the given element at the back of the queue.
pop front() removes and returns the queue’s first element.
pop back() removes and returns the queue’s final element.

 

Example

module queues_array;
  								//declaration
  bit    [31:0] queue_1[$]; 					//unbounded queue
  string  queue_2[$]; 
   
  initial begin
    								//Initialization:
    queue_1 = {0,1,2,3};
    queue_2 = {"Red","Blue","Green"};
     
    								//Size-Method
    $display("----- Queue_1 size is %0d  -----",queue_1.size());
    foreach(queue_1[i]) $display("\tqueue_1[%0d] = %0d",i,queue_1[i]);   
    $display("----- Queue_2 size is %0d  -----",queue_2.size());
    foreach(queue_2[i]) $display("\tqueue_2[%0d] = %0s",i,queue_2[i]);
     
    								//Insert-Method
    queue_2.insert(1,"Orange");
    $display("----- Queue_2 size  after inserting Orange is %0d  -----",queue_2.size());
    foreach(queue_2[i]) $display("\tqueue_2[%0d] = %0s",i,queue_2[i]);
     
    								//Delete Method
    queue_2.delete(3);
    $display("----- Queue_2 size after Delete is %0d  -----",queue_2.size());
    foreach(queue_2[i])$display("\tqueue_2[%0d] = %0s",i,queue_2[i]);
  end
 
endmodule

Output is shown in Figure 5 of the above Queue.

Fig 5: Queue Output

As a result, we studied array and queue with some excellent examples to explain all of the details.

  1. So, in a few questions, let’s summarise the entire blog.
  2. What exactly is an array?
  3. What exactly is a fixed-size array?
  4. What is the difference between a packed and an unpacked array? Give an example of how many distinct types of static arrays there are.
  5. What exactly is a “dynamic array,” and how does it vary from a static array?
  6. What is an associative array, and what distinguishes it from a dynamic array?
  7. What exactly is a queue? Also, discuss its methodologies.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments