Skip to content

Commit 7d08528

Browse files
authored
Clear clock and reset scope for RawModule (#607)
1 parent 989fbd5 commit 7d08528

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

chiselFrontend/src/main/scala/chisel3/core/Module.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ object Module {
3232

3333
val parent = Builder.currentModule
3434
val whenDepth: Int = Builder.whenDepth
35+
36+
// Save then clear clock and reset to prevent leaking scope, must be set again in the Module
3537
val clockAndReset: Option[ClockAndReset] = Builder.currentClockAndReset
38+
Builder.currentClockAndReset = None
3639

3740
// Execute the module, this has the following side effects:
3841
// - set currentModule
@@ -108,7 +111,7 @@ abstract class BaseModule extends HasId {
108111
require(_closed, "Can't get ports before module close")
109112
_ports.toSeq
110113
}
111-
114+
112115
// These methods allow checking some properties of ports before the module is closed,
113116
// mainly for compatibility purposes.
114117
protected def portsContains(elem: Data): Boolean = _ports contains elem

src/test/scala/chiselTests/RawModuleSpec.scala

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class PlusOneModule extends Module {
2828
io.out := io.in + 1.asUInt
2929
}
3030

31-
class RawModuleWithImpliitModule extends RawModule {
31+
class RawModuleWithImplicitModule extends RawModule {
3232
val in = IO(Input(UInt(32.W)))
3333
val out = IO(Output(UInt(32.W)))
3434
val clk = IO(Input(Clock()))
3535
val rst = IO(Input(Bool()))
36-
36+
3737
withClockAndReset(clk, rst) {
3838
val plusModule = Module(new PlusOneModule)
3939
plusModule.io.in := in
@@ -42,24 +42,46 @@ class RawModuleWithImpliitModule extends RawModule {
4242
}
4343

4444
class ImplicitModuleInRawModuleTester extends BasicTester {
45-
val plusModule = Module(new RawModuleWithImpliitModule)
45+
val plusModule = Module(new RawModuleWithImplicitModule)
4646
plusModule.clk := clock
4747
plusModule.rst := reset
4848
plusModule.in := 42.U
4949
assert(plusModule.out === 43.U)
5050
stop()
5151
}
5252

53+
class RawModuleWithDirectImplicitModule extends RawModule {
54+
val plusModule = Module(new PlusOneModule)
55+
}
56+
57+
class ImplicitModuleDirectlyInRawModuleTester extends BasicTester {
58+
val plusModule = Module(new RawModuleWithDirectImplicitModule)
59+
stop()
60+
}
61+
5362
class RawModuleSpec extends ChiselFlatSpec {
5463
"RawModule" should "elaborate" in {
55-
elaborate { new RawModuleWithImpliitModule }
64+
elaborate { new RawModuleWithImplicitModule }
5665
}
57-
66+
5867
"RawModule" should "work" in {
5968
assertTesterPasses({ new RawModuleTester })
6069
}
61-
70+
6271
"ImplicitModule in a withClock block in a RawModule" should "work" in {
6372
assertTesterPasses({ new ImplicitModuleInRawModuleTester })
6473
}
65-
}
74+
75+
76+
"ImplicitModule directly in a RawModule" should "fail" in {
77+
intercept[chisel3.internal.ChiselException] {
78+
elaborate { new RawModuleWithDirectImplicitModule }
79+
}
80+
}
81+
82+
"ImplicitModule directly in a RawModule in an ImplicitModule" should "fail" in {
83+
intercept[chisel3.internal.ChiselException] {
84+
elaborate { new ImplicitModuleDirectlyInRawModuleTester }
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)