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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
// See LICENSE for license details.
package chisel3.core
import scala.language.experimental.macros
import chisel3.internal._
import chisel3.internal.Builder.{pushCommand, pushOp}
import chisel3.internal.firrtl._
import chisel3.internal.sourceinfo.{SourceInfo, SourceInfoTransform, UnlocatableSourceInfo, DeprecatedSourceInfo}
import chisel3.SourceInfoDoc
import chisel3.core.BiConnect.DontCareCantBeSink
/** User-specified directions.
*/
sealed abstract class SpecifiedDirection
object SpecifiedDirection {
/** Default user direction, also meaning 'not-flipped'
*/
case object Unspecified extends SpecifiedDirection
/** Node and its children are forced as output
*/
case object Output extends SpecifiedDirection
/** Node and its children are forced as inputs
*/
case object Input extends SpecifiedDirection
/** Mainly for containers, children are flipped.
*/
case object Flip extends SpecifiedDirection
def flip(dir: SpecifiedDirection) = dir match {
case Unspecified => Flip
case Flip => Unspecified
case Output => Input
case Input => Output
}
/** Returns the effective SpecifiedDirection of this node given the parent's effective SpecifiedDirection
* and the user-specified SpecifiedDirection of this node.
*/
def fromParent(parentDirection: SpecifiedDirection, thisDirection: SpecifiedDirection) =
(parentDirection, thisDirection) match {
case (SpecifiedDirection.Output, _) => SpecifiedDirection.Output
case (SpecifiedDirection.Input, _) => SpecifiedDirection.Input
case (SpecifiedDirection.Unspecified, thisDirection) => thisDirection
case (SpecifiedDirection.Flip, thisDirection) => SpecifiedDirection.flip(thisDirection)
}
}
/** Resolved directions for both leaf and container nodes, only visible after
* a node is bound (since higher-level specifications like Input and Output
* can override directions).
*/
sealed abstract class ActualDirection
object ActualDirection {
/** Undirectioned, struct-like
*/
case object Unspecified extends ActualDirection
/** Output element, or container with all outputs (even if forced)
*/
case object Output extends ActualDirection
/** Input element, or container with all inputs (even if forced)
*/
case object Input extends ActualDirection
sealed abstract class BidirectionalDirection
case object Default extends BidirectionalDirection
case object Flipped extends BidirectionalDirection
case class Bidirectional(dir: BidirectionalDirection) extends ActualDirection
}
object debug { // scalastyle:ignore object.name
@chiselRuntimeDeprecated
@deprecated("debug doesn't do anything in Chisel3 as no pruning happens in the frontend", "chisel3")
def apply (arg: Data): Data = arg
}
/** Experimental hardware construction reflection API
*/
object DataMirror {
def widthOf(target: Data): Width = target.width
def specifiedDirectionOf(target: Data): SpecifiedDirection = target.specifiedDirection
def directionOf(target: Data): ActualDirection = {
requireIsHardware(target, "node requested directionality on")
target.direction
}
// Internal reflection-style APIs, subject to change and removal whenever.
object internal {
def isSynthesizable(target: Data) = target.topBindingOpt.isDefined
// For those odd cases where you need to care about object reference and uniqueness
def chiselTypeClone[T<:Data](target: Data): T = {
target.cloneTypeFull.asInstanceOf[T]
}
}
}
/** Creates a clone of the super-type of the input elements. Super-type is defined as:
* - for Bits type of the same class: the cloned type of the largest width
* - Bools are treated as UInts
* - For other types of the same class are are the same: clone of any of the elements
* - Otherwise: fail
*/
//scalastyle:off cyclomatic.complexity
private[core] object cloneSupertype {
def apply[T <: Data](elts: Seq[T], createdType: String)(implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions): T = {
require(!elts.isEmpty, s"can't create $createdType with no inputs")
if (elts.head.isInstanceOf[Bits]) {
val model: T = elts reduce { (elt1: T, elt2: T) => ((elt1, elt2) match {
case (elt1: Bool, elt2: Bool) => elt1
case (elt1: Bool, elt2: UInt) => elt2 // TODO: what happens with zero width UInts?
case (elt1: UInt, elt2: Bool) => elt1 // TODO: what happens with zero width UInts?
case (elt1: UInt, elt2: UInt) =>
// TODO: perhaps redefine Widths to allow >= op?
if (elt1.width == (elt1.width max elt2.width)) elt1 else elt2
case (elt1: SInt, elt2: SInt) => if (elt1.width == (elt1.width max elt2.width)) elt1 else elt2
case (elt1: FixedPoint, elt2: FixedPoint) => {
(elt1.binaryPoint, elt2.binaryPoint, elt1.width, elt2.width) match {
case (KnownBinaryPoint(bp1), KnownBinaryPoint(bp2), KnownWidth(w1), KnownWidth(w2)) =>
val maxBinaryPoint = bp1 max bp2
val maxIntegerWidth = (w1 - bp1) max (w2 - bp2)
FixedPoint((maxIntegerWidth + maxBinaryPoint).W, (maxBinaryPoint).BP)
case (KnownBinaryPoint(bp1), KnownBinaryPoint(bp2), _, _) =>
FixedPoint(Width(), (bp1 max bp2).BP)
case _ => FixedPoint()
}
}
case (elt1, elt2) =>
throw new AssertionError(
s"can't create $createdType with heterogeneous types ${elt1.getClass} and ${elt2.getClass}")
}).asInstanceOf[T] }
model.cloneTypeFull
}
else {
for (elt <- elts.tail) {
require(elt.getClass == elts.head.getClass,
s"can't create $createdType with heterogeneous types ${elts.head.getClass} and ${elt.getClass}")
require(elt typeEquivalent elts.head,
s"can't create $createdType with non-equivalent types ${elts.head} and ${elt}")
}
elts.head.cloneTypeFull
}
}
}
/** Returns the chisel type of a hardware object, allowing other hardware to be constructed from it.
*/
object chiselTypeOf {
def apply[T <: Data](target: T): T = {
requireIsHardware(target)
target.cloneTypeFull.asInstanceOf[T]
}
}
/**
* Input, Output, and Flipped are used to define the directions of Module IOs.
*
* Note that they currently clone their source argument, including its bindings.
*
* Thus, an error will be thrown if these are used on bound Data
*/
object Input {
def apply[T<:Data](source: T)(implicit compileOptions: CompileOptions): T = {
if (compileOptions.checkSynthesizable) {
requireIsChiselType(source)
}
val out = source.cloneType.asInstanceOf[T]
out.specifiedDirection = SpecifiedDirection.Input
out
}
}
object Output {
def apply[T<:Data](source: T)(implicit compileOptions: CompileOptions): T = {
if (compileOptions.checkSynthesizable) {
requireIsChiselType(source)
}
val out = source.cloneType.asInstanceOf[T]
out.specifiedDirection = SpecifiedDirection.Output
out
}
}
object Flipped {
def apply[T<:Data](source: T)(implicit compileOptions: CompileOptions): T = {
if (compileOptions.checkSynthesizable) {
requireIsChiselType(source)
}
val out = source.cloneType.asInstanceOf[T]
out.specifiedDirection = SpecifiedDirection.flip(source.specifiedDirection)
out
}
}
/** This forms the root of the type system for wire data types. The data value
* must be representable as some number (need not be known at Chisel compile
* time) of bits, and must have methods to pack / unpack structured data to /
* from bits.
*
* @groupdesc Connect Utilities for connecting hardware components
*/
abstract class Data extends HasId with NamedComponent with SourceInfoDoc {
// This is a bad API that punches through object boundaries.
@deprecated("pending removal once all instances replaced", "chisel3")
private[chisel3] def flatten: IndexedSeq[Element] = {
this match {
case elt: Aggregate => elt.getElements.toIndexedSeq flatMap {_.flatten}
case elt: Element => IndexedSeq(elt)
case elt => throwException(s"Cannot flatten type ${elt.getClass}")
}
}
// User-specified direction, local at this node only.
// Note that the actual direction of this node can differ from child and parent specifiedDirection.
private var _specifiedDirection: SpecifiedDirection = SpecifiedDirection.Unspecified
private[chisel3] def specifiedDirection: SpecifiedDirection = _specifiedDirection
private[core] def specifiedDirection_=(direction: SpecifiedDirection) = {
if (_specifiedDirection != SpecifiedDirection.Unspecified) {
this match {
// Anything flies in compatibility mode
case t: Record if !t.compileOptions.dontAssumeDirectionality =>
case _ => throw Binding.RebindingException(s"Attempted reassignment of user-specified direction to $this")
}
}
_specifiedDirection = direction
}
/** This overwrites a relative SpecifiedDirection with an explicit one, and is used to implement
* the compatibility layer where, at the elements, Flip is Input and unspecified is Output.
* DO NOT USE OUTSIDE THIS PURPOSE. THIS OPERATION IS DANGEROUS!
*/
private[core] def _assignCompatibilityExplicitDirection: Unit = {
(this, _specifiedDirection) match {
case (_: Analog, _) => // nothing to do
case (_, SpecifiedDirection.Unspecified) => _specifiedDirection = SpecifiedDirection.Output
case (_, SpecifiedDirection.Flip) => _specifiedDirection = SpecifiedDirection.Input
case (_, SpecifiedDirection.Input | SpecifiedDirection.Output) => // nothing to do
}
}
// Binding stores information about this node's position in the hardware graph.
// This information is supplemental (more than is necessary to generate FIRRTL) and is used to
// perform checks in Chisel, where more informative error messages are possible.
private var _binding: Option[Binding] = None
// Only valid after node is bound (synthesizable), crashes otherwise
protected def binding: Option[Binding] = _binding
protected def binding_=(target: Binding) {
if (_binding.isDefined) {
throw Binding.RebindingException(s"Attempted reassignment of binding to $this")
}
_binding = Some(target)
}
private[core] def topBindingOpt: Option[TopBinding] = _binding.map {
case ChildBinding(parent) => parent.topBinding
case bindingVal: TopBinding => bindingVal
}
private[core] def topBinding: TopBinding = topBindingOpt.get
/** Binds this node to the hardware graph.
* parentDirection is the direction of the parent node, or Unspecified (default) if the target
* node is the top-level.
* binding and direction are valid after this call completes.
*/
private[chisel3] def bind(target: Binding, parentDirection: SpecifiedDirection = SpecifiedDirection.Unspecified)
// Variant of bind that can be called from subclasses, used for bundle literals
protected def selfBind(target: Binding) = bind(target)
// Both _direction and _resolvedUserDirection are saved versions of computed variables (for
// efficiency, avoid expensive recomputation of frequent operations).
// Both are only valid after binding is set.
// Direction of this node, accounting for parents (force Input / Output) and children.
private var _direction: Option[ActualDirection] = None
private[chisel3] def direction: ActualDirection = _direction.get
private[core] def direction_=(actualDirection: ActualDirection) {
if (_direction.isDefined) {
throw Binding.RebindingException(s"Attempted reassignment of resolved direction to $this")
}
_direction = Some(actualDirection)
}
// Return ALL elements at root of this type.
// Contasts with flatten, which returns just Bits
// TODO: refactor away this, this is outside the scope of Data
private[chisel3] def allElements: Seq[Element]
private[core] def badConnect(that: Data)(implicit sourceInfo: SourceInfo): Unit =
throwException(s"cannot connect ${this} and ${that}")
private[chisel3] def connect(that: Data)(implicit sourceInfo: SourceInfo, connectCompileOptions: CompileOptions): Unit = {
if (connectCompileOptions.checkSynthesizable) {
requireIsHardware(this, "data to be connected")
requireIsHardware(that, "data to be connected")
this.topBinding match {
case _: ReadOnlyBinding => throwException(s"Cannot reassign to read-only $this")
case _ => // fine
}
try {
MonoConnect.connect(sourceInfo, connectCompileOptions, this, that, Builder.forcedUserModule)
} catch {
case MonoConnect.MonoConnectException(message) =>
throwException(
s"Connection between sink ($this) and source ($that) failed @$message"
)
}
} else {
this legacyConnect that
}
}
private[chisel3] def bulkConnect(that: Data)(implicit sourceInfo: SourceInfo, connectCompileOptions: CompileOptions): Unit = {
if (connectCompileOptions.checkSynthesizable) {
requireIsHardware(this, s"data to be bulk-connected")
requireIsHardware(that, s"data to be bulk-connected")
(this.topBinding, that.topBinding) match {
case (_: ReadOnlyBinding, _: ReadOnlyBinding) => throwException(s"Both $this and $that are read-only")
// DontCare cannot be a sink (LHS)
case (_: DontCareBinding, _) => throw DontCareCantBeSink
case _ => // fine
}
try {
BiConnect.connect(sourceInfo, connectCompileOptions, this, that, Builder.forcedUserModule)
} catch {
case BiConnect.BiConnectException(message) =>
throwException(
s"Connection between left ($this) and source ($that) failed @$message"
)
}
} else {
this legacyConnect that
}
}
/** Whether this Data has the same model ("data type") as that Data.
* Data subtypes should overload this with checks against their own type.
*/
private[core] def typeEquivalent(that: Data): Boolean
// Internal API: returns a ref that can be assigned to, if consistent with the binding
private[chisel3] def lref: Node = {
requireIsHardware(this)
topBindingOpt match {
case Some(binding: ReadOnlyBinding) => throwException(s"internal error: attempted to generate LHS ref to ReadOnlyBinding $binding")
case Some(binding: TopBinding) => Node(this)
case opt => throwException(s"internal error: unknown binding $opt in generating LHS ref")
}
}
// Internal API: returns a ref, if bound. Literals should override this as needed.
private[chisel3] def ref: Arg = {
requireIsHardware(this)
topBindingOpt match {
case Some(binding: LitBinding) => throwException(s"internal error: can't handle literal binding $binding")
case Some(binding: TopBinding) => Node(this)
case opt => throwException(s"internal error: unknown binding $opt in generating LHS ref")
}
}
private[chisel3] def width: Width
private[core] def legacyConnect(that: Data)(implicit sourceInfo: SourceInfo): Unit
/** Internal API; Chisel users should look at chisel3.chiselTypeOf(...).
*
* cloneType must be defined for any Chisel object extending Data.
* It is responsible for constructing a basic copy of the object being cloned.
*
* @return a copy of the object.
*/
def cloneType: this.type
/** Internal API; Chisel users should look at chisel3.chiselTypeOf(...).
*
* Returns a copy of this data type, with hardware bindings (if any) removed.
* Directionality data is still preserved.
*/
private[chisel3] def cloneTypeFull: this.type = {
val clone = this.cloneType.asInstanceOf[this.type] // get a fresh object, without bindings
// Only the top-level direction needs to be fixed up, cloneType should do the rest
clone.specifiedDirection = specifiedDirection
clone
}
final def := (that: Data)(implicit sourceInfo: SourceInfo, connectionCompileOptions: CompileOptions): Unit = this.connect(that)(sourceInfo, connectionCompileOptions)
final def <> (that: Data)(implicit sourceInfo: SourceInfo, connectionCompileOptions: CompileOptions): Unit = this.bulkConnect(that)(sourceInfo, connectionCompileOptions)
@chiselRuntimeDeprecated
@deprecated("litArg is deprecated, use litOption or litTo*Option", "chisel3.2")
def litArg(): Option[LitArg] = topBindingOpt match {
case Some(ElementLitBinding(litArg)) => Some(litArg)
case Some(BundleLitBinding(litMap)) => None // this API does not support Bundle literals
case _ => None
}
@chiselRuntimeDeprecated
@deprecated("isLit is deprecated, use litOption.isDefined", "chisel3.2")
def isLit(): Boolean = litArg.isDefined
/**
* If this is a literal that is representable as bits, returns the value as a BigInt.
* If not a literal, or not representable as bits (for example, is or contains Analog), returns None.
*/
def litOption(): Option[BigInt]
/**
* Returns the literal value if this is a literal that is representable as bits, otherwise crashes.
*/
def litValue(): BigInt = litOption.get
/** Returns the width, in bits, if currently known.
* @throws java.util.NoSuchElementException if the width is not known. */
final def getWidth: Int = width.get
/** Returns whether the width is currently known. */
final def isWidthKnown: Boolean = width.known
/** Returns Some(width) if the width is known, else None. */
final def widthOption: Option[Int] = if (isWidthKnown) Some(getWidth) else None
/** Packs the value of this object as plain Bits.
*
* This performs the inverse operation of fromBits(Bits).
*/
@chiselRuntimeDeprecated
@deprecated("Best alternative, .asUInt()", "chisel3")
def toBits(implicit compileOptions: CompileOptions): UInt = do_asUInt(DeprecatedSourceInfo, compileOptions)
/** Does a reinterpret cast of the bits in this node into the format that provides.
* Returns a new Wire of that type. Does not modify existing nodes.
*
* x.asTypeOf(that) performs the inverse operation of x := that.toBits.
*
* @note bit widths are NOT checked, may pad or drop bits from input
* @note that should have known widths
*/
def asTypeOf[T <: Data](that: T): T = macro SourceInfoTransform.thatArg
/** @group SourceInfoTransformMacro */
def do_asTypeOf[T <: Data](that: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
val thatCloned = Wire(that.cloneTypeFull)
thatCloned.connectFromBits(this.asUInt())
thatCloned
}
/** Assigns this node from Bits type. Internal implementation for asTypeOf.
*/
private[core] def connectFromBits(that: Bits)(implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions): Unit
/** Reinterpret cast to UInt.
*
* @note value not guaranteed to be preserved: for example, a SInt of width
* 3 and value -1 (0b111) would become an UInt with value 7
* @note Aggregates are recursively packed with the first element appearing
* in the least-significant bits of the result.
*/
final def asUInt(): UInt = macro SourceInfoTransform.noArg
/** @group SourceInfoTransformMacro */
def do_asUInt(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): UInt
/** Default pretty printing */
def toPrintable: Printable
}
trait WireFactory {
def apply[T <: Data](t: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
if (compileOptions.declaredTypeMustBeUnbound) {
requireIsChiselType(t, "wire type")
}
val x = t.cloneTypeFull
// Bind each element of x to being a Wire
x.bind(WireBinding(Builder.forcedUserModule))
pushCommand(DefWire(sourceInfo, x))
if (!compileOptions.explicitInvalidate) {
pushCommand(DefInvalid(sourceInfo, x.ref))
}
x
}
}
object Wire extends WireFactory
object WireInit {
def apply[T <: Data](init: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
val model = (init match {
// For e.g. Wire(init=0.U(k.W)), fix the Reg's width to k
case init: Bits if init.litIsForcedWidth == Some(false) => init.cloneTypeWidth(Width())
case _ => init.cloneTypeFull
}).asInstanceOf[T]
apply(model, init)
}
private def applyImpl[T <: Data](t: T, init: Data)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
implicit val noSourceInfo = UnlocatableSourceInfo
val x = Wire(t)
requireIsHardware(init, "wire initializer")
x := init
x
}
def apply[T <: Data](t: T, init: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
applyImpl(t, init)
}
def apply[T <: Data](t: T, init: DontCare.type)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
applyImpl(t, init)
}
}
/** RHS (source) for Invalidate API.
* Causes connection logic to emit a DefInvalid when connected to an output port (or wire).
*/
object DontCare extends Element(width = UnknownWidth()) {
// This object should be initialized before we execute any user code that refers to it,
// otherwise this "Chisel" object will end up on the UserModule's id list.
bind(DontCareBinding(), SpecifiedDirection.Output)
override def cloneType = DontCare
override def litOption = None
def toPrintable: Printable = PString("DONTCARE")
private[core] def connectFromBits(that: chisel3.core.Bits)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Unit = {
Builder.error("connectFromBits: DontCare cannot be a connection sink (LHS)")
}
def do_asUInt(implicit sourceInfo: chisel3.internal.sourceinfo.SourceInfo, compileOptions: CompileOptions): chisel3.core.UInt = {
Builder.error("DontCare does not have a UInt representation")
0.U
}
// DontCare's only match themselves.
private[core] def typeEquivalent(that: chisel3.core.Data): Boolean = that == DontCare
}
|