Skip to content

Make the clock jitter a function of the emulated time - #2583

Merged
kevinferrare merged 1 commit into
masterfrom
feature/remove-clock-jitter
Dec 2, 2026
Merged

kevinferrare merged 1 commit into
masterfrom
feature/remove-clock-jitter

Conversation

@kevinferrare

@kevinferrare kevinferrare commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Previously, each read of IEmulatedClock.ElapsedTimeMs stepped a PRNG state, so reading the clock had a side effect, reported time could go backward between consecutive reads, and runs with a different number of clock reads (such as interpreted vs. generated code) diverged.

  • Replace stateful per-read PRNG jitter in ClockJitter with a pure, piecewise-linear function of the emulated time and the seed (offset in [0, 0.01) ms, 1 ms period, splitmix64 endpoints), keeping no mutable jitter state.
  • Keep --ClockJitterSeed and IEmulatedClock.ElapsedTimeMsWithoutJitter (used by Opl3Fm on the mixer thread), and keep VgaPhaseLockTests unchanged.
  • Apply WithJitter in ClockBase.ElapsedTimeMs (WithJitter(ComputeMs(state))) and ClockBase.CurrentDateTime (WithJitter(ComputeMs(state) - state.AnchorMs)), dropping the state.Multiplier > 0 special case so freezing the clock stays continuous.
  • Remove EmulatedClockState.JitterState, the jitter mismatch check in ClockBase.EnsureCanRestore, GeneratedCodeDifferentialReport.WithoutJitterState, and the obsolete generator-state tests; rewrite the two MachineDeviceStateTests rollback tests to use an invalid DOS AllocationStrategy.
  • Update XML docs, README.md, docs/index.html, and .agents/skills/spice86-config/SKILL.md.

Stack

PR 1 of 4 in the input recording stack:

Test plan

  • Failing-first tests on origin/master (CyclesClock_WithSeed_TwoReadsAtTheSameCycleGiveTheSameTime and CyclesClock_WithSeed_ElapsedTimeNeverDecreases): Test run summary: Failed! total: 2, failed: 2, succeeded: 0, skipped: 0 (second differed from first by -0.001052503957907902; ElapsedTimeNeverDecreases failed at cycle 1 with difference -5.250395790668081E-05).
  • Mutation check (removing linear interpolation in RandomClockJitter.OffsetMs): Test run summary: Failed! total: 17, failed: 1, succeeded: 16, skipped: 0 — only CyclesClock_WithSeed_ElapsedTimeNeverDecreases failed, at period boundary cycle 2000 (difference -0.004747008280940435). Reverted.
  • VgaPhaseLockTests (SeededClockJitterBreaksTheVgaPhaseLock and MissingClockJitterKeepsTheVgaPhaseLock): Test run summary: Passed! total: 2, failed: 0, succeeded: 2, skipped: 0.
  • Micro-benchmark (ClockReadBenchmark, uncommitted, 100,000 reads per invoke, .NET 10.0.15 X64 RyuJIT x86-64-v4, AMD Ryzen 9 9950X3D):
    • Before (origin/master): ReadElapsedTimeMs_NoJitter 2.132 ns (0 B), ReadElapsedTimeMs_SeededJitter 2.160 ns (0 B, ratio 1.01).
    • After: ReadElapsedTimeMs_NoJitter 2.132 ns (0 B), ReadElapsedTimeMs_SeededJitter 4.032 ns (0 B, ratio 1.89).
  • dotnet build src/Spice86.sln: Build succeeded. 0 Warning(s), 0 Error(s).
  • Narrow suite (*ClockTests*, *ClockTimeMultiplierTests*, *EmulatedClockStateTests*, *MachineDeviceStateTests*, *VgaPhaseLockTests*, *Opl3FmTests*): Test run summary: Passed! total: 53, failed: 0, succeeded: 53, skipped: 0.
  • Full suite (--filter-not-class '*SingleStepTest*'): Test run summary: Passed! total: 8484, failed: 0, succeeded: 8484, skipped: 0.

History

An earlier version of this PR removed the clock jitter by mistake, and the branch was rewritten from master to keep the jitter as a stateless function of the emulated time.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 00:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain to block approval.

No open issues 🎉

What changed in this PR

Removes read-dependent clock jitter from Spice86 as the first step in the input-recording stack.

Changes:

  • Remove the jitter option, implementation, and clock-state fields; update clock consumers.
  • Delete jitter-only tests and fixtures, tighten timing assertions, and use invalid DOS state for restore-rejection tests.
  • Update CLI documentation and deterministic-timing guidance.
File Description
tests/​Spice86.Tests/​Video/​CgaIntegrationTests.cs Update clock construction.
tests/​Spice86.Tests/​Spice86Creator.cs Remove jitter configuration plumbing.
tests/​Spice86.Tests/​Resources/​VgaTests/​vga_phase_lock.asm Delete jitter-dependent fixture.
tests/​Spice86.Tests/​MouseDriverTests.cs Update clock construction.
tests/​Spice86.Tests/​MouseDriverStateTests.cs Update clock construction.
tests/​Spice86.Tests/​GeneratedCodeDifferentialReport.cs Remove jitter-state normalization.
tests/​Spice86.Tests/​Emulator/​VM/​Clock/​EmulatedClockStateTests.cs Remove jitter cases and assert exact elapsed time.
tests/​Spice86.Tests/​Emulator/​Devices/​Video/​VgaPhaseLockTests.cs Delete jitter-specific tests.
tests/​Spice86.Tests/​Emulator/​Devices/​MachineDeviceStateTests.cs Trigger restore rejection with invalid DOS state.
tests/​Spice86.Tests/​Emulator/​Devices/​Keyboard/​PS2KeyboardReleaseAllKeysTests.cs Update custom clock construction.
tests/​Spice86.Tests/​Emulator/​Devices/​ExternalInput/​PitTimerTests.cs Update clock construction.
tests/​Spice86.Tests/​Emulator/​Devices/​ExternalInput/​PitTimerStateTests.cs Update clock construction.
tests/​Spice86.Tests/​Emulator/​Devices/​ExternalInput/​PitModeTests.cs Update clock construction.
tests/​Spice86.Tests/​Emulator/​Devices/​ExternalInput/​Pit8254Tests.cs Update clock construction.
tests/​Spice86.Tests/​Emulator/​Devices/​ExternalInput/​FloppyDiskTimingServiceTests.cs Update clock construction.
tests/​Spice86.Tests/​Emulator/​Devices/​ExternalInput/​FloppyDiskControllerTests.cs Update clock construction.
tests/​Spice86.Tests/​Emulator/​Devices/​ExternalInput/​DeviceSchedulerTests.cs Update clock construction.
tests/​Spice86.Tests/​ClockTests.cs Remove jitter tests and tighten timing assertions.
tests/​Spice86.Tests/​AsmFixtures/​BiosDeviceScenarioCatalog.cs Remove phase-lock scenario and jitter settings.
tests/​Spice86.Tests/​AsmFixtures/​AsmRunSettings.cs Remove jitter seed setting.
src/​Spice86/​Spice86DependencyInjection.cs Construct clocks without jitter arguments.
src/​Spice86.Core/​Emulator/​VM/​Machine.cs Remove obsolete jitter comments and documentation.
src/​Spice86.Core/​Emulator/​VM/​Clock/​IEmulatedClock.cs Remove separate jitter-free clock property.
src/​Spice86.Core/​Emulator/​VM/​Clock/​EmulatedClockState.cs Remove captured jitter state.
src/​Spice86.Core/​Emulator/​VM/​Clock/​EmulatedClock.cs Remove jitter constructor parameter.
src/​Spice86.Core/​Emulator/​VM/​Clock/​CyclesClock.cs Remove jitter constructor parameter.
src/​Spice86.Core/​Emulator/​VM/​Clock/​ClockJitter.cs Delete jitter implementation.
src/​Spice86.Core/​Emulator/​VM/​Clock/​ClockBase.cs Remove jitter from reads, capture, and restore.
src/​Spice86.Core/​Emulator/​Devices/​Sound/​Opl3Fm.cs Read ElapsedTimeMs directly.
src/​Spice86.Core/​CLI/​Configuration.cs Remove --ClockJitterSeed.
README.md Remove obsolete CLI option documentation.
docs/​index.html Remove obsolete CLI option documentation.
.agents/​skills/​spice86-config/​SKILL.md Remove jitter option and revise timing guidance.

Review effort: Balanced

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

No change in generated code or listings. Base f803d5ce, head 042bff28.

Full diff and dumps (zip) · Run

Previously, each read of IEmulatedClock.ElapsedTimeMs stepped a PRNG state,
so reading the clock had a side effect, reported time could go backward,
and runs with a different number of clock reads (such as interpreted vs.
generated code) diverged.

Compute the jitter offset in [0, 0.01) ms as a pure, piecewise-linear
function of the emulated time and the seed (1 ms period, splitmix64
endpoints). Keep --ClockJitterSeed and ElapsedTimeMsWithoutJitter (for OPL),
remove EmulatedClockState.JitterState and the jitter restore check, update
docs and tests, and add determinism and monotonicity tests.
@kevinferrare
kevinferrare force-pushed the feature/remove-clock-jitter branch from fadb484 to 042bff2 Compare December 2, 2026 13:34
@kevinferrare kevinferrare changed the title Remove --ClockJitterSeed and clock jitter Make the clock jitter a function of the emulated time Dec 2, 2026
@kevinferrare

Copy link
Copy Markdown
Contributor Author

Rebased on the reworked #2583 (jitter kept, applied as a function of the emulated time). New head: 042bff2.

@kevinferrare
kevinferrare marked this pull request as ready for review December 2, 2026 14:11
@kevinferrare
kevinferrare merged commit 5a472ae into master Dec 2, 2026
6 checks passed
@kevinferrare
kevinferrare deleted the feature/remove-clock-jitter branch December 2, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants