2014-12-07 12:02:20 +01:00
|
|
|
// See LICENSE for license details.
|
|
|
|
|
|
|
|
package uncore
|
|
|
|
|
|
|
|
import Chisel._
|
2015-04-04 02:24:44 +02:00
|
|
|
|
2014-12-07 12:02:20 +01:00
|
|
|
object MuxBundle {
|
|
|
|
def apply[T <: Data] (default: T, mapping: Seq[(Bool, T)]): T = {
|
|
|
|
mapping.reverse.foldLeft(default)((b, a) => Mux(a._1, a._2, b))
|
|
|
|
}
|
2015-11-18 03:13:50 +01:00
|
|
|
|
2016-01-13 00:30:26 +01:00
|
|
|
def apply[T <: Data] (key: UInt, default: T, mapping: Seq[(UInt, T)]): T = {
|
2015-11-18 03:13:50 +01:00
|
|
|
apply(default, mapping.map{ case (a, b) => (a === key, b) })
|
|
|
|
}
|
2014-12-07 12:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Produces 0-width value when counting to 1
|
|
|
|
class ZCounter(val n: Int) {
|
|
|
|
val value = Reg(init=UInt(0, log2Ceil(n)))
|
|
|
|
def inc(): Bool = {
|
|
|
|
if (n == 1) Bool(true)
|
|
|
|
else {
|
|
|
|
val wrap = value === UInt(n-1)
|
|
|
|
value := Mux(Bool(!isPow2(n)) && wrap, UInt(0), value + UInt(1))
|
|
|
|
wrap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object ZCounter {
|
|
|
|
def apply(n: Int) = new ZCounter(n)
|
|
|
|
def apply(cond: Bool, n: Int): (UInt, Bool) = {
|
|
|
|
val c = new ZCounter(n)
|
|
|
|
var wrap: Bool = null
|
|
|
|
when (cond) { wrap = c.inc() }
|
|
|
|
(c.value, cond && wrap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 00:04:09 +01:00
|
|
|
object TwoWayCounter {
|
|
|
|
def apply(up: Bool, down: Bool, max: Int): UInt = {
|
|
|
|
val cnt = Reg(init = UInt(0, log2Up(max+1)))
|
|
|
|
when (up && !down) { cnt := cnt + UInt(1) }
|
|
|
|
when (down && !up) { cnt := cnt - UInt(1) }
|
|
|
|
cnt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 06:41:46 +02:00
|
|
|
class FlowThroughSerializer[T <: Bundle with HasTileLinkData](gen: T, n: Int) extends Module {
|
2014-12-07 12:02:20 +01:00
|
|
|
val io = new Bundle {
|
2015-07-16 03:06:27 +02:00
|
|
|
val in = Decoupled(gen).flip
|
|
|
|
val out = Decoupled(gen)
|
2014-12-07 12:02:20 +01:00
|
|
|
val cnt = UInt(OUTPUT, log2Up(n))
|
|
|
|
val done = Bool(OUTPUT)
|
|
|
|
}
|
2015-03-24 10:06:53 +01:00
|
|
|
val narrowWidth = io.in.bits.data.getWidth / n
|
|
|
|
require(io.in.bits.data.getWidth % narrowWidth == 0)
|
2014-12-07 12:02:20 +01:00
|
|
|
|
|
|
|
if(n == 1) {
|
2015-08-02 06:09:00 +02:00
|
|
|
io.out <> io.in
|
2015-07-31 08:45:48 +02:00
|
|
|
io.cnt := UInt(0)
|
2014-12-07 12:02:20 +01:00
|
|
|
io.done := Bool(true)
|
|
|
|
} else {
|
|
|
|
val cnt = Reg(init=UInt(0, width = log2Up(n)))
|
|
|
|
val wrap = cnt === UInt(n-1)
|
2015-07-16 03:06:27 +02:00
|
|
|
val rbits = Reg{io.in.bits}
|
2014-12-07 12:02:20 +01:00
|
|
|
val active = Reg(init=Bool(false))
|
|
|
|
|
2016-03-11 00:50:44 +01:00
|
|
|
val shifter = Wire(Vec(n, Bits(width = narrowWidth)))
|
2014-12-07 12:02:20 +01:00
|
|
|
(0 until n).foreach {
|
2015-03-24 10:06:53 +01:00
|
|
|
i => shifter(i) := rbits.data((i+1)*narrowWidth-1,i*narrowWidth)
|
2014-12-07 12:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
io.done := Bool(false)
|
|
|
|
io.cnt := cnt
|
|
|
|
io.in.ready := !active
|
|
|
|
io.out.valid := active || io.in.valid
|
|
|
|
io.out.bits := io.in.bits
|
|
|
|
when(!active && io.in.valid) {
|
2015-03-24 10:06:53 +01:00
|
|
|
when(io.in.bits.hasData()) {
|
2014-12-07 12:02:20 +01:00
|
|
|
cnt := Mux(io.out.ready, UInt(1), UInt(0))
|
|
|
|
rbits := io.in.bits
|
|
|
|
active := Bool(true)
|
|
|
|
}
|
2015-03-24 10:06:53 +01:00
|
|
|
io.done := !io.in.bits.hasData()
|
2014-12-07 12:02:20 +01:00
|
|
|
}
|
|
|
|
when(active) {
|
|
|
|
io.out.bits := rbits
|
2015-03-24 10:06:53 +01:00
|
|
|
io.out.bits.data := shifter(cnt)
|
2014-12-07 12:02:20 +01:00
|
|
|
when(io.out.ready) {
|
|
|
|
cnt := cnt + UInt(1)
|
|
|
|
when(wrap) {
|
|
|
|
cnt := UInt(0)
|
|
|
|
io.done := Bool(true)
|
|
|
|
active := Bool(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 10:06:53 +01:00
|
|
|
|
|
|
|
object FlowThroughSerializer {
|
2015-10-06 06:41:46 +02:00
|
|
|
def apply[T <: Bundle with HasTileLinkData](in: DecoupledIO[T], n: Int): DecoupledIO[T] = {
|
2015-03-24 10:06:53 +01:00
|
|
|
val fs = Module(new FlowThroughSerializer(in.bits, n))
|
|
|
|
fs.io.in.valid := in.valid
|
|
|
|
fs.io.in.bits := in.bits
|
|
|
|
in.ready := fs.io.in.ready
|
|
|
|
fs.io.out
|
|
|
|
}
|
2015-10-20 02:25:33 +02:00
|
|
|
}
|