1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
// See LICENSE for license details.
/** Wrappers for ready-valid (Decoupled) interfaces and associated circuit generators using them.
*/
package chisel3.util
import chisel3._
import chisel3.experimental.{DataMirror, Direction, requireIsChiselType}
import chisel3.internal.naming._ // can't use chisel3_ version because of compile order
/** An I/O Bundle containing 'valid' and 'ready' signals that handshake
* the transfer of data stored in the 'bits' subfield.
* The base protocol implied by the directionality is that
* the producer uses the interface as-is (outputs bits)
* while the consumer uses the flipped interface (inputs bits).
* The actual semantics of ready/valid are enforced via the use of concrete subclasses.
* @param gen the type of data to be wrapped in Ready/Valid
*/
abstract class ReadyValidIO[+T <: Data](gen: T) extends Bundle
{
// Compatibility hack for rocket-chip
private val genType = (DataMirror.internal.isSynthesizable(gen), chisel3.internal.Builder.currentModule) match {
case (true, Some(module: chisel3.core.MultiIOModule))
if !module.compileOptions.declaredTypeMustBeUnbound => chiselTypeOf(gen)
case _ => gen
}
val ready = Input(Bool())
val valid = Output(Bool())
val bits = Output(genType)
}
object ReadyValidIO {
implicit class AddMethodsToReadyValid[T<:Data](target: ReadyValidIO[T]) {
def fire(): Bool = target.ready && target.valid
/** Push dat onto the output bits of this interface to let the consumer know it has happened.
* @param dat the values to assign to bits.
* @return dat.
*/
def enq(dat: T): T = {
target.valid := true.B
target.bits := dat
dat
}
/** Indicate no enqueue occurs. Valid is set to false, and bits are
* connected to an uninitialized wire.
*/
def noenq(): Unit = {
target.valid := false.B
target.bits := DontCare
}
/** Assert ready on this port and return the associated data bits.
* This is typically used when valid has been asserted by the producer side.
* @return The data bits.
*/
def deq(): T = {
target.ready := true.B
target.bits
}
/** Indicate no dequeue occurs. Ready is set to false.
*/
def nodeq(): Unit = {
target.ready := false.B
}
}
}
/** A concrete subclass of ReadyValidIO signaling that the user expects a
* "decoupled" interface: 'valid' indicates that the producer has
* put valid data in 'bits', and 'ready' indicates that the consumer is ready
* to accept the data this cycle. No requirements are placed on the signaling
* of ready or valid.
* @param gen the type of data to be wrapped in DecoupledIO
*/
class DecoupledIO[+T <: Data](gen: T) extends ReadyValidIO[T](gen)
{
override def cloneType: this.type = new DecoupledIO(gen).asInstanceOf[this.type]
}
/** This factory adds a decoupled handshaking protocol to a data bundle. */
object Decoupled
{
/** Wraps some Data with a DecoupledIO interface. */
def apply[T <: Data](gen: T): DecoupledIO[T] = new DecoupledIO(gen)
/** Downconverts an IrrevocableIO output to a DecoupledIO, dropping guarantees of irrevocability.
*
* @note unsafe (and will error) on the producer (input) side of an IrrevocableIO
*/
@chiselName
def apply[T <: Data](irr: IrrevocableIO[T]): DecoupledIO[T] = {
require(DataMirror.directionOf(irr.bits) == Direction.Output, "Only safe to cast produced Irrevocable bits to Decoupled.") // scalastyle:ignore line.size.limit
val d = Wire(new DecoupledIO(irr.bits))
d.bits := irr.bits
d.valid := irr.valid
irr.ready := d.ready
d
}
}
/** A concrete subclass of ReadyValidIO that promises to not change
* the value of 'bits' after a cycle where 'valid' is high and 'ready' is low.
* Additionally, once 'valid' is raised it will never be lowered until after
* 'ready' has also been raised.
* @param gen the type of data to be wrapped in IrrevocableIO
*/
class IrrevocableIO[+T <: Data](gen: T) extends ReadyValidIO[T](gen)
{
override def cloneType: this.type = new IrrevocableIO(gen).asInstanceOf[this.type]
}
/** Factory adds an irrevocable handshaking protocol to a data bundle. */
object Irrevocable
{
def apply[T <: Data](gen: T): IrrevocableIO[T] = new IrrevocableIO(gen)
/** Upconverts a DecoupledIO input to an IrrevocableIO, allowing an IrrevocableIO to be used
* where a DecoupledIO is expected.
*
* @note unsafe (and will error) on the consumer (output) side of an DecoupledIO
*/
def apply[T <: Data](dec: DecoupledIO[T]): IrrevocableIO[T] = {
require(DataMirror.directionOf(dec.bits) == Direction.Input, "Only safe to cast consumed Decoupled bits to Irrevocable.") // scalastyle:ignore line.size.limit
val i = Wire(new IrrevocableIO(dec.bits))
dec.bits := i.bits
dec.valid := i.valid
i.ready := dec.ready
i
}
}
/** Producer - drives (outputs) valid and bits, inputs ready.
* @param gen The type of data to enqueue
*/
object EnqIO {
def apply[T<:Data](gen: T): DecoupledIO[T] = Decoupled(gen)
}
/** Consumer - drives (outputs) ready, inputs valid and bits.
* @param gen The type of data to dequeue
*/
object DeqIO {
def apply[T<:Data](gen: T): DecoupledIO[T] = Flipped(Decoupled(gen))
}
/** An I/O Bundle for Queues
* @param gen The type of data to queue
* @param entries The max number of entries in the queue.
*/
class QueueIO[T <: Data](private val gen: T, val entries: Int) extends Bundle
{ // See github.com/freechipsproject/chisel3/issues/765 for why gen is a private val and proposed replacement APIs.
/* These may look inverted, because the names (enq/deq) are from the perspective of the client,
* but internally, the queue implementation itself sits on the other side
* of the interface so uses the flipped instance.
*/
/** I/O to enqueue data (client is producer, and Queue object is consumer), is [[Chisel.DecoupledIO]] flipped. */
val enq = Flipped(EnqIO(gen))
/** I/O to dequeue data (client is consumer and Queue object is producer), is [[Chisel.DecoupledIO]]*/
val deq = Flipped(DeqIO(gen))
/** The current amount of data in the queue */
val count = Output(UInt(log2Ceil(entries + 1).W))
}
/** A hardware module implementing a Queue
* @param gen The type of data to queue
* @param entries The max number of entries in the queue
* @param pipe True if a single entry queue can run at full throughput (like a pipeline). The ''ready'' signals are
* combinationally coupled.
* @param flow True if the inputs can be consumed on the same cycle (the inputs "flow" through the queue immediately).
* The ''valid'' signals are coupled.
*
* @example {{{
* val q = Module(new Queue(UInt(), 16))
* q.io.enq <> producer.io.out
* consumer.io.in <> q.io.deq
* }}}
*/
@chiselName
class Queue[T <: Data](gen: T,
val entries: Int,
pipe: Boolean = false,
flow: Boolean = false)
(implicit compileOptions: chisel3.core.CompileOptions)
extends Module() {
@deprecated("Module constructor with override _reset deprecated, use withReset", "chisel3")
def this(gen: T, entries: Int, pipe: Boolean, flow: Boolean, override_reset: Option[Bool]) = {
this(gen, entries, pipe, flow)
this.override_reset = override_reset
}
@deprecated("Module constructor with override _reset deprecated, use withReset", "chisel3")
def this(gen: T, entries: Int, pipe: Boolean, flow: Boolean, _reset: Bool) = {
this(gen, entries, pipe, flow)
this.override_reset = Some(_reset)
}
val genType = if (compileOptions.declaredTypeMustBeUnbound) {
requireIsChiselType(gen)
gen
} else {
if (DataMirror.internal.isSynthesizable(gen)) {
chiselTypeOf(gen)
} else {
gen
}
}
val io = IO(new QueueIO(genType, entries))
private val ram = Mem(entries, genType)
private val enq_ptr = Counter(entries)
private val deq_ptr = Counter(entries)
private val maybe_full = RegInit(false.B)
private val ptr_match = enq_ptr.value === deq_ptr.value
private val empty = ptr_match && !maybe_full
private val full = ptr_match && maybe_full
private val do_enq = WireDefault(io.enq.fire())
private val do_deq = WireDefault(io.deq.fire())
when (do_enq) {
ram(enq_ptr.value) := io.enq.bits
enq_ptr.inc()
}
when (do_deq) {
deq_ptr.inc()
}
when (do_enq =/= do_deq) {
maybe_full := do_enq
}
io.deq.valid := !empty
io.enq.ready := !full
io.deq.bits := ram(deq_ptr.value)
if (flow) {
when (io.enq.valid) { io.deq.valid := true.B }
when (empty) {
io.deq.bits := io.enq.bits
do_deq := false.B
when (io.deq.ready) { do_enq := false.B }
}
}
if (pipe) {
when (io.deq.ready) { io.enq.ready := true.B }
}
private val ptr_diff = enq_ptr.value - deq_ptr.value
if (isPow2(entries)) {
io.count := Mux(maybe_full && ptr_match, entries.U, 0.U) | ptr_diff
} else {
io.count := Mux(ptr_match,
Mux(maybe_full,
entries.asUInt, 0.U),
Mux(deq_ptr.value > enq_ptr.value,
entries.asUInt + ptr_diff, ptr_diff))
}
}
/** Factory for a generic hardware queue.
*
* @param enq input (enqueue) interface to the queue, also determines width of queue elements
* @param entries depth (number of elements) of the queue
*
* @return output (dequeue) interface from the queue
*
* @example {{{
* consumer.io.in <> Queue(producer.io.out, 16)
* }}}
*/
object Queue
{
/** Create a queue and supply a DecoupledIO containing the product. */
@chiselName
def apply[T <: Data](
enq: ReadyValidIO[T],
entries: Int = 2,
pipe: Boolean = false,
flow: Boolean = false): DecoupledIO[T] = {
if (entries == 0) {
val deq = Wire(new DecoupledIO(enq.bits))
deq.valid := enq.valid
deq.bits := enq.bits
enq.ready := deq.ready
deq
} else {
require(entries > 0)
val q = Module(new Queue(chiselTypeOf(enq.bits), entries, pipe, flow))
q.io.enq.valid := enq.valid // not using <> so that override is allowed
q.io.enq.bits := enq.bits
enq.ready := q.io.enq.ready
TransitName(q.io.deq, q)
}
}
/** Create a queue and supply a IrrevocableIO containing the product.
* Casting from Decoupled is safe here because we know the Queue has
* Irrevocable semantics; we didn't want to change the return type of
* apply() for backwards compatibility reasons.
*/
@chiselName
def irrevocable[T <: Data](
enq: ReadyValidIO[T],
entries: Int = 2,
pipe: Boolean = false,
flow: Boolean = false): IrrevocableIO[T] = {
require(entries > 0) // Zero-entry queues don't guarantee Irrevocability
val deq = apply(enq, entries, pipe, flow)
val irr = Wire(new IrrevocableIO(deq.bits))
irr.bits := deq.bits
irr.valid := deq.valid
deq.ready := irr.ready
irr
}
}
|