From 770c5671744502ba7865a41472972388b2fade2c Mon Sep 17 00:00:00 2001 From: Stevo Date: Mon, 30 Jan 2017 12:17:33 -0800 Subject: Add shift register with reset (#439) * [stevo]: add reset initialization to shift register * [stevo]: better comment * [stevo]: add tests, fix bug --- src/main/scala/chisel3/util/Reg.scala | 16 ++++++++++++++++ src/test/scala/chiselTests/Reg.scala | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'src') diff --git a/src/main/scala/chisel3/util/Reg.scala b/src/main/scala/chisel3/util/Reg.scala index 00005e3a..27785dfb 100644 --- a/src/main/scala/chisel3/util/Reg.scala +++ b/src/main/scala/chisel3/util/Reg.scala @@ -61,4 +61,20 @@ object ShiftRegister in } } + + /** Returns the n-cycle delayed version of the input signal with reset initialization. + * + * @param in input to delay + * @param n number of cycles to delay + * @param resetData reset value for each register in the shift + * @param en enable the shift + */ + def apply[T <: Data](in: T, n: Int, resetData: T, en: Bool): T = { + // The order of tests reflects the expected use cases. + if (n != 0) { + RegEnable(apply(in, n-1, resetData, en), resetData, en) + } else { + in + } + } } diff --git a/src/test/scala/chiselTests/Reg.scala b/src/test/scala/chiselTests/Reg.scala index 43e64fe7..ef66c30a 100644 --- a/src/test/scala/chiselTests/Reg.scala +++ b/src/test/scala/chiselTests/Reg.scala @@ -4,7 +4,9 @@ package chiselTests import firrtl.ir.Input import org.scalatest._ +import org.scalatest.prop._ import chisel3._ +import chisel3.util._ import chisel3.core.DataMirror import chisel3.testers.BasicTester @@ -46,3 +48,33 @@ class RegSpec extends ChiselFlatSpec { elaborate{ new RegForcedWidthTester } } } + +class ShiftTester(n: Int) extends BasicTester { + val (cntVal, done) = Counter(true.B, n) + val start = 23.U + val sr = ShiftRegister(cntVal + start, n) + when(done) { + assert(sr === start) + stop() + } +} + +class ShiftResetTester(n: Int) extends BasicTester { + val (cntVal, done) = Counter(true.B, n-1) + val start = 23.U + val sr = ShiftRegister(cntVal + 23.U, n, 1.U, true.B) + when(done) { + assert(sr === 1.U) + stop() + } +} + +class ShiftRegisterSpec extends ChiselPropSpec { + property("ShiftRegister should shift") { + forAll(smallPosInts) { (shift: Int) => assertTesterPasses{ new ShiftTester(shift) } } + } + + property("ShiftRegister should reset all values inside") { + forAll(smallPosInts) { (shift: Int) => assertTesterPasses{ new ShiftResetTester(shift) } } + } +} -- cgit v1.2.3