summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/experimental/group.scala
blob: 202c95d8d7a053e668420f8300a2efa345d5b993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: Apache-2.0

package chisel3.util.experimental

import chisel3._
import chisel3.experimental.{annotate, ChiselAnnotation, RunFirrtlTransform}
import chisel3.internal.requireIsHardware
import firrtl.Transform
import firrtl.transforms.{GroupAnnotation, GroupComponents}

/** Marks that a module to be ignored in Dedup Transform in Firrtl pass
  *
  * @example {{{
  * class MyModule extends Module {
  *   val io = IO(new Bundle{
  *     val a = Input(Bool())
  *     val b = Output(Bool())
  *   })
  *   val reg1 = RegInit(0.U)
  *   reg1 := io.a
  *   val reg2 = RegNext(reg1)
  *   io.b := reg2
  *   group(Seq(reg1, reg2), "DosRegisters", "doubleReg")
  * }
  * }}}
  *
  * @note Intermediate wires will get pulled into the new instance, but intermediate registers will not
  *       because they are also connected to their module's clock port. This means that if you want
  *       a register to be included in a group, it must be explicitly referred to in the input list.
  */
object group {

  /** Marks a set of components (and their interconnected components) to be included in a new
    * instance hierarchy.
    *
    * @note Intermediate wires will get pulled into the new instance, but intermediate registers will not
    *       because they are also connected to their module's clock port. This means that if you want
    *       a register to be included in a group, it must be explicitly referred to in the input list.
    *
    * @param components components in this group
    * @param newModule suggested name of the new module
    * @param newInstance suggested name of the instance of the new module
    * @param outputSuffix suggested suffix of any output ports of the new module
    * @param inputSuffix suggested suffix of any input ports of the new module
    * @param compileOptions necessary for backwards compatibility
    * @tparam T Parent type of input components
    */
  def apply[T <: Data](
    components:   Seq[T],
    newModule:    String,
    newInstance:  String,
    outputSuffix: Option[String] = None,
    inputSuffix:  Option[String] = None
  )(
    implicit compileOptions: CompileOptions
  ): Unit = {
    if (compileOptions.checkSynthesizable) {
      components.foreach { data =>
        requireIsHardware(data, s"Component ${data.toString} is marked to group, but is not bound.")
      }
    }
    annotate(new ChiselAnnotation with RunFirrtlTransform {
      def toFirrtl = GroupAnnotation(components.map(_.toNamed), newModule, newInstance, outputSuffix, inputSuffix)

      override def transformClass: Class[_ <: Transform] = classOf[GroupComponents]
    })
  }
}