Skip to content

Commit

Permalink
Added disalowIOCreation as a public API
Browse files Browse the repository at this point in the history
  • Loading branch information
azidar committed Oct 13, 2023
1 parent 7acd8c3 commit fe5891a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion core/src/main/scala/chisel3/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ object IO {
*/
def apply[T <: Data](iodef: => T)(implicit sourceInfo: SourceInfo): T = {
val module = Module.currentModule.get // Impossible to fail
require(module.isIOCreationAllowed, "This module cannot have user-created IO")
if (!module.isIOCreationAllowed)
Builder.error("This module cannot have IOs instantiated after calling disallowIOCreation()")
require(!module.isClosed, "Can't add more ports after module close")
val prevId = Builder.idGen.value
val data = iodef // evaluate once (passed by name)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/chisel3/Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ package experimental {
private[chisel3] def isIOCreationAllowed = _isIOCreationAllowed

/** Disallow any more IO creation for this module. */
private[chisel3] def disallowIOCreation(): Unit = {
def disallowIOCreation(): Unit = {
_isIOCreationAllowed = false
}

Expand Down
23 changes: 20 additions & 3 deletions src/test/scala/chiselTests/FixedIOModuleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class FixedIOModuleSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
class Foo extends FixedIORawModule[Bool](Bool()) {
val a = IO(Bool())
}
val exception = intercept[IllegalArgumentException] {
ChiselStage.emitCHIRRTL(new Foo)
val exception = intercept[ChiselException] {
ChiselStage.emitCHIRRTL(new Foo, Array("--throw-on-first-error"))
}
exception.getMessage should include("This module cannot have user-created IO")
exception.getMessage should include("This module cannot have IOs instantiated after calling disallowIOCreation()")
}

"FixedIOBlackBox" should "create a module with flattend IO" in {
Expand All @@ -47,4 +47,21 @@ class FixedIOModuleSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
)()
}

"User defined RawModules" should "be able to lock down their ios" in {

class Bar extends RawModule {
val in = IO(Input(UInt(1.W)))
val out = IO(Output(UInt(1.W)))

disallowIOCreation()

val other = IO(Input(UInt(1.W)))
}

val exception = intercept[ChiselException] {
ChiselStage.emitCHIRRTL(new Bar, Array("--throw-on-first-error"))
}
exception.getMessage should include("This module cannot have IOs instantiated after calling disallowIOCreation()")
}

}

0 comments on commit fe5891a

Please sign in to comment.