int dyn [], d2[]; //声明动态数组
initial begin
dyn=new[5]; //分配5个元素
foreach (dyn[j]) dyn[j] =j; //对元素进行初始化
d2=dyn; //复制一个动态数组
d2[0]=5; //修改复制值
$display (dyn[0],d2[0]); //显示数值0和5
dyn=new[20] (dyn); //分配20个整数值并进行复制
dyn=new[100] ; //分配100个新的整数值 旧值不再存在
dyn.delete(); //删除所有元素
end
bit [7:0] mask[]=’{8’b0000_0000, 8’b0000_0001, 8‘b0000_0011,8’b0000_0111,
8’b0000_1111, 8’b0001_1111, 8’b0011_1111, 8’b0111_1111, 8’b1111_1111};
int j=1,
q2[$] ={3,4}, //队列常量不需要用’
q[$]={0,2,3}; //{0,2,3}
initial begin
q.insert(2,j); //{0,1,2,3}在第二元素之前插入j
q.delete(2); //{0,2,3}删除第二个元素
q.push_front(6); //{6,0,2,3}在队列前面插入6
j=q.pop_back; //{6,0,2} j=3 移除最后一位
q.push_back(8); //{6,0,2,8}在队列末尾插入8
j=q.pop_front; //{0,2,8} j=6 移除第一位
foreach (q[i])
$display(q[i]); //打印整个队列
q.delete(); //删除队列
end
int j=1,
q2[$] ={3,4}, //队列常量不需要用’
q[$]={0,2,5}; //{0,2,5}
initial begin
q={q[0],j,q[1:$]}; //{0,1,2,5}在2之前插入1
q={q[0:2],q2,q[3:$]}; //{0,1,2,3,4,5}在q中插入一个队列
q={q[0],q[2:$]}; //{0,2,3,4,5} 删掉第二个元素
q={6,q} ; //{6, 0,2,3,4,5}在队列前面插入
j=q[$]; //等同于j=5
q=q[0:$-1]; //{6, 0,2,3,4} 在队列末尾移除数据
q={q,8}; //{6, 0,2,3,4,8}末尾插入8
j=q[0]; //等同于j=6
q=q[1:$]; //{0,2,3,4,8} 移除第一个数
q={}; //删除队列
end
关联数组声明、初始化、使用:
bit[63:0] assoc[byte] , idx=1;
initial begin
//对稀疏分布的元素进行初始化
repeat(64) begin
assoc[idx]=idx;
idx=idx<<1;
end
//使用foreach遍历数组
foreach (assoc[i])
$display("assoc[%h]=%h",i,assoc[i]);
//使用函数遍历数组
if (assoc.first(idx))begin
do
$display("assoc[%h]=%h",idx,assoc[idx]");
while(assoc.next(idx)); //得到下一个索引
end
//找到并删除第一个元素
assoc.first(idx);
assoc.delete(idx);
$display("the array now has %0d elements",assoc.num);
end
tybe b[$] ={2,3,4,5};
int w;
w=b.sum(); //14=2+3+4+5
w=b.product(); //120=2*3*4*5
w=b.and(); //0000_0000= 2 & 3 & 4 & 5
数组定位方法:max、min、unique(独一无而的)
int f[6]='{1,6,2,6,8,6}; //定长数组
int d[]='{2,4,6,8,10}; //动态数组
int q[$]={1,3,5,7}; //队列
tp[$]; //保存结果的临时队列
tp=q.min(); //{1}
tp=d.max(); //{10}
tp=f.unique(); //{1,6,2,8}
//find with定位
int d[]='{9,1,8,3,4,4},tp[$];
//找出所有大于3的元素
tp=d.find with(item>3); //{9,8,4,4}
//foreach等效代码
tq.deldte();
foreach(d[i])
if(d[i]>3)
tp.push_back([i]);
//其他定位方法
tq=d.find_index with (item>3) ;
tq=d.find_first with (item>99) ; //{} 没有找到
tq=d.find_last with (item==4) ; // {4}
tq=d.find_first_index with (item==8) ; //{2} d[2]=8
tq=d.find_last_index with (item==4) ; //{5} d[5]=4
int d[]= '{9,1,8,3,4,4};
d.revrse(); //逆排序'{4.4.3.8.1.9};
d.sort(); //从小到大' {1,3,4,4,8,9};
d.rsort(); //从大到小 '{9,8,4,4,3,1};
d.shuffle(); //乱序排列
END