emuforge

The processor

The Nintendo 64's main processor is a VR4300 from NEC, a 64-bit MIPS processor (MIPS III) at 93.75 MHz. It has a floating point unit, two caches, a TLB and a timer. n64emu has an interpreter for it, in the crate n64-vr4300. It is written from the manual below. Only a few of its rules were measured on a console (below). What floating point instructions compute is a part of its own (n64-fpu).

"The manual" is NEC's VR4300, VR4305, VR4310 64-Bit Microprocessor User's Manual, U10504EJ7V0UM00 (7th edition). Numbers in brackets are its sections. No test program was used, and no other emulator's source. What the processor sees outside itself, such as RDRAM and the RCP, is the console's part.

How it meets the console

The processor leads. The console lets it run up to a time, the stop, and it calls the console's bus for what its caches do not have. No call is for a time at or after the stop. An access whose turn comes later waits, also in the middle of an instruction. So what is due at a time always comes before an access at that time. The crate's own documentation has the exact rules.

An example, step by step. A program stores to a register of the RCP at cycle 1000, and the next instruction is a long division:

  1. The store goes around the caches, so it goes to the write buffer. It is on the bus at cycle 1004, if nothing else is. The program goes on at once.
  2. The division starts, and needs 69 cycles. It has changed nothing when they have passed.
  3. The write is made before the division's result is there, with its own time. If it makes the RCP raise an interrupt, the division is aborted, and is run again after the interrupt.

The pipeline

The processor has five stages: IC, RF, EX, DC and WB (4.1). They all move together, or all stand still (4.6). So n64emu has one time for the whole pipeline. It goes from instruction to instruction, and adds the cycles in which the pipeline stands still.

  • The next instruction is always fetched before the current one uses memory, as in the pipeline. A store or CACHE cannot change it any more. Its line is filled first, unless the current instruction raises an exception in EX (4.7.3). It is also the slot after a branch or jump.
  • EX goes on while RF and DC wait, and RF starts to wait only after DC (4.6.1, 4.7.3). So of a long division and a cache miss at the same time, only the longer one counts.
  • An interrupt is taken in DC (figure 4-12), so after the cycles that an instruction needs in EX. It comes after the instruction's own exceptions (4.7.6).
  • The slot of a "likely" branch that is not taken does nothing, but has been fetched and needs its cycle.
WhatCyclesFrom
any instruction11.6
MULT, DIV, DMULT and DDIV, also unsigned, in EX5, 37, 8, 69table 3-12
ADD, MUL, DIV and SQRT of singles; of doubles, in EX3, 5, 29, 29; 3, 8, 58, 58table 7-14
conversions to and from integers; CVT.S.D; other floating point instructions, in EX5; 2; 1table 7-14
ADD, MUL, DIV, SQRT or an integer conversion that ends early, in EX27.5.6
the next instruction uses what a load gives, or a floating point result1 more4.6.5, 7.5.6
the next instruction uses the data cache after a store1 more4.6.7
a page that is not in the micro-TLB, but is in the TLB3 more4.6.2
a cache miss, without the bus's time6 or 7 moretables 11-1 and 11-2
an exception5 more: 2 (4.7), and 3 to fill the pipelinefigure 4-1
ERET2 morenote α in chapter 19

A cache miss needs 1 cycle until it is found, 1 to the write buffer, 1 or 2 to meet the bus's clock, 2 to the bus, and 1 back at the end. Between them is the time of the memory, which the bus says. The wait for the bus's clock depends on the absolute time: 3 cycles of the processor are 2 periods of that clock (62.5 MHz). Count goes up with every second cycle (6.3.3).

The caches

Both caches are found by the virtual address and checked with the physical one (11.2). They have their real contents, tags and states. So a game sees old data or instructions where a console would, such as after a DMA without CACHE, or when a program changes its instructions.

  • The instruction cache has 512 lines of 32 bytes. The processor waits for a whole line (4.6.3).
  • The data cache has 512 lines of 16 bytes, and writes a line back when it is replaced (11.3). The doubleword with what the program needs comes first (12.12.2). After a load, the pipeline goes on as soon as that is there (11.3.2). A store waits for the whole line (4.6.7). Another access to the line waits until it is complete, and so does the bus.
  • All operations of CACHE in the manual's table (16.6) are there. Four of them were measured on the data cache of one console, the owner's (record m2-2026-12-01): Index_Write_Back_Invalidate and Hit_Write_Back_Invalidate write a dirty line back and not a clean one, Hit_Write_Back writes nothing of a clean line and keeps it, and Hit_Invalidate drops a dirty line without writing it. n64emu gives the same 24 figures.

The write buffer has 4 doublewords (4.9). It takes stores that go around the caches, and lines that are written back. The pipeline only waits when it is full. Requests go out in order, so a read comes after all earlier writes (12.4.1). Only the dirty line that a miss replaces goes out after the miss's read (12.5.2).

Addresses and the TLB

Kernel, Supervisor and User mode have the address spaces of 5.2, with 32-bit and 64-bit addresses. The TLB has 32 entries, with pages from 4 KB to 16 MB. Instructions also use a micro-TLB of the two 4 KB pages that were used last (1.5.1). If two entries match an address, the TLB shuts down until a reset (5.1). A miss has the vector for 64-bit addresses when the space that the address is in has them (6.4.8). In User mode, Status can reverse the order of bytes (16.1).

Exceptions, resets and stops

All exceptions of chapter 6 are there, in the order of table 6-5, but for what the next section says. There are cold and soft resets, and the NMI. When a read has no answer, the processor does nothing until a cold or a soft reset, and an NMI does not help (14.1). A savestate keeps that. What a write without an answer does is not known, and not emulated.

Where the manual contradicts itself

WhatWhat it saysHere
table 3-12the cycles are "stalled", and also "required"all of the cycles in EX
interrupts and bus errorstable 6-5 has a data bus error first. 4.7.6 has it after a cache miss, and that after an interrupt4.7.6
clearing IP7 in Cause6.4.18 and 14.4: that ends the timer's interrupt. 6.3.6: only IP0 and IP1 can be written6.3.6: only writing Compare ends it
Index_Write_Back_Invalidate16.6: it writes back a line that is not invalid. 11.3.1 and figure 11-6: only dirty lines are written backonly dirty lines
using what MTC1 wrote7.5.1: it has an interlock. 7.5.6: it has nonenone
64-bit floating point instructions in 32-bit User and Supervisor modetheir pages and 6.4.13: reserved. Figure 17-3: "unimplemented operation"reserved
codes of COP1 with a gamma, such as format 18figure 16-1: reserved instructions. Figure 17-3: unimplemented operationsfigure 17-3
the immediate of TLTIU and TGEIUtable 3-18: no sign. 16.6: with its signwith its sign
the vector of a TLB miss6.4.8 goes by the space of the address. 5.2.2 to 5.2.4 can be read as the modeby the address
Randomit is read-only, and its bit 5 can be written (5.4.2)only bit 5 can be written

What is a guess

Each of these is a named constant, or is marked where it is used, so that a measurement can replace it. The manual does not say more.

Time:

  • The wait for the bus's clock: both clocks start together at time 0, also after a reset. An edge at the very start of a cycle is too early. Times are rounded up to whole cycles. Together, this makes the wait 2 cycles if the cycle's number is a multiple of 3, and 1 otherwise.
  • CACHE: 1 cycle more, but for reading a data tag, besides its accesses. 4.6.8 has no number.
  • The write buffer: a write needs 4 or 5 cycles to the bus, as a read's address does after its first cycle. Each store needs one doubleword of the buffer, also one of a part of a doubleword. A data line needs two, and a line of instructions all four. A place is free when the bus is done with the write.
  • The second half of a data line comes 3 cycles after the first one.
  • Accesses around the caches have the same 6 or 7 cycles as a miss. Instructions are fetched one word at a time.
  • The micro-TLB is not used for addresses that are not mapped. Writing the TLB or changing the address space in EntryHi empties it.
  • A store makes the next instruction wait only if both use cached addresses.
  • What MFC0, DMFC0, SC and SCD give has the same interlock as a load. With FR = 0, the two halves of a floating point register count as one for the interlock.
  • Which conversions end early: those of zero, infinity, NaNs and denormal numbers, and of 64-bit integers that are too large.

CP0 and memory:

  • MTC0 and DMTC0 have an effect from the next instruction on (CP0_WRITE). Measured for Count on one console, the owner's (record m2-2026-12-01, our program tools/console/m2.c): MFC0 right after MTC0 to Count read the new value. CP0 is written in WB and read in DC (4.6.9), which is the same cycle for the next instruction. That the other registers are the same is a guess. Chapter 19 asks for more instructions between them, as it is meant for programs and also covers other processors. The results of TLB instructions are there at once.
  • 32-bit addresses: only the low 32 bits of an address count, in all modes. The manual says so for Kernel mode.
  • LLbit is cleared when the linked line leaves the data cache. LL works in the same way without the cache.
  • After a bus error, the line is not valid, and its old contents are gone. A dirty old line is still written back. Fill in CACHE raises a data bus error.
  • When Count and Compare are made equal by writing one of them, the timer's interrupt comes at once, and it stays after 0 is written to Cause. Writing Compare clears it. These were measured on the owner's console (the same record): Count written equal to Compare and one below it set IP7, one above it did not, and so does n64emu. Count goes up with even cycles, which the same record does not fit: read 0 to 4 instructions after it was written, it was 0, 0, 0, 1 and 1 there, and is 0, 1, 1, 2 and 2 in n64emu. On the console it goes up 3 cycles after the write, and every 2 after that. With 5 instructions or more, the next line of the instruction cache comes in between on the console, and one instruction later in n64emu. Neither is changed yet.
  • A shut down TLB finds nothing. TLBP finds nothing if several entries match. PageMask values that are not in table 5-7 are used as they are. Bits of a frame's number that are inside of a page do not count.
  • MTC0 to a 64-bit register writes its 32 bits with their sign, and DMFC0 reads a 32-bit one in the same way. Registers that do not exist are 0.
  • PRId is 0B10 and FCR0 0A00, as one console, the owner's, has them (record m1-2026-12-01): other consoles may have other revisions. KSU = 3 is User mode. When other exceptions come, CE in Cause is 0.
  • With the order of bytes reversed, the two instructions of a doubleword change places too.
  • After turning on and after a reset: registers and caches are 0 at first. The TLB's entries are for addresses that are never looked for. Resets keep the caches, and lose the writes that wait.

Instructions:

  • 32-bit instructions with registers that are not sign-extended do what the manual's formulas say. It calls the result meaningless.
  • Divisions by zero, but for DIV: B.1.6 has values without naming an instruction. The most negative number divided by -1 gives itself.
  • What the manual calls undefined for floating point registers: with FR = 0, an odd number in arithmetic or with 64 bits is the even one before it. A 32-bit result leaves the upper half as it was.
  • A branch in a delay slot counts its own target and return address from its own address. Codes of CACHE that are not in the table do nothing.
  • CFC0 and CTC0 do nothing. Figure 16-1 names them, and their pages only say that they are "not valid for CP0", with no exception.
  • CP2's instructions, which figure 16-1 names, need CU2 (6.4.12), and then do nothing. BC2's condition is false. Only CP3's are reserved instructions (1.4.5).
  • Hazards of HI and LO (MFHI or MFLO right before a multiplication or division) do nothing. A branch right after a comparison waits, and sees its result. A change of FCR31 is seen at once.

What is missing

  • The multiplication bug of some of these processors. It is not known what the wrong result is. Its place is marked in the code, and it is off.
  • Low power mode (RP in Status) is kept, and does not change the clock.
  • BE and EP in Config are kept, and do nothing. The manual only allows a change of them right after a cold reset.
  • The CP0 bypass interlock (4.6.9) needs an instruction that has raised an exception, and one right after it. The second one is always aborted, so there is nothing that could wait.
  • Writes to the processor's interrupt register from outside (14.5) are not there. The bus gives the pins instead.
  • A recompiler. One was built and taken out, as it was never faster (below). The interpreter is the only way, and would stay the reference for a new one.

How it is tested

The tests have a small bus and a small assembler. They run every encoding of the opcode tables (figures 16-1 and 17-3) with random free fields, after a cold reset, and check that what the tables name is no reserved instruction. They also check each group of instructions, the order of exceptions, the TLB, the caches and the times in the table above. What they expect comes from the manual. Where that says nothing, they check what n64emu does, so that a change of a guess is seen.

Random programs are run again and again, cut in different ways: in as few pieces as possible, one instruction at a time, in random pieces, with a state saved and loaded at each cut, and without any shortcuts (Vr4300::set_careful, which is for tests and off otherwise). A device changes interrupts, memory and the stop at random times. All calls to the bus with their times, and the whole state, have to be the same. Two states are only compared after the same last stop.

cargo run --release --example speed -p n64-vr4300 measures small loops. It shows the shape of the interpreter, and not how fast games are.

Speed

Where the time goes, by perf and by samples of the call stack, in the first 900 to 3000 frames of games with n64emu-dev --gpu Arc on one computer (the owner's, a Ryzen 9 5950X), on main at 62d744d: the processor's own code is about 25% of all time in Super Mario 64, Banjo-Kazooie and Ocarina of Time, 23% in Star Fox 64, 14% in Mario Kart 64 and 9% in GoldenEye 007. The RSP is about half. F-Zero X is an exception: in its start, where the core tries the boot of several chips (about 8 s), 94% is the processor, and most of that is looking for addresses in the TLB.

No part stands out. Of the processor's own time, the choice of what an instruction does is 13 to 20%, the loop of run and attend 10 to 15% (the RSP stops the processor every 8 to 12 cycles while it runs), fetching 12 to 15%, the interlock after loads 7 to 11%, and the floating point unit's arithmetic about 1 to 5%. In a loop of integer instructions, the interpreter runs about 75 of the computer's instructions for each of the processor's, at 3.7 of them in a cycle: the time is the bookkeeping of each instruction, and not its decoding.

What is done: another line of kseg0 that the instruction cache has is fetched without the slow path. What an instruction needs for the interlock is kept for its word (at the place of its address in the cache, checked by the word, so that it is right whoever wrote the cache). The next word from the same line is fetched without a call. The hashes of each of 900 frames are the same as before in Super Mario 64, Banjo-Kazooie, Ocarina of Time and GoldenEye 007. In Super Mario 64 and Banjo-Kazooie, 900 frames need 3% fewer of the computer's instructions and 2 to 3% fewer cycles. The loops of the example went from 253 to about 265 million instructions a second for integers, 208 to 250 for loads and stores in kseg0, and 107 to 135 for doubles.

What was tried, and did not help: one table for all opcodes and SPECIAL's functions (no difference in the example), a match in the TLB without branches (6% more of the computer's instructions in F-Zero X's start), and a quicker way for ADD, SUB and MUL on the host's floating point unit, exact by TwoSum and a fused multiply-add (2c626bd, taken out again): the exact way already needs only about 5 ns for each of these, and the quicker one was no faster. Floating point arithmetic is at most 4.5% of the instructions (Banjo-Kazooie: MUL.S 1.1%, ADD.S 0.6%, MUL.D and ADD.D 0.6% each), and 0.1 to 1.5% in the other games.

A recompiler that was tried

It was built on 2026-12-02, and taken out the same day, as it never ran faster than the interpreter. Its code is in the history before commit bae7f7d. What it was, and what was learned:

  • It was a front end of n64-jit, the framework that the RSP's recompiler uses too, on Cranelift. It compiled what is in the instruction cache, as the console also runs only that, in units of the code that one window of 4 KB could reach. Lines were checked by their words, never by a hash. Instructions that it did not compile were calls of the interpreter's own step from compiled code, and it was exact at every milestone: random programs cut in all the ways above, each block compared with the interpreter in whole games (up to 127 million blocks in 900 frames of Banjo-Kazooie), and the hashes of every frame of four games.
  • Why it was not faster (Super Mario 64 (USA), 900 frames, a second run with its hot code compiled at the start, user cycles by perf): compiled code needed about 56 of the computer's cycles for each instruction that it ran itself, and the interpreter 46 for each, with its slow ways and its bookkeeping. Units were tiny, and were entered once in about 6.5 instructions (24.4 million entries in 900 frames), so each entry paid the dispatcher and the state's loads and stores for a few instructions. Each instruction became about 80 of Cranelift's, a load or a store of the state for each register that it used. Compiling took about 120 µs for each instruction, so the hot code of Super Mario 64 took 14 to 16 s to compile at the start.
  • The first version, before the framework, was exact too and slower in games for the same reasons: compiling during play took 27% of the time, and code that was compiled only 2.6%.
  • A new attempt starts from a written design, with units that go on through jumps to registers, shared exits, a limit on the IR for each instruction, and a test on hot loops before it is joined. The interpreter, at 46 cycles for each instruction, is the cheaper place to start.

From commit 9ba9172cfa64. Edit this page