Skip to content

Commit a7effb7

Browse files
committed
[ChiselSim] Use ResetProcedure stimulus, NFC
Refactor the `SimulatorAPI` to use the canned `ResetProcedure` stimulus as opposed to hand-rolling it. Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent 21183dc commit a7effb7

File tree

1 file changed

+8
-59
lines changed

1 file changed

+8
-59
lines changed

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

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package chisel3.simulator
44

55
import chisel3.{Module, RawModule}
6+
import chisel3.simulator.stimulus.ResetProcedure
67
import chisel3.testing.HasTestingDirectory
78
import chisel3.util.simpleClassName
89
import java.nio.file.Files
@@ -34,36 +35,9 @@ trait SimulatorAPI {
3435
.result
3536
}
3637

37-
/** Simulate a [[Module]] using a standard initialization procedure that is
38-
* suitable for any Chisel-generated Verilog module. The commands specified
39-
* in the `body` will run _after_ this initialization procedure.
38+
/** Simulate a [[Module]] using a standard initialization procedure.
4039
*
41-
* The initialization procedure is as follows:
42-
*
43-
* time 0: bring everything up using simulator settings
44-
* time 1: bring reset out of `x` and deassert it.
45-
* time 2: assert reset
46-
* time 3: first clock edge
47-
* time 4 + n: deassert reset (where n == `additionalResetCycles`)
48-
*
49-
* This is doing several times:
50-
*
51-
* 1. There is guaranteed to be a time when FIRRTL/Verilog-based
52-
* randomization can happen at _either_ time 0 or time 1.)
53-
* 2. If time 1 is used for FIRRTL/Verilog-based randomization, then time 0
54-
* can be used for simulator-based initialization, e.g.,
55-
* `+vcs+initreg+random`. Simulator initialization will race with
56-
* FIRRTL/Verilog-based randomization and it is critical that they do
57-
* not happen at the same time.
58-
* 3. Both FIRRTL/Verilog-based randomization and simulator-based
59-
* randomization should not occur on a clock edge, e.g., an edge when
60-
* reset is asserted. This can be yet-another race condition that has
61-
* to be avoided.
62-
* 4. Reset always sees a posedge. This avoids problems with asynchronous
63-
* reset logic behavior where they may (correctly in Verilog) _not_ fire
64-
* if you bring the design with reset asserted. Note: it would be fine
65-
* to do an `x -> 1` transition to show an edge, however, it looks
66-
* cleaner to bring reset to `0`.
40+
* For details of the initialization procedure see [[ResetProcedure]].
6741
*
6842
* @param module the Chisel module to generate
6943
* @param layerControl layers that should be enabled
@@ -77,35 +51,10 @@ trait SimulatorAPI {
7751
module: => T,
7852
chiselSettings: ChiselSettings[T] = ChiselSettings.default[T],
7953
additionalResetCycles: Int = 0
80-
)(stimulus: (T) => Unit)(implicit hasSimulator: HasSimulator, testingDirectory: HasTestingDirectory): Unit = {
81-
82-
hasSimulator.getSimulator
83-
.simulate(module, chiselSettings) { module =>
84-
val dut = module.wrapped
85-
val reset = module.port(dut.reset)
86-
val clock = module.port(dut.clock)
87-
val controller = module.controller
88-
89-
// Run the initialization procedure.
90-
controller.run(1)
91-
reset.set(0)
92-
controller.run(1)
93-
reset.set(1)
94-
clock.tick(
95-
timestepsPerPhase = 1,
96-
maxCycles = 1 + additionalResetCycles,
97-
inPhaseValue = 0,
98-
outOfPhaseValue = 1,
99-
sentinel = None
100-
)
101-
reset.set(0)
102-
controller.run(0)
103-
104-
// Run the user code.
105-
stimulus(dut)
106-
}
107-
.result
108-
109-
}
54+
)(stimulus: (T) => Unit)(implicit hasSimulator: HasSimulator, testingDirectory: HasTestingDirectory): Unit =
55+
simulateRaw(module, chiselSettings) { dut =>
56+
ResetProcedure.module(additionalResetCycles)(dut)
57+
stimulus(dut)
58+
}
11059

11160
}

0 commit comments

Comments
 (0)