emuforge

Performance

mdemu 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.

The idea

Find out when something can next be seen, and do nothing exactly until then. Before, every part of the console ran for a moment after each of the 68000's instructions, about 13,000 times a frame. Now 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.

Games wait a lot. The 68000, the Z80, the SH-2s, the Mega CD's sub CPU and the SVP all often run a short loop until something happens. Each one has its own way to pass these loops without running them.

Time and the processors

What is doneWhy the result is the same
The VDP gets behind until it could be seen: an interrupt, the Z80's interrupt line, the end of a picture, or an access. That can be many lines later. With something to copy or write (DMA and its FIFO), it goes step by step as beforeWith nothing to copy or write, only its events matter, and they come in the same order at the same pixels. A test with random registers and times compares it with small steps, and checks that nothing that can be seen changes before the time that it says
A wait of the 68000 is replayed. When it goes back in its program, all of it is as it was the last time, and only memory that a write would change was read, the next time around is kept, step by step. Then each step's cycles and what it leaves on its bus come from thereEverything else still happens for each step: memory refresh, stalls, the Z80, and what is beside the console. The 68000 is put at the right step when there is an interrupt, when the VDP takes the bus, or when the Z80 writes to its memory
A Z80 that waits is parked, and does nothing. Where it is only depends on how long it was parkedIt ends with any change that it could see: its interrupt line, its bus and reset lines, and writes to its memory. R is moved on as often as it would be, and a loop that reads or writes R itself is not passed. Its time is brought up to date when the YM2612 needs it
A Z80 that waits for the YM2612 is parked too. Music drivers often read its status again and again, for its timers or the end of its busy time. In Castlevania: Bloodlines, Ecco the Dolphin and Lunar, that was 84 to 96% of all the Z80's cycles that were run. In most of the 21 games that were counted, it was less than 10%Without a write, the status only changes when the busy time ends or a timer overflows. The chip says how long that takes at least, and the Z80 goes on before then. Another value, or a write, ends the wait. A test goes tick by tick from random states of the timers. Another one compares a game that waits for both timers and for the busy time
When both wait, and nothing else runs beside the console, only cycles are counted until something could be seenThe same sums are done in the same order, with refresh and stalls
The rest of a Z80 instruction is passed at onceThe instruction was run in its first cycle. The others only count down. While the Z80 is held for a DMA, it goes cycle by cycle as before
The 68000 reads its memory and most of the cartridge's program directlyNothing else is at these addresses: that is found when the game starts. With a 32X or a Mega CD, and in cartridges with banks, the program is read as before, but for what comes next
The words of a 68000's instruction are read together: up to four with one check, in the cartridge's plain part, in the console's main memory, and in the sub CPU's program RAM. Each opcode then has a handler that knows its size, its operands' modes and its conditionNothing can change these words before the instruction has read them. Before it writes or jumps, it has read them all, and each is left on the bus in its turn. DMA from the decoder only writes when a sample is due, which is never in the middle of an instruction. Other code reads word by word. A test compares both ways for all opcodes. While it was made, it was compared with the old processor in 52 million steps. In the Mega CD's random test, the exact sub CPU reads its program word by word
Only time is added after a step while nothing else has to run: nothing is in the slot, no timer of a controller counts, and the VDP is quiet. A loop that writes cannot be a wait, so no processor is copied for itEach access that can change that says so, and it is found again

A test runs a game that waits in many ways beside a console that takes none of the shortcuts in this table, apart from the last two, on NTSC and on PAL. The state, the picture and sound are the same in each frame, and it checks that waits of both processors were passed.

Drawing

What is doneWhy the result is the same
A dot of a layer is a byte, with its colour, palette and priority. Cells of both planes are drawn whole, and several at once, and plane A is not drawn where the window is. Sprites are drawn 8 dots at a time, with masks that find collisionsTests compare planes, the window and sprites with each dot by itself, 20,000 random cases for each, with everything that they leave for later lines. In a savestate, dots are still the three values that they were
Layers are put together with sums of bytes and no branches, also with shadow and highlight, so the compiler can do several dots at a time. With the debug register, the exact rule draws each dotA test compares all 2,097,152 combinations of a sprite's, plane A's and plane B's dot, with one backdrop colour
A line's colours are made again only when they changedThey are compared with the colour memory for each line. A loaded state cannot leave old ones
A row of a tile is taken apart at once: 8 dots in one 64-bit numberA test compares random rows, also flipped, with each dot by itself

Sound

What is doneWhy the result is the same
The YM2612's samples are made many at once, channel by channel, mostly at the frame's end. Between changes of the envelopes and the LFO, which are found ahead, only phases and feedback move. A silent channel skips ahead, but for its feedback and its last samplesA write first makes the samples of the channel that it changes. The DAC's and the LFO's values are kept for each sample. Channels with SSG-EG use the exact code. Tests compare it with one sample at a time, with random writes, resets and states, and check that envelopes do not change before the time that is found
The PSG goes from change to change, and only catches up for a write and at a frame's end. A write by the Z80 comes before the 68000's running instruction has passed, as everOnly a write changes what it does. A test compares it with each cycle. Another one writes to both chips from the Z80, and compares mixing after each step with mixing once a frame
Sound is mixed once a frame, with both sides filtered together. So are both sides of the Mega CD's soundAdding in another order could change the last bits, so sums keep their order. Tests compare it with one sample at a time, and rounding with Rust's own

The Mega CD, the SVP and the 32X

What is doneWhy the result is the same
The Mega CD's hardware runs at its next possible event, and before what could show or change it: accesses, interrupts, resets and savestates. Samples and the stopwatch are only made when something uses them. A sub CPU that waits is left where it isWhat a wait uses cannot change by itself. Its registers and all that it wrote are as they were the last time. An interrupt that is enabled ends it
The sub CPU gets behind, up to 256 steps, also when it does not waitThe end of each step is kept, so each instruction sees the devices as they are at the end of its own step. Where it uses what time changes, they get that time first. Where it makes them due sooner, it stops at the last step before that. The console's accesses and DMA make it catch up first. A test runs 120 random programs, with accesses by the console, interrupts, the decoder and its DMA, PCM, the drive, graphics, saves, resets and states, and compares what the console sees and the states with going step by step
The SVP runs when the 68000 or DMA reaches its memory or registers, for a savestate, and at least every 16,384 cycles. Its waits are passed. In Virtua Racing, 89% of its instructions were in two loops that waitNothing outside changes what it has while it runs on. So when it jumps back with the same registers and wrote nothing, it does the same again. A test compares random programs with each step
Waits of the SH-2s are passed. A pass may not write, fill a line of the cache or change its order of use, or use what is in the SH7604. Interrupts, resets, DMA and everything that the pass read are looked at again before each runIt is counted as the exact code counts it, with slices and delay slots, so it stops at the same instruction. A communication port is only looked at when the other SH-2 is not behind
A loop that turns out to be no wait is left alone for longer each time, from 12 up to 192 runs of the SH-2, until a wait is passedThis only decides when the processor looks for a wait, and not what a wait is
Loops that only count are passed too, up to where a comparison could change or a number could wrap. In Chaotix, more than half of the SH-2 instructions that were left ran in such loops, and in Stellar Assault nearly 9 of 10Counters are only added to and compared, and nothing else uses them
The 32X gets up to 256 steps behind, and then goes through the same steps one after the other. Most steps ran 256 at a time in the games that were timedAnything that the 68000 could see or change makes it catch up first, and so does the end of a frame. Slices, the order of both SH-2s and where PWM sums are split stay the same. A test makes random accesses, and compares states with a 32X that never gets behind
An SH-2's code in its cache or data array is decoded once, and runs to the end of the line with one check for each instruction. Comparisons run together with their branch, and branches with their delay slotAnything that changes the line, or the order in which it is used, decodes it again: writes, fills, purges, the cache's settings, and loaded states. Cycles and the program counter are written before each access to the bus. Tests compare each kind of instruction, branches and limits with the plain code
An SH-2 remembers the line of its cache that its code is in, and looks for interrupts after what can change them. The 32X's VDP, PWM and other parts do less in each stepEverything that can change that line makes it forget it. PWM sums keep their order and where they are split, as their last bits depend on both

Tests compare 25 kinds of SH-2 loops and 150 random programs with running them, with interrupts, DMA, resets and loaded states.

How much faster it is

Both tables compare mdemu only with its own older build: f5fe28a, from before all of this work, and c7bddf6, which has all of it. Both ran on the same computer, with an AMD Ryzen 9 5950X and Linux.

The core, for each frame

GameForf5fe28ac7bddf6Now takes
Sonic The Hedgehog 3 (USA)Mega Drive1.80 ms0.53 ms29%
Gunstar Heroes (USA)Mega Drive1.74 ms0.49 ms28%
Castlevania: Bloodlines (USA)Mega Drive1.59 ms0.43 ms27%
Ecco the Dolphin (USA, Europe, Korea)Mega Drive1.70 ms0.31 ms18%
Thunder Force IV (Europe)Mega Drive2.00 ms0.74 ms37%
Silpheed (USA)Mega CD2.20 ms0.57 ms26%
Lunar: The Silver Star (USA)Mega CD2.06 ms0.58 ms28%
Virtua Fighter (Europe)32X7.79 ms4.56 ms59%
Knuckles' Chaotix (Japan, USA)32X4.87 ms2.40 ms49%
Doom (Europe)32X6.40 ms2.40 ms38%

The geometric mean is 32% of the time, so the core is about three times as fast in these games. It is the time that one CPU core takes for a frame, on average over 300 to 600 frames from frame 2,400 on. mdemu-dev presses the same buttons in both builds, which get into many games. It has no window and no sound output. Forge's builds.py times --quiet ran both builds in turns on one core, and ran again what other work on the computer disturbed. The lowest of three rounds counts. Before that, a copy of the new build was timed against itself: they differed by 0.5% on average, and by 1.6% at most. In each game, the 68000s' and the SH-2s' memory was the same in both builds at the end.

Final Fight CD is not here. c7bddf6 corrects its drive, so the two builds do not run the same thing any more.

The whole app

Gamef5fe28ac7bddf6
Sonic The Hedgehog 316%7%
Castlevania: Bloodlines13%6%
Thunder Force IV14%8%
Ecco the Dolphin17%7%
Silpheed18%8%
Lunar: The Silver Star14%6%
Virtua Fighter44%26%
Doom38%19%

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 in each game. It was a Wayland desktop with KDE, OpenGL on an Intel Arc A750, and the powersave governor. Other work ran on the computer, and nothing was run again because of it. So this is less exact than the first table, and small differences mean nothing. What the desktop itself needs to show the window is not in it.

Neither table is a promise for another game or computer. They are a few games, and parts of them. Other parts can need more time or less.

The app's sound

On Linux, the app's sound now goes to PulseAudio and PipeWire through forge's own small client. The client library it used before cleared 1 MiB before each read of the sound server's messages. In Sonic 3, on a hidden screen, it did so 469 times a second, and it also asked the server for the time after each request. Now there is one read and one write for each of the 94 requests a second. The core and its sound are the same.

Whole app, % of one CPU coreBeforeAfterSaved
Sonic The Hedgehog 3 (Mega Drive)6.73 (6.68 to 6.79)5.43 (5.28 and 5.58)19%
Silpheed (Mega CD)7.95 (7.93 to 7.98)6.73 (6.72 and 6.75)15%
Doom (32X, Europe)18.33 (18.18 to 18.44)16.93 (16.82 and 17.04)8%

The sound threads went from 1.39% to 0.16% of a core, by about as much in each game. The rest of the differences is noise in the main thread, such as 0.10 in Sonic 3. In Sonic 3, the app's threads went to sleep 387 times a second in place of 615.

This is the app's CPU time, with the kernel's, on one computer on 2026-12-01: each game from power-on without buttons, for 16 seconds at normal speed after the first 8, in a window and at volume 0. Both builds are 76f218b, and only forge's commit and what it needs differ. They ran in turns, twice each, with a copy of the older build too. "Before" has all four runs of the older build and its copy. Their averages differed by at most 0.12. Each run had all of its frames, and none was late. Not checked: the sound by ear, its delay, and playback through ALSA. These numbers are not comparable with the table above, which is one run for each build while other work ran on the computer.

What was checked

Development has the tools. The work was done in two parts. Each was compared as a whole with the core from before it. Between them, some faults were fixed, which change what a few games do (Status).

The first part, compared with f5fe28a

67 games: 41 for the Mega Drive (one of them with the SVP), 17 for the Mega CD and 9 for the 32X. All of them ran for 1,500 frames with buttons that look random, and 21 of them also for 3,000 frames with buttons that get into many games. In all 163,500 frames, memory, the picture, sound and the state are the same. The memory is that of the 68000s and the SH-2s, and the state has everything else, such as the Z80's and the video memory. Then states were loaded in the middle of the same 88 runs, after something else was run, and all went on the same.

The second part, compared with 51b5fd3

This part has the waits for the YM2612, the words that are read together, the single check after a step, and the Mega CD's sub CPU and the 32X that get behind. It also has the decoded SH-2 code, and longer pauses between looking for SH-2 waits. The same 67 games were used, and five more for the Mega CD that use the graphics chip a lot: Batman Returns, Battlecorps, BC Racers, Jaguar XJ220 and Night Striker. In 3,000 frames, they start it from 106 to 9,863 times. They ran for 3,000 frames with buttons that look random. In all 178,500 frames of the 72 games, memory, the picture, sound and the state are the same. Then states were loaded in the middle of all 93 runs, and all went on the same. One change is not in these games, and is in Status: what happens when a 68000 divides $80000000.

Not checked in games, in both parts: whole games, interlaced pictures, programs that use the VDP's debug register, other regions than each game's own, and a 32X with a Mega CD. Tests have some of these.

What was tried and is not here

WhatWhat happened
Going from slot to slot in the VDP while it copies or writes, and counting controllers' timers only while they runslightly slower, also in Sonic CD, where the VDP is often at that
Applying the 68000's writes without moving their listsslightly slower
Letting writes of the same value be part of the 68000's waitsno gain in five games, also where the 68000 hardly waits
Link-time optimization over everything, also with panic = "abort", and a build for CPUs with AVX2no clear gain. The build for AVX2 was about 1% faster
A larger table for the YM2612about 0.2% of a frame, for 12.8 KB of the CPU's cache
Keeping a line that is drawn from the same things as in the frame beforenot built: an estimate of 1 to 4% of a frame, before the work of knowing what changed
Letting the 32X fall behind for a fixed timememory changed in all six games where it was tried. It has to be until the next thing that can be seen, which is how it is now
A 68000 with a handler for each opcode, and a table of 65,536 of themas fast as reading the words together, but the developer tool had 20.6 MB instead of 14.7. A faster way to read single words, for programs in the Mega CD's word RAM, was taken from it
A loop of its own for a 68000 that waits while the Z80 runsno difference in three games
A Z80 with each of its opcodes compiled by itself, and flags in one byte2 to 3% faster in three games by itself, and no difference in 11 games with all of the above
Running steps together on the 32X while one SH-2 waitsslower. Only 4 to 7 steps could be joined at a time, and only 20 to 60% of them
Remembering more waits of the SH-2s, or reading the next decoded instruction aheadno gain, or slower
Drawing with AVX2 instructions: planes, layers and coloursabout 2.5% faster than the plain code in seven games, with everything above, and the output was the same in eight games. The limit that was set before the test was 3%. It needs a crate with unsafe, and a second version for other CPUs

Looking for more

  • Large changes helped and small ones did not. Not doing work saved a lot. Making each step of the main loop a little cheaper was slower or made no difference.
  • Use games in play, and look at pictures of what is timed. In a first try, several games were paused. Then most of the time is spent waiting, and nothing on the screen changes.
  • Count before you build. Counts do not depend on what else the computer does, as times do. They found the SVP's two loops and the SH-2s' loops that count.
  • What is left: in 32X games that draw in 3D, nearly all of the time is in the SH-2s. Small tests suggest that much of that is jumps from one kind of instruction to another that the CPU cannot foresee. Elsewhere, no part is much larger than the others.

From commit 1fd712ad794d. Edit this page