The RSP
The RSP is the second processor of the console, a part of the RCP. It runs small programs that games load into it. They prepare lists of
commands for the RDP, and most games make their sound with it too. It has a scalar unit, much like the main processor without
multiplication, and a vector unit, which works on eight numbers of 16 bits at once.
crates/n64-rsp is a port of the RSP of ares (ISC), by Near, Giovanni Bajo, invertego and the others
in its history. Thank you. The interface to the rest of the console, how it keeps time, and what it finds ahead are our own.
Its attribution says what came from where. The rest of n64emu's core is not from ares.
Facts about the memory map are from n64brew's page Memory_map.
What it has
| Part | What it is |
| IMEM | 4 KB for the program. The RSP runs nothing else, and only the processor and DMA can write it |
| DMEM | 4 KB for data. Its loads and stores use only this. Each byte's address wraps around by itself, so nothing has to be aligned |
| The scalar unit | 32 registers of 32 bits. Nothing overflows, and there are no exceptions |
| The vector unit | 32 registers with eight lanes of 16 bits, an accumulator of 48 bits for each lane, and three registers of flags: VCO, VCC and VCE |
| DMA | between RDRAM and either memory, with one transfer that runs and one that can wait |
| Registers | eight, which both processors see. They have the DMA, the status with eight signals, and a semaphore |
Both processors can see each other only here: in the RSP's memories, its registers, its interrupt, and RDRAM. The RSP gets to RDRAM only with
DMA. Its COP0 registers 0 to 7 are its own, and 8 to 15 are the RDP's command interface, so a program can give the RDP its commands itself.
What the processor sees
| Address | What is there |
04000000 to 0403FFFF | DMEM and then IMEM, again and again |
04040000 to 0407FFFF | the eight registers, again and again |
04080000 to 040BFFFF | the program counter, and a register that always reads as 0 here |
- A write is always of a whole word. The RCP ignores the size of an access. After a store of one byte, all four bytes are
what the processor had on the bus.
- Reading the semaphore sets it. Whoever reads 0 has it, until any write to it.
- In SP_STATUS, each flag has a bit that clears it and one that sets it. Both together do nothing.
- A program ends with
BREAK, which halts the RSP and can set its interrupt. When it is started again, it goes on with the
next instruction, also where the BREAK was in a delay slot. Writing the program counter forgets such a jump.
How long instructions take
A step takes one cycle of the RCP (62.5 MHz), and has one instruction, or a scalar and a vector instruction that run beside each other. The second
one may not use a vector register or flags that the first one writes. A branch is always the last one of its step, and what is in its delay slot is
alone in a step. A step also waits:
- until a scalar register that was loaded, or a vector register that was written, can be read;
- before a store, when there was a load two steps before. Moves between the units and COP0 count as both;
- for one cycle after a delay slot, when the branch was taken. An instruction at an odd word is then alone in its step.
All that a step does happens at its start, also its accesses to registers. A step takes five cycles at most. ares's authors tested its timing
on a console with random programs. Their notes say that it does not cover everything (below).
A DMA, step by step
A program, or the processor, writes the address in DMEM or IMEM, the address in RDRAM, and then the length, which also has the number of rows and the gap between
them in RDRAM. The register that gets the length says which way it goes.
- If no transfer runs, this one starts. Otherwise, it waits, and takes the place of one that already waited. The
registers show the one that runs, and never the one that waits.
- A row starts. The console says how long it takes (
Bus::dma_time), and that does not change any more. When nothing
else uses RDRAM, it is 8 bytes in each cycle. The time is at least one cycle.
- At the row's end, all of its bytes are copied at once, and both addresses go on. An address in DMEM or IMEM wraps around
within its memory. One in RDRAM has 24 bits, and wraps too.
- The next row starts at the same time. After the last one, the length reads as
FF8, and a transfer that waited starts. Here, there is
no time between rows or transfers, as in ares.
For example, a transfer with two rows of 16 bytes that starts at cycle 100 ends its first row at 102, and its second one at 104. At 102, a read by
the processor sees the data of the first row, as the RSP and its DMA go first. At the same time, a row's end
comes before the RSP's own step.
Halting the RSP does not stop a DMA. A reset does, and nothing of the row that runs is copied, as in ares.
How it runs beside the rest
- The core runs the RSP up to a time, in any pieces. The state is the same as with one cycle at a time, and tests compare that with
random programs, accesses and loaded states. What happens at that time is included, so at the same time, the RSP goes first. What an access
starts comes after it: a program that is started at cycle 100 has its first step at 101.
- It says when it could next use anything outside. That is never later than its first use of RDRAM, of the RDP's registers, or of its interrupt, and
SP_STATUS and the DMA's registers do not change before it either. It is the end of a DMA's row or the next step, whichever is first. The
time can be too early, and never too late. It is always later than the last time that the core ran the RSP up to. Tests check this from random states, one
cycle at a time.
- It also says when it could next set its interrupt or write to the RDP's registers. These are the only two things that the rest of the
console sees without an access of its own, so the core can let the RSP get behind until then, as long as it runs the RSP up to the
time before anything else that the RSP could see or change, such as RDRAM. This bound is the shortest way through the program in IMEM
to BREAK, to a write to SP_STATUS or to the RDP's registers, or to the start of a DMA, which could change IMEM. A DMA into IMEM ends it
at the end of its row. It is found again when IMEM changes, which takes a few microseconds.
- A loop that waits keeps this bound short. Its distance to the next of those instructions stays the same each time around, however
long the wait lasts. In made-up programs that wait for their own DMA, the processor stopped about every 4 to 30 cycles, and not every
1.5.
run_to_counted counts this, and another bound that is only there to be counted.
- A loop that waits is passed (
wait.rs, our own). After a jump or branch back, the RSP is compared with what it was the last time
at the same place: its scalar registers, the program counter, the pipeline, and SP_STATUS with the semaphore. If they are the same, and no
instruction in between stored, changed the vector unit, wrote a register of COP0, read one of the RDP's, or was BREAK, the next time around does
the same in as many cycles. Nothing else that an instruction sees can change before the DMA's row ends or the processor does something, and
both make it forget the loop. So all rounds that end before the caller's time or the row's end are added at once, and while it is known,
next_signal and next_event are the end of the DMA's row, or never. Tests compare random programs that wait in many ways (for the DMA, the
semaphore, a signal, the RDP, a count, with stores and with the vector unit) with running each round, in any pieces, with accesses and
loaded states, and step the exact one cycle by cycle to check both times. Rsp::set_exact_waits runs each round.
- A change of DMEM, the semaphore or the program counter is not among these. Others can only see those with an access, and before each access
the RSP first runs up to its time.
- The state is all that the RSP has, also in the middle of a row. What it finds ahead from IMEM is not in it.
What an instruction does is kept apart from how it is found and timed, so that a recompiler can be added in a crate of its own: n64-rsp-jit, below.
The recompiler (n64-rsp-jit)
On by default. n64emu-dev --no-rsp-jit runs the interpreter alone, and --rsp-jit-lockstep compares each block of compiled code with it. It is
the RSP's front end on n64-jit, n64emu's recompiler framework on Cranelift, and has no unsafe code of its own. The interpreter stays, runs every step
that uses the rest of the console, and is the reference. It is our own; what each instruction does in it is written again from the port of ares
(its attribution).
- One unit per microcode, entered by a key that has the program counter and the whole pipeline (
key.rs), so that the time of each step is the
interpreter's from where it starts. A microcode is a family of IMEM's contents that agree on every word that its units were made from, each compared in full:
games change words of IMEM that are no code between tasks (Super Mario 64 (USA) changes the word at 0xEC0 in each task), so all of IMEM would be a new
unit for each task. A unit's entries are the places where runs started or came back from the interpreter, and all that can be reached from them by branches
and jumps whose targets are known; a place is made an entry the second time that it is asked about, and the unit is then made again.
- Nodes: the steps from a place up to a branch and its delay slot, timed and paired when compiling by the interpreter's own rules
(
n64_rsp::recompile::plan_step). A node runs only whole: if a step of it would start at or after the limit, the interpreter runs those steps. The scalar
registers are Cranelift values within a node and are written back at its end; the vector unit is i16x8 SIMD for its common calculations, and the
interpreter's own functions for the others. A jump to an address in a register goes on through a table of the unit's nodes.
- It never calls the console. It leaves before each step that would (MFC0 from and MTC0 to the RDP's registers, MTC0 that can start a DMA, set the
interrupt or halt, and BREAK), and
n64-rsp runs that step with the console, so run_to, run_to_call, run_to_signal, mark and rewind keep
the interpreter's protocol. After a branch back it does the interpreter's check for loops that wait; after a round that read the RDP's registers it leaves
that check to the interpreter, which asks the console, so such loops are passed as without it, and the scheduler calls the RSP exactly as often.
- Checked: every node against the interpreter from the same state on random programs (lockstep), whole runs with accesses, leads that are taken back and
runs to a signal against the interpreter in any pieces, and games (below).
Counters, 900 frames (n64emu-dev --rsp-jit --calls, build 78f3159, 2026-12-02). The hashes of all 900 frames were the same as the interpreter's
(the reference build 23ec1b7), and the scheduler's calls of the RSP the same in number and length as the interpreter's.
| Super Mario 64 (USA) | Banjo-Kazooie (USA) |
The RSP's own runs (n64-rsp's run_to and the others, with a program) per frame, RCP cycles each | 4,923 × 90.6 | 14,876 × 47.4 |
| Runs of the recompiler that ran compiled code, per frame | 4,579 | 7,371 |
| Its cycles in compiled code | 96.7% | 42.9% (most of the rest are rounds of loops that poll the RDP, which the interpreter passes) |
| Units compiled during play (no list of hot code yet), and their time | 43, 14.8 s | 35, 7.9 s |
| Microcodes (families) | 9 | 6 |
What the program accessed, per frame (rsp_accesses, COP0's registers): Super Mario 64 reads SP_DMA_BUSY 4,835 times, SP_DMA_FULL 1,125, SP_STATUS
1,079, DPC_CURRENT and DPC_STATUS 622 each, and writes the DMA's addresses 1,127 each, SP_DMA_RDLEN 438, SP_DMA_WRLEN 689 and DPC_END 621; 1,130 rows of DMA
start and end. Banjo-Kazooie reads DPC_CURRENT and DPC_STATUS 10,700 times each, in loops that wait for the RDP. Between two calls of the console the RSP
runs a median of 16 to 32 cycles in Super Mario 64 and 8 to 16 in Banjo-Kazooie. So runs end at the console, not in compiled code: calls that end
sooner (reads of the RDP's registers, the ends of DMA rows) are the scheduler's to make cheaper (Bus::may_call_ahead lets a lead read the RDP's registers).
Hot code and pieces (n64emu-dev --hot FILE, Console::use_hot_code): a new key gets a piece of its own with only the code
that the unit's other pieces do not have. The RSP's list has each microcode's words and all keys that it was asked about, and is compiled before the
first frame (as whole units, by n64-jit's prepare); a second run compiles nothing during play. The app keeps the list in a file for each game.
User cycles, 900 frames (perf stat -e cycles:u, build 3bfbfb3, 2026-12-02, the core answering may_call_ahead, with other work on the machine at
the same time):
| interpreter | first run | second run |
| Super Mario 64 (USA) | 39.4G | 50.8G (1,597 units in play, 1,590 of them pieces; 1.0 s compiling) | 41.7G (7 units at start, 2.25 s; none in play) |
| Banjo-Kazooie (USA) | 34.0G | 44.6G (982 in play; 0.5 s) | 35.7G (6 units at start, 1.18 s; none in play) |
Where a second run's time goes (perf record, by function, put into kinds; build 3bfbfb3): the processor and the RDP take what they take with
the interpreter. Compiling at start is 24% of Super Mario 64's cycles and 14% of Banjo-Kazooie's. The RSP's own work in play is about 12.5G cycles
in Super Mario 64 against the interpreter's 21.9G, and 9.8G in Banjo-Kazooie against 14.4G (sums of the kinds below, not a measurement of their
own). Per piece of work in Super Mario 64, at 4.7 GHz:
| cost |
| compiled code | 6.0 host cycles (1.3 ns) per RSP cycle, against 54.5 for the interpreter with all that is around it |
| calls from compiled code into Rust (mostly the vector unit's loads and stores, by the interpreter's own functions) | 10.9 host cycles per RSP cycle |
the dispatcher (Jit::run, the directory, keys, places) | about 23 ns per event (5.8 million runs, 4.1 million entries, 6.4 million steps of the interpreter in runs) |
| a step of the interpreter inside a run | about 19 ns |
a step or block that uses the console, run by n64-rsp | about 335 ns, 1.7 million in 900 frames |
So at a call of a few hundred RSP cycles that ends at an access of the console, the RSP would cost about 40% of the interpreter's time; what limits it
is first the calls into Rust for the vector unit's loads and stores, then the steps that use the console, then the start's compiling as whole units.
How it was checked
- It was compared with ares's own RSP, compiled by itself at the same commit, with nothing else of ares: one step at a
time from random states, random programs after each step, DMA in each cycle, and the divisions with all inputs of 16 bits and some of 32. What was left out
is in the differences. That the two agree does not show that ares is right.
- The crate's own tests compare multiplications with accumulators of 48 bits, comparisons and clips with numbers of 32 bits, and loads and stores with
a second way to write their rules. Others have small programs for all of the above.
- The recompiler is compared with the interpreter: every node from the same state on random programs, and whole runs in any pieces
(above). In games, the hashes of 900 frames of two games were the same with and without it.
- Not checked yet: test programs on this core, and the console.
Differences from ares
- Rows of a DMA end at their exact times. In ares, rows end between steps of the RSP, and at most one of them ends after each step. So times can
differ when a program starts a transfer with several rows, or one that has to wait. Such transfers were only compared while the RSP was halted.
- The program counter can be read while the RSP runs. ares gives a random number, as its notes say that the console gives garbage. n64emu is the same each time, so it gives
the program counter. What the console really gives is not known.
- Opcodes that some emulators use for debugging (emux) do nothing, and wait for no register.
- ares lets its parts run ahead of each other, and brings them together at some accesses. n64emu does not. Each access has its own time.
What is not known
- When the bytes of a row move on the console. Here, all of them move at the row's end.
- If there is time between rows or transfers, or before the first one, and how long a row really takes. 8 bytes in each cycle is what ares has, which names no source for it.
- What single step does. The flag can be set and read, and does nothing, as in ares.
- What a reset in the middle of a row does, and what the memories have at power-on. Here they are 0, as in ares.
- If the RSP still waits for older instructions after it was halted and started again. It does here, as in ares.
- Four things that ares marks as unverified: that control register 3 is VCE, when it is read and when it is written, and
that VEQ, VNE and VGE clear the upper half of VCC. VLT does the same, without such a mark.
- Parts of the timing. ares's authors say that the times of instructions that do not exist are not there, and that a few cases are missing in which two instructions cannot
run beside each other.
- Where the accesses of a step are in time, when the step first waits for a register. Here they are at its start.
- What a jump to an address that is no multiple of 4 leaves in the program counter. Here, as in ares, its lowest bits stay,
and the instruction is the one of that word.
What a test program could measure
- When the bytes of a row move to RDRAM. A program fills DMEM with zeros, and starts a DMA of 4 KB in one row to RDRAM. Right after that, it writes
another number to one word after the other, from the last one to the first. Words that have that number in RDRAM were copied after the program
wrote them, and the others before. The same is done from the first word to the last one, and with a wait before the first write. With
the copy at the row's end, as it is here, all that the program wrote during the row arrives.
- When they move from RDRAM. A program starts a DMA of 4 KB in one row to DMEM, and reads one word of it again and again. It counts until
that word changes. The same is done with other words, and with SP_DMA_BUSY. Here, all of them change together with SP_DMA_BUSY, which only
becomes 0 after the last row.
- How long rows take, and if there is a gap. A program counts until SP_DMA_BUSY is 0, for 1 to 512 times 8 bytes in one row, for 1 to 256 rows
of 8 bytes, with and without a gap in RDRAM, and with a second transfer that waits. Here, each takes one cycle for 8 bytes. With RDRAM, the gap and the page in RDRAM can matter,
so the same is done with several addresses. The main processor should run in its cache and not use RDRAM meanwhile.
- What the registers show during a row: the addresses and the length, read again and again. Here they only change at a row's end.
- What halting, a write to the program counter, and the time between halt and start do to a DMA, and to a register that the next
instruction waits for.
- What the program counter reads as while the RSP runs, and what single step does.
None of these has been run yet.