Skip to Content
The Alchemy Project

src/system/system_serial.c

Included by system.c.

Structures

NameDescription
structSERIAL_HALFWORDA halfword in a record of its own, which sySerialStepBlockTransfer keeps its constant 1 in.

Macros

NameDescription
#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_ … }The link cable: payload exchange, block transfers and the link test.
enumanonymous enum { SERIAL_SENDING, SERIAL_RECEIVING }The bits sySerialGetActiveTransfers returns.

Functions

NameDescription
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)Disables the serial and timer 3 interrupts, stops the transfer timer and clears SerialStateWork.transferEnabled.
static __inline__ voidsySerialStartClocking (volatile Uint32 *siocnt, Uint8 mode)The parent's connection: timer 3, not the serial interrupt, now clocks the transfers, in mode (SERIAL_MODE_CLOCKING).
static __inline__ Sint32sySerialGetLinkStatus (Uint32 control)The link status the polls return, from SIOCNT's control: the consoles heard from, LINK_STATUS_CLOCKING, LINK_STATUS_ERROR and LINK_STATUS_EXTRA_CONSOLE when the multiplayer id says a third is there.
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)Once connected (SERIAL_PHASE_CONNECTED), gathers the received payloads into receive and builds the next packet from send.
voidsySerialPrepareSendPacket (void *payload)Builds the packet to send in sendBuf[0] (SERIAL_PACKET): the packet number, the consoles not yet heard from, the checksum and 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 blocks of SERIAL_BLOCK_BYTES: 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.
voidsySerialWaitForSend (void)Waits a tick at a time until the queued send is done, giving up after SERIAL_TRANSFER_TIMEOUT_TICKS ticks.
voidsySerialWaitForReceive (void)Waits a tick at a time until the queued receive is done, giving up after SERIAL_TRANSFER_TIMEOUT_TICKS ticks.
voidsySerialWaitForTransfers (void)Waits until both the send and the receive have finished, giving up after SERIAL_TRANSFER_TIMEOUT_TICKS ticks.
Sint32sySerialGetActiveTransfers (void)Returns SERIAL_SENDING while a send runs and SERIAL_RECEIVING while a receive runs.
voidsyLinkBattleResetTransferState (void)Clears the serial transfer for a link battle with interrupts off: nothing to send or receive, no flags, and status SERIAL_STATUS_RUNNING.
voidsySerialStepBlockTransfer (void)Runs each VBlank after the exchange, called by syVBlankHandler: moves the bulk transfer on by one SERIAL_BLOCK_BYTES block each way.
voidsySerialTestDelay (void)An empty function the link test calls to waste time.
voidsySerialTestRun (void)Unused: a link cable test nothing calls.

Variables

NameDescription
extern Uint8MapGridMetatileBuffer []The map cell buffer at its own address, which the effects borrow: no map is loaded during a battle.

Macros in detail

#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 750 of file system_serial.c.

#define LINK_TEST_TILES (LINK_TEST_BYTES / TILE_SIZE_4BPP)

Referenced by sySerialTestRun().

Definition at line 751 of file system_serial.c.

#define LINK_TEST_TILE (2 * BG_SCREEN_SIZE / TILE_SIZE_4BPP)

Referenced by sySerialTestRun().

Definition at line 752 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 754 of file system_serial.c.

Enumerations in detail

anonymous enum

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

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

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 3 of file system_serial.c.

anonymous enum

enum { SERIAL_SENDING, SERIAL_RECEIVING }

The bits sySerialGetActiveTransfers returns.

EnumeratorValueDescription
SERIAL_SENDING1
SERIAL_RECEIVING2

Definition at line 20 of file system_serial.c.

Functions in detail

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, syIntFunction(), syLinkBattleResetTransferState() and sySerialHandleTransferInterrupt().

Referenced by scClearReceiveByLink(), scInit(), scLinkLobbyInitSerial() and sySerialTestRun().

Definition at line 37 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.

Definition at line 93 of file system_serial.c.

sySerialDisableTransfer

void sySerialDisableTransfer(void)

Disables the serial and timer 3 interrupts, stops the transfer timer and clears SerialStateWork.transferEnabled. Nothing in the game calls it.

References SerialStateWork.

Definition at line 103 of file system_serial.c.

sySerialStartClocking

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

The parent's connection: timer 3, not the serial interrupt, now clocks the transfers, in mode (SERIAL_MODE_CLOCKING).

References SerialStateWork.

Referenced by sySerialPollAndTransfer() and sySerialPollStatus().

Definition at line 120 of file system_serial.c.

sySerialGetLinkStatus

static __inline__ Sint32 sySerialGetLinkStatus(Uint32 control)

The link status the polls return, from SIOCNT's control: the consoles heard from, LINK_STATUS_CLOCKING, LINK_STATUS_ERROR and LINK_STATUS_EXTRA_CONSOLE when the multiplayer id says a third is there.

References result, SerialStateWork, Sint32 and sySerialAddErrorFlag().

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

Definition at line 140 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().

Definition at line 156 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 201 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: in bits 0 and 1 the consoles heard from this tick (both: LINK_STATUS_HEARD_BOTH, which the block transfer and sySerialWaitForStatusMask wait for), in bits 8 and 9 those heard from since the link began, and LINK_STATUS_CLOCKING, LINK_STATUS_ERROR and LINK_STATUS_EXTRA_CONSOLE.

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

Referenced by syVBlankHandler().

Definition at line 234 of file system_serial.c.

sySerialPrepareSendPacket

void sySerialPrepareSendPacket(void *payload)

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

References SerialStateWork and syDMAXCopy().

Referenced by sySerialExchangePayloads() and sySerialPollAndTransfer().

Definition at line 252 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 312 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 362 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(), scClearReceiveByLink(), scLinkLobbyInitSerial() and scLinkLobbyLeaveArena().

Definition at line 450 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 459 of file system_serial.c.

sySerialStartSend

Sint32 sySerialStartSend(void *src, Sint32 size)

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

References SerialBlockNumber, SerialSendSize, SerialSendSource, SerialTransfer and Sint32.

Referenced by btAppendLinkedActions(), btLinkBattleSendParty(), scLinkLobbySendPartyRecords() and sySerialTestRun().

Definition at line 471 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 and Sint32.

Referenced by btAppendLinkedActions(), btLinkBattleReceiveParty(), scClearReceiveByLink(), scLinkLobbyReceivePartyRecords() and sySerialTestRun().

Definition at line 490 of file system_serial.c.

sySerialWaitForSend

void sySerialWaitForSend(void)

Waits a tick at a time until the queued send is done, giving up after SERIAL_TRANSFER_TIMEOUT_TICKS ticks.

References SerialSendSource and sySynch().

Referenced by btLinkBattleSendParty().

Definition at line 509 of file system_serial.c.

sySerialWaitForReceive

void sySerialWaitForReceive(void)

Waits a tick at a time until the queued receive is done, giving up after SERIAL_TRANSFER_TIMEOUT_TICKS ticks.

References SerialReceiveDestination and sySynch().

Referenced by btLinkBattleReceiveParty().

Definition at line 525 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 ticks. Reached through the stub table.

References SerialReceiveDestination, SerialSendSource and sySynch().

Definition at line 542 of file system_serial.c.

sySerialGetActiveTransfers

Sint32 sySerialGetActiveTransfers(void)

Returns SERIAL_SENDING while a send runs and SERIAL_RECEIVING while a receive runs. The link battle and the Battle Arena wait on it between ticks.

References SerialReceiveDestination, SerialSendSource and Sint32.

Referenced by btAppendLinkedActions(), scLinkLobbyReceivePartyRecords() and scLinkLobbySendPartyRecords().

Definition at line 558 of file system_serial.c.

syLinkBattleResetTransferState

void syLinkBattleResetTransferState(void)

Clears the serial transfer for a link battle with interrupts off: nothing to send or receive, no flags, and status SERIAL_STATUS_RUNNING.

References SerialReceiveDestination, SerialReceivedSize, SerialSendSize, SerialSendSource and SerialTransfer.

Referenced by btAppendLinkedActions() and sySerialInit().

Definition at line 576 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 SERIAL_BLOCK_BYTES block each way.

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

Referenced by syVBlankHandler().

Definition at line 595 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 742 of file system_serial.c.

sySerialTestRun

void sySerialTestRun(void)

Unused: a link cable test nothing calls. It shows a row of tiles, receives the partner's blocks into the map grid metatile buffer and shows them as those tiles, and sends a part of the cartridge while A or B is held; Start stalls it a while.

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

Definition at line 760 of file system_serial.c.