associtive array

Associate array:
When the size of the collection is unknown or the data space is sparse and non conginious, an associative array is a better option.

Associative arrays do not have any storage allocated until it is used, and the index expression is not restricted to integral expressions, but can be of any type.

An associative array implements a lookup table of the elements of its declared type. The data type to be used as an index serves as the lookup key, and imposes an ordering.


Syntax

data_type array_id [ index_type ];

where
data_type is the data type of the array elements. Can be any type allowed for fixed-size arrays.
array_id is the name of the array being declared.
index_type is the data-type to be used as an index, or *. If * is specified, then the array is indexed by any integral expression of arbitrary size. An index type restricts the indexing expressions to a particular type.





Declaring Associative Arrays

logic [7:0] a_array[*]; // wildcard index type can be indexed by any integral datatype.

logic [7:0] a_array[int]; // int index type

integer age[string]; // string index type

int array_name [ some_Class ];// Class index

int array_name [ integer ];// Integer index

typedef bit signed [4:1] Nibble;

int array_name [ Nibble ]; // Signed packed array

array methos

Array operation Methods :

Operations
 foreach is used with associative arrays

 Reading from an element that has not been allocated results in a x for 4- state and a 0 for 2-state
• num() — returns the number of entries in the Associative array

• first() — assigns the value of the first index in the Associative array to the given index variable

• last() — assigns the value of the last index in the Associative array to the given index variable

• next() — assigns the value of the next index in the Associative array to the given index variable

• prev() — assigns the value of the previous index in the Associative array to the given index variable

• delete() — removes all the elements in the Associative array.

o 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

Array reduction methods :


Array reduction methods (Can be used on unpacked arrays: fixed,dynamic, queue, associative)

sum, product, and, or and xor
/// program here update later ///////////
bit on[10]
int summ;
initial begin
foreach (on[i])
on[i]=i; //0,1,0,1,0,1,0,1,0,1
$display(on.sum) //1
summ=on.sum; //summ=5
end

Array locator methods:

sort, rsort, reverse, find_first, find_first_with_index

int q[$] = {1,3,5,7}, tq[$];
int d[] = ‘{9,1,8,3,4};
int f[6] = ‘{1,6,2,6,8,6};
$display(q.sum, q.product); // 16 105
tq = q.min(); // {1}
tq = q.max(); // {7}
tq = f.unique; // {1,6,2,8}
tq = d.find with (item > 3); // {9,8,4}
tq = d.find_index with (item > 3); // {0,2,4}


int d[] = ‘{9,1,8,3,4,4}, tq[$];
tq = d.find with(item > 3);
tq = d.find_index with (item>99);
tq = d.find_first_index with (item==8);
tq = d.find_last_index with (item==4);



Array locator methods:


sort, rsort, reverse, shuffle

int d[] = ‘{9,1,8,3,4,4},
d.reverse();
d.sort();
d.rsort();
d.shuffle();

Example 01: Array_methods

module array_methods();
// find data in an unpacked array
// min, max and unique returns a queue

int data [0:9] = {1,2,3,6,5,7,8,9,9,2}; // int data[10]
int queue [$];
reg [7:0] memory [0:7] = '{1, 2, 3, 4, 5, 6, 7, 8};
initial begin
// return a queue
$display("Min of array %p",data.min);
$display("Max of array %p",data.max);
$display("Sum of array %0d",data.sum);
$display("Product of array %0d",data.product);
$display("XOR of array %0d",data.xor);
$display("AND of array %0d",data.and);
$display("OR of array %0d",data.or);
$display("Min of memory array %p",memory.min);
queue = data.min;
$display("Min size element is %0d",queue.pop_front());
queue = data.max;
$display("Max size element is %0d",queue.pop_front());
// call data.sort(), data.reverse(), data.rsort() and data.shuffle()
data.shuffle();
$finish;
end
endmodule

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# Min of array '{1}
# Max of array '{9}
# Sum of array 52
# Product of array 1632960
# XOR of array 14
# AND of array 0
# OR of array 15
# Min of memory array '{1}
# Min size element is 1
# Max size element is 9
# ------------------------------------------------------


Example 02: assignment_operator

module assignment_operator ();

// a, d are unsigned number
reg [31:0] a = 32'hf000_0000;

// b, c are signed number
integer b = 32'hf000_0000;
int c = 32'hf000_0000;


reg [31:0] d = 32'hf000_0000;

initial begin
$display (" a := %h", a );
a += 4; $display (" a += 4 := %h", a );
a -= 4; $display (" a -= 4 := %h", a );
a *= 4; $display (" a *= 4 := %h", a );
a /= 4; $display (" a /= 4 := %h", a );
a %= 17; $display (" a %s= 17 := %h", "%", a );
a &= 16'hFFFF; $display (" a &= 16'hFFFF := %h", a );
a |= 16'hFFFF; $display (" a |= 16'hFFFF := %h", a );
a ^= 16'hAAAA; $display (" a ^= 16h'AAAA := %h", a );
a <<= 4; $display (" a <<= 4 := %h", a );
a >>= 4; $display (" a >>= 4 := %h", a );
a <<<= 14; $display (" a <<<= 14 := %h", a );
a >>>= 14;
$display (" a >>>= 14 := %h", a );
b >>>= 14;
$display (" b >>>= 14 := %h", b );
c >>>= 14;
$display (" c >>>= 14 := %h", c );

d >>>= 14;
$display (" d >>>= 14 := %h", d );

b %= 17;
$display (" b %s= 17 := %h", "%", b);
b &= 16'hFFFF;
$display (" b &= 16'hFFFF := %h", b);
b |= 16'hFFFF;
$display (" b |= 16'hFFFF := %h", b);
b ^= 16'hAAAA;
$display (" b ^= 16h'AAAA := %h", b);

#1 $finish;
end
endmodule

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# a := f0000000
# a += 4 := f0000004
# a -= 4 := f0000000
# a *= 4 := c0000000
# a /= 4 := 30000000
# a %= 17 := 0000000e
# a &= 16'hFFFF := 0000000e
# a |= 16'hFFFF := 0000ffff
# a ^= 16h'AAAA := 00005555
# a <<= 4 := 00055550
# a >>= 4 := 00005555
# a <<<= 14 := 15554000
# a >>>= 14 := 00005555
# b >>>= 14 := ffffc000
# c >>>= 14 := ffffc000
# d >>>= 14 := 0003c000
# b %= 17 := fffffff3
# b &= 16'hFFFF := 0000fff3
# b |= 16'hFFFF := 0000ffff
# b ^= 16h'AAAA := 00005555
# ------------------------------------------------------

Example 01: Array_methods

module array_methods();
program assoc_array;

bit a_array[*]; // associative array of bits (unspecified index)

// unspecified index (*) implies any integral value
bit [7:0] month [string]; // associative array of 8-bit vectors, indexed by string

string JAN= "JAN";

initial
begin
month ["JAN"] = 31;
month ["FEB"] = 28;
month ["MAR"] = 31;
month ["APRIL"] = 30;
month ["MAY"] = 31;
month ["JUNE"] = 30;
month ["JULY"] = 31;
month ["AUG"] = 30;
month ["SEP"] = 31;
month ["OCT"] = 30;
month ["NOV"] = 31;
month ["DEC"] = 30;

$display("JAN is month 1st month of the year and having %d Days ", month[JAN], "[%0d months available]",month.num());
end
initial
foreach(month[i]) $display("\t month [ %0d] \t = %0d",i,month[i]);
endprogram

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# JAN is month 1st month of the year and having 31 Days [12 months available]
# month [ APRIL] = 30
# month [ AUG] = 30
# month [ DEC] = 30
# month [ FEB] = 28
# month [ JAN] = 31
# month [ JULY] = 31
# month [ JUNE] = 30
# month [ MAR] = 31
# month [ MAY] = 31
# month [ NOV] = 31
# month [ OCT] = 30
# month [ SEP] = 31
# ------------------------------------------------------