summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/Arbiter.scala
diff options
context:
space:
mode:
authorJim Lawson2016-06-20 11:08:46 -0700
committerJim Lawson2016-06-20 11:08:46 -0700
commitd408d73a171535bd7c2ba9d0037c194022b8a62f (patch)
tree81885a99ec56e89532bc3fa338f22b163dcc4d1f /src/main/scala/chisel3/util/Arbiter.scala
parentb5a534914795d9d17f4dfe623525f1b804e4c60f (diff)
Rename chisel3 package.
Diffstat (limited to 'src/main/scala/chisel3/util/Arbiter.scala')
-rw-r--r--src/main/scala/chisel3/util/Arbiter.scala119
1 files changed, 119 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..3723f2a9
--- /dev/null
+++ b/src/main/scala/chisel3/util/Arbiter.scala
@@ -0,0 +1,119 @@
+// See LICENSE for license details.
+
+/** Arbiters in all shapes and sizes.
+ */
+
+package chisel.util
+
+import chisel._
+
+/** An I/O bundle for the Arbiter */
+class ArbiterIO[T <: Data](gen: T, n: Int) extends Bundle {
+ val in = Vec(n, Decoupled(gen)).flip
+ val out = Decoupled(gen)
+ val chosen = UInt(OUTPUT, 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 = 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 usage:
+ 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 usage:
+ 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 = 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
+}