Skip to content

Commit b56b519

Browse files
authored
Merge pull request #31 from rameloni/improve-parametricSimulator
Improve parametric and tywaves simulator
2 parents e418da6 + f6fa338 commit b56b519

File tree

9 files changed

+66
-55
lines changed

9 files changed

+66
-55
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,16 @@ simulate(
145145
A simulation can be customized by passing some settings to the simulator. The following options can be specified
146146
for `ParametricSimulator` and / or `TywavesSimulator` classes using the following syntax:
147147

148-
| Setting | Description | Simulator |
149-
|:------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------|
150-
| `VcdTrace` | Enable the VCD output optimizing out signals starting with an underscore (_) in the final verilog | `ParametricSimulator` and `TywavesSimulator` |
151-
| `VcdTraceWithUnderscore` | Enable the VCD output (including "underscored" signals) | `ParametricSimulator` and `TywavesSimulator` |
152-
| `SaveWorkdir` | Save the workdir of `svsim` | `ParametricSimulator` and `TywavesSimulator` |
153-
| `SaveWorkdirFile(name: String)` | Save the workdir with a specific name | `ParametricSimulator` and `TywavesSimulator` |
154-
| `NameTrace(name: String)` | Give a name to the VCD trace | `ParametricSimulator` and `TywavesSimulator` |
155-
| `WithFirtoolArgs(args: Seq[String])` | Pass arguments to `firtool` under the simulation | `ParametricSimulator` and `TywavesSimulator` |
156-
| `WithTywavesWaveforms(runWaves: Boolean)` | Enable the generation of extra debug information (to fully exploit the tywaves project) and (optionally `runWaves=true`) launch the waveform viewer directly once the simulation has been completed | `ParametricSimulator` and `TywavesSimulator` |
148+
| Setting | Description | Simulator |
149+
|:--------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------|
150+
| `VcdTrace` | Enable the VCD output optimizing out signals starting with an underscore (_) in the final verilog | `ParametricSimulator` and `TywavesSimulator` |
151+
| `VcdTraceWithUnderscore` | Enable the VCD output (including "underscored" signals) | `ParametricSimulator` and `TywavesSimulator` |
152+
| `SaveWorkdir` | Save the workdir of `svsim` | `ParametricSimulator` and `TywavesSimulator` |
153+
| `SaveWorkdirFile(name: String)` | Save the workdir with a specific name | `ParametricSimulator` and `TywavesSimulator` |
154+
| `NameTrace(name: String)` | Give a name to the VCD trace | `ParametricSimulator` and `TywavesSimulator` |
155+
| `WithFirtoolArgs(args: Seq[String])` | Pass arguments to `firtool` under the simulation | `ParametricSimulator` and `TywavesSimulator` |
156+
| `WithTywavesWaveforms(runWaves: Boolean)` | Enable the generation of extra debug information (to fully exploit the tywaves project) and (optionally `runWaves=true`) launch the waveform viewer directly once the simulation has been completed | `ParametricSimulator` and `TywavesSimulator` |
157+
| `WithTywavesWaveformsGo(runWaves: Boolean)` | Same as `WithTywavesWaveforms` but without blocking sbt if `runWaves` is `true` | `ParametricSimulator` and `TywavesSimulator` |
157158

158159
> **Note**: open an issue/PR to request new settings.
159160
@@ -227,7 +228,7 @@ The following list shows a summary of the features added by the Tywaves project
227228
- [x] Selectable signal value rendering (with type information attached)
228229
- [ ] Automatic/custom signal value rendering
229230
- [ ] For loops code generation
230-
- [ ] Temporary values (also inside `when` and `otherwise` blocks)
231+
- [x] Temporary values (also inside `when` and `otherwise` blocks)
231232

232233
# Versioning and tools ([ref](https://github.com/rameloni/tywaves-chisel-demo/wiki/Tywaves-internals#tywaves-software-architecture))
233234

example/gcd.test.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
//> using dep "org.scalatest::scalatest:3.2.18"
77

88
// DO NOT EDIT THE ORTHER OF THESE IMPORTS (it will be solved in future versions)
9+
import chisel3._
910
import tywaves.simulator._
1011
import tywaves.simulator.ParametricSimulator._
1112
import tywaves.simulator.simulatorSettings._
12-
import chisel3._
1313
import circt.stage.ChiselStage
1414
// _root_ disambiguates from package chisel3.util.circt if user imports chisel3.util._
1515
//import _root_.circt.stage.ChiselStage

example/myfsm.test.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,15 @@ class MyFSMTest extends AnyFunSpec with Matchers {
8080
circt.stage.FirtoolOption("--emit-hgldd"),
8181
),
8282
)
83+
simulate(new MyFSM(), Seq(VcdTrace, WithTywavesWaveformsGo(true)), simName = "runs_MYFSM_correctly_launch_tywaves_and_go") {
84+
fsm =>
85+
fsm.clock.step(10)
86+
fsm.clock.step(10)
87+
}
8388
simulate(new MyFSM(), Seq(VcdTrace, WithTywavesWaveforms(true)), simName = "runs_MYFSM_correctly_launch_tywaves") {
8489
fsm =>
8590
fsm.clock.step(10)
91+
fsm.io.inputState.poke(MyFSMStates.StateA)
8692
fsm.clock.step(10)
8793
}
8894
}

src/main/scala/tywaves/simulator/ParametricSimulator.scala

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,13 @@ import java.time.{LocalDateTime, format}
1515
*/
1616
object ParametricSimulator extends PeekPokeAPI {
1717

18-
private var simulator = new ParametricSimulator
19-
20-
/** If true, the simulator will be reset before running each simulation */
21-
private var _resetSimulationBeforeRun = false
22-
2318
/** Use this method to run a simulations */
2419
def simulate[T <: RawModule](
2520
module: => T,
2621
settings: Seq[SimulatorSettings] = Seq(),
2722
simName: String = "defaultSimulation",
28-
)(body: T => Unit): Unit = {
29-
if (_resetSimulationBeforeRun)
30-
reset()
31-
simulator.simulate(module, settings, simName)(body)
32-
}
33-
34-
/**
35-
* Use this method to manually reset the simulator and run multiple
36-
* independent simulations
37-
*/
38-
def reset(): Unit =
39-
simulator = new ParametricSimulator
40-
41-
def resetBeforeEachRun(): Unit =
42-
_resetSimulationBeforeRun = true
23+
)(body: T => Unit): Unit =
24+
(new ParametricSimulator).simulate(module, settings, simName)(body)
4325
}
4426

4527
/**

src/main/scala/tywaves/simulator/TywavesInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private[tywaves] object TywavesInterface {
1111
hglddDirPath: Option[String],
1212
extraScopes: Seq[String],
1313
topModuleName: Option[String],
14+
waitFor: Boolean = true,
1415
): Unit = {
1516
{
1617
import scala.sys.process._
@@ -36,6 +37,8 @@ private[tywaves] object TywavesInterface {
3637

3738
// Execute and return to the caller
3839
@unused val process = new ProcessBuilder(cmd: _*).inheritIO().start()
40+
if (waitFor)
41+
process.waitFor()
3942
// No wait for the process to finish
4043
}
4144
}

src/main/scala/tywaves/simulator/TywavesSimulator.scala

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import tywaves.circuitmapper.TypedConverter
77

88
object TywavesSimulator extends PeekPokeAPI {
99

10-
private[simulator] case class Tywaves(runWaves: Boolean) extends SimulatorSettings
11-
val WithTywavesWaveforms: Boolean => Tywaves = (runWaves: Boolean) => Tywaves(runWaves)
10+
private[simulator] case class Tywaves(runWaves: Boolean, waitFor: Boolean) extends SimulatorSettings
1211

13-
private var simulator = new TywavesSimulator
12+
/** Generate tywaves info and optionally run the waveform */
13+
val WithTywavesWaveforms: Boolean => Tywaves = (runWaves: Boolean) => Tywaves(runWaves, waitFor = true)
1414

15-
/** If true, the simulator will be reset before running each simulation */
16-
private var _resetSimulationBeforeRun = false
15+
/**
16+
* Generate tywaves info and optionally run the waveform without blocking sbt
17+
*/
18+
val WithTywavesWaveformsGo: Boolean => Tywaves = (runWaves: Boolean) => Tywaves(runWaves, waitFor = false)
1719

1820
/** Use this method to run a simulation */
1921
def simulate[T <: RawModule](
@@ -44,28 +46,22 @@ object TywavesSimulator extends PeekPokeAPI {
4446
TypedConverter.createDebugInfoHgldd(() => module, simulator.wantedWorkspacePath)
4547

4648
// Run tywaves viewer if the Tywaves waveform generation is enabled by Tywaves(true)
47-
if (finalSettings.contains(Tywaves(true)))
49+
val (runWaves, waitFor) =
50+
if (finalSettings.contains(Tywaves(runWaves = true, waitFor = true))) { (true, true) }
51+
else if (finalSettings.contains(Tywaves(runWaves = true, waitFor = false))) { (true, false) }
52+
else { (false, false) }
53+
if (runWaves)
4854
TywavesInterface.run(
4955
vcdPath = simulator.finalTracePath.get,
5056
hglddDirPath = Some(TypedConverter.getDebugInfoDir(gOpt = true)),
5157
extraScopes = extraScopes,
5258
topModuleName = TypedConverter.getTopModuleName,
59+
waitFor = waitFor,
5360
)
5461
} else if (containTywaves)
5562
throw new Exception("Tywaves waveform generation requires a trace file. Please enable VcdTrace.")
5663

5764
}
5865

59-
/**
60-
* Use this method to manually reset the simulator and run multiple
61-
* independent simulations
62-
*/
63-
@deprecated
64-
def reset(): Unit =
65-
simulator = new TywavesSimulator
66-
67-
@deprecated
68-
def resetBeforeEachRun(): Unit =
69-
_resetSimulationBeforeRun = true
7066
}
7167
class TywavesSimulator extends ParametricSimulator
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tywaves.simulator
2+
3+
import gcd.GCD
4+
import org.scalatest.funspec.AnyFunSpec
5+
import org.scalatest.matchers.must.Matchers
6+
7+
class ImportSimulatorReverseOrder extends AnyFunSpec with Matchers {
8+
describe("Issue 27") {
9+
it("Should import chisel before tywaves") {
10+
import chisel3._
11+
import tywaves.simulator.TywavesSimulator._
12+
simulate(new GCD()) {
13+
gcd =>
14+
gcd.clock.step()
15+
gcd.io.loadValues.poke(true.B)
16+
}
17+
}
18+
it("Should import chisel after tywaves") {
19+
import tywaves.simulator.TywavesSimulator._
20+
import chisel3._
21+
simulate(new GCD()) {
22+
gcd =>
23+
gcd.clock.step()
24+
gcd.io.loadValues.poke(true.B)
25+
}
26+
}
27+
}
28+
29+
}

src/test/scala/tywaves/simulator/ParametricSimulatorSpec.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class ParametricSimulatorSpec extends AnyFunSpec with Matchers {
2525

2626
describe("ParametricSimulator Runs") {
2727

28-
resetBeforeEachRun()
29-
3028
it("runs GCD correctly without settings") {
3129
simulate(new GCD())(gcd => gcdTb(gcd))
3230
}
@@ -90,7 +88,7 @@ class ParametricSimulatorSpec extends AnyFunSpec with Matchers {
9088
}
9189

9290
describe("ParametricSimulator Exceptions") {
93-
resetBeforeEachRun()
91+
9492
it("throws an exception when NameTrace is used without VcdTrace or VcdTraceWithUnderscore") {
9593
intercept[Exception] {
9694
simulate(new GCD(), Seq(NameTrace("")))(_ => gcdTb _)

src/test/scala/tywaves/simulator/TywavesSimulatorSpec.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class TywavesSimulatorSpec extends AnyFunSpec with Matchers {
2323
}
2424

2525
describe("New TywavesFunctionalities") {
26-
resetBeforeEachRun()
2726

2827
it("runs GCD with waveform generation") {
2928
simulate(new GCD(), Seq(VcdTrace, WithTywavesWaveforms(false)), simName = "runs_gcd_with_waveform_generation") {
@@ -66,8 +65,6 @@ class TywavesSimulatorSpec extends AnyFunSpec with Matchers {
6665

6766
describe("Tywaves with ParametricSimulator Functionalities") {
6867

69-
resetBeforeEachRun()
70-
7168
it("runs GCD correctly without settings") {
7269
simulate(new GCD())(gcd => gcdTb(gcd))
7370
}
@@ -131,7 +128,6 @@ class TywavesSimulatorSpec extends AnyFunSpec with Matchers {
131128
}
132129

133130
describe("TywavesSimulator Exceptions") {
134-
resetBeforeEachRun()
135131
it("throws an exception when NameTrace is used without VcdTrace or VcdTraceWithUnderscore") {
136132
intercept[Exception] {
137133
simulate(new GCD(), Seq(NameTrace("")))(_ => gcdTb _)

0 commit comments

Comments
 (0)