diff --git a/include/link.h b/include/link.h index 34710597f..3b621e6c1 100644 --- a/include/link.h +++ b/include/link.h @@ -56,17 +56,17 @@ #define LINKCMD_BLENDER_SEND_KEYS 0x4444 #define LINKCMD_BLENDER_SCORE_BEST 0x4523 #define LINKCMD_BLENDER_SCORE_GOOD 0x5432 -#define LINKCMD_0x5555 0x5555 -#define LINKCMD_0x5566 0x5566 +#define LINKCMD_DUMMY_1 0x5555 +#define LINKCMD_DUMMY_2 0x5566 #define LINKCMD_READY_CLOSE_LINK 0x5FFF -#define LINKCMD_0x6666 0x6666 -#define LINKCMD_0x7777 0x7777 +#define LINKCMD_SEND_EMPTY 0x6666 +#define LINKCMD_SEND_0xEE 0x7777 #define LINKCMD_BLENDER_PLAY_AGAIN 0x7779 #define LINKCMD_COUNTDOWN 0x7FFF #define LINKCMD_CONT_BLOCK 0x8888 #define LINKCMD_BLENDER_NO_BERRIES 0x9999 #define LINKCMD_BLENDER_NO_PBLOCK_SPACE 0xAAAA -#define LINKCMD_0xAAAB 0xAAAB +#define LINKCMD_SEND_ITEM 0xAAAB #define LINKCMD_READY_TO_TRADE 0xAABB #define LINKCMD_READY_FINISH_TRADE 0xABCD #define LINKCMD_INIT_BLOCK 0xBBBB @@ -76,17 +76,18 @@ #define LINKCMD_START_TRADE 0xCCDD #define LINKCMD_CONFIRM_FINISH_TRADE 0xDCBA #define LINKCMD_SET_MONS_TO_TRADE 0xDDDD -#define LINKCMD_0xDDEE 0xDDEE +#define LINKCMD_PLAYER_CANCEL_TRADE 0xDDEE #define LINKCMD_REQUEST_CANCEL 0xEEAA -#define LINKCMD_CANCEL_TRADE 0xEEBB -#define LINKCMD_0xEECC 0xEECC +#define LINKCMD_BOTH_CANCEL_TRADE 0xEEBB +#define LINKCMD_PARTNER_CANCEL_TRADE 0xEECC +#define LINKCMD_NONE 0xEFFF #define LINKTYPE_TRADE 0x1111 #define LINKTYPE_TRADE_CONNECTING 0x1122 #define LINKTYPE_TRADE_SETUP 0x1133 #define LINKTYPE_TRADE_DISCONNECTED 0x1144 #define LINKTYPE_BATTLE 0x2211 -#define LINKTYPE_0x2222 0x2222 // unused battle? +#define LINKTYPE_UNUSED_BATTLE 0x2222 // Unused, inferred from gap #define LINKTYPE_SINGLE_BATTLE 0x2233 #define LINKTYPE_DOUBLE_BATTLE 0x2244 #define LINKTYPE_MULTI_BATTLE 0x2255 @@ -98,7 +99,7 @@ #define LINKTYPE_BERRY_BLENDER_SETUP 0x4411 #define LINKTYPE_BERRY_BLENDER 0x4422 #define LINKTYPE_MYSTERY_EVENT 0x5501 -#define LINKTYPE_0x5502 0x5502 // unused? +#define LINKTYPE_UNUSED_EREADER 0x5502 // Unused, inferred from gap #define LINKTYPE_EREADER 0x5503 #define LINKTYPE_CONTEST_GMODE 0x6601 #define LINKTYPE_CONTEST_EMODE 0x6602 @@ -290,17 +291,17 @@ bool8 HandleLinkConnection(void); void SetLinkDebugValues(u32 seed, u32 flags); void SetBerryBlenderLinkCallback(void); void SetSuppressLinkErrorMessage(bool8 flag); -void sub_800B524(struct LinkPlayer *linkPlayer); +void ConvertLinkPlayerName(struct LinkPlayer *linkPlayer); u8 GetSioMultiSI(void); void ClearSavedLinkPlayers(void); -void BufferLinkErrorInfo(u32 status, u8 lastSendQueueCount, u8 lastRecvQueueCount, u8 unk_06); -void sub_800B348(void); +void BufferLinkErrorInfo(u32 status, u8 lastSendQueueCount, u8 lastRecvQueueCount, bool8 disconnected); +void LocalLinkPlayerToBlock(void); void LinkPlayerFromBlock(u32 who); bool32 Link_AnyPartnersPlayingFRLG_JP(void); void ResetLinkPlayerCount(void); void SaveLinkPlayers(u8 a0); void SetWirelessCommType0(void); -bool32 sub_800B504(void); +bool32 IsLinkRecvQueueLengthAtLeast3(void); extern u16 gLinkPartnersHeldKeys[6]; extern u32 gLinkDebugSeed; diff --git a/src/cable_club.c b/src/cable_club.c index 8b321d670..1d8c6e53d 100644 --- a/src/cable_club.c +++ b/src/cable_club.c @@ -904,7 +904,7 @@ static void Task_StartWirelessCableClubBattle(u8 taskId) { struct LinkPlayer *player = (struct LinkPlayer *)gBlockRecvBuffer[i]; gLinkPlayers[i] = *player; - sub_800B524(&gLinkPlayers[i]); + ConvertLinkPlayerName(&gLinkPlayers[i]); ResetBlockReceivedFlag(i); } tState = 4; diff --git a/src/link.c b/src/link.c index 9f0ac6654..6dc5a09f3 100644 --- a/src/link.c +++ b/src/link.c @@ -41,8 +41,8 @@ struct LinkTestBGInfo { u32 screenBaseBlock; u32 paletteNum; - u32 dummy_8; - u32 dummy_C; + u32 baseChar; + u32 unused; }; static struct BlockTransfer sBlockSend; @@ -97,28 +97,26 @@ struct Link gLink; u8 gLastRecvQueueCount; u16 gLinkSavedIme; -EWRAM_DATA u8 gLinkTestDebugValuesEnabled = 0; -EWRAM_DATA u8 gUnknown_020223BD = 0; +static EWRAM_DATA u8 sLinkTestDebugValuesEnabled = 0; +static EWRAM_DATA u8 sDummyFlag = FALSE; EWRAM_DATA u32 gBerryBlenderKeySendAttempts = 0; EWRAM_DATA u16 gBlockRecvBuffer[MAX_RFU_PLAYERS][BLOCK_BUFFER_SIZE / 2] = {}; EWRAM_DATA u8 gBlockSendBuffer[BLOCK_BUFFER_SIZE] = {}; -EWRAM_DATA bool8 gLinkOpen = FALSE; +static EWRAM_DATA bool8 sLinkOpen = FALSE; EWRAM_DATA u16 gLinkType = 0; -EWRAM_DATA u16 gLinkTimeOutCounter = 0; +static EWRAM_DATA u16 sTimeOutCounter = 0; EWRAM_DATA struct LinkPlayer gLocalLinkPlayer = {}; EWRAM_DATA struct LinkPlayer gLinkPlayers[MAX_RFU_PLAYERS] = {}; -EWRAM_DATA struct LinkPlayer gSavedLinkPlayers[MAX_RFU_PLAYERS] = {}; +static EWRAM_DATA struct LinkPlayer sSavedLinkPlayers[MAX_RFU_PLAYERS] = {}; EWRAM_DATA struct { u32 status; u8 lastRecvQueueCount; u8 lastSendQueueCount; - u8 unk_06; + bool8 disconnected; } sLinkErrorBuffer = {}; static EWRAM_DATA u16 sReadyCloseLinkAttempts = 0; // never read static EWRAM_DATA void *sLinkErrorBgTilemapBuffer = NULL; -// Static ROM declarations - static void InitLocalLinkPlayer(void); static void VBlankCB_LinkError(void); static void CB2_LinkTest(void); @@ -131,7 +129,7 @@ static void LinkCB_BlockSend(void); static void LinkCB_BlockSendEnd(void); static void SetBlockReceivedFlag(u8 who); static u16 LinkTestCalcBlockChecksum(const u16 *src, u16 size); -static void LinkTest_prnthex(u32 pos, u8 a0, u8 a1, u8 a2); +static void LinkTest_PrintHex(u32 pos, u8 a0, u8 a1, u8 a2); static void LinkCB_RequestPlayerDataExchange(void); static void Task_PrintTestData(u8 taskId); @@ -160,8 +158,6 @@ static void DoSend(void); static void StopTimer(void); static void SendRecvDone(void); -// .rodata - static const u16 sWirelessLinkDisplayPal[] = INCBIN_U16("graphics/interface/wireless_link_display.gbapal"); static const u32 sWirelessLinkDisplayGfx[] = INCBIN_U32("graphics/interface/wireless_link_display.4bpp.lz"); static const u32 sWirelessLinkDisplayTilemap[] = INCBIN_U32("graphics/interface/wireless_link_display.bin.lz"); @@ -226,15 +222,13 @@ static const struct WindowTemplate sLinkErrorWindowTemplates[] = { }; static const u8 sTextColors[] = { TEXT_COLOR_TRANSPARENT, TEXT_COLOR_WHITE, TEXT_COLOR_DARK_GREY }; -static const u8 sUnused_082ED224[] = {0x00, 0xFF, 0xFE, 0xFF, 0x00}; - -// .text +static const u8 sUnusedData[] = {0x00, 0xFF, 0xFE, 0xFF, 0x00}; bool8 IsWirelessAdapterConnected(void) { SetWirelessCommType1(); InitRFUAPI(); - if (rfu_LMAN_REQBN_softReset_and_checkID() == 0x8001) + if (rfu_LMAN_REQBN_softReset_and_checkID() == RFU_ID) { rfu_REQ_stopMode(); rfu_waitREQComplete(); @@ -251,13 +245,13 @@ void Task_DestroySelf(u8 taskId) DestroyTask(taskId); } -static void InitLinkTestBG(u8 paletteNum, u8 bgNum, u8 screenBaseBlock, u8 charBaseBlock, u16 a4) +static void InitLinkTestBG(u8 paletteNum, u8 bgNum, u8 screenBaseBlock, u8 charBaseBlock, u16 baseChar) { LoadPalette(sLinkTestDigitsPal, paletteNum * 16, 0x20); - DmaCopy16(3, sLinkTestDigitsGfx, (u16 *)BG_CHAR_ADDR(charBaseBlock) + (16 * a4), sizeof sLinkTestDigitsGfx); + DmaCopy16(3, sLinkTestDigitsGfx, (u16 *)BG_CHAR_ADDR(charBaseBlock) + (16 * baseChar), sizeof sLinkTestDigitsGfx); gLinkTestBGInfo.screenBaseBlock = screenBaseBlock; gLinkTestBGInfo.paletteNum = paletteNum; - gLinkTestBGInfo.dummy_8 = a4; + gLinkTestBGInfo.baseChar = baseChar; switch (bgNum) { case 1: @@ -274,17 +268,19 @@ static void InitLinkTestBG(u8 paletteNum, u8 bgNum, u8 screenBaseBlock, u8 charB SetGpuReg(REG_OFFSET_BG0VOFS + bgNum * 4, 0); } -void sub_80094EC(u8 paletteNum, u8 bgNum, u8 screenBaseBlock, u8 charBaseBlock) +// Unused +static void LoadLinkTestBgGfx(u8 paletteNum, u8 bgNum, u8 screenBaseBlock, u8 charBaseBlock) { LoadPalette(sLinkTestDigitsPal, paletteNum * 16, 0x20); DmaCopy16(3, sLinkTestDigitsGfx, (u16 *)BG_CHAR_ADDR(charBaseBlock), sizeof sLinkTestDigitsGfx); gLinkTestBGInfo.screenBaseBlock = screenBaseBlock; gLinkTestBGInfo.paletteNum = paletteNum; - gLinkTestBGInfo.dummy_8 = 0; + gLinkTestBGInfo.baseChar = 0; SetGpuReg(sBGControlRegs[bgNum], BGCNT_SCREENBASE(screenBaseBlock) | BGCNT_CHARBASE(charBaseBlock)); } -void LinkTestScreen(void) +// Unused +static void LinkTestScreen(void) { int i; @@ -346,10 +342,9 @@ static void InitLink(void) int i; for (i = 0; i < CMD_LENGTH; i++) - { - gSendCmd[i] = 0xEfff; - } - gLinkOpen = TRUE; + gSendCmd[i] = LINKCMD_NONE; + + sLinkOpen = TRUE; EnableSerial(); } @@ -402,7 +397,7 @@ void CloseLink(void) { LinkRfu_Shutdown(); } - gLinkOpen = FALSE; + sLinkOpen = FALSE; DisableSerial(); } @@ -413,14 +408,14 @@ static void TestBlockTransfer(u8 nothing, u8 is, u8 used) if (sLinkTestLastBlockSendPos != sBlockSend.pos) { - LinkTest_prnthex(sBlockSend.pos, 2, 3, 2); + LinkTest_PrintHex(sBlockSend.pos, 2, 3, 2); sLinkTestLastBlockSendPos = sBlockSend.pos; } for (i = 0; i < MAX_LINK_PLAYERS; i++) { if (sLinkTestLastBlockRecvPos[i] != sBlockRecv[i].pos) { - LinkTest_prnthex(sBlockRecv[i].pos, 2, i + 4, 2); + LinkTest_PrintHex(sBlockRecv[i].pos, 2, i + 4, 2); sLinkTestLastBlockRecvPos[i] = sBlockRecv[i].pos; } } @@ -435,8 +430,8 @@ static void TestBlockTransfer(u8 nothing, u8 is, u8 used) ResetBlockReceivedFlag(i); if (gLinkTestBlockChecksums[i] != 0x0342) { - gLinkTestDebugValuesEnabled = FALSE; - gUnknown_020223BD = FALSE; + sLinkTestDebugValuesEnabled = FALSE; + sDummyFlag = FALSE; } } } @@ -469,7 +464,7 @@ static void LinkTestProcessKeyInput(void) { SetCloseLinkCallback(); } - if (gLinkTestDebugValuesEnabled) + if (sLinkTestDebugValuesEnabled) { SetLinkDebugValues(gMain.vblankCounter2, gLinkCallback ? gLinkVSyncDisabled : gLinkVSyncDisabled | 0x10); } @@ -489,7 +484,7 @@ u16 LinkMain2(const u16 *heldKeys) { u8 i; - if (!gLinkOpen) + if (!sLinkOpen) { return 0; } @@ -555,10 +550,10 @@ static void ProcessRecvCmds(u8 unused) case LINKCMD_BLENDER_SEND_KEYS: gLinkPartnersHeldKeys[i] = gRecvCmds[i][1]; break; - case LINKCMD_0x5555: + case LINKCMD_DUMMY_1: gLinkDummy2 = TRUE; break; - case LINKCMD_0x5566: + case LINKCMD_DUMMY_2: gLinkDummy2 = TRUE; break; case LINKCMD_INIT_BLOCK: @@ -612,7 +607,7 @@ static void ProcessRecvCmds(u8 unused) linkPlayer->neverRead = 0; linkPlayer->progressFlags = 0; } - sub_800B524(linkPlayer); + ConvertLinkPlayerName(linkPlayer); if (strcmp(block->magic1, sASCIIGameFreakInc) != 0 || strcmp(block->magic2, sASCIIGameFreakInc) != 0) { @@ -664,22 +659,19 @@ static void BuildSendCmd(u16 command) gSendCmd[0] = LINKCMD_BLENDER_SEND_KEYS; gSendCmd[1] = gMain.heldKeys; break; - case LINKCMD_0x5555: - gSendCmd[0] = LINKCMD_0x5555; + case LINKCMD_DUMMY_1: + gSendCmd[0] = LINKCMD_DUMMY_1; break; - case LINKCMD_0x6666: - gSendCmd[0] = LINKCMD_0x6666; + case LINKCMD_SEND_EMPTY: + gSendCmd[0] = LINKCMD_SEND_EMPTY; gSendCmd[1] = 0; break; - case LINKCMD_0x7777: + case LINKCMD_SEND_0xEE: { u8 i; - - gSendCmd[0] = LINKCMD_0x7777; + gSendCmd[0] = LINKCMD_SEND_0xEE; for (i = 0; i < 5; i++) - { gSendCmd[i + 1] = 0xEE; - } break; } case LINKCMD_INIT_BLOCK: @@ -690,8 +682,8 @@ static void BuildSendCmd(u16 command) case LINKCMD_BLENDER_NO_PBLOCK_SPACE: gSendCmd[0] = LINKCMD_BLENDER_NO_PBLOCK_SPACE; break; - case LINKCMD_0xAAAB: - gSendCmd[0] = LINKCMD_0xAAAB; + case LINKCMD_SEND_ITEM: + gSendCmd[0] = LINKCMD_SEND_ITEM; gSendCmd[1] = gSpecialVar_ItemId; break; case LINKCMD_SEND_BLOCK_REQ: @@ -702,14 +694,13 @@ static void BuildSendCmd(u16 command) gSendCmd[0] = LINKCMD_READY_CLOSE_LINK; gSendCmd[1] = gReadyCloseLinkType; break; - case LINKCMD_0x5566: - gSendCmd[0] = LINKCMD_0x5566; + case LINKCMD_DUMMY_2: + gSendCmd[0] = LINKCMD_DUMMY_2; break; case LINKCMD_SEND_HELD_KEYS: if (gHeldKeyCodeToSend == 0 || gLinkTransferringData) - { break; - } + gSendCmd[0] = LINKCMD_SEND_HELD_KEYS; gSendCmd[1] = gHeldKeyCodeToSend; break; @@ -819,7 +810,7 @@ bool32 Link_AnyPartnersPlayingFRLG_JP(void) void OpenLinkTimed(void) { sPlayerDataExchangeStatus = EXCHANGE_NOT_STARTED; - gLinkTimeOutCounter = 0; + sTimeOutCounter = 0; OpenLink(); } @@ -892,7 +883,7 @@ u8 GetLinkPlayerDataExchangeStatusTimed(int minPlayers, int maxPlayers) } } } - else if (++gLinkTimeOutCounter > 600) + else if (++sTimeOutCounter > 600) { sPlayerDataExchangeStatus = EXCHANGE_TIMED_OUT; } @@ -909,9 +900,7 @@ bool8 IsLinkPlayerDataExchangeComplete(void) for (i = 0; i < GetLinkPlayerCount(); i++) { if (gLinkPlayers[i].linkType == gLinkPlayers[0].linkType) - { count++; - } } if (count == GetLinkPlayerCount()) { @@ -936,9 +925,7 @@ void ResetLinkPlayers(void) int i; for (i = 0; i <= MAX_LINK_PLAYERS; i++) - { gLinkPlayers[i] = (struct LinkPlayer){}; - } } static void ResetBlockSend(void) @@ -966,9 +953,8 @@ static bool32 InitBlockSend(const void *src, size_t size) else { if (src != gBlockSendBuffer) - { memcpy(gBlockSendBuffer, src, size); - } + sBlockSend.src = gBlockSendBuffer; } BuildSendCmd(LINKCMD_INIT_BLOCK); @@ -980,9 +966,7 @@ static bool32 InitBlockSend(const void *src, size_t size) static void LinkCB_BlockSendBegin(void) { if (++sBlockSendDelayCounter > 2) - { gLinkCallback = LinkCB_BlockSend; - } } static void LinkCB_BlockSend(void) @@ -1020,13 +1004,9 @@ void SetBerryBlenderLinkCallback(void) { gBerryBlenderKeySendAttempts = 0; if (gWirelessCommType) - { Rfu_SetBerryBlenderLinkCallback(); - } else - { gLinkCallback = LinkCB_BerryBlenderSendHeldKeys; - } } // Unused @@ -1044,9 +1024,8 @@ static void SendBerryBlenderNoSpaceForPokeblocks(void) u8 GetMultiplayerId(void) { if (gWirelessCommType == TRUE) - { return Rfu_GetMultiplayerId(); - } + return SIO_MULTI_CNT->id; } @@ -1061,18 +1040,16 @@ u8 bitmask_all_link_players_but_self(void) bool8 SendBlock(u8 unused, const void *src, u16 size) { if (gWirelessCommType == TRUE) - { return Rfu_InitBlockSend(src, size); - } + return InitBlockSend(src, size); } bool8 SendBlockRequest(u8 blockReqType) { if (gWirelessCommType == TRUE) - { return Rfu_SendBlockRequest(blockReqType); - } + if (gLinkCallback == NULL) { gBlockRequestType = blockReqType; @@ -1085,31 +1062,25 @@ bool8 SendBlockRequest(u8 blockReqType) bool8 IsLinkTaskFinished(void) { if (gWirelessCommType == TRUE) - { return IsLinkRfuTaskFinished(); - } + return gLinkCallback == NULL; } u8 GetBlockReceivedStatus(void) { if (gWirelessCommType == TRUE) - { return Rfu_GetBlockReceivedStatus(); - } + return (gBlockReceivedStatus[3] << 3) | (gBlockReceivedStatus[2] << 2) | (gBlockReceivedStatus[1] << 1) | (gBlockReceivedStatus[0] << 0); } static void SetBlockReceivedFlag(u8 who) { if (gWirelessCommType == TRUE) - { Rfu_SetBlockReceivedFlag(who); - } else - { gBlockReceivedStatus[who] = TRUE; - } } void ResetBlockReceivedFlags(void) @@ -1119,16 +1090,12 @@ void ResetBlockReceivedFlags(void) if (gWirelessCommType == TRUE) { for (i = 0; i < MAX_RFU_PLAYERS; i++) - { Rfu_ResetBlockReceivedFlag(i); - } } else { for (i = 0; i < MAX_LINK_PLAYERS; i++) - { gBlockReceivedStatus[i] = FALSE; - } } } @@ -1147,9 +1114,7 @@ void ResetBlockReceivedFlag(u8 who) void CheckShouldAdvanceLinkState(void) { if ((gLinkStatus & LINK_STAT_MASTER) && EXTRACT_PLAYER_COUNT(gLinkStatus) > 1) - { gShouldAdvanceLinkState = 1; - } } static u16 LinkTestCalcBlockChecksum(const u16 *src, u16 size) @@ -1159,92 +1124,90 @@ static u16 LinkTestCalcBlockChecksum(const u16 *src, u16 size) chksum = 0; for (i = 0; i < size / 2; i++) - { chksum += src[i]; - } + return chksum; } -static void LinkTest_prnthexchar(char a0, u8 a1, u8 a2) +static void LinkTest_PrintNumChar(char val, u8 x, u8 y) { u16 *vAddr; vAddr = (u16 *)BG_SCREEN_ADDR(gLinkTestBGInfo.screenBaseBlock); - vAddr[a2 * 32 + a1] = (gLinkTestBGInfo.paletteNum << 12) | (a0 + 1 + gLinkTestBGInfo.dummy_8); + vAddr[y * 32 + x] = (gLinkTestBGInfo.paletteNum << 12) | (val + 1 + gLinkTestBGInfo.baseChar); } -static void LinkTest_prntchar(char a0, u8 a1, u8 a2) +static void LinkTest_PrintChar(char val, u8 x, u8 y) { u16 *vAddr; vAddr = (u16 *)BG_SCREEN_ADDR(gLinkTestBGInfo.screenBaseBlock); - vAddr[a2 * 32 + a1] = (gLinkTestBGInfo.paletteNum << 12) | (a0 + gLinkTestBGInfo.dummy_8); + vAddr[y * 32 + x] = (gLinkTestBGInfo.paletteNum << 12) | (val + gLinkTestBGInfo.baseChar); } -static void LinkTest_prnthex(u32 pos, u8 a0, u8 a1, u8 a2) +static void LinkTest_PrintHex(u32 num, u8 x, u8 y, u8 length) { - char sp[32 / 2]; + char buff[16]; int i; - for (i = 0; i < a2; i++) + for (i = 0; i < length; i++) { - sp[i] = pos & 0xf; - pos >>= 4; + buff[i] = num & 0xF; + num >>= 4; } - for (i = a2 - 1; i >= 0; i--) + for (i = length - 1; i >= 0; i--) { - LinkTest_prnthexchar(sp[i], a0, a1); - a0++; + LinkTest_PrintNumChar(buff[i], x, y); + x++; } } -static void LinkTest_prntint(int a0, u8 a1, u8 a2, u8 a3) +static void LinkTest_PrintInt(int num, u8 x, u8 y, u8 length) { - char sp[32 / 2]; - int sp10; + char buff[16]; + int negX; int i; - sp10 = -1; - if (a0 < 0) + negX = -1; + if (num < 0) { - sp10 = a1; - a0 = -a0; + negX = x; + num = -num; } - for (i = 0; i < a3; i++) + for (i = 0; i < length; i++) { - sp[i] = a0 % 10; - a0 /= 10; + buff[i] = num % 10; + num /= 10; } - for (i = a3 - 1; i >= 0; i--) + for (i = length - 1; i >= 0; i--) { - LinkTest_prnthexchar(sp[i], a1, a2); - a1++; - } - if (sp10 != -1) - { - LinkTest_prnthexchar(*"\n", sp10, a2); + LinkTest_PrintNumChar(buff[i], x, y); + x++; } + + if (negX != -1) + LinkTest_PrintNumChar(*"\n", negX, y); } -static void LinkTest_prntstr(const char *a0, u8 a1, u8 a2) +static void LinkTest_PrintString(const char *str, u8 x, u8 y) { - int r6; + int xOffset; int i; - int r5; + int yOffset; - r5 = 0; - r6 = 0; - for (i = 0; a0[i] != 0; a0++) + yOffset = 0; + xOffset = 0; + for (i = 0; str[i] != 0; str++) { - if (a0[i] == *"\n") + if (str[i] == *"\n") { - r5++; - r6 = 0; + yOffset++; + xOffset = 0; } else { - LinkTest_prntchar(a0[i], a1 + r6, a2 + r5); - r6++; + LinkTest_PrintChar(str[i], x + xOffset, y + yOffset); + xOffset++; } } } @@ -1260,29 +1223,28 @@ static void LinkCB_RequestPlayerDataExchange(void) static void Task_PrintTestData(u8 taskId) { - char sp[32]; + char testTitle[32]; int i; - strcpy(sp, sASCIITestPrint); - LinkTest_prntstr(sp, 5, 2); - LinkTest_prnthex(gShouldAdvanceLinkState, 2, 1, 2); - LinkTest_prnthex(gLinkStatus, 15, 1, 8); - LinkTest_prnthex(gLink.state, 2, 10, 2); - LinkTest_prnthex(EXTRACT_PLAYER_COUNT(gLinkStatus), 15, 10, 2); - LinkTest_prnthex(GetMultiplayerId(), 15, 12, 2); - LinkTest_prnthex(gLastSendQueueCount, 25, 1, 2); - LinkTest_prnthex(gLastRecvQueueCount, 25, 2, 2); - LinkTest_prnthex(GetBlockReceivedStatus(), 15, 5, 2); - LinkTest_prnthex(gLinkDebugSeed, 2, 12, 8); - LinkTest_prnthex(gLinkDebugFlags, 2, 13, 8); - LinkTest_prnthex(GetSioMultiSI(), 25, 5, 1); - LinkTest_prnthex(IsSioMultiMaster(), 25, 6, 1); - LinkTest_prnthex(IsLinkConnectionEstablished(), 25, 7, 1); - LinkTest_prnthex(HasLinkErrorOccurred(), 25, 8, 1); + strcpy(testTitle, sASCIITestPrint); + LinkTest_PrintString(testTitle, 5, 2); + LinkTest_PrintHex(gShouldAdvanceLinkState, 2, 1, 2); + LinkTest_PrintHex(gLinkStatus, 15, 1, 8); + LinkTest_PrintHex(gLink.state, 2, 10, 2); + LinkTest_PrintHex(EXTRACT_PLAYER_COUNT(gLinkStatus), 15, 10, 2); + LinkTest_PrintHex(GetMultiplayerId(), 15, 12, 2); + LinkTest_PrintHex(gLastSendQueueCount, 25, 1, 2); + LinkTest_PrintHex(gLastRecvQueueCount, 25, 2, 2); + LinkTest_PrintHex(GetBlockReceivedStatus(), 15, 5, 2); + LinkTest_PrintHex(gLinkDebugSeed, 2, 12, 8); + LinkTest_PrintHex(gLinkDebugFlags, 2, 13, 8); + LinkTest_PrintHex(GetSioMultiSI(), 25, 5, 1); + LinkTest_PrintHex(IsSioMultiMaster(), 25, 6, 1); + LinkTest_PrintHex(IsLinkConnectionEstablished(), 25, 7, 1); + LinkTest_PrintHex(HasLinkErrorOccurred(), 25, 8, 1); + for (i = 0; i < MAX_LINK_PLAYERS; i++) - { - LinkTest_prnthex(gLinkTestBlockChecksums[i], 10, 4 + i, 4); - } + LinkTest_PrintHex(gLinkTestBlockChecksums[i], 10, 4 + i, 4); } void SetLinkDebugValues(u32 seed, u32 flags) @@ -1298,9 +1260,8 @@ u8 GetSavedLinkPlayerCountAsBitFlags(void) flags = 0; for (i = 0; i < gSavedLinkPlayerCount; i++) - { flags |= (1 << i); - } + return flags; } @@ -1311,9 +1272,8 @@ u8 GetLinkPlayerCountAsBitFlags(void) flags = 0; for (i = 0; i < GetLinkPlayerCount(); i++) - { flags |= (1 << i); - } + return flags; } @@ -1324,9 +1284,7 @@ void SaveLinkPlayers(u8 playerCount) gSavedLinkPlayerCount = playerCount; gSavedMultiplayerId = GetMultiplayerId(); for (i = 0; i < MAX_RFU_PLAYERS; i++) - { - gSavedLinkPlayers[i] = gLinkPlayers[i]; - } + sSavedLinkPlayers[i] = gLinkPlayers[i]; } // The number of players when trading began. This is frequently compared against the @@ -1349,7 +1307,7 @@ bool8 DoesLinkPlayerCountMatchSaved(void) for (i = 0; i < gSavedLinkPlayerCount; i++) { - if (gLinkPlayers[i].trainerId == gSavedLinkPlayers[i].trainerId) + if (gLinkPlayers[i].trainerId == sSavedLinkPlayers[i].trainerId) { if (gLinkType == LINKTYPE_BATTLE_TOWER) { @@ -1375,12 +1333,15 @@ bool8 DoesLinkPlayerCountMatchSaved(void) void ClearSavedLinkPlayers(void) { int i; - - // Clearly not what was meant to be written, but here it is anyway. - for (i = 0; i < 4; i++) - { - CpuSet(&gSavedLinkPlayers[i], NULL, sizeof(struct LinkPlayer)); - } + // The CpuSet loop below is incorrectly writing to NULL + // instead of sSavedLinkPlayers. + // Additionally it's using the wrong array size. +#ifdef UBFIX + memset(sSavedLinkPlayers, 0, sizeof(sSavedLinkPlayers)); +#else + for (i = 0; i < MAX_LINK_PLAYERS; i++) + CpuSet(&sSavedLinkPlayers[i], NULL, sizeof(struct LinkPlayer)); +#endif } void CheckLinkPlayersMatchSaved(void) @@ -1389,8 +1350,8 @@ void CheckLinkPlayersMatchSaved(void) for (i = 0; i < gSavedLinkPlayerCount; i++) { - if (gSavedLinkPlayers[i].trainerId != gLinkPlayers[i].trainerId - || StringCompare(gSavedLinkPlayers[i].name, gLinkPlayers[i].name) != 0) + if (sSavedLinkPlayers[i].trainerId != gLinkPlayers[i].trainerId + || StringCompare(sSavedLinkPlayers[i].name, gLinkPlayers[i].name) != 0) { gLinkErrorOccurred = TRUE; CloseLink(); @@ -1413,9 +1374,8 @@ u8 GetLinkPlayerCount_2(void) bool8 IsLinkMaster(void) { if (gWirelessCommType) - { return Rfu_IsMaster(); - } + return EXTRACT_MASTER(gLinkStatus); } @@ -1568,9 +1528,8 @@ void SetLinkStandbyCallback(void) else { if (gLinkCallback == NULL) - { gLinkCallback = LinkCB_Standby; - } + gLinkDummy1 = FALSE; } } @@ -1606,7 +1565,7 @@ static void LinkCB_StandbyForAll(void) static void CheckErrorStatus(void) { - if (gLinkOpen && EXTRACT_LINK_ERRORS(gLinkStatus)) + if (sLinkOpen && EXTRACT_LINK_ERRORS(gLinkStatus)) { if (!gSuppressLinkErrorMessage) { @@ -1620,12 +1579,12 @@ static void CheckErrorStatus(void) } } -void BufferLinkErrorInfo(u32 status, u8 lastSendQueueCount, u8 lastRecvQueueCount, bool8 unk_06) +void BufferLinkErrorInfo(u32 status, u8 lastSendQueueCount, u8 lastRecvQueueCount, bool8 disconnected) { sLinkErrorBuffer.status = status; sLinkErrorBuffer.lastSendQueueCount = lastSendQueueCount; sLinkErrorBuffer.lastRecvQueueCount = lastRecvQueueCount; - sLinkErrorBuffer.unk_06 = unk_06; + sLinkErrorBuffer.disconnected = disconnected; } void CB2_LinkError(void) @@ -1645,16 +1604,15 @@ void CB2_LinkError(void) ScanlineEffect_Stop(); if (gWirelessCommType) { - if (!sLinkErrorBuffer.unk_06) - { + if (!sLinkErrorBuffer.disconnected) gWirelessCommType = 3; - } + ResetLinkRfuGFLayer(); } SetVBlankCallback(VBlankCB_LinkError); ResetBgsAndClearDma3BusyFlags(0); InitBgsFromTemplates(0, sLinkErrorBgTemplates, ARRAY_COUNT(sLinkErrorBgTemplates)); - sLinkErrorBgTilemapBuffer = tilemapBuffer = malloc(0x800); + sLinkErrorBgTilemapBuffer = tilemapBuffer = malloc(BG_SCREEN_SIZE); SetBgTilemapBuffer(1, tilemapBuffer); if (InitWindows(sLinkErrorWindowTemplates)) { @@ -1714,14 +1672,16 @@ static void CB2_PrintErrorMessage(void) switch (gMain.state) { case 00: - if (sLinkErrorBuffer.unk_06) + // Below is only true for the RFU, so the other error + // type is inferred to be from a wired connection + if (sLinkErrorBuffer.disconnected) ErrorMsg_MoveCloserToPartner(); else ErrorMsg_CheckConnections(); break; case 02: ShowBg(0); - if (sLinkErrorBuffer.unk_06) + if (sLinkErrorBuffer.disconnected) ShowBg(1); break; case 30: @@ -1748,7 +1708,7 @@ static void CB2_PrintErrorMessage(void) { PlaySE(SE_PIN); gWirelessCommType = 0; - sLinkErrorBuffer.unk_06 = 0; + sLinkErrorBuffer.disconnected = FALSE; sub_81700F8(); } } @@ -1762,10 +1722,9 @@ static void CB2_PrintErrorMessage(void) } } } + if (gMain.state != 160) - { gMain.state++; - } } // TODO: there might be a file boundary here, let's name it @@ -1795,7 +1754,7 @@ bool8 HasLinkErrorOccurred(void) return gLinkErrorOccurred; } -void sub_800B348(void) +void LocalLinkPlayerToBlock(void) { struct LinkPlayerBlock *block; @@ -1816,11 +1775,11 @@ void LinkPlayerFromBlock(u32 who) block = (struct LinkPlayerBlock *)gBlockRecvBuffer[who_]; player = &gLinkPlayers[who_]; *player = block->linkPlayer; - sub_800B524(player); - if (strcmp(block->magic1, sASCIIGameFreakInc) != 0 || strcmp(block->magic2, sASCIIGameFreakInc) != 0) - { + ConvertLinkPlayerName(player); + + if (strcmp(block->magic1, sASCIIGameFreakInc) != 0 + || strcmp(block->magic2, sASCIIGameFreakInc) != 0) SetMainCallback2(CB2_LinkError); - } } bool8 HandleLinkConnection(void) @@ -1832,10 +1791,9 @@ bool8 HandleLinkConnection(void) { gLinkStatus = LinkMain1(&gShouldAdvanceLinkState, gSendCmd, gRecvCmds); LinkMain2(&gMain.heldKeys); + if ((gLinkStatus & LINK_STAT_RECEIVED_NOTHING) && sub_808766C() == TRUE) - { return TRUE; - } } else { @@ -1844,9 +1802,7 @@ bool8 HandleLinkConnection(void) if (sub_808766C() == TRUE) { if (r4 == TRUE || IsRfuRecvQueueEmpty() || r5) - { return TRUE; - } } } return FALSE; @@ -1855,42 +1811,34 @@ bool8 HandleLinkConnection(void) void SetWirelessCommType1(void) { if (gReceivedRemoteLinkPlayers == 0) - { gWirelessCommType = 1; - } } static void SetWirelessCommType0_Internal(void) { if (gReceivedRemoteLinkPlayers == 0) - { gWirelessCommType = 0; - } } void SetWirelessCommType0(void) { if (gReceivedRemoteLinkPlayers == 0) - { gWirelessCommType = 0; - } } u32 GetLinkRecvQueueLength(void) { if (gWirelessCommType != 0) - { return GetRfuRecvQueueLength(); - } + return gLink.recvQueue.count; } -bool32 sub_800B504(void) +bool32 IsLinkRecvQueueLengthAtLeast3(void) { if (GetLinkRecvQueueLength() > 2) - { return TRUE; - } + return FALSE; } @@ -1900,9 +1848,9 @@ u8 GetWirelessCommType(void) return gWirelessCommType; } -void sub_800B524(struct LinkPlayer *player) +void ConvertLinkPlayerName(struct LinkPlayer *player) { - player->progressFlagsCopy = player->progressFlags; + player->progressFlagsCopy = player->progressFlags; // ? Perhaps relocating for a longer name field ConvertInternationalString(player->name, player->language); } @@ -2410,9 +2358,7 @@ void ResetSendBuffer(void) for (i = 0; i < CMD_LENGTH; i++) { for (j = 0; j < QUEUE_CAPACITY; j++) - { - gLink.sendQueue.data[i][j] = 0xEFFF; - } + gLink.sendQueue.data[i][j] = LINKCMD_NONE; } } @@ -2429,9 +2375,7 @@ void ResetRecvBuffer(void) for (j = 0; j < CMD_LENGTH; j++) { for (k = 0; k < QUEUE_CAPACITY; k++) - { - gLink.recvQueue.data[i][j][k] = 0xEFFF; - } + gLink.recvQueue.data[i][j][k] = LINKCMD_NONE; } } } diff --git a/src/link_rfu_2.c b/src/link_rfu_2.c index 54f21ed9f..6366b9704 100644 --- a/src/link_rfu_2.c +++ b/src/link_rfu_2.c @@ -1697,7 +1697,7 @@ static void sub_801084C(u8 taskId) if (AreNoPlayersReceiving()) { ResetBlockReceivedFlags(); - sub_800B348(); + LocalLinkPlayerToBlock(); gTasks[taskId].data[0]++; } break; @@ -1786,7 +1786,7 @@ static void ReceiveRfuLinkPlayers(const struct SioInfo *sioInfo) for (i = 0; i < MAX_RFU_PLAYERS; i++) { gLinkPlayers[i] = sioInfo->linkPlayers[i]; - sub_800B524(gLinkPlayers + i); + ConvertLinkPlayerName(gLinkPlayers + i); } } @@ -1831,7 +1831,7 @@ static void Task_ExchangeLinkPlayers(u8 taskId) ResetBlockReceivedFlag(r4); r2 = (struct LinkPlayerBlock *)gBlockRecvBuffer[r4]; gLinkPlayers[r4] = r2->linkPlayer; - sub_800B524(gLinkPlayers + r4); + ConvertLinkPlayerName(gLinkPlayers + r4); gTasks[taskId].data[0]++; } break; @@ -1887,7 +1887,7 @@ static void sub_8010D0C(u8 taskId) case 0: if (Rfu.playerCount) { - sub_800B348(); + LocalLinkPlayerToBlock(); SendBlock(0, gBlockSendBuffer, sizeof(struct LinkPlayerBlock)); gTasks[taskId].data[0]++; } diff --git a/src/menu_helpers.c b/src/menu_helpers.c index b31d84a1e..3fe19efcc 100644 --- a/src/menu_helpers.c +++ b/src/menu_helpers.c @@ -319,7 +319,7 @@ bool8 MenuHelpers_CallLinkSomething(void) { if (sub_81221D0() == TRUE) return TRUE; - else if (sub_800B504() != TRUE) + else if (IsLinkRecvQueueLengthAtLeast3() != TRUE) return FALSE; else return TRUE; diff --git a/src/trade.c b/src/trade.c index a42c9c805..0e5110a58 100644 --- a/src/trade.c +++ b/src/trade.c @@ -1160,12 +1160,12 @@ static void ReactToLinkTradeData(u8 mpId, u8 status) { switch (gBlockRecvBuffer[0][0]) { - case LINKCMD_CANCEL_TRADE: + case LINKCMD_BOTH_CANCEL_TRADE: BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 16, RGB_BLACK); PrintTradeMessage(TRADE_MSG_WAITING_FOR_FRIEND); sTradeMenuData->tradeMenuFunc = TRADEMENUFUNC_CANCEL_TRADE_1; break; - case LINKCMD_0xEECC: + case LINKCMD_PARTNER_CANCEL_TRADE: PrintTradeMessage(TRADE_MSG_FRIEND_WANTS_TO_TRADE); sTradeMenuData->tradeMenuFunc = TRADEMENUFUNC_REDRAW_MAIN_MENU; break; @@ -1180,7 +1180,7 @@ static void ReactToLinkTradeData(u8 mpId, u8 status) BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 16, RGB_BLACK); sTradeMenuData->tradeMenuFunc = TRADEMENUFUNC_LINK_TRADE_WAIT_FADE; break; - case LINKCMD_0xDDEE: + case LINKCMD_PLAYER_CANCEL_TRADE: PrintTradeMessage(TRADE_MSG_CANCELED); sTradeMenuData->tradeMenuFunc = TRADEMENUFUNC_REDRAW_MAIN_MENU; } @@ -1208,7 +1208,7 @@ static void QueueLinkTradeData(void) && sTradeMenuData->partnerLinkFlagChoseAction == WANTS_TO_CANCEL) { PrintTradeMessage(TRADE_MSG_CANCELED); - sTradeMenuData->linkData[0] = LINKCMD_0xEECC; + sTradeMenuData->linkData[0] = LINKCMD_PARTNER_CANCEL_TRADE; sTradeMenuData->linkData[1] = 0; QueueAction(QUEUE_DELAY_DATA, QUEUE_SEND_DATA); sTradeMenuData->playerLinkFlagStatus = sTradeMenuData->partnerLinkFlagStatus = 0; @@ -1219,7 +1219,7 @@ static void QueueLinkTradeData(void) && sTradeMenuData->partnerLinkFlagChoseAction == WANTS_TO_TRADE) { PrintTradeMessage(TRADE_MSG_FRIEND_WANTS_TO_TRADE); - sTradeMenuData->linkData[0] = LINKCMD_0xDDEE; + sTradeMenuData->linkData[0] = LINKCMD_PLAYER_CANCEL_TRADE; sTradeMenuData->linkData[1] = 0; QueueAction(QUEUE_DELAY_DATA, QUEUE_SEND_DATA); sTradeMenuData->playerLinkFlagStatus = sTradeMenuData->partnerLinkFlagStatus = 0; @@ -1229,7 +1229,7 @@ static void QueueLinkTradeData(void) else if (sTradeMenuData->playerLinkFlagChoseAction == WANTS_TO_CANCEL && sTradeMenuData->partnerLinkFlagChoseAction == WANTS_TO_CANCEL) { - sTradeMenuData->linkData[0] = LINKCMD_CANCEL_TRADE; + sTradeMenuData->linkData[0] = LINKCMD_BOTH_CANCEL_TRADE; sTradeMenuData->linkData[1] = 0; QueueAction(QUEUE_DELAY_DATA, QUEUE_SEND_DATA); BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 16, RGB_BLACK); @@ -1255,7 +1255,7 @@ static void QueueLinkTradeData(void) || sTradeMenuData->partnerLinkFlagStatus == CANCEL_TRADE) { PrintTradeMessage(TRADE_MSG_CANCELED); - sTradeMenuData->linkData[0] = LINKCMD_0xDDEE; + sTradeMenuData->linkData[0] = LINKCMD_PLAYER_CANCEL_TRADE; sTradeMenuData->linkData[1] = 0; QueueAction(QUEUE_DELAY_DATA, QUEUE_SEND_DATA); sTradeMenuData->playerLinkFlagStatus = 0;