Skip to Content
The Alchemy Project

src/system/system_serial.c

The link cable: payload exchange, block transfers and the link test. More…

Included by system.c.

Structures

NameDescription
structSERIAL_HALFWORDA halfword in a record of its own, only to hold sySerialStepBlockTransfer's 1 (its FAKEMATCH).

Macros

NameDescription
#defineSERIAL_DEFINE_WAIT (name, pending)Each waits a tick at a time until pending is 0, giving up after SERIAL_TRANSFER_TIMEOUT_TICKS: the send, then the receive.
#defineLINK_TEST_BYTES 640The link test's block: LINK_TEST_TILES tiles' worth of bytes, shown from tile LINK_TEST_TILE of character block 0.
#defineLINK_TEST_TILES (LINK_TEST_BYTES / TILE_SIZE_4BPP)
#defineLINK_TEST_TILE (2 * BG_SCREEN_SIZE / TILE_SIZE_4BPP)
#defineLINK_TEST_SECOND_SOURCE (RomStart + 4096)What B sends: the cartridge's bytes from 4 KB in, where A sends its first.

Enumerations

NameDescription
enumanonymous enum { SERIAL_PACKET_HALFWORDS, SERIAL_PAYLOAD_BYTES, SERIAL_BLOCK_ … }

Functions

NameDescription
static inline voidsySerialStartClocking (volatile Uint32 *siocnt, Uint8 mode)Makes this console, the parent, clock the transfers: timer 3's interrupt starts each one from now on, and the serial interrupt is switched off in IE and SIOCNT.
static inline Sint32sySerialGetLinkStatus (Uint32 control)The link status (LINK_STAT) from the consoles heard and SIOCNT's control: bits 0 and 1 the consoles heard from this tick, bits 8 and 9 those heard from since the link began, and the clocking, error and extra console bits.
voidsySerialInit (void)Resets the serial port into multiplayer mode, installs the serial and timer 3 interrupt handlers, clears SerialStateWork and points its packet buffers into its storage, clears the bulk transfer state and sets SerialExchangeActive, which starts the per-tick exchange.
voidsySerialEnableTransferTimer (void)Lets the timer start transfers again once a link mode is set, by setting SerialStateWork.transferEnabled.
voidsySerialDisableTransfer (void)Unused: disables the serial and timer 3 interrupts, stops the transfer timer and clears SerialStateWork.transferEnabled.
Sint32sySerialPollAndTransfer (void *send, void *receive)Looks for the link partner and, once connected, does one exchange (as sySerialExchangePayloads).
Sint32sySerialPollStatus (void)As sySerialPollAndTransfer, without exchanging data: sySynch() calls it once a tick while SerialExchangeActive is set, and the VBlank handler does the exchange.
Sint32sySerialExchangePayloads (void *send, void *receive)
voidsySerialPrepareSendPacket (void *payload)Builds the packet to send in sendBuf[0]: [0] the packet number, [1] the consoles not yet heard from, [2] the checksum and [4] the 24-byte payload.
Uint8sySerialCollectReceivedPayloads (void *payload)Swaps in the packets the interrupt handler finished, then copies the payload of each one whose checksum is good to payload (24 bytes per console), and records the consoles heard from in currentMask and receivedMask.
voidsySerialHandleTransferInterrupt (void)The serial and timer 3 interrupt, installed by sySerialInit: each one moves a halfword.
voidsySerialRemoveIRQHandlers (void)Clears SerialExchangeActive, which stops the per-tick exchange, and removes the link interrupt handlers.
Uint32sySerialWaitForStatusMask (Sint32 mask)Waits a tick at a time until every status bit in mask is set in LinkStatusWork, then returns this console's multiplayer id.
Sint32sySerialStartSend (void *src, Sint32 size)Starts sending size bytes from src to the partner in 20-byte blocks: sets the send source, size and block number that sySerialStepBlockTransfer works from.
Sint32sySerialStartReceive (void *dest)Starts receiving the partner's blocks into dest: sets the receive destination and clears the received size.
voidsySerialWaitForTransfers (void)Waits until both the send and the receive have finished, giving up after SERIAL_TRANSFER_TIMEOUT_TICKS.
Sint32sySerialGetActiveTransfers (void)Returns bit 0 while a send runs and bit 1 while a receive runs.
voidsyLinkBattleResetTransferState (void)sySerialResetTransferState as a function, for the link set-up and a link battle that fails.
voidsySerialStepBlockTransfer (void)Runs each VBlank after the exchange, called by syVBlankHandler: moves the bulk transfer on by one 20-byte block each way.
voidsySerialTestDelay (void)An empty function the link test calls to waste time.
voidsySerialTestRun (void)A link cable test left in the game: it shows a row of tiles, receives blocks into the map grid metatile buffer and shows them as tiles, and sends a part of the ROM when A or B is held.

Description

The link cable: payload exchange, block transfers and the link test. Part of system.c.

Macros in detail

SERIAL_DEFINE_WAIT

#define SERIAL_DEFINE_WAIT(name, pending)

Each waits a tick at a time until pending is 0, giving up after SERIAL_TRANSFER_TIMEOUT_TICKS: the send, then the receive.

Definition at line 497 of file system_serial.c.

#define LINK_TEST_BYTES 640

The link test's block: LINK_TEST_TILES tiles' worth of bytes, shown from tile LINK_TEST_TILE of character block 0.

Referenced by sySerialTestRun().

Definition at line 719 of file system_serial.c.

#define LINK_TEST_TILES (LINK_TEST_BYTES / TILE_SIZE_4BPP)

Referenced by sySerialTestRun().

Definition at line 720 of file system_serial.c.

#define LINK_TEST_TILE (2 * BG_SCREEN_SIZE / TILE_SIZE_4BPP)

Referenced by sySerialTestRun().

Definition at line 721 of file system_serial.c.

#define LINK_TEST_SECOND_SOURCE (RomStart + 4096)

What B sends: the cartridge's bytes from 4 KB in, where A sends its first.

Referenced by sySerialTestRun().

Definition at line 723 of file system_serial.c.

Enumerations in detail

anonymous enum

enum { SERIAL_PACKET_HALFWORDS, SERIAL_PAYLOAD_BYTES, SERIAL_BLOCK_BYTES, SERIAL_TRANSFER_TIMEOUT_TICKS, … }

EnumeratorValueDescription
SERIAL_PACKET_HALFWORDS14A packet: its number, missing mask, checksum and the 24-byte payload.
SERIAL_PAYLOAD_BYTES24One console's payload each tick.
SERIAL_BLOCK_BYTES20Bulk data carried in each payload.
SERIAL_TRANSFER_TIMEOUT_TICKS600000About 10,000 seconds, nearly three hours.
SERIAL_IDLE_HALFWORD0xfefeSent and seen between packets.
SERIAL_TIMER_RELOAD65536 - 13981Timer 3's reload: it overflows 13981 cycles on, from one halfword transfer to the next, about 20 a tick.

Definition at line 4 of file system_serial.c.

Functions in detail

sySerialStartClocking

static inline void sySerialStartClocking(volatile Uint32 *siocnt, Uint8 mode)

Makes this console, the parent, clock the transfers: timer 3's interrupt starts each one from now on, and the serial interrupt is switched off in IE and SIOCNT.

References SerialStateWork.

Referenced by sySerialPollAndTransfer() and sySerialPollStatus().

Definition at line 23 of file system_serial.c.

sySerialGetLinkStatus

static inline Sint32 sySerialGetLinkStatus(Uint32 control)

The link status (LINK_STAT) from the consoles heard and SIOCNT's control: bits 0 and 1 the consoles heard from this tick, bits 8 and 9 those heard from since the link began, and the clocking, error and extra console bits.

References SerialStateWork, Sint32 and sySerialAddErrorFlag().

Referenced by sySerialExchangePayloads(), sySerialPollAndTransfer() and sySerialPollStatus().

Definition at line 41 of file system_serial.c.

sySerialInit

void sySerialInit(void)

Resets the serial port into multiplayer mode, installs the serial and timer 3 interrupt handlers, clears SerialStateWork and points its packet buffers into its storage, clears the bulk transfer state and sets SerialExchangeActive, which starts the per-tick exchange.

References LinkExchangeState, SerialExchangeActive, SerialReceiveDestination, SerialReceivedSize, SerialSendSize, SerialSendSource, SerialStateWork, Sint32, state, syIntFunction(), syLinkBattleResetTransferState() and sySerialHandleTransferInterrupt().

Referenced by scBattleArenaRestartLink(), scInit() and sySerialTestRun().

Definition at line 59 of file system_serial.c.

sySerialEnableTransferTimer

void sySerialEnableTransferTimer(void)

Lets the timer start transfers again once a link mode is set, by setting SerialStateWork.transferEnabled.

References SerialStateWork and state.

Definition at line 115 of file system_serial.c.

sySerialDisableTransfer

void sySerialDisableTransfer(void)

Unused: disables the serial and timer 3 interrupts, stops the transfer timer and clears SerialStateWork.transferEnabled.

References SerialStateWork and state.

Definition at line 125 of file system_serial.c.

sySerialPollAndTransfer

Sint32 sySerialPollAndTransfer(void *send, void *receive)

Looks for the link partner and, once connected, does one exchange (as sySerialExchangePayloads).

References SerialStateWork, Sint32, sySerialCollectReceivedPayloads(), sySerialGetLinkStatus(), sySerialPrepareSendPacket() and sySerialStartClocking().

Referenced by scTretTreeBranchExchangeInputPacket().

Definition at line 142 of file system_serial.c.

sySerialPollStatus

Sint32 sySerialPollStatus(void)

As sySerialPollAndTransfer, without exchanging data: sySynch() calls it once a tick while SerialExchangeActive is set, and the VBlank handler does the exchange.

References SerialStateWork, Sint32, sySerialGetLinkStatus() and sySerialStartClocking().

Referenced by sySynch().

Definition at line 190 of file system_serial.c.

sySerialExchangePayloads

Sint32 sySerialExchangePayloads(void *send, void *receive)

Once connected (SERIAL_PHASE_CONNECTED), gathers the received payloads into
`receive` and builds the next packet from `send`. Called by the VBlank
handler while SerialExchangeActive is set, which stores the result in
LinkStatusWork. Returns the link status:
      bits 0 and 1 : consoles 0 and 1 heard from this tick
      0x80         : this console clocks the transfers (LINK_STATUS_CLOCKING)
      bits 8 and 9 : consoles 0 and 1 heard from since the link began
      0x1000       : the last transfer interrupt found SIOCNT's error bit
                                     set (LINK_STATUS_ERROR)
      0x2000       : SIOCNT's multiplayer id is 2 or more
So LINK_STATUS_HEARD_BOTH is what the block transfer and
sySerialWaitForStatusMask(LINK_STATUS_HEARD_BOTH) wait for.

References SerialStateWork, Sint32, sySerialCollectReceivedPayloads(), sySerialGetLinkStatus() and sySerialPrepareSendPacket().

Referenced by syVBlankHandler().

Definition at line 230 of file system_serial.c.

sySerialPrepareSendPacket

void sySerialPrepareSendPacket(void *payload)

Builds the packet to send in sendBuf[0]: [0] the packet number, [1] the consoles not yet heard from, [2] the checksum and [4] the 24-byte payload.

References SerialStateWork and syDMAXCopy().

Referenced by sySerialExchangePayloads() and sySerialPollAndTransfer().

Definition at line 248 of file system_serial.c.

sySerialCollectReceivedPayloads

Uint8 sySerialCollectReceivedPayloads(void *payload)

Swaps in the packets the interrupt handler finished, then copies the payload of each one whose checksum is good to payload (24 bytes per console), and records the consoles heard from in currentMask and receivedMask.

References SerialStateWork, Sint32 and syDMAXCopy().

Referenced by sySerialExchangePayloads() and sySerialPollAndTransfer().

Definition at line 301 of file system_serial.c.

sySerialHandleTransferInterrupt

void sySerialHandleTransferInterrupt(void)

The serial and timer 3 interrupt, installed by sySerialInit: each one moves a halfword.

References SerialStateWork and Sint32.

Referenced by sySerialInit().

Definition at line 351 of file system_serial.c.

sySerialRemoveIRQHandlers

void sySerialRemoveIRQHandlers(void)

Clears SerialExchangeActive, which stops the per-tick exchange, and removes the link interrupt handlers.

References SerialExchangeActive and syIntFunction().

Referenced by btAppendLinkedActions(), scBattleArenaLeaveArena(), scBattleArenaRestartLink() and scInit().

Definition at line 438 of file system_serial.c.

sySerialWaitForStatusMask

Uint32 sySerialWaitForStatusMask(Sint32 mask)

Waits a tick at a time until every status bit in mask is set in LinkStatusWork, then returns this console's multiplayer id.

References LinkStatusWork, Sint32 and sySynch().

Referenced by sySerialTestRun().

Definition at line 447 of file system_serial.c.

sySerialStartSend

Sint32 sySerialStartSend(void *src, Sint32 size)

Starts sending size bytes from src to the partner in 20-byte blocks: sets the send source, size and block number that sySerialStepBlockTransfer works from.

References SerialBlockNumber, SerialSendSize, SerialSendSource, SerialTransfer, Sint32 and state.

Referenced by btAppendLinkedActions(), btLinkBattleSendParty(), scBattleArenaSendPartyRecords(), scInit() and sySerialTestRun().

Definition at line 459 of file system_serial.c.

sySerialStartReceive

Sint32 sySerialStartReceive(void *dest)

Starts receiving the partner's blocks into dest: sets the receive destination and clears the received size.

References SerialBlockNumber, SerialReceiveDestination, SerialReceivedSize, SerialTransfer, Sint32 and state.

Referenced by btAppendLinkedActions(), btLinkBattleReceiveParty(), scBattleArenaReceivePartyRecords() and sySerialTestRun().

Definition at line 478 of file system_serial.c.

sySerialWaitForTransfers

void sySerialWaitForTransfers(void)

Waits until both the send and the receive have finished, giving up after SERIAL_TRANSFER_TIMEOUT_TICKS. Reached through the stub table.

References SerialReceiveDestination, SerialSendSource and sySynch().

Definition at line 518 of file system_serial.c.

sySerialGetActiveTransfers

Sint32 sySerialGetActiveTransfers(void)

Returns bit 0 while a send runs and bit 1 while a receive runs. The link battle and the Battle Arena poll it between ticks.

References SerialReceiveDestination, SerialSendSource and Sint32.

Referenced by btAppendLinkedActions(), scBattleArenaReceivePartyRecords() and scBattleArenaSendPartyRecords().

Definition at line 534 of file system_serial.c.

syLinkBattleResetTransferState

void syLinkBattleResetTransferState(void)

sySerialResetTransferState as a function, for the link set-up and a link battle that fails.

References sySerialResetTransferState().

Referenced by btAppendLinkedActions() and sySerialInit().

Definition at line 552 of file system_serial.c.

sySerialStepBlockTransfer

void sySerialStepBlockTransfer(void)

Runs each VBlank after the exchange, called by syVBlankHandler: moves the bulk transfer on by one 20-byte block each way.

References LinkStatusWork, SerialBlockNumber, SerialPeerPayloads, SerialReceiveDestination, SerialReceivedSize, SerialSendSize, SerialSendSource, SerialTransfer and syDMAXCopy().

Referenced by syVBlankHandler().

Definition at line 566 of file system_serial.c.

sySerialTestDelay

void sySerialTestDelay(void)

An empty function the link test calls to waste time.

Referenced by sySerialTestRun().

Definition at line 713 of file system_serial.c.

sySerialTestRun

void sySerialTestRun(void)

A link cable test left in the game: it shows a row of tiles, receives blocks into the map grid metatile buffer and shows them as tiles, and sends a part of the ROM when A or B is held.

References MapGridMetatileBuffer, PerData, SerialReceiveDestination, Sint32, syDMAXCopy(), sySerialInit(), sySerialStartReceive(), sySerialStartSend(), sySerialTestDelay(), sySerialWaitForStatusMask(), sySoundPlayStub() and sySynch().

Definition at line 728 of file system_serial.c.