From ab4c1a9bad94d9319ebbe3323785a9e1c802ed60 Mon Sep 17 00:00:00 2001 From: nmlgc Date: Mon, 6 Sep 2021 20:16:39 +0200 Subject: [PATCH] [Maintenance] Add a template for stupid bytewise access to logical structures For the cases where ZUN's code would always be cleaner without it. Part of P0157, funded by Yanga. --- decomp.hpp | 10 +++++++++- th01/formats/stagedat.hpp | 13 +++++-------- th01/fuuin_02.cpp | 1 + th01/hiscore/scoredat.hpp | 18 +++++++----------- th01/main/stage/stageobj.cpp | 11 ++++++----- th01/main_19.cpp | 1 + 6 files changed, 29 insertions(+), 25 deletions(-) diff --git a/decomp.hpp b/decomp.hpp index a645a42d..387553c5 100644 --- a/decomp.hpp +++ b/decomp.hpp @@ -1,6 +1,6 @@ /* ReC98 * ----- - * Macros and inline functions to help decompiling the seemingly impossible + * Declarations to help decompiling the seemingly impossible */ // Flag comparisons @@ -29,6 +29,14 @@ } // extern "C" was a mistake +// Should just be unwrapped wherever it appears. Code that directly uses T +// would be much cleaner. +template union StupidBytewiseWrapperAround { + T t; + int8_t byte[sizeof(T)]; + uint8_t ubyte[sizeof(T)]; +}; + // poke() versions that actually inline with pseudoregisters // --------------------------------------------------------- #define pokew(sgm, off, val) { *(uint16_t far *)(MK_FP(sgm, off)) = val; } diff --git a/th01/formats/stagedat.hpp b/th01/formats/stagedat.hpp index 8e93c205..0ae4a0ee 100644 --- a/th01/formats/stagedat.hpp +++ b/th01/formats/stagedat.hpp @@ -65,14 +65,11 @@ typedef enum { OT_BAR_RIGHT = 21, } obstacle_type_t; -union stagedat_stage_t { - struct { - // Stores the cards of [CARDS_PER_BYTE] horizontal tiles in the lower - // [CARDS_PER_BYTE] bits. The other bits are unused. - uint8_t cards[STAGEOBJS_Y][STAGEOBJS_X / CARDS_PER_BYTE]; +struct stagedat_stage_t { + // Stores the cards of [CARDS_PER_BYTE] horizontal tiles in the lower + // [CARDS_PER_BYTE] bits. The other bits are unused. + uint8_t cards[STAGEOBJS_Y][STAGEOBJS_X / CARDS_PER_BYTE]; - int8_t obstacles[STAGEOBJS_Y][STAGEOBJS_X]; // obstacle_type_t - } type; - int8_t byte[sizeof((stagedat_stage_t*)0)->type]; + int8_t obstacles[STAGEOBJS_Y][STAGEOBJS_X]; // obstacle_type_t }; /// -------------------------------------------------------------------------- diff --git a/th01/fuuin_02.cpp b/th01/fuuin_02.cpp index a3f54202..e1cf3236 100644 --- a/th01/fuuin_02.cpp +++ b/th01/fuuin_02.cpp @@ -11,6 +11,7 @@ extern "C" { #include #include "ReC98.h" #include "x86real.h" +#include "decomp.hpp" #include "pc98kbd.h" #include "twobyte.h" #include "master.hpp" diff --git a/th01/hiscore/scoredat.hpp b/th01/hiscore/scoredat.hpp index 5ed94dbf..77c8aa0a 100644 --- a/th01/hiscore/scoredat.hpp +++ b/th01/hiscore/scoredat.hpp @@ -13,13 +13,6 @@ #define SCOREDAT_NAME_KEY 0x9C -// Exclusively used to store full-width Shift-JIS code points. -// Not null-terminated. -union scoredat_name_t { - int16_t codepoint[SCOREDAT_NAME_KANJI]; - int8_t byte[SCOREDAT_NAME_BYTES]; -}; - // Encodes or decodes a single name byte. int8_t scoredat_name_byte_encode(int8_t byte); int8_t scoredat_name_byte_decode(int8_t byte); @@ -29,7 +22,11 @@ int8_t scoredat_name_byte_decode(int8_t byte); struct scoredat_t { // Not null-terminated! char magic[sizeof(SCOREDAT_MAGIC) - 1]; - scoredat_name_t name[SCOREDAT_PLACES]; + + // Exclusively used to store full-width Shift-JIS code points. + // Not null-terminated. + int16_t name[SCOREDAT_PLACES][SCOREDAT_NAME_KANJI]; + uint32_t points[SCOREDAT_PLACES]; int16_t stage[SCOREDAT_PLACES]; twobyte_t route[SCOREDAT_PLACES]; @@ -50,10 +47,9 @@ inline int8_t& scoredat_route_byte(int place, int byte) } // Null-terminated version of scoredat_name_t, used internally. -union scoredat_name_z_t { +typedef StupidBytewiseWrapperAround scoredat_name_z_t; // Loads the score file for the current [rank], recreating it if necessary. // Returns 0 on success, 1 on failure. diff --git a/th01/main/stage/stageobj.cpp b/th01/main/stage/stageobj.cpp index bee730fb..891ef624 100644 --- a/th01/main/stage/stageobj.cpp +++ b/th01/main/stage/stageobj.cpp @@ -3,6 +3,7 @@ extern "C" { #include #include "platform.h" #include "pc98.h" +#include "decomp.hpp" #include "planar.h" #include "master.hpp" #include "th01/common.h" @@ -26,7 +27,7 @@ static const int TURRET_SLOW_INTERVAL = 200; // ------- struct stage_t { - stagedat_stage_t dat; + StupidBytewiseWrapperAround dat; int8_t padding[5]; }; extern stage_t scene_stage[STAGES_PER_SCENE]; @@ -36,19 +37,19 @@ extern stage_t scene_stage[STAGES_PER_SCENE]; // ----------------------------------------- inline int cards_begin() { - return offsetof(stagedat_stage_t, type.cards); + return offsetof(stagedat_stage_t, cards); } inline int cards_end() { - return (cards_begin() + sizeof(((stagedat_stage_t*)0)->type.cards)); + return (cards_begin() + sizeof(((stagedat_stage_t*)0)->cards)); } inline int obstacles_begin() { - return offsetof(stagedat_stage_t, type.obstacles); + return offsetof(stagedat_stage_t, obstacles); } inline int obstacles_end() { - return (obstacles_begin() + sizeof(((stagedat_stage_t*)0)->type.obstacles)); + return (obstacles_begin() + sizeof(((stagedat_stage_t*)0)->obstacles)); } // ----------------------------------------- diff --git a/th01/main_19.cpp b/th01/main_19.cpp index e464feb0..37f1b354 100644 --- a/th01/main_19.cpp +++ b/th01/main_19.cpp @@ -11,6 +11,7 @@ extern "C" { #include #include "ReC98.h" #include "x86real.h" +#include "decomp.hpp" #include "twobyte.h" #include "master.hpp" #include "th01/ranks.h"