From 6a5b20ee8ab122e0fd4cad454fe24fea4fbc7cf5 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Mon, 19 Nov 2018 17:15:28 +0100 Subject: [PATCH] Fix fossil specials --- src/fossil_specials.c | 147 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 129 insertions(+), 18 deletions(-) diff --git a/src/fossil_specials.c b/src/fossil_specials.c index f562d83c2..251f69b1f 100644 --- a/src/fossil_specials.c +++ b/src/fossil_specials.c @@ -32,7 +32,7 @@ struct Struct8617DA4 struct Struct203CF10 { u8 *buffer; - u8 curr_buffer_index; + u8 currIndex; }; struct DynamicSpriteFrameImage @@ -147,7 +147,7 @@ EWRAM_DATA static struct Struct203CF10 *sUnknown_0203CF10 = NULL; EWRAM_DATA static u16 *sUnknown_0203CF14 = NULL; // IWRAM bss -IWRAM_DATA u16 gUnknown_030012A8[8]; +IWRAM_DATA static u16 gUnknown_030012A8[8]; // text void sub_81BEB24(void) @@ -261,7 +261,7 @@ static void sub_81BED50(u8 taskId) if (gTasks[taskId].data[1] > 1) { index = gTasks[taskId].data[3]; - sUnknown_0203CF10[index].buffer = (u8 *)Alloc(INNER_BUFFER_LENGTH); + sUnknown_0203CF10[index].buffer = Alloc(INNER_BUFFER_LENGTH); for (i = 0; i <= (INNER_BUFFER_LENGTH - 1); i++) sUnknown_0203CF10[index].buffer[i] = i; for (i = 0; i <= (INNER_BUFFER_LENGTH - 1); i++) @@ -284,10 +284,10 @@ static void sub_81BED50(u8 taskId) for (j = 0; j < 1; j++) { sub_81BF2B8(sUnknown_0203CF04, - ((((OUTER_BUFFER_LENGTH - 1) - i) * INNER_BUFFER_LENGTH) + sUnknown_0203CF10[i].buffer[(sUnknown_0203CF10[i].curr_buffer_index)++]), + ((((OUTER_BUFFER_LENGTH - 1) - i) * INNER_BUFFER_LENGTH) + sUnknown_0203CF10[i].buffer[(sUnknown_0203CF10[i].currIndex)++]), 0, INNER_BUFFER_LENGTH, 1); } - if (sUnknown_0203CF10[i].curr_buffer_index > (INNER_BUFFER_LENGTH - 1)) + if (sUnknown_0203CF10[i].currIndex > (INNER_BUFFER_LENGTH - 1)) { FREE_AND_SET_NULL(sUnknown_0203CF10[i].buffer); gTasks[taskId].data[2]++; @@ -296,7 +296,7 @@ static void sub_81BED50(u8 taskId) } } LoadBgTiles(0, sUnknown_0203CF04, MIRAGE_TOWER_GFX_LENGTH, 0); - if (sUnknown_0203CF10[OUTER_BUFFER_LENGTH - 1].curr_buffer_index > (INNER_BUFFER_LENGTH - 1)) + if (sUnknown_0203CF10[OUTER_BUFFER_LENGTH - 1].currIndex > (INNER_BUFFER_LENGTH - 1)) break; return; case 4: @@ -338,9 +338,9 @@ static void sub_81BF028(u8 taskId) switch (gTasks[taskId].data[0]) { case 1: - sUnknown_0203CF0C = AllocZeroed(sizeof(struct Struct203CF0C)); + sUnknown_0203CF0C = AllocZeroed(sizeof(*sUnknown_0203CF0C)); sUnknown_0203CF0C->frameImageTiles = AllocZeroed(ROOT_FOSSIL_GFX_LENGTH); - sUnknown_0203CF0C->frameImage = AllocZeroed(sizeof(struct DynamicSpriteFrameImage)); + sUnknown_0203CF0C->frameImage = AllocZeroed(sizeof(*sUnknown_0203CF0C->frameImage)); sUnknown_0203CF0C->unkC = AllocZeroed(ROOT_FOSSIL_GFX_RANDOMIZER_LENGTH * sizeof(u16)); sUnknown_0203CF0C->unk10 = 0; break; @@ -417,13 +417,15 @@ static void sub_81BF248(struct Sprite *sprite) } } +#ifdef NONMATCHING static void sub_81BF2B8(u8* a, u16 b, u8 c, u8 d, u8 e) { - u8 r5, r4, r0, r2, r1; + u8 r5, r4, r0, r2; + u16 var; r5 = b / d; r4 = r5; - gUnknown_030012A8[0] = r5; + gUnknown_030012A8[0] = r4; r2 = b % d; r0 = r2; @@ -439,14 +441,123 @@ static void sub_81BF2B8(u8* a, u16 b, u8 c, u8 d, u8 e) gUnknown_030012A8[4] = r0; gUnknown_030012A8[5] = r5; - d /= 8; - d *= 64; - gUnknown_030012A8[6] = (d * r5) + (r0 * 64); + var = ((d / 8) * (r5 * 64)) + (r0 * 64); + gUnknown_030012A8[6] = var; - gUnknown_030012A8[7] = gUnknown_030012A8[6] + 9 * r4; + var += (r4 * 8) + r2; + gUnknown_030012A8[7] = var; - r1 = (b & 1); - r1 ^= 1; - c = (c << (r1 << 2)) | (15 << ((r1 ^ 1) << 2)); - a[(e << 5) + r4] &= c; + // This part is non-matching. 99% sure it IS functionally equivalent, though. + b = (b & 1) ^ 1; + c = (c << ((b) << 2)) | (15 << ((b ^ 1) << 2)); + + a[(var / 2) + (e * 32)] &= c; } + +#else +NAKED +static void sub_81BF2B8(u8* a, u16 b, u8 c, u8 d, u8 e) +{ + asm_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, 0x8\n\ + str r0, [sp]\n\ + mov r10, r1\n\ + adds r6, r2, 0\n\ + mov r8, r3\n\ + ldr r0, [sp, 0x28]\n\ + mov r9, r0\n\ + lsls r1, 16\n\ + lsrs r1, 16\n\ + mov r10, r1\n\ + lsls r6, 24\n\ + lsrs r6, 24\n\ + mov r0, r8\n\ + lsls r0, 24\n\ + mov r8, r0\n\ + lsrs r7, r0, 24\n\ + mov r1, r9\n\ + lsls r1, 24\n\ + lsrs r1, 24\n\ + mov r9, r1\n\ + mov r0, r10\n\ + adds r1, r7, 0\n\ + bl __divsi3\n\ + adds r5, r0, 0\n\ + lsls r5, 24\n\ + lsrs r4, r5, 24\n\ + ldr r3, =gUnknown_030012A8\n\ + strh r4, [r3]\n\ + mov r0, r10\n\ + adds r1, r7, 0\n\ + str r3, [sp, 0x4]\n\ + bl __modsi3\n\ + lsls r0, 24\n\ + lsrs r2, r0, 24\n\ + ldr r3, [sp, 0x4]\n\ + strh r2, [r3, 0x2]\n\ + movs r1, 0x7\n\ + ands r4, r1\n\ + ands r2, r1\n\ + strh r4, [r3, 0x4]\n\ + strh r2, [r3, 0x6]\n\ + lsrs r0, 27\n\ + lsrs r5, 27\n\ + strh r0, [r3, 0x8]\n\ + strh r5, [r3, 0xA]\n\ + mov r1, r8\n\ + lsrs r1, 27\n\ + lsls r1, 6\n\ + mov r8, r1\n\ + mov r1, r8\n\ + muls r1, r5\n\ + lsls r0, 6\n\ + adds r1, r0\n\ + lsls r1, 16\n\ + lsrs r1, 16\n\ + strh r1, [r3, 0xC]\n\ + lsls r4, 3\n\ + adds r4, r2\n\ + adds r1, r4\n\ + lsls r4, r1, 16\n\ + lsrs r4, 17\n\ + strh r1, [r3, 0xE]\n\ + movs r1, 0x1\n\ + mov r0, r10\n\ + ands r1, r0\n\ + movs r2, 0x1\n\ + eors r1, r2\n\ + lsls r0, r1, 2\n\ + lsls r6, r0\n\ + eors r1, r2\n\ + lsls r1, 2\n\ + movs r0, 0xF\n\ + lsls r0, r1\n\ + orrs r6, r0\n\ + lsls r6, 24\n\ + lsrs r6, 24\n\ + mov r1, r9\n\ + lsls r1, 5\n\ + mov r9, r1\n\ + add r9, r4\n\ + ldr r1, [sp]\n\ + add r1, r9\n\ + ldrb r0, [r1]\n\ + ands r6, r0\n\ + strb r6, [r1]\n\ + add sp, 0x8\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\ + "); +} +#endif // NONMATCHING