Rendered from docs/WIRELESS.md in the repository.
Goal: deliver Generation 3 Mystery Gift Wonder Cards over a link cable. The first target is the Japanese Old Sea Map, which takes Emerald players to Faraway Island and Mew. The DUO's GBA plays the part of the Game Boy Advance Wireless Adapter and of the distributor on the other side of the "wireless" link. The Pokémon game on a second GBA then receives and saves the gift itself, exactly as it did at the 2005 events.
Status:
- Step 1 (detection) passes on real hardware: a Japanese Emerald accepted the DUO as a Wireless Adapter (lab session 10).
- Step 2 (the command layer) passes in the simulator. On real hardware, commands stalled at 2 MHz (lab session 11). A probe found why (session 12), and the fix is ready to try.
Why the adapter, and why it can be faked
- In Emerald, FireRed and LeafGreen, Mystery Gift only works through the Wireless Adapter.
- The main menu offers it only when an adapter answers.
- Japanese Emerald's e-Reader option accepts only Trainer Hill data.
- Its Mystery Events (link cable) never received Wonder Cards. (Source: the Emerald decompilation,
main_menu.c,ereader_screen.c.) - The adapter is a small computer on the link port. It speaks a documented protocol: Normal-mode SPI with
32-bit words,
0x9966…command packets, and clock-direction swaps (see the gba-link-connection notes). A Raspberry Pi Pico has already been used to emulate it (GBA-Global-Link). Here, another GBA does the job.
Step 1: detection (done in the simulator)
When the main menu opens, the game calls IsWirelessAdapterConnected(). That resets the adapter and runs
librfu's Sio32ID exchange:
- Normal 32-bit mode, with the game as the clock master at 256 kHz, roughly 2 ms between attempts.
- The words spell NINTENDO and end with the adapter's ID, 0x8001. Nothing more is needed for the game to believe an adapter is present.
Nintendo's library implements both roles of this exchange. The DUO's program adapter_detect
(firmware/programs.py) is the adapter-side role, ported from it. In the simulator, against a model of
the game's side (tests/sio32id_model.py, from the same library), the words exchanged are exactly those
captured from a real adapter:
| Game sends | Adapter (DUO) answers |
|---|---|
FFFF494E |
494EFFFF |
B6B1494E |
494EB6B1 |
B6B1544E |
544EB6B1 |
| … | … |
B0BB8001 |
8001B0BB |
python duo_events.py adapter-test # needs firmware v4.2
The tool uploads the program, reports what the game is doing as it happens, and prints PASS when the game has accepted the adapter ID. Nothing is written to either cartridge.
Notes for the real test:
- Cable. The adapter conversation uses the crossed data lines, SO→SI and SI→SO. A GBA link cable (AGB-005) doesn't carry them both ways: one plug has no SO, because GBA multiplayer uses SD. A Game Boy Color link cable (CGB-003) fits the GBA's port and is crossed.
- Menu. The game shows Mystery Gift only once it's switched on with the questionnaire phrase at a Poké Mart. The detection runs either way, and the tool sees the result from the DUO's side.
- After detection, the game sends one adapter command ("stop mode"), which this first program doesn't answer. The game ignores the result of that command, so the menu carries on normally.
Step 2: the command layer (done in the simulator)
The second program, adapter (firmware/programs/adapter.c), plays a whole Wireless Adapter with
nobody else in range. It is written in C and built with clang for the GBA's ARM7TDMI. It does four
things:
- The ID exchange, as in step 1. It repeats the exchange whenever the game resets the adapter, which librfu does before every session and before putting the adapter to sleep.
- Commands. The game sends
0x9966LLCCplusLLdata words, and the adapter answers each with0x80000000. Then it acknowledges with0x9966LL(CC+0x80)plus its response. The game clocks every word at 2 MHz. Between words, both sides signal "ready" on their SO lines. On the DUO that's SIOCNT bit 3 out and bit 2 in, ported fromlibrfu_intr.c. - The clock swap. After
0x25,0x27,0x35or0x37, the game becomes the clock slave, and the adapter takes its turn. With no-one in range, there's only ever one thing to say:0x99660027, "nothing happened". It's sent after the number of frames the game chose in its Setup command (32 for Pokémon), with the inverted handshake. - The answers.
- Searches find nobody.
- Status reports "idle".
- Unknown commands are refused politely with
0x996601EE, as the real adapter does.
Every command the game sends is kept in a trace. The PC reads it and prints it as it happens:
python duo_events.py gift # needs firmware v4.2; Ctrl+C or 10 minutes to stop
Testing in the simulator. tests/rfu_game_model.py is the game's side of the link, ported from
librfu:
- the master and slave word handlers;
- the timeouts and the two retries;
- the ID exchange.
The simulator now models both SO lines, either GBA being clock master, and a word being lost when the receiving side wasn't ready, as on hardware. The tests play:
- the whole Mystery Gift search, as Emerald drives it with nobody in range;
- a command abandoned half-way and started again;
- a silent cable.
All three pass with no retries. Four planted bugs are each caught:
- skipping the handshake;
- a wrong acknowledgement;
- never taking the adapter's turn;
- no recovery from an abandoned command.
What to look for on real hardware
- Enable Mystery Gift in the game first. Answer the Poké Mart questionnaire with the phrase. One source (WikiDex) gives the Japanese phrase as ともだち いっぱい ふしぎ つうしん; English games use LINK TOGETHER WITH ALL. Then save the game. That's the game's own save, done by the player; the tool never writes saves.
- Run
python duo_events.py gift, then choose Mystery Gift → Wonder Cards → Wireless Communication. - A healthy trace is:
- "ID exchange completed";
- Hello, setup and broadcast data;
- "start searching";
- "search results?" repeated about once a frame.
The game should keep showing its searching message. Press B to cancel: the trace should end with another ID exchange and "sleep (0x3D)". The game says the search was cancelled and returns to its menu, with no link error.
What real hardware showed (lab session 11)
The ID exchange works, but the first command never completes. The game clocks commands at 2 MHz, eight times faster than the ID exchange. Its words reach the DUO intact, but it rejects the DUO's answer three times and gives up.
A GBA clock slave answering over a link cable seems too slow at 2 MHz: gba-link-connection warns against GBA-to-GBA 2 Mbps except with very short wires. When the simulator adds a one-bit lag to the DUO's 2 MHz answers, it reproduces the session exactly.
The 2 MHz probe
python duo_events.py probe
This works like gift (the old --trace), but it answers each attempt of a command a different way:
- a one-bit lag: load
W << 1, with SO at W's top bit; - only the first bit late: load
W, with SO at its top bit; - as designed.
The game only moves on to the ready handshake when it got exactly 0x80000000, so the table at
the end shows which way works. For words after a handshake, which must be loaded "ready" (SO low),
the probe briefly flicks SO low and back high before the game's first clock edge. Whether that
gets the acknowledgement through is counted too. The simulator can't judge this part, because it
only sees the lines between instruction blocks.
The answer (lab session 12)
The probe showed that only the first bit is late. At 2 MHz, the game reads the first bit of each of the DUO's words from its SO resting level; the other 31 bits arrive intact.
The adapter program now handles this:
- It loads every answer with SO already at the word's top bit.
- After a handshake it flicks SO low ("ready") and straight back high. It stops the moment the game's SO shows it is about to clock, and leaves SIOCNT alone for the rest of the word.
- When the DUO takes its turn as clock master, it ignores the first bit of the game's acknowledgement, which arrives late the same way.
The simulator models this, and Emerald's whole search completes there with no retries.
Sessions 13 and 14: the ready flick
With the first-bit fix, the game accepts every command's first word. The acknowledgement then needs the DUO to signal "ready" (SO low) while still delivering a top bit of 1 when the clock starts. A two-instruction flick (about 0.25 µs low) was noticed in the probe (session 12), when it was repeated every few microseconds. It was almost never noticed at a slower repeat (session 14). So the flick is probably too narrow to make it reliably down the cable.
The game starts its clock a microsecond or two after it notices the low. That's
handshake_wait(0) in librfu_intr.c, then STWI_stop_timer_in_RAM(), then two SIOCNT writes. So
there is a window: wide enough to be seen, and over before the first clock edge.
The adapter now tunes the flick width itself:
- If the game doesn't clock the word, it widens the flick every four flicks.
- If the game clocks the word but then refuses it (first bit wrong), it narrows it.
- It restarts each word from the last width that worked.
- After each flick it keeps clear of SIOCNT for longer than a whole word takes.
At the end of a trace, the PC prints a table of flicks per width (clocked, accepted, refused), the width it settled on, the measured length of one width step, and a link scope: the game's SO line sampled after the first few flicks.
Step 3, stage B: a distributor in range (done in the simulator)
The adapter now has one host in range. It broadcasts what Emerald's Mystery Gift looks for:
- serial number 0x7F7D (
RFU_SERIAL_WONDER_DISTRIBUTOR); - as its game name, a
struct RfuGameDatasaying "Japanese, Emerald, has a Wonder Card"; - user name "DUO";
- the checksum librfu insists on.
When the game connects (0x1F, 0x20, 0x21), the adapter accepts it. The trace says "the game
connected to the distributor". In the simulator, a model of Emerald's search filter and
connection sequence finds the host and connects. See DISTRIBUTOR.md for the
whole plan.
Stages C and D1 also pass in the simulator: after connecting, the game's name arrives over librfu's reliable NI channel, and the host tells the game it may join. The details are in DISTRIBUTOR.md.
A real adapter in the DUO's port (adapter-check, 25 November)
Two real Wireless Adapters arrived. Everything above has the DUO playing the adapter, checked
against the decompilation and the simulator, never against a real adapter. adapter-check turns
it round: with an adapter plugged straight into the DUO's GBA, the DUO plays the game's side.
The program is firmware/programs/rfu_check.c, ported from librfu's clock-master code:
AgbRFU_SoftReset, then the Sio32ID exchange as clock master at 256 kHz.- The status commands at 2 MHz, each with librfu's ready handshake: 0x10, 0x12, 0x13, 0x11, 0x14 and 0x15. Then Emerald's setup 0x17 (0x003C0420), and 0x16 with an empty name.
- A search: 0x1C, then 0x1D once a frame for the seconds asked, keeping every broadcast heard, then 0x1E.
- 0x13 again, then 0x3D sleep.
It never hosts, connects or sends data.
The PC prints each answer next to what adapter.c answers (EMULATED_ANSWERS in duo_events.py)
and the longest handshake waits. It decodes each broadcast heard: serial, game and language,
trainer ID, Union Room activity, name and librfu's checksum. Everything goes into
logs/adapter_check_*.json.
In the simulator, tests/rfu_adapter_model.py plays the adapter as clock slave. The three
tests are:
- the adapter answers as
adapter.cdoes, and a Union Room host is heard; - different answers are reported, not mistaken for failures;
- an empty port gives up cleanly.
What it should settle on hardware:
- the real version (0x12), status (0x13) and configuration (0x15) words, which
adapter.cguessed; - how long a real adapter takes over each handshake;
- the exact bytes a game broadcasts in the Union Room, to compare with the room code.
Host mode (adapter-check --host, 25 November evening). The first step towards real
over-the-air gifts. After the questions, the DUO broadcasts through the real adapter what a
distributor broadcasts:
- 0x16 with the serial (7F7D by default), "English Emerald, has a Wonder Card", the activity, the
user name "DUO", and librfu's checksum. This is the layout of adapter.c's
build_host.
Then it hosts:
- 0x19 opens the host.
- 0x1A asks "who has joined?" once a frame.
- When a game joins, 0x1B closes the host to others.
- 0x26 then reads the game's frames for two seconds, keeping the first 64 words.
- 0x30 hangs up, and 0x3D puts the adapter to sleep.
It sends the game nothing yet, so the game gives up with a communication error. This stage only answers: does a real game find and join the DUO over the air?
It polls with 0x24/0x26 and never uses the clock-swap commands. gba-link-connection's
LinkWireless does the same; to check on hardware. Two simulator tests cover it, with a model
adapter that lets a child join. Next: send the game the host's frames (adapter.c's
host_frame and the Mystery Gift server), so the card goes over the air.
The air model (simulator, 25-26 November)
The DUO with a real adapter is one end of a radio link, so the simulator now has the other end too
(tests/rfu_air.py, tests/rfu_wire.py). The pieces:
- The link. Each game talks to its own adapter model over a link cable. That is the bit-level
link port of
rfu_game_modelandrfu_adapter_model, including the clock swap after 0x25 and 0x27. - The radio. The adapters relay to each other through
Air. It covers broadcast and search (0x16, 0x1C-0x1E), hosting (0x19-0x1B), connecting (0x1F-0x21), data both ways (0x24/0x25 to 0x26) and hanging up (0x30). - The data. A packet arrives as the words the sender gave. A parent's packet is a byte count
followed by 3-byte-headed sub-frames; a child's is the byte count << 8 followed by 2-byte
sub-frames. These are the formats adapter.c's
host_receiveandjoin_receiveread. - The swap. After a swap the adapter says 0x28 "data ready" as soon as a packet is waiting, and 0x27 "nothing happened" after two frames.
- Time. Python games run on their own
rfu_wire.Clock. When the emulated DUO is one of the stations, its adapter's answers pull that clock up to the emulator's time.
Two tests use it:
tests/test_rfu_air.py(pure Python): a game hosts, a second game finds it with Emerald's Mystery Gift search, joins, and three packets go each way.- In
tests/test_v4.py, "adapter check --host over the air": the DUO runsrfu_checkin host mode through its adapter, a Python game with its own adapter finds the broadcast and joins, and the game's "HELLO" frame arrives among the frames the DUO keeps.
Children use slot 0 only, as in adapter.c. Radio timing, signal strength and lost packets are not modelled yet.
The distributor through a real adapter (air_host, 26 November)
The air_host build is adapter.c compiled with -DAIR_HOST (firmware/programs.py). It keeps the
distributor's server side as it is: build_host's broadcast, host_receive, host_frame, the NI
layer, link_rfu_2's parent and the Mystery Gift server.
What changes is the link port. The part that played the adapter as clock slave is replaced by librfu's master side, taken from rfu_check.c, so the DUO is now the parent game with a real adapter in its port:
- AgbRFU_SoftReset and Sio32ID, then 0x10, 0x17, 0x16 (
host_cfg) and 0x19. - 0x1A once a frame until a game joins, then 0x1B.
- Once a frame: 0x26 (the child's packet, handed to
host_receive), then 0x24 (host_frame's packet, resent if the game's UNI answer went missing, as before). - 0x30 and 0x3D at the end.
PC selectors: 35 GO (seconds), 36 status (step | failures << 8 | child id << 16), 37 frames | packets received << 16, 38 retries | resends << 16. The trace, the gift loading (PUT, 20/21) and the NI selectors are unchanged.
Size: 10,316 bytes, ending at 0x0203A85C, well below 0x0203AE00. The slave builds (adapter, union, join, lab, probe) come out byte for byte the same as before.
The simulator test "air_host: ..." in tests/test_v4.py puts the air model's adapter in the
DUO's port and a Python game with its own adapter over the air. The game finds the distributor with
Japanese Emerald's search (serial 0x7F7F), joins, and sends its name over NI. The DUO's NI layer
receives it and answers "you may join" (RFU_STATUS_JOIN_GROUP_OK), with no retries. There is no PC
command for it yet.
End to end (02:00). The second test, "air_host: end to end over the air", loads a Wonder Card
the way the tool does (PUT, then the CRC check). It then runs the game's whole Mystery Gift
(gift_script: search, connect, name, join status, link players, receive_gift) against the DUO
through the two adapters. The game saves the card and its RAM script byte for byte. The DUO
finishes with mg_step 6, no errors and no command retries, after about 190 frames of 0x26 + 0x24.
Two things came out of it:
- The swap timeout. A child's adapter says 0x27 "nothing happened" only after the low byte of the game's 0x17 system config, in frames: 32 for Emerald's 0x003C0420, as adapter.c reads it. The air model now uses that value. With a fixed two frames, the game gave up mid-gift.
- A pause on the DUO. Once, during the Mystery Gift phase, the DUO's frame loop went 4 frames between packets. That is harmless against a 32-frame timeout, but worth checking on hardware (the loop's slowest frame). It is probably host_frame's Mystery Gift step building a block.
gift --air (26 November)
python duo_events.py gift --air --card FILE.wc3 [--serial HEX] [--seconds N] is the command (hardware
test 23). The steps:
- Upload
air_hostand load the card (the same PUT and CRC check asgift). - Set the broadcast with PC selector 39 (serial | language << 24). By default the serial follows the card's language: 7F7F and language 1 for a Japanese card, 7F7D and 2 otherwise.
- GO (selector 35) with at most 120 seconds.
- Print each step and the trace as they happen.
- Stop three seconds after Mystery Gift reaches its last step. The program then hangs up (0x30) and puts the adapter to sleep (0x3D).
The record is air_gift.last; there is no log file yet.
The tests in tests/test_v4.py:
- the tool's command with an English card (serial 7F7D) over the air model, with the game saving it;
- no card, and nobody joining;
- the two firmware-level tests above.
Planted bugs (tests/mutate4.py), all caught:
- the firmware: the game's packet handed over one word late; resending only the UNI packets the game answered; the PC's serial losing its top bit;
- the tool: the Japanese serial for every card; stopping as soon as a game joins.
The firmware ones were checked here by patching the five changed bytes of the embedded image,
because the PC has no clang. mutate4 prog rebuilds them wherever clang 18.1 is installed.
The DUO joins a game over the air (air_join, 26 November)
The air_join build is adapter.c with -DAIR_HOST -DGAME_HOSTS -DUNION_ROOM. It is the "join"
build's child (our name over NI, link_rfu_2's child, the Mystery Gift client), now driving a real
adapter as librfu's child:
- 0x16 (our game data, from
our_name), 0x1C, then 0x1D once a frame until a game with serial 0x0002 shows, then 0x1E. - 0x1F to that host, 0x20 until done, 0x21.
- Once a frame: 0x26, whose packet goes to
join_receive, then 0x24 withjoin_frame's packet.
Size: 11,260 bytes, ending at 0x0203AC0C. The other builds are unchanged byte for byte.
In the simulator, the game hosts (host_game_model's lead_and_accept) and the DUO finds it over
the air and connects. Its name arrives over NI, the game answers "you may join", and there are no
retries.
Still open: Mystery Gift with a friend over the air (lead_and_share). The card and RAM
script blocks reach the DUO and the game's server finishes, but the link never closes. The DUO's
client is left in mode 0 with join_linked 0. The cable build passes the same exchange. The test is
kept and skipped; set AIR_JOIN_H3=1 to run it. Narrowed down at 07:00:
- the game sends READY_CLOSE once and waits for the DUO's (
wait_all_ready_close, ready [True, False]); - the DUO never sends it: there is no "ready to close" in its trace;
- its selector 32 reads join_linked 0 at the end, although it traced "link players exchanged".
Something is resetting the DUO's child state or keeping its send-block state busy, so close_due
is never acted on. The failure message (host_game_model) now names the game's link callback and
its ready flags.
The air model now has radio rounds. It carries one packet each way per frame, in order (a queue per station), as real adapters exchange once per radio frame. Without rounds, a sender faster than its partner piled up packets. With "latest wins", the Python game (not paced to frames) lost link tables. All the earlier over-the-air tests still pass with rounds.
Next steps
- Run
python duo_events.py giftagain on real hardware. Look at the flick table and the scope, whether the game finds and connects to "DUO", and the game's own data frames printed at the end. - A distributor in range. Answer "search results?" with one host whose broadcast data matches what Emerald's Mystery Gift looks for. Then handle Connect, and the data commands with real clock swaps ("data ready"), carrying librfu's own packet framing.
- The distributor: the host side of Mystery Gift, from the decompilation's
mystery_gift_server.c. It sends the genuine Wonder Card and its script (E - Item Old Sea Map (JPN).wc3from Project Pokémon's EventsGallery, supplied by the user, never stored in this repository). - Delivery: back up the save before, receive, then back up after and compare.