Practical Guide to SystemVerilog Array Manipulation Methods

In this tutorial, we will dive into the world of array manipulation in SystemVerilog. We will discuss various built-in methods for searching, ordering, and reducing arrays, as well as techniques for effectively utilizing them in your code.

Understanding Array Manipulation Methods

In this section, we'll explore the essential aspects of array manipulation methods in SystemVerilog in a more user-friendly manner. We will examine how to use them without diving into the formal syntax.

Basic Structure

Array manipulation methods are applied to arrays using a dot . followed by the method name. If an array method requires an argument, it can be provided within parentheses () after the method name. Some methods also accept an optional with clause, which specifies an expression that the method should use while processing the array elements.

Here's the general pattern for using an array method:

result = array.methodName(argument) with (expression);

Working with Expressions and Iterators

Some array methods, such as find(), findIndex(), and sort(), use expressions within the with clause to filter, search, or order the array elements. The default iterator variable is item, which represents the current element of the array during iteration. However, you can specify a custom iterator variable using the method's argument.

For example:

int dataArray[] = {1, 2, 3, 4, 5};
int evenNumbers[$];

evenNumbers = dataArray.find(x) with (x % 2 == 0);

In this example, x is the custom iterator variable, and the with expression filters out the even numbers from the dataArray.

Error Handling

SystemVerilog array methods can generate errors in certain situations, such as:

  1. Specifying an iterator variable without a with clause.
  2. Using a with clause with a method that doesn't support it, like reverse() or shuffle().

In these cases, you'll receive a compiler error, and you'll need to fix the issue before your code can run successfully.

Now that you have a clear understanding of the basic structure and usage of array manipulation methods, we can proceed to explore their various categories and specific methods in more detail.

Array Locator Methods

Array locator methods operate on unpacked arrays, including queues. They are helpful for searching an array for elements or indices that satisfy a given expression. Let's explore some commonly used locator methods:

Find() and findIndex()

find() returns all the elements satisfying a given expression, while findIndex() returns the indices of all elements satisfying the expression.

Example:

int dataArray[] = {1, 3, 5, 7, 9};
int queueArray[$];

// Returns all odd elements
queueArray = dataArray.find() with (item % 2 == 1);

FindFirst() and findFirstIndex()

findFirst() returns the first element satisfying a given expression, while findFirstIndex() returns the index of the first element satisfying the expression.

Example:

int dataArray[] = {2, 4, 6, 8, 10};
int firstEven;

// Returns the first even element (2)
firstEven = dataArray.findFirst() with (item % 2 == 0); 

FindLast() and findLastIndex()

findLast() returns the last element satisfying a given expression, while findLastIndex() returns the index of the last element satisfying the expression.

Example:

int dataArray[] = {11, 13, 15, 17, 19};
int lastOdd;

// Returns the last odd element (19)
lastOdd = dataArray.findLast() with (item % 2 == 1);

Min(), max(), unique() and uniqueIndex()

min() returns the element with the minimum value, while max() returns the element with the maximum value. unique() returns all elements with unique values, and uniqueIndex() returns the indices of all elements with unique values.

Example:

int dataArray[] = {4, 2, 8, 2, 4};
int minValue, maxValue;
int uniqueValues[$], uniqueIndices[$];

minValue = dataArray.min();
maxValue = dataArray.max();
uniqueValues = dataArray.unique();
uniqueIndices = dataArray.uniqueIndex();

Array Ordering Methods

Array ordering methods reorder the elements of unpacked arrays, except for associative arrays. Let's explore some commonly used ordering methods:

Reverse()

reverse() reverses the order of elements in the array.

Example:

int dataArray[] = {10, 20, 30, 40};
dataArray.reverse(); // dataArray becomes {40, 30, 20, 10}

Sort() and rsort()

sort() sorts the array in ascending order, while rsort() sorts the array in descending order. Optionally, the with clause can be used with an expression to specify a sorting criterion.

Example:

int dataArray[] = {50, 40, 10, 30, 20};
dataArray.sort(); // dataArray becomes {10, 20, 30, 40, 50}
dataArray.rsort(); // dataArray becomes {50, 40, 30, 20, 10}

Shuffle()

shuffle() randomizes the order of the elements in the array.

Example:

int dataArray[] = {60, 70, 80, 90, 100};
dataArray.shuffle(); // dataArray gets a random order

Array Reduction Methods

Array reduction methods can be applied to unpacked arrays of integral values to reduce the array to a single value. The optional with clause is used to specify the values used in the reduction.

Sum() and product()

sum() returns the sum of all array elements, while product() returns the product of all array elements. If a with clause is specified, the sum or product of the values yielded by evaluating the expression for each array element is returned.

Example:

int dataArray[] = {1, 2, 3, 4, 5};
int sumValue, productValue;

sumValue = dataArray.sum();
productValue = dataArray.product();

And(), or() and xor()

and() returns the bitwise AND (&) of all array elements, while or() returns the bitwise OR (|) of all array elements. xor() returns the bitwise XOR (^) of all array elements. If a with clause is specified, the AND, OR, or XOR of the values yielded by evaluating the expression for each array element is returned.

Example:

int dataArray[] = {0x0f, 0x1f, 0x2f, 0x3f};
int andValue, orValue, xorValue;

andValue = dataArray.and();
orValue = dataArray.or();
xorValue = dataArray.xor();

Iterator Index Querying

For some expressions used in array manipulation methods, the actual array indices at each iteration are needed, not just the array element. The index() method of an iterator returns the index value of the specified dimension.

Example:

int dataArray[] = {10, 20, 30, 40, 50};
int indexArray[$];

indexArray = dataArray.findIndex with (item == item.index());

This tutorial covered the basics of array manipulation methods in SystemVerilog. You can now apply these methods to effectively search, order, and reduce arrays in your designs. Happy coding!