summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/BlackBoxUtils.scala
blob: 579a6307452a08fb3e9a572ca0818ca21420d62c (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: Apache-2.0

package chisel3.util

import chisel3._
import chisel3.experimental.{ChiselAnnotation, RunFirrtlTransform}
import firrtl.transforms.{BlackBoxInlineAnno, BlackBoxNotFoundException, BlackBoxPathAnno, BlackBoxSourceHelper}
import firrtl.annotations.ModuleName
import logger.LazyLogging

private[util] object BlackBoxHelpers {

  implicit class BlackBoxInlineAnnoHelpers(anno: BlackBoxInlineAnno.type) extends LazyLogging {

    /** Generate a BlackBoxInlineAnno from a Java Resource and a module name. */
    def fromResource(resourceName: String, moduleName: ModuleName) = try {
      val blackBoxFile = os.resource / os.RelPath(resourceName.dropWhile(_ == '/'))
      val contents = os.read(blackBoxFile)
      if (contents.size > BigInt(2).pow(20)) {
        val message =
          s"Black box resource $resourceName, which will be converted to an inline annotation, is greater than 1 MiB." +
            "This may affect compiler performance. Consider including this resource via a black box path."
        logger.warn(message)
      }
      BlackBoxInlineAnno(moduleName, blackBoxFile.last, contents)
    } catch {
      case e: os.ResourceNotFoundException =>
        throw new BlackBoxNotFoundException(resourceName, e.getMessage)
    }
  }
}

import BlackBoxHelpers._

trait HasBlackBoxResource extends BlackBox {
  self: BlackBox =>

  /** Copies a Java resource containing some text into the output directory. This is typically used to copy a Verilog file
    * to the final output directory, but may be used to copy any Java resource (e.g., a C++ testbench).
    *
    * Resource files are located in project_root/src/main/resources/.
    * Example of adding the resource file project_root/src/main/resources/blackbox.v:
    * {{{
    * addResource("/blackbox.v")
    * }}}
    */
  def addResource(blackBoxResource: String): Unit = {
    val anno = new ChiselAnnotation with RunFirrtlTransform {
      def toFirrtl = BlackBoxInlineAnno.fromResource(blackBoxResource, self.toNamed)
      def transformClass = classOf[BlackBoxSourceHelper]
    }
    chisel3.experimental.annotate(anno)
  }
}

trait HasBlackBoxInline extends BlackBox {
  self: BlackBox =>

  /** Creates a black box verilog file, from the contents of a local string
    *
    * @param blackBoxName   The black box module name, to create filename
    * @param blackBoxInline The black box contents
    */
  def setInline(blackBoxName: String, blackBoxInline: String): Unit = {
    val anno = new ChiselAnnotation with RunFirrtlTransform {
      def toFirrtl = BlackBoxInlineAnno(self.toNamed, blackBoxName, blackBoxInline)
      def transformClass = classOf[BlackBoxSourceHelper]
    }
    chisel3.experimental.annotate(anno)
  }
}

trait HasBlackBoxPath extends BlackBox {
  self: BlackBox =>

  /** Copies a file to the target directory
    *
    * This works with absolute and relative paths. Relative paths are relative
    * to the current working directory, which is generally not the same as the
    * target directory.
    */
  def addPath(blackBoxPath: String): Unit = {
    val anno = new ChiselAnnotation with RunFirrtlTransform {
      def toFirrtl = BlackBoxPathAnno(self.toNamed, blackBoxPath)
      def transformClass = classOf[BlackBoxSourceHelper]
    }
    chisel3.experimental.annotate(anno)
  }
}