Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enrich ShiftRegister with SyncReadMem-based implementation. #2891

Merged
merged 16 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ class SourceInfoTransform(val c: Context) extends AutoSourceTransform {
def inNResetDataEnArg(in: c.Tree, n: c.Tree, resetData: c.Tree, en: c.Tree): c.Tree = {
q"$thisObj.$doFuncTerm($in, $n, $resetData, $en)($implicitSourceInfo, $implicitCompileOptions)"
}

def inNEnUseDualSRAMpNameArg(in: c.Tree, n: c.Tree, en: c.Tree, useDualSRAMPort: c.Tree, name: c.Tree): c.Tree = {
q"$thisObj.$doFuncTerm($in, $n, $en, $useDualSRAMPort, $name)($implicitSourceInfo, $implicitCompileOptions)"
}
}

// Workaround for https://github.com/sbt/sbt/issues/3966
Expand Down
90 changes: 90 additions & 0 deletions src/main/scala/chisel3/util/Reg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,96 @@ object ShiftRegister {
compileOptions: CompileOptions
): T =
ShiftRegisters(in, n, resetData, en).lastOption.getOrElse(in)

/** Returns the n-cycle delayed version of the input signal (SyncReadMem-based ShiftRegister implementation).
*
* @param in input to delay
* @param n number of cycles to delay
* @param en enable the shift
* @param useDualSRAMPort dual port or single port SRAM based implementation
milovanovic marked this conversation as resolved.
Show resolved Hide resolved
* @param name name of SyncReadMem object
*/
def mem[T <: Data](in: T, n: Int, en: Bool, useDualSRAMPort: Boolean, name: Option[String]): T =
milovanovic marked this conversation as resolved.
Show resolved Hide resolved
macro SourceInfoTransform.inNEnUseDualSRAMpNameArg

/** @group SourceInfoTransformMacro */
def do_mem[T <: Data](
in: T,
n: Int,
en: Bool,
useDualSRAMPort: Boolean,
milovanovic marked this conversation as resolved.
Show resolved Hide resolved
name: Option[String]
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): T = _apply_impl_mem(in, n, en, useDualSRAMPort, name)

private def _apply_impl_mem[T <: Data](
in: T,
n: Int,
en: Bool = true.B,
useDualSRAMPort: Boolean = false,
milovanovic marked this conversation as resolved.
Show resolved Hide resolved
name: Option[String] = None
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): T = {
if (n == 0) {
in
} else if (n == 1) {
val out = RegEnable(in, en)
out
} else if (useDualSRAMPort) {
val mem = SyncReadMem(n, in.cloneType)
if (name != None) {
mem.suggestName(name.get)
}
val raddr = Counter(en, n)._1
val out = mem.read(raddr, en)

val waddr = RegEnable(raddr, (n - 1).U, en)
when(en) {
mem.write(waddr, in)
}
out
} else {
require(n % 2 == 0, "Odd shift register length with single-port SRAMs is not supported")

val out_sp0 = Wire(in.cloneType)
out_sp0 := DontCare

val out_sp1 = Wire(in.cloneType)
out_sp1 := DontCare

val mem_sp0 = SyncReadMem(n / 2, in.cloneType)
val mem_sp1 = SyncReadMem(n / 2, in.cloneType)

if (name != None) {
mem_sp0.suggestName(name.get + "_0")
mem_sp1.suggestName(name.get + "_1")
}

val index_counter = Counter(en, n)._1
val raddr_sp0 = index_counter >> 1.U
val raddr_sp1 = RegEnable(raddr_sp0, (n / 2 - 1).U, en)

val wen_sp0 = index_counter(0)
val wen_sp1 = WireDefault(false.B)
wen_sp1 := ~wen_sp0

when(en) {
val rdwrPort = mem_sp0(raddr_sp0)
when(wen_sp0) { rdwrPort := in }.otherwise { out_sp0 := rdwrPort }
}

when(en) {
val rdwrPort = mem_sp1(raddr_sp1)
when(wen_sp1) { rdwrPort := in }.otherwise { out_sp1 := rdwrPort }
}
val out = Mux(~wen_sp1, out_sp0, out_sp1)
out
}
}
}

object ShiftRegisters {
Expand Down
22 changes: 22 additions & 0 deletions src/test/scala/chiselTests/Reg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class ShiftTester(n: Int) extends BasicTester {
}
}

class ShiftMemTester(n: Int, dp_mem: Boolean) extends BasicTester {
val (cntVal, done) = Counter(true.B, n)
val start = 23.U
val sr = ShiftRegister.mem(cntVal + start, n, true.B, dp_mem, Some("simple_sr"))
when(RegNext(done)) {
assert(sr === start)
stop()
}
}

class ShiftResetTester(n: Int) extends BasicTester {
val (cntVal, done) = Counter(true.B, n - 1)
val start = 23.U
Expand All @@ -71,6 +81,18 @@ class ShiftRegisterSpec extends ChiselPropSpec {
}
}

class ShiftRegisterMemSpec extends ChiselPropSpec {
property("ShiftRegister with dual-port SRAM should shift") {
forAll(Gen.choose(0, 4)) { (shift: Int) => assertTesterPasses { new ShiftMemTester(shift, true) } }
}

property("ShiftRegister with single-port SRAM should shift") {
forAll(Gen.choose(0, 6).suchThat(_ % 2 == 0)) { (shift: Int) =>
assertTesterPasses { new ShiftMemTester(shift, false) }
}
}
}

class ShiftsTester(n: Int) extends BasicTester {
val (cntVal, done) = Counter(true.B, n)
val start = 23.U
Expand Down
13 changes: 13 additions & 0 deletions src/test/scala/chiselTests/util/RegSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ class ShiftRegisterSpec extends AnyFlatSpec with Matchers {
(chirrtl should not).include("Reg.scala")
}

it should "have source locators when passed in, n, en, useDualSRAMPort, name" in {
class MyModule extends Module {
val in = IO(Input(Bool()))
val out = IO(Output(Bool()))
out := ShiftRegister.mem(in, 2, true.B, false, Some("sr"))
}
val chirrtl = ChiselStage.emitCHIRRTL(new MyModule)
val reset = """reset .*RegSpec.scala""".r
(chirrtl should include).regex(reset)
val update = """out_r.* in .*RegSpec.scala""".r
(chirrtl should include).regex(update)
(chirrtl should not).include("Reg.scala")
}
}

class ShiftRegistersSpec extends AnyFlatSpec with Matchers {
Expand Down