From ff16812f99f62a495975e7cc3c68ede339171765 Mon Sep 17 00:00:00 2001 From: gAlfonso-bit <83477269+gAlfonso-bit@users.noreply.github.com> Date: Sun, 23 May 2021 10:56:37 -0400 Subject: [PATCH 1/6] UBFix: uninitialized variables in m4a engine and siirtc.c (#1432) --- src/m4a.c | 9 +++++++++ src/siirtc.c | 30 ++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/m4a.c b/src/m4a.c index 717cafc78e..105312a40c 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -1525,6 +1525,10 @@ void ply_xwave(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *track { u32 wav; +#ifdef UBFIX + wav = 0; +#endif + READ_XCMD_BYTE(wav, 0) // UB: uninitialized variable READ_XCMD_BYTE(wav, 1) READ_XCMD_BYTE(wav, 2) @@ -1592,6 +1596,10 @@ void ply_xcmd_0C(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *tra { u32 unk; +#ifdef UBFIX + unk = 0; +#endif + READ_XCMD_BYTE(unk, 0) // UB: uninitialized variable READ_XCMD_BYTE(unk, 1) @@ -1611,6 +1619,7 @@ void ply_xcmd_0C(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *tra void ply_xcmd_0D(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *track) { u32 unk; + #ifdef UBFIX unk = 0; #endif diff --git a/src/siirtc.c b/src/siirtc.c index 01d2e0e723..5f4fc0a23c 100644 --- a/src/siirtc.c +++ b/src/siirtc.c @@ -71,6 +71,7 @@ static bool8 sLocked; static int WriteCommand(u8 value); static int WriteData(u8 value); static u8 ReadData(); + static void EnableGpioPortRead(); static void DisableGpioPortRead(); @@ -98,8 +99,12 @@ u8 SiiRtcProbe(void) errorCode = 0; +#ifdef BUGFIX + if (!(rtc.status & SIIRTCINFO_24HOUR) || (rtc.status & SIIRTCINFO_POWER)) +#else if ((rtc.status & (SIIRTCINFO_POWER | SIIRTCINFO_24HOUR)) == SIIRTCINFO_POWER || (rtc.status & (SIIRTCINFO_POWER | SIIRTCINFO_24HOUR)) == 0) +#endif { // The RTC is in 12-hour mode. Reset it and switch to 24-hour mode. @@ -131,7 +136,7 @@ u8 SiiRtcProbe(void) bool8 SiiRtcReset(void) { - u8 result; + bool8 result; struct SiiRtcInfo rtc; if (sLocked == TRUE) @@ -392,7 +397,11 @@ static int WriteCommand(u8 value) GPIO_PORT_DATA = (temp << 1) | SCK_HI | CS_HI; } - // control reaches end of non-void function + // Nothing uses the returned value from this function, + // so the undefined behavior is harmless in the vanilla game. +#ifdef UBFIX + return 0; +#endif } static int WriteData(u8 value) @@ -409,7 +418,11 @@ static int WriteData(u8 value) GPIO_PORT_DATA = (temp << 1) | SCK_HI | CS_HI; } - // control reaches end of non-void function + // Nothing uses the returned value from this function, + // so the undefined behavior is harmless in the vanilla game. +#ifdef UBFIX + return 0; +#endif } static u8 ReadData() @@ -417,9 +430,10 @@ static u8 ReadData() u8 i; u8 temp; u8 value; - #ifdef UBFIX + +#ifdef UBFIX value = 0; - #endif +#endif for (i = 0; i < 8; i++) { @@ -431,7 +445,7 @@ static u8 ReadData() GPIO_PORT_DATA = SCK_HI | CS_HI; temp = ((GPIO_PORT_DATA & SIO_HI) >> 1); - value = (value >> 1) | (temp << 7); // UB: value is uninitialized on first iteration + value = (value >> 1) | (temp << 7); } return value; @@ -439,10 +453,10 @@ static u8 ReadData() static void EnableGpioPortRead() { - GPIO_PORT_READ_ENABLE = 1; + GPIO_PORT_READ_ENABLE = TRUE; } static void DisableGpioPortRead() { - GPIO_PORT_READ_ENABLE = 0; + GPIO_PORT_READ_ENABLE = FALSE; } From aca96a1510879906237a4b6b2176fe5e342e1386 Mon Sep 17 00:00:00 2001 From: gAlfonso-bit <83477269+gAlfonso-bit@users.noreply.github.com> Date: Sun, 23 May 2021 19:48:34 -0400 Subject: [PATCH 2/6] Mark 0xFFF8 as ~7 Since 7 is used as a mask, I wondered if 0xFFF8 was used to undo the mask and it turns out it was. --- gflib/bg.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/gflib/bg.c b/gflib/bg.c index 3c215c1034..b6d1b0b924 100644 --- a/gflib/bg.c +++ b/gflib/bg.c @@ -55,7 +55,7 @@ void ResetBgs(void) static void SetBgModeInternal(u8 bgMode) { - sGpuBgConfigs.bgVisibilityAndMode &= 0xFFF8; + sGpuBgConfigs.bgVisibilityAndMode &= ~0x7; sGpuBgConfigs.bgVisibilityAndMode |= bgMode; } @@ -66,13 +66,11 @@ u8 GetBgMode(void) void ResetBgControlStructs(void) { - struct BgConfig* bgConfigs = &sGpuBgConfigs.configs[0]; - struct BgConfig zeroedConfig = sZeroedBgControlStruct; int i; for (i = 0; i < NUM_BACKGROUNDS; i++) { - bgConfigs[i] = zeroedConfig; + sGpuBgConfigs.configs[i] = sZeroedBgControlStruct; } } From 94939e395b58cf3e774a76d49b8518b8c2937432 Mon Sep 17 00:00:00 2001 From: Kurausukun Date: Sun, 23 May 2021 22:26:34 -0400 Subject: [PATCH 3/6] one last goto --- gflib/bg.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/gflib/bg.c b/gflib/bg.c index b6d1b0b924..0c702ae0fb 100644 --- a/gflib/bg.c +++ b/gflib/bg.c @@ -173,36 +173,30 @@ u8 LoadBgVram(u8 bg, const void *src, u16 size, u16 destOffset, u8 mode) u16 offset; s8 cursor; - if (!IsInvalidBg(bg) && sGpuBgConfigs.configs[bg].visible) - { - switch (mode) - { - case 0x1: - offset = sGpuBgConfigs.configs[bg].charBaseIndex * BG_CHAR_SIZE; - break; - case 0x2: - offset = sGpuBgConfigs.configs[bg].mapBaseIndex * BG_SCREEN_SIZE; - break; - default: - cursor = -1; - goto end; - } + if (IsInvalidBg(bg) || !sGpuBgConfigs.configs[bg].visible) + return -1; + switch (mode) + { + case 0x1: + offset = sGpuBgConfigs.configs[bg].charBaseIndex * BG_CHAR_SIZE; offset = destOffset + offset; - cursor = RequestDma3Copy(src, (void*)(offset + BG_VRAM), size, 0); - if (cursor == -1) - { return -1; - } - } - else - { - return -1; + break; + case 0x2: + offset = sGpuBgConfigs.configs[bg].mapBaseIndex * BG_SCREEN_SIZE; + offset = destOffset + offset; + cursor = RequestDma3Copy(src, (void*)(offset + BG_VRAM), size, 0); + if (cursor == -1) + return -1; + break; + default: + cursor = -1; + break; } -end: return cursor; } From 5ae5cf110d722c751a3fbfec38ec8f4498a85616 Mon Sep 17 00:00:00 2001 From: Kurausukun Date: Tue, 25 May 2021 04:03:11 -0400 Subject: [PATCH 4/6] [LEAK-INFORMED] fix fakematch in DrawWallpaper --- src/pokemon_storage_system.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/pokemon_storage_system.c b/src/pokemon_storage_system.c index e5720d9148..a7a418a30a 100644 --- a/src/pokemon_storage_system.c +++ b/src/pokemon_storage_system.c @@ -5437,19 +5437,20 @@ static bool32 WaitForWallpaperGfxLoad(void) static void DrawWallpaper(const void *tilemap, s8 direction, u8 offset) { - s16 var = (offset * 2) + 3; + s16 var = offset * 256; + s16 var2 = (offset * 2) + 3; s16 x = ((sStorage->bg2_X / 8 + 10) + (direction * 24)) & 0x3F; - CopyRectToBgTilemapBufferRect(2, tilemap, 0, 0, 0x14, 0x12, x, 2, 0x14, 0x12, 0x11, offset << 8, var); - - if (direction == 0) - return; - if (direction > 0) - x *= 1, x += 0x14; // x * 1 is needed to match, but can be safely removed as it makes no functional difference - else - x -= 4; + CopyRectToBgTilemapBufferRect(2, tilemap, 0, 0, 0x14, 0x12, x, 2, 0x14, 0x12, 0x11, var, var2); + if (direction) + { + if (direction > 0) + x += 0x14; + else + x -= 4; FillBgTilemapBufferRect(2, 0, x, 2, 4, 0x12, 0x11); + } } static void TrimOldWallpaper(void *tilemap) From 5d5327c0b73784c47d18734a291b5463289ca9c5 Mon Sep 17 00:00:00 2001 From: Kurausukun Date: Tue, 25 May 2021 04:32:47 -0400 Subject: [PATCH 5/6] better match --- src/pokemon_storage_system.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/pokemon_storage_system.c b/src/pokemon_storage_system.c index a7a418a30a..c82caf0b0d 100644 --- a/src/pokemon_storage_system.c +++ b/src/pokemon_storage_system.c @@ -5443,14 +5443,14 @@ static void DrawWallpaper(const void *tilemap, s8 direction, u8 offset) CopyRectToBgTilemapBufferRect(2, tilemap, 0, 0, 0x14, 0x12, x, 2, 0x14, 0x12, 0x11, var, var2); - if (direction) - { - if (direction > 0) - x += 0x14; - else - x -= 4; + if (direction == 0) + return; + if (direction > 0) + x += 0x14; + else + x -= 4; + FillBgTilemapBufferRect(2, 0, x, 2, 4, 0x12, 0x11); - } } static void TrimOldWallpaper(void *tilemap) From c9c558606899f898c7b14ad4a35f19ce2831d66a Mon Sep 17 00:00:00 2001 From: gAlfonso-bit <83477269+gAlfonso-bit@users.noreply.github.com> Date: Thu, 27 May 2021 08:32:09 -0400 Subject: [PATCH 6/6] =?UTF-8?q?Fixed=20ChangeBgY=5FScreenOff=E2=80=98s=20s?= =?UTF-8?q?ignature.=20(#1447)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix Functions * Fix ChangeBgY_ScreenOff signature * Remove unneeded changes * Fix argument mismatch Just to get this out of the way * Not needed * Update palette.c --- gflib/bg.c | 10 +++++----- gflib/bg.h | 2 +- src/battle_factory.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/gflib/bg.c b/gflib/bg.c index 0c702ae0fb..ec7c2113b1 100644 --- a/gflib/bg.c +++ b/gflib/bg.c @@ -246,17 +246,17 @@ static void SetBgAffineInternal(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispC switch (sGpuBgConfigs.bgVisibilityAndMode & 0x7) { + default: + case 0: + return; case 1: if (bg != 2) return; break; case 2: - if (bg < 2 || bg >= NUM_BACKGROUNDS) + if (bg != 2 && bg != 3) return; break; - case 0: - default: - return; } src.texX = srcCenterX; @@ -689,7 +689,7 @@ s32 ChangeBgY(u8 bg, s32 value, u8 op) return sGpuBgConfigs2[bg].bg_y; } -s32 ChangeBgY_ScreenOff(u8 bg, u32 value, u8 op) +s32 ChangeBgY_ScreenOff(u8 bg, s32 value, u8 op) { u8 mode; u16 temp1; diff --git a/gflib/bg.h b/gflib/bg.h index 3c7eee2927..58fd1282c0 100644 --- a/gflib/bg.h +++ b/gflib/bg.h @@ -59,7 +59,7 @@ u16 GetBgAttribute(u8 bg, u8 attributeId); s32 ChangeBgX(u8 bg, s32 value, u8 op); s32 GetBgX(u8 bg); s32 ChangeBgY(u8 bg, s32 value, u8 op); -s32 ChangeBgY_ScreenOff(u8 bg, u32 value, u8 op); +s32 ChangeBgY_ScreenOff(u8 bg, s32 value, u8 op); s32 GetBgY(u8 bg); void SetBgAffine(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispCenterX, s16 dispCenterY, s16 scaleX, s16 scaleY, u16 rotationAngle); u8 Unused_AdjustBgMosaic(u8 a1, u8 a2); diff --git a/src/battle_factory.c b/src/battle_factory.c index 72772929a9..e0bfdfdd0b 100644 --- a/src/battle_factory.c +++ b/src/battle_factory.c @@ -38,7 +38,7 @@ static void GenerateInitialRentalMons(void); static void GetOpponentMostCommonMonType(void); static void GetOpponentBattleStyle(void); static void RestorePlayerPartyHeldItems(void); -static u16 GetFactoryMonId(u8 lvlMode, u8 challengeNum, bool8 arg2); +static u16 GetFactoryMonId(u8 lvlMode, u8 challengeNum, bool8 useBetterRange); static u8 GetMoveBattleStyle(u16 move); // Number of moves needed on the team to be considered using a certain battle style