From af472505c60f7a0463e8195e9039af4806b9d42b Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sat, 29 May 2021 20:57:36 -0400 Subject: Document game_init.c --- arm9/src/error_message_reset.c | 2 +- arm9/src/game_init.c | 251 ++++++++++++++++++++++------------------- arm9/src/main.c | 10 +- arm9/src/options.c | 6 +- arm9/src/unk_02016B94.c | 4 +- arm9/src/unk_0202F150.c | 2 +- arm9/src/unk_02031734.c | 14 +-- 7 files changed, 155 insertions(+), 134 deletions(-) (limited to 'arm9/src') diff --git a/arm9/src/error_message_reset.c b/arm9/src/error_message_reset.c index 28627d6b..9996cbb1 100644 --- a/arm9/src/error_message_reset.c +++ b/arm9/src/error_message_reset.c @@ -61,7 +61,7 @@ THUMB_FUNC void PrintErrorMessageAndReset() reg_GX_DISPCNT &= 0xFFFFE0FF; reg_GXS_DB_DISPCNT &= 0xFFFFE0FF; - FUN_0201669C(4, 8); + SetKeyRepeatTimers(4, 8); gMain.unk65 = 0; GX_SwapDisplay(); diff --git a/arm9/src/game_init.c b/arm9/src/game_init.c index 64ea7a54..2b8f8c8f 100644 --- a/arm9/src/game_init.c +++ b/arm9/src/game_init.c @@ -30,7 +30,7 @@ OS_DTCM; static struct { void * contents; u32 name_hash; -} UNK_021C4928[128]; +} sFileCache[128]; struct Main gMain; @@ -136,7 +136,7 @@ void FUN_02015FC8(void) { csum += digest[i]; } - csum = (csum << 24) >> 24; + csum %= 256; while (csum & 3) { csum++; @@ -189,7 +189,7 @@ void InitGraphicMemory(void) MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE); } -void * FUN_020161A4(u32 heap_id, const char * path) +void * AllocAndReadFile(u32 heap_id, const char * path) { void * ret; @@ -214,7 +214,7 @@ void * FUN_020161A4(u32 heap_id, const char * path) return ret; } -void FUN_020161F8(const char * path, void ** ptr) +void OpenAndReadWholeFile(const char * path, void ** ptr) { FSFile file; FS_InitFile(&file); @@ -227,94 +227,94 @@ void FUN_020161F8(const char * path, void ** ptr) } } -u32 FUN_02016230(const s8 * str) +u32 GetFilenameHash(const s8 * str) { u16 len = (u16)strlen(str); - u16 sp4; + u16 numWords; if ((len % 4) != 0) - sp4 = (u16)((len / 4) + 1); + numWords = (u16)((len / 4) + 1); else - sp4 = (u16)(len / 4); - u32 r7 = 0; + numWords = (u16)(len / 4); + u32 hash = 0; s32 i, j; - for (i = 0; i < sp4; i++) + for (i = 0; i < numWords; i++) { - u32 r1 = 0; + u32 curWord = 0; for (j = 0; j < 4; j++) { - int r5 = str[4 * i + j]; - if (r5 == 0) + int curChar = str[4 * i + j]; + if (curChar == 0) break; - r1 |= r5 << (8 * j); + curWord |= curChar << (8 * j); } - r7 ^= r1; + hash ^= curWord; } - return r7; + return hash; } -int FUN_020162A0(u32 a0) +int GetFileCacheId(u32 hash) { for (int i = 0; i < 128; i++) { - if (UNK_021C4928[i].name_hash == a0 && UNK_021C4928[i].contents != NULL) + if (sFileCache[i].name_hash == hash && sFileCache[i].contents != NULL) return i; } return -1; } -int FUN_020162C8(void * a0, u32 a1) +int AddFileToCache(void * contents, u32 hash) { for (int i = 0; i < 128; i++) { - if (UNK_021C4928[i].contents == NULL) + if (sFileCache[i].contents == NULL) { - UNK_021C4928[i].contents = a0; - UNK_021C4928[i].name_hash = a1; + sFileCache[i].contents = contents; + sFileCache[i].name_hash = hash; return i; } } return -1; } -void FUN_020162FC(void) +void ClearFileCache(void) { for (int i = 127; i > -1; i--) { - if (UNK_021C4928[i].contents != NULL) + if (sFileCache[i].contents != NULL) { - FreeToHeap(UNK_021C4928[i].contents); - UNK_021C4928[i].contents = NULL; - UNK_021C4928[i].name_hash = 0; + FreeToHeap(sFileCache[i].contents); + sFileCache[i].contents = NULL; + sFileCache[i].name_hash = 0; } } } -void * FUN_02016324(const s8 * str, u32 heap_id) +void * OpenFileCached(const s8 * str, u32 heap_id) { - s8 sp0[32]; + s8 filenameBuf[32]; FSFile file; void * ret; - int r5 = 0; + int skipCache = 0; if (str[0] == '!') { - strcpy(sp0, str + 1); - r5 = 1; + strcpy(filenameBuf, str + 1); + skipCache = 1; } else { - strcpy(sp0, str); + strcpy(filenameBuf, str); } - u32 r7 = FUN_02016230(sp0); - s32 r0 = FUN_020162A0(r7); - if (r0 >= 0 && r5 == 0) + u32 hash = GetFilenameHash(filenameBuf); + s32 cacheId = GetFileCacheId(hash); + if (cacheId >= 0 && skipCache == 0) { - ret = UNK_021C4928[r0].contents; + ret = sFileCache[cacheId].contents; } else { FS_InitFile(&file); - if (FS_OpenFile(&file, (const char *)sp0)) + if (FS_OpenFile(&file, (const char *)filenameBuf)) { u32 size = file.prop.file.bottom - file.prop.file.top; ret = AllocFromHeap(heap_id, size); @@ -327,8 +327,8 @@ void * FUN_02016324(const s8 * str, u32 heap_id) } } FS_CloseFile(&file); - if (r5 == 0) - FUN_020162C8(ret, r7); + if (skipCache == 0) + AddFileToCache(ret, hash); } else ret = NULL; @@ -336,24 +336,24 @@ void * FUN_02016324(const s8 * str, u32 heap_id) return ret; } -void FUN_020163BC(void) +void InitKeypadAndTouchpad(void) { TPCalibrateParam tp; - gMain.unk34 = 0; - gMain.unk38 = 0; - gMain.unk3C = 0; - gMain.unk40 = 0; - gMain.unk44 = 0; + gMain.buttonMode = 0; + gMain.heldKeysRaw = 0; + gMain.newKeysRaw = 0; + gMain.newAndRepeatedKeysRaw = 0; + gMain.heldKeys = 0; gMain.newKeys = 0; gMain.newAndRepeatedKeys = 0; - gMain.unk50 = 0; - gMain.unk54 = 8; - gMain.unk58 = 15; - gMain.unk5C = 0; - gMain.unk5E = 0; - gMain.unk60 = 0; - gMain.unk62 = 0; - gMain.unk64 = 0; + gMain.keyRepeatCounter = 0; + gMain.keyRepeatContinueDelay = 8; + gMain.keyRepeatStartDelay = 15; + gMain.touchX = 0; + gMain.touchY = 0; + gMain.touchNew = 0; + gMain.touchHeld = 0; + gMain.touchpadReadAuto = 0; TP_Init(); if (TP_GetUserInfo(&tp) == TRUE) TP_SetCalibrateParam(&tp); @@ -382,38 +382,53 @@ void FUN_02016454(u8 a0) gMain.unk67 &= ~a0; } -void FUN_02016464(void) +void ReadKeypadAndTocuhpad(void) { TPData raw, calib; if (PAD_DetectFold()) { + // Can't press any buttons while the lid is closed. gMain.newKeys = 0; - gMain.unk44 = 0; + gMain.heldKeys = 0; gMain.newAndRepeatedKeys = 0; - gMain.unk60 = 0; - gMain.unk62 = 0; + gMain.touchNew = 0; + gMain.touchHeld = 0; return; } - u32 r0 = PAD_Read(); - gMain.unk40 = gMain.unk3C = (r0 ^ gMain.unk38) & r0; - if (r0 != 0 && gMain.unk38 == r0) + + u32 padRead = PAD_Read(); + + // newKeys is all keys that were pressed on this frame but + // not the last frame. + gMain.newAndRepeatedKeysRaw = gMain.newKeysRaw = (padRead ^ gMain.heldKeysRaw) & padRead; + + // If you are holding down buttons, indicate them "repeated" every few frames + // as defined by .keyRepeatStartDelay and .keyRepeatContinueDelay. + // Same logic as gen3, but fixes the bug where the + // remapped keys are incorrectly used here. + // See also: pokeemerald/src/main.c:ReadKeys + if (padRead != 0 && gMain.heldKeysRaw == padRead) { - if (--gMain.unk50 == 0) + if (--gMain.keyRepeatCounter == 0) { - gMain.unk40 = r0; - gMain.unk50 = gMain.unk54; + gMain.newAndRepeatedKeysRaw = padRead; + gMain.keyRepeatCounter = gMain.keyRepeatContinueDelay; } } else { - gMain.unk50 = gMain.unk58; + gMain.keyRepeatCounter = gMain.keyRepeatStartDelay; } - gMain.unk38 = r0; - gMain.newKeys = gMain.unk3C; - gMain.unk44 = r0; - gMain.newAndRepeatedKeys = gMain.unk40; - FUN_02016568(); - if (gMain.unk64 == 0) + gMain.heldKeysRaw = padRead; + + // Apply the button mode option to the read key input + gMain.newKeys = gMain.newKeysRaw; + gMain.heldKeys = padRead; + gMain.newAndRepeatedKeys = gMain.newAndRepeatedKeysRaw; + ApplyButtonModeToInput(); + + // Read the touchpad. New to gen 4. + if (gMain.touchpadReadAuto == 0) { while (TP_RequestRawSampling(&raw)) ; @@ -421,111 +436,117 @@ void FUN_02016464(void) else TP_GetLatestRawPointInAuto(&raw); TP_GetCalibratedPoint(&calib, &raw); - if (calib.validity == 0) + + // If the touchpad is valid, we gucci. + if (calib.validity == TP_VALIDITY_VALID) { - gMain.unk5C = calib.x; - gMain.unk5E = calib.y; + gMain.touchX = calib.x; + gMain.touchY = calib.y; } - else if (gMain.unk62) + + // If the touchpad was used last frame, salvage what we can. + else if (gMain.touchHeld) { switch (calib.validity) { - case 1: - gMain.unk5E = calib.y; + case TP_VALIDITY_INVALID_X: + gMain.touchY = calib.y; break; - case 2: - gMain.unk5C = calib.x; + case TP_VALIDITY_INVALID_Y: + gMain.touchX = calib.x; break; - case 3: + case TP_VALIDITY_INVALID_XY: break; } } + // What was read from the touchpad was not salvageable. + // Ignore touch input. else calib.touch = 0; - gMain.unk60 = (u16)((gMain.unk62 ^ calib.touch) & calib.touch); - gMain.unk62 = calib.touch; + gMain.touchNew = (u16)((gMain.touchHeld ^ calib.touch) & calib.touch); + gMain.touchHeld = calib.touch; } -void FUN_02016568(void) +void ApplyButtonModeToInput(void) { - switch (gMain.unk34) + switch (gMain.buttonMode) { case 0: // Normal break; case 1: // Start = X if (gMain.newKeys & PAD_BUTTON_START) gMain.newKeys |= PAD_BUTTON_X; - if (gMain.unk44 & PAD_BUTTON_START) - gMain.unk44 |= PAD_BUTTON_X; + if (gMain.heldKeys & PAD_BUTTON_START) + gMain.heldKeys |= PAD_BUTTON_X; if (gMain.newAndRepeatedKeys & PAD_BUTTON_START) gMain.newAndRepeatedKeys |= PAD_BUTTON_X; break; case 2: // Swap X and Y; unused in the retail game { - u32 r1 = 0; + u32 swapMask = 0; if (gMain.newKeys & PAD_BUTTON_X) { - r1 |= PAD_BUTTON_Y; + swapMask |= PAD_BUTTON_Y; } if (gMain.newKeys & PAD_BUTTON_Y) { - r1 |= PAD_BUTTON_X; + swapMask |= PAD_BUTTON_X; } - gMain.newKeys &= 0xF3FF; - gMain.newKeys |= r1; + gMain.newKeys &= ((PAD_BUTTON_X | PAD_BUTTON_Y) ^ 0xFFFF);; + gMain.newKeys |= swapMask; } { - u32 r1 = 0; - if (gMain.unk44 & PAD_BUTTON_X) + u32 swapMask = 0; + if (gMain.heldKeys & PAD_BUTTON_X) { - r1 |= PAD_BUTTON_Y; + swapMask |= PAD_BUTTON_Y; } - if (gMain.unk44 & PAD_BUTTON_Y) + if (gMain.heldKeys & PAD_BUTTON_Y) { - r1 |= PAD_BUTTON_X; + swapMask |= PAD_BUTTON_X; } - gMain.unk44 &= 0xF3FF; - gMain.unk44 |= r1; + gMain.heldKeys &= ((PAD_BUTTON_X | PAD_BUTTON_Y) ^ 0xFFFF); + gMain.heldKeys |= swapMask; } { - u32 r1 = 0; + u32 swapMask = 0; if (gMain.newAndRepeatedKeys & PAD_BUTTON_X) { - r1 |= PAD_BUTTON_Y; + swapMask |= PAD_BUTTON_Y; } if (gMain.newAndRepeatedKeys & PAD_BUTTON_Y) { - r1 |= PAD_BUTTON_X; + swapMask |= PAD_BUTTON_X; } - gMain.newAndRepeatedKeys &= 0xF3FF; - gMain.newAndRepeatedKeys |= r1; + gMain.newAndRepeatedKeys &= ((PAD_BUTTON_X | PAD_BUTTON_Y) ^ 0xFFFF); + gMain.newAndRepeatedKeys |= swapMask; } break; case 3: // L = A if (gMain.newKeys & PAD_BUTTON_L) gMain.newKeys |= PAD_BUTTON_A; - if (gMain.unk44 & PAD_BUTTON_L) - gMain.unk44 |= PAD_BUTTON_A; + if (gMain.heldKeys & PAD_BUTTON_L) + gMain.heldKeys |= PAD_BUTTON_A; if (gMain.newAndRepeatedKeys & PAD_BUTTON_L) gMain.newAndRepeatedKeys |= PAD_BUTTON_A; - gMain.newKeys &= 0xFCFF; - gMain.unk44 &= 0xFCFF; - gMain.newAndRepeatedKeys &= 0xFCFF; + gMain.newKeys &= ((PAD_BUTTON_L | PAD_BUTTON_R) ^ 0xFFFF); + gMain.heldKeys &= ((PAD_BUTTON_L | PAD_BUTTON_R) ^ 0xFFFF); + gMain.newAndRepeatedKeys &= ((PAD_BUTTON_L | PAD_BUTTON_R) ^ 0xFFFF); } } -void FUN_0201669C(int x, int y) +void SetKeyRepeatTimers(int x, int y) { - gMain.unk54 = x; - gMain.unk58 = y; + gMain.keyRepeatContinueDelay = x; + gMain.keyRepeatStartDelay = y; } -void FUN_020166A8(u8 a0) +void SetSoftResetDisableMask(u8 a0) { - gMain.unk68 |= a0; + gMain.softResetDisabled |= a0; } -void FUN_020166B8(u8 a0) +void ClearSoftResetDisableMask(u8 a0) { - gMain.unk68 &= ~a0; + gMain.softResetDisabled &= ~a0; } diff --git a/arm9/src/main.c b/arm9/src/main.c index 7a8a14d8..1421ac48 100644 --- a/arm9/src/main.c +++ b/arm9/src/main.c @@ -54,10 +54,10 @@ THUMB_FUNC void NitroMain(void) { InitSystemForTheGame(); InitGraphicMemory(); - FUN_020163BC(); + InitKeypadAndTouchpad(); FUN_02016438(0); - PM_GetBackLight((PMBackLightSwitch *)SDK_STATIC_BSS_START, NULL); + PM_GetBackLight(&gBacklightTop, NULL); FUN_02022294(); GF_InitRTCWork(); @@ -105,8 +105,8 @@ THUMB_FUNC void NitroMain(void) { FUN_02000EE8(); HandleDSLidAction(); - FUN_02016464(); - if ((gMain.unk38 & SOFT_RESET_KEY) == SOFT_RESET_KEY && !gMain.unk68) // soft reset? + ReadKeypadAndTocuhpad(); + if ((gMain.heldKeysRaw & SOFT_RESET_KEY) == SOFT_RESET_KEY && !gMain.softResetDisabled) // soft reset? { DoSoftReset(0); // soft reset? } @@ -249,7 +249,7 @@ THUMB_FUNC void FUN_02000F4C(u32 arg0, u32 arg1) while (1) { HandleDSLidAction(); - FUN_02016464(); + ReadKeypadAndTocuhpad(); if (gMain.newKeys & 1) break; FUN_02000E9C(); diff --git a/arm9/src/options.c b/arm9/src/options.c index 296b398a..03b63864 100644 --- a/arm9/src/options.c +++ b/arm9/src/options.c @@ -35,14 +35,14 @@ void Options_SetButtonModeOnMain(struct SaveBlock2 * sav2, u32 buttonMode) { switch (buttonMode) { case 1: - gMain.unk34 = 1; + gMain.buttonMode = 1; break; case 2: - gMain.unk34 = 3; + gMain.buttonMode = 3; break; case 0: default: - gMain.unk34 = 0; + gMain.buttonMode = 0; break; } } diff --git a/arm9/src/unk_02016B94.c b/arm9/src/unk_02016B94.c index 6a9dd194..19ec831d 100644 --- a/arm9/src/unk_02016B94.c +++ b/arm9/src/unk_02016B94.c @@ -7745,7 +7745,7 @@ THUMB_FUNC void FUN_0201AB24(struct Window *window, u8 param1) THUMB_FUNC u32 FUN_0201AB28(struct Window *window, u32 heap_id, const char *path) { - void *ptr = FUN_020161A4(heap_id, path); + void *ptr = AllocAndReadFile(heap_id, path); window->unk00 = ptr; u32 st0; NNS_G2dGetUnpackedBGCharacterData(ptr, &st0); @@ -7755,7 +7755,7 @@ THUMB_FUNC u32 FUN_0201AB28(struct Window *window, u32 heap_id, const char *path THUMB_FUNC u32 FUN_0201AB44(struct Window *window, u32 heap_id, const char *path) { - void *ptr = FUN_020161A4(heap_id, path); + void *ptr = AllocAndReadFile(heap_id, path); window->unk00 = ptr; u32 st0; NNS_G2dGetUnpackedPaletteData(ptr, &st0); diff --git a/arm9/src/unk_0202F150.c b/arm9/src/unk_0202F150.c index 09b5b56f..ef9a6193 100644 --- a/arm9/src/unk_0202F150.c +++ b/arm9/src/unk_0202F150.c @@ -535,7 +535,7 @@ THUMB_FUNC BOOL FUN_0202FB80() { UNK_021C59F4.unk00 = 0; FUN_0202F984(); - UNK_021C59F4.unk04->unk628 |= gMain.unk44 & 0x7FFF; + UNK_021C59F4.unk04->unk628 |= gMain.heldKeys & 0x7FFF; FUN_02030674(); FUN_0202FCCC(); diff --git a/arm9/src/unk_02031734.c b/arm9/src/unk_02031734.c index 57764b82..49283907 100644 --- a/arm9/src/unk_02031734.c +++ b/arm9/src/unk_02031734.c @@ -1807,7 +1807,7 @@ THUMB_FUNC void FUN_020331C4() { if (UNK_021C5A00 != NULL) { - FUN_020166B8(1); + ClearSoftResetDisableMask(1); FUN_02031D20(FUN_02032440, 5); } } @@ -1875,7 +1875,7 @@ THUMB_FUNC void FUN_02033288(struct SaveBlock2 *sav2) UNK_021C5A00->unk46 = 1; UNK_021C5A00->unk28 = sav2; FUN_020334E8(0, 1); - FUN_020166A8(1); + SetSoftResetDisableMask(1); } } @@ -1883,7 +1883,7 @@ THUMB_FUNC void FUN_020332DC() { if (UNK_021C5A00 != 0) { - FUN_020166B8(1); + ClearSoftResetDisableMask(1); FUN_020334E8(0, 0); FreeToHeap(UNK_021C5A00); UNK_021C5A00 = NULL; @@ -1903,7 +1903,7 @@ THUMB_FUNC void FUN_02033310(struct SaveBlock2 *sav2) UNK_021C5A00->unk46 = 1; UNK_021C5A00->unk28 = sav2; FUN_020334E8(0, 1); - FUN_020166A8(1); + SetSoftResetDisableMask(1); } } @@ -1911,7 +1911,7 @@ THUMB_FUNC void FUN_02033364() { if (UNK_021C5A00 != 0) { - FUN_020166B8(1); + ClearSoftResetDisableMask(1); FUN_020334E8(0, 0); FreeToHeap(UNK_021C5A00); UNK_021C5A00 = NULL; @@ -1985,7 +1985,7 @@ THUMB_FUNC void *FUN_0203346C(struct SaveBlock2 *sav2, u32 param1) return 0; } - FUN_020166A8(1); + SetSoftResetDisableMask(1); FUN_02016828(3, 0xf, 0x7080); FUN_02031734(sav2, 0x17); UNK_021C5A00->unk00 = AllocFromHeap(0xf, param1); @@ -2127,7 +2127,7 @@ THUMB_FUNC void FUN_020335F4() FUN_0200541C(); FUN_0202287C(UNK_021C5A00->unk28); - gMain.unk64 = 1; + gMain.touchpadReadAuto = 1; if (UNK_021C5A00->unk4E == 3) { FUN_020335A4(3); -- cgit v1.2.3 From 9da7dd90041b785cb156069ccdf96996d9a162e2 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sun, 30 May 2021 08:54:50 -0400 Subject: Refactor heap.c using NNS types, 1 --- arm9/src/error_message_reset.c | 12 +-- arm9/src/game_init.c | 33 ++++---- arm9/src/heap.c | 182 +++++++++++++++++++---------------------- arm9/src/unk_02016B94.c | 6 +- 4 files changed, 109 insertions(+), 124 deletions(-) (limited to 'arm9/src') diff --git a/arm9/src/error_message_reset.c b/arm9/src/error_message_reset.c index 9996cbb1..f028f038 100644 --- a/arm9/src/error_message_reset.c +++ b/arm9/src/error_message_reset.c @@ -8,7 +8,9 @@ const struct UnkStruct_02016B94_4 UNK_020FF49C = { 0, 3, 3, 0x1a, 0x12, 1, 0x23 }; -const u32 UNK_020FF4A4[2] = { 0x00020000, 0x00000000 }; +const struct UnkStruct_020EDB10 UNK_020FF4A4[] = { + {0x00020000, OS_ARENA_MAIN} +}; const struct GraphicsModes UNK_020FF4AC = { mode1 : 1 }; @@ -44,13 +46,13 @@ THUMB_FUNC void PrintErrorMessageAndReset() OS_SetArenaHi(OS_ARENA_MAIN, OS_GetInitArenaHi(OS_ARENA_MAIN)); OS_SetArenaLo(OS_ARENA_MAIN, OS_GetInitArenaLo(OS_ARENA_MAIN)); - FUN_020166C8((u32 *)UNK_020FF4A4, 1, 1, 0); + FUN_020166C8(UNK_020FF4A4, NELEMS(UNK_020FF4A4), 1, 0); FUN_0200E3A0(PM_LCD_TOP, 0); FUN_0200E3A0(PM_LCD_BOTTOM, 0); - OS_DisableIrqMask(1); - OS_SetIrqFunction(1, &VBlankHandler); - OS_EnableIrqMask(1); + OS_DisableIrqMask(OS_IE_V_BLANK); + OS_SetIrqFunction(OS_IE_V_BLANK, &VBlankHandler); + OS_EnableIrqMask(OS_IE_V_BLANK); Main_SetVBlankIntrCB(NULL, NULL); diff --git a/arm9/src/game_init.c b/arm9/src/game_init.c index 2b8f8c8f..05b1f9bd 100644 --- a/arm9/src/game_init.c +++ b/arm9/src/game_init.c @@ -10,6 +10,7 @@ #include "game_init.h" #include "registers.h" #include "heap.h" +#include "OS_interrupt.h" #pragma thumb on @@ -34,7 +35,7 @@ static struct { struct Main gMain; -void Main_HBlankIntr(BOOL); +void Main_ToggleHBlankInterrupt(BOOL enableFlag); void FUN_0201B5CC(void *); void FUN_02015EA0(void) @@ -66,7 +67,7 @@ void Main_SetVBlankIntrCB(void (*a0)(void *), void * a1) void FUN_02015F1C(void) { - Main_HBlankIntr(FALSE); + Main_ToggleHBlankInterrupt(FALSE); gMain.hBlankIntr = NULL; gMain.hBlankIntrArg = NULL; } @@ -75,7 +76,7 @@ BOOL FUN_02015F34(void (*a0)(void *), void * a1) { if (a0 == 0) { - Main_HBlankIntr(FALSE); + Main_ToggleHBlankInterrupt(FALSE); gMain.hBlankIntr = NULL; gMain.hBlankIntrArg = NULL; return TRUE; @@ -84,7 +85,7 @@ BOOL FUN_02015F34(void (*a0)(void *), void * a1) { gMain.hBlankIntrArg = a1; gMain.hBlankIntr = a0; - Main_HBlankIntr(TRUE); + Main_ToggleHBlankInterrupt(TRUE); return TRUE; } else @@ -93,36 +94,36 @@ BOOL FUN_02015F34(void (*a0)(void *), void * a1) } } -void FUN_02015F6C(void) +void Main_CallHBlankCallback(void) { if (gMain.hBlankIntr != NULL) gMain.hBlankIntr(gMain.hBlankIntrArg); } -void Main_HBlankIntr(BOOL a0) +void Main_ToggleHBlankInterrupt(BOOL enableFlag) { (void)OS_DisableIrq(); - if (!a0) + if (!enableFlag) { (void)OS_GetIrqMask(); - OS_DisableIrqMask(2); + OS_DisableIrqMask(OS_IE_H_BLANK); GX_HBlankIntr(FALSE); } else { (void)OS_GetIrqMask(); - OS_SetIrqFunction(2, FUN_02015F6C); - OS_EnableIrqMask(2); + OS_SetIrqFunction(OS_IE_H_BLANK, Main_CallHBlankCallback); + OS_EnableIrqMask(OS_IE_H_BLANK); GX_HBlankIntr(TRUE); } (void)OS_EnableIrq(); } -const u32 UNK_020EDB10[][2] = { - { 0x0000D000, 0x00000000 }, - { 0x00021000, 0x00000000 }, - { 0x00001000, 0x00000000 }, - { 0x0010D800, 0x00000000 } +const struct UnkStruct_020EDB10 UNK_020EDB10[] = { + { 0x00D000, OS_ARENA_MAIN }, + { 0x021000, OS_ARENA_MAIN }, + { 0x001000, OS_ARENA_MAIN }, + { 0x10D800, OS_ARENA_MAIN } }; void FUN_02015FC8(void) @@ -141,7 +142,7 @@ void FUN_02015FC8(void) { csum++; } - FUN_020166C8((u32 *)UNK_020EDB10, 4, 92, csum); + FUN_020166C8(UNK_020EDB10, NELEMS(UNK_020EDB10), 92, csum); } void InitSystemForTheGame(void) diff --git a/arm9/src/heap.c b/arm9/src/heap.c index 7f35fdcc..93ecefa4 100644 --- a/arm9/src/heap.c +++ b/arm9/src/heap.c @@ -1,28 +1,20 @@ #include "heap.h" #include "error_message_reset.h" #include "unk_02031734.h" - - -extern void *NNS_FndCreateExpHeapEx(void *param0, u32 param1, u32 param2); -extern void *NNS_FndAllocFromExpHeapEx(void *param0, u32 param1, s32 param2); -extern void NNS_FndDestroyExpHeap(); -extern void NNS_FndFreeToExpHeap(void *ptr1, void *ptr2); -extern u32 NNS_FndGetTotalFreeSizeForExpHeap(void *param0); -extern void NNS_FndInitAllocatorForExpHeap(u32 param0, void *param1, u32 param2); -extern u32 NNS_FndGetSizeForMBlockExpHeap(void *param0); -extern void NNS_FndResizeForMBlockExpHeap(void *ptr1, void *ptr2, u32 param2); - +#include "NNS_FND_expheap.h" struct UnkStruct_020166C8 UNK_021C4D28; - -THUMB_FUNC void FUN_020166C8(u32 *param0, u32 param1, u32 param2, u32 pre_size) +THUMB_FUNC void FUN_020166C8(const struct UnkStruct_020EDB10 *templates, u32 nTemplates, u32 totalNumHeaps, u32 pre_size) { - u32 unk_size = param1 + 24; + void * ptr; + u32 unk_size, r7; - if (param2 < unk_size) + unk_size = nTemplates + 24; + + if (totalNumHeaps < unk_size) { - param2 = unk_size; + totalNumHeaps = unk_size; } if (pre_size != 0) { @@ -34,90 +26,80 @@ THUMB_FUNC void FUN_020166C8(u32 *param0, u32 param1, u32 param2, u32 pre_size) OS_AllocFromArenaLo(OS_ARENA_MAIN, pre_size, 4); } - u32 r7 = param2 * 2; - - void *ptr = OS_AllocFromArenaLo(OS_ARENA_MAIN, (unk_size * 3 + 1) * sizeof(u32) + r7 + param2, 4); - UNK_021C4D28.unk00 = ptr; - ptr += (unk_size + 1) * 4; - UNK_021C4D28.unk04 = ptr; - ptr += unk_size * 4; - UNK_021C4D28.unk08 = ptr; - ptr += unk_size * 4; - UNK_021C4D28.unk0c = ptr; - ptr += r7; - UNK_021C4D28.unk10 = ptr; - UNK_021C4D28.unk14 = (u16)param2; - UNK_021C4D28.unk16 = (u16)param1; - - r7 = 0; + UNK_021C4D28.heapHandles = (NNSFndHeapHandle*) OS_AllocFromArenaLo( + OS_ARENA_MAIN, + (unk_size + 1) * sizeof(NNSFndHeapHandle) + + unk_size * sizeof(NNSFndHeapHandle) + + unk_size * sizeof(void *) + + totalNumHeaps * sizeof(u16) + + totalNumHeaps, + 4 + ); + UNK_021C4D28.unk04 = UNK_021C4D28.heapHandles + (unk_size + 1); + UNK_021C4D28.unk08 = (void **)(UNK_021C4D28.unk04 + unk_size); + UNK_021C4D28.unk0c = (u16 *)(UNK_021C4D28.unk08 + unk_size); + UNK_021C4D28.heapIdxs = (u8 *)(UNK_021C4D28.unk0c + totalNumHeaps); + UNK_021C4D28.totalNumHeaps = (u16)totalNumHeaps; + UNK_021C4D28.nTemplates = (u16)nTemplates; + UNK_021C4D28.unk1a = (u16)unk_size; UNK_021C4D28.unk18 = (u16)unk_size; - while (r7 < param1) + for (r7 = 0; r7 < nTemplates; r7++) { - void *ptr; - if (param0[1] == 0 || param0[1] != 2) - { - ptr = OS_AllocFromArenaLo(OS_ARENA_MAIN, param0[0], 4); - } - else + switch (templates[r7].arena) { - ptr = OS_AllocFromArenaHi(OS_ARENA_MAINEX, param0[0], 4); + case OS_ARENA_MAIN: + default: + ptr = OS_AllocFromArenaLo(OS_ARENA_MAIN, templates[r7].size, 4); + break; + case OS_ARENA_MAINEX: + ptr = OS_AllocFromArenaHi(OS_ARENA_MAINEX, templates[r7].size, 4); + break; } - if (ptr != 0) + if (ptr != NULL) { - UNK_021C4D28.unk00[r7] = NNS_FndCreateExpHeapEx(ptr, param0[0], 0); - UNK_021C4D28.unk10[r7] = (u8)r7; + UNK_021C4D28.heapHandles[r7] = NNS_FndCreateExpHeapEx(ptr, templates[r7].size, 0); + UNK_021C4D28.heapIdxs[r7] = (u8)r7; } else { - GF_AssertFail(); + GF_ASSERT(0); } - - param0 += 2; - r7++; } - while (param1 < unk_size + 1) + while (nTemplates < unk_size + 1) { - UNK_021C4D28.unk00[param1] = 0; - UNK_021C4D28.unk10[param1] = (u8)UNK_021C4D28.unk1a; + UNK_021C4D28.heapHandles[nTemplates] = NULL; + UNK_021C4D28.heapIdxs[nTemplates] = (u8)UNK_021C4D28.unk1a; - param1++; + nTemplates++; } - while (param1 < param2) + while (nTemplates < totalNumHeaps) { - UNK_021C4D28.unk10[param1] = (u8)UNK_021C4D28.unk1a; + UNK_021C4D28.heapIdxs[nTemplates] = (u8)UNK_021C4D28.unk1a; - param1++; + nTemplates++; } - for (param1 = 0; param1 < param2; param1++) + for (nTemplates = 0; nTemplates < totalNumHeaps; nTemplates++) { - UNK_021C4D28.unk0c[param1] = 0; + UNK_021C4D28.unk0c[nTemplates] = 0; } } THUMB_FUNC s32 FUN_020167F4() { - s32 i = UNK_021C4D28.unk16; - s32 j = UNK_021C4D28.unk18; + s32 i; + s32 j; - if (i < j) + for (i = UNK_021C4D28.nTemplates; i < UNK_021C4D28.unk18; i++) { - void **ptr = UNK_021C4D28.unk00 + i; - do - { - if (*ptr == 0) - { - return i; - } - i++; - ptr++; - } while (i < j); + if (UNK_021C4D28.heapHandles[i] == NULL) + return i; } return -1; @@ -137,10 +119,10 @@ THUMB_FUNC u32 FUN_02016834(u32 param0, u32 param1, u32 param2, s32 param3) { GF_ASSERT(OS_GetProcMode() != OS_PROCMODE_IRQ); - u8 *ptr = UNK_021C4D28.unk10; + u8 *ptr = UNK_021C4D28.heapIdxs; if (UNK_021C4D28.unk1a == ptr[param1]) { - void *ptr2 = UNK_021C4D28.unk00[ptr[param0]]; + void *ptr2 = UNK_021C4D28.heapHandles[ptr[param0]]; if (ptr2 != 0) { void *ptr3 = NNS_FndAllocFromExpHeapEx(ptr2, param2, param3); @@ -149,14 +131,14 @@ THUMB_FUNC u32 FUN_02016834(u32 param0, u32 param1, u32 param2, s32 param3) param3 = FUN_020167F4(); if (param3 >= 0) { - UNK_021C4D28.unk00[param3] = NNS_FndCreateExpHeapEx(ptr3, param2, 0); + UNK_021C4D28.heapHandles[param3] = NNS_FndCreateExpHeapEx(ptr3, param2, 0); - if (UNK_021C4D28.unk00[param3] != 0) + if (UNK_021C4D28.heapHandles[param3] != 0) { UNK_021C4D28.unk04[param3] = ptr2; UNK_021C4D28.unk08[param3] = ptr3; - UNK_021C4D28.unk10[param1] = (u8)param3; + UNK_021C4D28.heapIdxs[param1] = (u8)param3; return 1; } @@ -191,11 +173,11 @@ THUMB_FUNC void FUN_020168D0(u32 heap_id) { GF_ASSERT (OS_GetProcMode() != OS_PROCMODE_IRQ); - if (UNK_021C4D28.unk00[UNK_021C4D28.unk10[heap_id]] != 0) + if (UNK_021C4D28.heapHandles[UNK_021C4D28.heapIdxs[heap_id]] != 0) { NNS_FndDestroyExpHeap(); - u8 index = UNK_021C4D28.unk10[heap_id]; + u8 index = UNK_021C4D28.heapIdxs[heap_id]; void *ptr1 = UNK_021C4D28.unk04[index]; void *ptr2 = UNK_021C4D28.unk08[index]; if (ptr1 != 0 && ptr2 != 0) @@ -207,11 +189,11 @@ THUMB_FUNC void FUN_020168D0(u32 heap_id) GF_AssertFail(); } - UNK_021C4D28.unk00[UNK_021C4D28.unk10[heap_id]] = 0; - UNK_021C4D28.unk04[UNK_021C4D28.unk10[heap_id]] = 0; - UNK_021C4D28.unk08[UNK_021C4D28.unk10[heap_id]] = 0; + UNK_021C4D28.heapHandles[UNK_021C4D28.heapIdxs[heap_id]] = 0; + UNK_021C4D28.unk04[UNK_021C4D28.heapIdxs[heap_id]] = 0; + UNK_021C4D28.unk08[UNK_021C4D28.heapIdxs[heap_id]] = 0; - UNK_021C4D28.unk10[heap_id] = (u8)UNK_021C4D28.unk1a; + UNK_021C4D28.heapIdxs[heap_id] = (u8)UNK_021C4D28.unk1a; } } @@ -245,10 +227,10 @@ THUMB_FUNC void FUN_02016988() void *AllocFromHeap(u32 heap_id, u32 size) { void *ptr = 0; - if (heap_id < UNK_021C4D28.unk14) + if (heap_id < UNK_021C4D28.totalNumHeaps) { - u8 index = UNK_021C4D28.unk10[heap_id]; - ptr = FUN_02016944(UNK_021C4D28.unk00[index], size, 4, heap_id); + u8 index = UNK_021C4D28.heapIdxs[heap_id]; + ptr = FUN_02016944(UNK_021C4D28.heapHandles[index], size, 4, heap_id); } if (ptr != 0) { @@ -265,10 +247,10 @@ void *AllocFromHeap(u32 heap_id, u32 size) void *AllocFromHeapAtEnd(u32 heap_id, u32 size) { void *ptr = 0; - if (heap_id < UNK_021C4D28.unk14) + if (heap_id < UNK_021C4D28.totalNumHeaps) { - u8 index = UNK_021C4D28.unk10[heap_id]; - ptr = FUN_02016944(UNK_021C4D28.unk00[index], size, -4, heap_id); + u8 index = UNK_021C4D28.heapIdxs[heap_id]; + ptr = FUN_02016944(UNK_021C4D28.heapHandles[index], size, -4, heap_id); } if (ptr != 0) @@ -287,10 +269,10 @@ void FreeToHeap(void *ptr) { u8 heap_id = (u8)((u32 *)ptr)[-1]; - if ((u16)heap_id < UNK_021C4D28.unk14) + if ((u16)heap_id < UNK_021C4D28.totalNumHeaps) { - u8 index = UNK_021C4D28.unk10[heap_id]; - void *ptr2 = UNK_021C4D28.unk00[index]; + u8 index = UNK_021C4D28.heapIdxs[heap_id]; + void *ptr2 = UNK_021C4D28.heapHandles[index]; GF_ASSERT(ptr2); if (UNK_021C4D28.unk0c[heap_id] == 0) @@ -313,10 +295,10 @@ void FreeToHeapExplicit(u32 param0, void *param1) { GF_ASSERT (OS_GetProcMode() != OS_PROCMODE_IRQ); - if (param0 < UNK_021C4D28.unk14) + if (param0 < UNK_021C4D28.totalNumHeaps) { - u8 index = UNK_021C4D28.unk10[param0]; - void *ptr = UNK_021C4D28.unk00[index]; + u8 index = UNK_021C4D28.heapIdxs[param0]; + void *ptr = UNK_021C4D28.heapHandles[index]; GF_ASSERT (ptr ); u8 heap_id = (u8)((u32 *)param1)[-1]; @@ -334,10 +316,10 @@ void FreeToHeapExplicit(u32 param0, void *param1) THUMB_FUNC u32 FUN_02016AF8(u32 param0) { - if (param0 < UNK_021C4D28.unk14) + if (param0 < UNK_021C4D28.totalNumHeaps) { - u8 index = UNK_021C4D28.unk10[param0]; - return NNS_FndGetTotalFreeSizeForExpHeap(UNK_021C4D28.unk00[index]); + u8 index = UNK_021C4D28.heapIdxs[param0]; + return NNS_FndGetTotalFreeSizeForExpHeap(UNK_021C4D28.heapHandles[index]); } GF_AssertFail(); @@ -346,11 +328,11 @@ THUMB_FUNC u32 FUN_02016AF8(u32 param0) THUMB_FUNC void FUN_02016B20(u32 param0, u32 param1, u32 param2) { - if (param1 < UNK_021C4D28.unk14) + if (param1 < UNK_021C4D28.totalNumHeaps) { - u8 index = UNK_021C4D28.unk10[param1]; - NNS_FndInitAllocatorForExpHeap(param0, UNK_021C4D28.unk00[index], param2); + u8 index = UNK_021C4D28.heapIdxs[param1]; + NNS_FndInitAllocatorForExpHeap(param0, UNK_021C4D28.heapHandles[index], param2); return; } @@ -366,9 +348,9 @@ THUMB_FUNC void FUN_02016B44(void *ptr, u32 param1) { u8 heap_id = (u8)((u32 *)ptr)[-1]; - u8 index = UNK_021C4D28.unk10[heap_id]; + u8 index = UNK_021C4D28.heapIdxs[heap_id]; - NNS_FndResizeForMBlockExpHeap(UNK_021C4D28.unk00[index], ptr - 16, param1); + NNS_FndResizeForMBlockExpHeap(UNK_021C4D28.heapHandles[index], ptr - 16, param1); return; } GF_AssertFail(); diff --git a/arm9/src/unk_02016B94.c b/arm9/src/unk_02016B94.c index 19ec831d..8ad78373 100644 --- a/arm9/src/unk_02016B94.c +++ b/arm9/src/unk_02016B94.c @@ -98,7 +98,7 @@ THUMB_FUNC void FUN_02016BF4(u32 *param0, u32 param1) #ifdef NONMATCHING THUMB_FUNC void FUN_02016C18( - struct UnkStruct_02016B94_2 *param0, u8 param1, struct UnkStruct_02016B94_1 *param2, u8 param3) + struct UnkStruct_02016B94_2 *param0, u8 param1, const struct UnkStruct_02016B94_1 *param2, u8 param3) { u8 res = FUN_020177DC(param2->unk10, param3); switch (param1) @@ -342,7 +342,7 @@ THUMB_FUNC void FUN_02016C18( } #else THUMB_FUNC asm void FUN_02016C18( - struct UnkStruct_02016B94_2 *param0, u8 param1, struct UnkStruct_02016B94_1 *param2, u8 param3) + struct UnkStruct_02016B94_2 *param0, u8 param1, const struct UnkStruct_02016B94_1 *param2, u8 param3) { // clang-format off push {r3-r7, lr} @@ -4603,7 +4603,7 @@ THUMB_FUNC void FUN_020190EC(struct UnkStruct_02016B94_2 *param0, } THUMB_FUNC void FUN_02019150( - struct UnkStruct_02016B94_2 *param0, struct Window *param1, struct UnkStruct_02016B94_4 *param2) + struct UnkStruct_02016B94_2 *param0, struct Window *param1, const struct UnkStruct_02016B94_4 *param2) { FUN_02019064(param0, -- cgit v1.2.3 From 2e6a113eb27577764b1b6de22e04ae1f73d3069f Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sun, 30 May 2021 09:19:03 -0400 Subject: Refactor heap.c, 2 --- arm9/src/heap.c | 89 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 43 deletions(-) (limited to 'arm9/src') diff --git a/arm9/src/heap.c b/arm9/src/heap.c index 93ecefa4..79763865 100644 --- a/arm9/src/heap.c +++ b/arm9/src/heap.c @@ -2,6 +2,7 @@ #include "error_message_reset.h" #include "unk_02031734.h" #include "NNS_FND_expheap.h" +#include "NNS_FND_allocator.h" struct UnkStruct_020166C8 UNK_021C4D28; @@ -61,7 +62,7 @@ THUMB_FUNC void FUN_020166C8(const struct UnkStruct_020EDB10 *templates, u32 nTe if (ptr != NULL) { - UNK_021C4D28.heapHandles[r7] = NNS_FndCreateExpHeapEx(ptr, templates[r7].size, 0); + UNK_021C4D28.heapHandles[r7] = NNS_FndCreateExpHeap(ptr, templates[r7].size); UNK_021C4D28.heapIdxs[r7] = (u8)r7; } else @@ -115,56 +116,56 @@ THUMB_FUNC u32 FUN_02016828(u32 param0, u32 param1, u32 param2) return FUN_02016834(param0, param1, param2, -4); } -THUMB_FUNC u32 FUN_02016834(u32 param0, u32 param1, u32 param2, s32 param3) +THUMB_FUNC u32 FUN_02016834(u32 param0, u32 param1, u32 size, s32 alignment) { GF_ASSERT(OS_GetProcMode() != OS_PROCMODE_IRQ); u8 *ptr = UNK_021C4D28.heapIdxs; if (UNK_021C4D28.unk1a == ptr[param1]) { - void *ptr2 = UNK_021C4D28.heapHandles[ptr[param0]]; - if (ptr2 != 0) + NNSFndHeapHandle ptr2 = UNK_021C4D28.heapHandles[ptr[param0]]; + if (ptr2 != NULL) { - void *ptr3 = NNS_FndAllocFromExpHeapEx(ptr2, param2, param3); - if (ptr3 != 0) + void *ptr3 = NNS_FndAllocFromExpHeapEx(ptr2, size, alignment); + if (ptr3 != NULL) { - param3 = FUN_020167F4(); - if (param3 >= 0) + s32 i = FUN_020167F4(); + if (i >= 0) { - UNK_021C4D28.heapHandles[param3] = NNS_FndCreateExpHeapEx(ptr3, param2, 0); + UNK_021C4D28.heapHandles[i] = NNS_FndCreateExpHeap(ptr3, size); - if (UNK_021C4D28.heapHandles[param3] != 0) + if (UNK_021C4D28.heapHandles[i] != 0) { - UNK_021C4D28.unk04[param3] = ptr2; - UNK_021C4D28.unk08[param3] = ptr3; - UNK_021C4D28.heapIdxs[param1] = (u8)param3; + UNK_021C4D28.unk04[i] = ptr2; + UNK_021C4D28.unk08[i] = ptr3; + UNK_021C4D28.heapIdxs[param1] = (u8)i; - return 1; + return TRUE; } else { - GF_AssertFail(); + GF_ASSERT(0); } } else { - GF_AssertFail(); + GF_ASSERT(0); } } else { - GF_AssertFail(); + GF_ASSERT(0); } } else { - GF_AssertFail(); + GF_ASSERT(0); } } else { - GF_AssertFail(); + GF_ASSERT(0); } return 0; } @@ -173,25 +174,27 @@ THUMB_FUNC void FUN_020168D0(u32 heap_id) { GF_ASSERT (OS_GetProcMode() != OS_PROCMODE_IRQ); - if (UNK_021C4D28.heapHandles[UNK_021C4D28.heapIdxs[heap_id]] != 0) + NNSFndHeapHandle handle = UNK_021C4D28.heapHandles[UNK_021C4D28.heapIdxs[heap_id]]; + + if (handle != NULL) { - NNS_FndDestroyExpHeap(); + NNS_FndDestroyExpHeap(handle); u8 index = UNK_021C4D28.heapIdxs[heap_id]; - void *ptr1 = UNK_021C4D28.unk04[index]; + NNSFndHeapHandle ptr1 = UNK_021C4D28.unk04[index]; void *ptr2 = UNK_021C4D28.unk08[index]; - if (ptr1 != 0 && ptr2 != 0) + if (ptr1 != NULL && ptr2 != NULL) { NNS_FndFreeToExpHeap(ptr1, ptr2); } else { - GF_AssertFail(); + GF_ASSERT(0); } - UNK_021C4D28.heapHandles[UNK_021C4D28.heapIdxs[heap_id]] = 0; - UNK_021C4D28.unk04[UNK_021C4D28.heapIdxs[heap_id]] = 0; - UNK_021C4D28.unk08[UNK_021C4D28.heapIdxs[heap_id]] = 0; + UNK_021C4D28.heapHandles[UNK_021C4D28.heapIdxs[heap_id]] = NULL; + UNK_021C4D28.unk04[UNK_021C4D28.heapIdxs[heap_id]] = NULL; + UNK_021C4D28.unk08[UNK_021C4D28.heapIdxs[heap_id]] = NULL; UNK_021C4D28.heapIdxs[heap_id] = (u8)UNK_021C4D28.unk1a; } @@ -288,7 +291,7 @@ void FreeToHeap(void *ptr) return; } - GF_AssertFail(); + GF_ASSERT(0); } void FreeToHeapExplicit(u32 param0, void *param1) @@ -311,49 +314,49 @@ void FreeToHeapExplicit(u32 param0, void *param1) return; } - GF_AssertFail(); + GF_ASSERT(0); } -THUMB_FUNC u32 FUN_02016AF8(u32 param0) +THUMB_FUNC u32 GF_ExpHeap_FndGetTotalFreeSize(u32 heap_id) { - if (param0 < UNK_021C4D28.totalNumHeaps) + if (heap_id < UNK_021C4D28.totalNumHeaps) { - u8 index = UNK_021C4D28.heapIdxs[param0]; + u8 index = UNK_021C4D28.heapIdxs[heap_id]; return NNS_FndGetTotalFreeSizeForExpHeap(UNK_021C4D28.heapHandles[index]); } - GF_AssertFail(); + GF_ASSERT(0); return 0; } -THUMB_FUNC void FUN_02016B20(u32 param0, u32 param1, u32 param2) +THUMB_FUNC void GF_ExpHeap_FndInitAllocator(NNSFndAllocator * pAllocator, u32 heap_id, int alignment) { - if (param1 < UNK_021C4D28.totalNumHeaps) + if (heap_id < UNK_021C4D28.totalNumHeaps) { - u8 index = UNK_021C4D28.heapIdxs[param1]; - NNS_FndInitAllocatorForExpHeap(param0, UNK_021C4D28.heapHandles[index], param2); + u8 index = UNK_021C4D28.heapIdxs[heap_id]; + NNS_FndInitAllocatorForExpHeap(pAllocator, UNK_021C4D28.heapHandles[index], alignment); return; } - GF_AssertFail(); + GF_ASSERT(0); } -THUMB_FUNC void FUN_02016B44(void *ptr, u32 param1) +THUMB_FUNC void ReallocFromHeap(void *ptr, u32 newSize) { GF_ASSERT (OS_GetProcMode() != OS_PROCMODE_IRQ); - param1 += 16; - if (NNS_FndGetSizeForMBlockExpHeap(ptr - 16) >= param1) + newSize += 16; + if (NNS_FndGetSizeForMBlockExpHeap(ptr - 16) >= newSize) { u8 heap_id = (u8)((u32 *)ptr)[-1]; u8 index = UNK_021C4D28.heapIdxs[heap_id]; - NNS_FndResizeForMBlockExpHeap(UNK_021C4D28.heapHandles[index], ptr - 16, param1); + NNS_FndResizeForMBlockExpHeap(UNK_021C4D28.heapHandles[index], ptr - 16, newSize); return; } - GF_AssertFail(); + GF_ASSERT(0); } THUMB_FUNC u32 FUN_02016B90(u32 param0) -- cgit v1.2.3 From daf02d9b198906d93a9b289315d0fc1e519843ef Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sun, 30 May 2021 20:13:58 -0400 Subject: Document heap.c, 3 --- arm9/src/error_message_reset.c | 2 +- arm9/src/game_init.c | 2 +- arm9/src/heap.c | 239 ++++++++++++++++++++++------------------- arm9/src/unk_02031734.c | 32 +++--- 4 files changed, 147 insertions(+), 128 deletions(-) (limited to 'arm9/src') diff --git a/arm9/src/error_message_reset.c b/arm9/src/error_message_reset.c index f028f038..520a5077 100644 --- a/arm9/src/error_message_reset.c +++ b/arm9/src/error_message_reset.c @@ -46,7 +46,7 @@ THUMB_FUNC void PrintErrorMessageAndReset() OS_SetArenaHi(OS_ARENA_MAIN, OS_GetInitArenaHi(OS_ARENA_MAIN)); OS_SetArenaLo(OS_ARENA_MAIN, OS_GetInitArenaLo(OS_ARENA_MAIN)); - FUN_020166C8(UNK_020FF4A4, NELEMS(UNK_020FF4A4), 1, 0); + InitHeapSystem(UNK_020FF4A4, NELEMS(UNK_020FF4A4), 1, 0); FUN_0200E3A0(PM_LCD_TOP, 0); FUN_0200E3A0(PM_LCD_BOTTOM, 0); diff --git a/arm9/src/game_init.c b/arm9/src/game_init.c index 05b1f9bd..f907d646 100644 --- a/arm9/src/game_init.c +++ b/arm9/src/game_init.c @@ -142,7 +142,7 @@ void FUN_02015FC8(void) { csum++; } - FUN_020166C8(UNK_020EDB10, NELEMS(UNK_020EDB10), 92, csum); + InitHeapSystem(UNK_020EDB10, NELEMS(UNK_020EDB10), 92, csum); } void InitSystemForTheGame(void) diff --git a/arm9/src/heap.c b/arm9/src/heap.c index 79763865..ba3776ac 100644 --- a/arm9/src/heap.c +++ b/arm9/src/heap.c @@ -4,12 +4,32 @@ #include "NNS_FND_expheap.h" #include "NNS_FND_allocator.h" -struct UnkStruct_020166C8 UNK_021C4D28; +struct HeapInfo +{ + NNSFndHeapHandle *heapHandles; + NNSFndHeapHandle *parentHeapHandles; + void **subHeapRawPtrs; + u16 *numMemBlocks; + u8 *heapIdxs; + u16 totalNumHeaps; + u16 nTemplates; + u16 maxHeaps; + u16 unallocatedHeapId; +}; + +typedef struct MemoryBlock +{ + u8 filler_00[12]; + u32 heapId:8; + u32 filler_0D:24; +} MemoryBlock; -THUMB_FUNC void FUN_020166C8(const struct UnkStruct_020EDB10 *templates, u32 nTemplates, u32 totalNumHeaps, u32 pre_size) +struct HeapInfo sHeapInfo; + +THUMB_FUNC void InitHeapSystem(const struct UnkStruct_020EDB10 *templates, u32 nTemplates, u32 totalNumHeaps, u32 pre_size) { void * ptr; - u32 unk_size, r7; + u32 unk_size, i; unk_size = nTemplates + 24; @@ -27,7 +47,7 @@ THUMB_FUNC void FUN_020166C8(const struct UnkStruct_020EDB10 *templates, u32 nTe OS_AllocFromArenaLo(OS_ARENA_MAIN, pre_size, 4); } - UNK_021C4D28.heapHandles = (NNSFndHeapHandle*) OS_AllocFromArenaLo( + sHeapInfo.heapHandles = (NNSFndHeapHandle*) OS_AllocFromArenaLo( OS_ARENA_MAIN, (unk_size + 1) * sizeof(NNSFndHeapHandle) + unk_size * sizeof(NNSFndHeapHandle) @@ -36,34 +56,34 @@ THUMB_FUNC void FUN_020166C8(const struct UnkStruct_020EDB10 *templates, u32 nTe + totalNumHeaps, 4 ); - UNK_021C4D28.unk04 = UNK_021C4D28.heapHandles + (unk_size + 1); - UNK_021C4D28.unk08 = (void **)(UNK_021C4D28.unk04 + unk_size); - UNK_021C4D28.unk0c = (u16 *)(UNK_021C4D28.unk08 + unk_size); - UNK_021C4D28.heapIdxs = (u8 *)(UNK_021C4D28.unk0c + totalNumHeaps); - UNK_021C4D28.totalNumHeaps = (u16)totalNumHeaps; - UNK_021C4D28.nTemplates = (u16)nTemplates; + sHeapInfo.parentHeapHandles = sHeapInfo.heapHandles + (unk_size + 1); + sHeapInfo.subHeapRawPtrs = (void **)(sHeapInfo.parentHeapHandles + unk_size); + sHeapInfo.numMemBlocks = (u16 *)(sHeapInfo.subHeapRawPtrs + unk_size); + sHeapInfo.heapIdxs = (u8 *)(sHeapInfo.numMemBlocks + totalNumHeaps); + sHeapInfo.totalNumHeaps = (u16)totalNumHeaps; + sHeapInfo.nTemplates = (u16)nTemplates; - UNK_021C4D28.unk1a = (u16)unk_size; - UNK_021C4D28.unk18 = (u16)unk_size; + sHeapInfo.unallocatedHeapId = (u16)unk_size; + sHeapInfo.maxHeaps = (u16)unk_size; - for (r7 = 0; r7 < nTemplates; r7++) + for (i = 0; i < nTemplates; i++) { - switch (templates[r7].arena) + switch (templates[i].arena) { case OS_ARENA_MAIN: default: - ptr = OS_AllocFromArenaLo(OS_ARENA_MAIN, templates[r7].size, 4); + ptr = OS_AllocFromArenaLo(OS_ARENA_MAIN, templates[i].size, 4); break; case OS_ARENA_MAINEX: - ptr = OS_AllocFromArenaHi(OS_ARENA_MAINEX, templates[r7].size, 4); + ptr = OS_AllocFromArenaHi(OS_ARENA_MAINEX, templates[i].size, 4); break; } if (ptr != NULL) { - UNK_021C4D28.heapHandles[r7] = NNS_FndCreateExpHeap(ptr, templates[r7].size); - UNK_021C4D28.heapIdxs[r7] = (u8)r7; + sHeapInfo.heapHandles[i] = NNS_FndCreateExpHeap(ptr, templates[i].size); + sHeapInfo.heapIdxs[i] = (u8)i; } else { @@ -71,75 +91,73 @@ THUMB_FUNC void FUN_020166C8(const struct UnkStruct_020EDB10 *templates, u32 nTe } } - while (nTemplates < unk_size + 1) + for (i = nTemplates; i < unk_size + 1; i++) { - UNK_021C4D28.heapHandles[nTemplates] = NULL; - UNK_021C4D28.heapIdxs[nTemplates] = (u8)UNK_021C4D28.unk1a; - - nTemplates++; + sHeapInfo.heapHandles[i] = NULL; + sHeapInfo.heapIdxs[i] = (u8)sHeapInfo.unallocatedHeapId; } - while (nTemplates < totalNumHeaps) + while (i < totalNumHeaps) { - UNK_021C4D28.heapIdxs[nTemplates] = (u8)UNK_021C4D28.unk1a; + sHeapInfo.heapIdxs[i] = (u8)sHeapInfo.unallocatedHeapId; - nTemplates++; + i++; } - for (nTemplates = 0; nTemplates < totalNumHeaps; nTemplates++) + for (i = 0; i < totalNumHeaps; i++) { - UNK_021C4D28.unk0c[nTemplates] = 0; + sHeapInfo.numMemBlocks[i] = 0; } } -THUMB_FUNC s32 FUN_020167F4() +THUMB_FUNC s32 FindFirstAvailableHeapHandle() { s32 i; s32 j; - for (i = UNK_021C4D28.nTemplates; i < UNK_021C4D28.unk18; i++) + for (i = sHeapInfo.nTemplates; i < sHeapInfo.maxHeaps; i++) { - if (UNK_021C4D28.heapHandles[i] == NULL) + if (sHeapInfo.heapHandles[i] == NULL) return i; } return -1; } -THUMB_FUNC u32 FUN_0201681C(u32 param0, u32 param1, u32 param2) +THUMB_FUNC BOOL CreateHeap(u32 parent, u32 child, u32 size) { - return FUN_02016834(param0, param1, param2, 4); + return CreateHeapInternal(parent, child, size, 4); } -THUMB_FUNC u32 FUN_02016828(u32 param0, u32 param1, u32 param2) +THUMB_FUNC BOOL CreateHeapAtEnd(u32 parent, u32 child, u32 size) { - return FUN_02016834(param0, param1, param2, -4); + return CreateHeapInternal(parent, child, size, -4); } -THUMB_FUNC u32 FUN_02016834(u32 param0, u32 param1, u32 size, s32 alignment) +THUMB_FUNC BOOL CreateHeapInternal(u32 parent, u32 child, u32 size, s32 alignment) { GF_ASSERT(OS_GetProcMode() != OS_PROCMODE_IRQ); - u8 *ptr = UNK_021C4D28.heapIdxs; - if (UNK_021C4D28.unk1a == ptr[param1]) + u8 *ptr = sHeapInfo.heapIdxs; + if (sHeapInfo.unallocatedHeapId == ptr[child]) { - NNSFndHeapHandle ptr2 = UNK_021C4D28.heapHandles[ptr[param0]]; - if (ptr2 != NULL) + NNSFndHeapHandle parentHeap = sHeapInfo.heapHandles[ptr[parent]]; + if (parentHeap != NULL) { - void *ptr3 = NNS_FndAllocFromExpHeapEx(ptr2, size, alignment); - if (ptr3 != NULL) + void *newHeapAddr = NNS_FndAllocFromExpHeapEx(parentHeap, size, alignment); + if (newHeapAddr != NULL) { - s32 i = FUN_020167F4(); + s32 i = FindFirstAvailableHeapHandle(); if (i >= 0) { - UNK_021C4D28.heapHandles[i] = NNS_FndCreateExpHeap(ptr3, size); + sHeapInfo.heapHandles[i] = NNS_FndCreateExpHeap(newHeapAddr, size); - if (UNK_021C4D28.heapHandles[i] != 0) + if (sHeapInfo.heapHandles[i] != 0) { - UNK_021C4D28.unk04[i] = ptr2; - UNK_021C4D28.unk08[i] = ptr3; - UNK_021C4D28.heapIdxs[param1] = (u8)i; + sHeapInfo.parentHeapHandles[i] = parentHeap; + sHeapInfo.subHeapRawPtrs[i] = newHeapAddr; + sHeapInfo.heapIdxs[child] = (u8)i; return TRUE; } @@ -170,56 +188,56 @@ THUMB_FUNC u32 FUN_02016834(u32 param0, u32 param1, u32 size, s32 alignment) return 0; } -THUMB_FUNC void FUN_020168D0(u32 heap_id) +THUMB_FUNC void DestroyHeap(u32 heap_id) { GF_ASSERT (OS_GetProcMode() != OS_PROCMODE_IRQ); - NNSFndHeapHandle handle = UNK_021C4D28.heapHandles[UNK_021C4D28.heapIdxs[heap_id]]; + NNSFndHeapHandle handle = sHeapInfo.heapHandles[sHeapInfo.heapIdxs[heap_id]]; if (handle != NULL) { NNS_FndDestroyExpHeap(handle); - u8 index = UNK_021C4D28.heapIdxs[heap_id]; - NNSFndHeapHandle ptr1 = UNK_021C4D28.unk04[index]; - void *ptr2 = UNK_021C4D28.unk08[index]; - if (ptr1 != NULL && ptr2 != NULL) + u8 index = sHeapInfo.heapIdxs[heap_id]; + NNSFndHeapHandle parentHeap = sHeapInfo.parentHeapHandles[index]; + void *childRaw = sHeapInfo.subHeapRawPtrs[index]; + if (parentHeap != NULL && childRaw != NULL) { - NNS_FndFreeToExpHeap(ptr1, ptr2); + NNS_FndFreeToExpHeap(parentHeap, childRaw); } else { GF_ASSERT(0); } - UNK_021C4D28.heapHandles[UNK_021C4D28.heapIdxs[heap_id]] = NULL; - UNK_021C4D28.unk04[UNK_021C4D28.heapIdxs[heap_id]] = NULL; - UNK_021C4D28.unk08[UNK_021C4D28.heapIdxs[heap_id]] = NULL; + sHeapInfo.heapHandles[sHeapInfo.heapIdxs[heap_id]] = NULL; + sHeapInfo.parentHeapHandles[sHeapInfo.heapIdxs[heap_id]] = NULL; + sHeapInfo.subHeapRawPtrs[sHeapInfo.heapIdxs[heap_id]] = NULL; - UNK_021C4D28.heapIdxs[heap_id] = (u8)UNK_021C4D28.unk1a; + sHeapInfo.heapIdxs[heap_id] = (u8)sHeapInfo.unallocatedHeapId; } } -THUMB_FUNC u32 *FUN_02016944(void *param0, u32 param1, s32 param2, u32 param3) +THUMB_FUNC void *AllocFromHeapInternal(NNSFndHeapHandle heap, u32 size, s32 alignment, u32 heap_id) { - GF_ASSERT(param0); + GF_ASSERT(heap); OSIntrMode intr_mode = OS_DisableInterrupts(); - param1 += 16; - u32 *ptr = (u32 *)NNS_FndAllocFromExpHeapEx(param0, param1, param2); + size += 16; + void *ptr = NNS_FndAllocFromExpHeapEx(heap, size, alignment); OS_RestoreInterrupts(intr_mode); - if (ptr != 0) + if (ptr != NULL) { - ptr[3] = (ptr[3] & ~0xff) | (param3 & 0xff); + ((MemoryBlock *)ptr)->heapId = heap_id; - ptr += 4; + ptr += sizeof(MemoryBlock); } return ptr; } -THUMB_FUNC void FUN_02016988() +THUMB_FUNC void AllocFail() { if (FUN_02031810() != 0) { @@ -230,18 +248,18 @@ THUMB_FUNC void FUN_02016988() void *AllocFromHeap(u32 heap_id, u32 size) { void *ptr = 0; - if (heap_id < UNK_021C4D28.totalNumHeaps) + if (heap_id < sHeapInfo.totalNumHeaps) { - u8 index = UNK_021C4D28.heapIdxs[heap_id]; - ptr = FUN_02016944(UNK_021C4D28.heapHandles[index], size, 4, heap_id); + u8 index = sHeapInfo.heapIdxs[heap_id]; + ptr = AllocFromHeapInternal(sHeapInfo.heapHandles[index], size, 4, heap_id); } - if (ptr != 0) + if (ptr != NULL) { - UNK_021C4D28.unk0c[heap_id]++; + sHeapInfo.numMemBlocks[heap_id]++; } else { - FUN_02016988(); + AllocFail(); } return ptr; @@ -250,19 +268,19 @@ void *AllocFromHeap(u32 heap_id, u32 size) void *AllocFromHeapAtEnd(u32 heap_id, u32 size) { void *ptr = 0; - if (heap_id < UNK_021C4D28.totalNumHeaps) + if (heap_id < sHeapInfo.totalNumHeaps) { - u8 index = UNK_021C4D28.heapIdxs[heap_id]; - ptr = FUN_02016944(UNK_021C4D28.heapHandles[index], size, -4, heap_id); + u8 index = sHeapInfo.heapIdxs[heap_id]; + ptr = AllocFromHeapInternal(sHeapInfo.heapHandles[index], size, -4, heap_id); } - if (ptr != 0) + if (ptr != NULL) { - UNK_021C4D28.unk0c[heap_id]++; + sHeapInfo.numMemBlocks[heap_id]++; } else { - FUN_02016988(); + AllocFail(); } return ptr; @@ -270,23 +288,24 @@ void *AllocFromHeapAtEnd(u32 heap_id, u32 size) void FreeToHeap(void *ptr) { - u8 heap_id = (u8)((u32 *)ptr)[-1]; + ptr -= sizeof(MemoryBlock); + u32 heap_id = ((MemoryBlock *)ptr)->heapId; - if ((u16)heap_id < UNK_021C4D28.totalNumHeaps) + if (heap_id < sHeapInfo.totalNumHeaps) { - u8 index = UNK_021C4D28.heapIdxs[heap_id]; - void *ptr2 = UNK_021C4D28.heapHandles[index]; - GF_ASSERT(ptr2); + u8 index = sHeapInfo.heapIdxs[heap_id]; + NNSFndHeapHandle heap = sHeapInfo.heapHandles[index]; + GF_ASSERT(heap != NULL); - if (UNK_021C4D28.unk0c[heap_id] == 0) + if (sHeapInfo.numMemBlocks[heap_id] == 0) { - FUN_02016B90(heap_id); + GF_heap_c_dummy_return_true(heap_id); } - GF_ASSERT(UNK_021C4D28.unk0c[heap_id]); + GF_ASSERT(sHeapInfo.numMemBlocks[heap_id] != 0); - UNK_021C4D28.unk0c[heap_id]--; + sHeapInfo.numMemBlocks[heap_id]--; OSIntrMode intr_mode = OS_DisableInterrupts(); - NNS_FndFreeToExpHeap(ptr2, ptr - 16); + NNS_FndFreeToExpHeap(heap, ptr); OS_RestoreInterrupts(intr_mode); return; } @@ -294,23 +313,23 @@ void FreeToHeap(void *ptr) GF_ASSERT(0); } -void FreeToHeapExplicit(u32 param0, void *param1) +void FreeToHeapExplicit(u32 heap_id, void *ptr) { GF_ASSERT (OS_GetProcMode() != OS_PROCMODE_IRQ); - if (param0 < UNK_021C4D28.totalNumHeaps) + if (heap_id < sHeapInfo.totalNumHeaps) { - u8 index = UNK_021C4D28.heapIdxs[param0]; - void *ptr = UNK_021C4D28.heapHandles[index]; - GF_ASSERT (ptr ); + u8 index = sHeapInfo.heapIdxs[heap_id]; + NNSFndHeapHandle heap = sHeapInfo.heapHandles[index]; + GF_ASSERT ( heap != NULL ); - u8 heap_id = (u8)((u32 *)param1)[-1]; - GF_ASSERT (heap_id == param0); + ptr -= sizeof(MemoryBlock); + GF_ASSERT (((MemoryBlock *)ptr)->heapId == heap_id); - NNS_FndFreeToExpHeap(ptr, param1 - 16); - GF_ASSERT (UNK_021C4D28.unk0c[param0]); + NNS_FndFreeToExpHeap(heap, ptr); + GF_ASSERT (sHeapInfo.numMemBlocks[heap_id] != 0); - UNK_021C4D28.unk0c[param0]--; + sHeapInfo.numMemBlocks[heap_id]--; return; } @@ -319,10 +338,10 @@ void FreeToHeapExplicit(u32 param0, void *param1) THUMB_FUNC u32 GF_ExpHeap_FndGetTotalFreeSize(u32 heap_id) { - if (heap_id < UNK_021C4D28.totalNumHeaps) + if (heap_id < sHeapInfo.totalNumHeaps) { - u8 index = UNK_021C4D28.heapIdxs[heap_id]; - return NNS_FndGetTotalFreeSizeForExpHeap(UNK_021C4D28.heapHandles[index]); + u8 index = sHeapInfo.heapIdxs[heap_id]; + return NNS_FndGetTotalFreeSizeForExpHeap(sHeapInfo.heapHandles[index]); } GF_ASSERT(0); @@ -331,11 +350,11 @@ THUMB_FUNC u32 GF_ExpHeap_FndGetTotalFreeSize(u32 heap_id) THUMB_FUNC void GF_ExpHeap_FndInitAllocator(NNSFndAllocator * pAllocator, u32 heap_id, int alignment) { - if (heap_id < UNK_021C4D28.totalNumHeaps) + if (heap_id < sHeapInfo.totalNumHeaps) { - u8 index = UNK_021C4D28.heapIdxs[heap_id]; - NNS_FndInitAllocatorForExpHeap(pAllocator, UNK_021C4D28.heapHandles[index], alignment); + u8 index = sHeapInfo.heapIdxs[heap_id]; + NNS_FndInitAllocatorForExpHeap(pAllocator, sHeapInfo.heapHandles[index], alignment); return; } @@ -351,16 +370,16 @@ THUMB_FUNC void ReallocFromHeap(void *ptr, u32 newSize) { u8 heap_id = (u8)((u32 *)ptr)[-1]; - u8 index = UNK_021C4D28.heapIdxs[heap_id]; + u8 index = sHeapInfo.heapIdxs[heap_id]; - NNS_FndResizeForMBlockExpHeap(UNK_021C4D28.heapHandles[index], ptr - 16, newSize); + NNS_FndResizeForMBlockExpHeap(sHeapInfo.heapHandles[index], ptr - 16, newSize); return; } GF_ASSERT(0); } -THUMB_FUNC u32 FUN_02016B90(u32 param0) +THUMB_FUNC BOOL GF_heap_c_dummy_return_true(u32 heap_id) { -#pragma unused(param0) - return 1; +#pragma unused(heap_id) + return TRUE; } diff --git a/arm9/src/unk_02031734.c b/arm9/src/unk_02031734.c index 49283907..7d3b4c03 100644 --- a/arm9/src/unk_02031734.c +++ b/arm9/src/unk_02031734.c @@ -97,14 +97,14 @@ THUMB_FUNC void FUN_020317C0() if (UNK_021C5A00->unk3F >= 0x13) { - FUN_020168D0(0x31); + DestroyHeap(0x31); } FUN_02033ED0(); FUN_0202E4F0(); FreeToHeap(UNK_021C5A00); - FUN_020168D0(0xf); + DestroyHeap(0xf); UNK_021C5A00 = NULL; } @@ -123,7 +123,7 @@ THUMB_FUNC void FUN_02031824(struct SaveBlock2 *sav2) { if (UNK_021C5A00 == NULL) { - FUN_02016828(3, 0xf, 0xe000); + CreateHeapAtEnd(3, 0xf, 0xe000); FUN_02031734(sav2, 0xa); UNK_021C5A00->unk40 = 0; @@ -201,7 +201,7 @@ THUMB_FUNC void FUN_02031948(struct SaveBlock2 *sav2, u8 param1, u8 param2, u32 { if (FUN_02030F40() == 0) { - FUN_02016828(3, 0xf, 0x7080); + CreateHeapAtEnd(3, 0xf, 0x7080); FUN_02031734(sav2, param1); UNK_021C5A00->unk40 = param2; UNK_021C5A00->unk30 = param3; @@ -213,7 +213,7 @@ THUMB_FUNC void FUN_02031990(struct SaveBlock2 *sav2, u8 param1, u8 param2, u32 { if (FUN_02030F40() == 0) { - FUN_02016828(3, 0xf, 0x7080); + CreateHeapAtEnd(3, 0xf, 0x7080); FUN_02031734(sav2, param1); UNK_021C5A00->unk40 = param2; UNK_021C5A00->unk30 = param3; @@ -273,7 +273,7 @@ THUMB_FUNC void FUN_02031A7C(struct SaveBlock2 *sav2) { if (UNK_021C5A00 == NULL) { - if (FUN_02016828(3, 0xf, 0x7080) == 0) + if (CreateHeapAtEnd(3, 0xf, 0x7080) == 0) { FUN_020335E0(); } @@ -406,7 +406,7 @@ THUMB_FUNC void FUN_02031C74(struct SaveBlock2 *sav2, u8 param1) { if (FUN_02030F40() == 0) { - FUN_02016828(3, 0xf, 0x7080); + CreateHeapAtEnd(3, 0xf, 0x7080); FUN_02031734(sav2, param1); FUN_02031D20(FUN_02032E00, 0); } @@ -416,7 +416,7 @@ THUMB_FUNC void FUN_02031CA8(struct SaveBlock2 *sav2, u8 param1) { if (FUN_02030F40() == 0) { - FUN_02016828(3, 0xf, 0x7080); + CreateHeapAtEnd(3, 0xf, 0x7080); FUN_02031734(sav2, param1); FUN_02031D20(FUN_02032E48, 0); } @@ -1393,7 +1393,7 @@ THUMB_FUNC void FUN_02032BD0(struct SaveBlock2 *sav2) { if (FUN_02030F40() == 0) { - FUN_02016828(3, 0xf, 0x7000); + CreateHeapAtEnd(3, 0xf, 0x7000); FUN_02031734(sav2, 0xe); UNK_021C5A00->unk40 = 0; FUN_02031D20(FUN_02032B8C, 0); @@ -1479,7 +1479,7 @@ THUMB_FUNC void FUN_02032D44(struct SaveBlock2 *sav2) { if (FUN_02030F40() == 0) { - FUN_02016828(3, 0xf, 0x7000); + CreateHeapAtEnd(3, 0xf, 0x7000); FUN_02031734(sav2, 0x11); UNK_021C5A00->unk40 = 0; FUN_02031D20(FUN_02032CF4, 0); @@ -1866,7 +1866,7 @@ THUMB_FUNC void FUN_02033288(struct SaveBlock2 *sav2) { if (UNK_021C5A00 == NULL) { - FUN_02016828(3, 0xf, 0x100); + CreateHeapAtEnd(3, 0xf, 0x100); UNK_021C5A00 = (struct UnkStruct_02031734 *)AllocFromHeap(0xf, sizeof(struct UnkStruct_02031734)); MI_CpuFill8(UNK_021C5A00, 0, sizeof(struct UnkStruct_02031734)); @@ -1887,7 +1887,7 @@ THUMB_FUNC void FUN_020332DC() FUN_020334E8(0, 0); FreeToHeap(UNK_021C5A00); UNK_021C5A00 = NULL; - FUN_020168D0(0xf); + DestroyHeap(0xf); } } @@ -1895,7 +1895,7 @@ THUMB_FUNC void FUN_02033310(struct SaveBlock2 *sav2) { if (UNK_021C5A00 == NULL) { - FUN_02016828(3, 0xf, 0x100); + CreateHeapAtEnd(3, 0xf, 0x100); UNK_021C5A00 = (struct UnkStruct_02031734 *)AllocFromHeap(0xf, sizeof(struct UnkStruct_02031734)); MI_CpuFill8(UNK_021C5A00, 0, sizeof(struct UnkStruct_02031734)); @@ -1915,7 +1915,7 @@ THUMB_FUNC void FUN_02033364() FUN_020334E8(0, 0); FreeToHeap(UNK_021C5A00); UNK_021C5A00 = NULL; - FUN_020168D0(0xf); + DestroyHeap(0xf); } } @@ -1966,7 +1966,7 @@ THUMB_FUNC void FUN_0203341C() { if (FUN_0202E4C8()) { - FUN_02016828(3, 0x31, 0x31000); + CreateHeapAtEnd(3, 0x31, 0x31000); if (FUN_0202F918(1, 1, 0x200, 1) != 0) { MOD05_021D74E0(UNK_021C5A00->unk28, 0x31); @@ -1986,7 +1986,7 @@ THUMB_FUNC void *FUN_0203346C(struct SaveBlock2 *sav2, u32 param1) } SetSoftResetDisableMask(1); - FUN_02016828(3, 0xf, 0x7080); + CreateHeapAtEnd(3, 0xf, 0x7080); FUN_02031734(sav2, 0x17); UNK_021C5A00->unk00 = AllocFromHeap(0xf, param1); MI_CpuFill8(UNK_021C5A00->unk00, 0, param1); -- cgit v1.2.3 From 3b6540d8235fe2cdd62b8bd618ce424d3944a912 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Mon, 31 May 2021 08:27:32 -0400 Subject: Fix weird access in ReallocFromHeap --- arm9/src/error_message_reset.c | 2 +- arm9/src/game_init.c | 2 +- arm9/src/heap.c | 35 ++++++++++++++++++----------------- 3 files changed, 20 insertions(+), 19 deletions(-) (limited to 'arm9/src') diff --git a/arm9/src/error_message_reset.c b/arm9/src/error_message_reset.c index 520a5077..de039375 100644 --- a/arm9/src/error_message_reset.c +++ b/arm9/src/error_message_reset.c @@ -8,7 +8,7 @@ const struct UnkStruct_02016B94_4 UNK_020FF49C = { 0, 3, 3, 0x1a, 0x12, 1, 0x23 }; -const struct UnkStruct_020EDB10 UNK_020FF4A4[] = { +const struct HeapParam UNK_020FF4A4[] = { {0x00020000, OS_ARENA_MAIN} }; diff --git a/arm9/src/game_init.c b/arm9/src/game_init.c index f907d646..840b17a4 100644 --- a/arm9/src/game_init.c +++ b/arm9/src/game_init.c @@ -119,7 +119,7 @@ void Main_ToggleHBlankInterrupt(BOOL enableFlag) (void)OS_EnableIrq(); } -const struct UnkStruct_020EDB10 UNK_020EDB10[] = { +const struct HeapParam UNK_020EDB10[] = { { 0x00D000, OS_ARENA_MAIN }, { 0x021000, OS_ARENA_MAIN }, { 0x001000, OS_ARENA_MAIN }, diff --git a/arm9/src/heap.c b/arm9/src/heap.c index ba3776ac..842fd33a 100644 --- a/arm9/src/heap.c +++ b/arm9/src/heap.c @@ -26,7 +26,7 @@ typedef struct MemoryBlock struct HeapInfo sHeapInfo; -THUMB_FUNC void InitHeapSystem(const struct UnkStruct_020EDB10 *templates, u32 nTemplates, u32 totalNumHeaps, u32 pre_size) +THUMB_FUNC void InitHeapSystem(const struct HeapParam *templates, u32 nTemplates, u32 totalNumHeaps, u32 pre_size) { void * ptr; u32 unk_size, i; @@ -39,6 +39,7 @@ THUMB_FUNC void InitHeapSystem(const struct UnkStruct_020EDB10 *templates, u32 n } if (pre_size != 0) { + // force align while (pre_size % 4 != 0) { pre_size++; @@ -113,7 +114,6 @@ THUMB_FUNC void InitHeapSystem(const struct UnkStruct_020EDB10 *templates, u32 n THUMB_FUNC s32 FindFirstAvailableHeapHandle() { s32 i; - s32 j; for (i = sHeapInfo.nTemplates; i < sHeapInfo.maxHeaps; i++) { @@ -153,7 +153,7 @@ THUMB_FUNC BOOL CreateHeapInternal(u32 parent, u32 child, u32 size, s32 alignmen sHeapInfo.heapHandles[i] = NNS_FndCreateExpHeap(newHeapAddr, size); - if (sHeapInfo.heapHandles[i] != 0) + if (sHeapInfo.heapHandles[i] != NULL) { sHeapInfo.parentHeapHandles[i] = parentHeap; sHeapInfo.subHeapRawPtrs[i] = newHeapAddr; @@ -185,12 +185,12 @@ THUMB_FUNC BOOL CreateHeapInternal(u32 parent, u32 child, u32 size, s32 alignmen { GF_ASSERT(0); } - return 0; + return FALSE; } THUMB_FUNC void DestroyHeap(u32 heap_id) { - GF_ASSERT (OS_GetProcMode() != OS_PROCMODE_IRQ); + GF_ASSERT(OS_GetProcMode() != OS_PROCMODE_IRQ); NNSFndHeapHandle handle = sHeapInfo.heapHandles[sHeapInfo.heapIdxs[heap_id]]; @@ -223,7 +223,7 @@ THUMB_FUNC void *AllocFromHeapInternal(NNSFndHeapHandle heap, u32 size, s32 alig GF_ASSERT(heap); OSIntrMode intr_mode = OS_DisableInterrupts(); - size += 16; + size += sizeof(MemoryBlock); void *ptr = NNS_FndAllocFromExpHeapEx(heap, size, alignment); OS_RestoreInterrupts(intr_mode); @@ -247,7 +247,7 @@ THUMB_FUNC void AllocFail() void *AllocFromHeap(u32 heap_id, u32 size) { - void *ptr = 0; + void *ptr = NULL; if (heap_id < sHeapInfo.totalNumHeaps) { u8 index = sHeapInfo.heapIdxs[heap_id]; @@ -267,7 +267,7 @@ void *AllocFromHeap(u32 heap_id, u32 size) void *AllocFromHeapAtEnd(u32 heap_id, u32 size) { - void *ptr = 0; + void *ptr = NULL; if (heap_id < sHeapInfo.totalNumHeaps) { u8 index = sHeapInfo.heapIdxs[heap_id]; @@ -315,19 +315,19 @@ void FreeToHeap(void *ptr) void FreeToHeapExplicit(u32 heap_id, void *ptr) { - GF_ASSERT (OS_GetProcMode() != OS_PROCMODE_IRQ); + GF_ASSERT(OS_GetProcMode() != OS_PROCMODE_IRQ); if (heap_id < sHeapInfo.totalNumHeaps) { u8 index = sHeapInfo.heapIdxs[heap_id]; NNSFndHeapHandle heap = sHeapInfo.heapHandles[index]; - GF_ASSERT ( heap != NULL ); + GF_ASSERT( heap != NULL ); ptr -= sizeof(MemoryBlock); - GF_ASSERT (((MemoryBlock *)ptr)->heapId == heap_id); + GF_ASSERT(((MemoryBlock *)ptr)->heapId == heap_id); NNS_FndFreeToExpHeap(heap, ptr); - GF_ASSERT (sHeapInfo.numMemBlocks[heap_id] != 0); + GF_ASSERT(sHeapInfo.numMemBlocks[heap_id] != 0); sHeapInfo.numMemBlocks[heap_id]--; return; @@ -363,16 +363,17 @@ THUMB_FUNC void GF_ExpHeap_FndInitAllocator(NNSFndAllocator * pAllocator, u32 he THUMB_FUNC void ReallocFromHeap(void *ptr, u32 newSize) { - GF_ASSERT (OS_GetProcMode() != OS_PROCMODE_IRQ); + GF_ASSERT(OS_GetProcMode() != OS_PROCMODE_IRQ); - newSize += 16; - if (NNS_FndGetSizeForMBlockExpHeap(ptr - 16) >= newSize) + newSize += sizeof(MemoryBlock); + ptr -= sizeof(MemoryBlock); + if (NNS_FndGetSizeForMBlockExpHeap(ptr) >= newSize) { - u8 heap_id = (u8)((u32 *)ptr)[-1]; + u32 heap_id = ((MemoryBlock *)ptr)->heapId; u8 index = sHeapInfo.heapIdxs[heap_id]; - NNS_FndResizeForMBlockExpHeap(sHeapInfo.heapHandles[index], ptr - 16, newSize); + NNS_FndResizeForMBlockExpHeap(sHeapInfo.heapHandles[index], ptr, newSize); return; } GF_ASSERT(0); -- cgit v1.2.3 From d612c41066844fc82edaaea6bc946adbcc71a3d9 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Mon, 31 May 2021 12:14:52 -0400 Subject: Better docs of gx, error_message_reset, etc --- arm9/src/error_message_reset.c | 37 ++++++++++++++++++------------------- arm9/src/game_init.c | 6 +++--- arm9/src/unk_02016B94.c | 14 +++++++------- 3 files changed, 28 insertions(+), 29 deletions(-) (limited to 'arm9/src') diff --git a/arm9/src/error_message_reset.c b/arm9/src/error_message_reset.c index de039375..2a23d08b 100644 --- a/arm9/src/error_message_reset.c +++ b/arm9/src/error_message_reset.c @@ -1,9 +1,10 @@ #include "error_message_reset.h" #include "GX_layers.h" +#include "GXS_ioreg.h" #include "unk_02031734.h" #include "unk_0202F150.h" #include "unk_02016B94.h" - +#include "PAD_pad.h" const struct UnkStruct_02016B94_4 UNK_020FF49C = { 0, 3, 3, 0x1a, 0x12, 1, 0x23 }; @@ -12,7 +13,7 @@ const struct HeapParam UNK_020FF4A4[] = { {0x00020000, OS_ARENA_MAIN} }; -const struct GraphicsModes UNK_020FF4AC = { mode1 : 1 }; +const struct GraphicsModes UNK_020FF4AC = { dispMode : GX_DISPMODE_GRAPHICS }; const struct UnkStruct_02016B94_1 UNK_020FF4BC = { 0, 0, 0x800, 0, 1, 0, 0, 6, 0, 1, 0, 0, 0 }; @@ -60,8 +61,8 @@ THUMB_FUNC void PrintErrorMessageAndReset() GX_DisableEngineALayers(); GX_DisableEngineBLayers(); - reg_GX_DISPCNT &= 0xFFFFE0FF; - reg_GXS_DB_DISPCNT &= 0xFFFFE0FF; + reg_GX_DISPCNT &= ~REG_GX_DISPCNT_DISPLAY_MASK; + reg_GXS_DB_DISPCNT &= ~REG_GXS_DB_DISPCNT_DISPLAY_MASK; SetKeyRepeatTimers(4, 8); @@ -70,8 +71,8 @@ THUMB_FUNC void PrintErrorMessageAndReset() reg_G2_BLDCNT = 0; reg_G2S_DB_BLDCNT = 0; - reg_GX_DISPCNT &= 0xFFFF1FFF; - reg_GXS_DB_DISPCNT &= 0xFFFF1FFF; + reg_GX_DISPCNT &= ~(REG_GX_DISPCNT_OW_MASK | REG_GX_DISPCNT_W1_MASK | REG_GX_DISPCNT_W0_MASK); + reg_GXS_DB_DISPCNT &= ~(REG_GXS_DB_DISPCNT_OW_MASK | REG_GXS_DB_DISPCNT_W1_MASK | REG_GXS_DB_DISPCNT_W0_MASK); GX_SetBanks(&UNK_020FF4D8); ptr = FUN_02016B94(0); @@ -106,24 +107,22 @@ THUMB_FUNC void PrintErrorMessageAndReset() FUN_0200A274(0, 0x3f, 3); FUN_02032DAC(); - lid: - HandleDSLidAction(); - FUN_0202FB80(); - if (!FUN_02033678()) + while (1) { - OS_WaitIrq(1, 1); - goto lid; + HandleDSLidAction(); + FUN_0202FB80(); + if (FUN_02033678()) + break; + OS_WaitIrq(TRUE, OS_IE_V_BLANK); } - - lid2: - HandleDSLidAction(); - if (!((u16)(((reg_PAD_KEYINPUT | *(vu16 *)HW_BUTTON_XY_BUF) ^ 0x2FFF) & 0x2FFF) & 1)) + while (1) { - OS_WaitIrq(1, 1); - goto lid2; + HandleDSLidAction(); + if ((PAD_Read() & PAD_BUTTON_A)) + break; + OS_WaitIrq(TRUE, OS_IE_V_BLANK); } - FUN_0200E3A0(PM_LCD_TOP, 0x7FFF); FUN_0200E3A0(PM_LCD_BOTTOM, 0x7FFF); diff --git a/arm9/src/game_init.c b/arm9/src/game_init.c index 840b17a4..1c6098ea 100644 --- a/arm9/src/game_init.c +++ b/arm9/src/game_init.c @@ -536,10 +536,10 @@ void ApplyButtonModeToInput(void) } } -void SetKeyRepeatTimers(int x, int y) +void SetKeyRepeatTimers(int continueDelay, int startDelay) { - gMain.keyRepeatContinueDelay = x; - gMain.keyRepeatStartDelay = y; + gMain.keyRepeatContinueDelay = continueDelay; + gMain.keyRepeatStartDelay = startDelay; } void SetSoftResetDisableMask(u8 a0) diff --git a/arm9/src/unk_02016B94.c b/arm9/src/unk_02016B94.c index 8ad78373..c03fbd75 100644 --- a/arm9/src/unk_02016B94.c +++ b/arm9/src/unk_02016B94.c @@ -73,25 +73,25 @@ THUMB_FUNC u32 FUN_02016BB8(u32 *param0) THUMB_FUNC void FUN_02016BBC(const struct GraphicsModes *modes) { - GX_SetGraphicsMode(modes->mode1, modes->mode2, modes->mode4); - GXS_SetGraphicsMode(modes->mode3); - reg_GX_DISPCNT &= 0xC7FFFFFF; - reg_GX_DISPCNT &= 0xF8FFFFFF; + GX_SetGraphicsMode(modes->dispMode, modes->bgMode, modes->_2d3dMode); + GXS_SetGraphicsMode(modes->subMode); + reg_GX_DISPCNT &= ~REG_GX_DISPCNT_BGSCREENOFFSET_MASK; + reg_GX_DISPCNT &= ~REG_GX_DISPCNT_BGCHAROFFSET_MASK; GX_DisableEngineALayers(); GX_DisableEngineBLayers(); } -THUMB_FUNC void FUN_02016BF4(u32 *param0, u32 param1) +THUMB_FUNC void FUN_02016BF4(const struct GraphicsModes *param0, u32 param1) { if (param1 == 0) { - GX_SetGraphicsMode(param0[0], param0[1], param0[3]); + GX_SetGraphicsMode(param0->dispMode, param0->bgMode, param0->_2d3dMode); GX_DisableEngineALayers(); } else { - GXS_SetGraphicsMode(param0[2]); + GXS_SetGraphicsMode(param0->subMode); GX_DisableEngineBLayers(); } } -- cgit v1.2.3