标签:递归 for var foreach iat event eth sys 写法
整理下system verilog中数组的用法,备忘。
int array1 [6]; //fixed size single dimension array
int array2 [5:0]; //fixed size single dimension array
int array3 [3:0][2:0]; //fixed size multi dimension array
bit array4[2:0]; //unpacked array declaration
bit [2:0][7:0] array5; //packed array declaration
bit [2:0][7:0] array6 [3]; //mixed packed and unpacked array
array赋初值:
array1 = ‘{0,1,2,3,4,5};
array2 = ‘{0,1,2,3,4,5};
array3 = ‘{ ‘{0,1,2,3},‘{4,5,6,7},‘{8,9,10,11}};
bit [2:0] [7:0] array5;
例如:
bit [7:0] array4[2:0];
在存储时:
定义:data_type array_name [ ];
function:
例子:
//declaration
bit [7:0] d_array1[ ];
int d_array2[ ];
//memory allocation
d_array1 = new[4]; //dynamic array of 4 elements
d_array2 = new[6]; //dynamic array of 6 elements
//array initialization
d_array1 = {0,1,2,3};
foreach(d_array2[j]) d_array2[j] = j;
主要有两种方法:
d_array1 = new[10];
d_array1 = new[10](d_array2);
对于第一种,会将数组清空,然后重新分配为大小为10的数组。
对于第二种,会保留原来的4个数据,然后分配新的6个元素的空间。
删除动态数组:d_array1.delete;
,没有括号。
Example:
module dynamic_array;
//dynamic array declaration
bit [7:0] d_array1[];
int d_array2[];
initial begin
//memory allocation
d_array1 = new[2];
d_array2 = new[3];
//array initialization
d_array1 = {2,3};
foreach(d_array2[j]) d_array2[j] = j;
$display("----- d_array1 Values are -----");
foreach(d_array1[i]) $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);
$display("----- d_array2 Values are -----");
foreach(d_array2[i]) $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);
//Increasing the size by overriding the old values
d_array1 = new[4]; //Create dynamic array of 4 elements
$display("Size of Array d_array1 %0d",d_array1.size());
$display("----- d_array1 Values are -----");
foreach(d_array1[i]) $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);
//Increasing the size by retaining the old values
d_array2 = new[5](d_array2); //Create dynamic array of 5 elements, retaining old values
$display("Size of Array d_array2 %0d",d_array2.size());
$display("----- d_array2 Values are -----");
foreach(d_array2[i]) $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);
$display("---------------------------------");
//delete array
d_array1.delete;
d_array2.delete;
$display("After Array Delete");
$display("\tSize of d_array1 %0d",d_array1.size());
$display("\tSize of d_array2 %0d",d_array2.size());
end
endmodule
仿真结果:
----- d_array1 Values are -----
d_aaray1[0] = 2
d_aaray1[1] = 3
----- d_array2 Values are -----
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
Size of Array d_array1 4
----- d_array1 Values are -----
d_aaray1[0] = 0
d_aaray1[1] = 0
d_aaray1[2] = 0
d_aaray1[3] = 0
Size of Array d_array2 5
----- d_array2 Values are -----
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
d_aaray2[3] = 0
d_aaray2[4] = 0
---------------------------------
After Array Delete
Size of d_array1 0
Size of d_array2 0
语法:data_type array_name [ index_type ];
例如:
int a_array1[*] ; // associative array of integer (unspecified index)
bit [31:0] a_array2[string]; // associative array of 32-bit, indexed by string
ev_array [myClass]; //associative array of event,indexed by class
Methods:
Method | Description |
---|---|
.num() | 返回元素个数 |
.delete(index) | 删除index索引的键和值,如果不加index则删除所有键和值 |
.exists(index) | 检查index键是否在数组中 |
.first(var) | 将第一个键赋给var |
.last(var) | 将最后一个键赋给var |
.next(var) | 将下一个键赋给var,如果后面没有键,则返回最后一个键 |
.prev(var) | 将上一个键赋给var,如果前面没有键,则返回第一个键 |
module associative_array;
//array declaration
int a_array[*];
int index;
initial begin
//allocating array and assigning value to it
repeat(3) begin
a_array[index] = index*2;
index=index+4;
end
//prev()-Associative array method
a_array.prev(index);
$display("entry is a_array[%0d] = %0d",index,a_array[index]);
//next()-Associative array method
a_array.next(index);
$display("entry is a_array[%0d] = %0d",index,a_array[index]);
a_array.next(index);
$display("entry is a_array[%0d] = %0d",index,a_array[index]);
a_array.prev(index);
$display("entry is a_array[%0d] = %0d",index,a_array[index]);
end
endmodule
结果:
entry is a_array[0] = 0
entry is a_array[4] = 8
entry is a_array[8] = 16
entry is a_array[4] = 8
使用与unpacked数组相同的语法声明,但是在定义时使用$符号作为数组大小。在队列中,0表示第一个条目,$表示最后一个条目。
写法:data_type queue_name[$];
分为两种:第一种是bounded queue,定义了上界的queue;而第二种是unbounded,没有定义上界的。例如:
bit queue_1[$]; // queue of bits (unbound queue)
int queue_2[$]; // queue of int
byte queue_3[$:255]; // queue of byte (bounded queue with 256 entries)
string queue_4[$]; // queue of strings
初始化方法:queue = {7,3,1,0,8};
Methods:
其中insert(index,var),将var插入到index这个位置,后面的往后挪。
push和pop的几个函数的功能如下:
此外,对于定容的队列,push_back将不起作用,而push_front则会将第一个值替换。
标签:递归 for var foreach iat event eth sys 写法
原文地址:https://www.cnblogs.com/lyc-seu/p/12725896.html