11package chisel3 .simulator
22
33import 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 */
711object 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()`. */
0 commit comments