From 85a97536ab347525239f3d4125f868ea032efcaa Mon Sep 17 00:00:00 2001 From: tignear Date: Sun, 18 Oct 2026 18:40:21 +0900 Subject: [PATCH] fix(js): return normally when bounded simulations stop Simulation.runUntil(endTime, { maxSteps }) treated the Rust bounded loop's false result as a timeout even when a design executed $stop. Return normally when an allowed step suspends the simulation, so callers can inspect isStopped() and resume it. Normalize fractional, negative and NaN budgets before the native call and the stop check so effective zero-step budgets still time out, including while a previous stop is pending. A resumed time slot can stop again or exhaust its budget before consuming pending host writes. Preserve dirty inputs after these interrupted resumptions so output reads still perform lazy combinational evaluation. Add TypeScript regressions for stop and resumption, normalized budgets, and pending input writes across repeated stops and budget exhaustion. Extend the Rust SystemVerilog $stop regression to verify bounded-loop results with actual stopped processes. Validation: - TypeScript Simulation unit tests (54 passed), package type checks, and Biome. - NAPI Simulation, timing-addon, cascaded-clock-addon, and e2e tests (168 passed). - Rust stop_suspends_the_simulation_until_it_continues regression. - cargo fmt and Clippy for the SystemVerilog integration test. - Reused the successful NAPI build and Rust checks from the prior revision; native sources are unchanged by this review fix. Fixes #1464 Change-Id: I3756bef676de6da4b8e9884f9025a72083dc21ae --- .../systemverilog/system_functions.rs | 20 ++++ packages/celox/src/simulation.test.ts | 105 ++++++++++++++++++ packages/celox/src/simulation.ts | 15 ++- 3 files changed, 136 insertions(+), 4 deletions(-) diff --git a/crates/celox/tests/frontends/systemverilog/system_functions.rs b/crates/celox/tests/frontends/systemverilog/system_functions.rs index 9db62d543e1..6ab72c2de2e 100644 --- a/crates/celox/tests/frontends/systemverilog/system_functions.rs +++ b/crates/celox/tests/frontends/systemverilog/system_functions.rs @@ -1337,6 +1337,26 @@ fn stop_suspends_the_simulation_until_it_continues() { assert_eq!(sim.time(), 10); assert_eq!(sim.get(count), 6u8.into()); assert_eq!(sim.get(other), 5u8.into()); + + // The bounded API returns false both at a new stop and when no step is + // allowed to resume it. Bindings distinguish these using the stop state + // and whether the budget allowed a step. + let mut sim = initial_simulation(source); + sim.modify(|io| io.set(rounds, 3u8)).unwrap(); + for expected in [1u8, 2, 4, 5] { + assert!(!sim.run_until_bounded(10, 1).unwrap()); + assert!(sim.is_stopped()); + assert_eq!(sim.time(), 0); + assert_eq!(sim.get(count), expected.into()); + assert!(!sim.run_until_bounded(10, 0).unwrap()); + assert!(sim.is_stopped()); + assert_eq!(sim.get(count), expected.into()); + } + assert!(sim.run_until_bounded(10, 1).unwrap()); + assert!(!sim.is_stopped()); + assert_eq!(sim.time(), 10); + assert_eq!(sim.get(count), 6u8.into()); + assert_eq!(sim.get(other), 5u8.into()); } /// The time step a `$stop` suspends continues where it stopped: an edge a diff --git a/packages/celox/src/simulation.test.ts b/packages/celox/src/simulation.test.ts index 9e0d355822b..e7b0072e175 100644 --- a/packages/celox/src/simulation.test.ts +++ b/packages/celox/src/simulation.test.ts @@ -389,6 +389,111 @@ describe("Simulation", () => { ); }); + test("runUntil with maxSteps: returns normally at $stop and can resume", () => { + const mock = createMockNative(); + let stopped = false; + mock.handle.isStopped = vi.fn(() => stopped); + vi.mocked(mock.handle.runUntilBounded!).mockImplementationOnce(() => { + mock.handle.step(); + stopped = true; + return false; + }); + const sim = Simulation.create(TopModule, { + __nativeCreate: mock.create, + }); + sim.dut.d = 42n; + + expect(() => sim.runUntil(25, { maxSteps: 1 })).not.toThrow(); + expect(sim.time()).toBe(5); + expect(sim.isStopped()).toBe(true); + expect(sim.dut.q).toBe(42n); + expect(mock.handle.evalComb).not.toHaveBeenCalled(); + + // No step was allowed to resume the stopped process, so this is + // still a budget exhaustion rather than a newly encountered stop. + expect(() => sim.runUntil(25, { maxSteps: 0 })).toThrow( + SimulationTimeoutError, + ); + expect(sim.isStopped()).toBe(true); + expect(sim.time()).toBe(5); + + vi.mocked(mock.handle.step).mockImplementationOnce(() => { + stopped = false; + return 5; + }); + sim.runUntil(25, { maxSteps: 5 }); + expect(sim.isStopped()).toBe(false); + expect(sim.time()).toBe(25); + }); + + test.each([0.5, -0.5, Number.NaN])( + "runUntil normalizes an effective zero budget while stopped (%s)", + (maxSteps) => { + const mock = createMockNative(); + mock.handle.isStopped = vi.fn(() => true); + vi.mocked(mock.handle.runUntilBounded!).mockReturnValue(false); + const sim = Simulation.create(TopModule, { + __nativeCreate: mock.create, + }); + sim.dut.d = 42n; + + expect(() => sim.runUntil(25, { maxSteps })).toThrow( + SimulationTimeoutError, + ); + expect(mock.handle.runUntilBounded).toHaveBeenCalledWith(25, 0); + expect(sim.isStopped()).toBe(true); + expect(sim.time()).toBe(0); + expect(sim.dut.q).toBe(42n); + expect(mock.handle.evalComb).toHaveBeenCalledOnce(); + }, + ); + + test("runUntil truncates a fractional positive budget before passing it to native code", () => { + const mock = createMockNative(); + const sim = Simulation.create(TopModule, { + __nativeCreate: mock.create, + }); + + sim.runUntil(25, { maxSteps: 5.9 }); + expect(mock.handle.runUntilBounded).toHaveBeenCalledWith(25, 5); + }); + + test.each([true, false])( + "runUntil preserves input writes when resuming a stopped slot (stops again: %s)", + (stopsAgain) => { + const mock = createMockNative(); + let stopped = true; + mock.handle.isStopped = vi.fn(() => stopped); + // Resuming the suspended slot can return before pending host + // writes are settled, both at another stop and at the step limit. + vi.mocked(mock.handle.runUntilBounded!).mockImplementationOnce(() => { + stopped = stopsAgain; + return false; + }); + const sim = Simulation.create(TopModule, { + __nativeCreate: mock.create, + }); + sim.dut.d = 42n; + + if (stopsAgain) { + expect(() => sim.runUntil(25, { maxSteps: 1 })).not.toThrow(); + } else { + expect(() => sim.runUntil(25, { maxSteps: 1 })).toThrow( + SimulationTimeoutError, + ); + } + expect(sim.time()).toBe(0); + expect(sim.isStopped()).toBe(stopsAgain); + expect(mock.handle.evalComb).not.toHaveBeenCalled(); + expect(sim.dut.q).toBe(42n); + expect(mock.handle.evalComb).toHaveBeenCalledOnce(); + // Reading twice must not evaluate twice or notify the scheduler again. + expect(sim.dut.q).toBe(42n); + expect(mock.handle.evalComb).toHaveBeenCalledOnce(); + expect(mock.handle.noteHostWrites).toHaveBeenCalledOnce(); + }, + ); + test("runUntil with maxSteps: timeout error has correct properties", () => { const mock = createMockNative(); const sim = Simulation.create(TopModule, { diff --git a/packages/celox/src/simulation.ts b/packages/celox/src/simulation.ts index 56fd3e998f1..49fbeb41a32 100644 --- a/packages/celox/src/simulation.ts +++ b/packages/celox/src/simulation.ts @@ -439,7 +439,8 @@ export class Simulation

> { * * When `maxSteps` is provided, the Rust run loop takes at most that many * steps and a `SimulationTimeoutError` is thrown if the budget is - * exhausted before reaching `endTime`. + * exhausted before reaching `endTime`. A design that executes `$stop` + * returns normally; use `isStopped()` to detect the suspension. */ runUntil(endTime: number, opts?: { maxSteps?: number }): void { this.ensureAlive(); @@ -451,7 +452,8 @@ export class Simulation

> { this._state.dirty = false; return; } - const max = opts.maxSteps; + // Normalize zero-step budgets before interpreting the native result. + const max = Math.max(0, Math.trunc(opts.maxSteps) || 0); if (!this._handle.runUntilBounded) { throw new Error("This simulation does not support bounded runs"); } @@ -463,9 +465,14 @@ export class Simulation

> { const next = this._handle.nextEventTime(); if (next == null || next > endTime) return; } + const wasStopped = this.isStopped(); if (!this._handle.runUntilBounded(endTime, max)) { - // A step took the pending inputs unless none was allowed. - if (max > 0) this._state.dirty = false; + if (max > 0) { + // A resumed slot can stop or exhaust the budget before pending + // host writes are consumed. Keep them eligible for lazy evaluation. + if (!wasStopped) this._state.dirty = false; + if (this.isStopped()) return; + } throw new SimulationTimeoutError( `runUntil: exceeded ${max} steps at time ${this._handle.time()} (target ${endTime})`, this._handle.time(),