Skip to content

Commit 8ca4391

Browse files
authored
[chiselsim] Add subdirectory to SimulatorAPI fns (#4815)
Add a subdirectory parameter to `simulate` and `simulateRaw` methods. This is intended for a bit of a corner case where a user wants to run simulate multiple times within a single test or they want to customize the test directory based on Cli arguments. Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent 4e086b5 commit 8ca4391

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/main/scala/chisel3/simulator/SimulatorAPI.scala

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ trait SimulatorAPI {
2121
* @param chiselOpts command line options to pass to Chisel
2222
* @param firtoolOpts command line options to pass to firtool
2323
* @param settings ChiselSim-related settings used for simulation
24+
* @param subdirectory an optional subdirectory for the test. This will be a
25+
* subdirectory under what is provided by `testingDirectory`.
2426
* @param stimulus directed stimulus to use
2527
* @param testingDirectory a type class implementation that can be used to
2628
* change the behavior of where files will be created
@@ -29,10 +31,11 @@ trait SimulatorAPI {
2931
* by default and if you set incompatible options, the simulation will fail.
3032
*/
3133
def simulateRaw[T <: RawModule](
32-
module: => T,
33-
chiselOpts: Array[String] = Array.empty,
34-
firtoolOpts: Array[String] = Array.empty,
35-
settings: Settings[T] = Settings.defaultRaw[T]
34+
module: => T,
35+
chiselOpts: Array[String] = Array.empty,
36+
firtoolOpts: Array[String] = Array.empty,
37+
settings: Settings[T] = Settings.defaultRaw[T],
38+
subdirectory: Option[String] = None
3639
)(stimulus: (T) => Unit)(
3740
implicit hasSimulator: HasSimulator,
3841
testingDirectory: HasTestingDirectory,
@@ -42,7 +45,13 @@ trait SimulatorAPI {
4245
backendSettingsModifications: svsim.BackendSettingsModifications
4346
): Unit = {
4447

45-
hasSimulator.getSimulator
48+
val modifiedTestingDirectory = subdirectory match {
49+
case Some(subdir) => testingDirectory.withSubdirectory(subdir)
50+
case None => testingDirectory
51+
}
52+
53+
hasSimulator
54+
.getSimulator(modifiedTestingDirectory)
4655
.simulate(module = module, chiselOpts = chiselOpts, firtoolOpts = firtoolOpts, settings = settings) { module =>
4756
stimulus(module.wrapped)
4857
}
@@ -59,6 +68,8 @@ trait SimulatorAPI {
5968
* @param settings ChiselSim-related settings used for simulation
6069
* @param additionalResetCycles a number of _additional_ cycles to assert
6170
* reset for
71+
* @param subdirectory an optional subdirectory for the test. This will be a
72+
* subdirectory under what is provided by `testingDirectory`.
6273
* @param stimulus directed stimulus to use
6374
* @param testingDirectory a type class implementation that can be used to
6475
* change the behavior of where files will be created
@@ -71,7 +82,8 @@ trait SimulatorAPI {
7182
chiselOpts: Array[String] = Array.empty,
7283
firtoolOpts: Array[String] = Array.empty,
7384
settings: Settings[T] = Settings.default[T],
74-
additionalResetCycles: Int = 0
85+
additionalResetCycles: Int = 0,
86+
subdirectory: Option[String] = None
7587
)(stimulus: (T) => Unit)(
7688
implicit hasSimulator: HasSimulator,
7789
testingDirectory: HasTestingDirectory,
@@ -83,7 +95,8 @@ trait SimulatorAPI {
8395
module = module,
8496
chiselOpts = chiselOpts,
8597
firtoolOpts = firtoolOpts,
86-
settings = settings
98+
settings = settings,
99+
subdirectory = subdirectory
87100
) { dut =>
88101
ResetProcedure.module(additionalResetCycles)(dut)
89102
stimulus(dut)

src/test/scala-2/chiselTests/simulator/scalatest/ChiselSimSpec.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,30 @@ class ChiselSimSpec extends AnyFunSpec with Matchers with ChiselSim with FileChe
182182
}
183183
}
184184

185+
it("should allow the user to change the subdirectory on SimulatorAPI methods") {
186+
class Foo extends Module {
187+
stop()
188+
}
189+
190+
var file = FileSystems
191+
.getDefault()
192+
.getPath(implicitly[HasTestingDirectory].getDirectory.toString, "foo", "workdir-verilator", "Makefile")
193+
.toFile
194+
file.delete()
195+
simulate(new Foo, subdirectory = Some("foo")) { _ => }
196+
info(s"$file exists")
197+
file should (exist)
198+
199+
file = FileSystems
200+
.getDefault()
201+
.getPath(implicitly[HasTestingDirectory].getDirectory.toString, "bar", "workdir-verilator", "Makefile")
202+
.toFile
203+
file.delete()
204+
simulateRaw(new Foo, subdirectory = Some("bar")) { _ => }
205+
info(s"$file exists")
206+
file should (exist)
207+
}
208+
185209
// Return a Verilator `HasSimulator` that will dump waves to `trace.vcd`.
186210
def verilatorWithWaves = HasSimulator.simulators
187211
.verilator(verilatorSettings =

0 commit comments

Comments
 (0)