Skip to content

Commit 71239a4

Browse files
committed
[ChiselSim] Add preprocessor defs to LayerControl
Add a member function `preprocessorDefines` to `LayerControl.Type` which will return any preprocessor defines that should be set to enable the requested layers. Add an abstract member function, `getLayerSubset`, which layer control concrete types implement to make the preprocessor define function work. Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent 6107183 commit 71239a4

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/main/scala-2/chisel3/simulator/LayerControl.scala

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package chisel3.simulator
22

33
import java.io.File
4-
import chisel3.layer.Layer
4+
import chisel3.RawModule
5+
import chisel3.layer.{ABI, Layer}
6+
import chisel3.simulator.ElaboratedModule
7+
import chisel3.stage.DesignAnnotation
8+
import svsim.CommonCompilationSettings.VerilogPreprocessorDefine
59

610
/** Utilities for enabling and disabling Chisel layers */
711
object LayerControl {
@@ -22,11 +26,41 @@ object LayerControl {
2226
* @param layerFilename the filename of a layer
2327
*/
2428
protected def shouldEnable(layerFilename: String): Boolean
29+
30+
/** Return the layers that should be enabled in a circuit. The layers must exist in the design.
31+
*
32+
* @param design an Annotation that contains an elaborated design used to check that the requested layers exist
33+
* @return all layers that should be enabled
34+
* @throws IllegalArgumentException if the requested layers
35+
*/
36+
protected def getLayerSubset(module: ElaboratedModule[_]): Seq[Layer]
37+
38+
/** Return the preprocessor defines that should be set to enable the layers of
39+
* this `LayerControl.Type`.
40+
*
41+
* This requires passing an elaborated module in order to know what layers
42+
* exist in the design.
43+
*
44+
* @param module an elaborated module
45+
* @return preprocessor defines to control the enabling of these layers
46+
*/
47+
final def preprocessorDefines(
48+
module: ElaboratedModule[_ <: RawModule]
49+
): Seq[VerilogPreprocessorDefine] = getLayerSubset(module).flatMap {
50+
case layer =>
51+
layer.config.abi match {
52+
case abi: chisel3.layer.ABI.PreprocessorDefine.type =>
53+
Some(VerilogPreprocessorDefine(abi.toMacroIdentifier(layer, module.wrapped.circuitName)))
54+
case _ => None
55+
}
56+
}
2557
}
2658

2759
/** Enable all layers */
2860
final case object EnableAll extends Type {
2961
override protected def shouldEnable(layerFilename: String) = true
62+
63+
override protected def getLayerSubset(module: ElaboratedModule[_]): Seq[Layer] = module.layers
3064
}
3165

3266
/** Enable only the specified layers
@@ -46,6 +80,19 @@ object LayerControl {
4680
}
4781
}
4882
override protected def shouldEnable(filename: String) = _shouldEnable(filename)
83+
84+
override protected def getLayerSubset(module: ElaboratedModule[_]): Seq[Layer] = {
85+
val definedLayers = module.layers
86+
layers.foreach { layer =>
87+
require(
88+
definedLayers.contains(layer),
89+
s"""cannot enable layer '${layer.fullName}' as it is not one of the defined layers: ${definedLayers.map(
90+
_.fullName
91+
)}"""
92+
)
93+
}
94+
layers
95+
}
4996
}
5097

5198
/** Disables all layers. This is the same as `Enable()`. */

src/main/scala-2/chisel3/simulator/package.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chisel3
33
import svsim._
44
import chisel3.reflect.DataMirror
55
import chisel3.experimental.dataview.reifyIdentityView
6+
import chisel3.stage.DesignAnnotation
67
import scala.collection.mutable
78
import java.nio.file.{Files, Path, Paths}
89
import firrtl.seqToAnnoSeq
@@ -14,7 +15,8 @@ package object simulator {
1415
*/
1516
final class ElaboratedModule[T] private[simulator] (
1617
private[simulator] val wrapped: T,
17-
private[simulator] val ports: Seq[(Data, ModuleInfo.Port)])
18+
private[simulator] val ports: Seq[(Data, ModuleInfo.Port)],
19+
private[simulator] val layers: Seq[chisel3.layer.Layer])
1820

1921
/**
2022
* A class that enables using a Chisel module to control an `svsim.Simulation`.
@@ -222,7 +224,8 @@ package object simulator {
222224
ports = ports.map(_._2)
223225
)
224226
)
225-
new ElaboratedModule(dut, ports)
227+
val layers = outputAnnotations.collectFirst { case DesignAnnotation(_, layers) => layers }.get
228+
new ElaboratedModule(dut, ports, layers)
226229
}
227230
}
228231
}

0 commit comments

Comments
 (0)