• 沒有找到結果。

tag is 0 for Invalid, 1 for Valid

7.8 Associative arrays

Dynamic arrays are useful for dealing with contiguous collections of variables whose number changes dynamically. When the size of the collection is unknown or the data space is sparse, 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.

The syntax to declare an associative array is as follows:

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 is *. 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. It shall be illegal for index_type to declare a type.

Examples of associative array declarations are as follows:

integer i_array[*]; // associative array of integer (unspecified // index)

bit [20:0] array_b[string]; // associative array of 21-bit vector, // indexed by string

event ev_array[myClass]; // associative array of event indexed by class // myClass

Array elements in associative arrays are allocated dynamically. An entry for a nonexistent associative array element shall be allocated when it is used as the target of an assignment or actual to an argument passed by reference. The associative array maintains the entries that have been assigned values and their relative order according to the index data type. Associative array elements are unpacked. In other words, other than for copying or comparing arrays, an individual element must be selected out of the array before it can be used in most expressions.

7.8.1 Wildcard index type For example:

int array_name [*];

Associative arrays that specify a wildcard index type have the following properties:

— The array may be indexed by any integral expression. Because the index expressions may be of dif-ferent sizes, the same numerical value can have multiple representations, each of a difdif-ferent size.

SystemVerilog resolves this ambiguity by removing the leading zeros and computing the minimal length and using that representation for the value.

— Nonintegral index values are illegal and result in an error.

— A 4-state index value containing X or Z is invalid.

— Indexing expressions are self-determined and treated as unsigned.

— A string literal index is automatically cast to a bit vector of equivalent size.

— The ordering is numerical (smallest to largest).

— Associative arrays that specify a wildcard index type shall not be used in a foreach loop (see 12.7.3) or with an array manipulation method (see 7.12) that returns an index value or array of values.

7.8.2 String index For example:

int array_name [ string ];

Associative arrays that specify a string index have the following properties:

— Indices can be strings or string literals of any length. Other types are illegal and shall result in a type check error.

VERIFICATION LANGUAGE

— An empty string “ ” index is valid.

— The ordering is lexicographical (lesser to greater).

7.8.3 Class index For example:

int array_name [ some_Class ];

Associative arrays that specify a class index have the following properties:

— Indices can be objects of that particular type or derived from that type. Any other type is illegal and shall result in a type check error.

— A null index is valid.

— The ordering is deterministic but arbitrary.

7.8.4 Integral index For example:

int array_name1 [ integer ];

typedef bit signed [4:1] SNibble;

int array_name2 [ SNibble ];

typedef bit [4:1] UNibble;

int array_name3 [ UNibble ];

Associative arrays that specify an index of integral data type shall have the following properties:

— The index expression shall be evaluated in terms of a cast to the index type, except that an implicit cast from a real or shortreal data type shall be illegal.

— A 4-state index expression containing X or Z is invalid.

— The ordering is signed or unsigned numerical, depending on the signedness of the index type.

7.8.5 Other user-defined types For example:

typedef struct {byte B; int I[*];} Unpkt;

int array_name [ Unpkt ];

In general, associative arrays that specify an index of any type have the following properties:

— Declared indices shall have the equality operator defined for its type to be legal. This includes all of the dynamically sized types as legal index types. However, real or shortreal data types, or a type containing a real or shortreal, shall be an illegal index type.

— An index expression that is or contains X or Z in any of its elements is invalid.

— An index expression that is or contains an empty value or null for any of its elements does not make the index invalid.

— If the relational operator is defined for the index type, the ordering is as defined in the preceding clauses. If not, the relative ordering of any two entries in such an associative array can vary, even between successive runs of the same tool. However, the relative ordering shall remain the same within the same simulation run while no indices have been added or deleted.

7.8.6 Accessing invalid indices

If a read operation uses an index that is a 4-state expression with one or more x or z bits, or an attempt is made to read a nonexistent entry, then a warning shall be issued and the nonexistent entry value for the array type shall be returned, as shown in Table 7-1 (see 7.4.6). A user-specified default shall not issue a warning and returns the value specified in 7.9.11.

If an invalid index is used during a write operation, the write shall be ignored, and a warning shall be issued.

7.8.7 Allocating associative array elements

An entry for a nonexistent associative array element shall be allocated when it is used as the target of an assignment or actual to an argument passed by reference. Some constructs perform both a read and write operation as part of a single statement, such as with an increment operation. In those cases, the nonexistent element shall be allocated with its default or user-specified initial value before any reference to that element.

For example:

int a[int] = '{default:1};

typedef struct { int x=1,y=2; } xy_t;

xy_t b[int];

begin a[1]++;

b[2].x = 5;

end

Assume the references to a[1] and b[2] are nonexistent elements before these statements execute. Upon executing a[1]++, a[1] will be allocated and initialized to 1. After the increment, a[1] will be 2. Upon executing b[2].x = 5, b[2] will be allocated and b[2].x will be 1 and b[2].y will be 2. After executing the assignment, b[2].x will be updated to 5.