Associative Arrays in System Verilog

Arrays :

                       1. size( )

                       2. delete( )

                                    1.size( )

                                   2. delete( ) & delete(index)

                                   3. insert(  )

                                   4. push( ) methods

                                   5. pop( ) methods

                                   1.size( )

                                   2.delete ( ) & delete(index)

                                  3.num ( )

                                  4.exists( )

                                  5.first( ) ,next( ), prev ( ), last( )

Common  Methods for Dynamic Arrays & Queues :

                       Associative Arrays in System Verilog

 

What is an Associative Array in System Verilog?

           Associative Array is a unpacked array which is used when the size of collection  is unknown and data space is scattered .Scattered Data space  means the indices are not sequential ,they are scattered.  i.e. ” index  1″ may not be present immediately after “index 0”.

          –> Associative Array is a memory which stores data using non-sequential indices and this non-sequential indices are

                 called as “keys”.

          –> keys (Index) in of associative array is not restricted to integer type (i.e.  numbers ) . Key may be integer (or)

                string type  (or) any type  . In associative arrays we can also use strings(“names”) as  identity for data.

            –> Associative arrays do not have any storage allocated until it is used. The unused spaces can be utilised for

                   different processes.

             Syntax : data_type variable_name[ index_type ];

                                — data_type : the type of data we are storing in the array.

                                — variable_name it is name for array.

                                — index_type :  the data type of index or key .

              Example :   1. int packet[ int ];  // here index is integer type i.e all indices must be numbers.

                                        packet[2] =50;

                                   2. int xtn[ string ];  // here index is string type i.e all indices must be names or characters

                                         xtn[“apple”] = 100 ;

                                  3. int mem [ * ];  // here we can declare index as any type i.e. one index may be number and anothe index

                                                                   may be string.

                                         mem[1]=20;

                                        mem[“red”] =50;

           In the above example , we are using “*” as index type which is a wildcard index type. If we mention “*” as index type ,it will accept any type of data as index for that array. one index may be integer type and one index may be string type .

 

Simulated_Example:

module tb();

             int mem[int];

             int transaction[string];

             int array[*];

             initial

              begin

                mem[0] = 10;

                transaction [“packet”] = 50;

               array [0] = 25;

               array [“apple”] = 35;

               $display(” mem[0] = %0p ” ,mem[0]);

               $display(” transaction [packet] = %0p”, transaction[“packet”]);

               $display(” array[0] = %0p “,array[0]);

               $display(” array[apple] = %0p”, array[“apple”]);

        end

endmodule

 

 

 

 

 

Simulator_output:

     *************************************************************************

       mem[0] = 10

       transaction [packet] = 50

       array[0] = 25

       array[apple] = 35

      ************************************************************************