2017-08-25 01:47:15 +02:00
|
|
|
// See LICENSE.SiFive for license details.
|
|
|
|
|
|
|
|
package freechips.rocketchip.util
|
|
|
|
|
|
|
|
import Chisel._
|
|
|
|
|
2017-09-06 02:32:53 +02:00
|
|
|
// Similar to the Chisel ShiftRegister but allows the user to suggest a
|
|
|
|
// name to the registers that get instantiated, and
|
|
|
|
// to provide a reset value.
|
|
|
|
object ShiftRegInit {
|
|
|
|
def apply[T <: Data](in: T, n: Int, init: T, name: Option[String] = None): T =
|
2017-09-06 19:37:59 +02:00
|
|
|
(0 until n).foldLeft(in) {
|
|
|
|
case (next, i) => {
|
|
|
|
val r = Reg(next, next = next, init = init)
|
|
|
|
name.foreach { na => r.suggestName(s"${na}_${i}") }
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
2017-09-06 02:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** These wrap behavioral
|
2017-09-06 19:37:59 +02:00
|
|
|
* shift registers into specific modules to allow for
|
2017-08-25 01:47:15 +02:00
|
|
|
* backend flows to replace or constrain
|
|
|
|
* them properly when used for CDC synchronization,
|
|
|
|
* rather than buffering.
|
|
|
|
*
|
2017-09-06 19:37:59 +02:00
|
|
|
* The different types vary in their reset behavior:
|
2017-08-30 21:59:16 +02:00
|
|
|
* AsyncResetShiftReg -- This is identical to the AsyncResetSynchronizerShiftReg,
|
2017-09-06 19:37:59 +02:00
|
|
|
* it is just named differently to distinguish its use case.
|
|
|
|
* This is an async ShiftRegister meant for timing,
|
2017-08-30 21:59:16 +02:00
|
|
|
* not for synchronization.
|
2017-09-06 19:37:59 +02:00
|
|
|
* AsyncResetSynchronizerShiftReg -- asynchronously reset to specific value.
|
|
|
|
* SyncResetSynchronizerShiftReg -- reset to specific value.
|
|
|
|
* SynchronizerShiftReg -- no reset, pipeline only.
|
2017-08-25 01:47:15 +02:00
|
|
|
*/
|
|
|
|
|
2017-08-30 21:59:16 +02:00
|
|
|
abstract class AbstractPipelineReg(w: Int = 1) extends Module {
|
2017-08-25 01:47:15 +02:00
|
|
|
val io = new Bundle {
|
|
|
|
val d = UInt(INPUT, width = w)
|
|
|
|
val q = UInt(OUTPUT, width = w)
|
|
|
|
}
|
2017-08-25 02:18:04 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 21:59:16 +02:00
|
|
|
object AbstractPipelineReg {
|
|
|
|
def apply [T <: Chisel.Data](gen: => AbstractPipelineReg, in: T, name: Option[String] = None): T = {
|
|
|
|
val chain = Module(gen)
|
|
|
|
name.foreach{ chain.suggestName(_) }
|
|
|
|
chain.io.d := in.asUInt
|
|
|
|
chain.io.q.asTypeOf(in)
|
2017-08-25 02:18:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 21:59:16 +02:00
|
|
|
class AsyncResetShiftReg(w: Int = 1, depth: Int = 1, init: Int = 0, name: String = "pipe") extends AbstractPipelineReg(w) {
|
|
|
|
require(depth > 0, "Depth must be greater than 0.")
|
2017-08-25 02:18:04 +02:00
|
|
|
|
2017-08-30 21:59:16 +02:00
|
|
|
override def desiredName = s"AsyncResetShiftReg_w${w}_d${depth}_i${init}"
|
2017-08-25 02:18:04 +02:00
|
|
|
|
2017-08-30 21:59:16 +02:00
|
|
|
val chain = List.tabulate(depth) { i =>
|
|
|
|
Module (new AsyncResetRegVec(w, init)).suggestName(s"${name}_${i}")
|
2017-08-25 01:47:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 21:59:16 +02:00
|
|
|
chain.last.io.d := io.d
|
|
|
|
chain.last.io.en := Bool(true)
|
2017-08-25 01:47:15 +02:00
|
|
|
|
2017-08-30 21:59:16 +02:00
|
|
|
(chain.init zip chain.tail).foreach { case (sink, source) =>
|
2017-08-25 01:47:15 +02:00
|
|
|
sink.io.d := source.io.q
|
|
|
|
sink.io.en := Bool(true)
|
|
|
|
}
|
2017-08-30 21:59:16 +02:00
|
|
|
io.q := chain.head.io.q
|
|
|
|
}
|
|
|
|
|
|
|
|
object AsyncResetShiftReg {
|
2017-09-06 19:37:59 +02:00
|
|
|
def apply [T <: Chisel.Data](in: T, depth: Int, init: Int = 0, name: Option[String] = None): T =
|
2017-09-06 00:16:08 +02:00
|
|
|
AbstractPipelineReg(new AsyncResetShiftReg(in.getWidth, depth, init), in, name)
|
2017-09-06 19:37:59 +02:00
|
|
|
|
|
|
|
def apply [T <: Chisel.Data](in: T, depth: Int, name: Option[String]): T =
|
|
|
|
apply(in, depth, 0, name)
|
|
|
|
|
|
|
|
def apply [T <: Chisel.Data](in: T, depth: Int, init: T, name: Option[String]): T =
|
|
|
|
apply(in, depth, init.litValue.toInt, name)
|
|
|
|
|
|
|
|
def apply [T <: Chisel.Data](in: T, depth: Int, init: T): T =
|
|
|
|
apply (in, depth, init.litValue.toInt, None)
|
2017-08-30 21:59:16 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 00:16:08 +02:00
|
|
|
// Note that it is important to ovveride "name" in order to ensure that the Chisel dedup does
|
2017-08-30 21:59:16 +02:00
|
|
|
// not try to merge instances of this with instances of the superclass.
|
2017-09-06 19:37:59 +02:00
|
|
|
class AsyncResetSynchronizerShiftReg(w: Int = 1, sync: Int = 3, init: Int = 0) extends AsyncResetShiftReg(w, depth = sync, init, name = "sync") {
|
2017-08-30 21:59:16 +02:00
|
|
|
require(sync > 0, "Sync must be greater than 0.")
|
2017-09-06 19:37:59 +02:00
|
|
|
override def desiredName = s"AsyncResetSynchronizerShiftReg_w${w}_d${sync}_i${init}"
|
2017-08-25 01:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
object AsyncResetSynchronizerShiftReg {
|
2017-09-06 19:37:59 +02:00
|
|
|
def apply [T <: Chisel.Data](in: T, depth: Int, init: Int = 0, name: Option[String] = None): T =
|
|
|
|
AbstractPipelineReg(new AsyncResetSynchronizerShiftReg(in.getWidth, depth, init), in, name)
|
|
|
|
|
|
|
|
def apply [T <: Chisel.Data](in: T, depth: Int, name: Option[String]): T =
|
|
|
|
apply(in, depth, 0, name)
|
|
|
|
|
|
|
|
def apply [T <: Chisel.Data](in: T, depth: Int, init: T, name: Option[String]): T =
|
|
|
|
apply(in, depth, init.litValue.toInt, name)
|
|
|
|
|
|
|
|
def apply [T <: Chisel.Data](in: T, depth: Int, init: T): T =
|
|
|
|
apply (in, depth, init.litValue.toInt, None)
|
2017-08-25 01:47:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 21:59:16 +02:00
|
|
|
class SynchronizerShiftReg(w: Int = 1, sync: Int = 3) extends AbstractPipelineReg(w) {
|
2017-08-30 21:33:03 +02:00
|
|
|
require(sync > 0, "Sync must be greater than 0.")
|
2017-08-25 01:47:15 +02:00
|
|
|
|
|
|
|
override def desiredName = s"SynchronizerShiftReg_w${w}_d${sync}"
|
|
|
|
|
|
|
|
val syncv = List.tabulate(sync) { i =>
|
|
|
|
val r = Reg(UInt(width = w))
|
|
|
|
r.suggestName(s"sync_${i}")
|
|
|
|
}
|
|
|
|
|
|
|
|
syncv.last := io.d
|
|
|
|
|
|
|
|
(syncv.init zip syncv.tail).foreach { case (sink, source) =>
|
|
|
|
sink := source
|
|
|
|
}
|
|
|
|
io.q := syncv.head
|
|
|
|
}
|
|
|
|
|
2017-09-06 19:37:59 +02:00
|
|
|
|
2017-08-25 01:47:15 +02:00
|
|
|
object SynchronizerShiftReg {
|
2017-08-25 02:18:04 +02:00
|
|
|
def apply [T <: Chisel.Data](in: T, sync: Int = 3, name: Option[String] = None): T =
|
2017-09-06 00:16:08 +02:00
|
|
|
AbstractPipelineReg(new SynchronizerShiftReg(in.getWidth, sync), in, name)
|
2017-08-25 01:47:15 +02:00
|
|
|
}
|
2017-09-06 19:37:59 +02:00
|
|
|
|
|
|
|
class SyncResetSynchronizerShiftReg(w: Int = 1, sync: Int = 3, init: Int = 0) extends AbstractPipelineReg(w) {
|
|
|
|
require (sync >= 0, "Sync must be greater than or equal to 0")
|
|
|
|
|
|
|
|
override def desiredName = s"SyncResetSynchronizerShiftReg_w${w}_d${sync}_i${init}"
|
|
|
|
|
|
|
|
io.q := ShiftRegInit(io.d, n = sync, init = init.U, name = Some("sync"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
object SyncResetSynchronizerShiftReg {
|
|
|
|
def apply [T <: Chisel.Data](in: T, sync: Int = 3, init: T, name: Option[String] = None): T =
|
|
|
|
AbstractPipelineReg(new SyncResetSynchronizerShiftReg(in.getWidth, sync, init.litValue.toInt), in, name)
|
|
|
|
}
|