summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/chisel3/util')
-rw-r--r--src/main/scala/chisel3/util/Arbiter.scala128
-rw-r--r--src/main/scala/chisel3/util/BitPat.scala90
-rw-r--r--src/main/scala/chisel3/util/Bitwise.scala79
-rw-r--r--src/main/scala/chisel3/util/Cat.scala20
-rw-r--r--src/main/scala/chisel3/util/CircuitMath.scala38
-rw-r--r--src/main/scala/chisel3/util/Conditional.scala94
-rw-r--r--src/main/scala/chisel3/util/Counter.scala62
-rw-r--r--src/main/scala/chisel3/util/Decoupled.scala269
-rw-r--r--src/main/scala/chisel3/util/Enum.scala53
-rw-r--r--src/main/scala/chisel3/util/ImplicitConversions.scala10
-rw-r--r--src/main/scala/chisel3/util/LFSR.scala26
-rw-r--r--src/main/scala/chisel3/util/Lookup.scala19
-rw-r--r--src/main/scala/chisel3/util/Math.scala44
-rw-r--r--src/main/scala/chisel3/util/Mux.scala65
-rw-r--r--src/main/scala/chisel3/util/OneHot.scala66
-rw-r--r--src/main/scala/chisel3/util/Reg.scala66
-rw-r--r--src/main/scala/chisel3/util/TransitName.scala24
-rw-r--r--src/main/scala/chisel3/util/Valid.scala61
-rw-r--r--src/main/scala/chisel3/util/util.scala12
19 files changed, 1226 insertions, 0 deletions
diff --git a/src/main/scala/chisel3/util/Arbiter.scala b/src/main/scala/chisel3/util/Arbiter.scala
new file mode 100644
index 00000000..89bb644a
--- /dev/null
+++ b/src/main/scala/chisel3/util/Arbiter.scala
@@ -0,0 +1,128 @@
+// See LICENSE for license details.
+
+/** Arbiters in all shapes and sizes.
+ */
+
+package chisel3.util
+
+import chisel3._
+// TODO: remove this once we have CompileOptions threaded through the macro system.
+import chisel3.core.ExplicitCompileOptions.NotStrict
+
+/** IO bundle definition for an Arbiter, which takes some number of ready-valid inputs and outputs
+ * (selects) at most one.
+ *
+ * @param gen data type
+ * @param n number of inputs
+ */
+class ArbiterIO[T <: Data](gen: T, n: Int) extends Bundle {
+ val in = Flipped(Vec(n, Decoupled(gen)))
+ val out = Decoupled(gen)
+ val chosen = Output(UInt.width(log2Up(n)))
+}
+
+/** Arbiter Control determining which producer has access
+ */
+private object ArbiterCtrl {
+ def apply(request: Seq[Bool]): Seq[Bool] = request.length match {
+ case 0 => Seq()
+ case 1 => Seq(Bool(true))
+ case _ => Bool(true) +: request.tail.init.scanLeft(request.head)(_ || _).map(!_)
+ }
+}
+
+abstract class LockingArbiterLike[T <: Data](gen: T, n: Int, count: Int, needsLock: Option[T => Bool]) extends Module {
+ def grant: Seq[Bool]
+ def choice: UInt
+ val io = IO(new ArbiterIO(gen, n))
+
+ io.chosen := choice
+ io.out.valid := io.in(io.chosen).valid
+ io.out.bits := io.in(io.chosen).bits
+
+ if (count > 1) {
+ val lockCount = Counter(count)
+ val lockIdx = Reg(UInt())
+ val locked = lockCount.value =/= UInt(0)
+ val wantsLock = needsLock.map(_(io.out.bits)).getOrElse(Bool(true))
+
+ when (io.out.fire() && wantsLock) {
+ lockIdx := io.chosen
+ lockCount.inc()
+ }
+
+ when (locked) { io.chosen := lockIdx }
+ for ((in, (g, i)) <- io.in zip grant.zipWithIndex)
+ in.ready := Mux(locked, lockIdx === UInt(i), g) && io.out.ready
+ } else {
+ for ((in, g) <- io.in zip grant)
+ in.ready := g && io.out.ready
+ }
+}
+
+class LockingRRArbiter[T <: Data](gen: T, n: Int, count: Int, needsLock: Option[T => Bool] = None)
+ extends LockingArbiterLike[T](gen, n, count, needsLock) {
+ lazy val lastGrant = RegEnable(io.chosen, io.out.fire())
+ lazy val grantMask = (0 until n).map(UInt(_) > lastGrant)
+ lazy val validMask = io.in zip grantMask map { case (in, g) => in.valid && g }
+
+ override def grant: Seq[Bool] = {
+ val ctrl = ArbiterCtrl((0 until n).map(i => validMask(i)) ++ io.in.map(_.valid))
+ (0 until n).map(i => ctrl(i) && grantMask(i) || ctrl(i + n))
+ }
+
+ override lazy val choice = Wire(init=UInt(n-1))
+ for (i <- n-2 to 0 by -1)
+ when (io.in(i).valid) { choice := UInt(i) }
+ for (i <- n-1 to 1 by -1)
+ when (validMask(i)) { choice := UInt(i) }
+}
+
+class LockingArbiter[T <: Data](gen: T, n: Int, count: Int, needsLock: Option[T => Bool] = None)
+ extends LockingArbiterLike[T](gen, n, count, needsLock) {
+ def grant: Seq[Bool] = ArbiterCtrl(io.in.map(_.valid))
+
+ override lazy val choice = Wire(init=UInt(n-1))
+ for (i <- n-2 to 0 by -1)
+ when (io.in(i).valid) { choice := UInt(i) }
+}
+
+/** Hardware module that is used to sequence n producers into 1 consumer.
+ * Producers are chosen in round robin order.
+ *
+ * @example {{{
+ * val arb = new RRArbiter(2, UInt())
+ * arb.io.in(0) <> producer0.io.out
+ * arb.io.in(1) <> producer1.io.out
+ * consumer.io.in <> arb.io.out
+ * }}}
+ */
+class RRArbiter[T <: Data](gen:T, n: Int) extends LockingRRArbiter[T](gen, n, 1)
+
+/** Hardware module that is used to sequence n producers into 1 consumer.
+ * Priority is given to lower producer.
+ *
+ * @example {{{
+ * val arb = Module(new Arbiter(2, UInt()))
+ * arb.io.in(0) <> producer0.io.out
+ * arb.io.in(1) <> producer1.io.out
+ * consumer.io.in <> arb.io.out
+ * }}}
+ */
+class Arbiter[T <: Data](gen: T, n: Int) extends Module {
+ val io = IO(new ArbiterIO(gen, n))
+
+ io.chosen := UInt(n-1)
+ io.out.bits := io.in(n-1).bits
+ for (i <- n-2 to 0 by -1) {
+ when (io.in(i).valid) {
+ io.chosen := UInt(i)
+ io.out.bits := io.in(i).bits
+ }
+ }
+
+ val grant = ArbiterCtrl(io.in.map(_.valid))
+ for ((in, g) <- io.in zip grant)
+ in.ready := g && io.out.ready
+ io.out.valid := !grant.last || io.in.last.valid
+}
diff --git a/src/main/scala/chisel3/util/BitPat.scala b/src/main/scala/chisel3/util/BitPat.scala
new file mode 100644
index 00000000..972010a6
--- /dev/null
+++ b/src/main/scala/chisel3/util/BitPat.scala
@@ -0,0 +1,90 @@
+// See LICENSE for license details.
+
+package chisel3.util
+
+import scala.language.experimental.macros
+
+import chisel3._
+import chisel3.internal.sourceinfo.{SourceInfo, SourceInfoTransform}
+
+object BitPat {
+ /** Parses a bit pattern string into (bits, mask, width).
+ *
+ * @return bits the literal value, with don't cares being 0
+ * @return mask the mask bits, with don't cares being 0 and cares being 1
+ * @return width the number of bits in the literal, including values and
+ * don't cares.
+ */
+ private def parse(x: String): (BigInt, BigInt, Int) = {
+ // Notes:
+ // While Verilog Xs also handle octal and hex cases, there isn't a
+ // compelling argument and no one has asked for it.
+ // If ? parsing is to be exposed, the return API needs further scrutiny
+ // (especially with things like mask polarity).
+ require(x.head == 'b', "BitPats must be in binary and be prefixed with 'b'")
+ var bits = BigInt(0)
+ var mask = BigInt(0)
+ for (d <- x.tail) {
+ if (d != '_') {
+ require("01?".contains(d), "Literal: " + x + " contains illegal character: " + d)
+ mask = (mask << 1) + (if (d == '?') 0 else 1)
+ bits = (bits << 1) + (if (d == '1') 1 else 0)
+ }
+ }
+ (bits, mask, x.length - 1)
+ }
+
+ /** Creates a [[BitPat]] literal from a string.
+ *
+ * @param n the literal value as a string, in binary, prefixed with 'b'
+ * @note legal characters are '0', '1', and '?', as well as '_' and white
+ * space (which are ignored)
+ */
+ def apply(n: String): BitPat = {
+ val (bits, mask, width) = parse(n)
+ new BitPat(bits, mask, width)
+ }
+
+ /** Creates a [[BitPat]] of all don't cares of the specified bitwidth. */
+ def dontCare(width: Int): BitPat = BitPat("b" + ("?" * width))
+
+ @deprecated("Use BitPat.dontCare", "chisel3")
+ def DC(width: Int): BitPat = dontCare(width) // scalastyle:ignore method.name
+
+ /** Allows BitPats to be used where a UInt is expected.
+ *
+ * @note the BitPat must not have don't care bits (will error out otherwise)
+ */
+ def bitPatToUInt(x: BitPat): UInt = {
+ require(x.mask == (BigInt(1) << x.getWidth) - 1)
+ UInt(x.value, x.getWidth)
+ }
+
+ /** Allows UInts to be used where a BitPat is expected, useful for when an
+ * interface is defined with BitPats but not all cases need the partial
+ * matching capability.
+ *
+ * @note the UInt must be a literal
+ */
+ def apply(x: UInt): BitPat = {
+ require(x.isLit)
+ val len = if (x.isWidthKnown) x.getWidth else 0
+ apply("b" + x.litValue.toString(2).reverse.padTo(len, "0").reverse.mkString)
+ }
+}
+
+// TODO: Break out of Core? (this doesn't involve FIRRTL generation)
+/** Bit patterns are literals with masks, used to represent values with don't
+ * cares. Equality comparisons will ignore don't care bits (for example,
+ * BitPat(0b10?1) === 0b1001.asUInt and 0b1011.asUInt.
+ */
+sealed class BitPat(val value: BigInt, val mask: BigInt, width: Int) {
+ def getWidth: Int = width
+ def === (that: UInt): Bool = macro SourceInfoTransform.thatArg
+ def =/= (that: UInt): Bool = macro SourceInfoTransform.thatArg
+ def != (that: UInt): Bool = macro SourceInfoTransform.thatArg
+
+ def do_=== (that: UInt)(implicit sourceInfo: SourceInfo): Bool = value.asUInt === (that & mask.asUInt) // scalastyle:ignore method.name
+ def do_=/= (that: UInt)(implicit sourceInfo: SourceInfo): Bool = !(this === that) // scalastyle:ignore method.name
+ def do_!= (that: UInt)(implicit sourceInfo: SourceInfo): Bool = this =/= that // scalastyle:ignore method.name
+}
diff --git a/src/main/scala/chisel3/util/Bitwise.scala b/src/main/scala/chisel3/util/Bitwise.scala
new file mode 100644
index 00000000..289d27b1
--- /dev/null
+++ b/src/main/scala/chisel3/util/Bitwise.scala
@@ -0,0 +1,79 @@
+// See LICENSE for license details.
+
+/** Miscellaneous circuit generators operating on bits.
+ */
+
+package chisel3.util
+
+import chisel3._
+import chisel3.core.SeqUtils
+
+object FillInterleaved {
+ /** Creates n repetitions of each bit of x in order.
+ *
+ * Output data-equivalent to in(size(in)-1) (n times) ## ... ## in(1) (n times) ## in(0) (n times)
+ * For example, FillInterleaved(2, "b1000") === UInt("b11 00 00 00")
+ */
+ def apply(n: Int, in: UInt): UInt = apply(n, in.toBools)
+
+ /** Creates n repetitions of each bit of x in order.
+ *
+ * Output data-equivalent to in(size(in)-1) (n times) ## ... ## in(1) (n times) ## in(0) (n times)
+ */
+ def apply(n: Int, in: Seq[Bool]): UInt = Cat(in.map(Fill(n, _)).reverse)
+}
+
+/** Returns the number of bits set (i.e value is 1) in the input signal.
+ */
+object PopCount
+{
+ def apply(in: Iterable[Bool]): UInt = SeqUtils.count(in.toSeq)
+ def apply(in: Bits): UInt = apply((0 until in.getWidth).map(in(_)))
+}
+
+object Fill {
+ /** Create n repetitions of x using a tree fanout topology.
+ *
+ * Output data-equivalent to x ## x ## ... ## x (n repetitions).
+ */
+ def apply(n: Int, x: UInt): UInt = {
+ n match {
+ case 0 => UInt.width(0)
+ case 1 => x
+ case _ if x.isWidthKnown && x.getWidth == 1 =>
+ Mux(x.toBool, UInt((BigInt(1) << n) - 1, n), UInt(0, n))
+ case _ if n > 1 =>
+ val p2 = Array.ofDim[UInt](log2Up(n + 1))
+ p2(0) = x
+ for (i <- 1 until p2.length)
+ p2(i) = Cat(p2(i-1), p2(i-1))
+ Cat((0 until log2Up(n + 1)).filter(i => (n & (1 << i)) != 0).map(p2(_)))
+ case _ => throw new IllegalArgumentException(s"n (=$n) must be nonnegative integer.")
+ }
+ }
+}
+
+object Reverse {
+ private def doit(in: UInt, length: Int): UInt = {
+ if (length == 1) {
+ in
+ } else if (isPow2(length) && length >= 8 && length <= 64) {
+ // This esoterica improves simulation performance
+ var res = in
+ var shift = length >> 1
+ var mask = UInt((BigInt(1) << length) - 1, length)
+ do {
+ mask = mask ^ (mask(length-shift-1,0) << shift)
+ res = ((res >> shift) & mask) | ((res(length-shift-1,0) << shift) & ~mask)
+ shift = shift >> 1
+ } while (shift > 0)
+ res
+ } else {
+ val half = (1 << log2Up(length))/2
+ Cat(doit(in(half-1,0), half), doit(in(length-1,half), length-half))
+ }
+ }
+ /** Returns the input in bit-reversed order. Useful for little/big-endian conversion.
+ */
+ def apply(in: UInt): UInt = doit(in, in.getWidth)
+}
diff --git a/src/main/scala/chisel3/util/Cat.scala b/src/main/scala/chisel3/util/Cat.scala
new file mode 100644
index 00000000..ba12a7d4
--- /dev/null
+++ b/src/main/scala/chisel3/util/Cat.scala
@@ -0,0 +1,20 @@
+// See LICENSE for license details.
+
+package chisel3.util
+
+import chisel3._
+import chisel3.core.SeqUtils
+
+object Cat {
+ /** Concatenates the argument data elements, in argument order, together.
+ */
+ def apply[T <: Bits](a: T, r: T*): UInt = apply(a :: r.toList)
+
+ /** Concatenates the data elements of the input sequence, in reverse sequence order, together.
+ * The first element of the sequence forms the most significant bits, while the last element
+ * in the sequence forms the least significant bits.
+ *
+ * Equivalent to r(0) ## r(1) ## ... ## r(n-1).
+ */
+ def apply[T <: Bits](r: Seq[T]): UInt = SeqUtils.asUInt(r.reverse)
+}
diff --git a/src/main/scala/chisel3/util/CircuitMath.scala b/src/main/scala/chisel3/util/CircuitMath.scala
new file mode 100644
index 00000000..d478e10e
--- /dev/null
+++ b/src/main/scala/chisel3/util/CircuitMath.scala
@@ -0,0 +1,38 @@
+// See LICENSE for license details.
+
+/** Circuit-land math operations.
+ */
+
+package chisel3.util
+
+import chisel3._
+
+object Log2 {
+ /** Returns the base-2 integer logarithm of the least-significant `width` bits of an UInt.
+ *
+ * @note The result is truncated, so e.g. Log2(UInt(13)) === UInt(3)
+ */
+ def apply(x: Bits, width: Int): UInt = {
+ if (width < 2) {
+ UInt(0)
+ } else if (width == 2) {
+ x(1)
+ } else if (width <= divideAndConquerThreshold) {
+ Mux(x(width-1), UInt(width-1), apply(x, width-1))
+ } else {
+ val mid = 1 << (log2Ceil(width) - 1)
+ val hi = x(width-1, mid)
+ val lo = x(mid-1, 0)
+ val useHi = hi.orR
+ Cat(useHi, Mux(useHi, Log2(hi, width - mid), Log2(lo, mid)))
+ }
+ }
+
+ /** Returns the base-2 integer logarithm of an UInt.
+ *
+ * @note The result is truncated, so e.g. Log2(UInt(13)) === UInt(3)
+ */
+ def apply(x: Bits): UInt = apply(x, x.getWidth)
+
+ private def divideAndConquerThreshold = 4
+}
diff --git a/src/main/scala/chisel3/util/Conditional.scala b/src/main/scala/chisel3/util/Conditional.scala
new file mode 100644
index 00000000..5830e014
--- /dev/null
+++ b/src/main/scala/chisel3/util/Conditional.scala
@@ -0,0 +1,94 @@
+// See LICENSE for license details.
+
+/** Conditional blocks.
+ */
+
+package chisel3.util
+
+import scala.language.reflectiveCalls
+import scala.language.experimental.macros
+import scala.reflect.runtime.universe._
+import scala.reflect.macros.blackbox._
+
+import chisel3._
+
+object unless { // scalastyle:ignore object.name
+ /** Does the same thing as [[when$ when]], but with the condition inverted.
+ */
+ def apply(c: Bool)(block: => Unit) {
+ when (!c) { block }
+ }
+}
+
+/** Implementation details for [[switch]]. See [[switch]] and [[chisel3.util.is is]] for the
+ * user-facing API.
+ */
+class SwitchContext[T <: Bits](cond: T) {
+ def is(v: Iterable[T])(block: => Unit) {
+ if (!v.isEmpty) {
+ when (v.map(_.asUInt === cond.asUInt).reduce(_||_)) {
+ block
+ }
+ }
+ }
+ def is(v: T)(block: => Unit) { is(Seq(v))(block) }
+ def is(v: T, vr: T*)(block: => Unit) { is(v :: vr.toList)(block) }
+}
+
+/** Use to specify cases in a [[switch]] block, equivalent to a [[when$ when]] block comparing to
+ * the condition variable.
+ *
+ * @note illegal outside a [[switch]] block
+ * @note multiple conditions may fire simultaneously
+ * @note dummy implementation, a macro inside [[switch]] transforms this into the actual
+ * implementation
+ */
+object is { // scalastyle:ignore object.name
+ // TODO: Begin deprecation of non-type-parameterized is statements.
+ /** Executes `block` if the switch condition is equal to any of the values in `v`.
+ */
+ def apply(v: Iterable[Bits])(block: => Unit) {
+ require(false, "The 'is' keyword may not be used outside of a switch.")
+ }
+
+ /** Executes `block` if the switch condition is equal to `v`.
+ */
+ def apply(v: Bits)(block: => Unit) {
+ require(false, "The 'is' keyword may not be used outside of a switch.")
+ }
+
+ /** Executes `block` if the switch condition is equal to any of the values in the argument list.
+ */
+ def apply(v: Bits, vr: Bits*)(block: => Unit) {
+ require(false, "The 'is' keyword may not be used outside of a switch.")
+ }
+}
+
+/** Conditional logic to form a switch block. See [[is$ is]] for the case API.
+ *
+ * @example {{{
+ * switch (myState) {
+ * is (state1) {
+ * // some logic here that runs when myState === state1
+ * }
+ * is (state2) {
+ * // some logic here that runs when myState === state2
+ * }
+ * }
+ * }}}
+ */
+object switch { // scalastyle:ignore object.name
+ def apply[T <: Bits](cond: T)(x: => Unit): Unit = macro impl
+ def impl(c: Context)(cond: c.Tree)(x: c.Tree): c.Tree = { import c.universe._
+ val sc = c.universe.internal.reificationSupport.freshTermName("sc")
+ def extractIsStatement(tree: Tree): List[c.universe.Tree] = tree match {
+ // TODO: remove when Chisel compatibility package is removed
+ case q"Chisel.`package`.is.apply( ..$params )( ..$body )" => List(q"$sc.is( ..$params )( ..$body )")
+ case q"chisel3.util.is.apply( ..$params )( ..$body )" => List(q"$sc.is( ..$params )( ..$body )")
+ case b => throw new Exception(s"Cannot include blocks that do not begin with is() in switch.")
+ }
+ val q"..$body" = x
+ val ises = body.flatMap(extractIsStatement(_))
+ q"""{ val $sc = new SwitchContext($cond); ..$ises }"""
+ }
+}
diff --git a/src/main/scala/chisel3/util/Counter.scala b/src/main/scala/chisel3/util/Counter.scala
new file mode 100644
index 00000000..ba66d667
--- /dev/null
+++ b/src/main/scala/chisel3/util/Counter.scala
@@ -0,0 +1,62 @@
+// See LICENSE for license details.
+
+package chisel3.util
+
+import chisel3._
+//import chisel3.core.ExplicitCompileOptions.Strict
+
+/** A counter module
+ *
+ * @param n number of counts before the counter resets (or one more than the
+ * maximum output value of the counter), need not be a power of two
+ */
+class Counter(val n: Int) {
+ require(n >= 0)
+ val value = if (n > 1) Reg(init=UInt(0, log2Up(n))) else UInt(0)
+
+ /** Increment the counter, returning whether the counter currently is at the
+ * maximum and will wrap. The incremented value is registered and will be
+ * visible on the next cycle.
+ */
+ def inc(): Bool = {
+ if (n > 1) {
+ val wrap = value === UInt(n-1)
+ value := value + UInt(1)
+ if (!isPow2(n)) {
+ when (wrap) { value := UInt(0) }
+ }
+ wrap
+ } else {
+ Bool(true)
+ }
+ }
+}
+
+object Counter
+{
+ /** Instantiate a [[Counter! counter]] with the specified number of counts.
+ */
+ def apply(n: Int): Counter = new Counter(n)
+
+ /** Instantiate a [[Counter! counter]] with the specified number of counts and a gate.
+ *
+ * @param cond condition that controls whether the counter increments this cycle
+ * @param n number of counts before the counter resets
+ * @return tuple of the counter value and whether the counter will wrap (the value is at
+ * maximum and the condition is true).
+ *
+ * @example {{{
+ * val countOn = Bool(true) // increment counter every clock cycle
+ * val (counterValue, counterWrap) = Counter(countOn, 4)
+ * when (counterValue === UInt(3)) {
+ * ...
+ * }
+ * }}}
+ */
+ def apply(cond: Bool, n: Int): (UInt, Bool) = {
+ val c = new Counter(n)
+ var wrap: Bool = null
+ when (cond) { wrap = c.inc() }
+ (c.value, cond && wrap)
+ }
+}
diff --git a/src/main/scala/chisel3/util/Decoupled.scala b/src/main/scala/chisel3/util/Decoupled.scala
new file mode 100644
index 00000000..a0cbf4f7
--- /dev/null
+++ b/src/main/scala/chisel3/util/Decoupled.scala
@@ -0,0 +1,269 @@
+// See LICENSE for license details.
+
+/** Wrappers for ready-valid (Decoupled) interfaces and associated circuit generators using them.
+ */
+
+package chisel3.util
+
+import chisel3._
+// TODO: remove this once we have CompileOptions threaded through the macro system.
+import chisel3.core.ExplicitCompileOptions.NotStrict
+
+/** 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 consumer
+ * uses the flipped interface. Actual semantics of ready/valid are
+ * enforced via use of concrete subclasses.
+ */
+abstract class ReadyValidIO[+T <: Data](gen: T) extends Bundle
+{
+ val ready = Input(Bool())
+ val valid = Output(Bool())
+ val bits = Output(gen.chiselCloneType)
+}
+
+object ReadyValidIO {
+
+ implicit class AddMethodsToReadyValid[T<:Data](val target: ReadyValidIO[T]) extends AnyVal {
+ 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 := Bool(true)
+ target.bits := dat
+ dat
+ }
+
+ /** Indicate no enqueue occurs. Valid is set to false, and all bits are set to zero.
+ */
+ def noenq(): Unit = {
+ target.valid := Bool(false)
+ // We want the type from the following, not any existing binding.
+ target.bits := target.bits.cloneType.fromBits(0.asUInt)
+ }
+
+ /** Assert ready on this port and return the associated data bits.
+ * This is typically used when valid has been asserted by the producer side.
+ * @param b ignored
+ * @return the data for this device,
+ */
+ def deq(): T = {
+ target.ready := Bool(true)
+ target.bits
+ }
+
+ /** Indicate no dequeue occurs. Ready is set to false
+ */
+ def nodeq(): Unit = {
+ target.ready := Bool(false)
+ }
+ }
+}
+
+/** 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.
+ */
+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
+ */
+ def apply[T <: Data](irr: IrrevocableIO[T]): DecoupledIO[T] = {
+ require(irr.bits.flatten forall (_.dir == OUTPUT), "Only safe to cast produced Irrevocable bits to Decoupled.")
+ val d = Wire(new DecoupledIO(irr.bits))
+ d.bits := irr.bits
+ d.valid := irr.valid
+ irr.ready := d.ready
+ d
+ }
+// override def cloneType: this.type = {
+// DeqIO(gen).asInstanceOf[this.type]
+// }
+}
+
+/** 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.
+ */
+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(dec.bits.flatten forall (_.dir == INPUT), "Only safe to cast consumed Decoupled bits to Irrevocable.")
+ val i = Wire(new IrrevocableIO(dec.bits))
+ dec.bits := i.bits
+ dec.valid := i.valid
+ i.ready := dec.ready
+ i
+ }
+}
+
+object EnqIO {
+ def apply[T<:Data](gen: T): DecoupledIO[T] = Decoupled(gen)
+}
+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](gen: T, entries: Int) extends Bundle
+{
+ /** I/O to enqueue data, is [[Chisel.DecoupledIO]] flipped */
+ val enq = DeqIO(gen)
+ /** I/O to enqueue data, is [[Chisel.DecoupledIO]]*/
+ val deq = EnqIO(gen)
+ /** The current amount of data in the queue */
+ val count = Output(UInt.width(log2Up(entries + 1)))
+}
+
+/** 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 = new Queue(UInt(), 16)
+ * q.io.enq <> producer.io.out
+ * consumer.io.in <> q.io.deq
+ * }}}
+ */
+class Queue[T <: Data](gen: T,
+ val entries: Int,
+ pipe: Boolean = false,
+ flow: Boolean = false,
+ override_reset: Option[Bool] = None)
+extends Module(override_reset=override_reset) {
+ def this(gen: T, entries: Int, pipe: Boolean, flow: Boolean, _reset: Bool) =
+ this(gen, entries, pipe, flow, Some(_reset))
+
+ val io = IO(new QueueIO(gen, entries))
+
+ val ram = Mem(entries, gen)
+ val enq_ptr = Counter(entries)
+ val deq_ptr = Counter(entries)
+ val maybe_full = Reg(init=Bool(false))
+
+ val ptr_match = enq_ptr.value === deq_ptr.value
+ val empty = ptr_match && !maybe_full
+ val full = ptr_match && maybe_full
+ val do_enq = Wire(init=io.enq.fire())
+ val do_deq = Wire(init=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 := Bool(true) }
+ when (empty) {
+ io.deq.bits := io.enq.bits
+ do_deq := Bool(false)
+ when (io.deq.ready) { do_enq := Bool(false) }
+ }
+ }
+
+ if (pipe) {
+ when (io.deq.ready) { io.enq.ready := Bool(true) }
+ }
+
+ val ptr_diff = enq_ptr.value - deq_ptr.value
+ if (isPow2(entries)) {
+ io.count := Cat(maybe_full && ptr_match, ptr_diff)
+ } else {
+ io.count := Mux(ptr_match,
+ Mux(maybe_full,
+ UInt(entries), UInt(0)),
+ Mux(deq_ptr.value > enq_ptr.value,
+ UInt(entries) + 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
+ *
+ * @returns 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. */
+ def apply[T <: Data](
+ enq: ReadyValidIO[T],
+ entries: Int = 2,
+ pipe: Boolean = false,
+ flow: Boolean = false): DecoupledIO[T] = {
+ val q = Module(new Queue(enq.bits.cloneType, 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.
+ */
+ def irrevocable[T <: Data](
+ enq: ReadyValidIO[T],
+ entries: Int = 2,
+ pipe: Boolean = false,
+ flow: Boolean = false): IrrevocableIO[T] = {
+ 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
+ }
+}
diff --git a/src/main/scala/chisel3/util/Enum.scala b/src/main/scala/chisel3/util/Enum.scala
new file mode 100644
index 00000000..55b595ee
--- /dev/null
+++ b/src/main/scala/chisel3/util/Enum.scala
@@ -0,0 +1,53 @@
+// See LICENSE for license details.
+
+/** Enum generators, allowing circuit constants to have more meaningful names.
+ */
+
+package chisel3.util
+
+import chisel3._
+
+object Enum {
+ /** Returns a sequence of Bits subtypes with values from 0 until n. Helper method. */
+ private def createValues[T <: Bits](nodeType: T, n: Int): Seq[T] =
+ (0 until n).map(x => nodeType.fromInt(x, log2Up(n)))
+
+ /** Returns n unique values of the specified type. Can be used with unpacking to define enums.
+ *
+ * @example {{{
+ * val state_on :: state_off :: Nil = Enum(UInt(), 2)
+ * val current_state = UInt()
+ * switch (current_state) {
+ * is (state_on) {
+ * ...
+ * }
+ * if (state_off) {
+ * ...
+ * }
+ * }
+ * }}}
+ *
+ */
+ def apply[T <: Bits](nodeType: T, n: Int): List[T] = createValues(nodeType, n).toList
+
+ /** Returns a map of the input symbols to unique values of the specified type.
+ *
+ * @example {{{
+ * val states = Enum(UInt(), 'on, 'off)
+ * val current_state = UInt()
+ * switch (current_state) {
+ * is (states('on)) {
+ * ...
+ * }
+ * if (states('off)) {
+ * ..
+ * }
+ * }
+ * }}}
+ */
+ def apply[T <: Bits](nodeType: T, l: Symbol *): Map[Symbol, T] = (l zip createValues(nodeType, l.length)).toMap
+
+ /** Returns a map of the input symbols to unique values of the specified type.
+ */
+ def apply[T <: Bits](nodeType: T, l: List[Symbol]): Map[Symbol, T] = (l zip createValues(nodeType, l.length)).toMap
+}
diff --git a/src/main/scala/chisel3/util/ImplicitConversions.scala b/src/main/scala/chisel3/util/ImplicitConversions.scala
new file mode 100644
index 00000000..4d816a19
--- /dev/null
+++ b/src/main/scala/chisel3/util/ImplicitConversions.scala
@@ -0,0 +1,10 @@
+// See LICENSE for license details.
+
+package chisel3.util
+
+import chisel3._
+
+object ImplicitConversions {
+ implicit def intToUInt(x: Int): UInt = UInt(x)
+ implicit def booleanToBool(x: Boolean): Bool = Bool(x)
+}
diff --git a/src/main/scala/chisel3/util/LFSR.scala b/src/main/scala/chisel3/util/LFSR.scala
new file mode 100644
index 00000000..fedbf194
--- /dev/null
+++ b/src/main/scala/chisel3/util/LFSR.scala
@@ -0,0 +1,26 @@
+// See LICENSE for license details.
+
+/** LFSRs in all shapes and sizes.
+ */
+
+package chisel3.util
+
+import chisel3._
+//import chisel3.core.ExplicitCompileOptions.Strict
+
+// scalastyle:off magic.number
+object LFSR16 {
+ /** Generates a 16-bit linear feedback shift register, returning the register contents.
+ * May be useful for generating a pseudorandom sequence.
+ *
+ * @param increment optional control to gate when the LFSR updates.
+ */
+ def apply(increment: Bool = Bool(true)): UInt = {
+ val width = 16
+ val lfsr = Reg(init=UInt(1, width))
+ when (increment) { lfsr := Cat(lfsr(0)^lfsr(2)^lfsr(3)^lfsr(5), lfsr(width-1,1)) }
+ lfsr
+ }
+}
+// scalastyle:on magic.number
+
diff --git a/src/main/scala/chisel3/util/Lookup.scala b/src/main/scala/chisel3/util/Lookup.scala
new file mode 100644
index 00000000..9e909c0c
--- /dev/null
+++ b/src/main/scala/chisel3/util/Lookup.scala
@@ -0,0 +1,19 @@
+// See LICENSE for license details.
+
+package chisel3.util
+
+import chisel3._
+
+object ListLookup {
+ def apply[T <: Data](addr: UInt, default: List[T], mapping: Array[(BitPat, List[T])]): List[T] = {
+ val map = mapping.map(m => (m._1 === addr, m._2))
+ default.zipWithIndex map { case (d, i) =>
+ map.foldRight(d)((m, n) => Mux(m._1, m._2(i), n))
+ }
+ }
+}
+
+object Lookup {
+ def apply[T <: Bits](addr: UInt, default: T, mapping: Seq[(BitPat, T)]): T =
+ ListLookup(addr, List(default), mapping.map(m => (m._1, List(m._2))).toArray).head
+}
diff --git a/src/main/scala/chisel3/util/Math.scala b/src/main/scala/chisel3/util/Math.scala
new file mode 100644
index 00000000..73665f0f
--- /dev/null
+++ b/src/main/scala/chisel3/util/Math.scala
@@ -0,0 +1,44 @@
+// See LICENSE for license details.
+
+/** Scala-land math helper functions, like logs.
+ */
+
+package chisel3.util
+
+import chisel3._
+
+/** Compute the log2 rounded up with min value of 1 */
+object log2Up {
+ def apply(in: BigInt): Int = {
+ require(in >= 0)
+ 1 max (in-1).bitLength
+ }
+ def apply(in: Int): Int = apply(BigInt(in))
+}
+
+/** Compute the log2 rounded up */
+object log2Ceil {
+ def apply(in: BigInt): Int = {
+ require(in > 0)
+ (in-1).bitLength
+ }
+ def apply(in: Int): Int = apply(BigInt(in))
+}
+
+/** Compute the log2 rounded down with min value of 1 */
+object log2Down {
+ def apply(in: BigInt): Int = log2Up(in) - (if (isPow2(in)) 0 else 1)
+ def apply(in: Int): Int = apply(BigInt(in))
+}
+
+/** Compute the log2 rounded down */
+object log2Floor {
+ def apply(in: BigInt): Int = log2Ceil(in) - (if (isPow2(in)) 0 else 1)
+ def apply(in: Int): Int = apply(BigInt(in))
+}
+
+/** Check if an Integer is a power of 2 */
+object isPow2 {
+ def apply(in: BigInt): Boolean = in > 0 && ((in & (in-1)) == 0)
+ def apply(in: Int): Boolean = apply(BigInt(in))
+}
diff --git a/src/main/scala/chisel3/util/Mux.scala b/src/main/scala/chisel3/util/Mux.scala
new file mode 100644
index 00000000..245de67e
--- /dev/null
+++ b/src/main/scala/chisel3/util/Mux.scala
@@ -0,0 +1,65 @@
+// See LICENSE for license details.
+
+/** Mux circuit generators.
+ */
+
+package chisel3.util
+
+import chisel3._
+import chisel3.core.SeqUtils
+
+/** Builds a Mux tree out of the input signal vector using a one hot encoded
+ * select signal. Returns the output of the Mux tree.
+ *
+ * @note results undefined if multiple select signals are simultaneously high
+ */
+object Mux1H {
+ def apply[T <: Data](sel: Seq[Bool], in: Seq[T]): T =
+ apply(sel zip in)
+ def apply[T <: Data](in: Iterable[(Bool, T)]): T = SeqUtils.oneHotMux(in)
+ def apply[T <: Data](sel: UInt, in: Seq[T]): T =
+ apply((0 until in.size).map(sel(_)), in)
+ def apply(sel: UInt, in: UInt): Bool = (sel & in).orR
+}
+
+/** Builds a Mux tree under the assumption that multiple select signals
+ * can be enabled. Priority is given to the first select signal.
+ *
+ * Returns the output of the Mux tree.
+ */
+object PriorityMux {
+ def apply[T <: Data](in: Seq[(Bool, T)]): T = SeqUtils.priorityMux(in)
+ def apply[T <: Data](sel: Seq[Bool], in: Seq[T]): T = apply(sel zip in)
+ def apply[T <: Data](sel: Bits, in: Seq[T]): T = apply((0 until in.size).map(sel(_)), in)
+}
+
+/** Creates a cascade of n Muxs to search for a key value. */
+object MuxLookup {
+ /** @param key a key to search for
+ * @param default a default value if nothing is found
+ * @param mapping a sequence to search of keys and values
+ * @return the value found or the default if not
+ */
+ def apply[S <: UInt, T <: Data] (key: S, default: T, mapping: Seq[(S, T)]): T = {
+ var res = default
+ for ((k, v) <- mapping.reverse)
+ res = Mux(k === key, v, res)
+ res
+ }
+}
+
+/** Given an association of values to enable signals, returns the first value with an associated
+ * high enable signal.
+ */
+object MuxCase {
+ /** @param default the default value if none are enabled
+ * @param mapping a set of data values with associated enables
+ * @return the first value in mapping that is enabled */
+ def apply[T <: Data] (default: T, mapping: Seq[(Bool, T)]): T = {
+ var res = default
+ for ((t, v) <- mapping.reverse){
+ res = Mux(t, v, res)
+ }
+ res
+ }
+}
diff --git a/src/main/scala/chisel3/util/OneHot.scala b/src/main/scala/chisel3/util/OneHot.scala
new file mode 100644
index 00000000..53ba8c3d
--- /dev/null
+++ b/src/main/scala/chisel3/util/OneHot.scala
@@ -0,0 +1,66 @@
+// See LICENSE for license details.
+
+/** Circuit generators for working with one-hot representations.
+ */
+
+package chisel3.util
+
+import chisel3._
+
+/** Returns the bit position of the sole high bit of the input bitvector.
+ *
+ * Inverse operation of [[UIntToOH]].
+ *
+ * @note assumes exactly one high bit, results undefined otherwise
+ */
+object OHToUInt {
+ def apply(in: Seq[Bool]): UInt = apply(Cat(in.reverse), in.size)
+ def apply(in: Vec[Bool]): UInt = apply(in.asUInt, in.size)
+ def apply(in: Bits): UInt = apply(in, in.getWidth)
+
+ def apply(in: Bits, width: Int): UInt = {
+ if (width <= 2) {
+ Log2(in, width)
+ } else {
+ val mid = 1 << (log2Up(width)-1)
+ val hi = in(width-1, mid)
+ val lo = in(mid-1, 0)
+ Cat(hi.orR, apply(hi | lo, mid))
+ }
+ }
+}
+
+/** Returns the bit position of the least-significant high bit of the input bitvector.
+ *
+ * Multiple bits may be high in the input.
+ */
+object PriorityEncoder {
+ def apply(in: Seq[Bool]): UInt = PriorityMux(in, (0 until in.size).map(UInt(_)))
+ def apply(in: Bits): UInt = apply(in.toBools)
+}
+
+/** Returns the one hot encoding of the input UInt.
+ */
+object UIntToOH {
+ def apply(in: UInt, width: Int = -1): UInt =
+ if (width == -1) {
+ UInt(1) << in
+ } else {
+ (UInt(1) << in(log2Up(width)-1,0))(width-1,0)
+ }
+}
+
+/** Returns a bit vector in which only the least-significant 1 bit in the input vector, if any,
+ * is set.
+ */
+object PriorityEncoderOH {
+ private def encode(in: Seq[Bool]): UInt = {
+ val outs = Seq.tabulate(in.size)(i => UInt(BigInt(1) << i, in.size))
+ PriorityMux(in :+ Bool(true), outs :+ UInt(0, in.size))
+ }
+ def apply(in: Seq[Bool]): Seq[Bool] = {
+ val enc = encode(in)
+ Seq.tabulate(in.size)(enc(_))
+ }
+ def apply(in: Bits): UInt = encode((0 until in.getWidth).map(i => in(i)))
+}
diff --git a/src/main/scala/chisel3/util/Reg.scala b/src/main/scala/chisel3/util/Reg.scala
new file mode 100644
index 00000000..713a3b2e
--- /dev/null
+++ b/src/main/scala/chisel3/util/Reg.scala
@@ -0,0 +1,66 @@
+// See LICENSE for license details.
+
+package chisel3.util
+
+import chisel3._
+// TODO: remove this once we have CompileOptions threaded through the macro system.
+import chisel3.core.ExplicitCompileOptions.NotStrict
+
+object RegNext {
+ /** Returns a register with the specified next and no reset initialization.
+ *
+ * Essentially a 1-cycle delayed version of the input signal.
+ */
+ def apply[T <: Data](next: T): T = Reg[T](null.asInstanceOf[T], next, null.asInstanceOf[T])
+
+ /** Returns a register with the specified next and reset initialization.
+ *
+ * Essentially a 1-cycle delayed version of the input signal.
+ */
+ def apply[T <: Data](next: T, init: T): T = Reg[T](null.asInstanceOf[T], next, init)
+}
+
+object RegInit {
+ /** Returns a register pre-initialized (on reset) to the specified value.
+ */
+ def apply[T <: Data](init: T): T = Reg[T](null.asInstanceOf[T], null.asInstanceOf[T], init)
+}
+
+object RegEnable {
+ /** Returns a register with the specified next, update enable gate, and no reset initialization.
+ */
+ def apply[T <: Data](updateData: T, enable: Bool): T = {
+ val clonedUpdateData = updateData.chiselCloneType
+ val r = Reg(clonedUpdateData)
+ when (enable) { r := updateData }
+ r
+ }
+
+ /** Returns a register with the specified next, update enable gate, and reset initialization.
+ */
+ def apply[T <: Data](updateData: T, resetData: T, enable: Bool): T = {
+ val r = RegInit(resetData)
+ when (enable) { r := updateData }
+ r
+ }
+}
+
+object ShiftRegister
+{
+ /** Returns the n-cycle delayed version of the input signal.
+ *
+ * @param in input to delay
+ * @param n number of cycles to delay
+ * @param en enable the shift
+ */
+ def apply[T <: Data](in: T, n: Int, en: Bool = Bool(true)): T = {
+ // The order of tests reflects the expected use cases.
+ if (n == 1) {
+ RegEnable(in, en)
+ } else if (n != 0) {
+ RegNext(apply(in, n-1, en))
+ } else {
+ in
+ }
+ }
+}
diff --git a/src/main/scala/chisel3/util/TransitName.scala b/src/main/scala/chisel3/util/TransitName.scala
new file mode 100644
index 00000000..ce6cb60f
--- /dev/null
+++ b/src/main/scala/chisel3/util/TransitName.scala
@@ -0,0 +1,24 @@
+// See LICENSE for license details.
+
+package chisel3.util
+
+import chisel3._
+import internal.HasId
+
+object TransitName {
+ // The purpose of this is to allow a library to 'move' a name call to a more
+ // appropriate place.
+ // For example, a library factory function may create a module and return
+ // the io. The only user-exposed field is that given IO, which can't use
+ // any name supplied by the user. This can add a hook so that the supplied
+ // name then names the Module.
+ // See Queue companion object for working example
+ def apply[T<:HasId](from: T, to: HasId): T = {
+ from.addPostnameHook((given_name: String) => {to.suggestName(given_name)})
+ from
+ }
+ def withSuffix[T<:HasId](suffix: String)(from: T, to: HasId): T = {
+ from.addPostnameHook((given_name: String) => {to.suggestName(given_name + suffix)})
+ from
+ }
+}
diff --git a/src/main/scala/chisel3/util/Valid.scala b/src/main/scala/chisel3/util/Valid.scala
new file mode 100644
index 00000000..3d153a2a
--- /dev/null
+++ b/src/main/scala/chisel3/util/Valid.scala
@@ -0,0 +1,61 @@
+// See LICENSE for license details.
+
+/** Wrappers for valid interfaces and associated circuit generators using them.
+ */
+
+package chisel3.util
+
+import chisel3._
+// TODO: remove this once we have CompileOptions threaded through the macro system.
+import chisel3.core.ExplicitCompileOptions.NotStrict
+
+/** An Bundle containing data and a signal determining if it is valid */
+class Valid[+T <: Data](gen: T) extends Bundle
+{
+ val valid = Output(Bool())
+ val bits = Output(gen.chiselCloneType)
+ def fire(dummy: Int = 0): Bool = valid
+ override def cloneType: this.type = Valid(gen).asInstanceOf[this.type]
+}
+
+/** Adds a valid protocol to any interface */
+object Valid {
+ def apply[T <: Data](gen: T): Valid[T] = new Valid(gen)
+}
+
+/** A hardware module that delays data coming down the pipeline
+ by the number of cycles set by the latency parameter. Functionality
+ is similar to ShiftRegister but this exposes a Pipe interface.
+
+ Example usage:
+ val pipe = new Pipe(UInt())
+ pipe.io.enq <> produce.io.out
+ consumer.io.in <> pipe.io.deq
+ */
+object Pipe
+{
+ def apply[T <: Data](enqValid: Bool, enqBits: T, latency: Int): Valid[T] = {
+ if (latency == 0) {
+ val out = Wire(Valid(enqBits))
+ out.valid <> enqValid
+ out.bits <> enqBits
+ out
+ } else {
+ val v = Reg(Bool(), next=enqValid, init=Bool(false))
+ val b = RegEnable(enqBits, enqValid)
+ apply(v, b, latency-1)
+ }
+ }
+ def apply[T <: Data](enqValid: Bool, enqBits: T): Valid[T] = apply(enqValid, enqBits, 1)
+ def apply[T <: Data](enq: Valid[T], latency: Int = 1): Valid[T] = apply(enq.valid, enq.bits, latency)
+}
+
+class Pipe[T <: Data](gen: T, latency: Int = 1) extends Module
+{
+ val io = IO(new Bundle {
+ val enq = Input(Valid(gen))
+ val deq = Output(Valid(gen))
+ })
+
+ io.deq <> Pipe(io.enq, latency)
+}
diff --git a/src/main/scala/chisel3/util/util.scala b/src/main/scala/chisel3/util/util.scala
new file mode 100644
index 00000000..812af21c
--- /dev/null
+++ b/src/main/scala/chisel3/util/util.scala
@@ -0,0 +1,12 @@
+// See LICENSE for license details.
+
+package chisel3
+
+package object util {
+
+ /** Synonyms, moved from main package object - maintain scope. */
+ type ValidIO[+T <: Data] = chisel3.util.Valid[T]
+ val ValidIO = chisel3.util.Valid
+ val DecoupledIO = chisel3.util.Decoupled
+
+}