MAX DUO Pokémon Toolkit GitHub

Home / Library

Mystery Event over the link cable (the e-Reader route)

Rendered from docs/EREADER.md in the repository.

Roadmap Stage 3. This describes, in our own words, how Ruby and Sapphire (and Japanese Emerald) receive a Mystery Event, as read from pret/pokeruby (src/link.c, src/mystery_event_menu.c, src/mystery_event_script.c). The DUO's GBA will stand in for the Card e-Reader+ on that link.

  • Mode. SIOCNT in multi-player mode at 115,200 bit/s. Each transfer moves one 16-bit word from every console at once: each loads SIOMLT_SEND, the master starts the transfer, and afterwards every console sees all the words in SIOMULTI0..3 (player 0 = the master).
  • Who is master. That's decided by the cable. The console on the end whose SI is grounded is player 0. On a Nintendo AGB-005 cable, that's the purple plug. Game Boy and Game Boy Color cables won't do, because multi-player mode needs the SD line.
  • Pacing (corrected after lab session 24). A slave only reacts, but it needs the master to pace exactly like this:
  • One command a frame. During the handshake, the master starts one transfer per frame (VBlank). Once connected, it runs one cycle of nine transfers per frame: a checksum slot, then eight words.
    • The game sends at most one command per frame, and takes in one received command per frame (LinkMain1 → DequeueRecvCmds), with room for 50 waiting.
  • Within a cycle, 0.75 ms between transfers. Each next transfer starts 0.75 ms after the previous one finished: timer 3, 197 ticks of 64 cycles, started at the end of the master's serial interrupt. That gives the slave's interrupt time to load its next word.
  • What went wrong in session 24. The DUO was master, starting transfers 12 scanlines after the previous start: only ~0.44 ms after the end, with cycles back to back. 45% of commands had checksum mismatches.
  • The fix. ereader.c now waits 13 scanlines (~0.83 ms) after each transfer ends, and starts one cycle per frame.
  • The simulator now gives the slave game a 0.6 ms reaction time and a five-scanline transfer, and checks the one-cycle-per-frame rule.
  • The handshake.
  • Everyone sends a handshake word: the master 0x8FFF, slaves 0xB9A0 (plus a small id).
  • The link counts as up once player 0 says 0x8FFF and the number of players agrees twice in a row.
  • Commands.
  • After the handshake, data moves in commands of 8 words (CMD_LENGTH), one word per transfer, then a ninth transfer carrying a checksum: the sum of the words received.
  • A command whose words are all zero means "nothing".
  • The commands that matter here:
  • 0x2222, link type: "send me your link player". Each side answers by sending its link-player block.
  • 0xBBBB, size, sender + 128: a block is about to follow.
  • 0x8888, 7 words: 14 bytes of the current block. Repeated until size bytes have arrived. Blocks over 256 bytes go into a larger shared buffer.
  • The link-player block is the same 60-byte form we already send over the wireless link:
  • "GameFreak inc." (14 bytes), then 2 bytes of padding;
  • a 28-byte struct LinkPlayer, laid out in Ruby as:

    Offset Field
    0x00 version + 0x4000 (u16)
    0x02 (u16)
    0x04 trainer ID (u32)
    0x08 name (11 bytes)
    0x13 gender
    0x14 link type (u32)
    0x18 id (u16)
    0x1A language (u16)
  • "GameFreak inc." again.

Framing, exactly

  • The cycle. Every console keeps a send index and a receive index, both 0 when the link comes up. A command takes nine transfers, in a repeating cycle:
  • The checksum slot. What arrives is everyone's checksum for the previous command; each console compares it with its own.
  • Then words 0 to 7. Each received word is stored, and added to a running checksum: the sum of every player's words in that command, modulo 0x10000.
  • What gets loaded when. After word 7 arrives, the console loads its checksum for the next transfer, and both indexes return to 0.
  • The first command. The first transfer after a successful handshake is a checksum slot, and isn't checked.
  • Empty commands. A command whose words are all zero is "nothing" and isn't queued.

Starting and closing

  • Starting, when the game is master.
  • Until the player presses A, the master sends the slave handshake word too. Its player count still works, so the menu can say whether two players are connected.
  • Pressing A makes it send 0x8FFF once, which brings the link up.
  • Then it sends 0x2222 to start the link-player exchange.
  • Starting, when the game is a slave. Pressing A does nothing. The master (the DUO) would have to send 0x8FFF and 0x2222 itself. But Ruby's Mystery Event menu never gets that far as a slave: see below.
  • Closing. Each side sends 0x5FFF. A console closes the link once it has seen 0x5FFF from every player. So the DUO must answer the game's 0x5FFF with its own.

What the Mystery Event menu does (Ruby)

  1. Sets the link type to 0x5501, opens the link, and shows "link standby".
  2. Waits until it is the link's master with at least two players: gLinkStatus & 0x20 (LINK_STAT_MASTER) and a player count of two or more. Only then does it say "press A".
  3. And every frame: once connected (gLinkStatus & 0x40), if it isn't master, it closes the link and shows an error.
  4. So the game must be on the cable's master end, and the e-Reader is the slave. That is why session 24's runs with the DUO as master never got further. If the DUO comes up as player 0, the tool now says to swap the cable's ends.
  5. When the player presses A, the master sends 0x2222. Each side then sends its link-player block.
  6. Checks.
  7. Every link player must carry the same link type (0x5501), or the exchange counts as failed.
  8. The two players' languages must match.
  9. Waits for one more block from the other side: the Mystery Event script itself, received into the shared buffer (gSharedMem, 0x7D4 bytes). Only a block over 256 bytes goes there (link.c: smaller blocks land in gBlockRecvBuffer), so a short script is never run: the menu runs the cleared buffer, says the event loaded and saves, with nothing stored. Found on hardware in lab session 29: Devin's trainer card (212 bytes) left Mossdeep's door locked, and the save backup showed the e-Reader trainer slot empty. The DUO now always sends the whole 2,004-byte buffer, the script followed by zeros (a planted bug checks it).
  10. Closes the link and runs the script from the start of that buffer (RunMysteryEventScript(gSharedMem)). Then it clears the buffer.
  11. Saves unless the status says otherwise:
Status Message Save
0, "loaded OK" (the starting status) the game's own success message yes
1, load error (a failed checksum or CRC command) "loading error" no
2, success (set by gift commands, or setstatus) the script's own message yes
3, failure (checkcompat failed, a full party) "can't be used" no
  1. Shows the message, waits for A, and resets the game.

The Mystery Event script

From pokeruby's mystery_event_script.c and script.c.

  • The engine. It runs commands until one yields, then carries on only while the script is marked valid. A byte that isn't a command (17 of them, 0–16) stops it at once.
  • The commands used here. Arguments are little-endian and read byte by byte, so no alignment is needed.
No. Command Arguments Notes
1 checkcompat base (u32), languages (u16), languages (u32), flags (u16), versions (u32) Sets the script's base address for pointers. Valid only if the 16-bit field has the game's region bit (Japanese 1, English 2, any other European language 4) and the 32-bit field its language's bit (Japanese 1<<0, English 1<<1, German 1<<2, French 1<<3, Italian 1<<4, Spanish 1<<5), flags has 0x4, and versions has Ruby 1<<7 or Sapphire 1<<8. Otherwise status 3. Yields
2 end
3 setmsg status (u8), text (pointer) Shows the text if the status matches (0xFF: any)
4 setstatus status (u8)
6 initramscript map group, map number, person (u8 each), start, end (pointers) InitRamScript: stores the script for that person, with magic 0x33 and a byte-sum checksum
11 setrecordmixinggift count, chance (u8 each), item (u16) The item spreads by Record Mixing; zero fields clear it
15 checksum sum (u32), start, end Byte sum. On mismatch: invalid, status 1. Yields
16 crc CRC-16 (u32), start, end The game's CRC-16. On mismatch: invalid, status 1. Yields
  • Pointers are word - base + (where the buffer is), with base from checkcompat.

Community files: .me3 is the saved form (lab session 25)

  • What a .me3 holds (1,012 bytes; checked against the Eon Ticket file and Ruby's code):
Offset Size What
0 4 the RAM script's checksum: the byte sum of the next 1,000 bytes
4 1 magic 0x33
5 3 map group, map number, person (the Eon Ticket: 8, 1, 1 = Norman in Petalburg City Gym)
8 996 the RAM script (a field script), zero-padded
1004 4 the Record Mixing gift's checksum: the byte sum of its data
1008 4 count, chance, item (u16) (the Eon Ticket: 1, 151, 275 = ITEM_EON_TICKET)

That's the game's RamScript and the start of its RecordMixingGift, exactly as saved. - Sent as it is, the game reads 0xF0 as its first command, which isn't one. It stops at once with status 0, shows its success message and saves, having stored nothing. That's what sessions 24 and 25 did: the link worked, but the game can't have received the ticket. - So the tool wraps a .me3 in the script that stores exactly its contents:

checkcompat base 0x02000000, English (or German) in both fields, flags 0x4, Ruby | Sapphire checksum byte sum of the RAM script, over the RAM script (damage -> "loading error", no save) initramscript map group, map number, person, the RAM script setrecordmixinggift count, chance, item (if the file has a gift) end <the RAM script>

  • The status stays 0, so the game shows its own success message and saves.
  • The language comes from the file's name ("(ENG)", "(GER)"). Japanese files are refused: the Japanese masks aren't known.
  • A model of Ruby's engine (tests/mevent_model.py, written from the decomp) checks that the game would then store the file's own bytes, RAM script and gift alike. Locally, it does so for the real Eon Ticket file too, which isn't in the repository.
  • A file that already starts with checkcompat is sent as it is, after reading its compatibility fields.

Community files: .ecb is the saved Enigma Berry (30 October)

  • What a .ecb holds (1,328 bytes; checked against the six English e-Card berries and pokeruby's global.berry.h and berry.c): Ruby's whole EnigmaBerry record, exactly as the game keeps it in its save.
Offset Field
0x000 name (7 bytes), firmness, size, largest and smallest yield
0x00C two description pointers: the saving game's addresses, meaningless elsewhere
0x014 growth time, five flavours (spicy, dry, sweet, bitter, sour), smoothness
0x01B picture (6×6 tiles, 4 bits per pixel), then the 16-colour palette at 0x49C
0x4BC two descriptions, 45 bytes each
0x516 item effect (18 bytes), hold effect, its parameter
0x52C checksum: the byte sum of everything before it, taken with the two pointers as 0
  • How the game takes a berry. setenigmaberry (command 7) copies 0x530 bytes from the received buffer into the save and points the two description pointers at the descriptions. It then checks the berry (IsEnigmaBerryValid: a growth time, a largest yield and the checksum). If it's good, the status is 2 ("success") and VAR_ENIGMA_BERRY_AVAILABLE is set; if not, "loading error". The message depends on what the save held: a first berry, a different one (it replaces the old), or the same one again.
  • So the tool wraps a .ecb like this, with the pointers cleared:

checkcompat as for a .me3 checksum byte sum of the berry record, over the berry record setenigmaberry the berry record end <the berry record> (1,364 bytes in all)

  • In the game. The berry isn't in the bag yet. Norman gives it in Petalburg City Gym, after his battle, unless the bag or PC already holds an e-Reader berry. It appears under its own name (for example PUMKIN BERRY), and grows like any other berry.
  • Checked locally with the six English e-Card berries (not in the repository): for each, in Ruby and Sapphire, the model stores every byte except the two pointers and counts the berry as valid. Japanese berries are refused for now, like Japanese .me3 files.

e-Card scans (.raw): the cards themselves (Stage 6, 30–31 October)

A .raw is one dot-code strip as the e-Reader reads it. Read back to front:

  1. Blocks. A long strip is 2,912 bytes: 28 blocks of 104 bytes (a short one, 1,872: 18 blocks). Each block is a 2-byte block header, then 102 bytes of one stream.
  2. Codewords. The stream interleaves Reed-Solomon codewords, 44 on a long strip (28 on a short): byte 0 of every codeword, then byte 1, and so on. Each codeword is 48 data bytes and 16 parity bytes (GF(256), polynomial 0x187, the parity as mGBA's _eReaderReedSolomon writes it). The tool checks every codeword's parity and refuses a damaged strip; it doesn't try to correct one.
  3. The strip. The codewords' data, in order: a 48-byte strip header (with NINTENDO), a 33-byte title, then the strip's share of the card.
  4. The card. The strips' shares joined in order ("(Strip 1)", "(Strip 2)": each strip has its own header and title, which are skipped): a 2-byte length, then a vpk0-compressed e-Reader program (HAL's LZ: literal bytes and copies, whose distances and lengths have their bit widths in two small Huffman trees; methods 0 and 1 differ in how distances are coded).
  5. The Mystery Event. For Ruby and Sapphire cards, the program carries the Mystery Event script the e-Reader sends, word for word: it starts with checkcompat and the base 0x02000000. The tool finds it, walks its commands to see how far its pointers reach, and sends exactly that. No wrapping: the card's own script, as the e-Reader sent it.

What the gallery's Ruby/Sapphire cards hold (all decoded locally; none is in the repository):

Cards Script In the game
6 English and 12 Japanese berries checkcompat setenigmaberry end Norman gives the berry
54 English and 112 Japanese trainers checkcompat addtrainer end (Japanese: initramscript first) the old man's house in Mossdeep City (pret: MossdeepCity_GameCorner): "A TRAINER named … is visiting my home"
the US Eon Ticket checkcompat crc initramscript setrecordmixinggift runscript end Norman; it stores exactly what the US e-Card .me3 holds
the Japanese Decoration Present (2 strips) checkcompat crc runscript end its script runs at once
the Berry Glitch Fixing Program (2 cards) none: an e-Reader program of its own refused: it writes the save itself
  • Checked with the model (tests/mevent_model.py, which now also carries out addtrainer and records runscript): every card, in Ruby and Sapphire, is accepted in its own language and refused in the other; every trainer passes the game's own checksum; the berries match the .ecb files, and the US Eon Ticket card matches the US e-Card .me3 byte for byte.
  • Refused on purpose: a card that would turn on the clock reset (enableresetrtc), since this tool leaves clocks alone; none of the gallery's Ruby/Sapphire cards does.
  • Sources: the strip layout and parity from mGBA (src/gba/cart/ereader.c); vpk0 from the vpk0 crate (tehzz/vpk0); both re-implemented here.

Japanese Ruby and Sapphire (Stage 7)

  • The language masks, from the cards. checkcompat's two language fields are 0x0001 on every Japanese card and 0x0002 on every English one (German, 0x0004, is from pokeruby). So Japanese .me3 and .ecb files are now wrapped with 0x0001, and Japanese cards go as they are.
  • French, Italian and Spanish (15 November). Read from every European Ruby and Sapphire's CheckCompatibility: the 16-bit field must have 4 (as German's), the 32-bit field the language's own bit: French 8, Italian 0x10, Spanish 0x20 (German's two are both 4). With these the gallery's French, Italian and Spanish Eon Tickets were stored byte for byte by the real games in the emulator (all six ROMs), and a French one sent with English's 16-bit mask was refused.
  • The tool warns at link-up when an event's language isn't the game's, since the game would only say it can't be used.
  • Menus. The Japanese title menu says ふしぎな できごと (fushigi na dekigoto). It's unlocked the same way as in English, with a Japanese phrase whose words this project hasn't confirmed.
  • Not yet on hardware: there's no Japanese Ruby or Sapphire here.

What the DUO does

  • Role. The second player. It follows the cable, master or slave, but only the slave role can work with Ruby's menu (above). It knows its role from the player number the GBA writes into SIOCNT (bits 4-5) after each transfer; before the first, from SI staying low. (One look at SI isn't enough: the other GBA drives it during transfers, which gave session 27's false "player 0".) As master, it paces exactly like Ruby's own master, which is kept for other uses and tests.
  • Handshake. Answer it.
  • On 0x2222. Send its link-player block, with link type 0x5501 and the game's language (read from the game's own block). Then send the event script, loaded from the PC, as one block.
  • Plain mode. With no event loaded, send a link-player block only. The game then times out harmlessly: a safe dry run.

On hardware so far

  • Session 27. The PUMKIN BERRY (a wrapped .ecb) went across with 0 mismatches, and Norman gave it. The trace's "player 0" was wrong (see Role above; fixed since).

  • Session 26. The wrapped Eon Ticket went across with 0 mismatches, Ruby saved it, and Norman gave the ticket. The e-Reader route works end to end.

  • Session 24. With the DUO as master, the link came up but nothing more happened. That's explained by the menu's master rule. There were also checksum mismatches, partly from our pacing, which was too fast and is now fixed, and possibly from a start race, now fixed with an in-flight flag.

  • Session 25. With the ends swapped, the game was master.
  • The link players were exchanged with 0 mismatches.
  • The event block went across, twice, and the game closed the link. But the block was the raw .me3, so the game stored nothing (above).
  • A few mismatches were counted after the close. They were very likely the game opening the link again, whose handshake words were counted as data. The program now follows the game starting over, and doesn't count those.
  • Diagnostics now reported at the end of a run:
  • where any mismatches happened, by stage;
  • the first four mismatches, with their words;
  • the longest time the DUO looked away from the link.

Open questions

  • ~~Ruby's success message and the Eon Ticket itself.~~ Done: the wrapped Eon Ticket arrived and Norman gave it (lab session 26).
  • ~~The Enigma Berry on hardware.~~ Done (lab session 27).
  • ~~The Japanese language masks.~~ 0x0001, read from the Japanese cards (above).
  • An e-Card trainer on hardware: send one, then visit Mossdeep.
  • Japanese Emerald's ふしぎな できごと and Trainer Hill (ereader_screen.c): another protocol (Stage 8).
  • The Japanese unlock phrase for Ruby and Sapphire.

Japanese Emerald's Trainer Hill e-Cards (Stage 8, part 1; simulator done, 1 November 2026)

Read from pokeemerald (ereader_screen.c, ereader_helpers.c, main_menu.c).

Where. With no Wireless Adapter attached, Japanese Emerald's title-menu Mystery Gift entry (ふしぎなおくりもの, once unlocked) opens the e-Reader screen (CB2_InitEReader), not Mystery Gift. (Its MYSTERY EVENTS entry, ふしぎなできごと, is the Ruby/Sapphire-style menu above.)

How the real e-Reader did it. The screen opens an ordinary link, type 0x5503, as master, and gives a partner 10 frames to answer. If none does, it asks the player to choose "connect" on the e-Reader, then sends the e-Reader a program of its own (32-bit serial at a timer's pace) and waits; that program scans the cards. Then a fresh link: link players (no language check), then one block, the Trainer Hill set, which the game checks (ValidateTrainerHillData), closes the link, and writes to its Trainer Hill save sector itself.

What the DUO does. It answers within the 10 frames, as an e-Reader already running Emerald's program would, so the game goes straight to receiving. The rest is the Mystery Event link exactly (ereader.c, unchanged apart from a 3,840-byte buffer), so the cable goes the same way round: the game on the master end.

The cards. Each of the 66 Emerald cards in Project Pokémon's gallery (64 trainers and two promos, Japanese) holds an e-Reader program (Z80) with one trainer record at offset 256: card number, three zero bytes, the trainer (Japanese layout: a 6-byte name, so the record is 0x270 bytes, 4 fewer than pokeemerald's English struct), the floor map, and a byte-sum checksum. The tool finds the record by its checksum and builds the set:

Offset Field
0 number of trainers (1–4: Japanese Trainer Hill has two floors of two)
1 id (0; asserted)
2 dummy (0; asserted)
4 byte sum of the trainers
8 6 trainer records of 0x270 bytes (the unused ones zero), then 40 zero bytes: 3,792 bytes in all

event --event takes up to four Emerald cards at once, as one set. All 66 cards decode to one valid record each, and every set built from them passes the game's checks in the model; the Ruby/Sapphire cards are unaffected.

Still to do: a hardware run (the user has Japanese Emerald).

Japanese FireRed/LeafGreen's Trainer Tower e-Cards (Stage 8, part 2; simulator done, 1 November 2026)

Read from pokefirered (ereader_screen.c, cereader_tool.c, trainer_tower.c). The e-Reader screen is Emerald's, line for line, with link type 0x5502; the set it receives is the Trainer Tower's. pokefirered (the English game) never calls CB2_InitEReader: the English release dropped the e-Reader, so these cards are for Japanese cartridges, whose menu path isn't confirmed here.

The cards. Each of the 37 FireRed/LeafGreen cards in the gallery (Japanese) holds one floor record at offset 256, struct TrainerTowerFloor in the Japanese layout, 0x3D4 bytes (12 fewer than pokefirered's English struct: three 6-byte names): id (the card number), the floor count of its series (8 on every card), the challenge type (0–3), the prize, three trainers, and a byte-sum checksum.

The set (struct EReaderTrainerTowerSet): the number of floors (1–8), id 0, dummy 0, the byte sum of the floors, then the floors. The game clears its 8 KB receive buffer before linking, so only the floors present need sending: the DUO sends up to four (3,928 bytes; its buffer is now 4,096). With fewer floors than the series' 8, the tower says how many it has. All 37 cards decode and pass the game's checks in the model; Emerald's cards and other games' (Colosseum, Pinball, the Channel) are told apart or refused.