Highlights
JSNES 3.0 replaces the emulation core with a cycle-accurate one. The CPU, PPU and APU now run in lockstep the way the real hardware does, and JSNES passes all 134 tests in the AccuracyCoin accuracy test ROM (up from 107 in 2.1.0). It's also slightly faster than 2.1.0.
The public API hasn't changed. The major version bump is because save states from 2.x can't be loaded (see below).
Breaking changes
- Save states from 2.x are incompatible.
nes.toJSON()now includes aversionfield, andnes.fromJSON()throws"This save state was made with an incompatible version of JSNES."for states saved by earlier versions. (Before, loading an old state into the new core would makeframe()loop forever.) If you persist save states, catch this error and discard the old state. emulateSound: falsenow really skips audio synthesis, as documented. Before, audio was still generated andonAudioSamplewas still called.
Cycle-accurate core
CPU
- Rewritten as a cycle-stepped 6502: every bus access is one CPU cycle, which clocks the APU, advances the PPU 3 dots and samples the interrupt lines. Instruction timing and dummy reads/writes come from the bus accesses themselves, not from a cycle table.
- Interrupts are polled every cycle, so CLI/SEI/PLP latency, the branch delay quirk, RTI timing and NMI hijacking of BRK/IRQ all behave like the real chip.
- Mapper IRQs are now level-triggered: they stay pending until the CPU services them or the mapper releases them. Before, an IRQ requested while the I flag was set was silently dropped. MMC3 releases the line on
$E000writes, and MMC5 on$5204reads.
DMA
- OAM DMA and DMC DMA halt the CPU on its next read cycle, with real halt, dummy and alignment cycles. The CPU's repeated reads during the halt have the right side effects on
$2002,$2007,$4015and$4016. - DMA register conflicts: when the CPU is halted while reading
$4000-$401F, DMA reads also hit the 2A03's internal registers. - Implicit 1-cycle DMC DMA abort, the DMC start delay, and SHx opcodes losing their
& (H+1)term when DMA interrupts them.
PPU
- Rewritten as a dot-accurate renderer: loopy v/t/x/w scroll registers, background fetch and shift-register pipeline, a byte-level sprite evaluation state machine (misaligned OAMADDR, sprite overflow bug, arbitrary sprite 0), and 8 sprite output units with X counters.
- Quirks emulated: the odd-frame dot skip, OAM corruption when rendering is disabled mid-line, the 2-dot delay on
$2001writes,$2004/$2007access during rendering, and the backdrop colour updating while rendering is off.
APU
- The frame counter, length counters, IRQ flags and DMC are exact to the CPU cycle. The DMC timer is clocked on APU cycles, so DMA requests land on the right get/put phase whatever the power-on alignment.
- Audio is synthesised once per output sample, with each channel averaged over the cycles in between.
Performance
Despite emulating much more, 3.0 is a little faster than 2.1.0 (about 4% faster on the bundled benchmark: 967 vs 927 fps on average). The PPU and APU run lazily ("catch-up") and step exactly only on cycles the CPU could observe, the background renders a tile at a time, and each line's sprites are precomputed.
Testing
- AccuracyCoin: 134/134, also checked from different CPU/PPU/APU power-on alignments.
- About 80 test ROMs from nes-test-roms (blargg's CPU, PPU, APU, DMA and MMC3 tests, among others) are now part of the test suite.