3.1 Chisel Standard Library
Chisel cheatsheet
Decoupled
DecoupledIO
provides a ready-valid interface for transferring data. Any Chisel data can be wrapped in a DecoupledIO
(used as the bits
field).
(master)The source drives the
bits
signal with the data to be transferred and thevalid
signal when there is data to be transferred.
(slave)The sink drives theready
signal when it is ready to accept data, and data is considered transferred when bothready
andvalid
are asserted on a cycle.ready
andvalid
should not be combinationally coupled, otherwise this may result in unsynthesizable combinational loops.ready
should only be dependent on whether the sink is able to receive data, andvalid
should only be dependent on whether the source has data. Only after the transaction (on the next clock cycle) should the values update.Output:
Decoupled(UInt(8.W))
Bundle with fields:1
2
3
4
5val out = new Bundle {
val valid = Output(Bool())
val ready = Input(Bool())
val bits = Output(UInt(8.W))
}Input:
Flipped(Decoupled(UInt(8.W)))
Bundle with fields:1
2
3
4
5val in = new Bundle {
val valid = Input(Bool())
val ready = Output(Bool())
val bits = Input(UInt(8.W))
}
Arbiter
- Arbiters routes data from n
DecoupledIO
sources to oneDecoupledIO
sink, given a prioritization. There are two types included in Chisel:Arbiter
: prioritizes lower-index producersRRArbiter
: runs in round-robin order
- Arbiter routing is implemented in combinational logic.
1 | class Mod extends Module { |
Queue
Queue
creates a FIFO (first-in, first-out) queue with Decoupled interfaces on both sides, allowing backpressure. Both the data type and number of elements are configurable.
1 | class Queue extends Module { |
Bitwise Utilities
PopCount
PopCount returns the number of high (1) bits in the input as a UInt
.
1 | test(new Module { |
Reverse
Reverse returns the bit-reversed sequence input. NOT ~
1 | test(new Module { |
OneHot encoding utilities
OneHot is an encoding of integers where there is one wire for each value, and exactly one wire is high.
The below two functions provide conversion between binary (UInt
) and OneHot encodings, and are inverses of each other:
- UInt to OneHot:
UIntToOH
- OneHot to UInt:
OHToUInt
1 | // UIntToOH |
Mux
- These muxes take in a list of values with select signals, and output the value associated with the lowest-index select signal.
- These can either take a list of (select: Bool, value: Data) tuples, or corresponding lists of selects and values as arguments.
Priority Mux
A PriorityMux
outputs the value associated with the lowest-index asserted select signal.
1 | PriorityMux(io.in_sels, io.in_bits) |
1 | test(new Module { |
OneHot Mux
An Mux1H
provides an efficient implementation when it is guaranteed that exactly one of the select signals will be high. Behavior is undefined if the assumption is not true.
1 | test(new Module { |
Counter
Counter
is a counter that can be incremented once every cycle, up to some specified limit, at which point it overflows. Note that it is not a Module, and its value is accessible.
1 | class Cnt extends Module { |