[SV] Arrays
- Packed / Unpacked
Packed : vector를 access가 용이하도록 subfield로 나눈 구조
dimension declared before the data identifier name => packed : contiguous
dimension declared after the data identifier name => unpacked : non-contiguous
ex) bit [2:0] [7:0] array1; ==> packed
bit [2:0] array2 [7:0]; ==> unpacked
- Dynamic
data_type array_name [];
- methods
new[] => allocates the storage
size() => returns the current size of a dynamic array
delete() => empties the array, resulting in a zero-sized array.
ex)
bit [7:0] dynamic_array[];
dynamic_array = new[4]; // 4 elements
dynamic_array = {0, 1, 2, 3]; // initialization
dynamic_array = new[10]; // increasing size
dynamic_array = new[10](dynamic_array); // retaining old values
dynamic_array.delete; // delete
- Associative
data_type array_name [index_type];
- methods
num() => returns the number of entries in the associative array
delete(index) => removes the entry at the specified index.exa_array.delete(index)
exists(index) => returns 1 if an element exists at the specified index else returns 0
first(var) => assigns the value of first index to the variable var
last(var) => assigns the value of last index to the variable var
next(var) => assigns the value of next index to the variable var
prev(var) => assigns the value of previous index to the variable var
ex)
int associative_array[*];
int index;
repeat(3)
associative_array[index] = index * 2;
index = index + 4;
associative_array.num() => 3
associative_array.first(index); => associative_array[0]
associative_array.last(index); => associative_array[8]
if(associative_array.exists(8)) => return 1
associative_array.prev(index); => associative_array[4]
associative_array.next(index); => associative_array[8]
- Queue
data_type queue_name[$];
- methods
size() => returns the number of items in the queue
insert() => inserts the given item at the specified index position
delete() => deletes the item at the specified index position
push_front() => inserts the given element at the front of the queue
push_back() => inserts the given element at the end of the queue
pop_front() => removes and returns the first elements of the queue
pop_back() => removes and returns the last elements of the queue
ex)
bit [31:0] queue1[$]; // unbounded queue
string queue2[$];
queue1 = {0, 1, 2, 3};
queue2 = {red, blue, green};
queue2.insert(2, yellow); // {red, blue, yellow, green}
queue1.push_front(10);
queue1.push_back(20);
// queue1 = {10, 0, 1, 2, 3, 20}
queue1.pop_front();
queue1.pop_back();
// queue1 = {0, 1, 2, 3}
int queue3[$:2]; // 3 element
queue3 = { 1, 2, 3 };
queue3.push_back(10); // {1, 2, 3} new element discard
queue3.push_front(10); // {10, 1 ,2} last element pushed out
- Array operation and methods
- foreach
ex)
string words [2] = '{ "red" , "blue" };
foreach ( words[i] )
$display( i, words[i]); // [1, red] [2, blue]
- locator methods
array.find() with (expression) => returns all the elements satisfying the given expression
array.find_index() with (expression) => returns the indices of all the elements satisfying the given expression
array.find_first() with (expression) => returns the first element satisfying the given expression
array.find_first_index() with (expression) => returns the index of the first element satisfying the given expression
array.find_last() with (expression) => returns the last element satisfying the given expression
array.find_last_index() with (expression) => returns the index of the last element satisfying the given expression
- ordering methods
array.reverse() => reverses the order of the elements in the array
array.sort() [with (expression)] => sorts the array in ascending order [with] can be used with expression
array.rsort() [with (expression)] => sorts the array in descending order [with] can be used with expression
array.shuffle() => randomizes the order of the elements in the array
- reduction methods
array.sum() => returns the sum of all the array elements
array.product() => returns the product of all the array elements
array.and() => returns the bitwise AND ( & ) of all the array elements
array.or() => returns the bitwise OR ( | ) of all the array elements
array.xor() => returns the bitwise XOR ( ^ ) of all the array elements