From c81d50cb2f57eb7d3121e71552c196f5867674cb Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Mon, 13 Nov 2017 01:46:22 -0500 Subject: [PATCH] LinkMain1 --- asm/cable_club.s | 8 +-- asm/link.s | 145 -------------------------------------- asm/mystery_event_menu.s | 2 +- asm/pokenav.s | 6 +- asm/trade.s | 6 +- include/link.h | 7 +- src/link.c | 147 +++++++++++++++++++++++++++++++++------ 7 files changed, 137 insertions(+), 184 deletions(-) diff --git a/asm/cable_club.s b/asm/cable_club.s index ae818fdc9..f892b2ca4 100644 --- a/asm/cable_club.s +++ b/asm/cable_club.s @@ -205,7 +205,7 @@ sub_80B24F8: @ 80B24F8 push {r4,lr} lsls r0, 24 lsrs r4, r0, 24 - bl sub_800B33C + bl HasLinkErrorOccurred lsls r0, 24 lsrs r0, 24 cmp r0, 0x1 @@ -239,7 +239,7 @@ sub_80B252C: @ 80B252C ands r0, r1 cmp r0, 0 beq _080B2570 - bl sub_800B320 + bl IsLinkConnectionEstablished lsls r0, 24 lsrs r1, r0, 24 cmp r1, 0 @@ -269,7 +269,7 @@ sub_80B2578: @ 80B2578 push {r4,lr} lsls r0, 24 lsrs r4, r0, 24 - bl sub_800B320 + bl IsLinkConnectionEstablished lsls r0, 24 cmp r0, 0 beq _080B258E @@ -309,7 +309,7 @@ sub_80B25CC: @ 80B25CC push {r4,lr} lsls r0, 24 lsrs r4, r0, 24 - bl sub_800B2E8 + bl GetSioMultiSI lsls r0, 24 lsrs r0, 24 cmp r0, 0x1 diff --git a/asm/link.s b/asm/link.s index 8ae822c47..3188bcb42 100644 --- a/asm/link.s +++ b/asm/link.s @@ -5,151 +5,6 @@ .text - thumb_func_start sub_800B638 -sub_800B638: @ 800B638 - push {r4-r7,lr} - adds r4, r0, 0 - adds r5, r1, 0 - adds r6, r2, 0 - ldr r0, =gLink - ldrb r0, [r0, 0x1] - cmp r0, 0x4 - bhi _0800B6EA - lsls r0, 2 - ldr r1, =_0800B65C - adds r0, r1 - ldr r0, [r0] - mov pc, r0 - .pool - .align 2, 0 -_0800B65C: - .4byte _0800B670 - .4byte _0800B680 - .4byte _0800B698 - .4byte _0800B6D4 - .4byte _0800B6DE -_0800B670: - bl DisableSerial - ldr r1, =gLink - movs r0, 0x1 - strb r0, [r1, 0x1] - b _0800B6EA - .pool -_0800B680: - ldrb r0, [r4] - cmp r0, 0x1 - bne _0800B6EA - bl EnableSerial - ldr r1, =gLink - movs r0, 0x2 - strb r0, [r1, 0x1] - b _0800B6EA - .pool -_0800B698: - ldrb r1, [r4] - cmp r1, 0x1 - beq _0800B6A8 - cmp r1, 0x2 - beq _0800B6C0 - bl sub_800B764 - b _0800B6EA -_0800B6A8: - ldr r2, =gLink - ldrb r0, [r2] - cmp r0, 0x8 - bne _0800B6EA - ldrb r0, [r2, 0x3] - cmp r0, 0x1 - bls _0800B6EA - strb r1, [r2, 0xE] - b _0800B6EA - .pool -_0800B6C0: - ldr r0, =gLink - movs r1, 0 - strb r1, [r0, 0x1] - ldr r0, =0x0400012a - strh r1, [r0] - b _0800B6EA - .pool -_0800B6D4: - bl sub_800B790 - ldr r1, =gLink - movs r0, 0x4 - strb r0, [r1, 0x1] -_0800B6DE: - adds r0, r5, 0 - bl sub_800B7C0 - adds r0, r6, 0 - bl sub_800B8A8 -_0800B6EA: - movs r0, 0 - strb r0, [r4] - ldr r1, =gLink - ldrb r2, [r1, 0x2] - ldrb r0, [r1, 0x3] - lsls r0, 2 - orrs r2, r0 - ldrb r0, [r1] - cmp r0, 0x8 - bne _0800B702 - movs r0, 0x20 - orrs r2, r0 -_0800B702: - ldrb r0, [r1, 0xC] - lsls r3, r0, 8 - ldrb r0, [r1, 0xF] - lsls r4, r0, 9 - ldrb r0, [r1, 0x10] - lsls r5, r0, 12 - ldrb r0, [r1, 0x11] - lsls r6, r0, 13 - ldrb r0, [r1, 0x12] - lsls r7, r0, 14 - ldrb r0, [r1, 0x1] - cmp r0, 0x4 - bne _0800B728 - movs r0, 0x40 - orrs r0, r3 - orrs r0, r2 - b _0800B72C - .pool -_0800B728: - adds r0, r2, 0 - orrs r0, r3 -_0800B72C: - orrs r0, r4 - orrs r0, r5 - orrs r0, r6 - orrs r0, r7 - adds r2, r0, 0 - ldrb r3, [r1, 0x13] - cmp r3, 0x1 - bne _0800B742 - movs r0, 0x80 - lsls r0, 9 - orrs r2, r0 -_0800B742: - ldrb r0, [r1, 0x2] - cmp r0, 0x3 - bls _0800B74E - movs r0, 0x80 - lsls r0, 10 - orrs r2, r0 -_0800B74E: - adds r1, r2, 0 - cmp r3, 0x2 - bne _0800B75A - movs r0, 0x80 - lsls r0, 11 - orrs r1, r0 -_0800B75A: - adds r0, r1, 0 - pop {r4-r7} - pop {r1} - bx r1 - thumb_func_end sub_800B638 - thumb_func_start sub_800B764 sub_800B764: @ 800B764 push {lr} diff --git a/asm/mystery_event_menu.s b/asm/mystery_event_menu.s index 4915c199c..cc2744ba0 100644 --- a/asm/mystery_event_menu.s +++ b/asm/mystery_event_menu.s @@ -333,7 +333,7 @@ _08178C42: adds r1, r4, r0 b _08178D1E _08178C54: - bl sub_800B320 + bl IsLinkConnectionEstablished lsls r0, 24 cmp r0, 0 beq _08178D00 diff --git a/asm/pokenav.s b/asm/pokenav.s index 98a0a4951..929e07295 100755 --- a/asm/pokenav.s +++ b/asm/pokenav.s @@ -28157,7 +28157,7 @@ sub_81D4EE4: @ 81D4EE4 lsrs r0, 24 cmp r0, 0x2 bhi _081D4F08 - bl sub_800B33C + bl HasLinkErrorOccurred lsls r0, 24 cmp r0, 0 beq _081D4F08 @@ -28248,7 +28248,7 @@ _081D4FA0: movs r0, 0x5 b _081D500E _081D4FB6: - bl sub_800B320 + bl IsLinkConnectionEstablished lsls r0, 24 cmp r0, 0 beq _081D500C @@ -28665,7 +28665,7 @@ _081D5360: strb r0, [r4, 0x8] b _081D548A _081D536A: - bl sub_800B33C + bl HasLinkErrorOccurred lsls r0, 24 cmp r0, 0 beq _081D537A diff --git a/asm/trade.s b/asm/trade.s index 25017bcfd..a5e7cf4ce 100644 --- a/asm/trade.s +++ b/asm/trade.s @@ -12856,7 +12856,7 @@ _0807E4AE: _0807E4B2: strb r0, [r1] _0807E4B4: - bl sub_800B33C + bl HasLinkErrorOccurred lsls r0, 24 cmp r0, 0 bne _0807E4C2 @@ -14099,7 +14099,7 @@ _0807F028: ldr r0, =c2_080543C4 bl SetMainCallback2 _0807F03A: - bl sub_800B33C + bl HasLinkErrorOccurred lsls r0, 24 cmp r0, 0 bne _0807F048 @@ -14883,7 +14883,7 @@ _0807F6F8: ldr r0, =c2_080543C4 bl SetMainCallback2 _0807F70C: - bl sub_800B33C + bl HasLinkErrorOccurred lsls r0, 24 cmp r0, 0 bne _0807F71A diff --git a/include/link.h b/include/link.h index 38c2f1388..88f638d78 100644 --- a/include/link.h +++ b/include/link.h @@ -153,7 +153,6 @@ extern const struct BlockRequest sBlockRequestLookupTable[5]; extern struct Link gLink; extern u16 gRecvCmds[MAX_RFU_PLAYERS][CMD_LENGTH]; -extern u8 gBlockSendBuffer[BLOCK_BUFFER_SIZE]; // gBlockSendBuffer extern u8 gBlockSendBuffer[BLOCK_BUFFER_SIZE]; extern u16 gLinkType; extern u32 gLinkStatus; @@ -194,7 +193,7 @@ bool8 IsLinkConnectionEstablished(void); void SetSuppressLinkErrorMessage(bool8); bool8 HasLinkErrorOccurred(void); void ResetSerial(void); -u32 LinkMain1(u8 *, u16 *, u16[CMD_LENGTH][MAX_LINK_PLAYERS]); +u32 LinkMain1(u8 *shouldAdvanceLinkState, u16 *sendCmd, u16 (*recvCmds)[CMD_LENGTH]); void LinkVSync(void); void Timer3Intr(void); void SerialCB(void); @@ -213,9 +212,7 @@ void SetLinkDebugValues(u32 seed, u32 flags); void sub_800A418(void); void SetSuppressLinkErrorMessage(bool8 flag); void sub_800B524(struct LinkPlayer *linkPlayer); -u8 sub_800B2E8(void); -u8 sub_800B320(void); -u8 sub_800B33C(void); +u8 GetSioMultiSI(void); void sub_800B9B8(void); extern u16 gUnknown_03003020[6]; diff --git a/src/link.c b/src/link.c index 3da48a951..3adfc1b76 100644 --- a/src/link.c +++ b/src/link.c @@ -78,7 +78,7 @@ bool8 gUnknown_03003078[MAX_LINK_PLAYERS]; u8 gUnknown_0300307C[MAX_LINK_PLAYERS]; u32 gFiller_03003080; u16 gLinkHeldKeys; -u16 gRecvCmds[MAX_RFU_PLAYERS][8]; +u16 gRecvCmds[MAX_RFU_PLAYERS][CMD_LENGTH]; u32 gLinkStatus; bool8 gUnknown_030030E4; bool8 gUnknown_030030E8; @@ -88,12 +88,12 @@ u16 gUnknown_030030F4; u8 gSuppressLinkErrorMessage; bool8 gWirelessCommType; bool8 gSavedLinkPlayerCount; -u16 gSendCmd[8]; +u16 gSendCmd[CMD_LENGTH]; u8 gSavedMultiplayerId; bool8 gReceivedRemoteLinkPlayers; struct LinkTestBGInfo gLinkTestBGInfo; void (*gLinkCallback)(void); -bool8 gShouldAdvanceLinkState; +u8 gShouldAdvanceLinkState; u16 gLinkTestBlockChecksums[MAX_LINK_PLAYERS]; u8 gBlockRequestType; u32 gFiller_03003154; @@ -149,14 +149,19 @@ static void sub_800AD5C(void); static void sub_800AD88(void); static void sub_800AE30(void); static void sub_800AE5C(void); -static void sub_800AEB4(void); -static void sub_800B1A0(void); -static bool8 sub_800B2F8(void); -u32 sub_800B638(bool8 *shouldAdvanceLinkState, u16 *sendCmd, u16 (*recvCmds)[8]); +static void CheckErrorStatus(void); +static void CB2_PrintErrorMessage(void); +static bool8 IsSioMultiMaster(void); +u32 LinkMain1(u8 *shouldAdvanceLinkState, u16 *sendCmd, u16 (*recvCmds)[CMD_LENGTH]); static void sub_800B4A4(void); void DisableSerial(void); void EnableSerial(void); +void sub_800B764(void); +void sub_800B790(void); +void sub_800B7C0(u16 *queue); +void sub_800B8A8(u16 (*queue)[CMD_LENGTH]); + // .rodata ALIGNED(4) const u16 gWirelessLinkDisplayPal[] = INCBIN_U16("graphics/interface/wireless_link_display.gbapal"); @@ -334,7 +339,7 @@ void Task_TriggerHandshake(u8 taskId) { if (++ gTasks[taskId].data[0] == 5) { - gShouldAdvanceLinkState = TRUE; + gShouldAdvanceLinkState = 1; DestroyTask(taskId); } } @@ -424,7 +429,7 @@ void LinkTestProcessKeyInput(void) { if (gMain.newKeys & A_BUTTON) { - gShouldAdvanceLinkState = TRUE; + gShouldAdvanceLinkState = 1; } if (gMain.heldKeys & B_BUTTON) { @@ -482,7 +487,7 @@ u16 LinkMain2(const u16 *heldKeys) { gLinkCallback(); } - sub_800AEB4(); + CheckErrorStatus(); } return gLinkStatus; } @@ -1134,7 +1139,7 @@ void sub_800A620(void) { if ((gLinkStatus & LINK_STAT_MASTER) && EXTRACT_PLAYER_COUNT(gLinkStatus) > 1) { - gShouldAdvanceLinkState = TRUE; + gShouldAdvanceLinkState = 1; } } @@ -1261,10 +1266,10 @@ static void Task_PrintTestData(u8 taskId) LinkTest_prnthex(GetBlockReceivedStatus(), 15, 5, 2); LinkTest_prnthex(gLinkDebugSeed, 2, 12, 8); LinkTest_prnthex(gLinkDebugFlags, 2, 13, 8); - LinkTest_prnthex(sub_800B2E8(), 25, 5, 1); - LinkTest_prnthex(sub_800B2F8(), 25, 6, 1); - LinkTest_prnthex(sub_800B320(), 25, 7, 1); - LinkTest_prnthex(sub_800B33C(), 25, 8, 1); + LinkTest_prnthex(GetSioMultiSI(), 25, 5, 1); + LinkTest_prnthex(IsSioMultiMaster(), 25, 6, 1); + LinkTest_prnthex(IsLinkConnectionEstablished(), 25, 7, 1); + LinkTest_prnthex(HasLinkErrorOccurred(), 25, 8, 1); for (i = 0; i < MAX_LINK_PLAYERS; i++) { LinkTest_prnthex(gLinkTestBlockChecksums[i], 10, 4 + i, 4); @@ -1588,7 +1593,7 @@ static void sub_800AE5C(void) } } -static void sub_800AEB4(void) +static void CheckErrorStatus(void) { if (gLinkOpen && EXTRACT_LINK_ERRORS(gLinkStatus)) { @@ -1660,7 +1665,7 @@ void CB2_LinkError(void) AnimateSprites(); BuildOamBuffer(); UpdatePaletteFade(); - SetMainCallback2(sub_800B1A0); + SetMainCallback2(CB2_PrintErrorMessage); } } @@ -1693,7 +1698,7 @@ static void sub_800B138(void) CopyWindowToVram(2, 3); } -static void sub_800B1A0(void) +static void CB2_PrintErrorMessage(void) { switch (gMain.state) { @@ -1762,17 +1767,17 @@ static void sub_800B1A0(void) } } -bool8 sub_800B2E8(void) +bool8 GetSioMultiSI(void) { return (REG_SIOCNT & 0x04) != 0; } -static bool8 sub_800B2F8(void) +static bool8 IsSioMultiMaster(void) { return (REG_SIOCNT & 0x8) && !(REG_SIOCNT & 0x04); } -bool8 sub_800B320(void) +bool8 IsLinkConnectionEstablished(void) { return EXTRACT_CONN_ESTABLISHED(gLinkStatus); } @@ -1782,7 +1787,7 @@ void SetSuppressLinkErrorMessage(bool8 flag) gSuppressLinkErrorMessage = flag; } -bool8 sub_800B33C(void) +bool8 HasLinkErrorOccurred(void) { return gLinkErrorOccurred; } @@ -1821,7 +1826,7 @@ bool8 HandleLinkConnection(void) if (gWirelessCommType == 0) { - gLinkStatus = sub_800B638(&gShouldAdvanceLinkState, gSendCmd, gRecvCmds); + gLinkStatus = LinkMain1(&gShouldAdvanceLinkState, gSendCmd, gRecvCmds); LinkMain2(&gMain.heldKeys); if ((gLinkStatus & LINK_STAT_RECEIVED_NOTHING) && sub_808766C() == TRUE) { @@ -1930,3 +1935,99 @@ void ResetSerial(void) EnableSerial(); DisableSerial(); } + +u32 LinkMain1(u8 *shouldAdvanceLinkState, u16 *sendCmd, u16 (*recvCmds)[CMD_LENGTH]) +{ + u32 retVal; + u32 retVal2; + + switch (gLink.state) + { + case LINK_STATE_START0: + DisableSerial(); + gLink.state = 1; + break; + case LINK_STATE_START1: + if (*shouldAdvanceLinkState == 1) + { + EnableSerial(); + gLink.state = 2; + } + break; + case LINK_STATE_HANDSHAKE: + switch (*shouldAdvanceLinkState) + { + default: + sub_800B764(); + break; + case 1: + if (gLink.isMaster == 8 && gLink.playerCount > 1) + { + gLink.handshakeAsMaster = TRUE; + } + break; + case 2: + gLink.state = LINK_STATE_START0; + REG_SIOMLT_SEND = 0; + break; + } + break; + case LINK_STATE_INIT_TIMER: + sub_800B790(); + gLink.state = LINK_STATE_CONN_ESTABLISHED; + // fallthrough + case LINK_STATE_CONN_ESTABLISHED: + sub_800B7C0(sendCmd); + sub_800B8A8(recvCmds); + break; + } + *shouldAdvanceLinkState = 0; + retVal = gLink.localId; + retVal |= (gLink.playerCount << 2); + if (gLink.isMaster == 8) + { + retVal |= 0x20; + } + { + u32 receivedNothing = gLink.receivedNothing << 8; + u32 link_field_F = gLink.link_field_F << 9; + u32 hardwareError = gLink.hardwareError << 12; + u32 badChecksum = gLink.badChecksum << 13; + u32 queueFull = gLink.queueFull << 14; + u32 val; + + if (gLink.state == LINK_STATE_CONN_ESTABLISHED) + { + val = 0x40; + val |= receivedNothing; + val |= retVal; + val |= link_field_F; + val |= hardwareError; + val |= badChecksum; + val |= queueFull; + } + else + { + val = retVal; + val |= receivedNothing; + val |= link_field_F; + val |= hardwareError; + val |= badChecksum; + val |= queueFull; + } + + retVal = val; + } + + if (gLink.lag == LAG_MASTER) + retVal |= 0x10000; + + if (gLink.localId > 3) + retVal |= 0x20000; + + retVal2 = retVal; + if (gLink.lag == LAG_SLAVE) + retVal2 |= 0x40000; + + return retVal2; +}