Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop Emitting BlackBoxResourceAnno #1954

Merged
merged 9 commits into from
Jun 10, 2021
14 changes: 11 additions & 3 deletions src/main/scala/chisel3/util/BlackBoxUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ package chisel3.util

import chisel3._
import chisel3.experimental.{ChiselAnnotation, RunFirrtlTransform}
import firrtl.transforms.{BlackBoxPathAnno, BlackBoxResourceAnno, BlackBoxInlineAnno, BlackBoxSourceHelper}
import firrtl.transforms.{BlackBoxPathAnno, BlackBoxResourceAnno, BlackBoxInlineAnno, BlackBoxSourceHelper,
BlackBoxNotFoundException}

trait HasBlackBoxResource extends BlackBox {
self: BlackBox =>

/** Copies a resource file to the target directory
/** 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:
Expand All @@ -19,7 +21,13 @@ trait HasBlackBoxResource extends BlackBox {
*/
def addResource(blackBoxResource: String): Unit = {
val anno = new ChiselAnnotation with RunFirrtlTransform {
def toFirrtl = BlackBoxResourceAnno(self.toNamed, blackBoxResource)
def toFirrtl = try {
val blackBoxFile = os.resource / os.RelPath(blackBoxResource.dropWhile(_ == '/'))
BlackBoxInlineAnno(self.toNamed, blackBoxFile.last, os.read(blackBoxFile))
} catch {
case e: os.ResourceNotFoundException =>
throw new BlackBoxNotFoundException(blackBoxResource, e.getMessage)
}
def transformClass = classOf[BlackBoxSourceHelper]
}
chisel3.experimental.annotate(anno)
Expand Down
11 changes: 9 additions & 2 deletions src/main/scala/chisel3/util/ExtModuleUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
package chisel3.util

import chisel3.experimental.{ChiselAnnotation, ExtModule, RunFirrtlTransform}
import firrtl.transforms.{BlackBoxPathAnno, BlackBoxResourceAnno, BlackBoxInlineAnno, BlackBoxSourceHelper}
import firrtl.transforms.{BlackBoxPathAnno, BlackBoxResourceAnno, BlackBoxInlineAnno, BlackBoxSourceHelper,
BlackBoxNotFoundException}

trait HasExtModuleResource extends ExtModule {
self: ExtModule =>
Expand All @@ -18,7 +19,13 @@ trait HasExtModuleResource extends ExtModule {
*/
def addResource(blackBoxResource: String): Unit = {
val anno = new ChiselAnnotation with RunFirrtlTransform {
def toFirrtl = BlackBoxResourceAnno(self.toNamed, blackBoxResource)
def toFirrtl = try {
seldridge marked this conversation as resolved.
Show resolved Hide resolved
val blackBoxFile = os.resource / os.RelPath(blackBoxResource.dropWhile(_ == '/'))
BlackBoxInlineAnno(self.toNamed, blackBoxFile.last, os.read(blackBoxFile))
} catch {
case e: os.ResourceNotFoundException =>
throw new BlackBoxNotFoundException(blackBoxResource, e.getMessage)
}
def transformClass = classOf[BlackBoxSourceHelper]
}
chisel3.experimental.annotate(anno)
Expand Down
15 changes: 15 additions & 0 deletions src/test/scala/chiselTests/BlackBoxImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import chisel3._
import chisel3.util.{HasBlackBoxInline, HasBlackBoxResource, HasBlackBoxPath}
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}
import firrtl.FirrtlExecutionSuccess
import firrtl.transforms.BlackBoxNotFoundException
import org.scalacheck.Test.Failed
import org.scalatest.Succeeded
import org.scalatest.freespec.AnyFreeSpec
Expand Down Expand Up @@ -88,6 +89,15 @@ class UsesBlackBoxMinusViaPath extends Module {
io.out := mod0.io.out
}

class BlackBoxResourceNotFound extends HasBlackBoxResource {
val io = IO(new Bundle{})
addResource("/missing.resource")
}

class UsesMissingBlackBoxResource extends RawModule {
val foo = Module(new BlackBoxResourceNotFound)
}

class BlackBoxImplSpec extends AnyFreeSpec with Matchers {
val targetDir = "test_run_dir"
val stage = new ChiselStage
Expand All @@ -114,5 +124,10 @@ class BlackBoxImplSpec extends AnyFreeSpec with Matchers {
verilogOutput.delete()
Succeeded
}
"Resource files that do not exist produce Chisel errors" in {
assertThrows[BlackBoxNotFoundException]{
ChiselStage.emitChirrtl(new UsesMissingBlackBoxResource)
}
}
}
}
16 changes: 16 additions & 0 deletions src/test/scala/chiselTests/ExtModuleImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import chisel3.util.{HasExtModuleInline, HasExtModulePath, HasExtModuleResource}
import firrtl.FirrtlExecutionSuccess
import firrtl.options.TargetDirAnnotation
import firrtl.stage.FirrtlCircuitAnnotation
import firrtl.transforms.BlackBoxNotFoundException
import org.scalacheck.Test.Failed
import org.scalatest.{FreeSpec, Matchers, Succeeded}

Expand Down Expand Up @@ -92,6 +93,15 @@ class UsesExtModuleMinusViaPath extends Module {
io.out := mod0.io.out
}

class ExtModuleResourceNotFound extends HasExtModuleResource {
val io = IO(new Bundle{})
addResource("/missing.resource")
}

class UsesMissingExtModuleResource extends RawModule {
val foo = Module(new ExtModuleResourceNotFound)
}

class ExtModuleImplSpec extends FreeSpec with Matchers {
"ExtModule can have verilator source implementation" - {

Expand Down Expand Up @@ -137,5 +147,11 @@ class ExtModuleImplSpec extends FreeSpec with Matchers {
verilogOutput.exists() should be(true)
verilogOutput.delete()
}

"Resource files that do not exist produce Chisel errors" in {
assertThrows[BlackBoxNotFoundException]{
ChiselStage.emitChirrtl(new UsesMissingExtModuleResource)
}
}
}
}