1
0

syncreg: Refactor common code

This commit is contained in:
Megan Wachs 2017-08-24 17:18:04 -07:00
parent d83a6dc6af
commit c78ee9f0e4

View File

@ -18,17 +18,30 @@ import Chisel._
* *
*/ */
class AsyncResetSynchronizerShiftReg(w: Int = 1, sync: Int = 3) extends Module { abstract class AbstractSynchronizerReg(w: Int = 1, sync: Int = 3) extends Module {
require(sync > 0, "Sync must be greater than 0.") require(sync > 0, "Sync must be greater than 0.")
override def desiredName = s"AsyncResetSynchronizerShiftReg_w${w}_d${sync}"
val io = new Bundle { val io = new Bundle {
val d = UInt(INPUT, width = w) val d = UInt(INPUT, width = w)
val q = UInt(OUTPUT, width = w) val q = UInt(OUTPUT, width = w)
} }
}
object AbstractSynchronizerReg {
def apply [T <: Chisel.Data](gen: (Int, Int) => AbstractSynchronizerReg, in: T, sync: Int = 3, name: Option[String] = None): T = {
val sync_reg = Module(gen(in.getWidth, sync))
name.foreach{ sync_reg.suggestName(_) }
sync_reg.io.d := in.asUInt
(in.chiselCloneType).fromBits(sync_reg.io.q)
}
}
class AsyncResetSynchronizerShiftReg(w: Int = 1, sync: Int = 3) extends AbstractSynchronizerReg {
override def desiredName = s"AsyncResetSynchronizerShiftReg_w${w}_d${sync}"
val syncv = List.tabulate(sync) { i => val syncv = List.tabulate(sync) { i =>
Module (new AsyncResetRegVec(w, 0)).suggestName(s"sync_${i}") Module (new AsyncResetRegVec(w, 0)).suggestName(s"sync_${i}")
} }
@ -45,27 +58,15 @@ class AsyncResetSynchronizerShiftReg(w: Int = 1, sync: Int = 3) extends Module {
object AsyncResetSynchronizerShiftReg { object AsyncResetSynchronizerShiftReg {
def apply [T <: Chisel.Data](in: T, sync: Int = 3, name: Option[String] = None): T = { def apply [T <: Chisel.Data](in: T, sync: Int = 3, name: Option[String] = None): T =
val sync_reg = Module(new AsyncResetSynchronizerShiftReg(in.getWidth, sync)) AbstractSynchronizerReg(gen = (w: Int, sync: Int) => { new AsyncResetSynchronizerShiftReg(w, sync)},
name.foreach{ sync_reg.suggestName(_) } in, sync, name)
sync_reg.io.d := in.asUInt
(in.chiselCloneType).fromBits(sync_reg.io.q)
}
} }
class SynchronizerShiftRegInit(w: Int = 1, sync: Int = 3) extends Module { class SynchronizerShiftRegInit(w: Int = 1, sync: Int = 3) extends AbstractSynchronizerReg {
require(sync > 0, "Sync must be greater than 0.")
override def desiredName = s"SynchronizerShiftRegInit_w${w}_d${sync}" override def desiredName = s"SynchronizerShiftRegInit_w${w}_d${sync}"
val io = new Bundle {
val d = UInt(INPUT, width = w)
val q = UInt(OUTPUT, width = w)
}
val syncv = List.tabulate(sync) { i => val syncv = List.tabulate(sync) { i =>
val r = RegInit(UInt(0, width = w)) val r = RegInit(UInt(0, width = w))
r.suggestName(s"sync_${i}") r.suggestName(s"sync_${i}")
@ -83,27 +84,15 @@ class SynchronizerShiftRegInit(w: Int = 1, sync: Int = 3) extends Module {
object SynchronizerShiftRegInit { object SynchronizerShiftRegInit {
def apply [T <: Chisel.Data](in: T, sync: Int = 3, name: Option[String] = None): T = { def apply [T <: Chisel.Data](in: T, sync: Int = 3, name: Option[String] = None): T =
val sync_reg = Module(new SynchronizerShiftRegInit(in.getWidth, sync)) AbstractSynchronizerReg(gen = (w: Int, sync: Int) => { new SynchronizerShiftRegInit(w, sync)},
name.foreach{ sync_reg.suggestName(_) } in, sync, name)
sync_reg.io.d := in.asUInt
(in.chiselCloneType).fromBits(sync_reg.io.q)
}
} }
class SynchronizerShiftReg(w: Int = 1, sync: Int = 3) extends AbstractSynchronizerReg {
class SynchronizerShiftReg(w: Int = 1, sync: Int = 3) extends Module {
require(sync > 0, "Sync must be greater than 0.")
override def desiredName = s"SynchronizerShiftReg_w${w}_d${sync}" override def desiredName = s"SynchronizerShiftReg_w${w}_d${sync}"
val io = new Bundle {
val d = UInt(INPUT, width = w)
val q = UInt(OUTPUT, width = w)
}
val syncv = List.tabulate(sync) { i => val syncv = List.tabulate(sync) { i =>
val r = Reg(UInt(width = w)) val r = Reg(UInt(width = w))
r.suggestName(s"sync_${i}") r.suggestName(s"sync_${i}")
@ -120,11 +109,7 @@ class SynchronizerShiftReg(w: Int = 1, sync: Int = 3) extends Module {
object SynchronizerShiftReg { object SynchronizerShiftReg {
def apply [T <: Chisel.Data](in: T, sync: Int = 3, name: Option[String] = None): T = { def apply [T <: Chisel.Data](in: T, sync: Int = 3, name: Option[String] = None): T =
val sync_reg = Module(new SynchronizerShiftReg(in.getWidth, sync)) AbstractSynchronizerReg(gen = (w: Int, sync: Int) => { new SynchronizerShiftReg(w, sync)},
name.foreach{ sync_reg.suggestName(_) } in, sync, name)
sync_reg.io.d := in.asUInt
(in.chiselCloneType).fromBits(sync_reg.io.q)
}
} }