From 507f1d8d36503af74b3d04a01d3b90900f08bb90 Mon Sep 17 00:00:00 2001 From: Jaizu Date: Wed, 11 Jun 2025 20:27:42 +0200 Subject: [PATCH] Modern fixes (#697) --- Makefile | 2 +- include/gba/m4a_internal.h | 4 ++++ include/global.h | 11 +++++++++++ include/librfu.h | 4 ++++ include/pokemon.h | 23 +++++++++++------------ src/daycare.c | 2 +- src/field_player_avatar.c | 2 +- src/librfu_intr.c | 12 ++++++++++++ src/m4a.c | 22 +++++++++++++++++++++- src/pokemon.c | 16 ++++++++++++++-- src/vs_seeker.c | 4 ++++ tools/mapjson/json11.cpp | 1 + 12 files changed, 85 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index d34ee28f7..26b1e4ca0 100644 --- a/Makefile +++ b/Makefile @@ -78,7 +78,7 @@ INCLUDE_SCANINC_ARGS := $(INCLUDE_DIRS:%=-I %) O_LEVEL ?= 2 CPPFLAGS := $(INCLUDE_CPP_ARGS) -Wno-trigraphs -D$(GAME_VERSION) -DREVISION=$(GAME_REVISION) -D$(GAME_LANGUAGE) -DMODERN=$(MODERN) ifeq ($(MODERN),0) - CPPFLAGS += -I tools/agbcc/include -I tools/agbcc -nostdinc -undef + CPPFLAGS += -I tools/agbcc/include -I tools/agbcc -nostdinc -undef -std=gnu89 CC1 := tools/agbcc/bin/agbcc$(EXE) override CFLAGS += -mthumb-interwork -Wimplicit -Wparentheses -Werror -O$(O_LEVEL) -fhex-asm LIBPATH := -L ../../tools/agbcc/lib diff --git a/include/gba/m4a_internal.h b/include/gba/m4a_internal.h index eeb79391b..fa509e511 100644 --- a/include/gba/m4a_internal.h +++ b/include/gba/m4a_internal.h @@ -170,7 +170,11 @@ struct SoundChannel struct MusicPlayerInfo; +#if __STDC_VERSION__ < 202311L typedef void (*MPlayFunc)(); +#else +typedef void (*MPlayFunc)(...); +#endif typedef void (*PlyNoteFunc)(u32, struct MusicPlayerInfo *, struct MusicPlayerTrack *); typedef void (*CgbSoundFunc)(void); typedef void (*CgbOscOffFunc)(u8); diff --git a/include/global.h b/include/global.h index ab42b113c..77155b090 100644 --- a/include/global.h +++ b/include/global.h @@ -19,6 +19,10 @@ #define asm_comment(x) asm volatile("@ -- " x " -- ") #define asm_unified(x) asm(".syntax unified\n" x "\n.syntax divided") +#if __STDC_VERSION__ < 202311L +#define asm __asm__ +#endif + // IDE support #if defined(__APPLE__) || defined(__CYGWIN__) || defined(__INTELLISENSE__) // We define these when using certain IDEs to fool preproc @@ -131,6 +135,13 @@ extern u8 gStringVar4[]; #define NUM_FLAG_BYTES ROUND_BITS_TO_BYTES(FLAGS_COUNT) #define NUM_ADDITIONAL_PHRASE_BYTES ROUND_BITS_TO_BYTES(NUM_ADDITIONAL_PHRASES) +// This returns the number of arguments passed to it (up to 8). +#define NARG_8(...) NARG_8_(_, ##__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0) +#define NARG_8_(_, a, b, c, d, e, f, g, h, N, ...) N + +#define CAT(a, b) CAT_(a, b) +#define CAT_(a, b) a ## b + // This produces an error at compile-time if expr is zero. // It looks like file.c:line: size of array `id' is negative #define STATIC_ASSERT(expr, id) typedef char id[(expr) ? 1 : -1]; diff --git a/include/librfu.h b/include/librfu.h index 886db0561..6981ba2bc 100644 --- a/include/librfu.h +++ b/include/librfu.h @@ -316,7 +316,11 @@ struct STWIStatus u8 recoveryCount; u8 unk_16; u8 unk_17; +#if __STDC_VERSION__ < 202311L void (*callbackM)(); +#else + void (*callbackM)(...); +#endif void (*callbackS)(u16); void (*callbackID)(void); union RfuPacket *txPacket; diff --git a/include/pokemon.h b/include/pokemon.h index 4202dd2c4..414dc4176 100644 --- a/include/pokemon.h +++ b/include/pokemon.h @@ -335,18 +335,17 @@ u8 GetGenderFromSpeciesAndPersonality(u16 species, u32 personality); void SetMultiuseSpriteTemplateToPokemon(u16 speciesTag, u8 battlerPosition); void SetMultiuseSpriteTemplateToTrainerBack(u16 trainerSpriteId, u8 battlerPosition); -// These are full type signatures for GetMonData() and GetBoxMonData(), -// but they are not used since some code erroneously omits the third arg. -// u32 GetMonData(struct Pokemon *mon, s32 field, u8 *data); -// u32 GetBoxMonData(struct BoxPokemon *boxMon, s32 field, u8 *data); - -#ifdef IS_POKEMON_C -u32 GetMonData(struct Pokemon *, s32, u8 *); -u32 GetBoxMonData(struct BoxPokemon *, s32, u8 *); -#else -u32 GetMonData(); -u32 GetBoxMonData(); -#endif // IS_POKEMON_C +/* GameFreak called Get(Box)MonData with either 2 or 3 arguments, for + * type safety we have a Get(Box)MonData macro which dispatches to + * either Get(Box)MonData2 or Get(Box)MonData3 based on the number of + * arguments. The two functions are aliases of each other, but they + * differ for matching purposes in the caller's codegen. */ +#define GetMonData(...) CAT(GetMonData, NARG_8(__VA_ARGS__))(__VA_ARGS__) +#define GetBoxMonData(...) CAT(GetBoxMonData, NARG_8(__VA_ARGS__))(__VA_ARGS__) +u32 GetMonData3(struct Pokemon *mon, s32 field, u8 *data); +u32 GetMonData2(struct Pokemon *mon, s32 field); +u32 GetBoxMonData3(struct BoxPokemon *boxMon, s32 field, u8 *data); +u32 GetBoxMonData2(struct BoxPokemon *boxMon, s32 field); void SetMonData(struct Pokemon *mon, s32 field, const void *dataArg); void SetBoxMonData(struct BoxPokemon *boxMon, s32 field, const void *dataArg); diff --git a/src/daycare.c b/src/daycare.c index b3bc5c3a0..efa5a951e 100644 --- a/src/daycare.c +++ b/src/daycare.c @@ -605,7 +605,7 @@ static void Debug_AddDaycareSteps(u16 numSteps) u8 GetNumLevelsGainedFromDaycare(void) { - if (GetBoxMonData(&gSaveBlock1Ptr->daycare.mons[gSpecialVar_0x8004], MON_DATA_SPECIES) != 0) + if (GetBoxMonData(&gSaveBlock1Ptr->daycare.mons[gSpecialVar_0x8004].mon, MON_DATA_SPECIES) != 0) return GetNumLevelsGainedForDaycareMon(&gSaveBlock1Ptr->daycare.mons[gSpecialVar_0x8004]); return 0; diff --git a/src/field_player_avatar.c b/src/field_player_avatar.c index 317c91c28..ef086dda3 100644 --- a/src/field_player_avatar.c +++ b/src/field_player_avatar.c @@ -125,7 +125,7 @@ static u8 TeleportAnim_RotatePlayer(struct ObjectEvent * object, s16 *timer); void MovementType_Player(struct Sprite *sprite) { - UpdateObjectEventCurrentMovement(&gObjectEvents[sprite->data[0]], sprite, ObjectEventCB2_NoMovement2); + UpdateObjectEventCurrentMovement(&gObjectEvents[sprite->data[0]], sprite, (bool8 (*)(struct ObjectEvent *, struct Sprite *))ObjectEventCB2_NoMovement2); } static u8 ObjectEventCB2_NoMovement2(struct ObjectEvent * object, struct Sprite *sprite) diff --git a/src/librfu_intr.c b/src/librfu_intr.c index c63ef5a94..e97072a85 100644 --- a/src/librfu_intr.c +++ b/src/librfu_intr.c @@ -6,7 +6,11 @@ static u16 handshake_wait(u16 slot); static void STWI_set_timer_in_RAM(u8 count); static void STWI_stop_timer_in_RAM(void); static void STWI_init_slave(void); +#if __STDC_VERSION__ < 202311L static void Callback_Dummy_M(int reqCommandId, int error, void (*callbackM)()); +#else +static void Callback_Dummy_M(int reqCommandId, int error, void (*callbackM)(...)); +#endif static void Callback_Dummy_S(u16 reqCommandId, void (*callbackS)(u16)); static void Callback_Dummy_ID(void (*callbackId)(void)); @@ -134,7 +138,11 @@ static void sio32intr_clock_master(void) } gSTWIStatus->sending = 0; if (gSTWIStatus->callbackM != NULL) +#if __STDC_VERSION__ < 202311L Callback_Dummy_M(gSTWIStatus->reqActiveCommand, gSTWIStatus->error, gSTWIStatus->callbackM); +#else + Callback_Dummy_M(gSTWIStatus->reqActiveCommand, gSTWIStatus->error, (void (*)(...))gSTWIStatus->callbackM); +#endif } else { @@ -387,7 +395,11 @@ static void STWI_init_slave(void) } NAKED +#if __STDC_VERSION__ < 202311L static void Callback_Dummy_M(int reqCommandId, int error, void (*callbackM)()) +#else +static void Callback_Dummy_M(int reqCommandId, int error, void (*callbackM)(...)) +#endif { asm("bx r2"); } diff --git a/src/m4a.c b/src/m4a.c index 31d519d7b..00d66f8fc 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -1,4 +1,4 @@ -#include +#include "global.h" #include "gba/m4a_internal.h" extern const u8 gCgb3Vol[]; @@ -283,6 +283,7 @@ void MPlayExtender(struct CgbChannel *cgbChans) soundInfo->ident++; +#if __STDC_VERSION__ < 202311L gMPlayJumpTable[8] = ply_memacc; gMPlayJumpTable[17] = ply_lfos; gMPlayJumpTable[19] = ply_mod; @@ -292,6 +293,17 @@ void MPlayExtender(struct CgbChannel *cgbChans) gMPlayJumpTable[31] = TrackStop; gMPlayJumpTable[32] = FadeOutBody; gMPlayJumpTable[33] = TrkVolPitSet; +#else + gMPlayJumpTable[8] = (void (*)(...))ply_memacc; + gMPlayJumpTable[17] = (void (*)(...))ply_lfos; + gMPlayJumpTable[19] = (void (*)(...))ply_mod; + gMPlayJumpTable[28] = (void (*)(...))ply_xcmd; + gMPlayJumpTable[29] = (void (*)(...))ply_endtie; + gMPlayJumpTable[30] = (void (*)(...))SampleFreqSet; + gMPlayJumpTable[31] = (void (*)(...))TrackStop; + gMPlayJumpTable[32] = (void (*)(...))FadeOutBody; + gMPlayJumpTable[33] = (void (*)(...))TrkVolPitSet; +#endif soundInfo->cgbChans = cgbChans; soundInfo->CgbSound = CgbSound; @@ -320,13 +332,21 @@ void MusicPlayerJumpTableCopy(void) void ClearChain(void *x) { +#if __STDC_VERSION__ < 202311L void (*func)(void *) = *(&gMPlayJumpTable[34]); +#else + void (*func)(...) = *(&gMPlayJumpTable[34]); +#endif func(x); } void Clear64byte(void *x) { +#if __STDC_VERSION__ < 202311L void (*func)(void *) = *(&gMPlayJumpTable[35]); +#else + void (*func)(...) = *(&gMPlayJumpTable[35]); +#endif func(x); } diff --git a/src/pokemon.c b/src/pokemon.c index 330ae3dea..66a5c3d23 100644 --- a/src/pokemon.c +++ b/src/pokemon.c @@ -2895,7 +2895,11 @@ static union PokemonSubstruct *GetSubstruct(struct BoxPokemon *boxMon, u32 perso return substruct; } -u32 GetMonData(struct Pokemon *mon, s32 field, u8 *data) +/* GameFreak called GetMonData with either 2 or 3 arguments, for type + * safety we have a GetMonData macro (in include/pokemon.h) which + * dispatches to either GetMonData2 or GetMonData3 based on the number + * of arguments. */ +u32 GetMonData3(struct Pokemon *mon, s32 field, u8 *data) { u32 ret; @@ -2963,7 +2967,13 @@ u32 GetMonData(struct Pokemon *mon, s32 field, u8 *data) return ret; } -u32 GetBoxMonData(struct BoxPokemon *boxMon, s32 field, u8 *data) +u32 GetMonData2(struct Pokemon *mon, s32 field) __attribute__((alias("GetMonData3"))); + +/* GameFreak called GetBoxMonData with either 2 or 3 arguments, for type + * safety we have a GetBoxMonData macro (in include/pokemon.h) which + * dispatches to either GetBoxMonData2 or GetBoxMonData3 based on the + * number of arguments. */ +u32 GetBoxMonData3(struct BoxPokemon *boxMon, s32 field, u8 *data) { s32 i; u32 retVal = 0; @@ -3319,6 +3329,8 @@ u32 GetBoxMonData(struct BoxPokemon *boxMon, s32 field, u8 *data) return retVal; } +u32 GetBoxMonData2(struct BoxPokemon *boxMon, s32 field) __attribute__((alias("GetBoxMonData3"))); + #define SET8(lhs) (lhs) = *data #define SET16(lhs) (lhs) = data[0] + (data[1] << 8) #define SET32(lhs) (lhs) = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24) diff --git a/src/vs_seeker.c b/src/vs_seeker.c index 4d98548a7..71b7b1ece 100644 --- a/src/vs_seeker.c +++ b/src/vs_seeker.c @@ -954,7 +954,11 @@ void ClearRematchStateByTrainerId(void) TryGetObjectEventIdByLocalIdAndMap(objectEventTemplates[i].localId, gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup, &objEventId); objectEvent = &gObjectEvents[objEventId]; + #if __STDC_VERSION__ < 202311L GetRandomFaceDirectionMovementType(&objectEventTemplates[i]); // You are using this function incorrectly. Please consult the manual. + #else + GetRandomFaceDirectionMovementType(); + #endif OverrideMovementTypeForObjectEvent(objectEvent, sFaceDirectionMovementTypeByFacingDirection[objectEvent->facingDirection]); gSaveBlock1Ptr->trainerRematches[objectEventTemplates[i].localId] = 0; if (gSelectedObjectEvent == objEventId) diff --git a/tools/mapjson/json11.cpp b/tools/mapjson/json11.cpp index 3ac5f392a..fcacdbeb3 100644 --- a/tools/mapjson/json11.cpp +++ b/tools/mapjson/json11.cpp @@ -23,6 +23,7 @@ #include #include #include +#include namespace json11 {