【SpinalHDL快速入门】4.6、复合类型之Vec

文章介绍了SpinalHDL中的Vec类型,包括如何声明和使用Vec来存储不同类型的信号,以及支持的比较运算符、类型转换、杂项操作和库辅助函数,如sCount、sExists等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

1.1、描述

Vec是一种复合类型,它在一个单一的名称下定义了一组索引信号(任何SpinalHDL基本类型)。

1.2、声明

声明向量的语法如下:

在这里插入图片描述

1.2.1、实例

// Create a vector of 2 signed integers
val myVecOfSInt = Vec(SInt(8 bits), 2)
myVecOfSInt(0) := 2
myVecOfSInt(1) := myVecOfSInt(0) + 3

// Create a vector of 3 different type elements
val myVecOfMixedUInt = Vec(UInt(3 bits), UInt(5 bits), UInt(8 bits))

val x, y, z = UInt(8 bits)
val myVecOf_xyz_ref = Vec(x, y, z)

// Iterate on a vector
for(element <- myVecOf_xyz_ref) {
	element := 0 // Assign x, y, z with the value 0
}

// Map on vector
myVecOfMixedUInt.map(_ := 0) // Assign all elements with value 0

// Assign 3 to the first element of the vector
myVecOf_xyz_ref(1) := 3

1.3、运算符

Vec类型支持以下运算符:

1.3.1、比较(Comparison)

在这里插入图片描述

// Create a vector of 2 signed integers
val vec2 = Vec(SInt(8 bits), 2)
val vec1 = Vec(SInt(8 bits), 2)

myBool := vec2 === vec1 // Compare all elements

1.3.2、类型转换(Type cast)

在这里插入图片描述

// Create a vector of 2 signed integers
val vec1 = Vec(SInt(8 bits), 2)

myBits_16bits := vec1.asBits

1.3.3、杂项(Misc)

在这里插入图片描述

// Create a vector of 2 signed integers
val vec1 = Vec(SInt(8 bits), 2)

println(vec1.getBitsWidth) // 16

1.3.4、Lib辅助函数(Lib helper functions)

注意:您需要导入 “spinal.lib._” 以将这些函数放在作用域中。

在这里插入图片描述

import spinal.lib._
// Create a vector with 4 unsigned integers
val vec1 = Vec(UInt(8 bits), 4)

// ... the vector is actually assigned somewhere

val c1: UInt = vec1.sCount(_ < 128) // vec中有多少个值小于128
val c2: UInt = vec1.sCount(0) // vec中有多少个值等于零
val b1: Bool = vec1.sExists(_ > 250) // 有没有大于250的元素
val b2: Bool = vec1.sContains(0) // vec里有零吗?
val u1: UInt = vec1.sFindFirst(_ < 10) // 获取第一个小于10的元素的索引
val u2: UInt = vec1.reduceBalancedTree(_ + _) // 将所有元素相加。

注意:sXXX前缀用于消除与接受lambda函数作为参数的同名Scala函数的歧义

警告:SpinalHDL的定点支持仅部分使用/测试,如果您发现任何错误或认为某些功能缺失,请创建Github问题。此外,请勿在代码中使用未记录的功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReCclay

如果觉得不错,不妨请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值