Vec

描述

Vec 是一种复合类型,它在一个变量中定义一组可索引的信号(任何 SpinalHDL 基本类型)。

声明

声明向量的语法如下:

声明

描述

Vec.fill(size: Int)(type: Data)

创建一个包含 size 个元素的 Data 类型向量

Vec(x, y, …)

创建一个向量,其中索引指向提供的元素。
不会创建新的硬件信号。
此构造函数支持混合宽度的元素。

示例

// Create a vector of 2 signed integers
val myVecOfSInt = Vec.fill(2)(SInt(8 bits))
myVecOfSInt(0) := 2                   // assignment to populate index 0
myVecOfSInt(1) := myVecOfSInt(0) + 3  // assignment to populate index 1

// 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

运算符

以下运算符可用于 Vec 类型:

比较运算

运算符

描述

返回类型

x === y

等价性判断

Bool

x =/= y

不等价判断运算

Bool

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

myBool := vec2 === vec1  // Compare all elements
// is equivalent to:
//myBool := vec2(0) === vec1(0) && vec2(1) === vec1(1)

类型转换

运算符

描述

返回类型

x.asBits

二进制转换为 Bits

Bits(w(x) bits)

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

myBits_16bits := vec1.asBits

杂项

运算符

描述

返回类型

x.getBitsWidth

返回 Vec 的完整大小

Int

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

println(widthOf(vec1)) // 16

库辅助函数

备注

您需要以 import spinal.lib._ 导入库,以将这些函数置于作用域中。

运算符

描述

返回类型

x.sCount(condition: T => Bool)

计算 Vec 中与给定条件匹配的次数。

UInt

x.sCount(value: T)

计算 Vec 中某个值出现的次数。

UInt

x.sExists(condition: T => Bool)

检查Vec中是否存在匹配条件的元素。

Bool

x.sContains(value: T)

检查 Vec 中是否存在具有给定值的元素。

Bool

x.sFindFirst(condition: T => Bool)

查找 Vec 中符合给定条件的第一个元素,如果成功找到,则返回该元素的索引。

(Bool, UInt)

x.reduceBalancedTree(op: (T, T) => T)

具有自动平衡功能的reduce函数,尽量减少生成电路的深度。 op 应该是具有可交换性和可结合性的。

T

x.shuffle(indexMapping: Int => Int)

使用将旧索引映射到新索引的函数对 Vec 进行混洗(shuffle)。

Vec[T]

import spinal.lib._

// Create a vector with 4 unsigned integers
val vec1 = Vec.fill(4)(UInt(8 bits))

// ... the vector is actually assigned somewhere

val c1: UInt = vec1.sCount(_ < 128) // how many values are lower than 128 in vec
val c2: UInt = vec1.sCount(0) // how many values are equal to zero in vec

val b1: Bool = vec1.sExists(_ > 250) // is there a element bigger than 250
val b2: Bool = vec1.sContains(0) // is there a zero in vec

val (u1Found, u1): (Bool, UInt) = vec1.sFindFirst(_ < 10) // get the index of the first element lower than 10
val u2: UInt = vec1.reduceBalancedTree(_ + _) // sum all elements together

备注

sXXX 前缀用于消除使用 lambda 函数作为参数的同名 Scala 函数带来的歧义。