|HOME |ABOUT |ARTICLES |ACK |FEEDBACK |TOC |LINKS |BLOG |JOBS |


Tutorials



ARRAY METHODS



Array Methods:



Systemverilog provides various kinds of methods that can be used on arrays. They are

Array querying functions
Array Locator Methods
Array ordering methods
Array reduction methods
Iterator index querying



Array Querying Functions:



SystemVerilog provides new system functions to return information about an array. They are


(S)$left


$left shall return the left bound (MSB) of the dimension.


(S)$right


$right shall return the right bound (LSB) of the dimension.


(S)$low


$low shall return the minimum of $left and $right of the dimension.


(S)$high


$high shall return the maximum of $left and $right of the dimension.


(S)$increment


$increment shall return 1 if $left is greater than or equal to $right and -1 if $left is less than $right.


(S)$size


$size shall return the number of elements in the dimension, which is equivalent to $high - $low +1.


(S)$dimensions


$dimensions shall return the total number of dimensions in the array.




EXAMPLE : arrays
module arr;
bit [2:0][3:0] arr [4:0][5:0];

initial
begin
$display(" $left %0d $right %0d $low %0d $high %0d $increment %0d $size %0d $dimensions %0d",$left(arr),$right(arr),$low(arr),$high(arr),$increment(arr),$size(arr),$dimensions(arr) );
end
endmodule

RESULTS :

$left 4 $right 0 $low 0 $high 4 $increment 1 $size 5 $dimensions 4


Array Locator Methods:



Array locator methods operate on any unpacked array, including queues, but their return type is a queue. These locator methods allow searching an array for elements (or their indexes) that satisfies a given expression. Array locator methods traverse the array in an unspecified order. The optional "with" expression should not include any side effects; if it does, the results are unpredictable.

The following locator methods are supported (the "with" clause is mandatory) :



(S)find()


find() returns all the elements satisfying the given expression


(S)find_index()


find_index() returns the indexes of all the elements satisfying the given expression


(S)find_first()


find_first() returns the first element satisfying the given expression


(S)find_first_index()


find_first_index() returns the index of the first element satisfying the given expression


(S)find_last()


find_last() returns the last element satisfying the given expression


(S)find_last_index()


find_last_index() returns the index of the last element satisfying the given expression

For the following locator methods the "with" clause (and its expression) can be omitted if the relational operators (<, >, ==) are defined for the element type of the given array. If a "with" clause is specified, the relational operators (<, >, ==) must be defined for the type of the expression.



(S)min()


min() returns the element with the minimum value or whose expression evaluates to a minimum


(S)max()


max() returns the element with the maximum value or whose expression evaluates to a maximum


(S)unique()


unique() returns all elements with unique values or whose expression is unique


(S)unique_index()


unique_index() returns the indexes of all elements with unique values or whose expression is unique



EXAMPLE :
module arr_me;
string SA[10], qs[$];
int IA[*], qi[$];
initial
begin
SA[1:5] ={"Bob","Abc","Bob","Henry","John"};
IA[2]=3;
IA[3]=13;
IA[5]=43;
IA[8]=36;
IA[55]=237;
IA[28]=39;
// Find all items greater than 5
qi = IA.find( x ) with ( x > 5 );
for ( int j = 0; j < qi.size; j++ ) $write("%0d_",qi[j] );
$display("");
// Find indexes of all items equal to 3
qi = IA.find_index with ( item == 3 );
for ( int j = 0; j < qi.size; j++ ) $write("%0d_",qi[j] );
$display("");
// Find first item equal to Bob
qs = SA.find_first with ( item == "Bob" );
for ( int j = 0; j < qs.size; j++ ) $write("%s_",qs[j] );
$display("");
// Find last item equal to Henry
qs = SA.find_last( y ) with ( y == "Henry" );
for ( int j = 0; j < qs.size; j++ ) $write("%s_",qs[j] );
$display("");
// Find index of last item greater than Z
qi = SA.find_last_index( s ) with ( s > "Z" );
for ( int j = 0; j < qi.size; j++ ) $write("%0d_",qi[j] );
$display("");
// Find smallest item
qi = IA.min;
for ( int j = 0; j < qi.size; j++ ) $write("%0d_",qi[j] );
$display("");
// Find string with largest numerical value
qs = SA.max with ( item.atoi );
for ( int j = 0; j < qs.size; j++ ) $write("%s_",qs[j] );
$display("");
// Find all unique strings elements
qs = SA.unique;
for ( int j = 0; j < qs.size; j++ ) $write("%s_",qs[j] );
$display("");
// Find all unique strings in lowercase
qs = SA.unique( s ) with ( s.tolower );
for ( int j = 0; j < qs.size; j++ ) $write("%s_",qs[j] );
end
endmodule

RESULTS :

13_43_36_39_237_
2_
Bob_
Henry_

3_
_
_Bob_Abc_Henry_John_
_Bob_Abc_Henry_John_


Array Ordering Methods:



Array ordering methods can reorder the elements of one-dimensional arrays or queues. The following ordering methods are supported:



(S)reverse()


reverse() reverses all the elements of the packed or unpacked arrays.


(S)sort()


sort() sorts the unpacked array in ascending order, optionally using the expression in the with clause.


(S)rsort()


rsort() sorts the unpacked array in descending order, optionally using the with clause expression.


(S)shuffle()


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


EXAMPLE:
module arr_order; string s[] = '{ "one", "two", "three" };
initial
begin
s.reverse;
for ( int j = 0; j < 3;j++ ) $write("%s",s[j] );
s.sort;
for ( int j = 0; j < 3;j++ ) $write("%s",s[j] );
s.rsort;
for ( int j = 0; j < 3;j++ ) $write("%s",s[j] );
s.shuffle;
for ( int j = 0; j < 3;j++ ) $write("%s",s[j] );
end
endmodule
RESULT:

three two one
one three two
two three one
three one two


Array Reduction Methods :



Array reduction methods can be applied to any unpacked array to reduce the array to a single value. The expression within the optional "with" clause can be used to specify the item to use in the reduction. The following reduction methods are supported:



(S)sum()


sum() returns the sum of all the array elements.


(S)product()


product() returns the product of all the array elements


(S)and()


and() returns the bit-wise AND ( & ) of all the array elements.


(S)or()


or() returns the bit-wise OR ( | ) of all the array elements


(S)xor()


xor() returns the logical XOR ( ^ ) of all the array elements.


EXAMPLE:
module array_redu();
byte b[] = { 1, 2, 3, 4 };
int sum,product,b_xor;
initial
begin
sum = b.sum ; // y becomes 10 => 1 + 2 + 3 + 4
product = b.product ; // y becomes 24 => 1 * 2 * 3 * 4
b_xor = b.xor with ( item + 4 ); // y becomes 12 => 5 ^ 6 ^ 7 ^ 8
$display(" Sum is %0d, product is %0d, xor is %0b ",sum,product,b_xor);
end
endmodule
RESULT

Sum is 10, product is 24, xor is 1100


Iterator Index Querying:



The expressions used by array manipulation methods sometimes need the actual array indexes at each iteration, not just the array element. The index method of an iterator returns the index value of the specified dimension.


// find all items equal to their position (index)
q = arr.find with ( item == item.index );
// find all items in mem that are greater than corresponding item in mem2
q = mem.find( x ) with ( x > mem2[x.index(1)][x.index(2)] );



Index
Introduction
Data Types
Literals
Strings
Userdefined Datatypes
Enumarations
Structures And Uniouns
Typedef
Arrays
Array Methods
Dynamic Arrays
Associative Arrays
Queues
Comparison Of Arrays
Linked List
Casting
Data Declaration
Reg And Logic
Operators 1
Operators 2
Operator Precedency
Events
Control Statements
Program Block
Procedural Blocks
Fork Join
Fork Control
Subroutines
Semaphore
Mailbox
Fine Grain Process Control

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!





<< PREVIOUS PAGE

TOP

NEXT PAGE >>

copyright 2007-2017 :: all rights reserved www.testbench.in::Disclaimer