System Verilog Associative Array Methods – first(),last(),prev(),next()
Associative Array Methods in System Verilog
Arrays :
1. size( )
2. delete( )
Queues
Bounded Queues
Queues methods
1.size( )
2. delete( ) & delete(index)
3. insert( )
4. push( ) methods
5. pop( ) methods
Associative arrays
Associative array methods
1.size( )
2.delete ( ) & delete(index)
3.num ( )
4.exists( )
5.first( ) ,next( ), prev ( ), last( )
Common Methods for Dynamic Arrays & Queues :
first( ) , next( ) , prev( ) & last( ) methods of Associative Array
5.a)first( ):
–> first method will return the first key of an array .
NOTE :
→There is no specific order for the keys . order of keys may change from simulator to simulator.
→ if we give four values to an array simulator may store any one of four values as first value as there is no specific order to
follow.
Syntax : array_name.first(variable);
Example :
int pkt [ *];
int first_key;
pkt [0:2] ={10,20,30};
first_key=pkt.first( );
5.b)next ( ):
–> next( ) method will check for the immediate next key for the key which we passed as an argument.
–>After identifying the next key it will return the key to the same argument and overrides the existing one.
Syntax : array_name.next(variable);
Example :
int pkt [ *];
int next_key;
pkt [0:2] ={10,20,30};
pkt.next(variable ); // here the returend value will override the existing value of
varibale
5.c)prev( ):
–> prev( ) method will check for the immediate previous key for the key which we passed as an argument.
–> after identifying the previous key it will return the key to the same argument and overrides the existing one.
Syntax : array_name.prev(variable);
Example :
int pkt [ *];
int next_key;
pkt [0:2] ={10,20,30};
pkt.next(variable ); // here the returend value will override the existing value of
varibale
5.d)last( ):
–> last ( ) method identifies the last key of the array and will return the last key
Syntax : array_name.last( );
Example :
int pkt [ *];
int last_key;
pkt [0:2] ={10,20,30};
last_key=pkt.last( );
Simulated_Example:
module tb();
int xtn[string];
string first_key;
string last_key;
initial
begin
xtn = ‘{
“green” : 10 ,
“blue” : 20 ,
“red” : 40,
“yellow” : 50 };
xtn.first(first_key);
$display(“first key of an array for first() method=%p”,first_key);
xtn.last(last_key);
$display(“last key of an array for last() method=%p”,last_key);
xtn.prev(last_key)
$display(” previous key of last_key is = %p”,last_key);
xtn.next(first_key)
$display(” next key of first_key is = %p “, first_key);
end
endmodule
Simulator_output :
********************************************************************************
first key of an array for first() method = “blue”
last key of an array for last() method = “yellow”
previous key of last_key is = “red”
next key of first_key is = “green”
*************************************************************************