SystemVerilog offers several built-in methods to help with array manipulation, enabling easy searching, ordering, and altering of array elements. In this article, we will explore these methods and demonstrate how to use them in practical examples. Whether you’re filtering elements, sorting arrays, or performing bitwise operations, this guide provides clear examples to get you started with SystemVerilog array manipulation.
Key SystemVerilog Array Manipulation Methods
SystemVerilog provides multiple built-in methods that make it easier to work with arrays. These methods allow us to perform operations like searching, sorting, and modifying arrays based on specified expressions. Let’s break down the key methods in SystemVerilog array manipulation.
Array Searching and Filtering Methods
Array manipulation in SystemVerilog often involves searching and filtering elements based on certain conditions. Here are some common methods used for this purpose:
Method Name | Description |
---|---|
find() | Returns all elements satisfying a given expression. |
find_index() | Returns the indices of elements that meet the expression condition. |
find_first() | Returns the first element that satisfies the expression. |
find_first_index() | Returns the index of the first element that satisfies the condition. |
find_last() | Returns the last element satisfying the expression. |
find_last_index() | Returns the index of the last element that satisfies the expression. |
Example Code for Array Searching and Filtering:
module tb;
int array[9] = '{4, 7, 2, 5, 7, 1, 6, 3, 1};
int res[$];
initial begin
res = array.find(x) with (x > 3);
$display ("find(x) : %p", res);
res = array.find_index with (item == 4);
$display ("find_index : res[%0d] = 4", res[0]);
res = array.find_first with (item < 5 & item >= 3);
$display ("find_first : %p", res);
res = array.find_first_index(x) with (x > 5);
$display ("find_first_index: %p", res);
res = array.find_last with (item <= 7 & item > 3);
$display ("find_last : %p", res);
res = array.find_last_index(x) with (x < 3);
$display ("find_last_index : %p", res);
end
endmodule
Simulation Output:
find(x) : '{4, 7, 5, 7, 6}
find_index : res[0] = 4
find_first : '{4}
find_first_index: '{1}
find_last : '{6}
find_last_index : '{8}
Methods for Minimum, Maximum, and Unique Values
These methods help in identifying elements with the minimum or maximum values, as well as finding unique elements in the array.
Method Name | Description |
---|---|
min() | Returns the element with the minimum value or the one that evaluates to the minimum. |
max() | Returns the element with the maximum value or the one that evaluates to the maximum. |
unique() | Returns elements with unique values or those evaluating to unique values. |
unique_index() | Returns the indices of elements with unique values. |
Example Code for Minimum, Maximum, and Unique Values:
module tb;
int array[9] = '{4, 7, 2, 5, 7, 1, 6, 3, 1};
int res[$];
initial begin
res = array.min();
$display ("min : %p", res);
res = array.max();
$display ("max : %p", res);
res = array.unique();
$display ("unique : %p", res);
res = array.unique(x) with (x < 3);
$display ("unique : %p", res);
res = array.unique_index;
$display ("unique_index : %p", res);
end
endmodule
Simulation Output:
min : '{1}
max : '{7}
unique : '{4, 7, 2, 5, 1, 6, 3}
unique : '{4, 2}
unique_index : '{0, 1, 2, 3, 5, 6, 7}
Modifying and Sorting Arrays
Some methods allow us to directly modify and reorder arrays, such as reversing the order, sorting in ascending or descending order, or even shuffling the elements randomly.
Method Name | Description |
---|---|
reverse() | Reverses the order of elements in the array. |
sort() | Sorts the array in ascending order. Optionally, use a with clause for custom sorting. |
rsort() | Sorts the array in descending order. Optionally, use a with clause for custom sorting. |
shuffle() | Randomizes the order of elements in the array. No with clause is allowed. |
Example Code for Modifying and Sorting Arrays:
module tb;
int array[9] = '{4, 7, 2, 5, 7, 1, 6, 3, 1};
initial begin
array.reverse();
$display ("reverse : %p", array);
array.sort();
$display ("sort : %p", array);
array.rsort();
$display ("rsort : %p", array);
for (int i = 0; i < 5; i++) begin
array.shuffle();
$display ("shuffle Iter:%0d = %p", i, array);
end
end
endmodule
Simulation Output:
reverse : '{1, 3, 6, 1, 7, 5, 2, 7, 4}
sort : '{1, 1, 2, 3, 4, 5, 6, 7, 7}
rsort : '{7, 7, 6, 5, 4, 3, 2, 1, 1}
shuffle Iter:0 = '{6, 7, 1, 7, 3, 2, 1, 4, 5}
shuffle Iter:1 = '{5, 1, 3, 4, 2, 7, 1, 7, 6}
shuffle Iter:2 = '{3, 1, 7, 4, 6, 7, 1, 2, 5}
shuffle Iter:3 = '{6, 4, 7, 3, 1, 7, 5, 2, 1}
shuffle Iter:4 = '{3, 6, 2, 5, 4, 7, 7, 1, 1}
Bitwise Operations on Arrays
SystemVerilog also provides bitwise methods for array elements. These methods are useful when you need to perform operations such as AND, OR, XOR, or sum and product calculations.
Method Name | Description |
---|---|
sum() | Returns the sum of all array elements. |
product() | Returns the product of all array elements. |
and() | Returns the bitwise AND of all array elements. |
or() | Returns the bitwise OR of all array elements. |
xor() | Returns the bitwise XOR of all array elements. |
Example Code for Bitwise Operations:
module tb;
int array[4] = '{1, 2, 3, 4};
int res[$];
initial begin
$display ("sum = %0d", array.sum());
$display ("product = %0d", array.product());
$display ("and = 0x%0h", array.and());
$display ("or = 0x%0h", array.or());
$display ("xor = 0x%0h", array.xor());
end
endmodule
Simulation Output:
sum = 10
product = 24
and = 0x0
or = 0x7
xor = 0x4
Sorting Custom Objects in Arrays
If you’re working with custom classes, you can sort arrays of objects based on specific properties, such as names or ranks.
Example Code for Sorting Custom Objects:
class Register;
string name;
rand bit [3:0] rank;
rand bit [3:0] pages;
function new (string name);
this.name = name;
endfunction
function void print();
$display("name=%s rank=%0d pages=%0d", name, rank, pages);
endfunction
endclass
module tb;
Register rt[4];
string name_arr[4] = '{"alexa", "siri", "google home", "cortana"}';
initial begin
$display ("-------- Initial Values --------");
foreach (rt[i]) begin
rt[i] = new (name_arr[i]);
rt[i].randomize();
rt[i].print();
end
$display ("--------- Sort by name ------------");
rt.sort(x) with (x.name);
foreach (rt[i]) rt[i].print();
$display ("--------- Sort by rank, pages -----------");
rt.sort(x) with ( {x.rank, x.pages});
foreach (rt[i]) rt[i].print();
end
endmodule
Simulation Output:
-------- Initial Values --------
name=alexa rank=12 pages=13
name=siri rank=6 pages=12
name=google home rank=12 pages=13
name=cortana rank=7 pages=11
--------- Sort by name ------------
name=alexa rank=12 pages=13
name=cortana rank=7 pages=11
name=google home rank=12 pages=13
name=siri rank=6 pages=12
--------- Sort by rank, pages -----------
name=siri rank=6 pages=12
name=cortana rank=7 pages=11
name=alexa rank=12 pages=13
name=google home rank=12 pages=13
Conclusion
In this article, we have explored SystemVerilog array manipulation methods that allow you to efficiently search, filter, sort, and modify arrays. Whether you’re working with basic arrays or custom objects, these methods simplify the task of array manipulation. By using methods such as find()
, min()
, sort()
, and bitwise operations, you can easily perform common array operations in your SystemVerilog code.