SV数组的方法——缩减、定位、排序

数组缩减方法

对数组缩减的时候需要注意位宽
缺省情况下,如果把单比特数组的所有元素相加,和也是单比特;
如果使用32比特的表达式,把结果保存在32比特变量里,与一个32b比特的变量进行比较,或者使用适当的with表达式,SV都会在数组求和的过程中使用32bit位宽

绿皮书代码打印结构是{1, 1, 1, 5},单比特相加的结果还是单比特,使用with,SV会使用32bit

// array_sum

// Description: 数组缩减方法 求和

module array_sum ;
	bit	on[10];		// 单比特数组
	int	total;		// 32bit整型
	
	initial	begin
		foreach (on[i])	
			on[i] = i;		// on[i]的值为0或1	0101_0101_01
		
		// 打印出单比特和
		$display ("on.sum = %0d", on.sum);			// on.sum = 1
		
		// 打印出32比特和
		$display ("on.sum = %0d", on.sum+32'd0);	// on.sum = 5		实际打印1
		
		// total是32比特整型
		total = on.sum+32'd0;
		$display ("total = %0d", total);			// total = 5		实际打印1
		
		// 将数组和与一个32比特数进行比较
		if (on.sum >= 32'd5)
			$display ("sum has 5 or more 1's");
			
		// 使用带32比特有符号运算的with表达式
		$display ("int sum = %0d", on.sum with (int'(item)));
	end
endmodule

在这里插入图片描述
其他的缩减方法还有product(积)、and(与)、or(或)、xor(异或)

数组定位方法:min、max、unique

min和max可以返回数组中的最小值和最大值,返回的是一个队列而非标量,这些方法也适用于关联数组。方法unique返回的是数组中具有唯一值的队列,即排除掉重复的数值,顺带排序;

module min_max_unique ;
	int	f[6] = '{1, 6, 8, 6, 2, 6};	// 定宽数组
	int d[ ] = '{2, 4, 6, 8, 10};	// 动态数组
	int q[$] = {1, 3, 5, 7};		// 队列
	
	int tq[$];
	
	initial	begin
		tq = q.min();
		foreach (tq[i])
			$display ("%0d", tq[i]);		// {1}
		tq = d.max();
		foreach (tq[i])
			$display ("%0d", tq[i]);		// {10}
		tq = f.unique();
		foreach (tq[i])
			$display ("%0d", tq[i]);		// {1, 2, 6, 8}
	end

endmodule

在这里插入图片描述

数组定位方法:find

表达式with可以指示SV进行搜索,

module find ;
	int d[ ] = '{9, 1, 8, 3, 4, 4};	// 动态数组
	int tq[$];						// 队列
	
	initial	begin
		// 找出所有大于3的元素
		tq = d.find with (item > 3);	// {9, 8, 4, 4};
		foreach (tq[i])	$display ("%0d", tq[i]);
		
		// 等效代码
		tq.delete();
		foreach (d[i])
			if (d[i] > 3)
				tq.push_back(d[i]);		// {9, 8, 4, 4};
		foreach (tq[i])	$display ("%0d", tq[i]);
				
		tq = d.find_index with (item > 3);			// {0, 2, 4, 5}
		$display ("find_index:");
		foreach (tq[i])	$display ("%0d", tq[i]);
		
		tq = d.find_first with (item > 99);			// {}
		$display ("find_first:");
		foreach (tq[i])	$display ("%0d", tq[i]);
		
		tq = d.find_first_index with (item == 8);	// {2}	d[2] = 8
		$display ("find_first_index:");
		foreach (tq[i])	$display ("%0d", tq[i]);
		
		tq = d.find_last with (item == 4);			// {4}
		$display ("find_last:");
		foreach (tq[i])	$display ("%0d", tq[i]);
		
		tq = d.find_last_index with (item == 4);	// {5}	d[5] = 4
		$display ("find_last_index:");
		foreach (tq[i])	$display ("%0d", tq[i]);
	end

endmodule

在这里插入图片描述
在条件语句with中,item被称为重复参数,它代表了数组中一个单独的元素。item是缺省的名字,也可以指定别的名字,只要在数组方法的参数列表中列出来就可以,如下,

tq = d.find_first with (item == 4);
tq = d.find_first() with (item == 4);
tq = d.find_first(item) with (item == 4);
tq = d.find_first(x) with (x == 4);

数组定位方法:sum

// add_with_sum

module add_with_sum ;
	int	count;		// 对应元素求和
	int	total;		// 对应元素个数
	int	d[ ] = '{9, 1, 8, 3, 4, 4};	// 动态数组
	
	initial	begin
		// 先将对应元素值与7进行比较,比较表达式返回1或0
		count = d.sum with (item > 7);				// {1, 0, 1, 0, 0, 0}
		$display ("count = %0d", count);
		
		// 得到比较表达式的结果,然后再把返回结果与对应元素相乘再求和
		total = d.sum with ((item > 7) * item);		// {9, 0, 8, 0, 0, 0}
		$display ("total = %0d", total);
		
		count = d.sum with (item < 8 ? 1 : 0);		// {0, 1, 0, 1, 1, 1}
		$display ("count = %0d", count);
		
		total = d.sum with (item < 8 ? item : 0);	// {0, 1, 0, 3, 4, 4}
		$display ("total = %0d", total);
		
		count = d.sum with ((item == 4) * 1);		// {0, 0, 0, 0, 1, 1}
		$display ("count = %0d", count);
	end

endmodule

当想直接用比较表达式的返回值1或0的时候,需要手动 *1 或者添加三目运算符才行 ?1:0

在这里插入图片描述

数组排序方法

方法说明
reverse反序
sort升序
rsort降序
shuffle乱序
// array_sorting

module array_sorting ;
	int d[ ] = '{9, 1, 8, 3, 4, 4};
	
	initial	begin
		d.reverse();	// '{4, 4, 3, 8, 1, 9}
		$display ("reverse: %p", d);
		d.sort();		// '{1, 3, 4, 4, 8, 9}
		$display ("sort: %p", d);
		d.rsort();		// '{9, 8, 4, 4, 3, 1}
		$display ("rsort: %p", d);
		d.shuffle();	// 
		$display ("shuffle: %p", d);
	end

endmodule

在这里插入图片描述
注意:
reverse和shuffle方法不能带with条件语句,它作用范围是整个数组;
sort和rsort方法可以带with条件语句,可以进行范围内排序;
数组定位方法是新建了一个队列来保存结果,排序方法是改变了原数组;

使用数组定位方法建立记分板

数组定位方法可以用来建立记分板;

	typedef struct packed {
		bit	[ 7: 0]	addr;
		bit	[ 7: 0]	pr	;
		bit	[15: 0]	data;
	} Packet;
	
	Packet	scb[$];	// 队列
	
	function void check_addr (addr);
		int intq[$];
		
		intq = scb.find_index() with (item.addr == addr);
		case (intq.size())
			0: $display ("Addr %h not fount in scoreboard", addr);
			1: scb.delete(intq[0]);
			default: $display ("ERROR: Multiple hits for addr %h", addr);
		endcase
	endfunction

check_addr( )函数在记分板里寻找和参数匹配的地址,find_index( )方法返回一个int队列。如果该队列为空(size == 0),则说明没有匹配值。如果该队列有一个成员(size == 1),则说明有一个匹配,该元素随后被check_addr( )函数删除。如果该队列有多个成员(size > 1),则说明记分板里有多个包地址和目标值匹配;

  • 4
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值