diff --git a/asm/dma3_manager.s b/asm/dma3_manager.s deleted file mode 100644 index 74832b527..000000000 --- a/asm/dma3_manager.s +++ /dev/null @@ -1,555 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - thumb_func_start ClearDma3Requests -@ void ClearDma3Requests() -ClearDma3Requests: @ 8000BB8 - push {lr} - ldr r2, =gDma3ManagerLocked - movs r0, 0x1 - strb r0, [r2] - ldr r1, =gDma3RequestCursor - movs r0, 0 - strb r0, [r1] - movs r3, 0 - ldr r0, =gDma3Requests - movs r1, 0x7F -_8000BCC: - strh r3, [r0, 0x8] - str r3, [r0] - str r3, [r0, 0x4] - adds r0, 0x10 - subs r1, 0x1 - cmp r1, 0 - bge _8000BCC - movs r0, 0 - strb r0, [r2] - pop {r0} - bx r0 - .pool - thumb_func_end ClearDma3Requests - - thumb_func_start ProcessDma3Requests -@ void ProcessDma3Requests() -ProcessDma3Requests: @ 8000BF0 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0xC - ldr r0, =gDma3ManagerLocked - ldrb r0, [r0] - cmp r0, 0 - beq _08000C06 - b _08000E46 -_08000C06: - movs r0, 0 - str r0, [sp, 0x8] - ldr r1, =gDma3Requests - ldr r2, =gDma3RequestCursor - ldrb r0, [r2] - lsls r0, 4 - adds r0, r1 - ldrh r0, [r0, 0x8] - mov r12, r2 - cmp r0, 0 - bne _08000C1E - b _08000E46 -_08000C1E: - mov r8, r1 - adds r1, 0x4 - mov r10, r1 - movs r6, 0x80 - lsls r6, 5 - ldr r7, =REG_DMA3 - movs r2, 0 - mov r9, r2 -_08000C2E: - mov r3, r12 - ldrb r0, [r3] - lsls r5, r0, 4 - mov r0, r8 - adds r1, r5, r0 - ldrh r0, [r1, 0x8] - ldr r2, [sp, 0x8] - adds r0, r2, r0 - lsls r0, 16 - lsrs r0, 16 - str r0, [sp, 0x8] - movs r0, 0xA0 - lsls r0, 8 - ldr r3, [sp, 0x8] - cmp r3, r0 - bls _08000C50 - b _08000E46 -_08000C50: - ldr r0, =REG_VCOUNT - ldrb r0, [r0] - cmp r0, 0xE0 - bls _08000C5A - b _08000E46 -_08000C5A: - ldrh r0, [r1, 0xA] - cmp r0, 0x2 - beq _08000CD0 - cmp r0, 0x2 - bgt _08000C80 - cmp r0, 0x1 - beq _08000C8C - b _08000DF0 - .pool -_08000C80: - cmp r0, 0x3 - beq _08000D3C - cmp r0, 0x4 - bne _08000C8A - b _08000D88 -_08000C8A: - b _08000DF0 -_08000C8C: - ldr r3, [r1] - mov r2, r10 - adds r0, r5, r2 - ldr r2, [r0] - ldrh r1, [r1, 0x8] - cmp r1, r6 - bhi _08000CA6 - str r3, [r7] - str r2, [r7, 0x4] - lsrs r0, r1, 2 - movs r1, 0x84 - lsls r1, 24 - b _08000DAA -_08000CA6: - ldr r4, =REG_DMA3 - str r3, [r4] - str r2, [r4, 0x4] - ldr r0, =0x84000400 - str r0, [r4, 0x8] - ldr r0, [r4, 0x8] - adds r3, r6 - adds r2, r6 - subs r1, r6 - cmp r1, r6 - bhi _08000CA6 - str r3, [r4] - str r2, [r4, 0x4] - lsrs r0, r1, 2 - movs r1, 0x84 - lsls r1, 24 - b _08000D76 - .pool -_08000CD0: - mov r3, r10 - adds r0, r5, r3 - ldr r4, [r0] - ldrh r1, [r1, 0x8] - cmp r1, r6 - bhi _08000CF4 - mov r0, r8 - adds r0, 0xC - adds r0, r5, r0 - ldr r0, [r0] - str r0, [sp] - mov r5, sp - str r5, [r7] - str r4, [r7, 0x4] - lsrs r0, r1, 2 - movs r1, 0x85 - lsls r1, 24 - b _08000DAA -_08000CF4: - mov r2, r12 - ldrb r0, [r2] - lsls r0, 4 - mov r5, r8 - adds r5, 0xC - adds r0, r5 - ldr r0, [r0] - str r0, [sp] - ldr r3, =REG_DMA3 - mov r0, sp - str r0, [r3] - str r4, [r3, 0x4] - ldr r0, =0x85000400 - str r0, [r3, 0x8] - ldr r0, [r3, 0x8] - adds r4, r6 - subs r1, r6 - cmp r1, r6 - bhi _08000CF4 - ldrb r0, [r2] - lsls r0, 4 - adds r0, r5 - ldr r0, [r0] - str r0, [sp] - mov r2, sp - str r2, [r3] - str r4, [r3, 0x4] - lsrs r0, r1, 2 - movs r1, 0x85 - lsls r1, 24 - b _08000DEA - .pool -_08000D3C: - ldr r3, [r1] - mov r2, r10 - adds r0, r5, r2 - ldr r2, [r0] - ldrh r1, [r1, 0x8] - cmp r1, r6 - bhi _08000D56 - str r3, [r7] - str r2, [r7, 0x4] - lsrs r0, r1, 1 - movs r1, 0x80 - lsls r1, 24 - b _08000DAA -_08000D56: - ldr r4, =REG_DMA3 - str r3, [r4] - str r2, [r4, 0x4] - ldr r0, =0x80000800 - str r0, [r4, 0x8] - ldr r0, [r4, 0x8] - adds r3, r6 - adds r2, r6 - subs r1, r6 - cmp r1, r6 - bhi _08000D56 - str r3, [r4] - str r2, [r4, 0x4] - lsrs r0, r1, 1 - movs r1, 0x80 - lsls r1, 24 -_08000D76: - orrs r0, r1 - str r0, [r4, 0x8] - ldr r0, [r4, 0x8] - b _08000DF0 - .pool -_08000D88: - mov r3, r10 - adds r0, r5, r3 - ldr r2, [r0] - ldrh r4, [r1, 0x8] - add r1, sp, 0x4 - cmp r4, r6 - bhi _08000DB2 - mov r0, r8 - adds r0, 0xC - adds r0, r5, r0 - ldr r0, [r0] - strh r0, [r1] - str r1, [r7] - str r2, [r7, 0x4] - lsrs r0, r4, 1 - movs r1, 0x81 - lsls r1, 24 -_08000DAA: - orrs r0, r1 - str r0, [r7, 0x8] - ldr r0, [r7, 0x8] - b _08000DF0 -_08000DB2: - mov r5, r12 - ldrb r0, [r5] - lsls r0, 4 - ldr r3, =gUnknown_0300001C - adds r0, r3 - ldr r0, [r0] - strh r0, [r1] - ldr r3, =REG_DMA3 - str r1, [r3] - str r2, [r3, 0x4] - ldr r0, =0x81000800 - str r0, [r3, 0x8] - ldr r0, [r3, 0x8] - adds r2, r6 - subs r4, r6 - cmp r4, r6 - bhi _08000DB2 - ldrb r0, [r5] - lsls r0, 4 - ldr r5, =gUnknown_0300001C - adds r0, r5 - ldr r0, [r0] - strh r0, [r1] - str r1, [r3] - str r2, [r3, 0x4] - lsrs r0, r4, 1 - movs r1, 0x81 - lsls r1, 24 -_08000DEA: - orrs r0, r1 - str r0, [r3, 0x8] - ldr r0, [r3, 0x8] -_08000DF0: - ldr r1, =gDma3Requests - mov r3, r12 - ldrb r0, [r3] - lsls r0, 4 - adds r0, r1 - mov r2, r9 - str r2, [r0] - ldrb r0, [r3] - lsls r0, 4 - add r0, r10 - str r2, [r0] - ldrb r0, [r3] - lsls r0, 4 - adds r0, r1 - movs r4, 0 - strh r2, [r0, 0x8] - ldrb r0, [r3] - lsls r0, 4 - adds r0, r1 - mov r5, r9 - strh r5, [r0, 0xA] - ldrb r0, [r3] - lsls r0, 4 - adds r1, 0xC - adds r0, r1 - mov r1, r9 - str r1, [r0] - ldrb r0, [r3] - adds r0, 0x1 - strb r0, [r3] - lsls r0, 24 - cmp r0, 0 - bge _08000E34 - strb r4, [r3] -_08000E34: - mov r2, r12 - ldrb r0, [r2] - lsls r0, 4 - ldr r3, =gDma3Requests - adds r0, r3 - ldrh r0, [r0, 0x8] - cmp r0, 0 - beq _08000E46 - b _08000C2E -_08000E46: - add sp, 0xC - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r0} - bx r0 - .pool - thumb_func_end ProcessDma3Requests - - thumb_func_start RequestDma3Copy -@ int RequestDma3Copy(void *src, void *dest, u16 size, u8 mode) -RequestDma3Copy: @ 8000E68 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - mov r12, r0 - mov r8, r1 - lsls r2, 16 - lsrs r7, r2, 16 - lsls r3, 24 - lsrs r4, r3, 24 - movs r5, 0 - ldr r1, =gDma3ManagerLocked - movs r0, 0x1 - strb r0, [r1] - ldr r0, =gDma3RequestCursor - ldrb r2, [r0] - mov r10, r1 - ldr r6, =gDma3Requests - mov r9, r6 - lsls r0, r2, 4 - adds r1, r0, r6 -_08000E94: - lsls r3, r2, 4 - ldrh r0, [r1, 0x8] - cmp r0, 0 - bne _08000ED4 - mov r0, r12 - str r0, [r1] - mov r0, r9 - adds r0, 0x4 - adds r0, r3, r0 - mov r3, r8 - str r3, [r0] - strh r7, [r1, 0x8] - cmp r4, 0x1 - bne _08000EC0 - strh r4, [r1, 0xA] - b _08000EC4 - .pool -_08000EC0: - movs r0, 0x3 - strh r0, [r1, 0xA] -_08000EC4: - movs r0, 0 - ldr r1, =gDma3ManagerLocked - strb r0, [r1] - lsls r0, r2, 16 - asrs r0, 16 - b _08000EF0 - .pool -_08000ED4: - adds r1, 0x10 - adds r2, 0x1 - cmp r2, 0x7F - ble _08000EE0 - adds r1, r6, 0 - movs r2, 0 -_08000EE0: - adds r5, 0x1 - cmp r5, 0x7F - ble _08000E94 - movs r0, 0 - mov r3, r10 - strb r0, [r3] - movs r0, 0x1 - negs r0, r0 -_08000EF0: - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - thumb_func_end RequestDma3Copy - - thumb_func_start RequestDma3Fill -@ int RequestDma3Fill(s32 value, void *dest, u16 size, u8 mode) -RequestDma3Fill: @ 8000F00 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - mov r9, r0 - mov r8, r1 - lsls r2, 16 - lsrs r7, r2, 16 - lsls r3, 24 - lsrs r5, r3, 24 - movs r6, 0 - ldr r0, =gDma3RequestCursor - ldrb r2, [r0] - ldr r1, =gDma3ManagerLocked - movs r0, 0x1 - strb r0, [r1] - mov r10, r1 - ldr r0, =gDma3Requests - mov r12, r0 - mov r4, r12 - lsls r0, r2, 4 - adds r0, 0x8 - adds r1, r0, r4 -_8000F30: - lsls r3, r2, 4 - ldrh r0, [r1] - cmp r0, 0 - bne _08000F78 - adds r0, r4, 0x4 - adds r0, r3, r0 - mov r6, r8 - str r6, [r0] - strh r7, [r1] - strh r5, [r1, 0x2] - adds r0, r4, 0 - adds r0, 0xC - adds r0, r3, r0 - mov r3, r9 - str r3, [r0] - cmp r5, 0x1 - bne _08000F64 - movs r0, 0x2 - b _08000F66 - .pool -_08000F64: - movs r0, 0x4 -_08000F66: - strh r0, [r1, 0x2] - movs r0, 0 - ldr r6, =gDma3ManagerLocked - strb r0, [r6] - lsls r0, r2, 16 - asrs r0, 16 - b _08000F96 - .pool -_08000F78: - adds r1, 0x10 - adds r2, 0x1 - cmp r2, 0x7F - ble _08000F86 - mov r1, r12 - adds r1, 0x8 - movs r2, 0 -_08000F86: - adds r6, 0x1 - cmp r6, 0x7F - ble _8000F30 - movs r0, 0 - mov r1, r10 - strb r0, [r1] - movs r0, 0x1 - negs r0, r0 -_08000F96: - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - thumb_func_end RequestDma3Fill - - thumb_func_start CheckForSpaceForDma3Request -@ int CheckForSpaceForDma3Request(s16 index) -CheckForSpaceForDma3Request: @ 8000FA4 - push {lr} - movs r2, 0 - lsls r0, 16 - asrs r1, r0, 16 - movs r3, 0x1 - negs r3, r3 - cmp r1, r3 - bne _08000FCC - ldr r1, =gDma3Requests -_08000FB6: - ldrh r0, [r1, 0x8] - cmp r0, 0 - bne _08000FD8 - adds r1, 0x10 - adds r2, 0x1 - cmp r2, 0x7F - ble _08000FB6 -_08000FC4: - movs r0, 0 - b _08000FDC - .pool -_08000FCC: - ldr r0, =gDma3Requests - lsls r1, 4 - adds r1, r0 - ldrh r0, [r1, 0x8] - cmp r0, 0 - beq _08000FC4 -_08000FD8: - movs r0, 0x1 - negs r0, r0 -_08000FDC: - pop {r1} - bx r1 - .pool - thumb_func_end CheckForSpaceForDma3Request - - .align 2, 0 @ Don't pad with nop. diff --git a/ld_script.txt b/ld_script.txt index c6c40a62f..7f06bb361 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -43,7 +43,7 @@ SECTIONS { asm/crt0.o(.text); src/main.o(.text); src/malloc.o(.text); - asm/dma3_manager.o(.text); + src/dma3_manager.o(.text); src/gpu_regs.o(.text); asm/bg.o(.text); asm/blit.o(.text); diff --git a/src/dma3_manager.c b/src/dma3_manager.c new file mode 100644 index 000000000..cd080a172 --- /dev/null +++ b/src/dma3_manager.c @@ -0,0 +1,528 @@ +#include "global.h" + +extern u8 gDma3ManagerLocked; +extern u8 gDma3RequestCursor; + +// size is 0x10 +struct DmaRequestsStruct +{ + /* 0x00 */ u8 *src; + /* 0x04 */ u8 *dest; + /* 0x08 */ u16 size; + /* 0x0A */ u16 mode; + /* 0x0C */ u32 value; +}; + +extern struct DmaRequestsStruct gDma3Requests[128]; + +void ClearDma3Requests(void) +{ + int i; + + gDma3ManagerLocked = TRUE; + gDma3RequestCursor = FALSE; + + for(i = 0; i < (u8)ARRAY_COUNT(gDma3Requests); i++) + { + gDma3Requests[i].size = 0; + gDma3Requests[i].src = 0; + gDma3Requests[i].dest = 0; + } + + gDma3ManagerLocked = FALSE; +} + +#ifdef NONMATCHING +void ProcessDma3Requests(void) +{ + // NOTE: the fillerA member of the DMA struct is actually u32 value; + // NOTE: gUnknown_0300001C is just a pointer inside the gDma3Requests structure, not a true symbol; feel free to remove + u16 total_size; + + if (gDma3ManagerLocked) + return; + + total_size = 0; + + // as long as there are DMA requests to process (unless size or vblank is an issue), do not exit + while (gDma3Requests[gDma3RequestCursor].size) + { + total_size += gDma3Requests[gDma3RequestCursor].size; + + if (total_size > 0xA000) + return; // don't do too much at once + + if (REG_VCOUNT > 224) + return;// we're about to leave vblank, stop + + switch (gDma3Requests[gDma3RequestCursor].mode) + { + case 1: // regular 32-bit copy + // _08000C8C + if(gDma3Requests[gDma3RequestCursor].size <= 0x1000) + { + DmaCopy32(3, gDma3Requests[gDma3RequestCursor].src, gDma3Requests[gDma3RequestCursor].dest, gDma3Requests[gDma3RequestCursor].size); + break; + } + while (gDma3Requests[gDma3RequestCursor].size > 0x1000) + { + DmaCopy32(3, gDma3Requests[gDma3RequestCursor].src, gDma3Requests[gDma3RequestCursor].dest, 0x1000); + gDma3Requests[gDma3RequestCursor].src += 0x1000; + gDma3Requests[gDma3RequestCursor].dest += 0x1000; + gDma3Requests[gDma3RequestCursor].size -= 0x1000; + } + DmaCopy32(3, gDma3Requests[gDma3RequestCursor].src, gDma3Requests[gDma3RequestCursor].dest, gDma3Requests[gDma3RequestCursor].size); + break; + case 2: // repeat a single 32-bit value across RAM + // _08000CD0 + while (gDma3Requests[gDma3RequestCursor].size > 0x1000) + { + DmaFill32(3, gDma3Requests[gDma3RequestCursor].value, gDma3Requests[gDma3RequestCursor].dest, 0x1000); + gDma3Requests[gDma3RequestCursor].dest += 0x1000; + gDma3Requests[gDma3RequestCursor].size -= 0x1000; + } + DmaFill32(3, gDma3Requests[gDma3RequestCursor].value, gDma3Requests[gDma3RequestCursor].dest, gDma3Requests[gDma3RequestCursor].size); + break; + case 3: // regular 16-bit copy + // _08000D3C + while (gDma3Requests[gDma3RequestCursor].size > 0x1000) + { + DmaCopy16(3, gDma3Requests[gDma3RequestCursor].src, gDma3Requests[gDma3RequestCursor].dest, 0x1000); + gDma3Requests[gDma3RequestCursor].src += 0x1000; + gDma3Requests[gDma3RequestCursor].dest += 0x1000; + gDma3Requests[gDma3RequestCursor].size -= 0x1000; + } + DmaCopy16(3, gDma3Requests[gDma3RequestCursor].src, gDma3Requests[gDma3RequestCursor].dest, gDma3Requests[gDma3RequestCursor].size); + break; + case 4: // repeat a single 16-bit value across RAM + // _08000D88 + while (gDma3Requests[gDma3RequestCursor].size > 0x1000) + { + DmaFill16(3, gDma3Requests[gDma3RequestCursor].value, gDma3Requests[gDma3RequestCursor].dest, 0x1000); + gDma3Requests[gDma3RequestCursor].dest += 0x1000; + gDma3Requests[gDma3RequestCursor].size -= 0x1000; + } + DmaFill16(3, gDma3Requests[gDma3RequestCursor].value, gDma3Requests[gDma3RequestCursor].dest, gDma3Requests[gDma3RequestCursor].size); + break; + } + gDma3Requests[gDma3RequestCursor].src = 0; + gDma3Requests[gDma3RequestCursor].dest = 0; + gDma3Requests[gDma3RequestCursor].size = 0; + gDma3Requests[gDma3RequestCursor].mode = 0; + gDma3Requests[gDma3RequestCursor].value = 0; + gDma3RequestCursor++; + + if (gDma3RequestCursor >= 128) // loop back to the first DMA request + gDma3RequestCursor = 0; + } +} +#else +__attribute__((naked)) +void ProcessDma3Requests(void) +{ + asm(".syntax unified\n\ + push {r4-r7,lr}\n\ + mov r7, r10\n\ + mov r6, r9\n\ + mov r5, r8\n\ + push {r5-r7}\n\ + sub sp, 0xC\n\ + ldr r0, =gDma3ManagerLocked\n\ + ldrb r0, [r0]\n\ + cmp r0, 0\n\ + beq _08000C06\n\ + b _08000E46\n\ +_08000C06:\n\ + movs r0, 0\n\ + str r0, [sp, 0x8]\n\ + ldr r1, =gDma3Requests\n\ + ldr r2, =gDma3RequestCursor\n\ + ldrb r0, [r2]\n\ + lsls r0, 4\n\ + adds r0, r1\n\ + ldrh r0, [r0, 0x8]\n\ + mov r12, r2\n\ + cmp r0, 0\n\ + bne _08000C1E\n\ + b _08000E46\n\ +_08000C1E:\n\ + mov r8, r1\n\ + adds r1, 0x4\n\ + mov r10, r1\n\ + movs r6, 0x80\n\ + lsls r6, 5\n\ + ldr r7, =0x040000D4 @REG_DMA3\n\ + movs r2, 0\n\ + mov r9, r2\n\ +_08000C2E:\n\ + mov r3, r12 @ gDma3RequestCursor\n\ + ldrb r0, [r3]\n\ + lsls r5, r0, 4\n\ + mov r0, r8 @ gDma3Requests\n\ + adds r1, r5, r0 @ gDma3Requests[gDma3RequestCursor]\n\ + ldrh r0, [r1, 0x8] @ gDma3Requests[gDma3RequestCursor].size\n\ + ldr r2, [sp, 0x8]\n\ + adds r0, r2, r0\n\ + lsls r0, 16\n\ + lsrs r0, 16\n\ + str r0, [sp, 0x8]\n\ + movs r0, 0xA0\n\ + lsls r0, 8\n\ + ldr r3, [sp, 0x8]\n\ + cmp r3, r0\n\ + bls _08000C50\n\ + b _08000E46\n\ +_08000C50:\n\ + ldr r0, =0x04000006 @REG_VCOUNT\n\ + ldrb r0, [r0]\n\ + cmp r0, 0xE0\n\ + bls _08000C5A\n\ + b _08000E46\n\ +_08000C5A:\n\ + ldrh r0, [r1, 0xA]\n\ + cmp r0, 0x2\n\ + beq _08000CD0\n\ + cmp r0, 0x2\n\ + bgt _08000C80\n\ + cmp r0, 0x1\n\ + beq _08000C8C\n\ + b _08000DF0\n\ + .pool\n\ +_08000C80:\n\ + cmp r0, 0x3\n\ + beq _08000D3C\n\ + cmp r0, 0x4\n\ + bne _08000C8A\n\ + b _08000D88\n\ +_08000C8A:\n\ + b _08000DF0\n\ +_08000C8C:\n\ + ldr r3, [r1]\n\ + mov r2, r10\n\ + adds r0, r5, r2\n\ + ldr r2, [r0]\n\ + ldrh r1, [r1, 0x8]\n\ + cmp r1, r6\n\ + bhi _08000CA6\n\ + str r3, [r7]\n\ + str r2, [r7, 0x4]\n\ + lsrs r0, r1, 2\n\ + movs r1, 0x84\n\ + lsls r1, 24\n\ + b _08000DAA\n\ +_08000CA6:\n\ + ldr r4, =0x040000D4 @REG_DMA3\n\ + str r3, [r4]\n\ + str r2, [r4, 0x4]\n\ + ldr r0, =0x84000400\n\ + str r0, [r4, 0x8]\n\ + ldr r0, [r4, 0x8]\n\ + adds r3, r6\n\ + adds r2, r6\n\ + subs r1, r6\n\ + cmp r1, r6\n\ + bhi _08000CA6\n\ + str r3, [r4]\n\ + str r2, [r4, 0x4]\n\ + lsrs r0, r1, 2\n\ + movs r1, 0x84\n\ + lsls r1, 24\n\ + b _08000D76\n\ + .pool\n\ +_08000CD0:\n\ + mov r3, r10\n\ + adds r0, r5, r3\n\ + ldr r4, [r0]\n\ + ldrh r1, [r1, 0x8]\n\ + cmp r1, r6\n\ + bhi _08000CF4\n\ + mov r0, r8\n\ + adds r0, 0xC\n\ + adds r0, r5, r0\n\ + ldr r0, [r0]\n\ + str r0, [sp]\n\ + mov r5, sp\n\ + str r5, [r7]\n\ + str r4, [r7, 0x4]\n\ + lsrs r0, r1, 2\n\ + movs r1, 0x85\n\ + lsls r1, 24\n\ + b _08000DAA\n\ +_08000CF4:\n\ + mov r2, r12\n\ + ldrb r0, [r2]\n\ + lsls r0, 4\n\ + mov r5, r8\n\ + adds r5, 0xC\n\ + adds r0, r5\n\ + ldr r0, [r0]\n\ + str r0, [sp]\n\ + ldr r3, =0x040000D4 @REG_DMA3\n\ + mov r0, sp\n\ + str r0, [r3]\n\ + str r4, [r3, 0x4]\n\ + ldr r0, =0x85000400\n\ + str r0, [r3, 0x8]\n\ + ldr r0, [r3, 0x8]\n\ + adds r4, r6\n\ + subs r1, r6\n\ + cmp r1, r6\n\ + bhi _08000CF4\n\ + ldrb r0, [r2]\n\ + lsls r0, 4\n\ + adds r0, r5\n\ + ldr r0, [r0]\n\ + str r0, [sp]\n\ + mov r2, sp\n\ + str r2, [r3]\n\ + str r4, [r3, 0x4]\n\ + lsrs r0, r1, 2\n\ + movs r1, 0x85\n\ + lsls r1, 24\n\ + b _08000DEA\n\ + .pool\n\ +_08000D3C:\n\ + ldr r3, [r1]\n\ + mov r2, r10\n\ + adds r0, r5, r2\n\ + ldr r2, [r0]\n\ + ldrh r1, [r1, 0x8]\n\ + cmp r1, r6\n\ + bhi _08000D56\n\ + str r3, [r7]\n\ + str r2, [r7, 0x4]\n\ + lsrs r0, r1, 1\n\ + movs r1, 0x80\n\ + lsls r1, 24\n\ + b _08000DAA\n\ +_08000D56:\n\ + ldr r4, =0x040000D4 @REG_DMA3\n\ + str r3, [r4]\n\ + str r2, [r4, 0x4]\n\ + ldr r0, =0x80000800\n\ + str r0, [r4, 0x8]\n\ + ldr r0, [r4, 0x8]\n\ + adds r3, r6\n\ + adds r2, r6\n\ + subs r1, r6\n\ + cmp r1, r6\n\ + bhi _08000D56\n\ + str r3, [r4]\n\ + str r2, [r4, 0x4]\n\ + lsrs r0, r1, 1\n\ + movs r1, 0x80\n\ + lsls r1, 24\n\ +_08000D76:\n\ + orrs r0, r1\n\ + str r0, [r4, 0x8]\n\ + ldr r0, [r4, 0x8]\n\ + b _08000DF0\n\ + .pool\n\ +_08000D88:\n\ + mov r3, r10\n\ + adds r0, r5, r3\n\ + ldr r2, [r0]\n\ + ldrh r4, [r1, 0x8]\n\ + add r1, sp, 0x4\n\ + cmp r4, r6\n\ + bhi _08000DB2\n\ + mov r0, r8\n\ + adds r0, 0xC\n\ + adds r0, r5, r0\n\ + ldr r0, [r0]\n\ + strh r0, [r1]\n\ + str r1, [r7]\n\ + str r2, [r7, 0x4]\n\ + lsrs r0, r4, 1\n\ + movs r1, 0x81\n\ + lsls r1, 24\n\ +_08000DAA:\n\ + orrs r0, r1\n\ + str r0, [r7, 0x8]\n\ + ldr r0, [r7, 0x8]\n\ + b _08000DF0\n\ +_08000DB2:\n\ + mov r5, r12\n\ + ldrb r0, [r5]\n\ + lsls r0, 4\n\ + ldr r3, =gUnknown_0300001C\n\ + adds r0, r3\n\ + ldr r0, [r0]\n\ + strh r0, [r1]\n\ + ldr r3, =0x040000D4 @REG_DMA3\n\ + str r1, [r3]\n\ + str r2, [r3, 0x4]\n\ + ldr r0, =0x81000800\n\ + str r0, [r3, 0x8]\n\ + ldr r0, [r3, 0x8]\n\ + adds r2, r6\n\ + subs r4, r6\n\ + cmp r4, r6\n\ + bhi _08000DB2\n\ + ldrb r0, [r5]\n\ + lsls r0, 4\n\ + ldr r5, =gUnknown_0300001C\n\ + adds r0, r5\n\ + ldr r0, [r0]\n\ + strh r0, [r1]\n\ + str r1, [r3]\n\ + str r2, [r3, 0x4]\n\ + lsrs r0, r4, 1\n\ + movs r1, 0x81\n\ + lsls r1, 24\n\ +_08000DEA:\n\ + orrs r0, r1\n\ + str r0, [r3, 0x8]\n\ + ldr r0, [r3, 0x8]\n\ +_08000DF0:\n\ + ldr r1, =gDma3Requests\n\ + mov r3, r12\n\ + ldrb r0, [r3]\n\ + lsls r0, 4\n\ + adds r0, r1\n\ + mov r2, r9\n\ + str r2, [r0]\n\ + ldrb r0, [r3]\n\ + lsls r0, 4\n\ + add r0, r10\n\ + str r2, [r0]\n\ + ldrb r0, [r3]\n\ + lsls r0, 4\n\ + adds r0, r1\n\ + movs r4, 0\n\ + strh r2, [r0, 0x8]\n\ + ldrb r0, [r3]\n\ + lsls r0, 4\n\ + adds r0, r1\n\ + mov r5, r9\n\ + strh r5, [r0, 0xA]\n\ + ldrb r0, [r3]\n\ + lsls r0, 4\n\ + adds r1, 0xC\n\ + adds r0, r1\n\ + mov r1, r9\n\ + str r1, [r0]\n\ + ldrb r0, [r3]\n\ + adds r0, 0x1\n\ + strb r0, [r3]\n\ + lsls r0, 24\n\ + cmp r0, 0\n\ + bge _08000E34\n\ + strb r4, [r3]\n\ +_08000E34:\n\ + mov r2, r12\n\ + ldrb r0, [r2]\n\ + lsls r0, 4\n\ + ldr r3, =gDma3Requests\n\ + adds r0, r3\n\ + ldrh r0, [r0, 0x8]\n\ + cmp r0, 0\n\ + beq _08000E46\n\ + b _08000C2E\n\ +_08000E46:\n\ + add sp, 0xC\n\ + pop {r3-r5}\n\ + mov r8, r3\n\ + mov r9, r4\n\ + mov r10, r5\n\ + pop {r4-r7}\n\ + pop {r0}\n\ + bx r0\n\ + .pool\n\ + .syntax divided"); +} +#endif + +int RequestDma3Copy(void *src, void *dest, u16 size, u8 mode) +{ + int cursor; + int var = 0; + + gDma3ManagerLocked = 1; + + cursor = gDma3RequestCursor; + while(1) + { + if(!gDma3Requests[cursor].size) // an empty copy was found and the current cursor will be returned. + { + gDma3Requests[cursor].src = src; + gDma3Requests[cursor].dest = dest; + gDma3Requests[cursor].size = size; + + if(mode == 1) + gDma3Requests[cursor].mode = mode; + else + gDma3Requests[cursor].mode = 3; + + gDma3ManagerLocked = FALSE; + return (s16)cursor; + } + if(++cursor >= 0x80) // loop back to start. + { + cursor = 0; + } + if(++var >= 0x80) // max checks were made. all resulted in failure. + { + break; + } + } + gDma3ManagerLocked = FALSE; + return -1; +} + +int RequestDma3Fill(s32 value, void *dest, u16 size, u8 mode) +{ + int cursor; + int var = 0; + + cursor = gDma3RequestCursor; + gDma3ManagerLocked = 1; + + while(1) + { + if(!gDma3Requests[cursor].size) + { + gDma3Requests[cursor].dest = dest; + gDma3Requests[cursor].size = size; + gDma3Requests[cursor].mode = mode; + gDma3Requests[cursor].value = value; + + if(mode == 1) + gDma3Requests[cursor].mode = 2; + else + gDma3Requests[cursor].mode = 4; + + gDma3ManagerLocked = FALSE; + return (s16)cursor; + } + if(++cursor >= 0x80) // loop back to start. + { + cursor = 0; + } + if(++var >= 0x80) // max checks were made. all resulted in failure. + { + break; + } + } + gDma3ManagerLocked = FALSE; + return -1; +} + +int CheckForSpaceForDma3Request(s16 index) +{ + int current = 0; + + if (index == -1) + { + for (; current < 0x80; current ++) + if (gDma3Requests[current].size) + return -1; + + return 0; + } + + if (gDma3Requests[index].size) + return -1; + + return 0; +}