2017-08-25 01:47:15 +02:00
|
|
|
// See LICENSE.SiFive for license details.
|
|
|
|
|
|
|
|
package freechips.rocketchip.util
|
|
|
|
|
|
|
|
import Chisel._
|
|
|
|
|
|
|
|
/** These wrap behavioral
|
|
|
|
* shift registers into specific
|
|
|
|
* modules to allow for
|
|
|
|
* backend flows to replace or constrain
|
|
|
|
* them properly when used for CDC synchronization,
|
|
|
|
* rather than buffering.
|
|
|
|
*
|
|
|
|
* The 3 different types vary in their reset behavior:
|
|
|
|
* AsyncResetSynchronizerShiftReg -- asynchronously reset to 0
|
|
|
|
* SynchronizerShiftReg -- no reset, pipeline only.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-08-25 02:18:04 +02:00
|
|
|
abstract class AbstractSynchronizerReg(w: Int = 1, sync: Int = 3) extends Module {
|
2017-08-25 01:47:15 +02:00
|
|
|
require(sync > 0, "Sync must be greater than 0.")
|
|
|
|
|
|
|
|
val io = new Bundle {
|
|
|
|
val d = UInt(INPUT, width = w)
|
|
|
|
val q = UInt(OUTPUT, width = w)
|
|
|
|
}
|
|
|
|
|
2017-08-25 02:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
object AbstractSynchronizerReg {
|
|
|
|
|
2017-08-30 21:00:14 +02:00
|
|
|
def apply [T <: Chisel.Data](gen: => AbstractSynchronizerReg, in: T, sync: Int = 3, name: Option[String] = None): T = {
|
|
|
|
val sync_chain = Module(gen)
|
2017-08-25 02:34:07 +02:00
|
|
|
name.foreach{ sync_chain.suggestName(_) }
|
|
|
|
sync_chain.io.d := in.asUInt
|
|
|
|
|
2017-08-30 21:00:14 +02:00
|
|
|
sync_chain.io.q.asTypeOf(in)
|
2017-08-25 02:18:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 20:50:25 +02:00
|
|
|
class AsyncResetSynchronizerShiftReg(w: Int = 1, sync: Int = 3) extends AbstractSynchronizerReg(w, sync) {
|
2017-08-25 02:18:04 +02:00
|
|
|
|
|
|
|
override def desiredName = s"AsyncResetSynchronizerShiftReg_w${w}_d${sync}"
|
|
|
|
|
2017-08-25 01:47:15 +02:00
|
|
|
val syncv = List.tabulate(sync) { i =>
|
|
|
|
Module (new AsyncResetRegVec(w, 0)).suggestName(s"sync_${i}")
|
|
|
|
}
|
|
|
|
|
|
|
|
syncv.last.io.d := io.d
|
|
|
|
syncv.last.io.en := Bool(true)
|
|
|
|
|
|
|
|
(syncv.init zip syncv.tail).foreach { case (sink, source) =>
|
|
|
|
sink.io.d := source.io.q
|
|
|
|
sink.io.en := Bool(true)
|
|
|
|
}
|
|
|
|
io.q := syncv.head.io.q
|
|
|
|
}
|
|
|
|
|
|
|
|
object AsyncResetSynchronizerShiftReg {
|
|
|
|
|
2017-08-25 02:18:04 +02:00
|
|
|
def apply [T <: Chisel.Data](in: T, sync: Int = 3, name: Option[String] = None): T =
|
2017-08-30 21:00:14 +02:00
|
|
|
AbstractSynchronizerReg(gen = {new AsyncResetSynchronizerShiftReg(in.getWidth, sync)},
|
2017-08-25 02:18:04 +02:00
|
|
|
in, sync, name)
|
2017-08-25 01:47:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 20:50:25 +02:00
|
|
|
class SynchronizerShiftReg(w: Int = 1, sync: Int = 3) extends AbstractSynchronizerReg(w, sync) {
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-30 21:00:14 +02:00
|
|
|
AbstractSynchronizerReg(gen = { new SynchronizerShiftReg(in.getWidth, sync)},
|
2017-08-25 02:18:04 +02:00
|
|
|
in, sync, name)
|
2017-08-25 01:47:15 +02:00
|
|
|
}
|