Skip to content

Scheduler

DoubleGate edited this page Oct 16, 2026 · 1 revision

Scheduler: one master clock

Specification: docs/scheduler.md. Decision record: ADR 0029, with the investigation that led to it in ADR 0002.

The scheduler lives in crates/rustynes-core and decides when each part of the console advances. Since v2.0.0 "Timebase" it is a single master-clock model; the earlier dot-lockstep loop is retired (see the end of this page).

How it works

  • One canonical counter. The scheduler keeps one master-clock counter. The CPU, PPU and APU positions are derived from it rather than kept in step by separate counters. The PPU runs 3 dots per CPU cycle on NTSC and Dendy and 3.2 on PAL.
  • The CPU steps an instruction at a time, and each cycle is clocked in two halves. start_cycle catches the PPU up (run_ppu_to) and runs the bus's per-cycle work; end_cycle catches the PPU up to the end of the cycle and ticks the DMC. A cycle with a bus access (read1 / write1) performs it between the two halves; an internal cycle (idle_tick) runs both halves without an access.
  • Why that matters. Because the PPU is caught up to every access, events inside an instruction (a sprite-zero hit on a precise dot, the MMC3 IRQ at dot 260, a mid-scanline scroll write) are visible to the next access without per-game patches.
  • DMA is one per-cycle engine. OAM DMA, DMC DMA and their overlap run through a single unified DMA model inside the CPU's read cycles; their cycle spans follow from the APU's get/put phase. The details, including the documented 2A03 die-revision frontier (ADR 0033), are in docs/scheduler.md.
  • Interrupts are levels. The CPU reads the /NMI and /IRQ levels from the bus every cycle and does its own edge detection and masking (docs/cpu-6502.md).

The bus owns everything mutable

The bus type is SystemBus (it was named LockstepBus until v2.9.8; ADR 0042). It holds the PPU, APU, cartridge and mapper, work RAM, controllers and the open-bus latch. Nes holds the CPU and the bus, and the CPU borrows the bus for each instruction (cpu.step(&mut bus), generic over the rustynes_cpu::Bus trait). The PPU sees a narrower PpuBus trait for mapper-mediated CHR and nametable access; the APU sees no bus at all, because the bus services the DMC sample fetch itself.

Determinism

Given the same seed, ROM and input sequence, the core produces a bit-identical framebuffer and audio. Save states, rewind, run-ahead, movies, regression tests and rollback netplay all depend on this, so wall-clock time, OS randomness and thread scheduling are kept out of the core; frame pacing and run-ahead orchestration live in the frontend.

Historical note: the dot-lockstep model

From the v1.0.0 production core through v1.10.0 the scheduler advanced the PPU one dot at a time with tick_one_dot() and ticked the CPU on every third dot, with several independently incremented counters kept in sync by construction. v2.0.0 replaced it with the model above and broke save-state and movie compatibility by design (ADR 0028). docs/scheduler.md and docs/architecture.md keep the old description as marked historical sections; it does not describe the current code.

Clone this wiki locally