From f1216076d7cb6a383682f423c9b5c14e152e484b Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Thu, 7 Sep 2017 00:51:59 -0600 Subject: [PATCH 01/11] Begin librfu decompilation --- asm/crt0.s | 2 +- asm/librfu.s | 454 ++++++------------------------------------- include/gba/io_reg.h | 6 + include/gba/macro.h | 10 + ld_script.txt | 1 + src/librfu.c | 217 +++++++++++++++++++++ sym_common.txt | 2 +- 7 files changed, 292 insertions(+), 400 deletions(-) create mode 100644 src/librfu.c diff --git a/asm/crt0.s b/asm/crt0.s index 14675fe4c..3a3034a37 100644 --- a/asm/crt0.s +++ b/asm/crt0.s @@ -115,7 +115,7 @@ IntrMain: @ 8000248 IntrMain_FoundIntr: strh r0, [r3, OFFSET_REG_IF - 0x200] bic r2, r2, r0 - ldr r0, =gUnknown_03007868 + ldr r0, =gRfuState ldr r0, [r0] ldrb r0, [r0, 0xA] mov r1, 0x8 diff --git a/asm/librfu.s b/asm/librfu.s index d8ca7538c..2f0359f37 100644 --- a/asm/librfu.s +++ b/asm/librfu.s @@ -4,348 +4,6 @@ .text - thumb_func_start STWI_init_all -STWI_init_all: @ 82E2A18 - push {r4,lr} - adds r3, r0, 0 - lsls r2, 24 - lsrs r2, 24 - cmp r2, 0x1 - bne _082E2A54 - adds r2, r3, 0 - adds r2, 0xE8 - str r2, [r1] - ldr r1, _082E2A40 - ldr r0, _082E2A44 - str r0, [r1] - str r2, [r1, 0x4] - ldr r0, _082E2A48 - str r0, [r1, 0x8] - ldr r0, [r1, 0x8] - ldr r1, _082E2A4C - ldr r2, _082E2A50 - adds r0, r3, r2 - b _082E2A5E - .align 2, 0 -_082E2A40: .4byte 0x040000d4 -_082E2A44: .4byte IntrSIO32 -_082E2A48: .4byte 0x800004b0 -_082E2A4C: .4byte gUnknown_03007868 -_082E2A50: .4byte 0x00000a48 -_082E2A54: - ldr r0, _082E2AD8 - str r0, [r1] - ldr r1, _082E2ADC - adds r0, r3, 0 - adds r0, 0xE8 -_082E2A5E: - str r0, [r1] - adds r2, r1, 0 - ldr r1, [r2] - str r3, [r1, 0x28] - adds r0, r3, 0 - adds r0, 0x74 - str r0, [r1, 0x24] - ldrb r0, [r1, 0x14] - movs r4, 0 - movs r0, 0x1 - strb r0, [r1, 0x14] - ldr r0, [r2] - str r4, [r0] - strb r4, [r0, 0x4] - ldr r0, [r2] - strb r4, [r0, 0x5] - ldr r0, [r2] - strb r4, [r0, 0x7] - ldr r0, [r2] - strb r4, [r0, 0x8] - ldr r0, [r2] - strb r4, [r0, 0x9] - ldr r0, [r2] - str r4, [r0, 0xC] - ldrb r1, [r0, 0x10] - strb r4, [r0, 0x10] - ldr r0, [r2] - ldrh r1, [r0, 0x12] - movs r1, 0 - strh r4, [r0, 0x12] - strb r1, [r0, 0x15] - ldr r0, [r2] - adds r0, 0x2C - ldrb r2, [r0] - strb r1, [r0] - ldr r1, _082E2AE0 - movs r2, 0x80 - lsls r2, 1 - adds r0, r2, 0 - strh r0, [r1] - subs r1, 0xC - ldr r2, _082E2AE4 - adds r0, r2, 0 - strh r0, [r1] - bl STWI_init_Callback_M - bl STWI_init_Callback_S - ldr r3, _082E2AE8 - ldrh r2, [r3] - strh r4, [r3] - ldr r4, _082E2AEC - ldrh r0, [r4] - movs r1, 0x80 - orrs r0, r1 - strh r0, [r4] - strh r2, [r3] - pop {r4} - pop {r0} - bx r0 - .align 2, 0 -_082E2AD8: .4byte IntrSIO32 -_082E2ADC: .4byte gUnknown_03007868 -_082E2AE0: .4byte 0x04000134 -_082E2AE4: .4byte 0x00005003 -_082E2AE8: .4byte 0x04000208 -_082E2AEC: .4byte 0x04000200 - thumb_func_end STWI_init_all - - thumb_func_start STWI_init_timer -STWI_init_timer: @ 82E2AF0 - push {r4,r5,lr} - ldr r2, _082E2B1C - str r2, [r0] - ldr r5, _082E2B20 - ldr r0, [r5] - movs r4, 0 - strb r1, [r0, 0xA] - ldr r3, _082E2B24 - ldrh r2, [r3] - strh r4, [r3] - ldr r4, _082E2B28 - ldr r1, [r5] - movs r0, 0x8 - ldrb r1, [r1, 0xA] - lsls r0, r1 - ldrh r1, [r4] - orrs r0, r1 - strh r0, [r4] - strh r2, [r3] - pop {r4,r5} - pop {r0} - bx r0 - .align 2, 0 -_082E2B1C: .4byte STWI_intr_timer -_082E2B20: .4byte gUnknown_03007868 -_082E2B24: .4byte 0x04000208 -_082E2B28: .4byte 0x04000200 - thumb_func_end STWI_init_timer - - thumb_func_start AgbRFU_SoftReset -AgbRFU_SoftReset: @ 82E2B2C - push {r4,r5,lr} - ldr r5, _082E2BC4 - movs r1, 0x80 - lsls r1, 8 - adds r0, r1, 0 - strh r0, [r5] - ldr r2, _082E2BC8 - adds r0, r2, 0 - strh r0, [r5] - ldr r1, _082E2BCC - ldr r0, [r1] - ldrb r0, [r0, 0xA] - lsls r0, 2 - ldr r3, _082E2BD0 - adds r2, r0, r3 - ldr r4, _082E2BD4 - adds r3, r0, r4 - movs r0, 0 - strh r0, [r3] - strh r0, [r2] - movs r0, 0x83 - strh r0, [r3] - ldrh r0, [r2] - adds r4, r1, 0 - cmp r0, 0x11 - bhi _082E2B6C - ldr r0, _082E2BD8 - adds r1, r0, 0 -_082E2B64: - strh r1, [r5] - ldrh r0, [r2] - cmp r0, 0x11 - bls _082E2B64 -_082E2B6C: - movs r0, 0x3 - strh r0, [r3] - ldr r1, _082E2BC4 - ldr r2, _082E2BC8 - adds r0, r2, 0 - strh r0, [r1] - subs r1, 0xC - ldr r3, _082E2BDC - adds r0, r3, 0 - strh r0, [r1] - ldr r0, [r4] - movs r2, 0 - str r2, [r0] - strb r2, [r0, 0x4] - ldr r0, [r4] - strb r2, [r0, 0x5] - ldr r0, [r4] - strb r2, [r0, 0x6] - ldr r0, [r4] - strb r2, [r0, 0x7] - ldr r0, [r4] - strb r2, [r0, 0x8] - ldr r0, [r4] - strb r2, [r0, 0x9] - ldr r0, [r4] - str r2, [r0, 0xC] - ldrb r1, [r0, 0x10] - strb r2, [r0, 0x10] - ldr r1, [r4] - ldrh r0, [r1, 0x12] - movs r3, 0 - strh r2, [r1, 0x12] - ldrb r0, [r1, 0x14] - movs r0, 0x1 - strb r0, [r1, 0x14] - ldr r0, [r4] - strb r3, [r0, 0x15] - ldr r0, [r4] - adds r0, 0x2C - ldrb r1, [r0] - strb r3, [r0] - pop {r4,r5} - pop {r0} - bx r0 - .align 2, 0 -_082E2BC4: .4byte 0x04000134 -_082E2BC8: .4byte 0x000080a0 -_082E2BCC: .4byte gUnknown_03007868 -_082E2BD0: .4byte 0x04000100 -_082E2BD4: .4byte 0x04000102 -_082E2BD8: .4byte 0x000080a2 -_082E2BDC: .4byte 0x00005003 - thumb_func_end AgbRFU_SoftReset - - thumb_func_start STWI_set_MS_mode -STWI_set_MS_mode: @ 82E2BE0 - lsls r0, 24 - lsrs r0, 24 - ldr r1, _082E2BF0 - ldr r1, [r1] - ldrb r2, [r1, 0x14] - strb r0, [r1, 0x14] - bx lr - .align 2, 0 -_082E2BF0: .4byte gUnknown_03007868 - thumb_func_end STWI_set_MS_mode - - thumb_func_start STWI_read_status -STWI_read_status: @ 82E2BF4 - push {lr} - lsls r0, 24 - lsrs r0, 24 - adds r1, r0, 0 - cmp r0, 0x1 - beq _082E2C20 - cmp r0, 0x1 - bgt _082E2C0A - cmp r0, 0 - beq _082E2C14 - b _082E2C48 -_082E2C0A: - cmp r1, 0x2 - beq _082E2C2C - cmp r1, 0x3 - beq _082E2C3C - b _082E2C48 -_082E2C14: - ldr r0, _082E2C1C - ldr r0, [r0] - ldrh r0, [r0, 0x12] - b _082E2C4A - .align 2, 0 -_082E2C1C: .4byte gUnknown_03007868 -_082E2C20: - ldr r0, _082E2C28 - ldr r0, [r0] - ldrb r0, [r0, 0x14] - b _082E2C4A - .align 2, 0 -_082E2C28: .4byte gUnknown_03007868 -_082E2C2C: - ldr r0, _082E2C38 - ldr r0, [r0] - ldr r0, [r0] - lsls r0, 16 - lsrs r0, 16 - b _082E2C4A - .align 2, 0 -_082E2C38: .4byte gUnknown_03007868 -_082E2C3C: - ldr r0, _082E2C44 - ldr r0, [r0] - ldrb r0, [r0, 0x6] - b _082E2C4A - .align 2, 0 -_082E2C44: .4byte gUnknown_03007868 -_082E2C48: - ldr r0, _082E2C50 -_082E2C4A: - pop {r1} - bx r1 - .align 2, 0 -_082E2C50: .4byte 0x0000ffff - thumb_func_end STWI_read_status - - thumb_func_start STWI_init_Callback_M -STWI_init_Callback_M: @ 82E2C54 - push {lr} - movs r0, 0 - bl STWI_set_Callback_M - pop {r0} - bx r0 - thumb_func_end STWI_init_Callback_M - - thumb_func_start STWI_init_Callback_S -STWI_init_Callback_S: @ 82E2C60 - push {lr} - movs r0, 0 - bl STWI_set_Callback_S - pop {r0} - bx r0 - thumb_func_end STWI_init_Callback_S - - thumb_func_start STWI_set_Callback_M -STWI_set_Callback_M: @ 82E2C6C - ldr r1, _082E2C74 - ldr r1, [r1] - str r0, [r1, 0x18] - bx lr - .align 2, 0 -_082E2C74: .4byte gUnknown_03007868 - thumb_func_end STWI_set_Callback_M - - thumb_func_start STWI_set_Callback_S -STWI_set_Callback_S: @ 82E2C78 - ldr r1, _082E2C80 - ldr r1, [r1] - str r0, [r1, 0x1C] - bx lr - .align 2, 0 -_082E2C80: .4byte gUnknown_03007868 - thumb_func_end STWI_set_Callback_S - - thumb_func_start STWI_set_Callback_ID -STWI_set_Callback_ID: @ 82E2C84 - ldr r1, _082E2C8C - ldr r1, [r1] - str r0, [r1, 0x20] - bx lr - .align 2, 0 -_082E2C8C: .4byte gUnknown_03007868 - thumb_func_end STWI_set_Callback_ID - thumb_func_start STWI_poll_CommandEnd STWI_poll_CommandEnd: @ 82E2C90 push {lr} @@ -368,7 +26,7 @@ _082E2CAA: pop {r1} bx r1 .align 2, 0 -_082E2CB4: .4byte gUnknown_03007868 +_082E2CB4: .4byte gRfuState thumb_func_end STWI_poll_CommandEnd thumb_func_start STWI_send_ResetREQ @@ -388,7 +46,7 @@ _082E2CD2: pop {r0} bx r0 .align 2, 0 -_082E2CD8: .4byte gUnknown_03007868 +_082E2CD8: .4byte gRfuState thumb_func_end STWI_send_ResetREQ thumb_func_start STWI_send_LinkStatusREQ @@ -408,7 +66,7 @@ _082E2CF6: pop {r0} bx r0 .align 2, 0 -_082E2CFC: .4byte gUnknown_03007868 +_082E2CFC: .4byte gRfuState thumb_func_end STWI_send_LinkStatusREQ thumb_func_start STWI_send_VersionStatusREQ @@ -428,7 +86,7 @@ _082E2D1A: pop {r0} bx r0 .align 2, 0 -_082E2D20: .4byte gUnknown_03007868 +_082E2D20: .4byte gRfuState thumb_func_end STWI_send_VersionStatusREQ thumb_func_start STWI_send_SystemStatusREQ @@ -448,7 +106,7 @@ _082E2D3E: pop {r0} bx r0 .align 2, 0 -_082E2D44: .4byte gUnknown_03007868 +_082E2D44: .4byte gRfuState thumb_func_end STWI_send_SystemStatusREQ thumb_func_start STWI_send_SlotStatusREQ @@ -468,7 +126,7 @@ _082E2D62: pop {r0} bx r0 .align 2, 0 -_082E2D68: .4byte gUnknown_03007868 +_082E2D68: .4byte gRfuState thumb_func_end STWI_send_SlotStatusREQ thumb_func_start STWI_send_ConfigStatusREQ @@ -488,7 +146,7 @@ _082E2D86: pop {r0} bx r0 .align 2, 0 -_082E2D8C: .4byte gUnknown_03007868 +_082E2D8C: .4byte gRfuState thumb_func_end STWI_send_ConfigStatusREQ thumb_func_start STWI_send_GameConfigREQ @@ -536,7 +194,7 @@ _082E2DDC: pop {r0} bx r0 .align 2, 0 -_082E2DE4: .4byte gUnknown_03007868 +_082E2DE4: .4byte gRfuState thumb_func_end STWI_send_GameConfigREQ thumb_func_start STWI_send_SystemConfigREQ @@ -571,7 +229,7 @@ _082E2E1E: pop {r0} bx r0 .align 2, 0 -_082E2E24: .4byte gUnknown_03007868 +_082E2E24: .4byte gRfuState thumb_func_end STWI_send_SystemConfigREQ thumb_func_start STWI_send_SC_StartREQ @@ -591,7 +249,7 @@ _082E2E42: pop {r0} bx r0 .align 2, 0 -_082E2E48: .4byte gUnknown_03007868 +_082E2E48: .4byte gRfuState thumb_func_end STWI_send_SC_StartREQ thumb_func_start STWI_send_SC_PollingREQ @@ -611,7 +269,7 @@ _082E2E66: pop {r0} bx r0 .align 2, 0 -_082E2E6C: .4byte gUnknown_03007868 +_082E2E6C: .4byte gRfuState thumb_func_end STWI_send_SC_PollingREQ thumb_func_start STWI_send_SC_EndREQ @@ -631,7 +289,7 @@ _082E2E8A: pop {r0} bx r0 .align 2, 0 -_082E2E90: .4byte gUnknown_03007868 +_082E2E90: .4byte gRfuState thumb_func_end STWI_send_SC_EndREQ thumb_func_start STWI_send_SP_StartREQ @@ -651,7 +309,7 @@ _082E2EAE: pop {r0} bx r0 .align 2, 0 -_082E2EB4: .4byte gUnknown_03007868 +_082E2EB4: .4byte gRfuState thumb_func_end STWI_send_SP_StartREQ thumb_func_start STWI_send_SP_PollingREQ @@ -671,7 +329,7 @@ _082E2ED2: pop {r0} bx r0 .align 2, 0 -_082E2ED8: .4byte gUnknown_03007868 +_082E2ED8: .4byte gRfuState thumb_func_end STWI_send_SP_PollingREQ thumb_func_start STWI_send_SP_EndREQ @@ -691,7 +349,7 @@ _082E2EF6: pop {r0} bx r0 .align 2, 0 -_082E2EFC: .4byte gUnknown_03007868 +_082E2EFC: .4byte gRfuState thumb_func_end STWI_send_SP_EndREQ thumb_func_start STWI_send_CP_StartREQ @@ -717,7 +375,7 @@ _082E2F24: pop {r0} bx r0 .align 2, 0 -_082E2F2C: .4byte gUnknown_03007868 +_082E2F2C: .4byte gRfuState thumb_func_end STWI_send_CP_StartREQ thumb_func_start STWI_send_CP_PollingREQ @@ -737,7 +395,7 @@ _082E2F4A: pop {r0} bx r0 .align 2, 0 -_082E2F50: .4byte gUnknown_03007868 +_082E2F50: .4byte gRfuState thumb_func_end STWI_send_CP_PollingREQ thumb_func_start STWI_send_CP_EndREQ @@ -757,7 +415,7 @@ _082E2F6E: pop {r0} bx r0 .align 2, 0 -_082E2F74: .4byte gUnknown_03007868 +_082E2F74: .4byte gRfuState thumb_func_end STWI_send_CP_EndREQ thumb_func_start STWI_send_DataTxREQ @@ -796,7 +454,7 @@ _082E2FB6: pop {r0} bx r0 .align 2, 0 -_082E2FBC: .4byte gUnknown_03007868 +_082E2FBC: .4byte gRfuState thumb_func_end STWI_send_DataTxREQ thumb_func_start STWI_send_DataTxAndChangeREQ @@ -835,7 +493,7 @@ _082E2FFE: pop {r0} bx r0 .align 2, 0 -_082E3004: .4byte gUnknown_03007868 +_082E3004: .4byte gRfuState thumb_func_end STWI_send_DataTxAndChangeREQ thumb_func_start STWI_send_DataRxREQ @@ -855,7 +513,7 @@ _082E3022: pop {r0} bx r0 .align 2, 0 -_082E3028: .4byte gUnknown_03007868 +_082E3028: .4byte gRfuState thumb_func_end STWI_send_DataRxREQ thumb_func_start STWI_send_MS_ChangeREQ @@ -875,7 +533,7 @@ _082E3046: pop {r0} bx r0 .align 2, 0 -_082E304C: .4byte gUnknown_03007868 +_082E304C: .4byte gRfuState thumb_func_end STWI_send_MS_ChangeREQ thumb_func_start STWI_send_DataReadyAndChangeREQ @@ -897,7 +555,7 @@ STWI_send_DataReadyAndChangeREQ: @ 82E3050 strb r3, [r0, 0x4] b _082E3092 .align 2, 0 -_082E3074: .4byte gUnknown_03007868 +_082E3074: .4byte gRfuState _082E3078: ldr r2, _082E309C ldr r1, [r2] @@ -919,7 +577,7 @@ _082E3096: pop {r0} bx r0 .align 2, 0 -_082E309C: .4byte gUnknown_03007868 +_082E309C: .4byte gRfuState thumb_func_end STWI_send_DataReadyAndChangeREQ thumb_func_start STWI_send_DisconnectedAndChangeREQ @@ -954,7 +612,7 @@ _082E30D6: pop {r0} bx r0 .align 2, 0 -_082E30DC: .4byte gUnknown_03007868 +_082E30DC: .4byte gRfuState thumb_func_end STWI_send_DisconnectedAndChangeREQ thumb_func_start STWI_send_ResumeRetransmitAndChangeREQ @@ -974,7 +632,7 @@ _082E30FA: pop {r0} bx r0 .align 2, 0 -_082E3100: .4byte gUnknown_03007868 +_082E3100: .4byte gRfuState thumb_func_end STWI_send_ResumeRetransmitAndChangeREQ thumb_func_start STWI_send_DisconnectREQ @@ -1000,7 +658,7 @@ _082E3128: pop {r0} bx r0 .align 2, 0 -_082E3130: .4byte gUnknown_03007868 +_082E3130: .4byte gRfuState thumb_func_end STWI_send_DisconnectREQ thumb_func_start STWI_send_TestModeREQ @@ -1030,7 +688,7 @@ _082E3160: pop {r0} bx r0 .align 2, 0 -_082E3168: .4byte gUnknown_03007868 +_082E3168: .4byte gRfuState thumb_func_end STWI_send_TestModeREQ thumb_func_start STWI_send_CPR_StartREQ @@ -1063,7 +721,7 @@ _082E319E: pop {r0} bx r0 .align 2, 0 -_082E31A4: .4byte gUnknown_03007868 +_082E31A4: .4byte gRfuState thumb_func_end STWI_send_CPR_StartREQ thumb_func_start STWI_send_CPR_PollingREQ @@ -1083,7 +741,7 @@ _082E31C2: pop {r0} bx r0 .align 2, 0 -_082E31C8: .4byte gUnknown_03007868 +_082E31C8: .4byte gRfuState thumb_func_end STWI_send_CPR_PollingREQ thumb_func_start STWI_send_CPR_EndREQ @@ -1103,7 +761,7 @@ _082E31E6: pop {r0} bx r0 .align 2, 0 -_082E31EC: .4byte gUnknown_03007868 +_082E31EC: .4byte gRfuState thumb_func_end STWI_send_CPR_EndREQ thumb_func_start STWI_send_StopModeREQ @@ -1123,7 +781,7 @@ _082E320A: pop {r0} bx r0 .align 2, 0 -_082E3210: .4byte gUnknown_03007868 +_082E3210: .4byte gRfuState thumb_func_end STWI_send_StopModeREQ thumb_func_start STWI_intr_timer @@ -1140,7 +798,7 @@ STWI_intr_timer: @ 82E3214 beq _082E3248 b _082E3270 .align 2, 0 -_082E322C: .4byte gUnknown_03007868 +_082E322C: .4byte gRfuState _082E3230: cmp r0, 0x3 beq _082E3252 @@ -1203,7 +861,7 @@ STWI_set_timer: @ 82E3278 beq _082E32BE b _082E32FC .align 2, 0 -_082E32A8: .4byte gUnknown_03007868 +_082E32A8: .4byte gRfuState _082E32AC: .4byte 0x04000100 _082E32B0: .4byte 0x04000208 _082E32B4: @@ -1287,7 +945,7 @@ STWI_stop_timer: @ 82E3324 strh r1, [r0] bx lr .align 2, 0 -_082E3344: .4byte gUnknown_03007868 +_082E3344: .4byte gRfuState _082E3348: .4byte 0x04000100 _082E334C: .4byte 0x04000102 thumb_func_end STWI_stop_timer @@ -1316,7 +974,7 @@ STWI_init: @ 82E3350 b _082E33CE .align 2, 0 _082E337C: .4byte 0x04000208 -_082E3380: .4byte gUnknown_03007868 +_082E3380: .4byte gRfuState _082E3384: ldr r4, _082E33B0 ldr r2, [r4] @@ -1340,7 +998,7 @@ _082E3384: bl _call_via_r2 b _082E33CE .align 2, 0 -_082E33B0: .4byte gUnknown_03007868 +_082E33B0: .4byte gRfuState _082E33B4: ldrb r0, [r2, 0x14] cmp r0, 0 @@ -1450,7 +1108,7 @@ STWI_start_Command: @ 82E3428 pop {r1} bx r1 .align 2, 0 -_082E3480: .4byte gUnknown_03007868 +_082E3480: .4byte gRfuState _082E3484: .4byte 0x99660000 _082E3488: .4byte 0x04000120 _082E348C: .4byte 0x04000208 @@ -1473,7 +1131,7 @@ STWI_restart_Command: @ 82E349C bl STWI_start_Command b _082E3512 .align 2, 0 -_082E34B4: .4byte gUnknown_03007868 +_082E34B4: .4byte gRfuState _082E34B8: ldrb r0, [r2, 0x6] cmp r0, 0x27 @@ -1553,7 +1211,7 @@ STWI_reset_ClockCounter: @ 82E351C movs r0, 0 bx lr .align 2, 0 -_082E3548: .4byte gUnknown_03007868 +_082E3548: .4byte gRfuState _082E354C: .4byte 0x04000120 _082E3550: .4byte 0x00005003 thumb_func_end STWI_reset_ClockCounter @@ -1587,7 +1245,7 @@ _082E35A8: ldmdb r11, {r11,sp,lr} bx lr .align 2, 0 -_082E35B4: .4byte gUnknown_03007868 +_082E35B4: .4byte gRfuState arm_func_end IntrSIO32 arm_func_start sio32intr_clock_master @@ -1761,7 +1419,7 @@ _082E3800: bl sub_82E3EA8 b _082E3840 .align 2, 0 -_082E382C: .4byte gUnknown_03007868 +_082E382C: .4byte gRfuState _082E3830: add r3, r5, 0x3 strh r3, [r4] @@ -2026,7 +1684,7 @@ _082E3BE4: bhi _082E3BE4 b _082E3C20 .align 2, 0 -_082E3BF4: .4byte gUnknown_03007868 +_082E3BF4: .4byte gRfuState _082E3BF8: .4byte 0x996601ee _082E3BFC: mov r2, 0xFF00 @@ -2086,7 +1744,7 @@ _082E3CA4: ldmdb r11, {r11,sp,lr} bx lr .align 2, 0 -_082E3CB8: .4byte gUnknown_03007868 +_082E3CB8: .4byte gRfuState arm_func_end handshake_wait arm_func_start STWI_set_timer_in_RAM @@ -2142,7 +1800,7 @@ _082E3D5C: mov r3, 0x3 b _082E3D8C .align 2, 0 -_082E3D74: .4byte gUnknown_03007868 +_082E3D74: .4byte gRfuState _082E3D78: mvn r3, 0x850 sub r3, r3, 0x2 @@ -2191,7 +1849,7 @@ STWI_stop_timer_in_RAM: @ 82E3DCC ldmdb r11, {r11,sp,lr} bx lr .align 2, 0 -_082E3E18: .4byte gUnknown_03007868 +_082E3E18: .4byte gRfuState arm_func_end STWI_stop_timer_in_RAM arm_func_start STWI_init_slave @@ -2231,7 +1889,7 @@ STWI_init_slave: @ 82E3E1C ldmdb r11, {r11,sp,lr} bx lr .align 2, 0 -_082E3EA4: .4byte gUnknown_03007868 +_082E3EA4: .4byte gRfuState arm_func_end STWI_init_slave arm_func_start sub_82E3EA8 @@ -2838,7 +2496,7 @@ rfu_REQ_stopMode: @ 82E42C4 b _082E4362 .align 2, 0 _082E42E4: .4byte 0x04000208 -_082E42E8: .4byte gUnknown_03007868 +_082E42E8: .4byte gRfuState _082E42EC: bl AgbRFU_SoftReset bl rfu_STC_clearAPIVariables @@ -2878,7 +2536,7 @@ _082E432A: b _082E4362 .align 2, 0 _082E433C: .4byte 0x00008001 -_082E4340: .4byte gUnknown_03007868 +_082E4340: .4byte gRfuState _082E4344: .4byte 0x04000100 _082E4348: .4byte 0x0105ffff _082E434C: .4byte rfu_CB_stopMode @@ -3156,7 +2814,7 @@ rfu_CB_configGameData: @ 82E4500 movs r0, 0x1 b _082E4552 .align 2, 0 -_082E4544: .4byte gUnknown_03007868 +_082E4544: .4byte gRfuState _082E4548: .4byte gUnknown_03007890 _082E454C: adds r1, r6, 0 @@ -4225,7 +3883,7 @@ _082E4D18: b _082E4D88 .align 2, 0 _082E4D20: .4byte gUnknown_03007890 -_082E4D24: .4byte gUnknown_03007868 +_082E4D24: .4byte gRfuState _082E4D28: .4byte gUnknown_03007894 _082E4D2C: .4byte gUnknown_03007898 _082E4D30: @@ -5195,7 +4853,7 @@ _082E547E: pop {r1} bx r1 .align 2, 0 -_082E5484: .4byte gUnknown_03007868 +_082E5484: .4byte gRfuState thumb_func_end rfu_getMasterSlave thumb_func_start rfu_clearAllSlot @@ -6506,7 +6164,7 @@ _082E5E12: _082E5E34: .4byte gUnknown_03007898 _082E5E38: .4byte gUnknown_03007894 _082E5E3C: .4byte gUnknown_03007890 -_082E5E40: .4byte gUnknown_03007868 +_082E5E40: .4byte gRfuState _082E5E44: ldr r0, _082E5E54 bl STWI_set_Callback_M @@ -8564,7 +8222,7 @@ _082E6D88: b _082E6DD6 .align 2, 0 _082E6DB4: .4byte 0x04000200 -_082E6DB8: .4byte gUnknown_03007868 +_082E6DB8: .4byte gRfuState _082E6DBC: .4byte sub_82E6F88 _082E6DC0: .4byte 0x04000100 _082E6DC4: @@ -8612,7 +8270,7 @@ _082E6E08: .align 2, 0 _082E6E14: .4byte 0x04000208 _082E6E18: .4byte 0x04000200 -_082E6E1C: .4byte gUnknown_03007868 +_082E6E1C: .4byte gRfuState thumb_func_end sub_82E6D6C thumb_func_start sub_82E6E20 @@ -8663,7 +8321,7 @@ sub_82E6E20: @ 82E6E20 .align 2, 0 _082E6E78: .4byte 0x04000208 _082E6E7C: .4byte 0x04000200 -_082E6E80: .4byte gUnknown_03007868 +_082E6E80: .4byte gRfuState _082E6E84: .4byte 0x04000134 _082E6E88: .4byte gUnknown_030078A0 _082E6E8C: .4byte 0x05000003 diff --git a/include/gba/io_reg.h b/include/gba/io_reg.h index 5234e5b6c..2b21086e3 100644 --- a/include/gba/io_reg.h +++ b/include/gba/io_reg.h @@ -137,6 +137,8 @@ #define REG_OFFSET_DMA3CNT_H 0xde #define REG_OFFSET_TMCNT 0x100 +#define REG_OFFSET_TMCNT_L 0x100 +#define REG_OFFSET_TMCNT_H 0x102 #define REG_OFFSET_TM0CNT 0x100 #define REG_OFFSET_TM0CNT_L 0x100 #define REG_OFFSET_TM0CNT_H 0x102 @@ -298,6 +300,8 @@ #define REG_ADDR_DMA3CNT_H (REG_BASE + REG_OFFSET_DMA3CNT_H) #define REG_ADDR_TMCNT (REG_BASE + REG_OFFSET_TMCNT) +#define REG_ADDR_TMCNT_L (REG_BASE + REG_OFFSET_TMCNT_L) +#define REG_ADDR_TMCNT_H (REG_BASE + REG_OFFSET_TMCNT_H) #define REG_ADDR_TM0CNT (REG_BASE + REG_OFFSET_TM0CNT) #define REG_ADDR_TM0CNT_L (REG_BASE + REG_OFFSET_TM0CNT_L) #define REG_ADDR_TM0CNT_H (REG_BASE + REG_OFFSET_TM0CNT_H) @@ -458,6 +462,8 @@ #define REG_DMA3CNT_H (*(vu16 *)REG_ADDR_DMA3CNT_H) #define REG_TMCNT(n) (*(vu16 *)(REG_ADDR_TMCNT + ((n) * 4))) +#define REG_TMCNT_L(n) (*(vu16 *)(REG_ADDR_TMCNT_L + ((n) * 4))) +#define REG_TMCNT_H(n) (*(vu16 *)(REG_ADDR_TMCNT_H + ((n) * 4))) #define REG_TM0CNT (*(vu32 *)REG_ADDR_TM0CNT) #define REG_TM0CNT_L (*(vu16 *)REG_ADDR_TM0CNT_L) #define REG_TM0CNT_H (*(vu16 *)REG_ADDR_TM0CNT_H) diff --git a/include/gba/macro.h b/include/gba/macro.h index 0217898e8..7b6b98c06 100644 --- a/include/gba/macro.h +++ b/include/gba/macro.h @@ -86,4 +86,14 @@ dmaRegs[5]; \ } +#define IntrEnable(flags) \ +{ \ + u16 imeTemp; \ + \ + imeTemp = REG_IME; \ + REG_IME = 0; \ + REG_IE |= flags; \ + REG_IME = imeTemp; \ +} \ + #endif // GUARD_GBA_MACRO_H diff --git a/ld_script.txt b/ld_script.txt index d0ae744d1..301f199f9 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -244,6 +244,7 @@ SECTIONS { src/agb_flash_1m.o(.text); src/agb_flash_mx.o(.text); src/siirtc.o(.text); + src/librfu.o(.text); asm/librfu.o(.text); asm/libagbsyscall.o(.text); tools/agbcc/lib/libgcc.a:_call_via_rX.o(.text); diff --git a/src/librfu.c b/src/librfu.c new file mode 100644 index 000000000..2cf5d6dad --- /dev/null +++ b/src/librfu.c @@ -0,0 +1,217 @@ +#include "global.h" + +#include "main.h" + +typedef struct RfuStruct +{ + s32 unk_0; + u8 unk_4; + u8 unk_5; + u8 unk_6; + u8 unk_7; + u8 unk_8; + u8 unk_9; + u8 timerSelect; + u8 unk_b; + u32 unk_c; + vu8 unk_10; + u8 unk_11; + vu16 unk_12; + vu8 msMode; + u8 unk_15; + u8 unk_16; + u8 unk_17; + void * callbackM; + void * callbackS; + u32 callbackID; + void * unk_24; + void * unk_28; + vu8 unk_2c; + u8 padding[3]; +} RfuStruct; + +typedef struct RfuIntrStruct +{ + u8 unk28Data[0x74]; + u8 unk24Data[0x74]; + u8 block1[0x960]; + u8 block2[0x30]; +} RfuIntrStruct; + +typedef struct RfuState +{ + RfuStruct *rfuStruct; +} RfuState; + +extern IntrFunc IntrSIO32(); +extern struct RfuState gRfuState; +void STWI_init_Callback_M(); +void STWI_init_Callback_S(); +void STWI_set_Callback_M(void * callback); +void STWI_set_Callback_S(void * callback); +extern void STWI_intr_timer(); + +void STWI_init_all(RfuIntrStruct *interruptStruct, IntrFunc *interrupt, bool8 copyInterruptToRam) +{ + struct RfuStruct *rfuStructTemp; + struct RfuStruct **rfuStructPtr; + u16 ime_temp; + int ret; + + // If we're copying our interrupt into RAM, DMA it to block1 and use + // block2 for our RfuStruct, otherwise block1 holds the RfuStruct. + // interrupt usually is a pointer to gIntrTable[1] + if (copyInterruptToRam == TRUE) + { + *interrupt = (IntrFunc)(&interruptStruct->block1); + DmaCopy16(3, &IntrSIO32, (void*)(&interruptStruct->block1), 0x960); + + gRfuState.rfuStruct = (struct RfuStruct*)(&interruptStruct->block2); + } + else + { + *interrupt = (IntrFunc)&IntrSIO32; + gRfuState.rfuStruct = (struct RfuStruct*)(&interruptStruct->block1); + } + + rfuStructPtr = (struct RfuStruct**)&gRfuState.rfuStruct; + (*rfuStructPtr)->unk_28 = (void*)&interruptStruct->unk28Data; + (*rfuStructPtr)->unk_24 = (void*)(&interruptStruct->unk24Data); + (*rfuStructPtr)->msMode = 1; + + (*rfuStructPtr)->unk_0 = 0; + (*rfuStructPtr)->unk_4 = 0; + (*rfuStructPtr)->unk_5 = 0; + (*rfuStructPtr)->unk_7 = 0; + (*rfuStructPtr)->unk_8 = 0; + (*rfuStructPtr)->unk_9 = 0; + (*rfuStructPtr)->unk_c = 0; + (*rfuStructPtr)->unk_10 = 0; + + // Don't @ me + rfuStructTemp = *rfuStructPtr; + rfuStructTemp->unk_12 = 0; + rfuStructTemp->unk_15 = 0; + + (*rfuStructPtr)->unk_2c = 0; + + REG_RCNT = 0x100; //TODO: mystery bit? + REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS; + STWI_init_Callback_M(); + STWI_init_Callback_S(); + + IntrEnable(INTR_FLAG_SERIAL); +} + +void STWI_init_timer(IntrFunc *interrupt, int timerSelect) +{ + *interrupt = &STWI_intr_timer; + gRfuState.rfuStruct->timerSelect = timerSelect; + + IntrEnable(INTR_FLAG_TIMER0 << gRfuState.rfuStruct->timerSelect); +} + +void AgbRFU_SoftReset() +{ + struct RfuStruct **rfuStructPtr; + struct RfuStruct *rfuStructTemp; + + REG_RCNT = 0x8000; + REG_RCNT = 0x80A0; // all these bits are undocumented + + { + vu16 *timerL = ®_TMCNT_L(gRfuState.rfuStruct->timerSelect); + vu16 *timerH = ®_TMCNT_H(gRfuState.rfuStruct->timerSelect); + + *timerH = 0; + *timerL = 0; + *timerH = 0x83; + + while (*timerL <= 0x11) + { + REG_RCNT = 0x80A2; + } + + *timerH = 3; + } + REG_RCNT = 0x80A0; + REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS; + + rfuStructPtr = (struct RfuStruct**)&gRfuState.rfuStruct; + + (*rfuStructPtr)->unk_0 = 0; + (*rfuStructPtr)->unk_4 = 0; + (*rfuStructPtr)->unk_5 = 0; + (*rfuStructPtr)->unk_6 = 0; + (*rfuStructPtr)->unk_7 = 0; + (*rfuStructPtr)->unk_8 = 0; + (*rfuStructPtr)->unk_9 = 0; + (*rfuStructPtr)->unk_c = 0; + (*rfuStructPtr)->unk_10 = 0; + + // Yeah this is the second time, there's probably something in the struct that I'm missing + rfuStructTemp = *rfuStructPtr; + rfuStructTemp->unk_12 = 0; + rfuStructTemp->msMode = 1; + (*rfuStructPtr)->unk_15 = 0; + + (*rfuStructPtr)->unk_2c = 0; +} + +void STWI_set_MS_mode(u8 mode) +{ + gRfuState.rfuStruct->msMode = mode; +} + +u32 STWI_read_status(u8 index) +{ + int result; + switch(index) + { + case 0: + return gRfuState.rfuStruct->unk_12; + case 1: + return gRfuState.rfuStruct->msMode; + case 2: + // something got inlined here? + //TODO: figure this one out + result = (gRfuState.rfuStruct->unk_0); + __asm__("lsl r0, r0, #16"); + __asm__("lsr r0, r0, #16"); + break; + + case 3: + return gRfuState.rfuStruct->unk_6; + break; + default: + return 0xFFFF; + break; + } + return result; +} + +void STWI_init_Callback_M() +{ + STWI_set_Callback_M(0); +} + +void STWI_init_Callback_S() +{ + STWI_set_Callback_S(0); +} + +void STWI_set_Callback_M(void * callback) +{ + gRfuState.rfuStruct->callbackM = callback; +} + +void STWI_set_Callback_S(void * callback) +{ + gRfuState.rfuStruct->callbackS = callback; +} + +void STWI_set_Callback_ID(u32 id) +{ + gRfuState.rfuStruct->callbackID = id; +} + diff --git a/sym_common.txt b/sym_common.txt index dbf6c17a8..233466e1c 100644 --- a/sym_common.txt +++ b/sym_common.txt @@ -448,7 +448,7 @@ gUnknown_03006370: @ 3006370 .include "agb_flash.o" -gUnknown_03007868: @ 3007868 +gRfuState: @ 3007868 .space 0x8 gUnknown_03007870: @ 3007870 From b420fb39624e10b364e17b593b1bf0d6e347a3ae Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Thu, 7 Sep 2017 03:59:39 -0600 Subject: [PATCH 02/11] Decompiled librfu to STWI_send_CP_EndREQ --- asm/librfu.s | 414 --------------------------------------------------- src/librfu.c | 230 +++++++++++++++++++++++++++- 2 files changed, 228 insertions(+), 416 deletions(-) diff --git a/asm/librfu.s b/asm/librfu.s index 2f0359f37..28c00f01f 100644 --- a/asm/librfu.s +++ b/asm/librfu.s @@ -4,420 +4,6 @@ .text - thumb_func_start STWI_poll_CommandEnd -STWI_poll_CommandEnd: @ 82E2C90 - push {lr} - ldr r0, _082E2CB4 - ldr r1, [r0] - adds r2, r1, 0 - adds r2, 0x2C - ldrb r1, [r2] - adds r3, r0, 0 - cmp r1, 0x1 - bne _082E2CAA - adds r1, r2, 0 -_082E2CA4: - ldrb r0, [r1] - cmp r0, 0x1 - beq _082E2CA4 -_082E2CAA: - ldr r0, [r3] - ldrh r0, [r0, 0x12] - pop {r1} - bx r1 - .align 2, 0 -_082E2CB4: .4byte gRfuState - thumb_func_end STWI_poll_CommandEnd - - thumb_func_start STWI_send_ResetREQ -STWI_send_ResetREQ: @ 82E2CB8 - push {lr} - movs r0, 0x10 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2CD2 - ldr r0, _082E2CD8 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2CD2: - pop {r0} - bx r0 - .align 2, 0 -_082E2CD8: .4byte gRfuState - thumb_func_end STWI_send_ResetREQ - - thumb_func_start STWI_send_LinkStatusREQ -STWI_send_LinkStatusREQ: @ 82E2CDC - push {lr} - movs r0, 0x11 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2CF6 - ldr r0, _082E2CFC - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2CF6: - pop {r0} - bx r0 - .align 2, 0 -_082E2CFC: .4byte gRfuState - thumb_func_end STWI_send_LinkStatusREQ - - thumb_func_start STWI_send_VersionStatusREQ -STWI_send_VersionStatusREQ: @ 82E2D00 - push {lr} - movs r0, 0x12 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2D1A - ldr r0, _082E2D20 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2D1A: - pop {r0} - bx r0 - .align 2, 0 -_082E2D20: .4byte gRfuState - thumb_func_end STWI_send_VersionStatusREQ - - thumb_func_start STWI_send_SystemStatusREQ -STWI_send_SystemStatusREQ: @ 82E2D24 - push {lr} - movs r0, 0x13 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2D3E - ldr r0, _082E2D44 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2D3E: - pop {r0} - bx r0 - .align 2, 0 -_082E2D44: .4byte gRfuState - thumb_func_end STWI_send_SystemStatusREQ - - thumb_func_start STWI_send_SlotStatusREQ -STWI_send_SlotStatusREQ: @ 82E2D48 - push {lr} - movs r0, 0x14 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2D62 - ldr r0, _082E2D68 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2D62: - pop {r0} - bx r0 - .align 2, 0 -_082E2D68: .4byte gRfuState - thumb_func_end STWI_send_SlotStatusREQ - - thumb_func_start STWI_send_ConfigStatusREQ -STWI_send_ConfigStatusREQ: @ 82E2D6C - push {lr} - movs r0, 0x15 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2D86 - ldr r0, _082E2D8C - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2D86: - pop {r0} - bx r0 - .align 2, 0 -_082E2D8C: .4byte gRfuState - thumb_func_end STWI_send_ConfigStatusREQ - - thumb_func_start STWI_send_GameConfigREQ -STWI_send_GameConfigREQ: @ 82E2D90 - push {r4,r5,lr} - adds r4, r0, 0 - adds r5, r1, 0 - movs r0, 0x16 - bl STWI_init - lsls r0, 16 - cmp r0, 0 - bne _082E2DDC - ldr r2, _082E2DE4 - ldr r1, [r2] - movs r0, 0x6 - strb r0, [r1, 0x4] - ldr r0, [r2] - ldr r1, [r0, 0x24] - adds r1, 0x4 - ldrh r0, [r4] - strh r0, [r1] - adds r1, 0x2 - adds r4, 0x2 - movs r2, 0xD -_082E2DBA: - ldrb r0, [r4] - strb r0, [r1] - adds r1, 0x1 - adds r4, 0x1 - subs r2, 0x1 - cmp r2, 0 - bge _082E2DBA - movs r2, 0x7 -_082E2DCA: - ldrb r0, [r5] - strb r0, [r1] - adds r1, 0x1 - adds r5, 0x1 - subs r2, 0x1 - cmp r2, 0 - bge _082E2DCA - bl STWI_start_Command -_082E2DDC: - pop {r4,r5} - pop {r0} - bx r0 - .align 2, 0 -_082E2DE4: .4byte gRfuState - thumb_func_end STWI_send_GameConfigREQ - - thumb_func_start STWI_send_SystemConfigREQ -STWI_send_SystemConfigREQ: @ 82E2DE8 - push {r4-r6,lr} - lsls r0, 16 - lsrs r6, r0, 16 - lsls r1, 24 - lsrs r5, r1, 24 - lsls r2, 24 - lsrs r4, r2, 24 - movs r0, 0x17 - bl STWI_init - lsls r0, 16 - cmp r0, 0 - bne _082E2E1E - ldr r2, _082E2E24 - ldr r1, [r2] - movs r0, 0x1 - strb r0, [r1, 0x4] - ldr r0, [r2] - ldr r0, [r0, 0x24] - adds r0, 0x4 - strb r4, [r0] - adds r0, 0x1 - strb r5, [r0] - adds r0, 0x1 - strh r6, [r0] - bl STWI_start_Command -_082E2E1E: - pop {r4-r6} - pop {r0} - bx r0 - .align 2, 0 -_082E2E24: .4byte gRfuState - thumb_func_end STWI_send_SystemConfigREQ - - thumb_func_start STWI_send_SC_StartREQ -STWI_send_SC_StartREQ: @ 82E2E28 - push {lr} - movs r0, 0x19 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2E42 - ldr r0, _082E2E48 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2E42: - pop {r0} - bx r0 - .align 2, 0 -_082E2E48: .4byte gRfuState - thumb_func_end STWI_send_SC_StartREQ - - thumb_func_start STWI_send_SC_PollingREQ -STWI_send_SC_PollingREQ: @ 82E2E4C - push {lr} - movs r0, 0x1A - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2E66 - ldr r0, _082E2E6C - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2E66: - pop {r0} - bx r0 - .align 2, 0 -_082E2E6C: .4byte gRfuState - thumb_func_end STWI_send_SC_PollingREQ - - thumb_func_start STWI_send_SC_EndREQ -STWI_send_SC_EndREQ: @ 82E2E70 - push {lr} - movs r0, 0x1B - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2E8A - ldr r0, _082E2E90 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2E8A: - pop {r0} - bx r0 - .align 2, 0 -_082E2E90: .4byte gRfuState - thumb_func_end STWI_send_SC_EndREQ - - thumb_func_start STWI_send_SP_StartREQ -STWI_send_SP_StartREQ: @ 82E2E94 - push {lr} - movs r0, 0x1C - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2EAE - ldr r0, _082E2EB4 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2EAE: - pop {r0} - bx r0 - .align 2, 0 -_082E2EB4: .4byte gRfuState - thumb_func_end STWI_send_SP_StartREQ - - thumb_func_start STWI_send_SP_PollingREQ -STWI_send_SP_PollingREQ: @ 82E2EB8 - push {lr} - movs r0, 0x1D - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2ED2 - ldr r0, _082E2ED8 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2ED2: - pop {r0} - bx r0 - .align 2, 0 -_082E2ED8: .4byte gRfuState - thumb_func_end STWI_send_SP_PollingREQ - - thumb_func_start STWI_send_SP_EndREQ -STWI_send_SP_EndREQ: @ 82E2EDC - push {lr} - movs r0, 0x1E - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2EF6 - ldr r0, _082E2EFC - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2EF6: - pop {r0} - bx r0 - .align 2, 0 -_082E2EFC: .4byte gRfuState - thumb_func_end STWI_send_SP_EndREQ - - thumb_func_start STWI_send_CP_StartREQ -STWI_send_CP_StartREQ: @ 82E2F00 - push {r4,lr} - lsls r0, 16 - lsrs r4, r0, 16 - movs r0, 0x1F - bl STWI_init - lsls r0, 16 - cmp r0, 0 - bne _082E2F24 - ldr r2, _082E2F2C - ldr r1, [r2] - movs r0, 0x1 - strb r0, [r1, 0x4] - ldr r0, [r2] - ldr r0, [r0, 0x24] - str r4, [r0, 0x4] - bl STWI_start_Command -_082E2F24: - pop {r4} - pop {r0} - bx r0 - .align 2, 0 -_082E2F2C: .4byte gRfuState - thumb_func_end STWI_send_CP_StartREQ - - thumb_func_start STWI_send_CP_PollingREQ -STWI_send_CP_PollingREQ: @ 82E2F30 - push {lr} - movs r0, 0x20 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2F4A - ldr r0, _082E2F50 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2F4A: - pop {r0} - bx r0 - .align 2, 0 -_082E2F50: .4byte gRfuState - thumb_func_end STWI_send_CP_PollingREQ - - thumb_func_start STWI_send_CP_EndREQ -STWI_send_CP_EndREQ: @ 82E2F54 - push {lr} - movs r0, 0x21 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E2F6E - ldr r0, _082E2F74 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E2F6E: - pop {r0} - bx r0 - .align 2, 0 -_082E2F74: .4byte gRfuState - thumb_func_end STWI_send_CP_EndREQ - thumb_func_start STWI_send_DataTxREQ STWI_send_DataTxREQ: @ 82E2F78 push {r4-r6,lr} diff --git a/src/librfu.c b/src/librfu.c index 2cf5d6dad..1b9846ece 100644 --- a/src/librfu.c +++ b/src/librfu.c @@ -2,6 +2,28 @@ #include "main.h" +enum +{ + RFU_RESET = 0x10, + RFU_LINK_STATUS, + RFU_VERSION_STATUS, + RFU_SYSTEM_STATUS, + RFU_SLOT_STATUS, + RFU_CONFIG_STATUS, + RFU_GAME_CONFIG, + RFU_SYSTEM_CONFIG, + RFU_UNK18, + RFU_SC_START, + RFU_SC_POLLING, + RFU_SC_END, + RFU_SP_START, + RFU_SP_POLLING, + RFU_SP_END, + RFU_CP_START, + RFU_CP_POLLING, + RFU_CP_END +}; + typedef struct RfuStruct { s32 unk_0; @@ -24,7 +46,7 @@ typedef struct RfuStruct void * callbackM; void * callbackS; u32 callbackID; - void * unk_24; + u8 * unk_24; void * unk_28; vu8 unk_2c; u8 padding[3]; @@ -49,6 +71,8 @@ void STWI_init_Callback_M(); void STWI_init_Callback_S(); void STWI_set_Callback_M(void * callback); void STWI_set_Callback_S(void * callback); +u16 STWI_init(u8 request); +int STWI_start_Command(); extern void STWI_intr_timer(); void STWI_init_all(RfuIntrStruct *interruptStruct, IntrFunc *interrupt, bool8 copyInterruptToRam) @@ -76,7 +100,7 @@ void STWI_init_all(RfuIntrStruct *interruptStruct, IntrFunc *interrupt, bool8 co rfuStructPtr = (struct RfuStruct**)&gRfuState.rfuStruct; (*rfuStructPtr)->unk_28 = (void*)&interruptStruct->unk28Data; - (*rfuStructPtr)->unk_24 = (void*)(&interruptStruct->unk24Data); + (*rfuStructPtr)->unk_24 = (u8*)(&interruptStruct->unk24Data); (*rfuStructPtr)->msMode = 1; (*rfuStructPtr)->unk_0 = 0; @@ -215,3 +239,205 @@ void STWI_set_Callback_ID(u32 id) gRfuState.rfuStruct->callbackID = id; } +u16 STWI_poll_CommandEnd() +{ + while ( gRfuState.rfuStruct->unk_2c == TRUE ); + + return gRfuState.rfuStruct->unk_12; +} + +void STWI_send_ResetREQ() +{ + if (!STWI_init(RFU_RESET)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_LinkStatusREQ() +{ + if (!STWI_init(RFU_LINK_STATUS)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_VersionStatusREQ() +{ + if (!STWI_init(RFU_VERSION_STATUS)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_SystemStatusREQ() +{ + if (!STWI_init(RFU_SYSTEM_STATUS)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_SlotStatusREQ() +{ + if (!STWI_init(RFU_SLOT_STATUS)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_ConfigStatusREQ() +{ + if (!STWI_init(RFU_CONFIG_STATUS)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_GameConfigREQ(u8 * unk1, u8 *data) +{ + u8 *v5; + int i; + + if (!STWI_init(RFU_GAME_CONFIG)) + { + gRfuState.rfuStruct->unk_4 = 6; //TODO: what is 6 + + //TODO: kinda gross but idk what's going on here + v5 = (u8*)gRfuState.rfuStruct->unk_24; + v5 += 4; + *(u16*)v5 = *(u16*)unk1; + + v5 += 2; + unk1 += 2; + i = 13; + do + { + *v5 = *unk1; + v5++; + unk1++; + i--; + } + while(i >= 0); + + i = 7; + do + { + *v5 = *data; + v5++; + data++; + i--; + } + while(i >= 0); + + STWI_start_Command(); + } +} + +void STWI_send_SystemConfigREQ(u16 unk1, u8 unk2, u8 unk3) +{ + u8 *v5; + + if (!STWI_init(RFU_SYSTEM_CONFIG)) + { + gRfuState.rfuStruct->unk_4 = 1; //TODO: what is 1 + + //TODO: kinda weird but idk what's going on here + v5 = (u8*)gRfuState.rfuStruct->unk_24; + v5 += 4; + + *v5++ = unk3; + *v5++ = unk2; + *(u16*)v5 = unk1; + STWI_start_Command(); + } +} + +void STWI_send_SC_StartREQ() +{ + if (!STWI_init(RFU_SC_START)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_SC_PollingREQ() +{ + if (!STWI_init(RFU_SC_POLLING)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_SC_EndREQ() +{ + if (!STWI_init(RFU_SC_END)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_SP_StartREQ() +{ + if (!STWI_init(RFU_SP_START)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_SP_PollingREQ() +{ + if (!STWI_init(RFU_SP_POLLING)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_SP_EndREQ() +{ + if (!STWI_init(RFU_SP_END)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_CP_StartREQ(u16 unk1) +{ + if (!STWI_init(RFU_CP_START)) + { + gRfuState.rfuStruct->unk_4 = 1; + *(u32*)(gRfuState.rfuStruct->unk_24 + 4) = unk1; + STWI_start_Command(); + } +} + +void STWI_send_CP_PollingREQ() +{ + if (!STWI_init(RFU_CP_POLLING)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + +void STWI_send_CP_EndREQ() +{ + if (!STWI_init(RFU_CP_END)) + { + gRfuState.rfuStruct->unk_4 = 0; + STWI_start_Command(); + } +} + From f4a21e5789eac049f6b9788123448b77fa9d7c06 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Sun, 10 Sep 2017 12:04:48 -0600 Subject: [PATCH 03/11] Cleanup and formatting fixes from camthesaxman --- src/librfu.c | 606 ++++++++++++++++++++++++--------------------------- 1 file changed, 279 insertions(+), 327 deletions(-) diff --git a/src/librfu.c b/src/librfu.c index 1b9846ece..8e450caa0 100644 --- a/src/librfu.c +++ b/src/librfu.c @@ -1,443 +1,395 @@ #include "global.h" - #include "main.h" enum { - RFU_RESET = 0x10, - RFU_LINK_STATUS, - RFU_VERSION_STATUS, - RFU_SYSTEM_STATUS, - RFU_SLOT_STATUS, - RFU_CONFIG_STATUS, - RFU_GAME_CONFIG, - RFU_SYSTEM_CONFIG, - RFU_UNK18, - RFU_SC_START, - RFU_SC_POLLING, - RFU_SC_END, - RFU_SP_START, - RFU_SP_POLLING, - RFU_SP_END, - RFU_CP_START, - RFU_CP_POLLING, - RFU_CP_END + RFU_RESET = 0x10, + RFU_LINK_STATUS, + RFU_VERSION_STATUS, + RFU_SYSTEM_STATUS, + RFU_SLOT_STATUS, + RFU_CONFIG_STATUS, + RFU_GAME_CONFIG, + RFU_SYSTEM_CONFIG, + RFU_UNK18, + RFU_SC_START, + RFU_SC_POLLING, + RFU_SC_END, + RFU_SP_START, + RFU_SP_POLLING, + RFU_SP_END, + RFU_CP_START, + RFU_CP_POLLING, + RFU_CP_END }; -typedef struct RfuStruct +struct RfuStruct { - s32 unk_0; - u8 unk_4; - u8 unk_5; - u8 unk_6; - u8 unk_7; - u8 unk_8; - u8 unk_9; - u8 timerSelect; - u8 unk_b; - u32 unk_c; - vu8 unk_10; - u8 unk_11; - vu16 unk_12; - vu8 msMode; - u8 unk_15; - u8 unk_16; - u8 unk_17; - void * callbackM; - void * callbackS; - u32 callbackID; - u8 * unk_24; - void * unk_28; - vu8 unk_2c; - u8 padding[3]; -} RfuStruct; + vs32 unk_0; + u8 unk_4; + u8 unk_5; + u8 unk_6; + u8 unk_7; + u8 unk_8; + u8 unk_9; + u8 timerSelect; + u8 unk_b; + u32 unk_c; + vu8 unk_10; + u8 unk_11; + vu16 unk_12; + vu8 msMode; + u8 unk_15; + u8 unk_16; + u8 unk_17; + void *callbackM; + void *callbackS; + u32 callbackID; + u8 *unk_24; + void *unk_28; + vu8 unk_2c; + u8 padding[3]; +}; -typedef struct RfuIntrStruct +struct RfuIntrStruct { - u8 unk28Data[0x74]; - u8 unk24Data[0x74]; - u8 block1[0x960]; - u8 block2[0x30]; -} RfuIntrStruct; + u8 unk28Data[0x74]; + u8 unk24Data[0x74]; + u8 block1[0x960]; + u8 block2[0x30]; +}; -typedef struct RfuState -{ - RfuStruct *rfuStruct; -} RfuState; - -extern IntrFunc IntrSIO32(); -extern struct RfuState gRfuState; -void STWI_init_Callback_M(); -void STWI_init_Callback_S(); +extern IntrFunc IntrSIO32(void); +extern struct RfuStruct *gRfuState; +void STWI_init_Callback_M(void); +void STWI_init_Callback_S(void); void STWI_set_Callback_M(void * callback); void STWI_set_Callback_S(void * callback); u16 STWI_init(u8 request); -int STWI_start_Command(); -extern void STWI_intr_timer(); +int STWI_start_Command(void); +extern void STWI_intr_timer(void); -void STWI_init_all(RfuIntrStruct *interruptStruct, IntrFunc *interrupt, bool8 copyInterruptToRam) +void STWI_init_all(struct RfuIntrStruct *interruptStruct, IntrFunc *interrupt, bool8 copyInterruptToRam) { - struct RfuStruct *rfuStructTemp; - struct RfuStruct **rfuStructPtr; - u16 ime_temp; - int ret; - - // If we're copying our interrupt into RAM, DMA it to block1 and use - // block2 for our RfuStruct, otherwise block1 holds the RfuStruct. - // interrupt usually is a pointer to gIntrTable[1] - if (copyInterruptToRam == TRUE) - { - *interrupt = (IntrFunc)(&interruptStruct->block1); - DmaCopy16(3, &IntrSIO32, (void*)(&interruptStruct->block1), 0x960); - - gRfuState.rfuStruct = (struct RfuStruct*)(&interruptStruct->block2); - } - else - { - *interrupt = (IntrFunc)&IntrSIO32; - gRfuState.rfuStruct = (struct RfuStruct*)(&interruptStruct->block1); - } - - rfuStructPtr = (struct RfuStruct**)&gRfuState.rfuStruct; - (*rfuStructPtr)->unk_28 = (void*)&interruptStruct->unk28Data; - (*rfuStructPtr)->unk_24 = (u8*)(&interruptStruct->unk24Data); - (*rfuStructPtr)->msMode = 1; - - (*rfuStructPtr)->unk_0 = 0; - (*rfuStructPtr)->unk_4 = 0; - (*rfuStructPtr)->unk_5 = 0; - (*rfuStructPtr)->unk_7 = 0; - (*rfuStructPtr)->unk_8 = 0; - (*rfuStructPtr)->unk_9 = 0; - (*rfuStructPtr)->unk_c = 0; - (*rfuStructPtr)->unk_10 = 0; - - // Don't @ me - rfuStructTemp = *rfuStructPtr; - rfuStructTemp->unk_12 = 0; - rfuStructTemp->unk_15 = 0; + // If we're copying our interrupt into RAM, DMA it to block1 and use + // block2 for our RfuStruct, otherwise block1 holds the RfuStruct. + // interrupt usually is a pointer to gIntrTable[1] + if (copyInterruptToRam == TRUE) + { + *interrupt = (IntrFunc)interruptStruct->block1; + DmaCopy16(3, &IntrSIO32, interruptStruct->block1, 0x960); + gRfuState = (struct RfuStruct*)interruptStruct->block2; + } + else + { + *interrupt = (IntrFunc)IntrSIO32; + gRfuState = (struct RfuStruct*)interruptStruct->block1; + } - (*rfuStructPtr)->unk_2c = 0; - - REG_RCNT = 0x100; //TODO: mystery bit? - REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS; - STWI_init_Callback_M(); - STWI_init_Callback_S(); - - IntrEnable(INTR_FLAG_SERIAL); + gRfuState->unk_28 = interruptStruct->unk28Data; + gRfuState->unk_24 = interruptStruct->unk24Data; + gRfuState->msMode = 1; + gRfuState->unk_0 = 0; + gRfuState->unk_4 = 0; + gRfuState->unk_5 = 0; + gRfuState->unk_7 = 0; + gRfuState->unk_8 = 0; + gRfuState->unk_9 = 0; + gRfuState->unk_c = 0; + gRfuState->unk_10 = 0; + gRfuState->unk_12 = 0; + gRfuState->unk_15 = 0; + gRfuState->unk_2c = 0; + + REG_RCNT = 0x100; //TODO: mystery bit? + REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS; + STWI_init_Callback_M(); + STWI_init_Callback_S(); + + IntrEnable(INTR_FLAG_SERIAL); } void STWI_init_timer(IntrFunc *interrupt, int timerSelect) { - *interrupt = &STWI_intr_timer; - gRfuState.rfuStruct->timerSelect = timerSelect; - - IntrEnable(INTR_FLAG_TIMER0 << gRfuState.rfuStruct->timerSelect); + *interrupt = STWI_intr_timer; + gRfuState->timerSelect = timerSelect; + + IntrEnable(INTR_FLAG_TIMER0 << gRfuState->timerSelect); } -void AgbRFU_SoftReset() +void AgbRFU_SoftReset(void) { - struct RfuStruct **rfuStructPtr; - struct RfuStruct *rfuStructTemp; + vu16 *timerL; + vu16 *timerH; - REG_RCNT = 0x8000; - REG_RCNT = 0x80A0; // all these bits are undocumented - - { - vu16 *timerL = ®_TMCNT_L(gRfuState.rfuStruct->timerSelect); - vu16 *timerH = ®_TMCNT_H(gRfuState.rfuStruct->timerSelect); + REG_RCNT = 0x8000; + REG_RCNT = 0x80A0; // all these bits are undocumented + timerL = ®_TMCNT_L(gRfuState->timerSelect); + timerH = ®_TMCNT_H(gRfuState->timerSelect); + *timerH = 0; + *timerL = 0; + *timerH = 0x83; + while (*timerL <= 0x11) + REG_RCNT = 0x80A2; + *timerH = 3; + REG_RCNT = 0x80A0; + REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS; - *timerH = 0; - *timerL = 0; - *timerH = 0x83; - - while (*timerL <= 0x11) - { - REG_RCNT = 0x80A2; - } - - *timerH = 3; - } - REG_RCNT = 0x80A0; - REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS; - - rfuStructPtr = (struct RfuStruct**)&gRfuState.rfuStruct; - - (*rfuStructPtr)->unk_0 = 0; - (*rfuStructPtr)->unk_4 = 0; - (*rfuStructPtr)->unk_5 = 0; - (*rfuStructPtr)->unk_6 = 0; - (*rfuStructPtr)->unk_7 = 0; - (*rfuStructPtr)->unk_8 = 0; - (*rfuStructPtr)->unk_9 = 0; - (*rfuStructPtr)->unk_c = 0; - (*rfuStructPtr)->unk_10 = 0; - - // Yeah this is the second time, there's probably something in the struct that I'm missing - rfuStructTemp = *rfuStructPtr; - rfuStructTemp->unk_12 = 0; - rfuStructTemp->msMode = 1; - (*rfuStructPtr)->unk_15 = 0; - - (*rfuStructPtr)->unk_2c = 0; + gRfuState->unk_0 = 0; + gRfuState->unk_4 = 0; + gRfuState->unk_5 = 0; + gRfuState->unk_6 = 0; + gRfuState->unk_7 = 0; + gRfuState->unk_8 = 0; + gRfuState->unk_9 = 0; + gRfuState->unk_c = 0; + gRfuState->unk_10 = 0; + gRfuState->unk_12 = 0; + gRfuState->msMode = 1; + gRfuState->unk_15 = 0; + gRfuState->unk_2c = 0; } void STWI_set_MS_mode(u8 mode) { - gRfuState.rfuStruct->msMode = mode; + gRfuState->msMode = mode; } -u32 STWI_read_status(u8 index) +u16 STWI_read_status(u8 index) { - int result; - switch(index) - { - case 0: - return gRfuState.rfuStruct->unk_12; - case 1: - return gRfuState.rfuStruct->msMode; - case 2: - // something got inlined here? - //TODO: figure this one out - result = (gRfuState.rfuStruct->unk_0); - __asm__("lsl r0, r0, #16"); - __asm__("lsr r0, r0, #16"); - break; - - case 3: - return gRfuState.rfuStruct->unk_6; - break; - default: - return 0xFFFF; - break; - } - return result; + switch (index) + { + case 0: + return gRfuState->unk_12; + case 1: + return gRfuState->msMode; + case 2: + return gRfuState->unk_0; + case 3: + return gRfuState->unk_6; + default: + return 0xFFFF; + } } -void STWI_init_Callback_M() +void STWI_init_Callback_M(void) { - STWI_set_Callback_M(0); + STWI_set_Callback_M(0); } -void STWI_init_Callback_S() +void STWI_init_Callback_S(void) { - STWI_set_Callback_S(0); + STWI_set_Callback_S(0); } -void STWI_set_Callback_M(void * callback) +void STWI_set_Callback_M(void *callback) { - gRfuState.rfuStruct->callbackM = callback; + gRfuState->callbackM = callback; } -void STWI_set_Callback_S(void * callback) +void STWI_set_Callback_S(void *callback) { - gRfuState.rfuStruct->callbackS = callback; + gRfuState->callbackS = callback; } void STWI_set_Callback_ID(u32 id) { - gRfuState.rfuStruct->callbackID = id; + gRfuState->callbackID = id; } -u16 STWI_poll_CommandEnd() +u16 STWI_poll_CommandEnd(void) { - while ( gRfuState.rfuStruct->unk_2c == TRUE ); - - return gRfuState.rfuStruct->unk_12; + while (gRfuState->unk_2c == TRUE) + ; + return gRfuState->unk_12; } -void STWI_send_ResetREQ() +void STWI_send_ResetREQ(void) { - if (!STWI_init(RFU_RESET)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_RESET)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_LinkStatusREQ() +void STWI_send_LinkStatusREQ(void) { - if (!STWI_init(RFU_LINK_STATUS)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_LINK_STATUS)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_VersionStatusREQ() +void STWI_send_VersionStatusREQ(void) { - if (!STWI_init(RFU_VERSION_STATUS)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_VERSION_STATUS)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_SystemStatusREQ() +void STWI_send_SystemStatusREQ(void) { - if (!STWI_init(RFU_SYSTEM_STATUS)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_SYSTEM_STATUS)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_SlotStatusREQ() +void STWI_send_SlotStatusREQ(void) { - if (!STWI_init(RFU_SLOT_STATUS)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_SLOT_STATUS)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_ConfigStatusREQ() +void STWI_send_ConfigStatusREQ(void) { - if (!STWI_init(RFU_CONFIG_STATUS)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_CONFIG_STATUS)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } void STWI_send_GameConfigREQ(u8 * unk1, u8 *data) { - u8 *v5; - int i; + u8 *v5; + int i; - if (!STWI_init(RFU_GAME_CONFIG)) - { - gRfuState.rfuStruct->unk_4 = 6; //TODO: what is 6 + if (!STWI_init(RFU_GAME_CONFIG)) + { + gRfuState->unk_4 = 6; //TODO: what is 6 - //TODO: kinda gross but idk what's going on here - v5 = (u8*)gRfuState.rfuStruct->unk_24; - v5 += 4; - *(u16*)v5 = *(u16*)unk1; + //TODO: kinda gross but idk what's going on here + v5 = (u8*)gRfuState->unk_24; + v5 += 4; + *(u16*)v5 = *(u16*)unk1; - v5 += 2; - unk1 += 2; - i = 13; - do - { - *v5 = *unk1; - v5++; - unk1++; - i--; - } - while(i >= 0); + v5 += 2; + unk1 += 2; - i = 7; - do - { - *v5 = *data; - v5++; - data++; - i--; - } - while(i >= 0); + for (i = 0; i < 14; i++) + { + *v5 = *unk1; + v5++; + unk1++; + } - STWI_start_Command(); - } + for (i = 0; i < 8; i++) + { + *v5 = *data; + v5++; + data++; + } + + STWI_start_Command(); + } } void STWI_send_SystemConfigREQ(u16 unk1, u8 unk2, u8 unk3) { u8 *v5; - if (!STWI_init(RFU_SYSTEM_CONFIG)) - { - gRfuState.rfuStruct->unk_4 = 1; //TODO: what is 1 + if (!STWI_init(RFU_SYSTEM_CONFIG)) + { + gRfuState->unk_4 = 1; //TODO: what is 1 - //TODO: kinda weird but idk what's going on here - v5 = (u8*)gRfuState.rfuStruct->unk_24; - v5 += 4; + //TODO: kinda weird but idk what's going on here + v5 = (u8*)gRfuState->unk_24; + v5 += 4; - *v5++ = unk3; - *v5++ = unk2; - *(u16*)v5 = unk1; - STWI_start_Command(); - } + *v5++ = unk3; + *v5++ = unk2; + *(u16*)v5 = unk1; + STWI_start_Command(); + } } -void STWI_send_SC_StartREQ() +void STWI_send_SC_StartREQ(void) { - if (!STWI_init(RFU_SC_START)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_SC_START)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_SC_PollingREQ() +void STWI_send_SC_PollingREQ(void) { - if (!STWI_init(RFU_SC_POLLING)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_SC_POLLING)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_SC_EndREQ() +void STWI_send_SC_EndREQ(void) { - if (!STWI_init(RFU_SC_END)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_SC_END)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_SP_StartREQ() +void STWI_send_SP_StartREQ(void) { - if (!STWI_init(RFU_SP_START)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_SP_START)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_SP_PollingREQ() +void STWI_send_SP_PollingREQ(void) { - if (!STWI_init(RFU_SP_POLLING)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_SP_POLLING)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_SP_EndREQ() +void STWI_send_SP_EndREQ(void) { - if (!STWI_init(RFU_SP_END)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_SP_END)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } void STWI_send_CP_StartREQ(u16 unk1) { - if (!STWI_init(RFU_CP_START)) - { - gRfuState.rfuStruct->unk_4 = 1; - *(u32*)(gRfuState.rfuStruct->unk_24 + 4) = unk1; - STWI_start_Command(); - } + if (!STWI_init(RFU_CP_START)) + { + gRfuState->unk_4 = 1; + *(u32*)(gRfuState->unk_24 + 4) = unk1; + STWI_start_Command(); + } } -void STWI_send_CP_PollingREQ() +void STWI_send_CP_PollingREQ(void) { - if (!STWI_init(RFU_CP_POLLING)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_CP_POLLING)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } -void STWI_send_CP_EndREQ() +void STWI_send_CP_EndREQ(void) { - if (!STWI_init(RFU_CP_END)) - { - gRfuState.rfuStruct->unk_4 = 0; - STWI_start_Command(); - } + if (!STWI_init(RFU_CP_END)) + { + gRfuState->unk_4 = 0; + STWI_start_Command(); + } } - From dac306c5e91ad8309bb0246d61204f8670e15955 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Sun, 10 Sep 2017 21:41:56 -0600 Subject: [PATCH 04/11] Decompiled librfu to STWI_send_DisconnectREQ --- asm/librfu.s | 243 --------------------------------------------------- src/librfu.c | 213 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 177 insertions(+), 279 deletions(-) diff --git a/asm/librfu.s b/asm/librfu.s index 28c00f01f..eb33c7768 100644 --- a/asm/librfu.s +++ b/asm/librfu.s @@ -4,249 +4,6 @@ .text - thumb_func_start STWI_send_DataTxREQ -STWI_send_DataTxREQ: @ 82E2F78 - push {r4-r6,lr} - adds r6, r0, 0 - lsls r4, r1, 24 - lsrs r5, r4, 24 - movs r0, 0x24 - bl STWI_init - lsls r0, 16 - cmp r0, 0 - bne _082E2FB6 - lsrs r2, r4, 26 - movs r0, 0x3 - ands r0, r5 - cmp r0, 0 - beq _082E2F98 - adds r2, 0x1 -_082E2F98: - ldr r1, _082E2FBC - ldr r0, [r1] - strb r2, [r0, 0x4] - ldr r0, [r1] - ldr r1, [r0, 0x24] - adds r1, 0x4 - ldrb r2, [r0, 0x4] - movs r0, 0x80 - lsls r0, 19 - orrs r2, r0 - adds r0, r6, 0 - bl CpuSet - bl STWI_start_Command -_082E2FB6: - pop {r4-r6} - pop {r0} - bx r0 - .align 2, 0 -_082E2FBC: .4byte gRfuState - thumb_func_end STWI_send_DataTxREQ - - thumb_func_start STWI_send_DataTxAndChangeREQ -STWI_send_DataTxAndChangeREQ: @ 82E2FC0 - push {r4-r6,lr} - adds r6, r0, 0 - lsls r4, r1, 24 - lsrs r5, r4, 24 - movs r0, 0x25 - bl STWI_init - lsls r0, 16 - cmp r0, 0 - bne _082E2FFE - lsrs r2, r4, 26 - movs r0, 0x3 - ands r0, r5 - cmp r0, 0 - beq _082E2FE0 - adds r2, 0x1 -_082E2FE0: - ldr r1, _082E3004 - ldr r0, [r1] - strb r2, [r0, 0x4] - ldr r0, [r1] - ldr r1, [r0, 0x24] - adds r1, 0x4 - ldrb r2, [r0, 0x4] - movs r0, 0x80 - lsls r0, 19 - orrs r2, r0 - adds r0, r6, 0 - bl CpuSet - bl STWI_start_Command -_082E2FFE: - pop {r4-r6} - pop {r0} - bx r0 - .align 2, 0 -_082E3004: .4byte gRfuState - thumb_func_end STWI_send_DataTxAndChangeREQ - - thumb_func_start STWI_send_DataRxREQ -STWI_send_DataRxREQ: @ 82E3008 - push {lr} - movs r0, 0x26 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E3022 - ldr r0, _082E3028 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E3022: - pop {r0} - bx r0 - .align 2, 0 -_082E3028: .4byte gRfuState - thumb_func_end STWI_send_DataRxREQ - - thumb_func_start STWI_send_MS_ChangeREQ -STWI_send_MS_ChangeREQ: @ 82E302C - push {lr} - movs r0, 0x27 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E3046 - ldr r0, _082E304C - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E3046: - pop {r0} - bx r0 - .align 2, 0 -_082E304C: .4byte gRfuState - thumb_func_end STWI_send_MS_ChangeREQ - - thumb_func_start STWI_send_DataReadyAndChangeREQ -STWI_send_DataReadyAndChangeREQ: @ 82E3050 - push {r4,r5,lr} - lsls r0, 24 - lsrs r4, r0, 24 - adds r5, r4, 0 - movs r0, 0x28 - bl STWI_init - lsls r0, 16 - lsrs r3, r0, 16 - cmp r3, 0 - bne _082E3096 - cmp r4, 0 - bne _082E3078 - ldr r0, _082E3074 - ldr r0, [r0] - strb r3, [r0, 0x4] - b _082E3092 - .align 2, 0 -_082E3074: .4byte gRfuState -_082E3078: - ldr r2, _082E309C - ldr r1, [r2] - movs r0, 0x1 - strb r0, [r1, 0x4] - ldr r0, [r2] - ldr r0, [r0, 0x24] - adds r0, 0x4 - strb r5, [r0] - adds r0, 0x1 - strb r3, [r0] - adds r0, 0x1 - strb r3, [r0] - strb r3, [r0, 0x1] -_082E3092: - bl STWI_start_Command -_082E3096: - pop {r4,r5} - pop {r0} - bx r0 - .align 2, 0 -_082E309C: .4byte gRfuState - thumb_func_end STWI_send_DataReadyAndChangeREQ - - thumb_func_start STWI_send_DisconnectedAndChangeREQ -STWI_send_DisconnectedAndChangeREQ: @ 82E30A0 - push {r4,r5,lr} - lsls r0, 24 - lsrs r4, r0, 24 - lsls r1, 24 - lsrs r5, r1, 24 - movs r0, 0x29 - bl STWI_init - lsls r0, 16 - lsrs r3, r0, 16 - cmp r3, 0 - bne _082E30D6 - ldr r2, _082E30DC - ldr r1, [r2] - movs r0, 0x1 - strb r0, [r1, 0x4] - ldr r0, [r2] - ldr r0, [r0, 0x24] - adds r0, 0x4 - strb r4, [r0] - adds r0, 0x1 - strb r5, [r0] - adds r0, 0x1 - strb r3, [r0] - strb r3, [r0, 0x1] - bl STWI_start_Command -_082E30D6: - pop {r4,r5} - pop {r0} - bx r0 - .align 2, 0 -_082E30DC: .4byte gRfuState - thumb_func_end STWI_send_DisconnectedAndChangeREQ - - thumb_func_start STWI_send_ResumeRetransmitAndChangeREQ -STWI_send_ResumeRetransmitAndChangeREQ: @ 82E30E0 - push {lr} - movs r0, 0x37 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E30FA - ldr r0, _082E3100 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E30FA: - pop {r0} - bx r0 - .align 2, 0 -_082E3100: .4byte gRfuState - thumb_func_end STWI_send_ResumeRetransmitAndChangeREQ - - thumb_func_start STWI_send_DisconnectREQ -STWI_send_DisconnectREQ: @ 82E3104 - push {r4,lr} - lsls r0, 24 - lsrs r4, r0, 24 - movs r0, 0x30 - bl STWI_init - lsls r0, 16 - cmp r0, 0 - bne _082E3128 - ldr r2, _082E3130 - ldr r1, [r2] - movs r0, 0x1 - strb r0, [r1, 0x4] - ldr r0, [r2] - ldr r0, [r0, 0x24] - str r4, [r0, 0x4] - bl STWI_start_Command -_082E3128: - pop {r4} - pop {r0} - bx r0 - .align 2, 0 -_082E3130: .4byte gRfuState - thumb_func_end STWI_send_DisconnectREQ - thumb_func_start STWI_send_TestModeREQ STWI_send_TestModeREQ: @ 82E3134 push {r4,r5,lr} diff --git a/src/librfu.c b/src/librfu.c index 8e450caa0..67b76b0d9 100644 --- a/src/librfu.c +++ b/src/librfu.c @@ -20,13 +20,41 @@ enum RFU_SP_END, RFU_CP_START, RFU_CP_POLLING, - RFU_CP_END + RFU_CP_END, + RFU_UNK22, + RFU_UNK23, + RFU_DATA_TX, + RFU_DATA_TX_AND_CHANGE, + RFU_DATA_RX, + RFU_MS_CHANGE, + RFU_DATA_READY_AND_CHANGE, + RFU_DISCONNECTED_AND_CHANGE, + RFU_UNK2A, + RFU_UNK2B, + RFU_UNK2C, + RFU_UNK2D, + RFU_UNK2E, + RFU_UNK2F, + RFU_DISCONNECT, + RFU_UNK31, + RFU_UNK32, + RFU_UNK33, + RFU_UNK34, + RFU_UNK35, + RFU_UNK36, + RFU_RESUME_RETRANSMIT_AND_CHANGE +}; + +struct RfuPacket +{ + u32 unk_0; + u32 data[0x1C]; }; struct RfuStruct { vs32 unk_0; - u8 unk_4; + u8 txParams; u8 unk_5; u8 unk_6; u8 unk_7; @@ -45,16 +73,16 @@ struct RfuStruct void *callbackM; void *callbackS; u32 callbackID; - u8 *unk_24; - void *unk_28; + struct RfuPacket *txPacket; + struct RfuPacket *rxPacket; vu8 unk_2c; u8 padding[3]; }; struct RfuIntrStruct { - u8 unk28Data[0x74]; - u8 unk24Data[0x74]; + u8 rxPacketAlloc[0x74]; + u8 txPacketAlloc[0x74]; u8 block1[0x960]; u8 block2[0x30]; }; @@ -86,11 +114,11 @@ void STWI_init_all(struct RfuIntrStruct *interruptStruct, IntrFunc *interrupt, b gRfuState = (struct RfuStruct*)interruptStruct->block1; } - gRfuState->unk_28 = interruptStruct->unk28Data; - gRfuState->unk_24 = interruptStruct->unk24Data; + gRfuState->rxPacket = (struct RfuPacket*)interruptStruct->rxPacketAlloc; + gRfuState->txPacket = (struct RfuPacket*)interruptStruct->txPacketAlloc; gRfuState->msMode = 1; gRfuState->unk_0 = 0; - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; gRfuState->unk_5 = 0; gRfuState->unk_7 = 0; gRfuState->unk_8 = 0; @@ -136,7 +164,7 @@ void AgbRFU_SoftReset(void) REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS; gRfuState->unk_0 = 0; - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; gRfuState->unk_5 = 0; gRfuState->unk_6 = 0; gRfuState->unk_7 = 0; @@ -208,7 +236,7 @@ void STWI_send_ResetREQ(void) { if (!STWI_init(RFU_RESET)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -217,7 +245,7 @@ void STWI_send_LinkStatusREQ(void) { if (!STWI_init(RFU_LINK_STATUS)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -226,7 +254,7 @@ void STWI_send_VersionStatusREQ(void) { if (!STWI_init(RFU_VERSION_STATUS)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -235,7 +263,7 @@ void STWI_send_SystemStatusREQ(void) { if (!STWI_init(RFU_SYSTEM_STATUS)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -244,7 +272,7 @@ void STWI_send_SlotStatusREQ(void) { if (!STWI_init(RFU_SLOT_STATUS)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -253,7 +281,7 @@ void STWI_send_ConfigStatusREQ(void) { if (!STWI_init(RFU_CONFIG_STATUS)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -265,15 +293,15 @@ void STWI_send_GameConfigREQ(u8 * unk1, u8 *data) if (!STWI_init(RFU_GAME_CONFIG)) { - gRfuState->unk_4 = 6; //TODO: what is 6 + gRfuState->txParams = 6; - //TODO: kinda gross but idk what's going on here - v5 = (u8*)gRfuState->unk_24; - v5 += 4; + //TODO: kinda gross but it was probably written weird + v5 = (u8*)gRfuState->txPacket; + v5 += sizeof(u32); *(u16*)v5 = *(u16*)unk1; - v5 += 2; - unk1 += 2; + v5 += sizeof(u16); + unk1 += sizeof(u16); for (i = 0; i < 14; i++) { @@ -299,11 +327,11 @@ void STWI_send_SystemConfigREQ(u16 unk1, u8 unk2, u8 unk3) if (!STWI_init(RFU_SYSTEM_CONFIG)) { - gRfuState->unk_4 = 1; //TODO: what is 1 + gRfuState->txParams = 1; - //TODO: kinda weird but idk what's going on here - v5 = (u8*)gRfuState->unk_24; - v5 += 4; + //TODO: kinda weird but I think it was written weird + v5 = (u8*)gRfuState->txPacket; + v5 += sizeof(u32); *v5++ = unk3; *v5++ = unk2; @@ -316,7 +344,7 @@ void STWI_send_SC_StartREQ(void) { if (!STWI_init(RFU_SC_START)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -325,7 +353,7 @@ void STWI_send_SC_PollingREQ(void) { if (!STWI_init(RFU_SC_POLLING)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -334,7 +362,7 @@ void STWI_send_SC_EndREQ(void) { if (!STWI_init(RFU_SC_END)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -343,7 +371,7 @@ void STWI_send_SP_StartREQ(void) { if (!STWI_init(RFU_SP_START)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -352,7 +380,7 @@ void STWI_send_SP_PollingREQ(void) { if (!STWI_init(RFU_SP_POLLING)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -361,7 +389,7 @@ void STWI_send_SP_EndREQ(void) { if (!STWI_init(RFU_SP_END)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -370,8 +398,8 @@ void STWI_send_CP_StartREQ(u16 unk1) { if (!STWI_init(RFU_CP_START)) { - gRfuState->unk_4 = 1; - *(u32*)(gRfuState->unk_24 + 4) = unk1; + gRfuState->txParams = 1; + gRfuState->txPacket->data[0] = unk1; STWI_start_Command(); } } @@ -380,7 +408,7 @@ void STWI_send_CP_PollingREQ(void) { if (!STWI_init(RFU_CP_POLLING)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; STWI_start_Command(); } } @@ -389,7 +417,120 @@ void STWI_send_CP_EndREQ(void) { if (!STWI_init(RFU_CP_END)) { - gRfuState->unk_4 = 0; + gRfuState->txParams = 0; + STWI_start_Command(); + } +} + +void STWI_send_DataTxREQ(void *in, u8 size) +{ + if (!STWI_init(RFU_DATA_TX)) + { + u8 txParams = (size / sizeof(u32)); + if (size & (sizeof(u32) - 1)) + txParams += 1; + + gRfuState->txParams = txParams; + CpuCopy32(in, gRfuState->txPacket->data, gRfuState->txParams * sizeof(u32)); + STWI_start_Command(); + } +} + +void STWI_send_DataTxAndChangeREQ(void *in, u8 size) +{ + if (!STWI_init(RFU_DATA_TX_AND_CHANGE)) + { + u8 txParams = (size / sizeof(u32)); + if (size & (sizeof(u32) - 1)) + txParams += 1; + + gRfuState->txParams = txParams; + CpuCopy32(in, gRfuState->txPacket->data, gRfuState->txParams * sizeof(u32)); + STWI_start_Command(); + } +} + +void STWI_send_DataRxREQ() +{ + if (!STWI_init(RFU_DATA_RX)) + { + gRfuState->txParams = 0; + STWI_start_Command(); + } +} + +void STWI_send_MS_ChangeREQ() +{ + if (!STWI_init(RFU_MS_CHANGE)) + { + gRfuState->txParams = 0; + STWI_start_Command(); + } +} + +void STWI_send_DataReadyAndChangeREQ(u8 unk) +{ + if (!STWI_init(RFU_DATA_READY_AND_CHANGE)) + { + if (!unk) + { + gRfuState->txParams = 0; + } + else + { + u8 *packetBytes; + + gRfuState->txParams = 1; + + packetBytes = (u8*)gRfuState->txPacket; + packetBytes += sizeof(u32); + + *packetBytes++ = unk; + *packetBytes++ = 0; + *packetBytes++ = 0; + *packetBytes = 0; + } + + STWI_start_Command(); + } +} + +void STWI_send_DisconnectedAndChangeREQ(u8 unk0, u8 unk1) +{ + if (!STWI_init(RFU_DISCONNECTED_AND_CHANGE)) + { + u8 *packetBytes; + + gRfuState->txParams = 1; + + packetBytes = (u8*)gRfuState->txPacket; + packetBytes += sizeof(u32); + + *packetBytes++ = unk0; + *packetBytes++ = unk1; + *packetBytes++ = 0; + *packetBytes = 0; + + STWI_start_Command(); + } +} + +void STWI_send_ResumeRetransmitAndChangeREQ() +{ + if (!STWI_init(RFU_RESUME_RETRANSMIT_AND_CHANGE)) + { + gRfuState->txParams = 0; + STWI_start_Command(); + } +} + +void STWI_send_DisconnectREQ(u8 unk) +{ + if (!STWI_init(RFU_DISCONNECT)) + { + gRfuState->txParams = 1; + gRfuState->txPacket->data[0] = unk; + STWI_start_Command(); } } From 9218c2ce16777bba33fea2b606c5805553d190de Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Sun, 10 Sep 2017 21:46:26 -0600 Subject: [PATCH 05/11] Decompile to STWI_send_TestModeREQ --- asm/librfu.s | 30 ------------------------------ src/librfu.c | 13 ++++++++++++- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/asm/librfu.s b/asm/librfu.s index eb33c7768..a01d4e505 100644 --- a/asm/librfu.s +++ b/asm/librfu.s @@ -4,36 +4,6 @@ .text - thumb_func_start STWI_send_TestModeREQ -STWI_send_TestModeREQ: @ 82E3134 - push {r4,r5,lr} - lsls r0, 24 - lsrs r5, r0, 24 - lsls r1, 24 - lsrs r4, r1, 24 - movs r0, 0x31 - bl STWI_init - lsls r0, 16 - cmp r0, 0 - bne _082E3160 - ldr r2, _082E3168 - ldr r1, [r2] - movs r0, 0x1 - strb r0, [r1, 0x4] - ldr r0, [r2] - ldr r1, [r0, 0x24] - lsls r0, r4, 8 - orrs r0, r5 - str r0, [r1, 0x4] - bl STWI_start_Command -_082E3160: - pop {r4,r5} - pop {r0} - bx r0 - .align 2, 0 -_082E3168: .4byte gRfuState - thumb_func_end STWI_send_TestModeREQ - thumb_func_start STWI_send_CPR_StartREQ STWI_send_CPR_StartREQ: @ 82E316C push {r4-r6,lr} diff --git a/src/librfu.c b/src/librfu.c index 67b76b0d9..00dde830b 100644 --- a/src/librfu.c +++ b/src/librfu.c @@ -36,7 +36,7 @@ enum RFU_UNK2E, RFU_UNK2F, RFU_DISCONNECT, - RFU_UNK31, + RFU_TEST_MODE, RFU_UNK32, RFU_UNK33, RFU_UNK34, @@ -534,3 +534,14 @@ void STWI_send_DisconnectREQ(u8 unk) STWI_start_Command(); } } + +void STWI_send_TestModeREQ(u8 unk0, u8 unk1) +{ + if (!STWI_init(RFU_TEST_MODE)) + { + gRfuState->txParams = 1; + gRfuState->txPacket->data[0] = unk0 | (unk1 << 8); + + STWI_start_Command(); + } +} From e26e1f1275a23e290322059bab1f80f89dac4f8a Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Sun, 10 Sep 2017 21:58:47 -0600 Subject: [PATCH 06/11] Use a union to differentiate between 8-bit and 32-bit operations on packets --- src/librfu.c | 72 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/src/librfu.c b/src/librfu.c index 00dde830b..5cbe9eb49 100644 --- a/src/librfu.c +++ b/src/librfu.c @@ -45,12 +45,23 @@ enum RFU_RESUME_RETRANSMIT_AND_CHANGE }; -struct RfuPacket +struct RfuPacket8 { - u32 unk_0; + u8 data[0x74]; +}; + +struct RfuPacket32 +{ + u32 command; u32 data[0x1C]; }; +union RfuPacket +{ + struct RfuPacket32 rfuPacket32; + struct RfuPacket8 rfuPacket8; +}; + struct RfuStruct { vs32 unk_0; @@ -73,8 +84,8 @@ struct RfuStruct void *callbackM; void *callbackS; u32 callbackID; - struct RfuPacket *txPacket; - struct RfuPacket *rxPacket; + union RfuPacket *txPacket; + union RfuPacket *rxPacket; vu8 unk_2c; u8 padding[3]; }; @@ -114,8 +125,8 @@ void STWI_init_all(struct RfuIntrStruct *interruptStruct, IntrFunc *interrupt, b gRfuState = (struct RfuStruct*)interruptStruct->block1; } - gRfuState->rxPacket = (struct RfuPacket*)interruptStruct->rxPacketAlloc; - gRfuState->txPacket = (struct RfuPacket*)interruptStruct->txPacketAlloc; + gRfuState->rxPacket = (union RfuPacket*)interruptStruct->rxPacketAlloc; + gRfuState->txPacket = (union RfuPacket*)interruptStruct->txPacketAlloc; gRfuState->msMode = 1; gRfuState->unk_0 = 0; gRfuState->txParams = 0; @@ -288,32 +299,32 @@ void STWI_send_ConfigStatusREQ(void) void STWI_send_GameConfigREQ(u8 * unk1, u8 *data) { - u8 *v5; + u8 *packetBytes; int i; if (!STWI_init(RFU_GAME_CONFIG)) { gRfuState->txParams = 6; - //TODO: kinda gross but it was probably written weird - v5 = (u8*)gRfuState->txPacket; - v5 += sizeof(u32); - *(u16*)v5 = *(u16*)unk1; + //TODO: what is unk1 + packetBytes = gRfuState->txPacket->rfuPacket8.data; + packetBytes += sizeof(u32); + *(u16*)packetBytes = *(u16*)unk1; - v5 += sizeof(u16); + packetBytes += sizeof(u16); unk1 += sizeof(u16); for (i = 0; i < 14; i++) { - *v5 = *unk1; - v5++; + *packetBytes = *unk1; + packetBytes++; unk1++; } for (i = 0; i < 8; i++) { - *v5 = *data; - v5++; + *packetBytes = *data; + packetBytes++; data++; } @@ -323,19 +334,18 @@ void STWI_send_GameConfigREQ(u8 * unk1, u8 *data) void STWI_send_SystemConfigREQ(u16 unk1, u8 unk2, u8 unk3) { - u8 *v5; - if (!STWI_init(RFU_SYSTEM_CONFIG)) { + u8 *packetBytes; + gRfuState->txParams = 1; - //TODO: kinda weird but I think it was written weird - v5 = (u8*)gRfuState->txPacket; - v5 += sizeof(u32); + packetBytes = gRfuState->txPacket->rfuPacket8.data; + packetBytes += sizeof(u32); - *v5++ = unk3; - *v5++ = unk2; - *(u16*)v5 = unk1; + *packetBytes++ = unk3; + *packetBytes++ = unk2; + *(u16*)packetBytes = unk1; STWI_start_Command(); } } @@ -399,7 +409,7 @@ void STWI_send_CP_StartREQ(u16 unk1) if (!STWI_init(RFU_CP_START)) { gRfuState->txParams = 1; - gRfuState->txPacket->data[0] = unk1; + gRfuState->txPacket->rfuPacket32.data[0] = unk1; STWI_start_Command(); } } @@ -431,7 +441,7 @@ void STWI_send_DataTxREQ(void *in, u8 size) txParams += 1; gRfuState->txParams = txParams; - CpuCopy32(in, gRfuState->txPacket->data, gRfuState->txParams * sizeof(u32)); + CpuCopy32(in, gRfuState->txPacket->rfuPacket32.data, gRfuState->txParams * sizeof(u32)); STWI_start_Command(); } } @@ -445,7 +455,7 @@ void STWI_send_DataTxAndChangeREQ(void *in, u8 size) txParams += 1; gRfuState->txParams = txParams; - CpuCopy32(in, gRfuState->txPacket->data, gRfuState->txParams * sizeof(u32)); + CpuCopy32(in, gRfuState->txPacket->rfuPacket32.data, gRfuState->txParams * sizeof(u32)); STWI_start_Command(); } } @@ -482,7 +492,7 @@ void STWI_send_DataReadyAndChangeREQ(u8 unk) gRfuState->txParams = 1; - packetBytes = (u8*)gRfuState->txPacket; + packetBytes = gRfuState->txPacket->rfuPacket8.data; packetBytes += sizeof(u32); *packetBytes++ = unk; @@ -503,7 +513,7 @@ void STWI_send_DisconnectedAndChangeREQ(u8 unk0, u8 unk1) gRfuState->txParams = 1; - packetBytes = (u8*)gRfuState->txPacket; + packetBytes = gRfuState->txPacket->rfuPacket8.data; packetBytes += sizeof(u32); *packetBytes++ = unk0; @@ -529,7 +539,7 @@ void STWI_send_DisconnectREQ(u8 unk) if (!STWI_init(RFU_DISCONNECT)) { gRfuState->txParams = 1; - gRfuState->txPacket->data[0] = unk; + gRfuState->txPacket->rfuPacket32.data[0] = unk; STWI_start_Command(); } @@ -540,7 +550,7 @@ void STWI_send_TestModeREQ(u8 unk0, u8 unk1) if (!STWI_init(RFU_TEST_MODE)) { gRfuState->txParams = 1; - gRfuState->txPacket->data[0] = unk0 | (unk1 << 8); + gRfuState->txPacket->rfuPacket32.data[0] = unk0 | (unk1 << 8); STWI_start_Command(); } From ce08f34ed54b9e5af9a8d1d6cd780df7207c1dbb Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Sun, 10 Sep 2017 22:22:10 -0600 Subject: [PATCH 07/11] Finish with STWI_send_* --- asm/librfu.s | 93 ---------------------------------------------------- src/librfu.c | 59 ++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 97 deletions(-) diff --git a/asm/librfu.s b/asm/librfu.s index a01d4e505..290c1b6c0 100644 --- a/asm/librfu.s +++ b/asm/librfu.s @@ -4,99 +4,6 @@ .text - thumb_func_start STWI_send_CPR_StartREQ -STWI_send_CPR_StartREQ: @ 82E316C - push {r4-r6,lr} - lsls r0, 16 - lsrs r5, r0, 16 - lsls r1, 16 - lsrs r4, r1, 16 - lsls r2, 24 - lsrs r6, r2, 24 - movs r0, 0x32 - bl STWI_init - lsls r0, 16 - cmp r0, 0 - bne _082E319E - ldr r2, _082E31A4 - ldr r1, [r2] - movs r0, 0x2 - strb r0, [r1, 0x4] - lsls r0, r5, 16 - orrs r0, r4 - ldr r1, [r2] - ldr r1, [r1, 0x24] - str r0, [r1, 0x4] - str r6, [r1, 0x8] - bl STWI_start_Command -_082E319E: - pop {r4-r6} - pop {r0} - bx r0 - .align 2, 0 -_082E31A4: .4byte gRfuState - thumb_func_end STWI_send_CPR_StartREQ - - thumb_func_start STWI_send_CPR_PollingREQ -STWI_send_CPR_PollingREQ: @ 82E31A8 - push {lr} - movs r0, 0x33 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E31C2 - ldr r0, _082E31C8 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E31C2: - pop {r0} - bx r0 - .align 2, 0 -_082E31C8: .4byte gRfuState - thumb_func_end STWI_send_CPR_PollingREQ - - thumb_func_start STWI_send_CPR_EndREQ -STWI_send_CPR_EndREQ: @ 82E31CC - push {lr} - movs r0, 0x34 - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E31E6 - ldr r0, _082E31EC - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E31E6: - pop {r0} - bx r0 - .align 2, 0 -_082E31EC: .4byte gRfuState - thumb_func_end STWI_send_CPR_EndREQ - - thumb_func_start STWI_send_StopModeREQ -STWI_send_StopModeREQ: @ 82E31F0 - push {lr} - movs r0, 0x3D - bl STWI_init - lsls r0, 16 - lsrs r1, r0, 16 - cmp r1, 0 - bne _082E320A - ldr r0, _082E3210 - ldr r0, [r0] - strb r1, [r0, 0x4] - bl STWI_start_Command -_082E320A: - pop {r0} - bx r0 - .align 2, 0 -_082E3210: .4byte gRfuState - thumb_func_end STWI_send_StopModeREQ - thumb_func_start STWI_intr_timer STWI_intr_timer: @ 82E3214 push {r4,lr} diff --git a/src/librfu.c b/src/librfu.c index 5cbe9eb49..dfb2f7221 100644 --- a/src/librfu.c +++ b/src/librfu.c @@ -37,12 +37,18 @@ enum RFU_UNK2F, RFU_DISCONNECT, RFU_TEST_MODE, - RFU_UNK32, - RFU_UNK33, - RFU_UNK34, + RFU_CPR_START, + RFU_CPR_POLLING, + RFU_CPR_END, RFU_UNK35, RFU_UNK36, - RFU_RESUME_RETRANSMIT_AND_CHANGE + RFU_RESUME_RETRANSMIT_AND_CHANGE, + RFU_UNK38, + RFU_UNK39, + RFU_UNK3A, + RFU_UNK3B, + RFU_UNK3C, + RFU_STOP_MODE, //3D }; struct RfuPacket8 @@ -555,3 +561,48 @@ void STWI_send_TestModeREQ(u8 unk0, u8 unk1) STWI_start_Command(); } } + +void STWI_send_CPR_StartREQ(u16 unk0, u16 unk1, u8 unk2) +{ + u32 *packetData; + u32 arg1; + + if (!STWI_init(RFU_CPR_START)) + { + gRfuState->txParams = 2; + + arg1 = unk1 | (unk0 << 16); + packetData = gRfuState->txPacket->rfuPacket32.data; + packetData[0] = arg1; + packetData[1] = unk2; + + STWI_start_Command(); + } +} + +void STWI_send_CPR_PollingREQ() +{ + if (!STWI_init(RFU_CPR_POLLING)) + { + gRfuState->txParams = 0; + STWI_start_Command(); + } +} + +void STWI_send_CPR_EndREQ() +{ + if (!STWI_init(RFU_CPR_END)) + { + gRfuState->txParams = 0; + STWI_start_Command(); + } +} + +void STWI_send_StopModeREQ() +{ + if (!STWI_init(RFU_STOP_MODE)) + { + gRfuState->txParams = 0; + STWI_start_Command(); + } +} From 3c51ecded9dee7c8d85642709d2e93af34274f39 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Mon, 11 Sep 2017 17:04:13 -0600 Subject: [PATCH 08/11] Decompile to STWI_init --- asm/librfu.s | 277 --------------------------------------------------- src/librfu.c | 143 +++++++++++++++++++++++--- 2 files changed, 128 insertions(+), 292 deletions(-) diff --git a/asm/librfu.s b/asm/librfu.s index 290c1b6c0..491b4fb66 100644 --- a/asm/librfu.s +++ b/asm/librfu.s @@ -4,283 +4,6 @@ .text - thumb_func_start STWI_intr_timer -STWI_intr_timer: @ 82E3214 - push {r4,lr} - ldr r4, _082E322C - ldr r1, [r4] - ldr r0, [r1, 0xC] - cmp r0, 0x2 - beq _082E323A - cmp r0, 0x2 - bgt _082E3230 - cmp r0, 0x1 - beq _082E3248 - b _082E3270 - .align 2, 0 -_082E322C: .4byte gRfuState -_082E3230: - cmp r0, 0x3 - beq _082E3252 - cmp r0, 0x4 - beq _082E3248 - b _082E3270 -_082E323A: - ldrb r0, [r1, 0x10] - movs r0, 0x1 - strb r0, [r1, 0x10] - movs r0, 0x32 - bl STWI_set_timer - b _082E3270 -_082E3248: - bl STWI_stop_timer - bl STWI_restart_Command - b _082E3270 -_082E3252: - ldrb r0, [r1, 0x10] - movs r0, 0x1 - strb r0, [r1, 0x10] - bl STWI_stop_timer - bl STWI_reset_ClockCounter - ldr r0, [r4] - ldr r2, [r0, 0x18] - cmp r2, 0 - beq _082E3270 - movs r0, 0xFF - movs r1, 0 - bl _call_via_r2 -_082E3270: - pop {r4} - pop {r0} - bx r0 - thumb_func_end STWI_intr_timer - - thumb_func_start STWI_set_timer -STWI_set_timer: @ 82E3278 - push {r4-r7,lr} - lsls r0, 24 - lsrs r3, r0, 24 - adds r5, r3, 0 - ldr r2, _082E32A8 - ldr r0, [r2] - ldrb r0, [r0, 0xA] - lsls r0, 2 - ldr r1, _082E32AC - adds r4, r0, r1 - adds r1, 0x2 - adds r7, r0, r1 - ldr r1, _082E32B0 - movs r0, 0 - strh r0, [r1] - adds r6, r2, 0 - cmp r3, 0x50 - beq _082E32D0 - cmp r3, 0x50 - bgt _082E32B4 - cmp r3, 0x32 - beq _082E32BE - b _082E32FC - .align 2, 0 -_082E32A8: .4byte gRfuState -_082E32AC: .4byte 0x04000100 -_082E32B0: .4byte 0x04000208 -_082E32B4: - cmp r5, 0x64 - beq _082E32E0 - cmp r5, 0x82 - beq _082E32F0 - b _082E32FC -_082E32BE: - ldr r1, _082E32CC - adds r0, r1, 0 - strh r0, [r4] - ldr r1, [r6] - movs r0, 0x1 - b _082E32FA - .align 2, 0 -_082E32CC: .4byte 0x0000fccb -_082E32D0: - ldr r1, _082E32DC - adds r0, r1, 0 - strh r0, [r4] - ldr r1, [r6] - movs r0, 0x2 - b _082E32FA - .align 2, 0 -_082E32DC: .4byte 0x0000fae0 -_082E32E0: - ldr r1, _082E32EC - adds r0, r1, 0 - strh r0, [r4] - ldr r1, [r6] - movs r0, 0x3 - b _082E32FA - .align 2, 0 -_082E32EC: .4byte 0x0000f996 -_082E32F0: - ldr r1, _082E3318 - adds r0, r1, 0 - strh r0, [r4] - ldr r1, [r6] - movs r0, 0x4 -_082E32FA: - str r0, [r1, 0xC] -_082E32FC: - movs r0, 0xC3 - strh r0, [r7] - ldr r2, _082E331C - ldr r1, [r6] - movs r0, 0x8 - ldrb r1, [r1, 0xA] - lsls r0, r1 - strh r0, [r2] - ldr r1, _082E3320 - movs r0, 0x1 - strh r0, [r1] - pop {r4-r7} - pop {r0} - bx r0 - .align 2, 0 -_082E3318: .4byte 0x0000f7ad -_082E331C: .4byte 0x04000202 -_082E3320: .4byte 0x04000208 - thumb_func_end STWI_set_timer - - thumb_func_start STWI_stop_timer -STWI_stop_timer: @ 82E3324 - ldr r2, _082E3344 - ldr r0, [r2] - movs r1, 0 - str r1, [r0, 0xC] - ldrb r0, [r0, 0xA] - lsls r0, 2 - ldr r3, _082E3348 - adds r0, r3 - strh r1, [r0] - ldr r0, [r2] - ldrb r0, [r0, 0xA] - lsls r0, 2 - ldr r2, _082E334C - adds r0, r2 - strh r1, [r0] - bx lr - .align 2, 0 -_082E3344: .4byte gRfuState -_082E3348: .4byte 0x04000100 -_082E334C: .4byte 0x04000102 - thumb_func_end STWI_stop_timer - - thumb_func_start STWI_init -STWI_init: @ 82E3350 - push {r4-r6,lr} - lsls r0, 24 - lsrs r5, r0, 24 - adds r6, r5, 0 - ldr r0, _082E337C - ldrh r0, [r0] - cmp r0, 0 - bne _082E3384 - ldr r0, _082E3380 - ldr r1, [r0] - ldrh r0, [r1, 0x12] - movs r0, 0x6 - strh r0, [r1, 0x12] - ldr r2, [r1, 0x18] - cmp r2, 0 - beq _082E33CE - ldrh r1, [r1, 0x12] - adds r0, r5, 0 - bl _call_via_r2 - b _082E33CE - .align 2, 0 -_082E337C: .4byte 0x04000208 -_082E3380: .4byte gRfuState -_082E3384: - ldr r4, _082E33B0 - ldr r2, [r4] - adds r3, r2, 0 - adds r3, 0x2C - ldrb r0, [r3] - cmp r0, 0x1 - bne _082E33B4 - ldrh r0, [r2, 0x12] - movs r1, 0 - movs r0, 0x2 - strh r0, [r2, 0x12] - ldrb r0, [r3] - strb r1, [r3] - ldr r0, [r4] - ldr r2, [r0, 0x18] - cmp r2, 0 - beq _082E33CE - ldrh r1, [r0, 0x12] - adds r0, r5, 0 - bl _call_via_r2 - b _082E33CE - .align 2, 0 -_082E33B0: .4byte gRfuState -_082E33B4: - ldrb r0, [r2, 0x14] - cmp r0, 0 - bne _082E33D2 - ldrh r0, [r2, 0x12] - movs r0, 0x4 - strh r0, [r2, 0x12] - ldr r3, [r2, 0x18] - cmp r3, 0 - beq _082E33CE - ldrh r1, [r2, 0x12] - adds r0, r5, 0 - bl _call_via_r3 -_082E33CE: - movs r0, 0x1 - b _082E341A -_082E33D2: - ldrb r0, [r3] - movs r1, 0 - movs r0, 0x1 - strb r0, [r3] - ldr r0, [r4] - strb r6, [r0, 0x6] - ldr r0, [r4] - str r1, [r0] - strb r1, [r0, 0x4] - ldr r0, [r4] - strb r1, [r0, 0x5] - ldr r0, [r4] - strb r1, [r0, 0x7] - ldr r0, [r4] - strb r1, [r0, 0x8] - ldr r0, [r4] - strb r1, [r0, 0x9] - ldr r0, [r4] - str r1, [r0, 0xC] - ldrb r2, [r0, 0x10] - strb r1, [r0, 0x10] - ldr r0, [r4] - ldrh r2, [r0, 0x12] - movs r2, 0 - strh r1, [r0, 0x12] - strb r2, [r0, 0x15] - ldr r1, _082E3420 - movs r2, 0x80 - lsls r2, 1 - adds r0, r2, 0 - strh r0, [r1] - subs r1, 0xC - ldr r2, _082E3424 - adds r0, r2, 0 - strh r0, [r1] - movs r0, 0 -_082E341A: - pop {r4-r6} - pop {r1} - bx r1 - .align 2, 0 -_082E3420: .4byte 0x04000134 -_082E3424: .4byte 0x00005003 - thumb_func_end STWI_init - thumb_func_start STWI_start_Command STWI_start_Command: @ 82E3428 push {r4,r5,lr} diff --git a/src/librfu.c b/src/librfu.c index dfb2f7221..f1c35b1ec 100644 --- a/src/librfu.c +++ b/src/librfu.c @@ -79,16 +79,16 @@ struct RfuStruct u8 unk_9; u8 timerSelect; u8 unk_b; - u32 unk_c; - vu8 unk_10; + int timerState; + vu8 timerActive; u8 unk_11; vu16 unk_12; vu8 msMode; u8 unk_15; u8 unk_16; u8 unk_17; - void *callbackM; - void *callbackS; + void (*callbackM)(); + void (*callbackS)(); u32 callbackID; union RfuPacket *txPacket; union RfuPacket *rxPacket; @@ -112,7 +112,11 @@ void STWI_set_Callback_M(void * callback); void STWI_set_Callback_S(void * callback); u16 STWI_init(u8 request); int STWI_start_Command(void); -extern void STWI_intr_timer(void); +void STWI_intr_timer(void); +void STWI_set_timer(u8 unk); +extern void STWI_stop_timer(void); +extern void STWI_restart_Command(void); +extern void STWI_reset_ClockCounter(void); void STWI_init_all(struct RfuIntrStruct *interruptStruct, IntrFunc *interrupt, bool8 copyInterruptToRam) { @@ -140,8 +144,8 @@ void STWI_init_all(struct RfuIntrStruct *interruptStruct, IntrFunc *interrupt, b gRfuState->unk_7 = 0; gRfuState->unk_8 = 0; gRfuState->unk_9 = 0; - gRfuState->unk_c = 0; - gRfuState->unk_10 = 0; + gRfuState->timerState = 0; + gRfuState->timerActive = 0; gRfuState->unk_12 = 0; gRfuState->unk_15 = 0; gRfuState->unk_2c = 0; @@ -187,8 +191,8 @@ void AgbRFU_SoftReset(void) gRfuState->unk_7 = 0; gRfuState->unk_8 = 0; gRfuState->unk_9 = 0; - gRfuState->unk_c = 0; - gRfuState->unk_10 = 0; + gRfuState->timerState = 0; + gRfuState->timerActive = 0; gRfuState->unk_12 = 0; gRfuState->msMode = 1; gRfuState->unk_15 = 0; @@ -466,7 +470,7 @@ void STWI_send_DataTxAndChangeREQ(void *in, u8 size) } } -void STWI_send_DataRxREQ() +void STWI_send_DataRxREQ(void) { if (!STWI_init(RFU_DATA_RX)) { @@ -475,7 +479,7 @@ void STWI_send_DataRxREQ() } } -void STWI_send_MS_ChangeREQ() +void STWI_send_MS_ChangeREQ(void) { if (!STWI_init(RFU_MS_CHANGE)) { @@ -531,7 +535,7 @@ void STWI_send_DisconnectedAndChangeREQ(u8 unk0, u8 unk1) } } -void STWI_send_ResumeRetransmitAndChangeREQ() +void STWI_send_ResumeRetransmitAndChangeREQ(void) { if (!STWI_init(RFU_RESUME_RETRANSMIT_AND_CHANGE)) { @@ -580,7 +584,7 @@ void STWI_send_CPR_StartREQ(u16 unk0, u16 unk1, u8 unk2) } } -void STWI_send_CPR_PollingREQ() +void STWI_send_CPR_PollingREQ(void) { if (!STWI_init(RFU_CPR_POLLING)) { @@ -589,7 +593,7 @@ void STWI_send_CPR_PollingREQ() } } -void STWI_send_CPR_EndREQ() +void STWI_send_CPR_EndREQ(void) { if (!STWI_init(RFU_CPR_END)) { @@ -598,7 +602,7 @@ void STWI_send_CPR_EndREQ() } } -void STWI_send_StopModeREQ() +void STWI_send_StopModeREQ(void) { if (!STWI_init(RFU_STOP_MODE)) { @@ -606,3 +610,112 @@ void STWI_send_StopModeREQ() STWI_start_Command(); } } + +void STWI_intr_timer(void) +{ + switch (gRfuState->timerState) + { + //TODO: Make an enum for these + case 2: + gRfuState->timerActive = 1; + STWI_set_timer(50); + break; + case 1: + case 4: + STWI_stop_timer(); + STWI_restart_Command(); + break; + case 3: + gRfuState->timerActive = 1; + STWI_stop_timer(); + STWI_reset_ClockCounter(); + if (gRfuState->callbackM) + gRfuState->callbackM(255, 0); + break; + } +} + +void STWI_set_timer(u8 unk) +{ + vu16 *timerL; + vu16 *timerH; + + timerL = ®_TMCNT_L(gRfuState->timerSelect); + timerH = ®_TMCNT_H(gRfuState->timerSelect); + REG_IME = 0; + switch (unk) + { + case 50: + *timerL = 0xFCCB; + gRfuState->timerState = 1; + break; + case 80: + *timerL = 0xFAE0; + gRfuState->timerState = 2; + break; + case 100: + *timerL = 0xF996; + gRfuState->timerState = 3; + break; + case 130: + *timerL = 0xF7AD; + gRfuState->timerState = 4; + break; + } + *timerH = TIMER_ENABLE | TIMER_INTR_ENABLE | TIMER_1024CLK; + REG_IF = INTR_FLAG_TIMER0 << gRfuState->timerSelect; + REG_IME = 1; +} + +void STWI_stop_timer(void) +{ + gRfuState->timerState = 0; + + REG_TMCNT_L(gRfuState->timerSelect) = 0; + REG_TMCNT_H(gRfuState->timerSelect) = 0; +} + +u16 STWI_init(u8 request) +{ + if (!REG_IME) + { + gRfuState->unk_12 = 6; + if (gRfuState->callbackM) + gRfuState->callbackM(request, gRfuState->unk_12); + return TRUE; + } + else if (gRfuState->unk_2c == TRUE) + { + gRfuState->unk_12 = 2; + gRfuState->unk_2c = FALSE; + if (gRfuState->callbackM) + gRfuState->callbackM(request, gRfuState->unk_12); + return TRUE; + } + else if(!gRfuState->msMode) + { + gRfuState->unk_12 = 4; + if (gRfuState->callbackM) + gRfuState->callbackM(request, gRfuState->unk_12, gRfuState); + return TRUE; + } + else + { + gRfuState->unk_2c = TRUE; + gRfuState->unk_6 = request; + gRfuState->unk_0 = 0; + gRfuState->txParams = 0; + gRfuState->unk_5 = 0; + gRfuState->unk_7 = 0; + gRfuState->unk_8 = 0; + gRfuState->unk_9 = 0; + gRfuState->timerState = 0; + gRfuState->timerActive = 0; + gRfuState->unk_12 = 0; + gRfuState->unk_15 = 0; + + REG_RCNT = 0x100; + REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS; + return FALSE; + } +} From a8a6816f0e6e6c20aa1ae23bf8030153c147b075 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Mon, 11 Sep 2017 17:49:50 -0600 Subject: [PATCH 09/11] Decompile STWI_start_command --- asm/librfu.s | 56 ---------------------------------------------------- src/librfu.c | 30 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 60 deletions(-) diff --git a/asm/librfu.s b/asm/librfu.s index 491b4fb66..10db8da19 100644 --- a/asm/librfu.s +++ b/asm/librfu.s @@ -4,62 +4,6 @@ .text - thumb_func_start STWI_start_Command -STWI_start_Command: @ 82E3428 - push {r4,r5,lr} - ldr r5, _082E3480 - ldr r0, [r5] - ldr r3, [r0, 0x24] - ldrb r1, [r0, 0x4] - lsls r1, 8 - ldrb r0, [r0, 0x6] - ldr r2, _082E3484 - orrs r0, r2 - orrs r1, r0 - str r1, [r3] - ldr r2, _082E3488 - ldr r1, [r5] - ldr r0, [r1, 0x24] - ldr r0, [r0] - str r0, [r2] - movs r2, 0 - str r2, [r1] - movs r0, 0x1 - strb r0, [r1, 0x5] - ldr r4, _082E348C - ldrh r3, [r4] - strh r2, [r4] - ldr r2, _082E3490 - ldr r1, [r5] - movs r0, 0x8 - ldrb r1, [r1, 0xA] - lsls r0, r1 - ldrh r1, [r2] - orrs r0, r1 - strh r0, [r2] - ldrh r0, [r2] - movs r1, 0x80 - orrs r0, r1 - strh r0, [r2] - strh r3, [r4] - ldr r1, _082E3494 - ldr r2, _082E3498 - adds r0, r2, 0 - strh r0, [r1] - movs r0, 0 - pop {r4,r5} - pop {r1} - bx r1 - .align 2, 0 -_082E3480: .4byte gRfuState -_082E3484: .4byte 0x99660000 -_082E3488: .4byte 0x04000120 -_082E348C: .4byte 0x04000208 -_082E3490: .4byte 0x04000200 -_082E3494: .4byte 0x04000128 -_082E3498: .4byte 0x00005083 - thumb_func_end STWI_start_Command - thumb_func_start STWI_restart_Command STWI_restart_Command: @ 82E349C push {r4,lr} diff --git a/src/librfu.c b/src/librfu.c index f1c35b1ec..37b76ec3e 100644 --- a/src/librfu.c +++ b/src/librfu.c @@ -73,7 +73,7 @@ struct RfuStruct vs32 unk_0; u8 txParams; u8 unk_5; - u8 unk_6; + u8 activeCommand; u8 unk_7; u8 unk_8; u8 unk_9; @@ -187,7 +187,7 @@ void AgbRFU_SoftReset(void) gRfuState->unk_0 = 0; gRfuState->txParams = 0; gRfuState->unk_5 = 0; - gRfuState->unk_6 = 0; + gRfuState->activeCommand = 0; gRfuState->unk_7 = 0; gRfuState->unk_8 = 0; gRfuState->unk_9 = 0; @@ -215,7 +215,7 @@ u16 STWI_read_status(u8 index) case 2: return gRfuState->unk_0; case 3: - return gRfuState->unk_6; + return gRfuState->activeCommand; default: return 0xFFFF; } @@ -702,7 +702,7 @@ u16 STWI_init(u8 request) else { gRfuState->unk_2c = TRUE; - gRfuState->unk_6 = request; + gRfuState->activeCommand = request; gRfuState->unk_0 = 0; gRfuState->txParams = 0; gRfuState->unk_5 = 0; @@ -719,3 +719,25 @@ u16 STWI_init(u8 request) return FALSE; } } + +int STWI_start_Command() +{ + u16 imeTemp; + + // Yes, it matters that it's casted to a u32... + *(u32*)gRfuState->txPacket->rfuPacket8.data = 0x99660000 | (gRfuState->txParams << 8) | gRfuState->activeCommand; + REG_SIODATA32 = gRfuState->txPacket->rfuPacket32.command; + + gRfuState->unk_0 = 0; + gRfuState->unk_5 = 1; + + imeTemp = REG_IME; + REG_IME = 0; + REG_IE |= (INTR_FLAG_TIMER0 << gRfuState->timerSelect); + REG_IE |= INTR_FLAG_SERIAL; + REG_IME = imeTemp; + + REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_MULTI_BUSY | SIO_115200_BPS; + + return 0; +} From dd316b28e017d24567a86e972d780a43d42cd140 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Tue, 12 Sep 2017 20:11:51 -0600 Subject: [PATCH 10/11] Decompile up to ARM functions in librfu, split ARM interrupt functions into their own .s --- asm/librfu.s | 790 ---------------------------------------------- asm/librfu_intr.s | 696 ++++++++++++++++++++++++++++++++++++++++ ld_script.txt | 1 + src/librfu.c | 49 ++- 4 files changed, 744 insertions(+), 792 deletions(-) create mode 100644 asm/librfu_intr.s diff --git a/asm/librfu.s b/asm/librfu.s index 10db8da19..a6d4dc88d 100644 --- a/asm/librfu.s +++ b/asm/librfu.s @@ -4,796 +4,6 @@ .text - thumb_func_start STWI_restart_Command -STWI_restart_Command: @ 82E349C - push {r4,lr} - ldr r4, _082E34B4 - ldr r2, [r4] - ldrb r0, [r2, 0x15] - adds r3, r4, 0 - cmp r0, 0x1 - bhi _082E34B8 - adds r0, 0x1 - strb r0, [r2, 0x15] - bl STWI_start_Command - b _082E3512 - .align 2, 0 -_082E34B4: .4byte gRfuState -_082E34B8: - ldrb r0, [r2, 0x6] - cmp r0, 0x27 - beq _082E34CA - cmp r0, 0x25 - beq _082E34CA - cmp r0, 0x35 - beq _082E34CA - cmp r0, 0x37 - bne _082E34EC -_082E34CA: - ldr r0, [r3] - ldrh r1, [r0, 0x12] - movs r2, 0 - movs r1, 0x1 - strh r1, [r0, 0x12] - adds r0, 0x2C - ldrb r1, [r0] - strb r2, [r0] - ldr r1, [r3] - ldr r2, [r1, 0x18] - cmp r2, 0 - beq _082E3512 - ldrb r0, [r1, 0x6] - ldrh r1, [r1, 0x12] - bl _call_via_r2 - b _082E3512 -_082E34EC: - ldrh r0, [r2, 0x12] - movs r1, 0 - movs r0, 0x1 - strh r0, [r2, 0x12] - adds r0, r2, 0 - adds r0, 0x2C - ldrb r2, [r0] - strb r1, [r0] - ldr r1, [r4] - ldr r2, [r1, 0x18] - cmp r2, 0 - beq _082E350C - ldrb r0, [r1, 0x6] - ldrh r1, [r1, 0x12] - bl _call_via_r2 -_082E350C: - ldr r1, [r4] - movs r0, 0x4 - str r0, [r1] -_082E3512: - movs r0, 0 - pop {r4} - pop {r1} - bx r1 - thumb_func_end STWI_restart_Command - - thumb_func_start STWI_reset_ClockCounter -STWI_reset_ClockCounter: @ 82E351C - ldr r3, _082E3548 - ldr r1, [r3] - movs r0, 0x5 - str r0, [r1] - movs r2, 0 - strb r2, [r1, 0x4] - ldr r0, [r3] - strb r2, [r0, 0x5] - ldr r1, _082E354C - movs r0, 0x80 - lsls r0, 24 - str r0, [r1] - adds r1, 0x8 - strh r2, [r1] - ldr r2, _082E3550 - adds r0, r2, 0 - strh r0, [r1] - adds r2, 0x7F - adds r0, r2, 0 - strh r0, [r1] - movs r0, 0 - bx lr - .align 2, 0 -_082E3548: .4byte gRfuState -_082E354C: .4byte 0x04000120 -_082E3550: .4byte 0x00005003 - thumb_func_end STWI_reset_ClockCounter - - arm_func_start IntrSIO32 -IntrSIO32: @ 82E3554 - mov r12, sp - stmdb sp!, {r11,r12,lr,pc} - ldr r3, _082E35B4 - ldr r0, [r3] - ldr r2, [r0] - sub r11, r12, 0x4 - cmp r2, 0xA - bne _082E3590 - ldr r0, [r0, 0x20] - cmp r0, 0 - ldmdbeq r11, {r11,sp,lr} - bxeq lr - bl sub_82E3EB0 - ldmdb r11, {r11,sp,lr} - bx lr -_082E3590: - ldrb r3, [r0, 0x14] - cmp r3, 0x1 - bne _082E35A8 - bl sio32intr_clock_master - ldmdb r11, {r11,sp,lr} - bx lr -_082E35A8: - bl sio32intr_clock_slave - ldmdb r11, {r11,sp,lr} - bx lr - .align 2, 0 -_082E35B4: .4byte gRfuState - arm_func_end IntrSIO32 - - arm_func_start sio32intr_clock_master -sio32intr_clock_master: @ 82E35B8 - mov r12, sp - stmdb sp!, {r4-r6,r11,r12,lr,pc} - mov r0, 0x50 - sub r11, r12, 0x4 - bl STWI_set_timer_in_RAM - mov r4, 0x120 - ldr r2, _082E382C - add r4, r4, 0x4000000 - ldr lr, [r4] - ldr r12, [r2] - ldr r3, [r12] - mov r6, r2 - cmp r3, 0 - bne _082E3638 - cmp lr, 0x80000000 - bne _082E36B8 - ldrb r2, [r12, 0x5] - ldrb r3, [r12, 0x4] - cmp r2, r3 - bhi _082E3628 - ldr r3, [r12, 0x24] - mov r1, r2 - ldr r2, [r3, r1, lsl 2] - str r2, [r4] - ldrb r3, [r12, 0x5] - add r3, r3, 0x1 - strb r3, [r12, 0x5] - b _082E3714 -_082E3628: - mov r3, 0x1 - str r3, [r12] - str lr, [r4] - b _082E3714 -_082E3638: - ldr r3, [r12] - cmp r3, 0x1 - bne _082E36C8 - mov r3, 0x99000000 - add r3, r3, 0x660000 - mov r5, 0x80000000 - and r2, lr, r5, asr 15 - cmp r2, r3 - bne _082E36B8 - mov r3, 0 - strb r3, [r12, 0x8] - ldr r1, [r6] - ldrb r0, [r1, 0x8] - ldr r2, [r1, 0x28] - str lr, [r2, r0, lsl 2] - ldrb r3, [r1, 0x8] - add r3, r3, 0x1 - strb r3, [r1, 0x8] - ldr r2, [r6] - strb lr, [r2, 0x9] - ldr r3, [r6] - mov r2, lr, lsr 8 - strb r2, [r3, 0x7] - ldr r1, [r6] - ldrb r2, [r1, 0x7] - ldrb r3, [r1, 0x8] - cmp r2, r3 - bcc _082E3700 - mov r3, 0x2 - str r3, [r1] - str r5, [r4] - b _082E3714 -_082E36B8: - bl STWI_stop_timer_in_RAM - mov r0, 0x82 - bl STWI_set_timer_in_RAM - b _082E3840 -_082E36C8: - ldr r3, [r12] - cmp r3, 0x2 - bne _082E3714 - ldrb r1, [r12, 0x8] - ldr r2, [r12, 0x28] - str lr, [r2, r1, lsl 2] - ldrb r3, [r12, 0x8] - add r3, r3, 0x1 - strb r3, [r12, 0x8] - ldr r1, [r6] - ldrb r2, [r1, 0x7] - ldrb r3, [r1, 0x8] - cmp r2, r3 - bcs _082E370C -_082E3700: - mov r3, 0x3 - str r3, [r1] - b _082E3714 -_082E370C: - mov r3, 0x80000000 - str r3, [r4] -_082E3714: - mov r0, 0x1 - bl handshake_wait - mov r0, r0, lsl 16 - cmp r0, 0x10000 - beq _082E3840 - mov r4, 0x128 - add r4, r4, 0x4000000 - mov r5, 0x5000 - add r3, r5, 0xB - strh r3, [r4] - mov r0, 0 - bl handshake_wait - mov r0, r0, lsl 16 - cmp r0, 0x10000 - beq _082E3840 - bl STWI_stop_timer_in_RAM - ldr r1, [r6] - ldr r0, [r1] - cmp r0, 0x3 - bne _082E3830 - ldrb r3, [r1, 0x9] - cmp r3, 0xA5 - cmpne r3, 0xA7 - beq _082E3788 - and r3, r3, 0xFF - cmp r3, 0xB5 - beq _082E3788 - cmp r3, 0xB7 - bne _082E37D0 -_082E3788: - mov r1, 0x120 - add r1, r1, 0x4000000 - mov r12, 0x128 - add r12, r12, 0x4000000 - ldr r0, [r6] - mov r3, 0 - strb r3, [r0, 0x14] - mov r2, 0x80000000 - str r2, [r1] - add r3, r3, 0x5000 - add r2, r3, 0x2 - strh r2, [r12] - add r3, r3, 0x82 - strh r3, [r12] - ldr r2, [r6] - mov r3, 0x5 - str r3, [r2] - b _082E3800 -_082E37D0: - cmp r3, 0xEE - bne _082E37F0 - add r3, r5, 0x3 - strh r3, [r4] - mov r2, 0x4 - str r2, [r1] - strh r0, [r1, 0x12] - b _082E3800 -_082E37F0: - add r3, r5, 0x3 - strh r3, [r4] - mov r2, 0x4 - str r2, [r1] -_082E3800: - ldr r2, [r6] - mov r3, 0 - strb r3, [r2, 0x2C] - ldr r0, [r6] - ldr r2, [r0, 0x18] - cmp r2, r3 - beq _082E3840 - ldrh r1, [r0, 0x12] - ldrb r0, [r0, 0x6] - bl sub_82E3EA8 - b _082E3840 - .align 2, 0 -_082E382C: .4byte gRfuState -_082E3830: - add r3, r5, 0x3 - strh r3, [r4] - add r2, r5, 0x83 - strh r2, [r4] -_082E3840: - ldmdb r11, {r4-r6,r11,sp,lr} - bx lr - arm_func_end sio32intr_clock_master - - arm_func_start sio32intr_clock_slave -sio32intr_clock_slave: @ 82E3848 - mov r12, sp - stmdb sp!, {r4-r6,r11,r12,lr,pc} - ldr r4, _082E3BF4 - mov r0, 0x64 - ldr r3, [r4] - mov r6, 0 - strb r6, [r3, 0x10] - sub r11, r12, 0x4 - bl STWI_set_timer_in_RAM - mov r0, r6 - bl handshake_wait - mov r0, r0, lsl 16 - cmp r0, 0x10000 - mov r5, r4 - beq _082E3C4C - mov r3, 0x128 - add r3, r3, 0x4000000 - mov r2, 0x5000 - add r2, r2, 0xA - strh r2, [r3] - mov lr, 0x120 - ldr r0, [r5] - add lr, lr, 0x4000000 - ldr r12, [lr] - ldr r3, [r0] - cmp r3, 0x5 - bne _082E3978 - ldr r3, [r0, 0x28] - mov r4, 0x1 - mov r0, 0x99000000 - str r12, [r3] - add r0, r0, 0x660000 - ldr r2, [r5] - mov r3, r0, lsr 16 - strb r4, [r2, 0x5] - cmp r3, r12, lsr 16 - bne _082E3AC4 - ldr r3, [r5] - mov r2, r12, lsr 8 - strb r2, [r3, 0x4] - ldr r2, [r5] - strb r12, [r2, 0x6] - ldr r1, [r5] - ldrb r3, [r1, 0x4] - cmp r3, r6 - bne _082E395C - ldrb r2, [r1, 0x6] - sub r3, r2, 0x27 - cmp r2, 0x36 - cmpne r3, 0x2 - bhi _082E3930 - add r3, r2, 0x80 - strb r3, [r1, 0x9] - ldr r2, [r5] - ldrb r3, [r2, 0x9] - ldr r1, [r2, 0x24] - add r3, r3, r0 - b _082E39E0 -_082E3930: - ldr r2, [r1, 0x24] - ldr r3, _082E3BF8 - str r3, [r2] - ldr r2, [r5] - ldrb r3, [r2, 0x6] - sub r3, r3, 0x10 - cmp r3, 0x2D - bhi _082E3A18 - ldr r3, [r2, 0x24] - str r4, [r3, 0x4] - b _082E3A24 -_082E395C: - mov r3, 0x80000000 - str r3, [lr] - strb r4, [r1, 0x5] - ldr r2, [r5] - add r3, r3, 0x80000006 - str r3, [r2] - b _082E3AD4 -_082E3978: - ldr r3, [r0] - cmp r3, 0x6 - bne _082E3A78 - ldrb r1, [r0, 0x5] - ldr r2, [r0, 0x28] - str r12, [r2, r1, lsl 2] - ldrb r3, [r0, 0x5] - add r3, r3, 0x1 - strb r3, [r0, 0x5] - ldr r1, [r5] - ldrb r2, [r1, 0x4] - ldrb r3, [r1, 0x5] - cmp r2, r3 - bcs _082E3A6C - ldrb r2, [r1, 0x6] - sub r3, r2, 0x28 - cmp r2, 0x36 - cmpne r3, 0x1 - bhi _082E39F0 - add r3, r2, 0x80 - strb r3, [r1, 0x9] - ldr r2, [r5] - ldrb r3, [r2, 0x9] - ldr r1, [r2, 0x24] - orr r3, r3, 0x99000000 - orr r3, r3, 0x660000 -_082E39E0: - str r3, [r1] - ldr r2, [r5] - strb r6, [r2, 0x7] - b _082E3A3C -_082E39F0: - ldr r2, [r1, 0x24] - ldr r3, _082E3BF8 - str r3, [r2] - ldr r2, [r5] - ldrb r3, [r2, 0x6] - sub r3, r3, 0x10 - cmp r3, 0x2D - ldrls r2, [r2, 0x24] - movls r3, 0x1 - bls _082E3A20 -_082E3A18: - ldr r2, [r2, 0x24] - mov r3, 0x2 -_082E3A20: - str r3, [r2, 0x4] -_082E3A24: - ldr r2, [r5] - mov r3, 0x1 - strb r3, [r2, 0x7] - ldr r1, [r5] - add r3, r3, 0x2 - strh r3, [r1, 0x12] -_082E3A3C: - ldr r0, [r5] - ldr r2, [r0, 0x24] - mov r3, 0x120 - ldr r1, [r2] - add r3, r3, 0x4000000 - str r1, [r3] - mov r2, 0x1 - strb r2, [r0, 0x8] - ldr r1, [r5] - mov r3, 0x7 - str r3, [r1] - b _082E3AD4 -_082E3A6C: - mov r3, 0x80000000 - str r3, [lr] - b _082E3AD4 -_082E3A78: - ldr r3, [r0] - cmp r3, 0x7 - bne _082E3AD4 - cmp r12, 0x80000000 - bne _082E3AC4 - ldrb r2, [r0, 0x7] - ldrb r3, [r0, 0x8] - cmp r2, r3 - movcc r3, 0x8 - strcc r3, [r0] - bcc _082E3AD4 - ldrb r1, [r0, 0x8] - ldr r3, [r0, 0x24] - ldr r2, [r3, r1, lsl 2] - str r2, [lr] - ldrb r3, [r0, 0x8] - add r3, r3, 0x1 - strb r3, [r0, 0x8] - b _082E3AD4 -_082E3AC4: - bl STWI_stop_timer_in_RAM - mov r0, 0x64 - bl STWI_set_timer_in_RAM - b _082E3C4C -_082E3AD4: - mov r0, 0x1 - bl handshake_wait - mov r0, r0, lsl 16 - cmp r0, 0x10000 - beq _082E3C4C - mov r6, r5 - ldr r3, [r6] - ldr r2, [r3] - cmp r2, 0x8 - bne _082E3B9C - mov r4, 0x128 - add r4, r4, 0x4000000 - mov r3, 0x5000 - add r3, r3, 0x2 - strh r3, [r4] - bl STWI_stop_timer_in_RAM - ldr r0, [r6] - ldrh r3, [r0, 0x12] - cmp r3, 0x3 - bne _082E3B48 - bl STWI_init_slave - ldr r3, [r6] - ldr r1, [r3, 0x1C] - cmp r1, 0 - beq _082E3C4C - mov r0, 0x1EC - add r0, r0, 0x2 - bl sub_82E3EAC - b _082E3C4C -_082E3B48: - mov r3, 0x120 - add r3, r3, 0x4000000 - mov r1, 0 - str r1, [r3] - mov r2, 0x5000 - strh r1, [r4] - add r2, r2, 0x3 - strh r2, [r4] - mov r3, 0x1 - strb r3, [r0, 0x14] - ldr r0, [r5] - ldr r2, [r0, 0x1C] - str r1, [r0] - cmp r2, r1 - beq _082E3C4C - ldrb r3, [r0, 0x4] - ldrb r0, [r0, 0x6] - mov r1, r2 - orr r0, r0, r3, lsl 8 - bl sub_82E3EAC - b _082E3C4C -_082E3B9C: - mov r3, 0x208 - add r3, r3, 0x4000000 - mov r2, 0 - strh r2, [r3] - mov r1, 0x100 - add r2, r1, 0x4000002 - ldrh r3, [r2] - tst r3, 0x80 - beq _082E3C20 - ldrh r3, [r2] - tst r3, 0x3 - bne _082E3BFC - mov r2, 0xFF00 - add r1, r1, 0x4000000 - ldrh r3, [r1] - add r2, r2, 0x9B - cmp r3, r2 - bls _082E3C20 -_082E3BE4: - ldrh r3, [r1] - cmp r3, r2 - bhi _082E3BE4 - b _082E3C20 - .align 2, 0 -_082E3BF4: .4byte gRfuState -_082E3BF8: .4byte 0x996601ee -_082E3BFC: - mov r2, 0xFF00 - add r1, r1, 0x4000000 - ldrh r3, [r1] - add r2, r2, 0xFE - cmp r3, r2 - bls _082E3C20 -_082E3C14: - ldrh r3, [r1] - cmp r3, r2 - bhi _082E3C14 -_082E3C20: - mov r1, 0x128 - add r1, r1, 0x4000000 - mov r0, 0x208 - add r0, r0, 0x4000000 - mov r3, 0x5000 - add r2, r3, 0x2 - strh r2, [r1] - add r3, r3, 0x82 - strh r3, [r1] - mov r2, 0x1 - strh r2, [r0] -_082E3C4C: - ldmdb r11, {r4-r6,r11,sp,lr} - bx lr - arm_func_end sio32intr_clock_slave - - arm_func_start handshake_wait -handshake_wait: @ 82E3C54 - mov r12, sp - stmdb sp!, {r11,r12,lr,pc} - mov r1, 0x128 - add r1, r1, 0x4000000 - mov r0, r0, lsl 16 - ldr r2, _082E3CB8 - sub r11, r12, 0x4 - mov lr, r0, lsr 14 - ldr r12, [r2] -_082E3C78: - ldrb r3, [r12, 0x10] - and r0, r3, 0xFF - cmp r0, 0x1 - beq _082E3CA4 - ldrh r3, [r1] - and r3, r3, 0x4 - cmp r3, lr - bne _082E3C78 - mov r0, 0 - ldmdb r11, {r11,sp,lr} - bx lr -_082E3CA4: - ldr r2, [r2] - mov r3, 0 - strb r3, [r2, 0x10] - ldmdb r11, {r11,sp,lr} - bx lr - .align 2, 0 -_082E3CB8: .4byte gRfuState - arm_func_end handshake_wait - - arm_func_start STWI_set_timer_in_RAM -STWI_set_timer_in_RAM: @ 82E3CBC - mov r12, sp - stmdb sp!, {r4,r5,r11,r12,lr,pc} - mov r1, 0x208 - add r1, r1, 0x4000000 - mov r3, 0 - sub r11, r12, 0x4 - ldr r12, _082E3D74 - and lr, r0, 0xFF - ldr r2, [r12] - cmp lr, 0x50 - ldrb r0, [r2, 0xA] - mov r4, r12 - mov r2, lr - strh r3, [r1] - mov r0, r0, lsl 2 - add r3, r3, 0x100 - add r1, r3, 0x4000000 - add r3, r3, 0x4000002 - add r5, r0, r3 - beq _082E3D44 - bgt _082E3D1C - cmp lr, 0x32 - beq _082E3D30 - b _082E3D90 -_082E3D1C: - cmp r2, 0x64 - beq _082E3D5C - cmp r2, 0x82 - beq _082E3D78 - b _082E3D90 -_082E3D30: - mvn r3, 0x334 - strh r3, [r0, r1] - ldr r2, [r4] - mov r3, 0x1 - b _082E3D8C -_082E3D44: - mov r3, 0xAE000000 - mov r3, r3, asr 20 - strh r3, [r0, r1] - ldr r2, [r4] - mov r3, 0x2 - b _082E3D8C -_082E3D5C: - mvn r3, 0x660 - sub r3, r3, 0x9 - strh r3, [r0, r1] - ldr r2, [r4] - mov r3, 0x3 - b _082E3D8C - .align 2, 0 -_082E3D74: .4byte gRfuState -_082E3D78: - mvn r3, 0x850 - sub r3, r3, 0x2 - strh r3, [r0, r1] - ldr r2, [r4] - mov r3, 0x4 -_082E3D8C: - str r3, [r2, 0xC] -_082E3D90: - mov r12, 0x200 - add r12, r12, 0x4000002 - mov r3, 0xC3 - strh r3, [r5] - mov r1, 0x208 - ldr r2, [r4] - add r1, r1, 0x4000000 - ldrb r0, [r2, 0xA] - sub r3, r3, 0xBB - mov r3, r3, lsl r0 - strh r3, [r12] - mov r2, 0x1 - strh r2, [r1] - ldmdb r11, {r4,r5,r11,sp,lr} - bx lr - arm_func_end STWI_set_timer_in_RAM - - arm_func_start STWI_stop_timer_in_RAM -STWI_stop_timer_in_RAM: @ 82E3DCC - mov r12, sp - stmdb sp!, {r11,r12,lr,pc} - mov r1, 0x100 - ldr lr, _082E3E18 - add r0, r1, 0x4000000 - ldr r2, [lr] - sub r11, r12, 0x4 - ldrb r3, [r2, 0xA] - mov r12, 0 - str r12, [r2, 0xC] - mov r3, r3, lsl 2 - strh r12, [r3, r0] - ldr r2, [lr] - ldrb r3, [r2, 0xA] - add r1, r1, 0x4000002 - mov r3, r3, lsl 2 - strh r12, [r3, r1] - ldmdb r11, {r11,sp,lr} - bx lr - .align 2, 0 -_082E3E18: .4byte gRfuState - arm_func_end STWI_stop_timer_in_RAM - - arm_func_start STWI_init_slave -STWI_init_slave: @ 82E3E1C - mov r12, sp - stmdb sp!, {r11,r12,lr,pc} - ldr r0, _082E3EA4 - ldr r2, [r0] - mov r3, 0x5 - str r3, [r2] - mov r1, 0 - strb r1, [r2, 0x14] - ldr r3, [r0] - strb r1, [r3, 0x4] - ldr r2, [r0] - strb r1, [r2, 0x5] - ldr r3, [r0] - strb r1, [r3, 0x6] - ldr r2, [r0] - strb r1, [r2, 0x7] - ldr r3, [r0] - strb r1, [r3, 0x8] - ldr r2, [r0] - strb r1, [r2, 0x9] - ldr r3, [r0] - str r1, [r3, 0xC] - sub r11, r12, 0x4 - strb r1, [r3, 0x10] - mov r2, 0x128 - ldr r12, [r0] - add r2, r2, 0x4000000 - strh r1, [r12, 0x12] - mov r3, 0x5000 - strb r1, [r12, 0x15] - add r3, r3, 0x82 - strh r3, [r2] - ldmdb r11, {r11,sp,lr} - bx lr - .align 2, 0 -_082E3EA4: .4byte gRfuState - arm_func_end STWI_init_slave - - arm_func_start sub_82E3EA8 -sub_82E3EA8: @ 82E3EA8 - bx r2 - arm_func_end sub_82E3EA8 - - arm_func_start sub_82E3EAC -sub_82E3EAC: @ 82E3EAC - bx r1 - arm_func_end sub_82E3EAC - - arm_func_start sub_82E3EB0 -sub_82E3EB0: @ 82E3EB0 - bx r0 - arm_func_end sub_82E3EB0 - thumb_func_start rfu_initializeAPI rfu_initializeAPI: @ 82E3EB4 push {r4-r7,lr} diff --git a/asm/librfu_intr.s b/asm/librfu_intr.s new file mode 100644 index 000000000..7f375e419 --- /dev/null +++ b/asm/librfu_intr.s @@ -0,0 +1,696 @@ + .include "asm/macros.inc" + + .syntax unified + + .text + + arm_func_start IntrSIO32 +IntrSIO32: @ 82E3554 + mov r12, sp + stmdb sp!, {r11,r12,lr,pc} + ldr r3, _082E35B4 + ldr r0, [r3] + ldr r2, [r0] + sub r11, r12, 0x4 + cmp r2, 0xA + bne _082E3590 + ldr r0, [r0, 0x20] + cmp r0, 0 + ldmdbeq r11, {r11,sp,lr} + bxeq lr + bl sub_82E3EB0 + ldmdb r11, {r11,sp,lr} + bx lr +_082E3590: + ldrb r3, [r0, 0x14] + cmp r3, 0x1 + bne _082E35A8 + bl sio32intr_clock_master + ldmdb r11, {r11,sp,lr} + bx lr +_082E35A8: + bl sio32intr_clock_slave + ldmdb r11, {r11,sp,lr} + bx lr + .align 2, 0 +_082E35B4: .4byte gRfuState + arm_func_end IntrSIO32 + + arm_func_start sio32intr_clock_master +sio32intr_clock_master: @ 82E35B8 + mov r12, sp + stmdb sp!, {r4-r6,r11,r12,lr,pc} + mov r0, 0x50 + sub r11, r12, 0x4 + bl STWI_set_timer_in_RAM + mov r4, 0x120 + ldr r2, _082E382C + add r4, r4, 0x4000000 + ldr lr, [r4] + ldr r12, [r2] + ldr r3, [r12] + mov r6, r2 + cmp r3, 0 + bne _082E3638 + cmp lr, 0x80000000 + bne _082E36B8 + ldrb r2, [r12, 0x5] + ldrb r3, [r12, 0x4] + cmp r2, r3 + bhi _082E3628 + ldr r3, [r12, 0x24] + mov r1, r2 + ldr r2, [r3, r1, lsl 2] + str r2, [r4] + ldrb r3, [r12, 0x5] + add r3, r3, 0x1 + strb r3, [r12, 0x5] + b _082E3714 +_082E3628: + mov r3, 0x1 + str r3, [r12] + str lr, [r4] + b _082E3714 +_082E3638: + ldr r3, [r12] + cmp r3, 0x1 + bne _082E36C8 + mov r3, 0x99000000 + add r3, r3, 0x660000 + mov r5, 0x80000000 + and r2, lr, r5, asr 15 + cmp r2, r3 + bne _082E36B8 + mov r3, 0 + strb r3, [r12, 0x8] + ldr r1, [r6] + ldrb r0, [r1, 0x8] + ldr r2, [r1, 0x28] + str lr, [r2, r0, lsl 2] + ldrb r3, [r1, 0x8] + add r3, r3, 0x1 + strb r3, [r1, 0x8] + ldr r2, [r6] + strb lr, [r2, 0x9] + ldr r3, [r6] + mov r2, lr, lsr 8 + strb r2, [r3, 0x7] + ldr r1, [r6] + ldrb r2, [r1, 0x7] + ldrb r3, [r1, 0x8] + cmp r2, r3 + bcc _082E3700 + mov r3, 0x2 + str r3, [r1] + str r5, [r4] + b _082E3714 +_082E36B8: + bl STWI_stop_timer_in_RAM + mov r0, 0x82 + bl STWI_set_timer_in_RAM + b _082E3840 +_082E36C8: + ldr r3, [r12] + cmp r3, 0x2 + bne _082E3714 + ldrb r1, [r12, 0x8] + ldr r2, [r12, 0x28] + str lr, [r2, r1, lsl 2] + ldrb r3, [r12, 0x8] + add r3, r3, 0x1 + strb r3, [r12, 0x8] + ldr r1, [r6] + ldrb r2, [r1, 0x7] + ldrb r3, [r1, 0x8] + cmp r2, r3 + bcs _082E370C +_082E3700: + mov r3, 0x3 + str r3, [r1] + b _082E3714 +_082E370C: + mov r3, 0x80000000 + str r3, [r4] +_082E3714: + mov r0, 0x1 + bl handshake_wait + mov r0, r0, lsl 16 + cmp r0, 0x10000 + beq _082E3840 + mov r4, 0x128 + add r4, r4, 0x4000000 + mov r5, 0x5000 + add r3, r5, 0xB + strh r3, [r4] + mov r0, 0 + bl handshake_wait + mov r0, r0, lsl 16 + cmp r0, 0x10000 + beq _082E3840 + bl STWI_stop_timer_in_RAM + ldr r1, [r6] + ldr r0, [r1] + cmp r0, 0x3 + bne _082E3830 + ldrb r3, [r1, 0x9] + cmp r3, 0xA5 + cmpne r3, 0xA7 + beq _082E3788 + and r3, r3, 0xFF + cmp r3, 0xB5 + beq _082E3788 + cmp r3, 0xB7 + bne _082E37D0 +_082E3788: + mov r1, 0x120 + add r1, r1, 0x4000000 + mov r12, 0x128 + add r12, r12, 0x4000000 + ldr r0, [r6] + mov r3, 0 + strb r3, [r0, 0x14] + mov r2, 0x80000000 + str r2, [r1] + add r3, r3, 0x5000 + add r2, r3, 0x2 + strh r2, [r12] + add r3, r3, 0x82 + strh r3, [r12] + ldr r2, [r6] + mov r3, 0x5 + str r3, [r2] + b _082E3800 +_082E37D0: + cmp r3, 0xEE + bne _082E37F0 + add r3, r5, 0x3 + strh r3, [r4] + mov r2, 0x4 + str r2, [r1] + strh r0, [r1, 0x12] + b _082E3800 +_082E37F0: + add r3, r5, 0x3 + strh r3, [r4] + mov r2, 0x4 + str r2, [r1] +_082E3800: + ldr r2, [r6] + mov r3, 0 + strb r3, [r2, 0x2C] + ldr r0, [r6] + ldr r2, [r0, 0x18] + cmp r2, r3 + beq _082E3840 + ldrh r1, [r0, 0x12] + ldrb r0, [r0, 0x6] + bl sub_82E3EA8 + b _082E3840 + .align 2, 0 +_082E382C: .4byte gRfuState +_082E3830: + add r3, r5, 0x3 + strh r3, [r4] + add r2, r5, 0x83 + strh r2, [r4] +_082E3840: + ldmdb r11, {r4-r6,r11,sp,lr} + bx lr + arm_func_end sio32intr_clock_master + + arm_func_start sio32intr_clock_slave +sio32intr_clock_slave: @ 82E3848 + mov r12, sp + stmdb sp!, {r4-r6,r11,r12,lr,pc} + ldr r4, _082E3BF4 + mov r0, 0x64 + ldr r3, [r4] + mov r6, 0 + strb r6, [r3, 0x10] + sub r11, r12, 0x4 + bl STWI_set_timer_in_RAM + mov r0, r6 + bl handshake_wait + mov r0, r0, lsl 16 + cmp r0, 0x10000 + mov r5, r4 + beq _082E3C4C + mov r3, 0x128 + add r3, r3, 0x4000000 + mov r2, 0x5000 + add r2, r2, 0xA + strh r2, [r3] + mov lr, 0x120 + ldr r0, [r5] + add lr, lr, 0x4000000 + ldr r12, [lr] + ldr r3, [r0] + cmp r3, 0x5 + bne _082E3978 + ldr r3, [r0, 0x28] + mov r4, 0x1 + mov r0, 0x99000000 + str r12, [r3] + add r0, r0, 0x660000 + ldr r2, [r5] + mov r3, r0, lsr 16 + strb r4, [r2, 0x5] + cmp r3, r12, lsr 16 + bne _082E3AC4 + ldr r3, [r5] + mov r2, r12, lsr 8 + strb r2, [r3, 0x4] + ldr r2, [r5] + strb r12, [r2, 0x6] + ldr r1, [r5] + ldrb r3, [r1, 0x4] + cmp r3, r6 + bne _082E395C + ldrb r2, [r1, 0x6] + sub r3, r2, 0x27 + cmp r2, 0x36 + cmpne r3, 0x2 + bhi _082E3930 + add r3, r2, 0x80 + strb r3, [r1, 0x9] + ldr r2, [r5] + ldrb r3, [r2, 0x9] + ldr r1, [r2, 0x24] + add r3, r3, r0 + b _082E39E0 +_082E3930: + ldr r2, [r1, 0x24] + ldr r3, _082E3BF8 + str r3, [r2] + ldr r2, [r5] + ldrb r3, [r2, 0x6] + sub r3, r3, 0x10 + cmp r3, 0x2D + bhi _082E3A18 + ldr r3, [r2, 0x24] + str r4, [r3, 0x4] + b _082E3A24 +_082E395C: + mov r3, 0x80000000 + str r3, [lr] + strb r4, [r1, 0x5] + ldr r2, [r5] + add r3, r3, 0x80000006 + str r3, [r2] + b _082E3AD4 +_082E3978: + ldr r3, [r0] + cmp r3, 0x6 + bne _082E3A78 + ldrb r1, [r0, 0x5] + ldr r2, [r0, 0x28] + str r12, [r2, r1, lsl 2] + ldrb r3, [r0, 0x5] + add r3, r3, 0x1 + strb r3, [r0, 0x5] + ldr r1, [r5] + ldrb r2, [r1, 0x4] + ldrb r3, [r1, 0x5] + cmp r2, r3 + bcs _082E3A6C + ldrb r2, [r1, 0x6] + sub r3, r2, 0x28 + cmp r2, 0x36 + cmpne r3, 0x1 + bhi _082E39F0 + add r3, r2, 0x80 + strb r3, [r1, 0x9] + ldr r2, [r5] + ldrb r3, [r2, 0x9] + ldr r1, [r2, 0x24] + orr r3, r3, 0x99000000 + orr r3, r3, 0x660000 +_082E39E0: + str r3, [r1] + ldr r2, [r5] + strb r6, [r2, 0x7] + b _082E3A3C +_082E39F0: + ldr r2, [r1, 0x24] + ldr r3, _082E3BF8 + str r3, [r2] + ldr r2, [r5] + ldrb r3, [r2, 0x6] + sub r3, r3, 0x10 + cmp r3, 0x2D + ldrls r2, [r2, 0x24] + movls r3, 0x1 + bls _082E3A20 +_082E3A18: + ldr r2, [r2, 0x24] + mov r3, 0x2 +_082E3A20: + str r3, [r2, 0x4] +_082E3A24: + ldr r2, [r5] + mov r3, 0x1 + strb r3, [r2, 0x7] + ldr r1, [r5] + add r3, r3, 0x2 + strh r3, [r1, 0x12] +_082E3A3C: + ldr r0, [r5] + ldr r2, [r0, 0x24] + mov r3, 0x120 + ldr r1, [r2] + add r3, r3, 0x4000000 + str r1, [r3] + mov r2, 0x1 + strb r2, [r0, 0x8] + ldr r1, [r5] + mov r3, 0x7 + str r3, [r1] + b _082E3AD4 +_082E3A6C: + mov r3, 0x80000000 + str r3, [lr] + b _082E3AD4 +_082E3A78: + ldr r3, [r0] + cmp r3, 0x7 + bne _082E3AD4 + cmp r12, 0x80000000 + bne _082E3AC4 + ldrb r2, [r0, 0x7] + ldrb r3, [r0, 0x8] + cmp r2, r3 + movcc r3, 0x8 + strcc r3, [r0] + bcc _082E3AD4 + ldrb r1, [r0, 0x8] + ldr r3, [r0, 0x24] + ldr r2, [r3, r1, lsl 2] + str r2, [lr] + ldrb r3, [r0, 0x8] + add r3, r3, 0x1 + strb r3, [r0, 0x8] + b _082E3AD4 +_082E3AC4: + bl STWI_stop_timer_in_RAM + mov r0, 0x64 + bl STWI_set_timer_in_RAM + b _082E3C4C +_082E3AD4: + mov r0, 0x1 + bl handshake_wait + mov r0, r0, lsl 16 + cmp r0, 0x10000 + beq _082E3C4C + mov r6, r5 + ldr r3, [r6] + ldr r2, [r3] + cmp r2, 0x8 + bne _082E3B9C + mov r4, 0x128 + add r4, r4, 0x4000000 + mov r3, 0x5000 + add r3, r3, 0x2 + strh r3, [r4] + bl STWI_stop_timer_in_RAM + ldr r0, [r6] + ldrh r3, [r0, 0x12] + cmp r3, 0x3 + bne _082E3B48 + bl STWI_init_slave + ldr r3, [r6] + ldr r1, [r3, 0x1C] + cmp r1, 0 + beq _082E3C4C + mov r0, 0x1EC + add r0, r0, 0x2 + bl sub_82E3EAC + b _082E3C4C +_082E3B48: + mov r3, 0x120 + add r3, r3, 0x4000000 + mov r1, 0 + str r1, [r3] + mov r2, 0x5000 + strh r1, [r4] + add r2, r2, 0x3 + strh r2, [r4] + mov r3, 0x1 + strb r3, [r0, 0x14] + ldr r0, [r5] + ldr r2, [r0, 0x1C] + str r1, [r0] + cmp r2, r1 + beq _082E3C4C + ldrb r3, [r0, 0x4] + ldrb r0, [r0, 0x6] + mov r1, r2 + orr r0, r0, r3, lsl 8 + bl sub_82E3EAC + b _082E3C4C +_082E3B9C: + mov r3, 0x208 + add r3, r3, 0x4000000 + mov r2, 0 + strh r2, [r3] + mov r1, 0x100 + add r2, r1, 0x4000002 + ldrh r3, [r2] + tst r3, 0x80 + beq _082E3C20 + ldrh r3, [r2] + tst r3, 0x3 + bne _082E3BFC + mov r2, 0xFF00 + add r1, r1, 0x4000000 + ldrh r3, [r1] + add r2, r2, 0x9B + cmp r3, r2 + bls _082E3C20 +_082E3BE4: + ldrh r3, [r1] + cmp r3, r2 + bhi _082E3BE4 + b _082E3C20 + .align 2, 0 +_082E3BF4: .4byte gRfuState +_082E3BF8: .4byte 0x996601ee +_082E3BFC: + mov r2, 0xFF00 + add r1, r1, 0x4000000 + ldrh r3, [r1] + add r2, r2, 0xFE + cmp r3, r2 + bls _082E3C20 +_082E3C14: + ldrh r3, [r1] + cmp r3, r2 + bhi _082E3C14 +_082E3C20: + mov r1, 0x128 + add r1, r1, 0x4000000 + mov r0, 0x208 + add r0, r0, 0x4000000 + mov r3, 0x5000 + add r2, r3, 0x2 + strh r2, [r1] + add r3, r3, 0x82 + strh r3, [r1] + mov r2, 0x1 + strh r2, [r0] +_082E3C4C: + ldmdb r11, {r4-r6,r11,sp,lr} + bx lr + arm_func_end sio32intr_clock_slave + + arm_func_start handshake_wait +handshake_wait: @ 82E3C54 + mov r12, sp + stmdb sp!, {r11,r12,lr,pc} + mov r1, 0x128 + add r1, r1, 0x4000000 + mov r0, r0, lsl 16 + ldr r2, _082E3CB8 + sub r11, r12, 0x4 + mov lr, r0, lsr 14 + ldr r12, [r2] +_082E3C78: + ldrb r3, [r12, 0x10] + and r0, r3, 0xFF + cmp r0, 0x1 + beq _082E3CA4 + ldrh r3, [r1] + and r3, r3, 0x4 + cmp r3, lr + bne _082E3C78 + mov r0, 0 + ldmdb r11, {r11,sp,lr} + bx lr +_082E3CA4: + ldr r2, [r2] + mov r3, 0 + strb r3, [r2, 0x10] + ldmdb r11, {r11,sp,lr} + bx lr + .align 2, 0 +_082E3CB8: .4byte gRfuState + arm_func_end handshake_wait + + arm_func_start STWI_set_timer_in_RAM +STWI_set_timer_in_RAM: @ 82E3CBC + mov r12, sp + stmdb sp!, {r4,r5,r11,r12,lr,pc} + mov r1, 0x208 + add r1, r1, 0x4000000 + mov r3, 0 + sub r11, r12, 0x4 + ldr r12, _082E3D74 + and lr, r0, 0xFF + ldr r2, [r12] + cmp lr, 0x50 + ldrb r0, [r2, 0xA] + mov r4, r12 + mov r2, lr + strh r3, [r1] + mov r0, r0, lsl 2 + add r3, r3, 0x100 + add r1, r3, 0x4000000 + add r3, r3, 0x4000002 + add r5, r0, r3 + beq _082E3D44 + bgt _082E3D1C + cmp lr, 0x32 + beq _082E3D30 + b _082E3D90 +_082E3D1C: + cmp r2, 0x64 + beq _082E3D5C + cmp r2, 0x82 + beq _082E3D78 + b _082E3D90 +_082E3D30: + mvn r3, 0x334 + strh r3, [r0, r1] + ldr r2, [r4] + mov r3, 0x1 + b _082E3D8C +_082E3D44: + mov r3, 0xAE000000 + mov r3, r3, asr 20 + strh r3, [r0, r1] + ldr r2, [r4] + mov r3, 0x2 + b _082E3D8C +_082E3D5C: + mvn r3, 0x660 + sub r3, r3, 0x9 + strh r3, [r0, r1] + ldr r2, [r4] + mov r3, 0x3 + b _082E3D8C + .align 2, 0 +_082E3D74: .4byte gRfuState +_082E3D78: + mvn r3, 0x850 + sub r3, r3, 0x2 + strh r3, [r0, r1] + ldr r2, [r4] + mov r3, 0x4 +_082E3D8C: + str r3, [r2, 0xC] +_082E3D90: + mov r12, 0x200 + add r12, r12, 0x4000002 + mov r3, 0xC3 + strh r3, [r5] + mov r1, 0x208 + ldr r2, [r4] + add r1, r1, 0x4000000 + ldrb r0, [r2, 0xA] + sub r3, r3, 0xBB + mov r3, r3, lsl r0 + strh r3, [r12] + mov r2, 0x1 + strh r2, [r1] + ldmdb r11, {r4,r5,r11,sp,lr} + bx lr + arm_func_end STWI_set_timer_in_RAM + + arm_func_start STWI_stop_timer_in_RAM +STWI_stop_timer_in_RAM: @ 82E3DCC + mov r12, sp + stmdb sp!, {r11,r12,lr,pc} + mov r1, 0x100 + ldr lr, _082E3E18 + add r0, r1, 0x4000000 + ldr r2, [lr] + sub r11, r12, 0x4 + ldrb r3, [r2, 0xA] + mov r12, 0 + str r12, [r2, 0xC] + mov r3, r3, lsl 2 + strh r12, [r3, r0] + ldr r2, [lr] + ldrb r3, [r2, 0xA] + add r1, r1, 0x4000002 + mov r3, r3, lsl 2 + strh r12, [r3, r1] + ldmdb r11, {r11,sp,lr} + bx lr + .align 2, 0 +_082E3E18: .4byte gRfuState + arm_func_end STWI_stop_timer_in_RAM + + arm_func_start STWI_init_slave +STWI_init_slave: @ 82E3E1C + mov r12, sp + stmdb sp!, {r11,r12,lr,pc} + ldr r0, _082E3EA4 + ldr r2, [r0] + mov r3, 0x5 + str r3, [r2] + mov r1, 0 + strb r1, [r2, 0x14] + ldr r3, [r0] + strb r1, [r3, 0x4] + ldr r2, [r0] + strb r1, [r2, 0x5] + ldr r3, [r0] + strb r1, [r3, 0x6] + ldr r2, [r0] + strb r1, [r2, 0x7] + ldr r3, [r0] + strb r1, [r3, 0x8] + ldr r2, [r0] + strb r1, [r2, 0x9] + ldr r3, [r0] + str r1, [r3, 0xC] + sub r11, r12, 0x4 + strb r1, [r3, 0x10] + mov r2, 0x128 + ldr r12, [r0] + add r2, r2, 0x4000000 + strh r1, [r12, 0x12] + mov r3, 0x5000 + strb r1, [r12, 0x15] + add r3, r3, 0x82 + strh r3, [r2] + ldmdb r11, {r11,sp,lr} + bx lr + .align 2, 0 +_082E3EA4: .4byte gRfuState + arm_func_end STWI_init_slave + + arm_func_start sub_82E3EA8 +sub_82E3EA8: @ 82E3EA8 + bx r2 + arm_func_end sub_82E3EA8 + + arm_func_start sub_82E3EAC +sub_82E3EAC: @ 82E3EAC + bx r1 + arm_func_end sub_82E3EAC + + arm_func_start sub_82E3EB0 +sub_82E3EB0: @ 82E3EB0 + bx r0 + arm_func_end sub_82E3EB0 diff --git a/ld_script.txt b/ld_script.txt index 301f199f9..450e9125f 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -245,6 +245,7 @@ SECTIONS { src/agb_flash_mx.o(.text); src/siirtc.o(.text); src/librfu.o(.text); + asm/librfu_intr.o(.text); asm/librfu.o(.text); asm/libagbsyscall.o(.text); tools/agbcc/lib/libgcc.a:_call_via_rX.o(.text); diff --git a/src/librfu.c b/src/librfu.c index 37b76ec3e..e647b9178 100644 --- a/src/librfu.c +++ b/src/librfu.c @@ -115,8 +115,8 @@ int STWI_start_Command(void); void STWI_intr_timer(void); void STWI_set_timer(u8 unk); extern void STWI_stop_timer(void); -extern void STWI_restart_Command(void); -extern void STWI_reset_ClockCounter(void); +int STWI_restart_Command(void); +int STWI_reset_ClockCounter(void); void STWI_init_all(struct RfuIntrStruct *interruptStruct, IntrFunc *interrupt, bool8 copyInterruptToRam) { @@ -741,3 +741,48 @@ int STWI_start_Command() return 0; } + +int STWI_restart_Command(void) +{ + if (gRfuState->unk_15 <= 1) + { + gRfuState->unk_15++; + STWI_start_Command(); + } + else + { + if (gRfuState->activeCommand == RFU_MS_CHANGE || gRfuState->activeCommand == RFU_DATA_TX_AND_CHANGE || gRfuState->activeCommand == RFU_UNK35 || gRfuState->activeCommand == RFU_RESUME_RETRANSMIT_AND_CHANGE) + { + gRfuState->unk_12 = 1; + gRfuState->unk_2c = 0; + + if (gRfuState->callbackM) + gRfuState->callbackM(gRfuState->activeCommand, gRfuState->unk_12); + } + else + { + gRfuState->unk_12 = 1; + gRfuState->unk_2c = 0; + + if (gRfuState->callbackM) + gRfuState->callbackM(gRfuState->activeCommand, gRfuState->unk_12); + + gRfuState->unk_0 = 4; //TODO: what's 4 + } + } + + return 0; +} + +int STWI_reset_ClockCounter() +{ + gRfuState->unk_0 = 5; //TODO: what is 5 + gRfuState->txParams = 0; + gRfuState->unk_5 = 0; + REG_SIODATA32 = (1 << 31); + REG_SIOCNT = 0; + REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS; + REG_SIOCNT = (SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS) + 0x7F; + + return 0; +} From 482b42a295a80952cea75a8176183692b8e8d120 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Sat, 21 Oct 2017 13:42:31 -0600 Subject: [PATCH 11/11] Split librfu into separate C files in its own directory, rfu_initializeAPI matching except for register differences --- include/librfu.h | 108 +++++++++++++++++++++++++++++++ ld_script.txt | 4 +- src/librfu_intr.c | 4 ++ src/librfu_rfu.c | 109 +++++++++++++++++++++++++++++++ src/{librfu.c => librfu_stwi.c} | 111 ++------------------------------ 5 files changed, 229 insertions(+), 107 deletions(-) create mode 100644 include/librfu.h create mode 100644 src/librfu_intr.c create mode 100644 src/librfu_rfu.c rename src/{librfu.c => librfu_stwi.c} (89%) diff --git a/include/librfu.h b/include/librfu.h new file mode 100644 index 000000000..dbc8a41a6 --- /dev/null +++ b/include/librfu.h @@ -0,0 +1,108 @@ +#include "main.h" + +enum +{ + RFU_RESET = 0x10, + RFU_LINK_STATUS, + RFU_VERSION_STATUS, + RFU_SYSTEM_STATUS, + RFU_SLOT_STATUS, + RFU_CONFIG_STATUS, + RFU_GAME_CONFIG, + RFU_SYSTEM_CONFIG, + RFU_UNK18, + RFU_SC_START, + RFU_SC_POLLING, + RFU_SC_END, + RFU_SP_START, + RFU_SP_POLLING, + RFU_SP_END, + RFU_CP_START, + RFU_CP_POLLING, + RFU_CP_END, + RFU_UNK22, + RFU_UNK23, + RFU_DATA_TX, + RFU_DATA_TX_AND_CHANGE, + RFU_DATA_RX, + RFU_MS_CHANGE, + RFU_DATA_READY_AND_CHANGE, + RFU_DISCONNECTED_AND_CHANGE, + RFU_UNK2A, + RFU_UNK2B, + RFU_UNK2C, + RFU_UNK2D, + RFU_UNK2E, + RFU_UNK2F, + RFU_DISCONNECT, + RFU_TEST_MODE, + RFU_CPR_START, + RFU_CPR_POLLING, + RFU_CPR_END, + RFU_UNK35, + RFU_UNK36, + RFU_RESUME_RETRANSMIT_AND_CHANGE, + RFU_UNK38, + RFU_UNK39, + RFU_UNK3A, + RFU_UNK3B, + RFU_UNK3C, + RFU_STOP_MODE, //3D +}; + +struct RfuPacket8 +{ + u8 data[0x74]; +}; + +struct RfuPacket32 +{ + u32 command; + u32 data[0x1C]; +}; + +union RfuPacket +{ + struct RfuPacket32 rfuPacket32; + struct RfuPacket8 rfuPacket8; +}; + +struct RfuStruct +{ + vs32 unk_0; + u8 txParams; + u8 unk_5; + u8 activeCommand; + u8 unk_7; + u8 unk_8; + u8 unk_9; + u8 timerSelect; + u8 unk_b; + int timerState; + vu8 timerActive; + u8 unk_11; + vu16 unk_12; + vu8 msMode; + u8 unk_15; + u8 unk_16; + u8 unk_17; + void (*callbackM)(); + void (*callbackS)(); + u32 callbackID; + union RfuPacket *txPacket; + union RfuPacket *rxPacket; + vu8 unk_2c; + u8 padding[3]; +}; + +struct RfuIntrStruct +{ + u8 rxPacketAlloc[0x74]; + u8 txPacketAlloc[0x74]; + u8 block1[0x960]; + u8 block2[0x30]; +}; + +extern struct RfuStruct *gRfuState; + +void STWI_init_all(struct RfuIntrStruct *interruptStruct, IntrFunc *interrupt, bool8 copyInterruptToRam); diff --git a/ld_script.txt b/ld_script.txt index 450e9125f..2b30d306b 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -244,8 +244,10 @@ SECTIONS { src/agb_flash_1m.o(.text); src/agb_flash_mx.o(.text); src/siirtc.o(.text); - src/librfu.o(.text); + src/librfu_stwi.o(.text); + src/librfu_intr.o(.text); asm/librfu_intr.o(.text); + src/librfu_rfu.o(.text); asm/librfu.o(.text); asm/libagbsyscall.o(.text); tools/agbcc/lib/libgcc.a:_call_via_rX.o(.text); diff --git a/src/librfu_intr.c b/src/librfu_intr.c new file mode 100644 index 000000000..bdf8b072a --- /dev/null +++ b/src/librfu_intr.c @@ -0,0 +1,4 @@ +#include "global.h" +#include "main.h" + +//TODO: decompile asm/librfu_intr.s to here diff --git a/src/librfu_rfu.c b/src/librfu_rfu.c new file mode 100644 index 000000000..cf3fe12ad --- /dev/null +++ b/src/librfu_rfu.c @@ -0,0 +1,109 @@ +#include "global.h" +#include "main.h" + +#include "librfu.h" + +struct RfuUnk1 +{ + u8 unk_0[0x14]; + u32 unk_14; + u32 unk_18; + struct RfuIntrStruct unk_1c; +}; + +struct RfuUnk2 +{ + u8 unk_0[0x68]; + u32 unk_68; + u32 unk_6c; + u8 unk_70[0x70]; +}; + +struct RfuUnk3 +{ + u32 unk_0; + u32 unk_4; + u8 unk_8[0xD4]; + u32 unk_dc; +}; + +extern u32 *gUnknown_03007890; +extern u32 *gUnknown_03007894; +extern struct RfuUnk3* gUnknown_03007898; +extern struct RfuUnk2* gUnknown_03007880[4]; +extern struct RfuUnk1* gUnknown_03007870[4]; +extern void* sub_82E53F4; +extern void rfu_STC_clearAPIVariables(void); + +// Nonmatching, only register differences +/*u16 rfu_initializeAPI(u32 *unk0, u16 unk1, IntrFunc *interrupt, bool8 copyInterruptToRam) +{ + u16 i; + u16 *v13; + u16 *v12; + u16 num; + + if (((u32)unk0 & 0xF000000) == 0x2000000 && copyInterruptToRam) + { + return 2; + } + + if ((u32)unk0 & 3) + return 2; + + // Nintendo pls, just use a ternary for once + if (copyInterruptToRam) + { + // An assert/debug print may have existed before, ie + // printf("%s %u < %u", "somefile.c:12345", unk1, num) + // to push this into r3? + num = 0xe64; + if (unk1 < num) + return 1; + } + + if (copyInterruptToRam == FALSE) + { + num = 0x504; // same as above, this should be r3 not r0 + if (unk1 < num) + return 1; + } + gUnknown_03007890 = unk0; + gUnknown_03007894 = unk0 + (0xB4 / sizeof(u32)); + gUnknown_03007898 = (struct RfuUnk3*)(unk0 + (0xDC / sizeof(u32))); + gUnknown_03007880[0] = (struct RfuUnk2*)(unk0 + (0x1BC / sizeof(u32))); + gUnknown_03007870[0] = (struct RfuUnk1*)(unk0 + (0x37C / sizeof(u32))); + + for (i = 1; i < 4; i++, num) + { + gUnknown_03007880[i] = (struct RfuUnk2*)&gUnknown_03007880[i-1]->unk_70; + gUnknown_03007870[i] = (struct RfuUnk1*)&gUnknown_03007870[i-1]->unk_1c; + } + + gUnknown_03007898->unk_dc = (u32)&gUnknown_03007870[3]->unk_1c; + STWI_init_all(&gUnknown_03007870[3]->unk_1c, interrupt, copyInterruptToRam); + rfu_STC_clearAPIVariables(); + + for (i = 0; i < 4; i++) + { + gUnknown_03007880[i]->unk_68 = 0; + gUnknown_03007880[i]->unk_6c = 0; + gUnknown_03007870[i]->unk_14 = 0; + gUnknown_03007870[i]->unk_18 = 0; + } + + // Not matching, register differences + v12 = (u16*)((u32)&sub_82E53F4 & ~1); + v13 = (u16*)gUnknown_03007898->unk_8; + + for (i = 47; i != 0xFFFF; i--) + { + *v13 = *v12; + ++v12; + ++v13; + } + + gUnknown_03007898->unk_4 = (u32)(&gUnknown_03007898->unk_8[1]); + + return 0; +}*/ diff --git a/src/librfu.c b/src/librfu_stwi.c similarity index 89% rename from src/librfu.c rename to src/librfu_stwi.c index e647b9178..21d38bb38 100644 --- a/src/librfu.c +++ b/src/librfu_stwi.c @@ -1,111 +1,10 @@ #include "global.h" -#include "main.h" - -enum -{ - RFU_RESET = 0x10, - RFU_LINK_STATUS, - RFU_VERSION_STATUS, - RFU_SYSTEM_STATUS, - RFU_SLOT_STATUS, - RFU_CONFIG_STATUS, - RFU_GAME_CONFIG, - RFU_SYSTEM_CONFIG, - RFU_UNK18, - RFU_SC_START, - RFU_SC_POLLING, - RFU_SC_END, - RFU_SP_START, - RFU_SP_POLLING, - RFU_SP_END, - RFU_CP_START, - RFU_CP_POLLING, - RFU_CP_END, - RFU_UNK22, - RFU_UNK23, - RFU_DATA_TX, - RFU_DATA_TX_AND_CHANGE, - RFU_DATA_RX, - RFU_MS_CHANGE, - RFU_DATA_READY_AND_CHANGE, - RFU_DISCONNECTED_AND_CHANGE, - RFU_UNK2A, - RFU_UNK2B, - RFU_UNK2C, - RFU_UNK2D, - RFU_UNK2E, - RFU_UNK2F, - RFU_DISCONNECT, - RFU_TEST_MODE, - RFU_CPR_START, - RFU_CPR_POLLING, - RFU_CPR_END, - RFU_UNK35, - RFU_UNK36, - RFU_RESUME_RETRANSMIT_AND_CHANGE, - RFU_UNK38, - RFU_UNK39, - RFU_UNK3A, - RFU_UNK3B, - RFU_UNK3C, - RFU_STOP_MODE, //3D -}; - -struct RfuPacket8 -{ - u8 data[0x74]; -}; - -struct RfuPacket32 -{ - u32 command; - u32 data[0x1C]; -}; - -union RfuPacket -{ - struct RfuPacket32 rfuPacket32; - struct RfuPacket8 rfuPacket8; -}; - -struct RfuStruct -{ - vs32 unk_0; - u8 txParams; - u8 unk_5; - u8 activeCommand; - u8 unk_7; - u8 unk_8; - u8 unk_9; - u8 timerSelect; - u8 unk_b; - int timerState; - vu8 timerActive; - u8 unk_11; - vu16 unk_12; - vu8 msMode; - u8 unk_15; - u8 unk_16; - u8 unk_17; - void (*callbackM)(); - void (*callbackS)(); - u32 callbackID; - union RfuPacket *txPacket; - union RfuPacket *rxPacket; - vu8 unk_2c; - u8 padding[3]; -}; - -struct RfuIntrStruct -{ - u8 rxPacketAlloc[0x74]; - u8 txPacketAlloc[0x74]; - u8 block1[0x960]; - u8 block2[0x30]; -}; +#include "librfu.h" extern IntrFunc IntrSIO32(void); -extern struct RfuStruct *gRfuState; + +extern void STWI_stop_timer(void); + void STWI_init_Callback_M(void); void STWI_init_Callback_S(void); void STWI_set_Callback_M(void * callback); @@ -114,7 +13,7 @@ u16 STWI_init(u8 request); int STWI_start_Command(void); void STWI_intr_timer(void); void STWI_set_timer(u8 unk); -extern void STWI_stop_timer(void); + int STWI_restart_Command(void); int STWI_reset_ClockCounter(void);