Skip to content

Commit 34fbd6c

Browse files
Add resetType to inline test TestParameters (#4789)
1 parent 4ed2b52 commit 34fbd6c

File tree

2 files changed

+83
-14
lines changed

2 files changed

+83
-14
lines changed

src/main/scala/chisel3/experimental/inlinetest/InlineTest.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class TestParameters[M <: RawModule, R] private[inlinetest] (
1919
/** A Definition of the DUT module. */
2020
val dutDefinition: Definition[M],
2121
/** The body for this test, returns a result. */
22-
val testBody: Instance[M] => R
22+
val testBody: Instance[M] => R,
23+
/** The reset type of the DUT module. */
24+
val resetType: Option[Module.ResetType.Type]
2325
) {
2426
final def desiredTestModuleName = s"test_${dutName}_${testName}"
2527
}
@@ -50,7 +52,11 @@ object TestHarness {
5052
* @tparam R the type of the result returned by the test body
5153
*/
5254
trait Module[M <: ChiselRawModule, R] extends RawModule[M, R] { this: ChiselModule =>
53-
override def resetType = Module.ResetType.Synchronous
55+
override def resetType = test.resetType match {
56+
case Some(rt @ Module.ResetType.Synchronous) => rt
57+
case Some(rt @ Module.ResetType.Asynchronous) => rt
58+
case _ => Module.ResetType.Synchronous
59+
}
5460
}
5561
}
5662

@@ -110,7 +116,11 @@ trait HasTests[M <: RawModule] { module: M =>
110116
testName: String
111117
)(testBody: Instance[M] => R)(implicit th: TestHarnessGenerator[M, R]): Unit =
112118
elaborateParentModule { moduleDefinition =>
113-
val test = new TestParameters[M, R](desiredName, testName, moduleDefinition, testBody)
119+
val resetType = module match {
120+
case module: Module => Some(module.resetType)
121+
case _ => None
122+
}
123+
val test = new TestParameters[M, R](desiredName, testName, moduleDefinition, testBody, resetType)
114124
th.generate(test)
115125
}
116126
}

src/test/scala/chiselTests/experimental/InlineTestSpec.scala

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import circt.stage.ChiselStage
1010
import org.scalatest.flatspec.AnyFlatSpec
1111
import org.scalatest.matchers.should.Matchers
1212

13+
import circt.stage.ChiselStage.emitCHIRRTL
14+
1315
class TestResultBundle extends Bundle {
1416
val finish = Output(Bool())
1517
val code = Output(UInt(8.W))
@@ -67,7 +69,10 @@ class ProtocolMonitor(bundleType: ProtocolBundle) extends Module {
6769
}
6870

6971
@instantiable
70-
class ModuleWithTests(ioWidth: Int = 32) extends Module with HasMonitorSocket with HasTests[ModuleWithTests] {
72+
class ModuleWithTests(ioWidth: Int = 32, override val resetType: Module.ResetType.Type = Module.ResetType.Synchronous)
73+
extends Module
74+
with HasMonitorSocket
75+
with HasTests[ModuleWithTests] {
7176
@public val io = IO(new ProtocolBundle(ioWidth))
7277

7378
override val monProbe = makeProbe(io)
@@ -112,42 +117,50 @@ class ModuleWithTests(ioWidth: Int = 32) extends Module with HasMonitorSocket wi
112117
}
113118
}
114119

115-
class InlineTestSpec extends AnyFlatSpec with Matchers with FileCheck {
120+
@instantiable
121+
class RawModuleWithTests(ioWidth: Int = 32) extends RawModule with HasTests[RawModuleWithTests] {
122+
@public val io = IO(new ProtocolBundle(ioWidth))
123+
io.out := io.in
124+
test("foo") { instance =>
125+
instance.io.in := 3.U(ioWidth.W)
126+
assert(instance.io.out === 3.U): Unit
127+
}
128+
}
129+
130+
class InlineTestSpec extends AnyFlatSpec with FileCheck {
116131
it should "generate a public module for each test" in {
117-
ChiselStage
118-
.emitCHIRRTL(new ModuleWithTests)
119-
.fileCheck()(
120-
"""
132+
emitCHIRRTL(new ModuleWithTests).fileCheck()(
133+
"""
121134
| CHECK: module ModuleWithTests
122135
| CHECK: output monProbe : Probe<{ in : UInt<32>, out : UInt<32>}>
123136
|
124137
| CHECK: public module test_ModuleWithTests_foo
125138
| CHECK-NEXT: input clock : Clock
126-
| CHECK-NEXT: input reset : UInt<1>
139+
| CHECK-NEXT: input reset
127140
| CHECK: inst dut of ModuleWithTests
128141
|
129142
| CHECK: public module test_ModuleWithTests_bar
130143
| CHECK-NEXT: input clock : Clock
131-
| CHECK-NEXT: input reset : UInt<1>
144+
| CHECK-NEXT: input reset
132145
| CHECK: inst dut of ModuleWithTests
133146
|
134147
| CHECK: public module test_ModuleWithTests_with_result
135148
| CHECK-NEXT: input clock : Clock
136-
| CHECK-NEXT: input reset : UInt<1>
149+
| CHECK-NEXT: input reset
137150
| CHECK-NEXT: output result : { finish : UInt<1>, code : UInt<8>}
138151
| CHECK: inst dut of ModuleWithTests
139152
|
140153
| CHECK: public module test_ModuleWithTests_with_monitor
141154
| CHECK-NEXT: input clock : Clock
142-
| CHECK-NEXT: input reset : UInt<1>
155+
| CHECK-NEXT: input reset
143156
| CHECK: inst dut of ModuleWithTests
144157
| CHECK: inst monitor of ProtocolMonitor
145158
| CHECK-NEXT: connect monitor.clock, clock
146159
| CHECK-NEXT: connect monitor.reset, reset
147160
| CHECK-NEXT: connect monitor.io.out, read(dut.monProbe).out
148161
| CHECK-NEXT: connect monitor.io.in, read(dut.monProbe).in
149162
"""
150-
)
163+
)
151164
}
152165

153166
it should "compile to verilog" in {
@@ -163,4 +176,50 @@ class InlineTestSpec extends AnyFlatSpec with Matchers with FileCheck {
163176
"""
164177
)
165178
}
179+
180+
it should "emit the correct reset types" in {
181+
def fileCheckString(resetType: String) =
182+
s"""
183+
| CHECK: module ModuleWithTests
184+
| CHECK-NEXT: input clock : Clock
185+
| CHECK-NEXT: input reset : ${resetType}
186+
|
187+
| CHECK: public module test_ModuleWithTests_foo
188+
| CHECK-NEXT: input clock : Clock
189+
| CHECK-NEXT: input reset : ${resetType}
190+
|
191+
| CHECK: public module test_ModuleWithTests_bar
192+
| CHECK-NEXT: input clock : Clock
193+
| CHECK-NEXT: input reset : ${resetType}
194+
|
195+
| CHECK: public module test_ModuleWithTests_with_result
196+
| CHECK-NEXT: input clock : Clock
197+
| CHECK-NEXT: input reset : ${resetType}
198+
|
199+
| CHECK: public module test_ModuleWithTests_with_monitor
200+
| CHECK-NEXT: input clock : Clock
201+
| CHECK-NEXT: input reset : ${resetType}
202+
"""
203+
204+
emitCHIRRTL(new ModuleWithTests(resetType = Module.ResetType.Synchronous)).fileCheck()(
205+
fileCheckString("UInt<1>")
206+
)
207+
emitCHIRRTL(new ModuleWithTests(resetType = Module.ResetType.Asynchronous)).fileCheck()(
208+
fileCheckString("AsyncReset")
209+
)
210+
emitCHIRRTL(new ModuleWithTests(resetType = Module.ResetType.Default)).fileCheck()(
211+
fileCheckString("UInt<1>")
212+
)
213+
214+
emitCHIRRTL(new RawModuleWithTests()).fileCheck()(
215+
"""
216+
| CHECK: module RawModuleWithTests
217+
| CHECK-NEXT: output io
218+
|
219+
| CHECK: public module test_RawModuleWithTests_foo
220+
| CHECK-NEXT: input clock : Clock
221+
| CHECK-NEXT: input reset : UInt<1>
222+
"""
223+
)
224+
}
166225
}

0 commit comments

Comments
 (0)