ch2-Array Manipulation
SystemVerilog Array Manipulation
There are many built-in methods in SystemVerilog to help in array searching and ordering.
Array manipulation methods simply iterate through the array elements and each element is used to evaluate the expression specified by the with
clause. The iterator argument specifies a local variable that can be used within the with
expression to refer to the current element in the iteration. If an argument is not provided, item is the name used by default.
Specifying an iterator argument without the with
clause is illegal.
Array Locator Methods
The with
clause and expresison is mandatory for some of these methods and for some others its optional.
Mandatory ‘with’ clause
These methods are used to filter out certain elements from an existing array based on a given expression. All such elements that satisfy the given expression is put into an array and returned. Hence the with
clause is mandatory for the following methods.
Method name | Description |
---|---|
find() | Returns all elements satisfying the given expression |
find_index() | Returns the indices of all elements satisfying the given expression |
find_first() | Returns the first element satisfying the given expression |
find_first_index() | Returns the index of the first element satisfying the given expression |
find_last() | Returns the last element satisfying the given expression |
find_last_index() | Returns the index of the last element satisfying the given expression |
Example
1 |
|
Simulation Log
1 |
|
Click to try this example in a simulator!
Optional ‘with’ clause
Methods | Description |
---|---|
min() | Returns the element with minimum value or whose expression evaluates to a minimum |
max() | Returns the element with maximum value or whose expression evaluates to a maximum |
unique() | Returns all elements with unique values or whose expression evaluates to a unique value |
unique_index() | Returns the indices of all elements with unique values or whose expression evaluates to a unique value |
Example
1 |
|
Simulation Log
1 |
|
Click to try this example in a simulator!
Array Ordering Methods
These methods operate and alter the array directly.
Method | Description |
---|---|
reverse() | Reverses the order of elements in the array |
sort() | Sorts the array in ascending order, optionally using with clause |
rsort() | Sorts the array in descending order, optionally using with clause |
shuffle() | Randomizes the order of the elements in the array. with clause is not allowed here. |
Example
1 |
|
Simulation Log
1 |
|
Click to try this example in a simulator!
Using array ordering on classes
1 |
|
Simulation Log
1 |
|
Click to try this example in a simulator!
Array Reduction Methods
Method | 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 |
1 |
|
Simulation Log
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!