The '''Joybus Protocol''' is a proprietary, non-standard, serial protocol by which the N64's [[Serial Interface]] (as well as the GameCube and Game Boy Advance) communicates with controllers, controller accessories ([[Controller Pak]], [[Rumble Pak]], etc.), [[Randnet Keyboard|keyboards]], [[Mouse|mice]], [[Game Pak|game cartridges]], and other devices plugged into the console.
The protocol is run between the [[PIF-NUS]] and the various joybus devices. PIF-NUS acts as a master in the protocol, though in general it only communicates with accessories as instructed by VR4300. In fact, the joybus protocol is normally encapsulated in a [https://n64brew.dev/wiki/PIF-NUS?wvprov=sticky-header#Joybus_frame_(controller_and_EEPROM_communication) frame] written in PIF RAM by VR4300, and replies are written back in the PIF-RAM for the VR4300 to accesses them.
= Operation =
[[File:Joybus_Bits.svg|thumb|270px]]
The protocol utilizes four types of bits: Zero, One, the Console Stop Bit, and the Controller Stop Bit. Zero, One, and the Controller Stop Bits are 4μs long, while the Console Stop Bit is 3μs.
Communication is always initiated by the console, by sending an 8 bit (one byte) command to the device plugged in (not necessarily always a standard controller), followed by additional arguments (zero or multiple bytes). A Console Stop Bit will usually follow (though the usage of this stop bit can be disabled or replaced with a wait time, see [https://n64brew.dev/wiki/PIF-NUS?wvprov=sticky-header#Flag_bits_in_PIF_command PIF Joybus flag bits]). For example, when the console wishes to write to a Controller Pak, it will send 0x03, followed by a 2 byte address, 32 bytes of data, and then the stop bit.
The controller or device will read this command and any extra data, and then respond appropriately. The reply is normally made by at least one byte, but can contain multiple bytes, followed by the controller stop bit.
The default state of the data pin is HIGH. If the connected device isn't in the process of sending a signal, it should not default to sending a HIGH signal (either set to LOW for reading, high-impedance, or disconnect). Wrongly setting the pin HIGH may disrupt the console and will prevent intermediate devices from reading any console signals.
=== Reset ===
The joybus protocol appears also to support a special "hardware reset" command on the wire, in which the line is pulled low for 1 ms. To ask the PIF to generate this signal, the frame must contain a [[PIF-NUS#Escape codes|special escape code]] (0xFD) or a [[PIF-NUS#TX byte: special flags|TX flag]] must be set, so this signal is sort of an "off-band" reset sent to the device.
= Command List =
Because all commands are exactly 8 bits long, there can be a total of 256 commands. However, only about 16 are known for the N64, and a few more for GameCube and Game Boy Advance.
The protocol architecture assumes that all Joybus devices must support two commands: 0x00 Info, and 0xFF Reset and Info. Besides these two commands, all other commands are actually device-specific. Software running on the N64 is supposed to first use the 0x00 Info command to discover connected devices, and then send them their own appropriate commands. In general, difference devices could thus reuse the same command ID, but this was never done for officially devices. This might be seen also a safeguard against the software having bugs or race conditions in device detection and thus ending up sending the wrong command for a device.
Official devices never reply to commands they do not support, causing a timeout to be reported in the [[PIF-NUS#RX byte: special flags|RX flags in the PIF frame]].
{| class="wikitable"
! Command
! Description
! Console
! Devices
! Tx Bytes
! Rx Bytes
{{ProtocolTableRow| 0xFF | Reset & Info | N64, GC, GBA | All | 1 | 3 }}
{{ProtocolTableRow| 0x00 | Info | N64, GC, GBA | All | 1 | 3 }}
{{ProtocolTableRow| 0x01 | Controller State | N64 | [[Controller]], Mouse, Densha de Go, Dance Pad, Fishing Rod | 1 | 4 }}
{{ProtocolTableRow| 0x02 | Read Controller Accessory | N64 | [[Transfer Pak|Transfer]], Controller, [[Bio Sensor]], and Rumble Paks | 3 | 33 }}
{{ProtocolTableRow| 0x03 | Write Controller Accessory | N64 | Transfer, Controller, Bio Sensor, and Rumble Paks | 35 | 1 }}
{{ProtocolTableRow| 0x04 | Read EEPROM | N64 | Cartridge | 2 | 8 }}
{{ProtocolTableRow| 0x05 | Write EEPROM | N64 | Cartridge | 10 | 1 }}
{{ProtocolTableRow| 0x06 | RTC{{sup|'''(1)'''}} Info | N64 | Dōbutsu no Mori (Animal Forest) Cartridge | 1 | 3 }}
{{ProtocolTableRow| 0x07 | Read RTC{{sup|'''(1)'''}} Block | N64 | Dōbutsu no Mori (Animal Forest) Cartridge | 2 | 9 }}
{{ProtocolTableRow| 0x08 | Write RTC{{sup|'''(1)'''}} Block | N64 | Dōbutsu no Mori (Animal Forest) Cartridge | 10 | 1 }}
{{ProtocolTableRow| 0x09-0x0D | Unknown | N64 | [[Voice Recognition Unit]] | ? | ? }}
{{ProtocolTableRow| 0x0E | Reserved | | | ? | ? }}
{{ProtocolTableRow| 0x0F | Unknown | | | ? | ? }}
{{ProtocolTableRow| 0x10 | Reserved | | | ? | ? }}
{{ProtocolTableRow| 0x11 | Reserved | | | ? | ? }}
{{ProtocolTableRow| 0x12 | Reserved | | | ? | ? }}
{{ProtocolTableRow| 0x13 | Read Keypress | N64 | [[Randnet Keyboard]] | 2 | 7 }}
{{ProtocolTableRow| 0x13 | Read GB{{refn|name=luigiPrinter|LuigiBlood (2019). [https://luigiblood.tumblr.com/post/187348407478/reverse-enginnering-the-unreleased-gameboy-printer Reverse enginnering the unreleased GameBoy Printer COLOR].}} | N64, GB | [[64GB Cable]], MBC4 GB Cartridge | 3 | 33 }}
{{ProtocolTableRow| 0x14 | Write GB{{refn|name=luigiPrinter}} | N64, GB | [[64GB Cable]], MBC4 GB Cartridge | 35 | 1 }}
{{ProtocolTableRow| 0x14 | Read GBA{{refn|name=gbatekSIO|GBATEK. [https://problemkaputt.de/gbatek-sio-joy-bus-mode.htm SIO JOY BUS Mode].}} | GC, GBA | GBA | 1 | 5 }}
{{ProtocolTableRow| 0x15 | Write GBA{{refn|name=gbatekSIO}} | GC, GBA | GBA | 5 | 1 }}
{{ProtocolTableRow| 0x16-0x2F | Unknown | | | ? | ? }}
{{ProtocolTableRow| 0x30 | Force Feedback | GC | Steering Wheel | ? | ? }}
{{ProtocolTableRow| 0x31-0x3F | Unknown | | | ? | ? }}
{{ProtocolTableRow| 0x40 | Short Poll | GC | Controller | ? | 8 }}
{{ProtocolTableRow| 0x41 | Read Origin | GC | Controller | ? | ? }}
{{ProtocolTableRow| 0x42 | Calibrate | GC | Controller | ? | ? }}
{{ProtocolTableRow| 0x43 | Long Poll | GC | Controller | 3 | 10 }}
{{ProtocolTableRow| 0x44-0x53 | Unknown | | | ? | ? }}
{{ProtocolTableRow| 0x54 | Poll | GC | Keyboard | ? | ? }}
{{ProtocolTableRow| 0x55-0xFE | Unknown | | | ? | ? }}
|}
'''{{sup|(1)}}''' ''Real Time Clock''
= Command Details =
=== 0xFF - Reset / Info ===
While identical to 0x00 in what data is returned to the console, if a device has an intended reset function, it should be performed when this command is sent. The N64 controller for example will reset the internal position of its analog stick to (0, 0), essentially recalibrating it. Other devices may have similar functionality, or none at all, but either way they should still send back the same data as if the 0x00 command was sent.
=== 0x00 - Info ===
This command requests information about the device. Always contains a 2-byte identifier and one byte of extra data.
{| class="wikitable"
! Identifier
! Console
! Device
! Third byte (if empty, no details known)
|-
|0x0000
|N64
|None (no device connected)
|Always 0x00
|-
| style="text-align:center;" | 0x0500 || style="text-align:center;" | N64 || Controller || Bit 0 and 1 (mask 0x3) are a 2-bit value:
* 0: never seen in testing
* 1: an accessory is currently inserted
* 2: an accessory is currently not inserted
* 3: a new accessory was inserted since the last Info command
(see below for more information)
Bit 2 (mask 0x4): checksum error in previous command
{{ProtocolIdentifierRow| 0x0500 | N64 | Dance Pad }}
|-
| style="text-align:center;"| 0x0500 || style="text-align:center;"| N64 || Densha de Go || Always 0x02
{{ProtocolIdentifierRow| 0x0001 | N64 | VRU }}
{{ProtocolIdentifierRow| 0x0200 | N64 | Mouse }}
{{ProtocolIdentifierRow| 0x0002 | N64 | Randnet Keyboard }}
{{ProtocolIdentifierRow| 0x0003 | N64 | 64GB Cable, MBC4 GB Cartridge }}
|-
| style="text-align:center;"| 0x0080 || style="text-align:center;"| N64 || 4 Kbit EEPROM || Bitfield: 0x80=Write in progress
|-
| style="text-align:center;"| 0x00C0 || style="text-align:center;"| N64 || 16 Kbit EEPROM || Bitfield: 0x80=Write in progress
{{ProtocolIdentifierRow| 0x0004 | GBA | Game Boy Advance }} || Lower 8 bits of GBA's JOYSTAT register.
* Bit 1: GBA is sending
* Bit 3: GBA is receiving
* Bits 4-5: General purpose flags
{{ProtocolIdentifierRow| 0x0900{{sup|'''(1)'''}} | GC | Controller }}
{{ProtocolIdentifierRow| 0x8800{{sup|'''(1)'''}} | GC | Receiver }}
{{ProtocolIdentifierRow| 0x8B10{{sup|'''(1)'''}} | GC | Wavebird }}
{{ProtocolIdentifierRow| 0x0800{{sup|'''(1)'''}} | GC | Steering Wheel }}
{{ProtocolIdentifierRow| 0x0802{{sup|'''(1)'''}} | GC | Keyboard }}
{{ProtocolIdentifierRow| 0x0808{{sup|'''(1)'''}} | GC | Dance Mat }}
|}
'''{{sup|(1)}}''' ''Requires verification yet.''
==== Controller status byte ====
The status byte for the N64 controller allows to detect whether an accessory is inserted in the controller or not. The status values 1 and 2 map to the intuitive meanings of "pak inserted" and "pak absent" (they basically map to the level status of the DETECT pin). Instead, status value 3 is special: it is basically returned the first time the Info packet is sent after a pak was newly inserted; another way of saying it is that the status value becomes 3 on the low-to-high transition of the DETECT pin, and stays at 3 until a read of the state is made by the N64. After 3 is returned, assuming the pak is not removed, next reads will always return 1.
From a software perspective, this means that a software cannot miss the change of an accessory; if the software sees the value 3, it means that a new accessory is inserted, even if the previous value was 1 (in which case, the previous accessory was removed and a new one is inserted). Instead, readings of 1 mean that the same accessory is still inserted.
=== 0x01 - Controller State ===
The response for this command is always the same length of 4 bytes (32 bits), but the data it represents will change depending on the type of controller device. A Controller Stop Bit is always included after the response bytes. ''Note that in the following waveform diagrams, any LOW bit should send a Zero Bit as described in the [[#Operation|operation section]] above. Do not hold the line LOW constantly.''
==== Standard Controller ====
The most common is the standard N64 controller. In which case, the data is the current state of the inputs from the controller: the 14 buttons, the current position of the analog stick, and the reset (RST) bit. The default state of buttons and RST is Zero. If a button is pressed, it becomes One. If LT, RT, and Start are pressed, RST is One, Start becomes Zero, and the analog stick's position is reset to (0, 0). Each axis of the analog stick is a [[w:Two's complement|Two's Complement]] byte, giving a decimal value ranging from -128 to +127, even though a standard controller may not reach the full range due to physical limitations. The bit order from left to right, of the response data, is as follows:
[[File:Controller-response.svg|980px]]
==== Mouse ====
The mouse follows a similar format to the standard controller. However, it only uses the A and B buttons, and the x/y axis bytes represent the relative position of the mouse.
[[File:Mouse-response.svg|980px]]
==== Densha de Go ====
[[File:Train-controller-diagram.jpg|thumb|Input diagram of Densha de Go! train controller.|200px]]
The game Densha de Go! used an exclusive train controller instead of the standard tri-wing controller. It had five buttons, and two multi-position levers. Refer to the right image for corresponding positions and values for each lever.
[[File:Train-controller-response.svg|980px]]
''(Acc refers to the accelerator lever)''
==== Other Devices ====
While more devices exist that use this command, specific response information is not known at this time.
=== 0x02 - Read Controller Accessory ===
{| class="wikitable"
|+
! colspan="2" |TX (3 bytes)
! colspan="2" |RX (33 bytes)
|-
!Bytes
!Description
!Bytes
!Description
|-
|0
|Command code 0x02
|0-31
|32 bytes read from the accessory
|-
|1-2
|16-bit address to read, aligned to 32 bytes
(plus a special CRC in the low 5 bits)
|32
|CRC8 of the read bytes
|}
This command is used to read data from the controller accessory (pak) connected to the bottom of the controller.
The protocol allows to perform reads of blocks of exactly 32-bytes, from a 16-bit address. Blocks can only be read from aligned addresses.
The address space is accessory-dependent. The controller pak for instance maps the 32 KiB of SRAM to addresses 0x0000-0x7FFF; the rumble pak uses only addresses 0x8000-0xBFFF as a single bit register to start or stop the motor. Please refer to the documentation of each accessory for more information on its address space.
Since the 16-bit address must be 32-byte aligned, the lower 5 bits are used for a checksum of the address itself, which is explained [[#Address Checksum|in the next section]]. This checksum is verified by the controller when the command is received, and is probably used as a way to verify that no bits were corrupted on the serial line.
The controller will reply with the 32 bytes read from the accessory. In addition to that, a 8-bit CRC over the 32 bytes ([[#Data CRC|also explained below]]) is appended to the reply, again probably to protect transfers from serial corruptions. The software on the N64 can verify if the CRC matches to make sure there was no corruption on the wire.
The accessory must begin responding with data within about 62.5 microseconds.
When a pak is not installed, this command will still complete successfully from a joybus perspective (so there will be no error signaled [[PIF-NUS#RX byte: special flags|in the PIF packet]] (the command would fail only if the controller was not connected). In this case, the whole reply is made of zeroes, and the CRC byte is instead 0xFF (which is the complement of the correct CRC). From a software perspective, whenever the CRC is complemented, it means that the accessory is absent.
When a pak is just installed, reads are still nullified as-if the pak is absent. Basically, the controller will not allow reads (or writes, see below) until the software on the N64 sends the Info command once to identify the presence of the pak; as explained in the Info command description above, the status byte in this first request will be 3 (to mean that a new pak was inserted). After this command is completed, it will be possible to perform reads (o writes) to the accessory.
If the pak receives a read command with an invalid address checksum (either because it was generated invalid by the N64 or because it did get corrupted on the wire), it will behave as if the pak is disconnected, that is, return a reply made of zeroes with a complemented CRC (0xFF).
=== 0x03 - Write Controller Accessory ===
{| class="wikitable"
|+
! colspan="2" |TX (35 bytes)
! colspan="2" |RX (1 bytes)
|-
!Bytes
!Description
!Bytes
!Description
|-
|0
|Command code 0x03
|0
|CRC8 of the written data
|-
|1-2
|16-bit address to read, aligned to 32 bytes
(plus a special CRC in the low 5 bits)
|
|
|-
|3-34
|Data to be written to the device
|
|
|}
This command is used to writes data from the controller accessory (pak) connected to the bottom of the controller.
The behavior of this command is completely specular to command 0x02 Read Controller Accessory. Please refer to the command above for the detailed description of all the behaviors.
It is worth notice that the CRC8 protecting the integrity of the transfer is computed by the controller and sent back to the N64. So if a corruption happens on the joybus wires, the write will still go through, but the N64 will then be able to detect that the corruption happened, and re-issue the write to try again writing the correct data.
When the pak is absent, the write command behaves like the read command: the command completes successfully from a joybus perspective, and the only indication that nothing really happened is that the CRC8 byte in the reply will be complemented (compared to the correct one).
=== 0x04 - Read EEPROM Block ===
If EEPROM is available, this command can be used to read 8 bytes of save data at a time. The command (`0x04`) is followed by which block of EEPROM to read from:
{| class="wikitable"
! Identifier
! Capacity
! Available Blocks
{{ProtocolIdentifierRow| 0x0080 | 4 Kibibits (512 bytes) | 0-63 }}
{{ProtocolIdentifierRow| 0x00C0 | 16 Kibibits (2048 bytes) | 0-255 }}
|}
In a 512 byte EEPROM, the top two bits of block number are ignored: blocks 64-255 are repeats of the first 64
=== 0x05 - Write EEPROM Block ===
If EEPROM is available, this command can be used to write 8 bytes of save data at a time. See "Read EEPROM Block" above for block addressing rules. The 8 bytes of data to write are included in the command immediately following the "block" byte.
Notice that the completion of the joybus command does not imply that the EEPROM was written. The EEPROM would just latch the 8 bytes of data, and then proceed performing the actual write in background, after the joybus command was already completed. It may take up to 6 milliseconds for the data to be written to EEPROM.
The 1-byte joybus reply specifies whether the write has started correctly or not. If a EEPROM write is requested while a previous one is still being performed, the EEPROM will reply with 0x80 (busy), and 0x00 otherwise.
Real EEPROM data storage has inherent unreliability that requires special consideration: EEPROM chips may become corrupted spontaneously due to uncontrollable factors such as power failure during write or memory cell fatigue causing write failures. It is recommended to use a checksum or parity bits in each EEPROM block to check that the data has not been corrupted. EEPROMs used by retail cartridges are rated for up to 100,000 write cycles and may only retain saved data for up to 10 years. Emulators and flashcarts now provide reliable storage for EEPROM commands that do not suffer from these issues.
=== 0x06 - Real-Time Clock Info ===
In order to permit using both an EEPROM and the RTC at the same time, the RTC does not respond to command 0, and has its own identify command.
If RTC (Real-Time Clock) is available, this command will return a 2 byte identifier (0x0010) and a status byte. If RTC is not available, this command will respond with three bytes containing zero.
{| class="wikitable"
! Status Byte Bit
! Meaning
! Description
{{ProtocolIdentifierRow| 7 (0x80) | Stopped | Clock is halted, and it is safe to write to block 2 }}
{{ProtocolIdentifierRow| 6-2 (0x7C) | Unused | These bits have never been seen set }}
{{ProtocolIdentifierRow| 1 (0x02) | Crystal failure | If this bit is set, the crystal is not working }}
{{ProtocolIdentifierRow| 0 (0x01) | Battery failure | If this bit is set, the supply voltage of the RTC became too low }}
|}
=== 0x07 - Read Real-Time Clock Block ===
The RTC read command sends 2 bytes (1 for the command, 1 for block selection) and responds with 8 bytes of block data plus a status byte that has the same meaning as the "RTC Info" command's status byte.
The RTC has three blocks each containing 8 bytes of data:
{| class="wikitable"
! Block
! Purpose
! Comment
{{ProtocolIdentifierRow| 0 | Control registers | Determines the current clock "mode" }}
{{ProtocolIdentifierRow| 1 | Unused | 8 bytes of battery-backed SRAM }}
{{ProtocolIdentifierRow| 2 | Date/Time Fields | The current date and time in binary-coded decimal }}
{{ProtocolIdentifierRow| 3 | Empty | Always 0 }}
|}
The top six bits of the block number are ignored: reading from or writing to blocks above the first 4 will wrap within the first four.
All RTC reads also include a status byte with the same meaning as the "RTC Info" command's status byte.
====RTC Block 0 (Control Registers)====
Byte 0:
{| class="wikitable"
! Bit number
! Mode
! Description
{{ProtocolIdentifierRow| 7-2 (0xFC) | Empty | Always 0 }}
{{ProtocolIdentifierRow| 1 (0x02) | Write Protect | Write protects field 2 (RTC) }}
{{ProtocolIdentifierRow| 0 (0x01) | Write Protect | Write protects field 1 (NVRAM) }}
|}
Byte 1:
{| class="wikitable"
! Bit number
! Mode
! Description
{{ProtocolIdentifierRow| 7 (0x80) | Unknown | Exists, changeable, no visible function }}
{{ProtocolIdentifierRow| 6-3,0 (0x79) | Empty | Always 0 }}
{{ProtocolIdentifierRow| 2,1 (0x06) | RTC Stop | If either bit is set, stops RTC from counting }}
|}
Bytes 2, 3, 6, and 7 always read back as 0.
Bytes 4 and 5 contain 7- and 6- bit numbers respectively that can be updated but have no visible function.
====RTC Block 1 (SRAM)====
RTC block 1 is not used by Animal Forest, and is not implemented by emulators or flashcarts.
====RTC Block 2 (Date/Time)====
RTC block 2 updates once per second with 8 bytes of data representing the current date and time. The fields are encoded using packed binary-coded decimals:
{| class="wikitable"
! Byte
! Purpose
! Description
{{ProtocolIdentifierRow| 0 | Seconds | (0-59) }}
{{ProtocolIdentifierRow| 1 | Minutes | (0-59) }}
{{ProtocolIdentifierRow| 2 | Hours | (0-23) + 0x80 }}
{{ProtocolIdentifierRow| 3 | Day of Month | (1-31) }}
{{ProtocolIdentifierRow| 4 | Day of Week | (0-6) Sunday - Saturday }}
{{ProtocolIdentifierRow| 5 | Month | (1-12) }}
{{ProtocolIdentifierRow| 6 | Last Two Digits of Year | (0-99) }}
{{ProtocolIdentifierRow| 7 | Centuries since 1900 | (0-1) }}
|}
====RTC Block 3 (Empty)====
RTC block 3 always reads back all zeroes.
=== 0x08 - Write Real-Time Clock Block ===
The RTC write command sends 10 bytes (1 for the command, 1 for block selection, and 8 for the block data) and responds with a status byte that has the same meaning as the "RTC Info" command's status byte. The block data to write matches the same format as the block read responses in "Read RTC Block" above.
The most-compatible routine for setting the time with the Joybus Real-Time Clock is:
# Send "Write RTC Block" command to Control Registers (Block 0) with write protect disabled and clock stopped (0x0004...).
# Wait 20 milliseconds for RTC block to write.
# Send "Read RTC Status" command, check for "Stopped" (0x80) status: if stopped, proceed; otherwise abort.
# Send "Write RTC Block" command to Date/Time Fields (Block 2) with new time to set.
# Wait 20 milliseconds for RTC block to write.
# Send "Write RTC Block" command to Control Registers (Block 0) with write protect enabled and clock running (0x0300...).
# Poll "RTC Read Status" command until "Running" (0x00) status.
# Wait 500 milliseconds before sending "RTC Read Block" command to Date/Time Fields (Block 2)
Emulator and flashcart support for writing to the Real-Time Clock is not guaranteed:
* Most emulators will ignore RTC write commands and always use the host system's local time.
* EverDrive 64 3.0 and EverDrive64 X7 will ignore Joybus RTC write commands; date/time must be set through the Everdrive OS.
* 64drive hw2 supports Joybus RTC writes, but requires a 500 millisecond delay after setting the time in order to read it back correctly.
For homebrew, it is recommended to test whether the RTC implementation supports the write command to set the proper expectation with players. To do this:
# Read the current RTC time.
# Write a different time to the RTC.
# Read the new RTC time.
# If the new time is within a second of what was written: write back the original time, success; otherwise failure.
=== 0x13 - Read from Game Boy Pak ===
Works identically to [[#0x02 - Read Controller Accessory|0x02 - Read Controller Accessory]], down to the address and data CRC, except that it reads from the Game Boy Pak which contains a MBC4 memory controller which supports JoyBus communication through the Nintendo 64 Controller port.
=== 0x14 - Write to Game Boy Pak ===
Similar to the previous command, this works identically like [[#0x03 - Write Controller Accessory|0x03 - Write Controller Accessory]], except that it writes to the Game Boy Pak.
=== 0x14 - Read from GBA ===
{| class="wikitable"
|+
! colspan="2" |TX (1 bytes)
! colspan="2" |RX (5 bytes)
|-
!Bytes
!Description
!Bytes
!Description
|-
|0
|Command code 0x14
|0-3
|4 bytes read from the GBA
|-
|
|
|4
|Lower 8 bits of GBA's JOYSTAT register
|}
This command is used to read data from the GBA.
The protocol allows to read exactly 4 bytes.
The GBA will reply with the 4 bytes. In addition to that, a fifth byte is transmitted containing the lower 8 bits of GBA's JOYSTAT register. The software on the N64 can verify if the status of the GBA matches expectations.
=== 0x15 - Write to GBA ===
{| class="wikitable"
|+
! colspan="2" |TX (5 bytes)
! colspan="2" |RX (1 bytes)
|-
!Bytes
!Description
!Bytes
!Description
|-
|0
|Command code 0x15
|0
|Lower 8 bits of GBA's JOYSTAT register
|-
|1-4
|Data to be written to the GBA
|
|
|}
This command is used to write data to the GBA.
The protocol allows to write exactly 4 bytes.
The GBA will reply with a single byte containing the lower 8 bits of GBA's JOYSTAT register. The software on the N64 can verify if the status of the GBA matches expectations.
= Checksums =
There are two different checksums used in some of the official commands. The first is an 8 bit CRC, and the other uses eleven different XOR values based on the position of each set bit. While the former is used for large chunks of data, the latter is used to check the 11 bit address for where that data is written to or read from. These checksums are used to ensure data integrity. The address checksum, which is 5 bits long, is always a part of the communication sent from the console to the device, just after the 11 bit address.
=== Data CRC ===
The 8 bit CRC known to be used in the controller accessory read/write commands, and the GBA read/write commands. It uses a standard CRC8 function, with a seed (initial value) of 0x00, and 0x85 for the polynomial. A CRC polynomial is a mathematical way to say that a particular number is used as a mask, or to be applied to the working CRC byte using an XOR operation. The mathematical form can be expressed as .
In every known case, the connected device is always the side which generates this CRC. For read commands, it reads 32 bytes of data from a particular address, calculates the CRC, and sends all 32 bytes plus the CRC byte back to the console. For write commands, it calculates the CRC from the 32 bytes sent by the console, sends the CRC byte back, and performs the write operation. The order in which it does this may differ for different devices. For write commands, the device has a window of approx 62.5 microseconds after the console stop bit, to begin sending the CRC byte.
=== Address Checksum ===
When the console wishes to read data from, or write data to, a particular address on the connected device, it will send the top 11 bits of a 16 bit address, plus a 5 bit checksum. This means these operations must be done in 32 byte chunks, as the lower 5 bits of the address are assumed to be 0, to make room for the checksum. '''''By definition, this checksum is not a CRC''''', because it doesn't use a cyclic code (as in, there is no bit shifting used), and the XOR value is not constant.
The 5 bit checksum is calculated using the following table. The working checksum is initially set to zero. For each bit of the 11 address bits, starting at the upper-most bit, if the bit is set (is 1), then XOR the corresponding byte to the working checksum. If the bit is clear (is 0), do nothing and move onto the next bit.
{| class="wikitable" style="text-align: center;"
! 15 !! 14 !! 13 !! 12 !! 11 !! 10 !! 9 !! 8 !! 7 !! 6 !! 5
|-
| 0x01 || 0x1A || 0x0D || 0x1C || 0x0E || 0x07 || 0x19 || 0x16 || 0x0B || 0x1F || 0x15
|}
If the resulting checksum matches the one provided by the console, the address is valid.
''The process as pseudocode:''{{refn|name=pseudoAddress|joeldipops (2020). [https://github.com/joeldipops/TransferBoy/blob/master/docs/TransferPakReference.md#memory-addresses-and-checksum Nintendo 64 Accessory Reference].}}
for bits 15 -> 5
if the bit is set
xor the checksum with the corresponding value in the above table
==References==
{{reflist}}