snesemu does less work for each frame where that cannot change anything. What it puts out has to be the same
to the last bit: memory, the picture, sound and savestates. It is not a trade of accuracy for speed. No
shortcut knows any game, and none needs a special kind of CPU or a setting. It is all safe Rust, without
generated machine code. nesemu and
mdemu did this first, and most of the ideas are from there.
The idea
Find out when something can next be seen, and do nothing exactly until then. Each part says how long it can
be left alone. That time can be too early, which only costs time. It is never too late. An access, an
interrupt or anything else that could bring it forward ends it, and it is found again. What is found ahead is
not in a savestate. Architecture has the basis of this, which was there
from the start: the picture, the sound side and the chips in cartridges catch up only when something could tell.
A profile came first, of 11 games that were seen in play or in moving scenes. In games without a chip, the sound side took about
40% of the time in most, the backgrounds 15 to 19%, and the main processor 20 to 25%. In Mega Man X2, about 26% was only for
reaching the Cx4. In Star Fox and Doom, about 17 to 20% was around the Super FX and its events. Each idea was counted
in games before it was built, where that was possible.
Sound
| What is done | Why the result is the same |
| The S-DSP is behind the sound processor, and makes up to 32 samples at once. Before, it did one of its 32 steps in each cycle | It says which memory it could use for 32 samples ahead: pages for the voices, and exact bytes for the echo. It catches up before a change of any of these, and before any use of what the echo could write. The same value again changes nothing where it only reads. Random tests check that it uses nothing else |
| It catches up for few uses of its registers. Many instructions read an address before they write it, and many music programs write the same values again and again | Only ENDX, VxENVX and VxOUTX change by themselves, so reading another register cannot tell. Only a write that could do something counts. KON always can, as it keys on again. Only some registers decide which memory is used. A random test writes all others meanwhile |
| With an echo of one entry, only the echo goes sample by sample. Two of the 11 games use that, and had a block for each sample | Each sample reads what the last one wrote. The voices do not, and if one does read it, the exact steps are used |
| The sound processor does whole instructions while there is time for the longest one, which has 12 cycles | There is no second copy of what an instruction does. Each opcode has a function made from the code for single cycles, with the opcode fixed, so that the compiler leaves out all the choices. A test compares both for all opcodes, with each use of the bus |
| Its waits are passed, for a timer or for a port. When it goes back with the same registers, and nothing has changed, it does the same again | A write of the same value is no change. A read of what the S-DSP can change is one, and so is a timer that had counted. Timers say when they next have something, and ports only change between two calls. A round can go back several times: copies are compared after 1, 2, 4 and more steps back |
How much a wait helps depends on the game's music program. It could pass 79% of the sound processor's cycles in Donkey Kong Country,
54% in Super Castlevania IV, 43% in Aladdin, 30% in Mega Man X2, and 0 to 8% in the other seven. Those change something nearly
all the time. What a music program reads and writes was logged. No program was disassembled, as some are the console maker's.
A test runs 120 random programs that play, use the echo, wait, read registers and change themselves, beside the
exact code. It compares what the main processor sees, all samples, the processor and the states.
The main processor
| What is done | Why the result is the same |
| WAI is passed up to the next time that something is due. In Donkey Kong Country, 38% of the processor's cycles were in it | Neither interrupt is active, and until then each of its cycles does nothing but take 6 clocks |
| Loops that wait are passed. When the processor is as it was before, the next time around is run and recorded. If it and main memory are then as they were, its steps are passed | It may only read main memory, the cartridge's own ROM and RAM, and ROM that a chip leaves alone. No DMA may be started, and no HDMA may write to memory. It may write to main memory and undo that, as calls do, and games that go through their tasks. Each step in which something could be due is run as ever, so refresh, HDMA, interrupts and a frame's end come at their own cycles. A recording is used only right after it was made |
| Two bytes of an instruction are read at once from main memory, the ROM, and ROM that a chip leaves alone | Only where nothing is due before the second byte's cycle and both are in the same page. So nothing can change it. Time, cycles and the open bus are as with two reads, and a random test compares that |
A test runs 24 random programs on each of 12 kinds of cartridges, with waits, interrupts, HDMA, loaded states and
resets. It compares the picture, sound and states with running each step, and checks that each kind passed waits.
The picture
| What is done | Why the result is the same |
| Tiles are read once for their 8 lines, and those without a pixel are skipped. In four games that were counted, 40 to 71% of the tile rows had none. With one screen, no window and no large mosaic, they go straight into the screen. Offset-per-tile and modes 5 and 6 still go the older way | Each layer remembers what its tiles were read with. Any change of that, or of VRAM, makes it read them again |
| A line that has not changed is not drawn again (how). 23 to 76% of the shown lines had the same inputs in ten games, and 2% in F-Zero, which moves nearly all of them | All that a line is drawn from is compared, or knows when it last changed. Lines that are drawn in pieces, interlaced pictures and loaded states do not count |
| Math with the fixed colour is a palette of 512 colours, so that each dot is looked up once | The result only depends on the colour and whether math applies. The palette knows what it was made from |
Tests compare all three with the exact way, in all modes, with random changes, regions, interlace, high resolution,
frames without a picture, states and resets. The kept lines need about 155 KB more memory.
Chips in cartridges
| What is done | Why the result is the same |
| ROM and RAM that a chip leaves alone are used directly: with the Cx4, the Super FX, the SA-1 (only ROM), the S-DD1 and the SPC7110 | A chip only takes memory or changes banks after a write to its registers, and the core looks at that right after it. Finding out late that it gave memory back only means asking it once more. A random test compares each kind with asking the chip |
| The SA-1's loops that wait are passed. 79 to 97% of its steps were in such loops, in the games that were counted | Nothing was written or read that changes by itself. Writes by the main processor and DMA, and the timer, end it. A test runs 2,600 random programs, and looks at the chip's interrupt clock by clock |
The Super FX runs ahead of its next event. Any of its instructions can be followed by STOP, so it had 12,065 events a frame in Star Fox and 6,716 in Doom. Only about 2 and fewer than 1 of them changed its interrupt | It never ends a STOP ahead, and goes back before anything could tell. A test compares 132,000 values of its status with going to each time |
| The SA-1 remembers the last 4 KB of ROM that it used, and the Super FX finds what an opcode does with one jump | The first is forgotten when the blocks change, and a test changes them. The second does the same as before, and the tests of the instructions check it |
The app
Each frame's colours were made one at a time. Now red, green and blue are made together, for a whole row in one simple
loop. In a small program that only did this, it took 12.5 µs a frame in place of 58 µs. A test compares all 32,768 colours.
On Linux, the app's sound goes to PulseAudio and PipeWire through forge's own small client. The library it used
before cleared 1 MiB before each read of the sound server's messages, and asked the server for the time after each
request. In Super Mario World on a hidden screen, the old app made 471 of these reads a second. The new one makes one
read and one write for each of the 94 requests a second. The core and its sound are the same. Its
CPU time was not measured in snesemu, and the times below are from before it. The sound was not judged by ear, and its delay was not measured.
What was checked
Each frame was compared with the build from before this work (4d88581): main memory, what the battery keeps, the picture,
sound, and the savestate. Each run started when the console was turned on, and had up to 2,700 frames:
- 383 runs of 295 games, 729,000 frames: all the same. 136 of the games have a chip: 50 with an SA-1, 36 with a DSP, 30 with a Super FX, 6 with a
Cx4, 5 with an SPC7110, 4 with an S-DD1, and 5 others. The rest are a random sample of releases without one, for NTSC and PAL. The
buttons followed a script or looked random. In 81 of the runs no button was pressed, and 16 were on the other region's console. Both
builds refuse three more runs, of two games whose chips are not there.
- Savestates were loaded in the middle of all 383 runs, after something else had run, and each of the next 60 frames was compared. It found
nothing wrong, but for what
snesemu-dev itself compared: in games with a clock, what the battery keeps has the time when the clock was last
read, and loading takes that again. The older build did so too.
- The new build was also compared with itself and
Console::set_exact, in 29 runs. All were the same. Chips in cartridges have no exact
switch. Their crates' tests compare them with going step by step.
tools/check.sh passes. SingleStepTests' cases all agree again, in release builds: 5,080,400 for the 65C816,
and 256,000 for the SPC700, which check its single cycles.
These are parts of games, and not whole ones, and games use little of what a console can do. So what is found
ahead has tests of its own, as the tables above say.
How much faster it is
Both tables compare snesemu only with its own older build: 4d88581, from before all of this work, and the
code of 9f18482, which has all of it. Both ran on the same computer, with an AMD Ryzen 9 5950X, Linux and the powersave governor.
The core, for each frame
| Game | Has | 4d88581 | 9f18482 | Now takes |
| Mega Man X2 (USA) | Cx4 | 0.95 ms | 0.32 ms | 34% |
| Super Ghouls 'N Ghosts (Europe) | | 0.72 ms | 0.31 ms | 44% |
| Doom (USA) | Super FX | 2.21 ms | 0.97 ms | 44% |
| Contra III: The Alien Wars (USA) | | 0.73 ms | 0.33 ms | 46% |
| Kirby Super Star (USA) | SA-1 | 1.91 ms | 0.93 ms | 49% |
| Donkey Kong Country (USA) | | 0.63 ms | 0.31 ms | 50% |
| Aladdin (USA) | | 0.71 ms | 0.35 ms | 50% |
| Star Fox (USA) | Super FX | 1.91 ms | 0.99 ms | 52% |
| Super Castlevania IV (USA) | | 0.61 ms | 0.32 ms | 52% |
| Earthworm Jim (USA) | | 0.84 ms | 0.49 ms | 59% |
| F-Zero (USA) | mode 7 | 0.79 ms | 0.50 ms | 64% |
The geometric mean is 49% of the time, so the core is about twice as fast in these games. It is the CPU time that
one CPU core takes for a frame, on average over 1,200 frames from frame 2,400 on (600 from frame 3,000 in Star Fox). snesemu-dev presses
the same buttons in both builds. Pictures from these frames were looked at: they are from play, or from scenes that move.
Forge's builds.py times --quiet ran both builds in turns, and ran again what other work on the computer disturbed. The
lowest of three rounds counts. A copy of the new build was timed against itself first: they differed by 0.1% on average, and by 2% at most. In each
game, main memory and what the battery keeps were the same in both builds at the end.
The whole app
| Game | 4d88581 | 9f18482 |
| Donkey Kong Country (USA) | 9.8% | 5.7% |
| Super Castlevania IV (USA) | 8.9% | 5.9% |
| Star Fox (USA) | 18.9% | 13.0% |
This is how much of one CPU core the app needs at the game's normal speed, with all of its threads, its window and its
sound. Each number is one measurement of 15 seconds, eight seconds after the start, in the games' intros and
demos, with no buttons. A picture from each run was looked at: both builds show the same scene. It was a Wayland desktop
with KDE. Other work ran on the computer, and nothing was run again because of it, so small differences mean nothing.
What the desktop itself needs to show the window is not in it. The app woke up about 650 times a second in both builds, which was not looked into.
Neither table is a promise for another game or computer. They are a few games, and parts of them.
What was tried and is not here
- All of an instruction's bytes read ahead, for the main processor: up to 2% fewer instructions without a chip.
Kirby Super Star needed about 10% more, as its SA-1 uses the same code, and the other chip games 3 to 4% more.
- One table for what a page has and how fast it is: at most 0.5%. Inlining all helpers of the main processor:
1 to 3% fewer instructions, but the program grew by 2.2 MB.
- Keeping the last four contents of CGRAM, so that lines stay when a game changes it and changes it back: no gain.
- Joining the Super FX's prefixes with the next instruction, and remembering which lines of its cache are loaded:
at most about 4% of Doom's cycles, and nothing clear in Star Fox. Keeping the status for each instruction at all times cost 8% in a
game that hardly reads it, so it is only kept while it is read.
- Finding longer loops of the SA-1: at most 10 to 12% of the steps that were left, so it was not built.
What could still help, with counts but nothing built: loops of the main processor that ask a chip again and
again. They are 83% of its cycles in Kirby Super Star and 67% in Star Fox. In Contra III, Super Castlevania IV and
Earthworm Jim, about 41 to 46% are in loops that change memory each time around, which looks like random numbers. These cannot be
passed as they are.