PIF-NUS

The PIF-NUS (or PIF, or PIF(P)-NUS on PAL) manages multiple critical functions of the N64 console. It is a physical microchip found on the console's motherboard, which is based on the Sharp SM5 Microcontroller. It is not clear whether SGI or Nintendo intended this to stand for "Peripheral InterFace" or not. While the naming is unintuitive, the Peripheral (or Parallel) Interface is used to read/write to the game ROM and devices like the 64DD; whereas, the PIF handles the following:
 * Console startup and piracy protections
 * Stores the first 2 stages of the Initial Program Load (IPL) that is executed by the VR4300 CPU
 * Console reset button to avoid corrupting save game data
 * Controller and EEPROM read/write via JoyBus protocol

Internal ROMs and RAM
Since PIF is based on the Sharp SM5 which is a programmable microcontroller, its logic is executed by a program that is burnt into an internal ROM, called PIF-SM5-ROM. This program is written for the SM5 4-bit core, and has been dumped via chip decapping. The jago85/UltraPIF_MCUproject on GitHub is a compatible implementation based on the STM32 architecture that can be inspected for further studying what PIF does in details. Everything described in this page is implemented by the means of this internal program.

Moreover, PIF contains a second internal ROM (1984 bytes) and a small RAM (64 bytes). These memories are usually referred to as PIF-ROM and PIF-RAM, but it is important not confuse this PIF-ROM with the previous PIF-SM5-ROM. Both PIF-ROM and PIF-RAM are memory mapped to the VR4300 address space via the SI interface in RCP (so each access actually requires a serial bus transmission and is thus quite slow).

The PIF-ROM contains the first two stages of code for the VR4300 boot process (IPL1 and IPL2) and is only memory mapped to VR4300 during the boot. After the boot process is finished, before jumping into the game code, the PIF locks the PIF-ROM for security reason, so that it cannot be accessed by VR4300 anymore. The PIF-ROM is slightly different between PAL and NTSC console: it actually hardcodes the region and communicate it to VR4300 during the boot via the PIF-RAM.

Dumping the PIF-ROM can be done via software thanks to a loophole: it is in fact possible to boot the console once, setup a hardware breakpoint at  via the MIPS COP0 Watch register, and then soft-reset the console; as soon as the boot resumes, the interrupt will trigger; a registered handler for that interrupt would then be able to read the contents of PIF-ROM that are now unlocked, and dump them somewhere (eg: into SRAM). Check the hcs64/pif_rom_dumper project on GitHub for an example of implementing this technique.

The PIF-RAM is always available to be accessed by VR4300 and is used to perform communication with the PIF. Normally, it is used as part of the Joybus protocol to communicate with controllers and EEPROMs.

RAM-based communication protocol
Communication between VR4300 and PIF happens using the 64-byte PIF-RAM. The last byte of PIF-RAM (offset 0x3F) is called the "command byte" and is interpreted as a bit mask: each bit corresponds to a different command that VR4300 asks the PIF to perform. While PIF is running, it is constantly monitor PIF-RAM and soon as it sees a bit going to 1 in the command byte, it performs the requested function and then turns off the bit. It is possible for the VR4300 to set more than one bit at the same time, but in general they are not fully orthogonal with each other. The rest of the PIF-RAM is used to provide the arguments for the requested command.

Console startup

 * 1) PIF holds all of the console components in reset mode
 * 2) PIF starts communicating with the CIC inside the cartridge
 * 3) CIC sends 1 nibble (4-bits): region identifier (0x1 = NTSC, 0x5 = PAL)
 * 4) CIC sends two 1-byte "seeds" that will be used to compute checksums. We call them IPL2 seed and IPL3 seed. These seeds are sent with some scrambling on the wire, possibly as obfuscation.
 * 5) CIC sends a 6 byte checksum (again, slightly obfuscated). This is the expected result for the IPL2 checksum algorithm (see below).
 * 6) PIF checks that the region identifier matches the region of the console (which is hardcoded within the PIF SM5 ROM itself). This is the actual region check, preventing cartridges of different regions from working oil the console.
 * 7) If the values don't match, the PIF stops the boot (same scenario if no cartridge is inserted)
 * 8) The user would generally reset using the button or power off
 * 9) Remove/Re-insert cartridge (or change games)
 * 10) Power on (go to step #1)
 * 11) PIF writes several booting information (including the two seeds) to the PIF-RAM word at offset   (mapped at  ), so that the CPU can later access them.
 * 12) PIF releases the reset pin for the whole console, allowing the VR4300 to actually boots.
 * 13) The PIF (console) and CIC (cartridge) begins doing a communication protocol which follows the challenge/response authentication pattern.
 * 14) The protocol continues to run as long as the console is powered on. If there is ever a failure in the data exchange or there is no answer (eg: the cartridge is removed), the console will be reset.
 * 15) VR4300 starts running code from address   which is mapped to PIF ROM via SI interface
 * 16) IPL1 is now executed directly fetching opcodes from PIF-ROM.
 * 17) These instructions are executed in this very slow manner. Thankfully IPL1 is only 52 instructions + some looping.
 * 18) It performs some really basic hardware initialization.
 * 19) It then copy the rest of the PIF-ROM (IPL2) to the RSP IMEM. Notice that at this point RDRAM is not initialized yet, so it cannot be used. RSP IMEM is instead available without any initialization and is much faster than PIF ROM thanks to the parallel bus.
 * 20) Jump to RSP IMEM to execute IPL2
 * 21) IPL2 is executed by the VR4300 reading the instructions from RSP IMEM
 * 22) More general hardware initialization
 * 23) The CPU reads the booting information at
 * 24) If the booting information says that it is a 64DD disk, it will jump to
 * 25) Load IPL3 from the cartridge ROM (offset 0x40-0x1000) into the RSP DMEM
 * 26) Run the IPL2 checksum algorithm over the contents of IPL3. This is done using the IPL2 seed provided by CIC at the beginning. The output is a 6-byte checksum.
 * 27) VR4300 asks PIF to verify whether the calculated 6-byte checksum is correct (see PIF command 0x20). PIF compares it with the checksum it received from CIC at boot, and replies with a yes or no. If the checksum does not match. VR4300 halts itself.
 * 28) VR4300 tells the PIF that the PIF-ROM can now be locked (PIF command 0x10).
 * 29) Jump to RSP DMEM to execute IPL3
 * 30) IPL3 is executed by the VR4300 reading the instructions from the RSP DMEM.
 * 31) Initialize RDRAM
 * 32) Depending on reset type
 * 33) Power On: Invalidate VR4300 ICache & DCache
 * 34) Reset : Writeback VR4300 ICache & DCache
 * 35) Now that RDRAM is available, IPL3 copies the second half of itself from DMEM to RDRAM (at address 0x8000'0040) and jumps there. This makes execution even faster, as running from RDRAM also allows instruction cache to be used.
 * 36) The code DMAs the first MB of cartridge ROM to RDRAM. This is a fixed size that cannot be changed and was deemed a good default.
 * 37) Run the IPL3 checksum algorithm over the first MB of ROM. This is done using the IPL3 seed provided by CIC at the beginning. The output is a 8-byte checksum.
 * 38) The 8-byte checksum is compared against the checksum stored at offset 0x10 in the ROM (part of the ROM Header). If it doesn't match, VR4300 halts itself.
 * 39) Reset RSP
 * 40) Clear Interrupts
 * 41) Clear IPL3 from DMEM
 * 42) Clear IPL2 from IMEM
 * 43) Jump to Game code in RDRAM. The initial PC is stored at offset 0x08 in the ROM (part of the ROM Header), though in some IPL3 variants it is slightly descrambled first.

IPL2 checksum algorithm
This is the algorithm performed by IPL2. It uses a 8-bit seed (provided by CIC, which changes across CIC variants) and produces a 6-byte checksum value. It is run over the contents of IPL3 as found in the game ROM to authenticate it. The calculated checksum is then compared against the correct checksum (provided by CIC). This allows to only run the IPL3 variant expected by the CIC.

Notice that the correct checksum value is never transmitted to VR4300: CIC sends it to PIF at boot, and PIF keeps it. Then IPL2 (run by VR4300) computes the checksum value, and sends it to PIF via command 0x20 (see above). This command asks PIF to compare the checksum calculated by VR4300 to that provided by CIC and provides a boolean answer to VR4300.

A reverse-engineered implementation of the checksum can be found on the jago85/PifChecksum repository on Github.

IPL3 checksum algorithm
This is the algorithm performed by IPL3. It is run over the contents of the first megabyte of cartridge ROM to authenticate it. It uses a 8-bit seed (provided by CIC, which changes across CIC variants) plus a 32-bit magic number (hardcoded in the IPL3 itself, which again changes across CIC variants) and produces a 8-byte checksum value. The calculated checksum is then compared against the correct checksum, which is written in the ROM header at offset 0x10.

Notice that the checksum algorithm is slightly tweaked across the different IPL3/CIC variant, even though the core of it is mostly the same.

When building a homebrew ROM using an official IPL3 variant, it is necessary to compute this checksum (using the correct seed, depending on the IPL3 variant) and store the result in the header, otherwise the ROM will not boot on a real console.

A reverse-engineered implementation of the checksum can be found in the n64crc tool. This tool implements all the different variations of the algorithm, depending on the exact IPL3/CIC pair.

* Initial Checksum: computed at the beginning of the checksum algorithm with:  and truncating the result to 32-bits. This value is noted here because many tools (like n64crc) hardcode this value rather than the IPL3 seed.

Console Reset
The reset process is driven by the PIF, which is connected to the physical reset button. The actual reset is done via a NMI to VR4300 which resets it by starting again the full boot process, but it is important to notice that RCP is not reset in any way. The boot code expects the RCP to be idle when the boot is initiated and is not guaranteed to work if the RCP is active in any way (DMAs in progress, RDP drawing triangles, RSP executing code, etc.), which means that it is up to the VR300 to stop issuing commands to the RCP and putting it in idle state before the reset is executed. To do so, VR4300 is given a forewarn that a reset is incoming via an interrupt (aptly called "pre-NMI") and is given grace time of 500ms before the actual NMI arrives.

This is the full sequence:
 * 1) User presses Console Reset button
 * 2) PIF reads the button state
 * 3) PIF toggles VR4300 Interrupt 2 (INT2) also known as "pre-NMI".
 * 4) This is the time and opportunity for the game to finish saving game data and stop issuing commands to RCP to avoid graphics/audio corruption and/or a hard freeze.
 * 5) PIF sends the RESET command to CIC (command )
 * 6) CIC waits for 500ms (grace time)
 * 7) CIC acknowledges the RESET command to PIF by writing a 0 bit.
 * 8) PIF toggles VR4300 Non-Maskable Interrupt (NMI) which resets it.
 * 9) PIF also unlocks the internal PIF ROM so that the boot process can start executing IPL1.

Controller and EEPROM communication
This is the main task of PIF while the game is running. All communication with connected hardware like controllers and accessory PAKs is made through the PIF, using the Joybus protocol.

The VR4300 writes a Joybus packet into PIF-RAM and then triggers a handshake (by modifying the last byte of PIF-RAM which acts as "command" byte). PIF will execute the commands in the packet and provide the results into PIF-RAM. VR4300 would then read there results from there. (TODO: better document this process).