Link cable
AcidBoy links two Game Boys for trading, battles and versus games in these ways:
| Mode | How | Where |
|---|---|---|
| Two games in one window | Desktop: Menu → Link cable → Two games in this window | LocalLink (core) |
| Two games on one phone | Mobile: Pause → Link cable → Two games on this phone | LocalLink in EmulatorHost |
| Direct network | Menu → Link cable → Host / Find / Join (TCP, port 5738); phones and computers mix | NetworkLink over TCP |
| Bluetooth | Mobile: Pause → Link cable → Host / Find a phone over Bluetooth | NetworkLink over Bluetooth LE |
| AcidBoy Online | Desktop: challenge a player in the lobby | NetworkLink over the server’s WebSocket relay |
The link code is shared by both apps and lives in src/AcidBoy.Client/Link: NetworkLink (the protocol),
LinkStepping (how a linked frame runs), LinkDiscovery (finding hosts on the LAN) and PacketStream (a
stream over small packets, for Bluetooth). Every network kind is the same NetworkLink over a different stream,
so a phone and a computer link exactly as two computers do.
Two games in one window
Section titled “Two games in one window”Pick a second ROM and both run side by side:
- Control: player 1 (keyboard and player-1 controllers) plays the outlined game, and F2 switches sides. A player-2 controller plays the other game.
- Sound: you hear the game you control.
- Same game twice: linking a game with itself (for example to trade between two saves) keeps the second save in
<rom>.2.sav.
The two consoles run in 8 slices per frame, so they are never more than about 2 ms of game time apart.
Two games on one phone
Section titled “Two games on one phone”The mobile app’s EmulatorHost runs the second game itself (AttachPartner), with the same 8 slices per frame
as the desktop (LinkStepping.RunLocalFrame):
- Screen: split in two. In landscape the games sit side by side; in portrait they are stacked, and the top half is turned round so two players can sit facing each other with the phone between them.
- Control: each half has its own touch buttons. Controllers take player 1 and player 2 in the order they are first used; with two connected, the touch buttons step aside (if Hide with a controller is on).
- Sound: tap a picture to hear that game; its bezel turns lime.
- Same game twice: the second copy keeps its own save,
saves/<sha1>.2.sav.
Network link (Link/NetworkLink.cs)
Section titled “Network link (Link/NetworkLink.cs)”NetworkLink connects two AcidBoy instances over any byte stream: a TCP socket for direct play, a
Bluetooth LE connection between phones, or a WebSocket stream to the relay for AcidBoy
Online.
Free running
Section titled “Free running”Two Game Boys joined by a cable run on their own clocks: one can be on the title screen while the other plays.
AcidBoy does the same. A game uses the cable while its serial port is requested (SC bit 7), a byte is on its way,
or it touched the port within the last second (IdleDots). Each side tells the other when its use starts and stops
(InUse message). Until both games use the cable, neither waits for the other: each counts the partner’s clock
as level with its own (an offset that follows the partner), so lockstep later starts with nobody behind. A game
listening alone, like Tetris’s title screen waiting for a second player, has nobody to wait for either.
A byte sent while the other game is not listening is lost, as on real hardware: the other game reads it as nobody answering.
Lockstep
Section titled “Lockstep”While both games use the cable (Synced), each side counts the dots it has emulated since the cable was plugged in
(LocalTime) and reports it to the partner after every slice (Time message). A side may only run while it is less
than the lead ahead of the partner’s last reported time. Each side sends the lead it wants (MaxLead, in a Lead
message) and both run with the larger one.
| Setting | MaxLead |
Use |
|---|---|---|
| Network sync: LAN | Half a frame | Direct play over a wired network |
| Network sync: Internet | 4 frames | High-latency direct play; always used online and by phones |
The lead must cover a round trip: a side that runs out of lead waits for the partner’s next Time. Half a frame
(about 8 ms) is shorter than a Wi-Fi round trip (5-30 ms, with spikes), so two consoles on the LAN setting over
Wi-Fi both crawl, at about 40% speed with gaps in the sound, and even at full average speed a side that keeps
running out of lead stalls on almost every frame. The mobile app therefore always asks for 4 frames, and has no
Network sync setting. Since both sides use the larger lead, a desktop on the LAN setting linked with a phone runs
with 4 frames too. Between two computers over Wi-Fi, choose Network sync: Internet on either one.
Three rules keep games such as Pokémon’s Cable Club correct at any lead:
- Transfers are timestamped. The master’s byte carries the emulated time its transfer started, and the partner applies it exactly when its own console reaches that time, never earlier. A byte applied “early” would find the partner’s game not yet listening, and would be lost.
- A listening side stays close. While a game waits for the partner’s clock (serial port armed
with the external clock), it may run only
ListeningLead(half a slice, about 1 ms of game time) ahead of the partner. Games treat a long silence as “nobody is sending” and change roles. Running far ahead, a side would give up on a byte the partner sent in time, both would become masters, and the link would hang. - Answers don’t wait for a frame. Games exchange hundreds of bytes one at a time. The network thread applies transfers and replies itself whenever the console is between frames, so a byte costs a network round trip, not a frame.
If the master’s 8 bits finish before the reply arrives, the master pauses (Serial.WaitingForLink)
until it does. The other side shows why it is waiting (see Status).
Wire protocol (version 5)
Section titled “Wire protocol (version 5)”Each message is a type byte followed by its payload. Integers are little-endian.
| Type | Message | Payload | Sent when |
|---|---|---|---|
| 1 | Hello |
u16 version, u8 length, UTF-8 game title, 20-byte ROM SHA-1 (zeros without a game) |
On connect |
| 2 | Time |
i64 dots emulated |
After every slice |
| 3 | Transfer |
i64 emulated time the transfer started, u8 data |
The master starts a transfer |
| 4 | Reply |
u8 data |
Answer to a Transfer |
| 5 | Bye |
— | Unplugging (a clean disconnect) |
| 6 | Activity |
u8: 0 running, 1 paused, 2 in menu, 3 no game |
When it changes |
| 7 | Ping |
— | After 1 s without sending anything |
| 8 | Lead |
i64 dots this side wants to be allowed ahead |
After Hello, and when the setting changes |
| 9 | InUse |
u8: 1 while this side’s game uses the cable, 0 after a second without |
When it changes |
The version is checked first, so a different version is refused with a clear message instead of
misreading the payload. Bump ProtocolVersion whenever the format changes.
| Version | Change |
|---|---|
| 2 | Hello carries the ROM SHA-1 |
| 3 | Activity and Ping |
| 4 | Transfer carries its emulated time |
| 5 | Lead: both sides run with the larger lead. InUse: the games run freely until both use the cable |
Liveness
Section titled “Liveness”Every message counts as a sign of life, and Ping fills the gaps while a side is paused or in a menu.
- After 4 s of silence the partner counts as lagging: “X is not responding (5s)…”.
- After 20 s (
UnresponsiveTimeout) the link closes with “The other player stopped responding”.
Disposing a link sends Bye, then closes the stream on a background task, because a WebSocket close
waits for the server’s answer.
Status on screen
Section titled “Status on screen”| Where | Shows |
|---|---|
| Bottom-right indicator | Green “Linked: X”, yellow “Connecting link…”, red “X: no answer” |
| Top-left while stalled | “X is in the menu; waiting…”, “X paused the game”, “X has no game loaded”, “X is not responding (Ns)” |
| Notification when it ends | “X unplugged the link cable”, or the error |
| Console log | Every link event with a timestamp (link: starting / linked with / closed / partner stopped answering) |
Threading
Section titled “Threading”All public methods are called from one thread: the desktop’s main thread, or the mobile app’s emulation
thread (EmulatorHost takes its own lock around every call). The read thread queues messages for Poll(),
which must run every loop iteration, even while paused or in a menu. EmulatorHost keeps polling every 16 ms
while paused, so heartbeats keep flowing and the partner sees “paused”. The _emulation lock guards the
console’s serial port and the link’s clock. The emulating thread holds it while emulating a slice; the read
thread takes it with TryEnter only, to apply transfers between frames.
Direct network play
Section titled “Direct network play”
- One player chooses Host network game (on a phone: Host over Wi-Fi). The menu shows the host’s addresses; the default port is 5738.
- The other chooses Find games on this network… and picks the host (on a phone, the Link cable
page searches as it opens), or Join network game… and types
192.168.1.5,name:portor[ipv6]:port. - Allow AcidBoy through the firewall when asked. Over the internet the host must forward the port, or you can use AcidBoy Online instead, which needs no port forwarding.
Phones and computers can host and join each other in any mix.
A host keeps hosting when its guest leaves (unplugs or loses the connection): it waits again on the same port, so the guest can rejoin without the host doing anything. Disconnect on the host stops hosting. Joining a phone that hosts needs a direct route to it; a router that puts the phone and the computer on different subnets often lets the phone reach the computer but not the other way round, so let the computer host then.
LAN discovery (Link/LinkDiscovery.cs)
Section titled “LAN discovery (Link/LinkDiscovery.cs)”A host answers searches on UDP port 5739 until somebody joins. The protocol is separate from the link’s, so it
never touches ProtocolVersion:
| Message | Bytes | Sent |
|---|---|---|
| Query | "ACIDBOY?", u8 version (1) |
Broadcast by the searcher, twice, to 255.255.255.255 and each interface’s broadcast address |
| Answer | "ACIDBOY!", u8 version, u16 TCP port, the first 4 bytes of the ROM’s SHA-1, u8 length, UTF-8 title |
Unicast back by each host still waiting |
Messages of another version are ignored. The searcher marks hosts that run the same ROM. Android holds a Wi-Fi
multicast lock while searching or hosting, since it filters broadcasts otherwise. On iOS, broadcasting needs an
entitlement Apple grants on request (com.apple.developer.networking.multicast); without it the search finds
nothing, and joining by address still works.
Bluetooth (mobile)
Section titled “Bluetooth (mobile)”Two phones link over Bluetooth LE with no network, Android and iPhone in any mix. The host is a GATT peripheral, the joiner a central:
| Item | Value |
|---|---|
| Service | a51db0e0-0001-4c1a-9e6b-2d3c4b5a6f70, advertised |
rx characteristic |
a51db0e0-0002-…: the joiner writes to the host (write without response) |
tx characteristic |
a51db0e0-0003-…: the host notifies the joiner |
| Host’s name | The game title, up to 20 bytes: manufacturer data (company 0xFFFF) from Android, the local name from iOS |
The bytes are the unchanged NetworkLink protocol. PacketStream turns them into packets of at most the
negotiated MTU minus 3 (Android asks for 247; iOS reports its own), with one packet in flight per direction:
Android waits for onNotificationSent/onCharacteristicWrite, iOS for peripheralManagerIsReady /
peripheralIsReadyToSendWriteWithoutResponse. Bluetooth always uses the loose (4-frame) sync, because its
round trips take a connection interval or two, like the internet’s.
Permissions: Android 12+ asks for Nearby devices (scan, connect, advertise; never used for location); Android 8-11 asks for location, which those versions need to scan. iOS asks for Bluetooth the first time.
On a phone, leaving the app pauses the game, and the partner sees “paused”. iOS suspends apps in the background, so after 20 s away the partner’s link closes, the same as for Wi-Fi.
Fast forward is off while linked. Opening the menu or pausing freezes both games (the partner is told why); the online chat does not pause anything. Reloading or resetting a game keeps the cable plugged in, but the other game will usually lose track of the link, as on real hardware.
