Skip to content

Commit

Permalink
Add suggestName method to HasTarget (#3881)
Browse files Browse the repository at this point in the history
  • Loading branch information
debs-sifive authored Feb 28, 2024
1 parent 36aeabb commit c161fcc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/src/main/scala/chisel3/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,19 @@ package object chisel3 {

final val deprecatedPublicAPIMsg = "APIs in chisel3.internal are not intended to be public"

/** Contains universal methods for target accesses.
/** Exposes target information and suggestName functionality of a NamedComponent.
*/
sealed trait HasTarget {
def toTarget: ReferenceTarget
def toAbsoluteTarget: ReferenceTarget
def toRelativeTarget(root: Option[BaseModule]): ReferenceTarget

/** Exposes the suggestName method of the NamedComponent so users can
* provide a seed to influence the name generation of this component.
*
* @param seed seed for the name of this component
*/
def suggestName(seed: String): Unit
}

object HasTarget {
Expand All @@ -431,6 +438,8 @@ package object chisel3 {
def toTarget = t.toTarget
def toAbsoluteTarget = t.toAbsoluteTarget
def toRelativeTarget(root: Option[BaseModule]) = t.toRelativeTarget(root)

def suggestName(seed: String): Unit = t.suggestName(seed)
}

}
Expand Down
28 changes: 28 additions & 0 deletions src/test/scala/chiselTests/util/SRAMSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,32 @@ class SRAMSpec extends ChiselFlatSpec {
dummyAnno should be(Some("~Top|Top>sram_mem"))
}

it should "Get emitted with a custom name when one is suggested" in {

class Top extends Module {
val sramInterface = SRAM(
size = 32,
tpe = UInt(8.W),
numReadPorts = 0,
numWritePorts = 0,
numReadwritePorts = 1
)
require(sramInterface.underlying.nonEmpty)
sramInterface.underlying.get.suggestName("carrot")
annotate(new ChiselAnnotation {
override def toFirrtl: Annotation = DummyAnno(sramInterface.underlying.get.toTarget)
})
}
val (chirrtlCircuit, annos) = getFirrtlAndAnnos(new Top)
val chirrtl = chirrtlCircuit.serialize
chirrtl should include("module Top :")
chirrtl should include("smem carrot : UInt<8> [32]")
chirrtl should include(
"wire sramInterface : { readPorts : { flip address : UInt<5>, flip enable : UInt<1>, data : UInt<8>}[0], writePorts : { flip address : UInt<5>, flip enable : UInt<1>, flip data : UInt<8>}[0], readwritePorts : { flip address : UInt<5>, flip enable : UInt<1>, flip isWrite : UInt<1>, readData : UInt<8>, flip writeData : UInt<8>}[1]}"
)

val dummyAnno = annos.collectFirst { case DummyAnno(t) => (t.toString) }
dummyAnno should be(Some("~Top|Top>carrot"))
}

}

0 comments on commit c161fcc

Please sign in to comment.