emuforge

Architecture

How n64emu is put together, and how its parts run and talk to each other. The hardware has a page for each part, with where its facts are from.

Where it is: all parts are joined. The RDP runs on the Vulkan device that the console's options name, and without a name on one that runs on the CPU. The app names one by itself (its GPU setting's Automatic). The trials that find the security chip, and the core's own tests that are not about the RDP, have a part in its place that gives commands their time and draws nothing (NoRdp), so that they need no GPU.

The parts

CrateWhat it isWhat it needs from outside
crates/n64-fputhe arithmetic of the processor's floating point unitnothing. It has no state and no time
crates/n64-vr4300the processor, with its caches, TLB and timera bus for what leaves the caches
crates/n64-rspthe RSP with its memories, registers and DMA. Its inside is a portRDRAM, the RDP's registers and its interrupt
crates/n64-rsp-jitthe RSP's recompiler: one unit of machine code for each microcode. It is written by the RSP's port, from n64-rsp's rulesn64-jit, and n64-rsp for every step that uses the rest of the console
crates/n64-jitthe recompilers' framework, on Cranelift: the code heap, the dispatcher, the code cache, the list of hot code, and calls back into Rust. All unsafe code of the recompilers is herea front end, which says what to compile and how its exits are found
crates/n64-rdpthe RDP and the VI's filters, on Vulkan. Its inside is a portcommands, RDRAM and the VI's registers
crates/n64-cartcartridges: file formats, and ROM, SRAM and FlashRAM on the parallel busthe game's file
crates/n64-pifthe PIF: the joybus with controllers, paks, EEPROM and the clock, the CIC, and the state in which a game startsthe buttons, and the player's time
crates/n64emu-corethe console: time, the bus, RDRAM and its timing, MI, PI, SI, AI, VI, RI, the RDP's command interface, frames, states and saves, and all that joins the partsa game and the buttons
apps/n64emuthe standalone app: forge's forge-app, with a thin layer that turns its input, settings, saves and states into the core'sa game's file, a window, sound and gamepads
apps/n64emu-labthe compatibility lab: forge's forge-sweep, with how a game is known and how its buttons are pressedthe games in its list
apps/n64emu-devruns the core for developers, without a window, and prints numbers: how a game starts, what the RCP does, stops, and hashes of each frame and its picturea game's file

No other crate knows of the core. The core meets each part through a trait of its own (parts.rs), and joined.rs puts the real ones behind them. Tests use simple parts in their place, so that what is between them can be seen.

                       the processor (n64-vr4300) leads
                                  │ SysAD: bus.rs
   ┌─────────────┬──────────────┬─┴────────────┬─────────────┬─────────────┐
 RDRAM and       the RSP        MI, VI, AI     PI ── the     SI ── the PIF
 its chips       (n64-rsp),     and the RDP's  cartridge     (n64-pif): joybus,
 (memory.rs,     which runs     command        (n64-cart)    controllers, paks,
 rdram.rs)       late           interface                    EEPROM, clock
   │                               │
   └── shared with the RDP (n64-rdp, on the GPU), which gets its commands
       when the emulated one starts them

Time

All of the console counts one time: ticks of 187.5 MHz since power-on, which a reset does not set back. A cycle of the processor is 2 of them, and one of the RCP 3. The VI's clock, from which the sound's comes, is a fraction of whole numbers of ticks with its remainder kept, so no time is lost however it is counted (time.rs).

The processor leads

The processor runs until Bus::stop_at: the first tick at which something else has to happen, in the processor's cycles. It uses the bus only at cycles before that, and what it has to do later waits in it. Then everything that is due at that time happens, in a fixed order, and it runs again. The rest of the console runs only up to the stop, and never up to the processor's own time, which can be later: writes that waited in its buffer can still come, with their own earlier times.

What happens by itself is in one list, with one time to compare with: the RSP's next signal, a full sync of the RDP, a block of the PI's DMA, the end of what the SI does, the PIF, the next sound buffer, a line of the picture, and the limit of a frame. A time can be too early, which only costs a stop, and never too late. At the same time, they happen in this order, and an access of the processor comes after them.

What runs late

Most of the console does nothing until something could tell, and then catches up in one go, in time order. What it does then is the same as if it had run all the time. Tests compare both, from random states.

PartIt catches up
The RSPbefore every access of the processor, every event and every state. The processor only stops for it where it could raise its interrupt or write to the RDP's registers (its next_signal). While it catches up, the core's time is the time of each of its calls, so that what else runs late sees them in their order
The RDP's command interfacebefore every use of RDRAM or its channel, and before its registers are used. Only a full sync is an event, found ahead from what is in its buffer
The AIbefore RDRAM that it still has to read is changed, before each of the RDP's commands, before its registers are used, and at the end of a frame. Only the start of the next buffer is an event
The VI's reads of RDRAMbefore every other use of the memory's channel

The memory and the GPU

The processor, the RSP's and PI's and SI's DMA, the AI and the RDP's fetches all use RDRAM through one place (memory.rs), with a model of how long each takes and who waits for whom (Memory and the bus).

The RDP's part is real_rdp.rs: n64-rdp's RDP and its memory, behind the core's trait. RDRAM's bytes are in its memory, and so are their ninth bits, one for each byte, which the core sees as four for each word (the bit order is one constant there). The RDP's crate does its work on the GPU whenever it can, and how long that takes is never seen. The core gives it each command when the emulated RDP starts it, and from then on, what it does can be seen (a guess). Before anything reads what the commands since the last finish could write, or writes what they could use, the core calls finish. A test runs the same programs with a GPU that does each command at once and with one that does nothing until finish, and both end in the same state.

The GPU is chosen by name, and never as the first one found. Graphics::device is the name of a Vulkan device, or a part of it that only one device has. Without one, it is the first device of the CPU type, such as llvmpipe, and no other. graphics_devices lists them for a front end.

After any error of the GPU, every call of the RDP fails with it, and the console stops and says why. It goes on only from a whole state: then a new RDP is made, on the same device or on one that is opened again if it was lost, and all of RDRAM is written again.

Frames

A frame is one field of the picture, and it ends where the picture ends, so that the game's work after that is in the next frame, with newer buttons. A frame always ends: while the VI is off, after the region's usual time, and never after more than 50 ms of the console's time (VI and DPC). At its end, all that runs late catches up. The picture and the sound are made only when they are asked for, and neither changes the console, so a frame that nobody looks at leaves the same state.

How a game starts

  1. n64-cart opens the file, and the saves are read. If they cannot be, the console is not made (StartError::Save), so that nobody writes over that file.
  2. The security chip (CIC) is found out, unless a setting or the save says it. Each candidate runs the game's own boot code on a console of its own, which has nothing of the player's, until the game ends its boot (trial.rs).
  3. n64-pif gives the state in which the boot code begins. What no source gives is in one place, Unknown, which a developer can change.
  4. The parts are made and joined, and the boot code runs as the game's own program.

A reset leaves the RCP as it is (n64brew, PIF-NUS). The processor starts the boot code again, as after a soft reset, which is a guess.

The trials

A trial passes when the game ends its boot. It gives up early only when it is certain that the processor can never go on: nothing is active that the loop could see, no interrupt can be taken, and the processor goes around a loop twice in exactly the same way, with its program and data out of the cache, so that each word that it reads is seen. Otherwise, it waits for the 5 seconds that the PIF gives a game. Nothing in it knows how any boot code works. Which candidate passes first is the order of n64-pif.

States and saves

A state is everything that a game can see, from all parts, each in sections of its own with versions. It is taken after everything has caught up and the GPU has finished. The RDP's part is in n64-rdp's own section (the ninth bits, TMEM and what commands set), and RDRAM's bytes are the core's. It is read into a copy that was made with the RDP, so that its sizes are right, and only taken by the RDP when the rest is right. When one is loaded, all of it is read into copies and checked first, together with whether its parts fit each other in time. Only then do they take the place of what was there. So a state that cannot be used changes nothing.

What batteries keep is part of a state, as games work with that memory. So load_state gives back the saves that it replaced (Loaded), which the app keeps first. A Controller Pak's data stays in the core while another pak is in its port, and Console::pak still gives it. The core writes no files: the app writes what it gets when the numbers of changes say so, and at its end. If the app ends without that, a Controller Pak that was taken out loses what it was given after the last time the app wrote it, as anything else that the app had not written.

Speed

Games wait a lot, and their waits are passed. In Super Mario 64, Star Fox 64 and GoldenEye 007, 60 to 85% of the processor's cycles in the first 600 frames are in loops that do the same again and again, mostly an idle loop of two instructions, until an interrupt comes.

What is doneWhy the output is the same
A loop of the processor that waits is passed (crates/n64-vr4300/src/wait.rs). At a branch back, the processor is compared with what it was at the same branch the last time around. If it is the same, the whole rounds that end before the run's limit are added at onceRegisters, CP0, CP1, the pipeline, the write buffer and the TLB are compared. A number counts all that this would not show: every use of the bus, CACHE, the TLB's instructions, exceptions and ERET, reads of Count and Random, and changes of the interrupt pins, the timer's interrupt and what MTC0 wrote. The caches only change by the processor itself. So the next round does the same, and Count and Random, which are made from the time and the number of instructions, come out as after each step. The last round before the limit is always run as usual
While the processor waits, it is not run for the RSP's stops (Machine::parked). The RSP stops the processor wherever it could raise its interrupt or write to the RDP's registers, which is every 8 to 12 cycles while it runs. Then only the RSP comes up to its timeNothing that the waiting processor sees can change but its interrupt pins. When they change, or anything else happens, the processor first catches up to that time with the pins as they were, as it would have (how a run is cut makes no difference), and then goes on as usual. It is never run past its own timer or a write that waits

Checked: the hashes of the memory, the sound, the state and the picture are the same in each of 600 frames as before, in Super Mario 64 (USA), Star Fox 64 (USA) and GoldenEye 007 (USA), on the Arc A750 and on llvmpipe, and so is the developer program's result for a sample of 40 games. Random programs that wait in many ways (idle, on a word in the cache, on Count, on Random, on a word without the cache, with a store, and loops that count) end in the same state, also cut at random times and with states saved and loaded. A test runs the console with a processor that waits and with one that is run at each stop, and both give the same. n64emu-dev --exact-waits runs each step.

Times, of n64emu-dev --frames 600 --gpu Arc on one computer (the owner's, with an Arc A750), against the build before (main at cb21743), with the change in user cycles: Super Mario 64 6.7 s to 4.7 s (-29%), Star Fox 64 4.9 s to 2.5 s (-49%), GoldenEye 007 12.5 s to 6.4 s (-54%). In the app, Super Mario 64's first 900 frames took 14.1 s of the CPU in place of 16.2 s: its frames 600 to 900 are mostly the RSP's work, which waits do not change.

What the developer program counts for this: waits (branches back, rounds that were found to be waits, run and passed), stops with how many came while the processor waited and how many of these did not run it at all, rdp (calls of commands and their words, finish by who needed it, writes, and the RSP's reads of the command interface's registers), and with --rsp-spins an estimate of the RSP's cycles in loops. In Super Mario 64's first 900 frames, about 9% of the RSP's cycles go around a loop with the same scalar registers, nearly all while its DMA runs.

Later work, each step checked by the hashes of every frame against the build before (the scheduler has the counts, and how it was made the times):

What is doneWhy the output is the same
The RSP leads the running processor up to just before its next call of the bus (Bus::lead, n64-rsp's run_to_call), and is taken back (rewind) when the processor needs something of it earliernothing outside the RSP can see what it did before its next call of the bus. When an access of the processor comes before the lead's end, the RSP goes back to the state from before the lead (mark), and runs up to the access as usual
Its lead goes on across reads of the RDP's registers whose values are known ahead (Dpc::steady_until, Bus::may_call_ahead)they read the same until that time, unless the processor writes them, which takes the lead back
A waiting processor stays parked across every event that it cannot see (Machine::parked)as above: only its interrupt pins, the PIF's NMI and its freeze can change what it does
The RSP is not run while it is halted and nothing of it happens by itselfits run_to gives the same result in any pieces, and the next run that is needed comes before anything could see it
The RSP's recompiler (n64-rsp-jit) runs its program as machine code, and leaves before each step that uses the rest of the console, which the interpreter runseach block does what the interpreter's steps do, and a test compares them from the same state (n64emu-dev --rsp-jit-lockstep does it in games). The scheduler calls the RSP exactly as often as without it
The picture is taken one frame late (Console::previous_picture), so the console does not wait for the GPUit is the same picture, given one frame later. It changes nothing in the console

The app

apps/n64emu is forge's standalone app, and writes no app of its own. Its layer (game.rs) turns what forge holds into the console's controller (the buttons in the core's order, and the stick from forge's axes), the core's settings into Options, and fields into whole pictures with Weaver. The console is made once, with the game's save (which keeps the security chip that a trial found) and each port's Controller Pak, which forge keeps as the player's own. Rumble, the reset button and plugged controllers go to the core's own calls. The status says what is still open.

The picture is not copied on its way to the screen. When the RDP's GPU is the one that forge-app's OpenGL draws with (the same device and driver UUIDs), the RDP writes each picture into one of three buffers whose memory it exports (VK_KHR_external_memory_fd), and OpenGL reads it as a texture buffer (GL_EXT_memory_object_fd); two exported semaphores of each buffer say when the RDP is done with it and when OpenGL is. The RDP weaves interlaced fields there too. forge-app draws it in one pass, each pixel of the window the average of the dots under it. Where they cannot share (OpenGL on another GPU, or a driver without those extensions), the picture is read back once, woven with Weaver, and written into the driver's memory that forge-app gives (Screen::pixels). The render scale (1x, 2x or 4x), which the base has for a console that draws in 3D, is drawn beside the exact picture (the RDP): it changes nothing that the game can see, and can be changed while a game runs.

Each game's hot code and its list of the RDP's shaders are kept in files of the data folder, and given to the core before the first frame (Console::use_hot_code, Console::compile_ahead). They change no result, only how soon code runs compiled and shaders are there.

From commit 9ba9172cfa64. Edit this page