From a28c7076c63509bccdaa0bdafd78826809152903 Mon Sep 17 00:00:00 2001 From: RuigeLee Date: Fri, 17 Apr 2026 17:04:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8C=E8=BE=B9=E6=B2=BF=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E6=88=90=E5=8A=9F=EF=BC=8C=E4=BD=BF=E7=94=A8?= =?UTF-8?q?scala=E9=9A=90=E5=BC=8F=E8=BD=AC=E6=8D=A2=E8=AF=AD=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/scala/util/DDFlipFlop.scala | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/scala/util/DDFlipFlop.scala b/src/main/scala/util/DDFlipFlop.scala index 2e3ba49..ebec2d0 100644 --- a/src/main/scala/util/DDFlipFlop.scala +++ b/src/main/scala/util/DDFlipFlop.scala @@ -19,8 +19,6 @@ class FDRegInit[T<:Data](init: T, pclk: Clock, nclk: Clock, reset: AsyncReset, m if( !mode ){ pReg := next } else { pReg := (nReg.asUInt ^ next.asUInt).asTypeOf(init) } nReg := (pReg.asUInt ^ next.asUInt).asTypeOf(init) - def apply(): T = value - } object FDRegInit{ @@ -28,6 +26,8 @@ object FDRegInit{ new FDRegInit(init, pclk, nclk, reset, mode) } + // 隐式转换:FDRegInit[T] → T,读取时自动调用 .apply() + implicit def toData[T<:Data](reg: FDRegInit[T]): T = reg.value } @@ -57,16 +57,19 @@ class FDShiftRegisters[T<:Data]( in: T, n: Int, init: T, en: Bool, pclk: Clock, private val nRegs = withClockAndReset(nclk, reset) { RegInit(VecInit(Seq.fill(n)( 0.U.asTypeOf(init)))) } // 移位逻辑:首位采样输入,其他依次移位 - if( !mode ){ pRegs(0) := in } else { pRegs(0) := (in.asUInt ^ nRegs(0).asUInt).asTypeOf(init) } - for(i <- 1 until n) { - if( !mode ){ pRegs(i) := pRegs(i-1) } - else { pRegs(i) := (pRegs(i-1).asUInt ^ nRegs(i).asUInt).asTypeOf(init)} + when(en){ + if( !mode ){ pRegs(0) := in } else { pRegs(0) := (in.asUInt ^ nRegs(0).asUInt).asTypeOf(init) } + for(i <- 1 until n) { + if( !mode ){ pRegs(i) := pRegs(i-1) } + else { pRegs(i) := (pRegs(i-1).asUInt ^ nRegs(i-1).asUInt ^ nRegs(i).asUInt).asTypeOf(init)} + } + + nRegs(0) := (in.asUInt ^ pRegs(0).asUInt).asTypeOf(init) + for(i <- 1 until n) { + nRegs(i) := (pRegs(i-1).asUInt ^ nRegs(i-1).asUInt ^ pRegs(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] =