Skip to content

Conversation

seldridge
Copy link
Member

Add timeout exception and runUntilFinished. Use this to replace some usages of ChiselSpec.

This is stacked on #4699. This PR is intended to show where I am going with this and to get buy-in on how tests will look before rolling this out everywhere (to replace more widely used ChiselSpec methods like assertTesterPasses and assertTesterFails.

Release Notes

Add Stimulus object to SimulatorAPI which is intended to contain basic, common stimulus that should be applied to a circuit. These can be chained together in series if desired. For now, only add runUntilFinished which is stimulus that runs for a number of cycles and then throws an exception if the simulation doesn't finish before the timeout.

Comment on lines -37 to +41
behavior.of("BoringUtils.{addSink, addSource}")
behavior.of("BoringUtils.addSink and BoringUtils.addSource")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verilator or the Makefiles we generate have problems with some more exotic symbols. I need to circle back and replace these with simpler ones, like we do for whitespace.

@seldridge seldridge force-pushed the dev/seldridge/chiselsim-chisel-settings branch from 58a1586 to bd1218d Compare February 19, 2025 19:38
runTester(
new ShouldntAssertTester { val dut = Module(new BoringInverter) }
) should be(true)
simulate(new ShouldntAssertTester { val dut = Module(new BoringInverter) })(Stimulus.runUntilFinished(3))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neat.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I was primarily concerned about. Two alternatives to this API are a dedicated simulateWithTimeout or a default argument timeout: BigInt to this method. I liked the factoring in this patch better as it seemed like keeping all stimulus separate was cleaner.

Comment on lines 21 to 35
def runUntilFinished[T <: Module](maxCycles: Int): (T) => Unit = { dut =>
AnySimulatedModule.current
.port(dut.clock)
.tick(
timestepsPerPhase = 1,
maxCycles = maxCycles,
inPhaseValue = 1,
outOfPhaseValue = 0,
sentinel = None,
checkElapsedCycleCount = (count: BigInt) => {
if (count == maxCycles)
throw new Exceptions.Timeout(maxCycles, "Expected a $finish, but none received")
}
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good--we could consider doing a typeclass instead of T <: Module. I say that because this won't be overloadable with other types so if someone wanted to use runUnitFinished for a type like BasicTester (or some future new version of that that has a done output like in the new inline unit testing stuff), then they'd have to give the new function a different name. Not sure if it's worthwhile and we can also add it later in a source-compatible way so not sure it matters now. Just pointing it out.

Copy link
Member Author

@seldridge seldridge Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched to a type class style approach in d33e461. Let me know what you think. (There are comments included in a later commit. It is best to just view the complete diff.)

@seldridge seldridge force-pushed the dev/seldridge/add-runUntilFinished branch from c160dba to 098d81d Compare February 20, 2025 05:08
@seldridge seldridge force-pushed the dev/seldridge/chiselsim-chisel-settings branch 2 times, most recently from 16ec7fe to ac56d63 Compare February 21, 2025 22:07
@seldridge seldridge force-pushed the dev/seldridge/add-runUntilFinished branch from 098d81d to b966876 Compare February 21, 2025 22:07
Copy link
Contributor

@jackkoenig jackkoenig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me!

@seldridge seldridge force-pushed the dev/seldridge/add-runUntilFinished branch from b966876 to 2ed0865 Compare February 22, 2025 03:29
Base automatically changed from dev/seldridge/chiselsim-chisel-settings to main February 22, 2025 03:35
@seldridge seldridge added the Feature New feature, will be included in release notes label Feb 22, 2025
Add a generic Timeout exception that can be used to indicate that a
simulation failed due to something that was supposed to happen within a
timeout, but did not.  This is intended to be used by other, higher-level
Simulator APIs.

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@sifive.com>
Add a new package, `chisel3.simulator.stimulus`, that contains canned
stimulus for use with ChiselSim.

Add one stimulus to this package, `RunUntilFinished`, which will run a
simulation and expect it to terminate naturally (via `chisel3.stop`, i.e.,
a Verilog `$finish`).  If the simulation runs, but does not finish, then
throw a timeout-specific exception.  Use a type class pattern for this, as
suggested by @jackkoenig with implementation generators for `Module`, when
provided with a timeout, and any type when provided with a timeout and a
function to get a `Clock`.

This moves towards removing the unpublished ChiselSpec (which is currently
part of the tests), but forms a core component of how users are expected
to use ChiselSim.  This works towards providing a published replacement
for ChiselSpec that avoids downstream users having to effectively
replicate ChiselSpec.

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@sifive.com>
Replace existing usages of ChiselSpec.runTester with ChiselSim APIs.

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@sifive.com>
@seldridge seldridge force-pushed the dev/seldridge/add-runUntilFinished branch from 2ed0865 to 89ce522 Compare February 22, 2025 04:05
@seldridge seldridge marked this pull request as ready for review February 22, 2025 04:06
@seldridge seldridge merged commit a00bd2d into main Feb 22, 2025
15 checks passed
@seldridge seldridge deleted the dev/seldridge/add-runUntilFinished branch February 22, 2025 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature, will be included in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants