建立双边沿采样Flip-Flop底层逻辑

This commit is contained in:
RuigeLee
2025-09-18 17:55:50 +08:00
parent fb26a40061
commit 2e19a95b92

View File

@@ -0,0 +1,74 @@
package BACK
import chisel3._
import chisel3.util._
class DDRegInit[T<:Data](init: T, pclk: Clock, nclk: Clock, reset: AsyncReset){
private val pReg = withClockAndReset(pclk, reset) { RegInit( init ) }
private val nReg = withClockAndReset(nclk, reset) { RegInit( 0.U.asTypeOf(init) ) }
val value: T = (pReg.asUInt ^ nReg.asUInt).asTypeOf(init)
private val next = WireDefault(value)
def :=(d:T): Unit = { next := d }
pReg := (nReg.asUInt ^ next.asUInt).asTypeOf(init)
nReg := (pReg.asUInt ^ next.asUInt).asTypeOf(init)
def apply(): T = value
}
object DDRegInit{
def apply[T<:Data](init: T, pclk: Clock, nclk: Clock, reset: AsyncReset): DDRegInit[T] = {
new DDRegInit(init, pclk, nclk, reset)
}
}
class DDRegNext[T<:Data](d: T, init: T, pclk: Clock, nclk: Clock, reset: AsyncReset ){
private val pReg = withClockAndReset(pclk, reset) { RegInit( init ) }
private val nReg = withClockAndReset(nclk, reset) { RegInit( 0.U.asTypeOf(init) ) }
pReg := (d.asUInt ^ nReg.asUInt).asTypeOf(init)
nReg := (d.asUInt ^ pReg.asUInt).asTypeOf(init)
val value: T = (pReg.asUInt ^ nReg.asUInt).asTypeOf(init)
def apply(): T = value
}
object DDRegNext{
def apply[T<:Data]( d: T, init: T, pclk: Clock, nclk: Clock, reset: AsyncReset ): DDRegNext[T] = {
new DDRegNext(d, init, pclk, nclk, reset )
}
}
class DDShiftRegisters[T<:Data]( in: T, n: Int, init: T, en: Bool, pclk: Clock, nclk: Clock, reset: AsyncReset ){
private val pRegs = withClockAndReset(pclk, reset) { RegInit(VecInit(Seq.fill(n)( init ))) }
private val nRegs = withClockAndReset(nclk, reset) { RegInit(VecInit(Seq.fill(n)( 0.U.asTypeOf(init)))) }
// 移位逻辑:首位采样输入,其他依次移位
pRegs(0) := (in.asUInt ^ nRegs(0).asUInt).asTypeOf(init)
for(i <- 1 until n) {
pRegs(i) := (pRegs(i-1).asUInt ^ nRegs(i).asUInt).asTypeOf(init)
}
nRegs(0) := (in.asUInt ^ pRegs(0).asUInt).asTypeOf(init)
for(i <- 1 until n) {
nRegs(i) := (nRegs(i-1).asUInt ^ pRegs(i).asUInt).asTypeOf(init)
}
// 输出为 pRegs 与 nRegs 异或
def apply(): Seq[T] = (pRegs zip nRegs).map{ case (a, b) => (a.asUInt ^ b.asUInt).asTypeOf(init) }
}
object DDShiftRegisters{
def apply[T<:Data]( in: T, n: Int, init: T, en: Bool, pclk: Clock, nclk: Clock, reset: AsyncReset ): DDShiftRegisters[T] = {
new DDShiftRegisters( in, n, init, en, pclk, nclk, reset )
}
}