From ebf4c5ca76fc2c05443361a7ca633a897a4a76d7 Mon Sep 17 00:00:00 2001 From: YamaArashi Date: Fri, 13 Jan 2017 17:40:03 -0800 Subject: [PATCH] decompile multiboot --- asm/multiboot.s | 906 ---------------------------------------- include/gba/gba.h | 1 + include/gba/io_reg.h | 3 + include/gba/macro.h | 2 + include/gba/multiboot.h | 55 +++ include/gba/syscall.h | 16 + include/gba/types.h | 21 + include/multiboot.h | 21 + ld_script.txt | 2 +- src/multiboot.c | 470 +++++++++++++++++++++ sym_bss.txt | 5 +- 11 files changed, 593 insertions(+), 909 deletions(-) delete mode 100644 asm/multiboot.s create mode 100644 include/gba/multiboot.h create mode 100644 include/multiboot.h create mode 100644 src/multiboot.c diff --git a/asm/multiboot.s b/asm/multiboot.s deleted file mode 100644 index e0e3055c2..000000000 --- a/asm/multiboot.s +++ /dev/null @@ -1,906 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - thumb_func_start MultiBootInit -@ void MultiBootInit(MultiBootParam *mp) -MultiBootInit: @ 81BA6D0 - adds r2, r0, 0 - movs r1, 0 - strb r1, [r2, 0x1E] - strb r1, [r2, 0x18] - strb r1, [r2, 0x1D] - adds r3, r2, 0 - adds r3, 0x4A - movs r0, 0xF - strb r0, [r3] - adds r0, r2, 0 - adds r0, 0x48 - strb r1, [r0] - strh r1, [r2, 0x16] - ldr r0, =0x04000134 - strh r1, [r0] - ldr r2, =0x04000128 - ldr r3, =0x00002003 - adds r0, r3, 0 - strh r0, [r2] - ldr r0, =0x0400012a - strh r1, [r0] - bx lr - .pool - thumb_func_end MultiBootInit - - thumb_func_start MultiBootMain -@ int MultiBootMain(MultiBootParam *mp) -MultiBootMain: @ 81BA70C - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - adds r7, r0, 0 - bl MultiBootCheckComplete - cmp r0, 0 - beq _081BA722 - b _081BAAEA -_081BA722: - adds r0, r7, 0 - adds r0, 0x4A - ldrb r1, [r0] - mov r10, r0 - cmp r1, 0xF - bls _081BA736 - subs r0, r1, 0x1 - mov r1, r10 - strb r0, [r1] - b _081BAAEA -_081BA736: - adds r1, r7, 0 - adds r1, 0x48 - ldrb r0, [r1] - cmp r0, 0 - beq _081BA760 - movs r0, 0 - strb r0, [r1] - ldr r0, =0x04000128 - ldrh r0, [r0] - movs r5, 0xFC - ands r5, r0 - cmp r5, 0x8 - beq _081BA760 - adds r0, r7, 0 - bl MultiBootInit - movs r0, 0x8 - eors r0, r5 - b _081BAAEC - .pool -_081BA760: - ldrb r0, [r7, 0x18] - cmp r0, 0xDF - bls _081BA7B2 - adds r0, r7, 0 - bl MultiBootHandShake - adds r5, r0, 0 - cmp r5, 0 - beq _081BA774 - b _081BAAEC -_081BA774: - adds r0, r7, 0 - adds r0, 0x4B - ldrb r0, [r0] - cmp r0, 0x1 - bne _081BA790 - ldrb r0, [r7, 0x18] - cmp r0, 0xE1 - bls _081BA790 - adds r0, r7, 0 - bl MultiBootCheckComplete - cmp r0, 0 - bne _081BA790 - b _081BAADA -_081BA790: - adds r0, r7, 0 - bl MultiBootCheckComplete - cmp r0, 0 - beq _081BA79C - b _081BAAEA -_081BA79C: - ldrh r0, [r7, 0x16] - cmp r0, 0 - bne _081BA7AC - adds r0, r7, 0 - bl MultiBootInit - movs r0, 0x71 - b _081BAAEC -_081BA7AC: - subs r0, 0x1 - strh r0, [r7, 0x16] - b _081BAAEA -_081BA7B2: - ldrb r0, [r7, 0x18] - cmp r0, 0x2 - bne _081BA7BA - b _081BA8F0 -_081BA7BA: - cmp r0, 0x2 - bgt _081BA7C8 - cmp r0, 0 - beq _081BA7D6 - cmp r0, 0x1 - beq _081BA892 - b _081BAA28 -_081BA7C8: - cmp r0, 0xD0 - bne _081BA7CE - b _081BA93C -_081BA7CE: - cmp r0, 0xD1 - bne _081BA7D4 - b _081BA9D6 -_081BA7D4: - b _081BAA28 -_081BA7D6: - movs r3, 0xE - movs r5, 0x3 - ldr r0, =0x04000120 - ldrh r0, [r0, 0x6] - adds r1, r0, 0 - ldr r0, =0x0000ffff - ldrb r2, [r7, 0x1E] - adds r6, r2, 0 - cmp r1, r0 - bne _081BA7FE - adds r4, r1, 0 - ldr r1, =0x04000126 -_081BA7EE: - asrs r3, 1 - subs r1, 0x2 - subs r5, 0x1 - cmp r5, 0 - beq _081BA7FE - ldrh r0, [r1] - cmp r0, r4 - beq _081BA7EE -_081BA7FE: - movs r0, 0xE - ands r3, r0 - strb r3, [r7, 0x1D] - movs r5, 0x3 - ldr r0, =0x04000120 - ldrh r0, [r0, 0x6] - adds r4, r0, 0 - asrs r0, r2, 3 - movs r1, 0x1 - ands r0, r1 - cmp r0, 0 - beq _081BA82C - ldr r0, =0x00007208 - b _081BA852 - .pool -_081BA82C: - subs r5, 0x1 - cmp r5, 0 - beq _081BA858 - lsls r0, r5, 1 - ldr r1, =0x04000120 - adds r0, r1 - ldrh r0, [r0] - adds r4, r0, 0 - adds r0, r2, 0 - asrs r0, r5 - movs r1, 0x1 - ands r0, r1 - cmp r0, 0 - beq _081BA82C - adds r0, r1, 0 - lsls r0, r5 - movs r1, 0xE4 - lsls r1, 7 - orrs r0, r1 -_081BA852: - cmp r4, r0 - beq _081BA82C - movs r3, 0 -_081BA858: - adds r0, r3, 0 - ands r0, r6 - strb r0, [r7, 0x1E] - cmp r3, 0 - bne _081BA868 - movs r0, 0xF - mov r2, r10 - strb r0, [r2] -_081BA868: - mov r1, r10 - ldrb r0, [r1] - cmp r0, 0 - bne _081BA884 - ldrb r0, [r7, 0x1D] - ldrb r2, [r7, 0x1E] - cmp r0, r2 - beq _081BA88A - adds r0, r7, 0 - bl MultiBootStartProbe - b _081BA892 - .pool -_081BA884: - subs r0, 0x1 - mov r1, r10 - strb r0, [r1] -_081BA88A: - ldrb r1, [r7, 0x1E] - movs r2, 0xC4 - lsls r2, 7 - b _081BA992 -_081BA892: - adds r1, r7, 0 - adds r1, 0x49 - movs r0, 0 - strb r0, [r1] - movs r5, 0x3 - mov r12, r1 - ldr r3, =gUnknown_0300129C -_081BA8A0: - lsls r0, r5, 1 - ldr r2, =0x04000120 - adds r0, r2 - ldrh r0, [r0] - adds r4, r0, 0 - asrs r0, r4, 8 - subs r2, r5, 0x1 - cmp r0, 0x72 - bne _081BA8CA - lsls r0, r2, 1 - adds r0, r3 - strh r4, [r0] - movs r0, 0xFF - ands r4, r0 - movs r0, 0x1 - lsls r0, r5 - cmp r4, r0 - bne _081BA8CA - ldrb r0, [r1] - orrs r4, r0 - strb r4, [r1] -_081BA8CA: - adds r5, r2, 0 - cmp r5, 0 - bne _081BA8A0 - ldrb r0, [r7, 0x1D] - mov r1, r12 - ldrb r1, [r1] - cmp r0, r1 - bne _081BA88A - movs r0, 0x2 - strb r0, [r7, 0x18] - mov r2, r12 - ldrb r1, [r2] - movs r2, 0xC2 - lsls r2, 7 - b _081BA992 - .pool -_081BA8F0: - movs r5, 0x3 - movs r0, 0x49 - adds r0, r7 - mov r12, r0 - mov r4, r12 - movs r6, 0x1 - ldr r1, =gUnknown_0300129C - mov r9, r1 - ldr r2, =0x04000120 - mov r8, r2 -_081BA904: - ldrb r3, [r4] - adds r0, r3, 0 - asrs r0, r5 - ands r0, r6 - subs r2, r5, 0x1 - cmp r0, 0 - beq _081BA92A - lsls r0, r5, 1 - add r0, r8 - ldrh r1, [r0] - lsls r0, r2, 1 - add r0, r9 - ldrh r0, [r0] - cmp r1, r0 - beq _081BA92A - adds r0, r6, 0 - lsls r0, r5 - eors r3, r0 - strb r3, [r4] -_081BA92A: - adds r5, r2, 0 - cmp r5, 0 - bne _081BA904 - b _081BAA90 - .pool -_081BA93C: - movs r3, 0x1 - movs r5, 0x3 - movs r0, 0x49 - adds r0, r7 - mov r12, r0 - adds r6, r7, 0 - adds r6, 0x19 - ldr r1, =gUnknown_0300129C - mov r8, r1 -_081BA94E: - lsls r0, r5, 1 - ldr r2, =0x04000120 - adds r0, r2 - ldrh r0, [r0] - adds r4, r0, 0 - subs r2, r5, 0x1 - adds r0, r6, r2 - strb r4, [r0] - mov r0, r12 - ldrb r1, [r0] - asrs r1, r5 - movs r0, 0x1 - ands r1, r0 - cmp r1, 0 - beq _081BA982 - asrs r0, r4, 8 - subs r0, 0x72 - cmp r0, 0x1 - bls _081BA976 - b _081BAAE0 -_081BA976: - lsls r0, r2, 1 - add r0, r8 - ldrh r0, [r0] - cmp r4, r0 - bne _081BA982 - movs r3, 0 -_081BA982: - adds r5, r2, 0 - cmp r5, 0 - bne _081BA94E - cmp r3, 0 - bne _081BA9A8 - ldrb r1, [r7, 0x1C] - movs r2, 0xC6 - lsls r2, 7 -_081BA992: - adds r0, r2, 0 - orrs r1, r0 - adds r0, r7, 0 - bl MultiBootSend - b _081BAAEC - .pool -_081BA9A8: - movs r0, 0xD1 - strb r0, [r7, 0x18] - movs r3, 0x11 - movs r5, 0x3 - adds r1, r6, 0x2 -_081BA9B2: - ldrb r0, [r1] - adds r3, r0 - subs r1, 0x1 - subs r5, 0x1 - cmp r5, 0 - bne _081BA9B2 - strb r3, [r7, 0x14] - movs r0, 0xFF - ands r3, r0 - movs r1, 0xC8 - lsls r1, 7 - adds r0, r1, 0 - orrs r3, r0 - adds r0, r7, 0 - adds r1, r3, 0 - bl MultiBootSend - b _081BAAEC -_081BA9D6: - movs r5, 0x3 - movs r2, 0x49 - ldrb r1, [r2, r7] - ldr r2, =0x04000126 - movs r3, 0x1 -_081BA9E0: - ldrh r0, [r2] - adds r4, r0, 0 - adds r0, r1, 0 - asrs r0, r5 - ands r0, r3 - cmp r0, 0 - beq _081BA9F4 - asrs r0, r4, 8 - cmp r0, 0x73 - bne _081BAAE0 -_081BA9F4: - subs r2, 0x2 - subs r5, 0x1 - cmp r5, 0 - bne _081BA9E0 - adds r0, r7, 0 - bl MultiBoot - adds r5, r0, 0 - cmp r5, 0 - bne _081BAA18 - movs r0, 0xE0 - strb r0, [r7, 0x18] - adds r0, 0xB0 - strh r0, [r7, 0x16] - b _081BAAEA - .pool -_081BAA18: - adds r0, r7, 0 - bl MultiBootInit - movs r0, 0x1E - mov r1, r10 - strb r0, [r1] - movs r0, 0x70 - b _081BAAEC -_081BAA28: - movs r5, 0x3 - movs r2, 0x49 - adds r2, r7 - mov r12, r2 - mov r8, r12 - movs r0, 0x1 - mov r9, r0 -_081BAA36: - mov r1, r8 - ldrb r6, [r1] - adds r0, r6, 0 - asrs r0, r5 - mov r2, r9 - ands r0, r2 - cmp r0, 0 - beq _081BAA70 - lsls r0, r5, 1 - ldr r1, =0x04000120 - adds r0, r1 - ldrh r0, [r0] - adds r4, r0, 0 - asrs r2, r4, 8 - ldrb r0, [r7, 0x18] - lsrs r0, 1 - movs r1, 0x62 - subs r1, r0 - mov r3, r9 - lsls r3, r5 - cmp r2, r1 - bne _081BAA6A - movs r0, 0xFF - ands r4, r0 - cmp r4, r3 - beq _081BAA70 -_081BAA6A: - eors r6, r3 - mov r2, r8 - strb r6, [r2] -_081BAA70: - subs r5, 0x1 - cmp r5, 0 - bne _081BAA36 - ldrb r0, [r7, 0x18] - cmp r0, 0xC4 - bne _081BAA90 - mov r0, r12 - ldrb r1, [r0] - movs r0, 0xE - ands r0, r1 - strb r0, [r7, 0x1E] - strb r5, [r7, 0x18] - b _081BA88A - .pool -_081BAA90: - mov r1, r12 - ldrb r0, [r1] - cmp r0, 0 - bne _081BAAA2 - adds r0, r7, 0 - bl MultiBootInit - movs r0, 0x50 - b _081BAAEC -_081BAAA2: - ldrb r0, [r7, 0x18] - adds r0, 0x2 - strb r0, [r7, 0x18] - lsls r0, 24 - lsrs r0, 24 - cmp r0, 0xC4 - bne _081BAAB2 - b _081BA88A -_081BAAB2: - ldrb r0, [r7, 0x18] - ldr r1, [r7, 0x28] - adds r0, r1 - subs r1, r0, 0x3 - ldrb r2, [r1] - lsls r2, 8 - subs r0, 0x4 - ldrb r1, [r0] - orrs r1, r2 - adds r0, r7, 0 - bl MultiBootSend - adds r5, r0, 0 - cmp r5, 0 - bne _081BAAEC - adds r0, r7, 0 - adds r0, 0x4B - ldrb r0, [r0] - cmp r0, 0x1 - bne _081BAAEA -_081BAADA: - bl MultiBootWaitSendDone - b _081BA736 -_081BAAE0: - adds r0, r7, 0 - bl MultiBootInit - movs r0, 0x60 - b _081BAAEC -_081BAAEA: - movs r0, 0 -_081BAAEC: - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - thumb_func_end MultiBootMain - - thumb_func_start MultiBootSend -@ int MultiBootSend(MultiBootParam *mp, u16 data) -MultiBootSend: @ 81BAAFC - push {r4,lr} - adds r2, r0, 0 - lsls r1, 16 - lsrs r1, 16 - ldr r3, =0x04000128 - ldrh r0, [r3] - movs r4, 0x8C - ands r4, r0 - cmp r4, 0x8 - bne _081BAB34 - ldr r0, =0x0400012a - strh r1, [r0] - ldr r1, =0x00002083 - adds r0, r1, 0 - strh r0, [r3] - adds r1, r2, 0 - adds r1, 0x48 - movs r0, 0x1 - strb r0, [r1] - movs r0, 0 - b _081BAB40 - .pool -_081BAB34: - adds r0, r2, 0 - bl MultiBootInit - movs r0, 0x8 - eors r4, r0 - adds r0, r4, 0 -_081BAB40: - pop {r4} - pop {r1} - bx r1 - thumb_func_end MultiBootSend - - thumb_func_start MultiBootStartProbe -@ void MultiBootStartProbe(MultiBootParam *mp) -MultiBootStartProbe: @ 81BAB48 - push {lr} - adds r1, r0, 0 - ldrb r0, [r1, 0x18] - cmp r0, 0 - beq _081BAB5A - adds r0, r1, 0 - bl MultiBootInit - b _081BAB66 -_081BAB5A: - adds r2, r1, 0 - adds r2, 0x4A - strb r0, [r2] - strb r0, [r1, 0x1E] - movs r0, 0x1 - strb r0, [r1, 0x18] -_081BAB66: - pop {r0} - bx r0 - thumb_func_end MultiBootStartProbe - - thumb_func_start MultiBootStartMaster -@ void MultiBootStartMaster(MultiBootParam *mp, u8 *srcp, int length, u8 palette_color, s8 palette_speed) -MultiBootStartMaster: @ 81BAB6C - push {r4-r7,lr} - adds r4, r0, 0 - adds r6, r1, 0 - ldr r0, [sp, 0x14] - lsls r3, 24 - lsrs r5, r3, 24 - lsls r0, 24 - lsrs r7, r0, 24 - movs r3, 0 - ldrb r0, [r4, 0x18] - cmp r0, 0 - bne _081BABA8 - ldrb r0, [r4, 0x1E] - cmp r0, 0 - beq _081BABA8 - adds r0, r4, 0 - adds r0, 0x4A - ldrb r0, [r0] - cmp r0, 0 - bne _081BABA8 - str r6, [r4, 0x20] - adds r2, 0xF - movs r0, 0x10 - negs r0, r0 - ands r2, r0 - subs r0, 0xF0 - adds r1, r2, r0 - ldr r0, =0x0003ff00 - cmp r1, r0 - bls _081BABB4 -_081BABA8: - adds r0, r4, 0 - bl MultiBootInit - b _081BAC28 - .pool -_081BABB4: - adds r0, r6, r2 - str r0, [r4, 0x24] - lsls r1, r7, 24 - movs r2, 0x80 - lsls r2, 19 - adds r0, r1, r2 - asrs r0, 24 - adds r2, r1, 0 - cmp r0, 0x8 - bhi _081BAC14 - lsls r0, 2 - ldr r1, =_081BABD8 - adds r0, r1 - ldr r0, [r0] - mov pc, r0 - .pool - .align 2, 0 -_081BABD8: - .4byte _081BABFC - .4byte _081BABFC - .4byte _081BABFC - .4byte _081BABFC - .4byte _081BAC06 - .4byte _081BAC0C - .4byte _081BAC0C - .4byte _081BAC0C - .4byte _081BAC0C -_081BABFC: - lsls r3, r5, 3 - asrs r1, r2, 24 - movs r0, 0x3 - subs r0, r1 - b _081BAC12 -_081BAC06: - movs r0, 0x38 - adds r3, r5, 0 - b _081BAC12 -_081BAC0C: - lsls r3, r5, 3 - asrs r0, r2, 24 - subs r0, 0x1 -_081BAC12: - orrs r3, r0 -_081BAC14: - movs r0, 0x3F - ands r3, r0 - lsls r0, r3, 1 - movs r2, 0x7F - negs r2, r2 - adds r1, r2, 0 - orrs r0, r1 - strb r0, [r4, 0x1C] - movs r0, 0xD0 - strb r0, [r4, 0x18] -_081BAC28: - pop {r4-r7} - pop {r0} - bx r0 - thumb_func_end MultiBootStartMaster - - thumb_func_start MultiBootCheckComplete -@ s32 MultiBootCheckComplete(MultiBootParam *mp) -MultiBootCheckComplete: @ 81BAC30 - push {lr} - ldrb r0, [r0, 0x18] - cmp r0, 0xE9 - beq _081BAC3C - movs r0, 0 - b _081BAC3E -_081BAC3C: - movs r0, 0x1 -_081BAC3E: - pop {r1} - bx r1 - thumb_func_end MultiBootCheckComplete - - thumb_func_start MultiBootHandShake -@ int MultiBootHandShake(MultiBootParam *mp) -MultiBootHandShake: @ 81BAC44 - push {r4-r6,lr} - adds r3, r0, 0 - ldrb r0, [r3, 0x18] - cmp r0, 0xE0 - beq _081BAC60 - cmp r0, 0xE0 - blt _081BAC70 - cmp r0, 0xE8 - bgt _081BAC70 - cmp r0, 0xE7 - blt _081BAC70 - movs r4, 0x3 - ldrb r5, [r3, 0x1E] - b _081BACD0 -_081BAC60: - movs r1, 0 - movs r0, 0xE1 - strb r0, [r3, 0x18] - str r1, [r3, 0x4] - movs r0, 0x80 - lsls r0, 13 - str r0, [r3] - b _081BACC2 -_081BAC70: - movs r4, 0x3 - ldrb r5, [r3, 0x1E] - movs r6, 0x1 - ldr r1, =0x04000126 -_081BAC78: - ldrh r0, [r1] - adds r2, r0, 0 - adds r0, r5, 0 - asrs r0, r4 - ands r0, r6 - cmp r0, 0 - beq _081BAC8C - ldr r0, [r3, 0x4] - cmp r2, r0 - bne _081BAC60 -_081BAC8C: - subs r1, 0x2 - subs r4, 0x1 - cmp r4, 0 - bne _081BAC78 - ldrb r0, [r3, 0x18] - adds r0, 0x1 - strb r0, [r3, 0x18] - ldr r1, [r3] - ldrh r0, [r3] - str r0, [r3, 0x4] - cmp r1, 0 - bne _081BACBA - ldr r0, [r3, 0x28] - adds r1, r0, 0 - adds r1, 0xAC - ldrb r1, [r1] - adds r0, 0xAD - ldrb r0, [r0] - lsls r0, 8 - orrs r1, r0 - str r1, [r3, 0x4] - lsls r1, 5 - str r1, [r3] -_081BACBA: - ldr r0, [r3] - lsrs r0, 5 - str r0, [r3] -_081BACC0: - ldrh r1, [r3] -_081BACC2: - adds r0, r3, 0 - bl MultiBootSend - b _081BAD28 - .pool -_081BACD0: - lsls r0, r4, 1 - ldr r1, =0x04000120 - adds r0, r1 - ldrh r0, [r0] - adds r2, r0, 0 - adds r0, r5, 0 - asrs r0, r4 - movs r1, 0x1 - ands r0, r1 - cmp r0, 0 - beq _081BACEC - ldr r0, [r3, 0x4] - cmp r2, r0 - bne _081BAD1C -_081BACEC: - subs r4, 0x1 - cmp r4, 0 - bne _081BACD0 - ldrb r0, [r3, 0x18] - adds r0, 0x1 - strb r0, [r3, 0x18] - lsls r0, 24 - lsrs r0, 24 - cmp r0, 0xE9 - beq _081BAD26 - ldr r0, [r3, 0x28] - adds r1, r0, 0 - adds r1, 0xAE - ldrb r1, [r1] - adds r0, 0xAF - ldrb r0, [r0] - lsls r0, 8 - orrs r1, r0 - str r1, [r3] - str r1, [r3, 0x4] - b _081BACC0 - .pool -_081BAD1C: - adds r0, r3, 0 - bl MultiBootInit - movs r0, 0x71 - b _081BAD28 -_081BAD26: - movs r0, 0 -_081BAD28: - pop {r4-r6} - pop {r1} - bx r1 - thumb_func_end MultiBootHandShake - - thumb_func_start MultiBootWaitCycles -@ void MultiBootWaitCycles(u32 cycles) -MultiBootWaitCycles: @ 81BAD30 - mov r2, pc - lsrs r2, 24 - movs r1, 0xC - cmp r2, 0x2 - beq _081BAD42 - movs r1, 0xD - cmp r2, 0x8 - beq _081BAD42 - movs r1, 0x4 -_081BAD42: - subs r0, r1 - bgt _081BAD42 - bx lr - thumb_func_end MultiBootWaitCycles - - thumb_func_start MultiBootWaitSendDone -@ void MultiBootWaitSendDone(void) -MultiBootWaitSendDone: @ 81BAD48 - push {r4,r5,lr} - movs r2, 0 - ldr r3, =0x04000128 - ldrh r1, [r3] - movs r0, 0x80 - ands r0, r1 - cmp r0, 0 - beq _081BAD6C - ldr r5, =0x0000795c - movs r4, 0x80 -_081BAD5C: - adds r2, 0x1 - cmp r2, r5 - bgt _081BAD6C - ldrh r1, [r3] - adds r0, r4, 0 - ands r0, r1 - cmp r0, 0 - bne _081BAD5C -_081BAD6C: - movs r0, 0x96 - lsls r0, 2 - bl MultiBootWaitCycles - pop {r4,r5} - pop {r0} - bx r0 - .pool - thumb_func_end MultiBootWaitSendDone - - .align 2, 0 @ Don't pad with nop. diff --git a/include/gba/gba.h b/include/gba/gba.h index 26342cf88..42ae3cdde 100644 --- a/include/gba/gba.h +++ b/include/gba/gba.h @@ -4,6 +4,7 @@ #include "gba/defines.h" #include "gba/io_reg.h" #include "gba/types.h" +#include "gba/multiboot.h" #include "gba/syscall.h" #include "gba/macro.h" diff --git a/include/gba/io_reg.h b/include/gba/io_reg.h index 0be92fa60..5234e5b6c 100644 --- a/include/gba/io_reg.h +++ b/include/gba/io_reg.h @@ -577,6 +577,8 @@ #define TIMER_ENABLE 0x80 // serial +#define SIO_ID 0x0030 // Communication ID + #define SIO_8BIT_MODE 0x0000 // Normal 8-bit communication mode #define SIO_32BIT_MODE 0x1000 // Normal 32-bit communication mode #define SIO_MULTI_MODE 0x2000 // Multi-player communication mode @@ -589,6 +591,7 @@ #define SIO_MULTI_SI 0x0004 // Multi-player communication SI terminal #define SIO_MULTI_SD 0x0008 // SD terminal +#define SIO_MULTI_BUSY 0x0080 #define SIO_ERROR 0x0040 // Detect error #define SIO_START 0x0080 // Start transfer diff --git a/include/gba/macro.h b/include/gba/macro.h index 62d3e73fa..230fb4383 100644 --- a/include/gba/macro.h +++ b/include/gba/macro.h @@ -25,6 +25,8 @@ CPU_FAST_SET_SRC_FIXED | ((size)/(32/8) & 0x1FFFFF)); \ } +#define CpuFastFill16(value, dest, size) CpuFastFill(((value) << 16) | (value), (dest), (size)) + #define CpuFastCopy(src, dest, size) CpuFastSet(src, dest, ((size)/(32/8) & 0x1FFFFF)) #define DmaSet(dmaNum, src, dest, control) \ diff --git a/include/gba/multiboot.h b/include/gba/multiboot.h new file mode 100644 index 000000000..e88b43a19 --- /dev/null +++ b/include/gba/multiboot.h @@ -0,0 +1,55 @@ +#ifndef GUARD_GBA_MULTIBOOT_H +#define GUARD_GBA_MULTIBOOT_H + +#define MULTIBOOT_NCHILD 3 // Maximum number of slaves +#define MULTIBOOT_HEADER_SIZE 0xc0 // Header size +#define MULTIBOOT_SEND_SIZE_MIN 0x100 // Minimum transmission size +#define MULTIBOOT_SEND_SIZE_MAX 0x40000 // Maximum transmission size + +struct MultiBootParam +{ + u32 system_work[5]; + u8 handshake_data; + u8 padding; + u16 handshake_timeout; + u8 probe_count; + u8 client_data[MULTIBOOT_NCHILD]; + u8 palette_data; + u8 response_bit; + u8 client_bit; + u8 reserved1; + u8 *boot_srcp; + u8 *boot_endp; + u8 *masterp; + u8 *reserved2[MULTIBOOT_NCHILD]; + u32 system_work2[4]; + u8 sendflag; + u8 probe_target_bit; + u8 check_wait; + u8 server_type; +}; + +#define MULTIBOOT_ERROR_04 0x04 +#define MULTIBOOT_ERROR_08 0x08 +#define MULTIBOOT_ERROR_0c 0x0c +#define MULTIBOOT_ERROR_40 0x40 +#define MULTIBOOT_ERROR_44 0x44 +#define MULTIBOOT_ERROR_48 0x48 +#define MULTIBOOT_ERROR_4c 0x4c +#define MULTIBOOT_ERROR_80 0x80 +#define MULTIBOOT_ERROR_84 0x84 +#define MULTIBOOT_ERROR_88 0x88 +#define MULTIBOOT_ERROR_8c 0x8c +#define MULTIBOOT_ERROR_NO_PROBE_TARGET 0x50 +#define MULTIBOOT_ERROR_NO_DLREADY 0x60 +#define MULTIBOOT_ERROR_BOOT_FAILURE 0x70 +#define MULTIBOOT_ERROR_HANDSHAKE_FAILURE 0x71 + +#define MULTIBOOT_CONNECTION_CHECK_WAIT 15 + +#define MULTIBOOT_SERVER_TYPE_NORMAL 0 +#define MULTIBOOT_SERVER_TYPE_QUICK 1 + +#define MULTIBOOT_HANDSHAKE_TIMEOUT 400 + +#endif // GUARD_GBA_MULTIBOOT_H diff --git a/include/gba/syscall.h b/include/gba/syscall.h index 3f41abba1..deddec5ba 100644 --- a/include/gba/syscall.h +++ b/include/gba/syscall.h @@ -17,6 +17,10 @@ void RegisterRamReset(u32 resetFlags); void VBlankIntrWait(void); +u16 Sqrt(u32 num); + +u16 ArcTan2(s16 x, s16 y); + #define CPU_SET_SRC_FIXED 0x01000000 #define CPU_SET_16BIT 0x00000000 #define CPU_SET_32BIT 0x04000000 @@ -27,6 +31,18 @@ void CpuSet(const void *src, void *dest, u32 control); void CpuFastSet(const void *src, void *dest, u32 control); +void BgAffineSet(struct BgAffineSrcData *src, struct BgAffineDstData *dest, s32 count); + void ObjAffineSet(struct ObjAffineSrcData *src, void *dest, s32 count, s32 offset); +void LZ77UnCompWram(const void *src, void *dest); + +void LZ77UnCompVram(const void *src, void *dest); + +void RLUnCompWram(const void *src, void *dest); + +void RLUnCompVram(const void *src, void *dest); + +int MultiBoot(struct MultiBootParam *mp); + #endif // GUARD_GBA_SYSCALL_H diff --git a/include/gba/types.h b/include/gba/types.h index be7390d5a..fd8a20a4c 100644 --- a/include/gba/types.h +++ b/include/gba/types.h @@ -74,6 +74,27 @@ struct OamData #define ST_OAM_H_RECTANGLE 1 #define ST_OAM_V_RECTANGLE 2 +struct BgAffineSrcData +{ + s32 texX; + s32 texY; + s16 scrX; + s16 scrY; + s16 sx; + s16 sy; + u16 alpha; +}; + +struct BgAffineDstData +{ + s16 pa; + s16 pb; + s16 pc; + s16 pd; + s32 dx; + s32 dy; +}; + struct ObjAffineSrcData { s16 xScale; diff --git a/include/multiboot.h b/include/multiboot.h new file mode 100644 index 000000000..950c853d0 --- /dev/null +++ b/include/multiboot.h @@ -0,0 +1,21 @@ +#ifndef GUARD_MULTIBOOT_H +#define GUARD_MULTIBOOT_H + +#define MULTIBOOT_MASTER_INFO 0x62 +#define MULTIBOOT_CLIENT_INFO 0x72 +#define MULTIBOOT_MASTER_START_PROBE 0x61 +#define MULTIBOOT_MASTER_REQUEST_DLREADY 0x63 +#define MULTIBOOT_CLIENT_DLREADY 0x73 +#define MULTIBOOT_MASTER_START_DL 0x64 +#define MULTIBOOT_MASTER_REQUEST_CRC 0x65 +#define MULTIBOOT_CLIENT_CALC_CRC 0x74 +#define MULTIBOOT_CLIENT_CRCREADY 0x75 +#define MULTIBOOT_MASTER_VERIFY_CRC 0x66 + +void MultiBootInit(struct MultiBootParam *mp); +int MultiBootMain(struct MultiBootParam *mp); +void MultiBootStartProbe(struct MultiBootParam *mp); +void MultiBootStartMaster(struct MultiBootParam *mp, u8 *srcp, int length, u8 palette_color, s8 palette_speed); +int MultiBootCheckComplete(struct MultiBootParam *mp); + +#endif // GUARD_MULTIBOOT_H diff --git a/ld_script.txt b/ld_script.txt index 31b058496..520aaa6e0 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -187,7 +187,7 @@ SECTIONS { asm/rom_817B064.o(.text); asm/front_pic_anim.o(.text); asm/rom_8184DA4.o(.text); - asm/multiboot.o(.text); + src/multiboot.o(.text); asm/rom_81BAD84.o(.text); } =0 diff --git a/src/multiboot.c b/src/multiboot.c new file mode 100644 index 000000000..80291ff46 --- /dev/null +++ b/src/multiboot.c @@ -0,0 +1,470 @@ +#include "gba/gba.h" +#include "multiboot.h" + +static u16 MultiBoot_required_data[MULTIBOOT_NCHILD]; + +static int MultiBootSend(struct MultiBootParam *mp, u16 data); +static int MultiBootHandShake(struct MultiBootParam *mp); +static void MultiBootWaitCycles(u32 cycles); +static void MultiBootWaitSendDone(void); + +void MultiBootInit(struct MultiBootParam *mp) +{ + mp->client_bit = 0; + mp->probe_count = 0; + mp->response_bit = 0; + + mp->check_wait = MULTIBOOT_CONNECTION_CHECK_WAIT; + mp->sendflag = 0; + + mp->handshake_timeout = 0; + + REG_RCNT = 0; + REG_SIOCNT = SIO_MULTI_MODE | SIO_115200_BPS; + REG_SIODATA8 = 0; +} + +int MultiBootMain(struct MultiBootParam *mp) +{ + int i; + int j; + int k; + + if (MultiBootCheckComplete(mp)) + { + return 0; + } + + if (mp->check_wait > MULTIBOOT_CONNECTION_CHECK_WAIT) + { + mp->check_wait--; + return 0; + } + +output_burst: + if (mp->sendflag) + { + mp->sendflag = 0; + + i = REG_SIOCNT & (SIO_MULTI_BUSY | SIO_ERROR | SIO_ID | SIO_MULTI_SD | SIO_MULTI_SI); + if (i != SIO_MULTI_SD) + { + MultiBootInit(mp); + return i ^ SIO_MULTI_SD; + } + } + + if (mp->probe_count >= 0xe0) + { + i = MultiBootHandShake(mp); + if (i) + { + return i; + } + + if (mp->server_type == MULTIBOOT_SERVER_TYPE_QUICK + && mp->probe_count > 0xe1 + && MultiBootCheckComplete(mp) == 0) + { + MultiBootWaitSendDone(); + goto output_burst; + } + + if (MultiBootCheckComplete(mp) == 0) + { + if (mp->handshake_timeout == 0) + { + MultiBootInit(mp); + return MULTIBOOT_ERROR_HANDSHAKE_FAILURE; + } + mp->handshake_timeout--; + } + + return 0; + } + + switch (mp->probe_count) + { + case 0: + k = 0x0e; + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + if (*(vu16 *)(REG_ADDR_SIOMULTI0 + i * 2) != 0xffff) + { + break; + } + k >>= 1; + } + + k &= 0x0e; + mp->response_bit = k; + + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + j = *(vu16 *)(REG_ADDR_SIOMULTI0 + i * 2); + if (mp->client_bit & (1 << i)) + { + if (j != ((MULTIBOOT_CLIENT_INFO << 8) | (1 << i))) + { + k = 0; + break; + } + } + } + + mp->client_bit &= k; + + if (k == 0) + { + mp->check_wait = MULTIBOOT_CONNECTION_CHECK_WAIT; + } + + if (mp->check_wait) + { + mp->check_wait--; + } + else + { + if (mp->response_bit != mp->client_bit) + { + MultiBootStartProbe(mp); + goto case_1; + } + } + + output_master_info: + return MultiBootSend(mp, (MULTIBOOT_MASTER_INFO << 8) | mp->client_bit); + + case_1: + case 1: + mp->probe_target_bit = 0; + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + j = *(vu16 *)(REG_ADDR_SIOMULTI0 + i * 2); + if ((j >> 8) == MULTIBOOT_CLIENT_INFO) + { + MultiBoot_required_data[i - 1] = j; + j &= 0xff; + if (j == (1 << i)) + { + mp->probe_target_bit |= j; + } + } + } + + if (mp->response_bit != mp->probe_target_bit) + { + goto output_master_info; + } + + mp->probe_count = 2; + return MultiBootSend(mp, (MULTIBOOT_MASTER_START_PROBE << 8) | mp->probe_target_bit); + + case 2: + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + if (mp->probe_target_bit & (1 << i)) + { + j = *(vu16 *)(REG_ADDR_SIOMULTI0 + i * 2); + if (j != MultiBoot_required_data[i - 1]) + { + mp->probe_target_bit ^= 1 << i; + } + } + } + goto output_header; + + case 0xd0: + k = 1; + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + j = *(vu16 *)(REG_ADDR_SIOMULTI0 + i * 2); + mp->client_data[i - 1] = j; + if (mp->probe_target_bit & (1 << i)) + { + if ((j >> 8) != MULTIBOOT_CLIENT_INFO + && (j >> 8) != MULTIBOOT_CLIENT_DLREADY) + { + MultiBootInit(mp); + return MULTIBOOT_ERROR_NO_DLREADY; + } + if (j == MultiBoot_required_data[i - 1]) + { + k = 0; + } + } + } + + if (k == 0) + { + return MultiBootSend(mp, (MULTIBOOT_MASTER_REQUEST_DLREADY << 8) | mp->palette_data); + } + + mp->probe_count = 0xd1; + + k = 0x11; + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + k += mp->client_data[i - 1]; + } + mp->handshake_data = k; + return MultiBootSend(mp, (MULTIBOOT_MASTER_START_DL << 8) | (k & 0xff)); + + case 0xd1: + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + j = *(vu16 *)(REG_ADDR_SIOMULTI0 + i * 2); + if (mp->probe_target_bit & (1 << i)) + { + if ((j >> 8) != MULTIBOOT_CLIENT_DLREADY) + { + MultiBootInit(mp); + return MULTIBOOT_ERROR_NO_DLREADY; + } + } + } + + i = MultiBoot(mp); + + if (i == 0) + { + mp->probe_count = 0xe0; + mp->handshake_timeout = MULTIBOOT_HANDSHAKE_TIMEOUT; + return 0; + } + MultiBootInit(mp); + mp->check_wait = MULTIBOOT_CONNECTION_CHECK_WAIT * 2; + return MULTIBOOT_ERROR_BOOT_FAILURE; + + default: + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + if (mp->probe_target_bit & (1 << i)) + { + j = *(vu16 *)(REG_ADDR_SIOMULTI0 + i * 2); + if ((j >> 8) != (MULTIBOOT_MASTER_START_PROBE + 1 - (mp->probe_count >> 1)) + || ((j & 0xff) != (1 << i))) + { + mp->probe_target_bit ^= 1 << i; + } + } + } + + if (mp->probe_count == 0xc4) + { + mp->client_bit = mp->probe_target_bit & 0x0e; + mp->probe_count = 0; + goto output_master_info; + } + + output_header: + if (mp->probe_target_bit == 0) + { + MultiBootInit(mp); + return MULTIBOOT_ERROR_NO_PROBE_TARGET; + } + + mp->probe_count += 2; + if (mp->probe_count == 0xc4) + { + goto output_master_info; + } + i = MultiBootSend(mp, + (mp->masterp[mp->probe_count - 4 + 1] << 8) + | mp->masterp[mp->probe_count - 4]); + + if (i) + { + return i; + } + if (mp->server_type == MULTIBOOT_SERVER_TYPE_QUICK) + { + MultiBootWaitSendDone(); + goto output_burst; + } + return 0; + } +} + +static int MultiBootSend(struct MultiBootParam *mp, u16 data) +{ + int i; + + i = REG_SIOCNT & (SIO_MULTI_BUSY | SIO_MULTI_SD | SIO_MULTI_SI); + if (i != SIO_MULTI_SD) + { + MultiBootInit(mp); + return i ^ SIO_MULTI_SD; + } + + REG_SIODATA8 = data; + REG_SIOCNT = SIO_MULTI_MODE | SIO_START | SIO_115200_BPS; + + mp->sendflag = 1; + return 0; +} + +void MultiBootStartProbe(struct MultiBootParam *mp) +{ + if (mp->probe_count != 0) + { + MultiBootInit(mp); + return; + } + mp->check_wait = 0; + mp->client_bit = 0; + mp->probe_count = 1; +} + +void MultiBootStartMaster(struct MultiBootParam *mp, u8 *srcp, int length, u8 palette_color, s8 palette_speed) +{ + int i = 0; + + if (mp->probe_count != 0 + || mp->client_bit == 0 + || mp->check_wait != 0) + { + MultiBootInit(mp); + return; + } + + mp->boot_srcp = srcp; + length = (length + 15) & ~15; + if (length < MULTIBOOT_SEND_SIZE_MIN || length > MULTIBOOT_SEND_SIZE_MAX) + { + MultiBootInit(mp); + return; + } + + mp->boot_endp = srcp + length; + + switch (palette_speed) + { + case -4: + case -3: + case -2: + case -1: + i = (palette_color << 3) | (3 - palette_speed); + break; + case 0: + i = 0x38 | palette_color; + break; + case 1: + case 2: + case 3: + case 4: + i = (palette_color << 3) | (palette_speed - 1); + break; + } + + mp->palette_data = ((i & 0x3f) << 1) | 0x81; + mp->probe_count = 0xd0; +} + +int MultiBootCheckComplete(struct MultiBootParam *mp) +{ + if (mp->probe_count == 0xe9) + { + return 1; + } + + return 0; +} + +static int MultiBootHandShake(struct MultiBootParam *mp) +{ + int i, j; + +#define send_data (mp->system_work[0]) +#define must_data (mp->system_work[1]) + + switch (mp->probe_count) + { + case_0xe0: + case 0xe0: + mp->probe_count = 0xe1; + must_data = 0x0000; + send_data = 0x100000; + return MultiBootSend(mp, 0x0000); + + default: + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + j = *(vu16 *)(REG_ADDR_SIOMULTI0 + i * 2); + if ((mp->client_bit & (1 << i)) + && j != must_data) + { + goto case_0xe0; + } + } + mp->probe_count++; + must_data = send_data & 0xffff; + if (send_data == 0x0000) + { + must_data = mp->masterp[0xac] | (mp->masterp[0xad] << 8); + send_data = must_data << 5; + } + send_data >>= 5; + output_common: + return MultiBootSend(mp, send_data); + + case 0xe7: + case 0xe8: + for (i = MULTIBOOT_NCHILD; i != 0; i--) + { + j = *(vu16 *)(REG_ADDR_SIOMULTI0 + i * 2); + if ((mp->client_bit & (1 << i)) && j != must_data) + { + MultiBootInit(mp); + return MULTIBOOT_ERROR_HANDSHAKE_FAILURE; + } + } + + mp->probe_count++; + if (mp->probe_count == 0xe9) + { + return 0; + } + + send_data = mp->masterp[0xae] | (mp->masterp[0xaf] << 8); + must_data = send_data; + goto output_common; + } + +#undef send_data +#undef must_data +} + +static void MultiBootWaitCycles(u32 cycles) +{ + asm("mov r2, pc"); + asm("lsr r2, #24"); + asm("mov r1, #12"); + asm("cmp r2, #0x02"); + asm("beq MultiBootWaitCyclesLoop"); + + asm("mov r1, #13"); + asm("cmp r2, #0x08"); + asm("beq MultiBootWaitCyclesLoop"); + + asm("mov r1, #4"); + + asm("MultiBootWaitCyclesLoop:"); + asm("sub r0, r1"); + asm("bgt MultiBootWaitCyclesLoop"); +} + +static void MultiBootWaitSendDone(void) +{ + int i; + + for (i = 0; i < 31069; i++) + { + if ((REG_SIOCNT & SIO_START) == 0) + { + break; + } + } + + MultiBootWaitCycles(600); +} diff --git a/sym_bss.txt b/sym_bss.txt index 734c4ec4c..e10bd14ba 100644 --- a/sym_bss.txt +++ b/sym_bss.txt @@ -433,8 +433,9 @@ gUnknown_03001294: @ 3001294 gUnknown_03001298: @ 3001298 .space 0x4 -gUnknown_0300129C: @ 300129C - .space 0xC + .include "src/multiboot.o" + + .space 0x4 @ XXX: why is this needed? gUnknown_030012A8: @ 30012A8 .space 0x10