Home System Verilog Constructs Assoc Arrays
Main Menu
Home
System Verilog
    - Constructs
        -- Introduction
        -- Data Types
        -- Type Casting
        -- SV Arrays
        -- Assoc Arrays
        -- Dynamic Array
        -- Queues
        -- Operators
        -- Procedural statements
        -- Classes
        -- OOP Concepts
        -- Inter process communication
        -- Randomization
        -- Interface
        -- Clocking block
        -- Conststraits
        -- Coverage
        -- DPI
        -- Regular expression
        -- Files
    - SV Classes
    - Functional Coverage SV
    - Examples
    - Tools
    - Links
    - Books
    - Interview Questions SV
Verilog
Methodologies
Open Vera
Digital Concepts
Verification Basics
Protocols
Scripting
Articles
Videos
Interview Questions
Computer Architechture
C and C++
Blog/Article
AsicGuru Blog
Tags Cloud
Ads
Usefull Sites
Know Your IP/Location
Local Information India
Buy Car/Inverter Batteries
Real Estate India
Sports Accessories India
Associative Arrys in System Verilog
Share This Articale:

Associative Arrays :

An Associative array is a better option when the size of the collection is unknown or the data space is sparse. So the associative arrays are mainly used to model the sparse memories. In the associative arrays the storage is allocated only when we use it not initially like in dynamic arrays. Associative arrays can be assigned only to another Associative array of a compatible
type and with the same index type.

Another main difference between Associative array and normal arrays is in that in assoc arrays the arrays subscript can be any scalar value.

So to summarize :

  1. Dynamically allocated, non-contiguous elements
  2. Accessed with integer, or string index, single dimension
  3. Great for sparse arrays with wide ranging index
  4. Array functions: exists, first, last, next, prev

eg :

mem[address];     // where address is a integer

str_arr["str"];             // string index is used

class_arr[class_obj]   // index is class object

* Another important thing about assoc arrays is that they are stored in random order. Associative arrays are unpacked arrays.

Build in methods :

  • num() — returns the number of entries in the Associative array                      Eg:sram_model.num()
  • first() — assigns the value of the first index in the Associative array to the given index variable Eg:sram_model.first(i);
  • last() — assigns the value of the last index in the Associative array to the given index variable Eg:sram_model.last(i);
  • next() — assigns the value of the next index in the Associative array to the given index variable Eg:sram_model.next(i);
  • prev() — assigns the value of the previous index in the Associative array to the given index variable Eg:sram_model.prev(i);
  • delete() — removes all the elements in the Associative array.Eg:sram_model.delete(); If the index is specified, then the element at the specified index is deleted
  • exists() — checks if element exists at the specified index in the Associative array Eg:sram_model.delete();

Example :

module assoc;
    bit [31:0] mem[*];
    int assoc[string];
    bit [31:0] i;
    initial
    begin
        mem[ 0 ] = 32'hFFAAFFAA;
        mem[ 3 ] = 32'hFFAAFFAA;
        mem[ 5 ] = 32'hFFAAFFAA;
        mem[ 7 ] = 32'hFFAAFFAA;
        $display("size of array: %d\n", mem.num());

        // find first element
        mem.first(i);
        $display ("the first index of the array is %d", i);

        // find the next element, which is now BASE_ADDR +3:
        mem.next(i);
        $display ("the next index used of the array is %d", i);

        // find the last element
        mem.last(i);
        $display ("the last index used of the array is %d", i);

        mem.prev(i);
        $display ("the previous index used of the array is %d", i);

        mem.delete();
        $display("size of array: %d\n", mem.num());
        $display(" Existence of array: %d\n", mem.exists(i));
    end
endmodule

Output :


# size of array:           4
#
# the first index of the array is          0
# the next index used of the array is          3
# the last index used of the array is          7
# the previous index used of the array is          5
# size of array:           0
#
#  Existence of array:           0
#

This Articles is written/submitted by puneet (Puneet Aggarwal). You can also contribute to Asicguru.com. Click here to start


Prev << SV Arrays

Next >> Dynamic Array

Posted By : sharath - Sept. 22, 2009, 10:43 p.m.

hii ,, how to pass the class for associative array...tell me one EX of assco(class_obj) and assoc("string");

 
Login/Register
Register
Sign In
Login with :-
gmail-yahoo-twitter-facebook
| | |  
  •  
  • Bookmark
    ADS