system verilog关联数组的使用方法

在验证环境中,我们可能会对一个比较大的地址范围的一小部分进行访问. 如果用动态数组来实现数据的存储和初始化显然是浪费的.
system verilog提供了关联数组,用来保存稀疏矩阵元素.它只针对写入的元素分配地址空间.关联数组的具体使用方法如下:
1.声明
通过方括号中放置数据类型实现 int data_as[bit [31:0]] : int为存储数据类型,bit[63:0]为寻址的类型.
2.初始化
关联数组初始化时使用 : '{};在大括号内写入键值对的信息,键值对用:连接;
data_as = '{1:20,2:21,3:22};
3.关联数组的大小
可以使用size()、num()来实现;没有写入数据时,大小为0
4.使用foreach遍历
foreach(data_as[i])begin
$display(“data_as[0x%0h] is 0x%0h”,data_index,data_as[data_index]);
end
5.使用first、next遍历
first返回最小的索引值;next返回比给定的索引值大的最小索引值; 二者配合使用实现关联数组的遍历;
data_as = '{1:20,2:21,3:22};
if(data_as.first(data_index))begin
do
begin $display(“data_as[0x%0h] is 0x%0h”,data_index,data_as[data_index]);
end
while(data_as.next(data_index));
end
data_as[0x0] is 0x14
data_as[0x1] is 0x15
data_as[0x2] is 0x16
6.使用last、prev遍历
last返回最大的索引值;prev返回比给定的索引值小的最大索引值; 二者配合使用实现关联数组的遍历;
data_as = '{1:20,2:21,3:22};
if(data_as.last(data_index))begin
do
begin $display(“data_as[0x%0h] is 0x%0h”,data_index,data_as[data_index]);
end
while(data_as.prev(data_index));
end
data_as[0x2] is 0x16
data_as[0x1] is 0x15
data_as[0x0] is 0x14
7.delete函数
在使用delete函数时,如果里面给了索引值,如果索引值存在,那么将相应的键值对删除;否则,什么都不做.如果没有给索引值,那么将整个关联数组删除.
data_as = '{1:20,2:21,3:22};
data_as.delete(100) //什么都不做
data_as.delete(1) //删除1:20键值对
8.exists函数
exists函数用来判断对应的索引值是否存在,存在则返回1,不存在则返回0
data_as = '{1:20,2:21,3:22};
data_as.exists(1) //返回1
data_as.exists(100) //返回0
9.实例
在这里插入图片描述
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值