System Verilog offers several built-in methods to work with arrays. These methods can be applied to static, dynamic arrays and queue, providing flexibility in managing data. Here’s a look at each of these methods and how they work:
1. find() and find_index() Methods
The find method searches for elements in an array that satisfy a given condition and returns them in a new array. The find_index method, similarly, returns an array of indices of elements that match the condition. The with keyword in System Verilog is used in conjunction with array methods like find and find_index to specify the condition or filter expression for the search. It allows you to define a lambda-like inline function that evaluates each element of the array against the given condition.
- Syntax:
array_name.find(item) with (expression);
array_name.find_index(item) with (expression);
- Example:
program find_example;
int arr[] = '{1, 3, 7, 10, 15};
int result[$];
int indices[$];
initial begin
result = arr.find(item) with (item > 5);
indices = arr.find_index(item) with (item > 5);
$display("Elements greater than 5: %p", result);
$display("Indices of elements greater than 5: %p", indices);
end
endprogram
Output:
Elements greater than 5: '{7, 10, 15}
Indices of elements greater than 5: '{2, 3, 4}
2. min() and max() Methods
The min and max methods return the minimum and maximum values in the array, respectively.
- Syntax:
array_name.min();
array_name.max();
- Example:
int my_array[] = {15, 6, 23, 9, 42};
int minimum = my_array.min(); // Returns 6
int maximum = my_array.max(); // Returns 42
3. sum() Method
The sum method calculates the sum of all elements in the array.
- Syntax:
array_name.sum();
- Example:
int my_array[] = {1, 2, 3, 4, 5};
int total_sum = my_array.sum(); // Returns 15
4. product() Method
The product method calculates the product of all elements in the array.
- Syntax:
array_name.product();
- Example:
int my_array[] = {2, 3, 4};
int total_product = my_array.product(); // Returns 24
5. sort() Method
The sort method sorts the array in ascending order. This is useful when you need to organize data or perform binary search operations on sorted arrays.
- Syntax:
array_name.sort();
- Example:
int my_array[] = {4, 1, 5, 3, 2};
my_array.sort(); // The array becomes {1, 2, 3, 4, 5}
You can also use a comparison function to customize sorting behavior:
my_array.sort( function(int a, int b) return a > b; );
6. rsort() Method
The rsort method sorts the array in descending order.
- Syntax:
array_name.rsort();
- Example:
int my_array[] = {4, 1, 5, 3, 2};
my_array.rsort(); // The array becomes {5, 4, 3, 2, 1}
7. reverse() Method
The reverse method reverses the order of elements in the array, making it useful when you need to invert the order of elements without sorting.
- Syntax:
array_name.reverse();
- Example:
int my_array[] = {1, 2, 3, 4, 5};
my_array.reverse(); // The array becomes {5, 4, 3, 2, 1}
8. shuffle() Method
The shuffle method randomly shuffles the elements of the array. It’s commonly used in verification for creating randomized test cases.
- Syntax:
array_name.shuffle();
- Example:
int my_array[] = {1, 2, 3, 4, 5};
my_array.shuffle(); // The elements are rearranged randomly
9. unique() Method
The unique method removes duplicate elements from the array, keeping only the first occurrence of each element.
- Syntax:
array_name.unique();
- Example:
int my_array[] = {1, 2, 2, 3, 3, 4};
my_array.unique(); // The array becomes {1, 2, 3, 4}
10. and() and or() Methods
The and and or methods return the bitwise AND or OR of all elements in the array. These methods are handy when you need to apply logical operations across an array of values.
- Syntax:
array_name.and();
array_name.or();
- Example:
int my_array[] = {1, 3, 7};
int result_and = my_array.and(); // Returns bitwise AND of elements
int result_or = my_array.or(); // Returns bitwise OR of elements
Practical Example: Using Array Methods in SystemVerilog
Let’s look at an example to see how some of these methods can be applied in practice. The code below demonstrates initializing an array and using different array manipulation methods:
module array_example;
int my_array[] = {5, 8, 3, 3, 7, 10, 2};
int found[$];
int m_array[$];
int unique_array[$];
initial begin
// Display original array
$display("Original Array: %p", my_array);
// Find elements greater than 5
found = my_array.find(item) with (item > 5);
$display("Elements > 5: %p", found);
// Find the minimum and maximum values
m_array = my_array.min();
$display("Min: %0p", m_array);
m_array = my_array.max();
$display("Max: %0p", m_array);
// Sort the array in ascending order
my_array.sort();
$display("Sorted Array: %p", my_array);
// Reverse the sorted array
my_array.reverse();
$display("Reversed Array: %p", my_array);
// Remove duplicates
unique_array = my_array.unique();
$display("Unique Elements: %p", unique_array);
// Shuffle the array
my_array.shuffle();
$display("Shuffled Array: %p", my_array);
end
endmodule
Output:
Original Array: '{5, 8, 3, 3, 7, 10, 2}
Elements > 5: '{8, 7, 10}
Min: '{2}
Max: '{10}
Sorted Array: '{2, 3, 3, 5, 7, 8, 10}
Reversed Array: '{10, 8, 7, 5, 3, 3, 2}
Unique Elements: '{10, 8, 7, 5, 3, 2}
Shuffled Array: '{7, 5, 10, 3, 8, 2, 3}