You're reading an pre-release version of this documentation.
For the latest stable release version, please have a look at master.

实用工具

一些实用工具也在 spinal.core

免状态工具

语法

返回类型

描述

toGray(x : UInt)

返回从 x (UInt)转换而来的灰度值

fromGray(x : Bits)

UInt

返回从 x (灰度)转换而来的UInt值

Reverse(x : T)

T

翻转所有位(lsb + n -> msb - n)

OHToUInt(x : Seq[Bool])
OHToUInt(x : BitVector)

UInt

返回 x 中唯一被设置(为1)的比特位的索引

CountOne(x : Seq[Bool])
CountOne(x : BitVector)

UInt

返回 x 中被设为1的位数

CountLeadingZeroes(x : Bits)

UInt

返回从最显著位 (MSB) 开始连续零位的个数

MajorityVote(x : Seq[Bool])
MajorityVote(x : BitVector)

Bool

如果设置为1的位数 > x.size / 2,则返回 True

EndiannessSwap(that: T[, base:BitCount])

T

大端 <-> 小端

OHMasking.first(x : Bits)

对x应用掩码以仅保留第一个设为1的位

OHMasking.last(x : Bits)

对x应用掩码以仅保留最后一个设为1的位

OHMasking.roundRobin(
requests : Bits,
ohPriority : Bits
)

对x应用掩码以仅保留 requests 中设置1的位。
它从 ohPriority 位置开始访问 requests
例如,如果 requests 为“1001”且 ohPriority 为“0010”,则 roundRobin 函数将从第二位开始访问 requests 并返回“1000”。
MuxOH (
oneHot : IndexedSeq[Bool],
inputs : Iterable[T]
)

T

根据 oneHot 向量从 inputs 中返回多路复用的 T

PriorityMux (
sel: Seq[Bool],
in: Seq[T]
)

T

返回第一个其 selTruein 元素。

PriorityMux (
in: Seq[(Bool, T)]
)

T

返回第一个其 selTruein 元素。

全状态工具

语法

返回类型

描述

Delay(that: T, cycleCount: Int)

T

返回延迟了 cycleCount 周期的 that

History (
that: T, length: Int
[, when : Bool][, init : T]
)

Vec[T]

返回长度为 length 的Vec
其第一个元素是 that ,最后一个元素是延迟了 length -1的 that
内部移位寄存器会在 when 有效时采样
History (
that: T, range: Range
[, when : Bool][, init : T]
)

Vec[T]

History(that, length) 相同
但返回长度为 range.length 的Vec
其中第一个元素延迟了 range.low 个周期
且最后一个是延迟了 range.high 个周期的元素

BufferCC(input : T)

T

返回利用两个触发器将输入信号同步到当前时钟域后的新信号

计数器

计数器工具可用于轻松实例化硬件计数器。

实例化语法

备注

Counter(start: BigInt, end: BigInt[, inc : Bool])

Counter(range : Range[, inc : Bool])

x to y x until y 语法兼容

Counter(stateCount: BigInt[, inc : Bool])

从 0 开始,到 stateCount - 1 结束

Counter(bitCount: BitCount[, inc : Bool])

从0开始到 (1 << bitCount) - 1 结束

Counter(time: TimeNumber[, inc : Bool])

Starts at zero and ends after time clocks

计数器可以通过方法控制,并且可以连线可以被读取:

val counter = Counter(2 to 9) // Creates a counter of 8 states (2 to 9)
// Methods
counter.clear()               // Resets the counter
counter.increment()           // Increments the counter
// Signals
counter.value                 // Current value
counter.valueNext             // Next value
counter.willOverflow          // True if the counter overflows this cycle
counter.willOverflowIfInc     // True if the counter would overflow this cycle if an increment was done
// Cast
when(counter === 5){ ... }    // counter is implicitly casted to its current value

Counter 溢出(达到最终值)时,它会重新启动下一个周期并设置为起始值。

备注

目前仅支持向上计数器。

CounterFreeRun 构建一个始终运行的计数器: CounterFreeRun(stateCount: BigInt)

超时

超时工具可用于方便地实例化一个硬件超时。

实例化语法

备注

Timeout(cycles : BigInt)

cycles 个时钟周期后触发超时(tick)

Timeout(time : TimeNumber)

在持续 time 时间后触发超时(tick)

Timeout(frequency : HertzNumber)

frequency 频率触发超时信号(tick)

下方是可以与Counter工具一起使用的不同语法句式的示例

val timeout = Timeout(10 ms)  // Timeout who tick after 10 ms
when(timeout) {               // Check if the timeout has tick
    timeout.clear()           // Ask the timeout to clear its flag
}

备注

如果您使用时间或频率设置实例化 Timeout ,则隐含地 ClockDomain 应该具有频率设置。

复位控制

复位控制(ResetCtrl)提供了一些实用工具来管理复位。

asyncAssertSyncDeassert

您可以使用“异步有效同步无效”的逻辑来构造异步复位。为此,可以使用 ResetCtrl.asyncAssertSyncDeassert 函数,该函数将返回过滤后的值。

参数名称

类型

描述

input

Bool

输入的异步信号,用于触发复位

clockDomain

ClockDomain

返回的新复位信号对齐的时钟域(ClockDomain)

inputPolarity

Polarity

HIGH/LOW (default=HIGH)

outputPolarity

Polarity

HIGH/LOW (default=clockDomain.config.resetActiveLevel)

bufferDepth

Int

防止亚稳态所需的寄存器级数(默认为2)

另外还有一个 ResetCtrl.asyncAssertSyncDeassertDrive 版本的工具,它直接使用新构造的复位信号作为 clockDomain 这个时钟域的复位。

Logic simplification utilities

DecodingSpec with Masked provide a minimal Boolean simplification and decode-table utility using the Quine–McCluskey algorithm.

Provides masked pattern matching, Quine–McCluskey style logic reduction, and a high-level decode-table builder.

Masked

Represents a bit pattern with care (significant) and don’t-care bits. - value = bit values - care = which bits must match (1 = match, 0 = don’t care)

Example:

Masked(0010),
Masked(11-1),
Masked(1--0)

e.g RISC-V instructions:

val ADD     = M"0000000----------000-----0110011"
val ADDI    = M"-----------------000-----0010011"

Used to define instruction encodings for decode tables.

DecodingSpec

High-level builder for decode tables using Masked patterns.

Methods:
  • addNeeds(key : Masked, value : Masked)

  • addNeeds(keys : Seq[Masked], value : Masked)

  • build(sel, coverAll)

  • setDefault(value : Masked)

This generate simplified decode logic.

Example:

val spec = new DecodingSpec(UInt(4 bits))
spec.setDefault(Masked(U"0011"))
spec.addNeeds(Masked(B"000"), Masked(U"1000"))
result := spec.build(sel, allPatterns)

Generates minimized combinational decode logic.

The practical use is to define bit patterns as Masked and feed them into DecodingSpec to build compact decode logic (e.g., RISC-V). The output hardware is then minimized (fewer LUTs / simpler gates).

特殊工具

语法

返回类型

描述

LatencyAnalysis(paths : Node*)

Int

以周期为单位返回经过所有节点的最短路径,
从第一个节点到最后一个节点