1
0

fix RRArbiter

This commit is contained in:
Andrew Waterman 2012-03-19 00:19:33 -07:00
parent ba06cd953e
commit 2ed0be65f9

View File

@ -166,55 +166,54 @@ class ioArbiter[T <: Data](n: Int)(data: => T) extends Bundle {
val chosen = Bits(log2up(n), OUTPUT) val chosen = Bits(log2up(n), OUTPUT)
} }
object ArbiterCtrl
{
def apply(request: Seq[Bool]) = {
Bool(true) +: (1 until request.length).map(i => !foldR(request.slice(0, i))(_||_))
}
}
class Arbiter[T <: Data](n: Int)(data: => T) extends Component { class Arbiter[T <: Data](n: Int)(data: => T) extends Component {
val io = new ioArbiter(n)(data) val io = new ioArbiter(n)(data)
io.in(0).ready := io.out.ready val grant = ArbiterCtrl(io.in.map(_.valid))
for (i <- 1 to n-1) { (0 until n).map(i => io.in(i).ready := grant(i) && io.out.ready)
io.in(i).ready := !io.in(i-1).valid && io.in(i-1).ready
}
var dout = io.in(n-1).bits var dout = io.in(n-1).bits
var choose = Bits(n-1) var choose = Bits(n-1)
for (i <- 1 to n-1) { for (i <- n-2 to 0 by -1) {
dout = Mux(io.in(n-1-i).valid, io.in(n-1-i).bits, dout) dout = Mux(io.in(i).valid, io.in(i).bits, dout)
choose = Mux(io.in(n-1-i).valid, Bits(n-1-i), choose) choose = Mux(io.in(i).valid, Bits(i), choose)
} }
var vout = io.in(0).valid io.out.valid := foldR(io.in.map(_.valid))(_||_)
for (i <- 1 to n-1) io.out.bits <> dout
vout = vout || io.in(i).valid io.chosen := choose
vout <> io.out.valid
dout <> io.out.bits
choose <> io.chosen
} }
class RRArbiter[T <: Data](n: Int)(data: => T) extends Component { class RRArbiter[T <: Data](n: Int)(data: => T) extends Component {
val io = new ioArbiter(n)(data) val io = new ioArbiter(n)(data)
val last_grant = Reg(resetVal = UFix(0, log2up(n))) val last_grant = Reg(resetVal = Bits(0, log2up(n)))
var valid = io.in(n-1).valid val g = ArbiterCtrl((0 until n).map(i => io.in(i).valid && UFix(i) > last_grant) ++ io.in.map(_.valid))
var next_grant = UFix(n-1) val grant = (0 until n).map(i => g(i) && UFix(i) > last_grant || g(i+n))
var mux = (new Mux1H(n)) { data } (0 until n).map(i => io.in(i).ready := grant(i) && io.out.ready)
for (i <- n-2 to 0 by -1) { var choose = Bits(n-1)
valid = valid || io.in(i).valid for (i <- n-2 to 0 by -1)
next_grant = Mux(io.in(i).valid, UFix(i), next_grant) choose = Mux(io.in(i).valid, Bits(i), choose)
}
for (i <- n-1 to 1 by -1) for (i <- n-1 to 1 by -1)
next_grant = Mux(last_grant < UFix(i) && io.in(i).valid, UFix(i), next_grant) choose = Mux(io.in(i).valid && UFix(i) > last_grant, Bits(i), choose)
for (i <- 0 until n) { when (io.out.valid && io.out.ready) {
mux.io.sel(i) := next_grant === UFix(i) last_grant := choose
mux.io.in(i) := io.in(i).bits
io.in(i).ready := io.out.ready && next_grant === UFix(i)
}
when (valid && io.out.ready) {
last_grant := next_grant
} }
io.out.valid := valid val dvec = Vec(n) { Wire() { data } }
io.out.bits := mux.io.out (0 until n).map(i => dvec(i) := io.in(i).bits )
io.out.valid := foldR(io.in.map(_.valid))(_||_)
io.out.bits := dvec(choose)
io.chosen := choose
} }
class ioLockingArbiter[T <: Data](n: Int)(data: => T) extends Bundle { class ioLockingArbiter[T <: Data](n: Int)(data: => T) extends Bundle {