From 3b31d9fcfc67d60bb4863644af3090c8a9687984 Mon Sep 17 00:00:00 2001 From: dbalatoni13 <40299962+dbalatoni13@users.noreply.github.com> Date: Sun, 6 Apr 2025 00:19:49 +0200 Subject: [PATCH] Port HuSprAnimRead --- CMakeLists.txt | 4 + include/dolphin/types.h | 2 +- include/port/byteswap.h | 22 +++ src/game/init.c | 2 +- src/game/main.c | 2 +- src/game/sprman.c | 57 ++++++- src/port/ar.c | 17 ++ src/port/byteswap.cpp | 352 ++++++++++++++++++++++++++++++++++++++++ src/port/dvd.c | 5 +- src/port/stubs.c | 7 +- 10 files changed, 454 insertions(+), 16 deletions(-) create mode 100644 include/port/byteswap.h create mode 100644 src/port/byteswap.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b0ff80c..49180e9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,9 +39,12 @@ set(DOLPHIN_FILES ) set(GAME_FILES + src/game/armem.c src/game/card.c src/game/ClusterExec.c src/game/data.c + src/game/decode.c + src/game/dvd.c src/game/EnvelopeExec.c src/game/fault.c src/game/flag.c @@ -71,6 +74,7 @@ set(GAME_FILES set(PORT_FILES src/port/ar.c src/port/arq.c + src/port/byteswap.cpp src/port/dvd.c src/port/imgui.cpp src/port/OS.c diff --git a/include/dolphin/types.h b/include/dolphin/types.h index db8b9b8c..b4d273b4 100644 --- a/include/dolphin/types.h +++ b/include/dolphin/types.h @@ -67,7 +67,7 @@ typedef int BOOL; #define NULL ((void *)0) #endif #endif -#if !defined(__cplusplus) || __cplusplus < 201103L +#if !defined(__cplusplus) #ifndef nullptr #define nullptr NULL #endif diff --git a/include/port/byteswap.h b/include/port/byteswap.h new file mode 100644 index 00000000..59df1f70 --- /dev/null +++ b/include/port/byteswap.h @@ -0,0 +1,22 @@ +#ifndef _SRC_BYTESWAP_H_ +#define _SRC_BYTESWAP_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "game/animdata.h" + +void byteswap_animdata(void *src, AnimData* dest); +void byteswap_animbankdata(void *src, AnimBankData *dest); +void byteswap_animpatdata(void *src, AnimPatData *dest); +void byteswap_animbmpdata(void *src, AnimBmpData *dest); +void byteswap_animframedata(AnimFrameData *src); +void byteswap_animlayerdata(AnimLayerData *src); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/game/init.c b/src/game/init.c index 3deb6725..10979b1c 100644 --- a/src/game/init.c +++ b/src/game/init.c @@ -54,7 +54,7 @@ void HuSysInit(GXRenderModeObj *mode) VISetWindowTitle("Mario Party 4"); #endif PADInit(); - #ifndef TARGET_PC + #ifdef __MWERKS__ #if VERSION_NTSC if(OSGetProgressiveMode() == 1 && VIGetDTVStatus() == 1) { mode = &GXNtsc480Prog; diff --git a/src/game/main.c b/src/game/main.c index 29f1b24f..09931a38 100644 --- a/src/game/main.c +++ b/src/game/main.c @@ -125,7 +125,7 @@ void main(void) GWPlayerCfg[i].character = -1; } - omMasterInit(0, _ovltbl, OVL_COUNT, OVL_BOOT); + //omMasterInit(0, _ovltbl, OVL_COUNT, OVL_BOOT); VIWaitForRetrace(); if (VIGetNextField() == 0) { diff --git a/src/game/sprman.c b/src/game/sprman.c index 159b0f47..043abc36 100644 --- a/src/game/sprman.c +++ b/src/game/sprman.c @@ -3,6 +3,9 @@ #include "game/init.h" #include "dolphin/mtx.h" +#include + +#include "port/byteswap.h" #define SPRITE_DIRTY_ATTR 0x1 #define SPRITE_DIRTY_XFORM 0x2 @@ -212,27 +215,58 @@ AnimData *HuSprAnimRead(void *data) AnimBmpData *bmp; AnimBankData *bank; AnimPatData *pat; +#ifdef TARGET_PC + AnimBmpData *bmp2; + AnimBankData *bank2; + AnimPatData *pat2; +#endif +#ifdef TARGET_PC + AnimData *anim = HuMemDirectMallocNum(HEAP_DATA, sizeof(AnimData), MEMORY_DEFAULT_NUM); + byteswap_animdata(data, anim); +#else AnimData *anim = (AnimData *)data; - if((u32)anim->bank & 0xFFFF0000) { +#endif + if((uintptr_t)anim->bank & ~0xFFFF) { anim->useNum++; return anim; } - bank = (AnimBankData *)((u32)anim->bank+(u32)data); + bank = (AnimBankData *)((uintptr_t)anim->bank+(uintptr_t)data); +#ifdef TARGET_PC + bank2 = HuMemDirectMallocNum(HEAP_DATA, sizeof(AnimBankData), MEMORY_DEFAULT_NUM); + byteswap_animbankdata(bank, bank2); + bank = bank2; +#endif anim->bank = bank; - pat = (AnimPatData *)((u32)anim->pat+(u32)data); + pat = (AnimPatData *)((uintptr_t)anim->pat+(uintptr_t)data); +#ifdef TARGET_PC + pat2 = HuMemDirectMallocNum(HEAP_DATA, sizeof(AnimPatData), MEMORY_DEFAULT_NUM); + byteswap_animpatdata(pat, pat2); + pat = pat2; +#endif anim->pat = pat; - bmp = (AnimBmpData *)((u32)anim->bmp+(u32)data); + bmp = (AnimBmpData *)((uintptr_t)anim->bmp+(uintptr_t)data); +#ifdef TARGET_PC + bmp2 = HuMemDirectMallocNum(HEAP_DATA, sizeof(AnimBmpData), MEMORY_DEFAULT_NUM); + byteswap_animbmpdata(bmp, bmp2); + bmp = bmp2; +#endif anim->bmp = bmp; for(i=0; ibankNum; i++, bank++) { - bank->frame = (AnimFrameData *)((u32)bank->frame+(u32)data); + bank->frame = (AnimFrameData *)((uintptr_t)bank->frame+(uintptr_t)data); +#ifdef TARGET_PC + byteswap_animframedata(bank->frame); +#endif } for(i=0; ipatNum; i++, pat++) { - pat->layer = (AnimLayerData *)((u32)pat->layer+(u32)data); + pat->layer = (AnimLayerData *)((uintptr_t)pat->layer+(uintptr_t)data); +#ifdef TARGET_PC + byteswap_animlayerdata(pat->layer); +#endif } for(i=0; ibmpNum; i++, bmp++) { - bmp->palData = (void *)((u32)bmp->palData+(u32)data); - bmp->data = (void *)((u32)bmp->data+(u32)data); + bmp->palData = (void *)((uintptr_t)bmp->palData+(uintptr_t)data); + bmp->data = (void *)((uintptr_t)bmp->data+(uintptr_t)data); } anim->useNum = 0; return anim; @@ -401,6 +435,11 @@ void HuSprAnimKill(AnimData *anim) HuMemDirectFree(anim->bmp->palData); } } +#ifdef TARGET_PC + HuMemDirectFree(anim->bank); + HuMemDirectFree(anim->pat); + HuMemDirectFree(anim->bmp); +#endif HuMemDirectFree(anim); } } @@ -720,4 +759,4 @@ void AnimDebug(AnimData *anim) OSReport("\tpixSize %d,palNum %d,size (%d,%d)\n", bmp->pixSize, bmp->palNum, bmp->sizeX, bmp->sizeY); bmp++; } -} \ No newline at end of file +} diff --git a/src/port/ar.c b/src/port/ar.c index 3f628cd2..b7ae0092 100644 --- a/src/port/ar.c +++ b/src/port/ar.c @@ -4,6 +4,23 @@ u8 ARAM[16 * 1024 * 1024]; +u32 ARInit(u32 *stack_index_addr, u32 num_entries) +{ + puts("ARInit"); + return 0x4000; +} + +BOOL ARCheckInit() +{ + return TRUE; +} + +u32 ARGetSize() +{ + // TODO + return 0x8000; +} + void ARStartDMA(u32 type, u32 mainmem_addr, u32 aram_addr, u32 length) { switch (type) diff --git a/src/port/byteswap.cpp b/src/port/byteswap.cpp new file mode 100644 index 00000000..547dedcb --- /dev/null +++ b/src/port/byteswap.cpp @@ -0,0 +1,352 @@ +#include +#include +#include +#include +#include +#include + +extern "C" +{ +#include "port/byteswap.h" + +typedef struct AnimData32b { + s16 bankNum; + s16 patNum; + s16 bmpNum; + s16 useNum; + + u32 bank; + u32 pat; + u32 bmp; +} AnimData32b; + +typedef struct AnimBankData32b { + s16 timeNum; + s16 unk; + u32 frame; +} AnimBankData32b; + +typedef struct AnimPatData32b { + s16 layerNum; + s16 centerX; + s16 centerY; + s16 sizeX; + s16 sizeY; + u32 layer; +} AnimPatData32b; + +typedef struct AnimBmpData32b { + u8 pixSize; + u8 dataFmt; + s16 palNum; + s16 sizeX; + s16 sizeY; + u32 dataSize; + u32 palData; + u32 data; +} AnimBmpData32b; + +} + +template [[nodiscard]] constexpr T bswap16(T val) noexcept +{ + static_assert(sizeof(T) == sizeof(u16)); + union { + u16 u; + T t; + } v{.t = val}; +#if __GNUC__ + v.u = __builtin_bswap16(v.u); +#elif _WIN32 + v.u = _byteswap_ushort(v.u); +#else + v.u = (v.u << 8) | ((v.u >> 8) & 0xFF); +#endif + return v.t; +} + +template [[nodiscard]] constexpr T bswap32(T val) noexcept +{ + static_assert(sizeof(T) == sizeof(u32)); + union { + u32 u; + T t; + } v{.t = val}; +#if __GNUC__ + v.u = __builtin_bswap32(v.u); +#elif _WIN32 + v.u = _byteswap_ulong(v.u); +#else + v.u = ((v.u & 0x0000FFFF) << 16) | ((v.u & 0xFFFF0000) >> 16) | ((v.u & 0x00FF00FF) << 8) | + ((v.u & 0xFF00FF00) >> 8); +#endif + return v.t; +} + +static void bswap16_unaligned(u8 *ptr) +{ + u8 temp = ptr[0]; + ptr[0] = ptr[1]; + ptr[1] = temp; +} + +static void bswap32_unaligned(u8 *ptr) +{ + u8 temp = ptr[0]; + ptr[0] = ptr[3]; + ptr[3] = temp; + temp = ptr[1]; + ptr[1] = ptr[2]; + ptr[2] = temp; +} + +static std::unordered_set sVisitedPtrs; + +template T *offset_ptr(B &base, T *ptr) +{ + return reinterpret_cast(reinterpret_cast(&base) + + reinterpret_cast(ptr)); +} +template T *offset_ptr(B &base, T *ptr, void *extra) +{ + return reinterpret_cast(reinterpret_cast(&base) + + reinterpret_cast(ptr) + + reinterpret_cast(extra)); +} + +template static inline void bswap(B &base, T &data); +template void bswap(B &base, P *&ptr) +{ + ptr = bswap32(ptr); +} +template void bswap(B &base, T *&ptr, s32 count) +{ + ptr = bswap32(ptr); + if (ptr == nullptr) + { + return; + } + T *objBase = offset_ptr(base, ptr); + for (s32 i = 0; i < count; ++i) + { + if (sVisitedPtrs.contains(objBase)) + { + continue; + } + sVisitedPtrs.insert(objBase); + bswap(base, *objBase); + ++objBase; + } +} +template void bswap_list(B &base, T **&ptr) +{ + ptr = bswap32(ptr); + if (ptr == nullptr) + { + return; + } + T **objBase = offset_ptr(base, ptr); + while (*objBase != nullptr) + { + bswap(base, *objBase, 1); + ++objBase; + } +} +template void bswap_list(B &base, T *(&ptr)[]) +{ + T **objBase = ptr; + while (*objBase != nullptr) + { + bswap(base, *objBase, 1); + ++objBase; + } +} +template void bswap_flat(B &base, T *start, s32 count) +{ + T *objBase = start; + for (s32 i = 0; i < count; ++i) + { + bswap(base, objBase[i]); + } +} +template void bswap(B &base, f32 &v) +{ + v = bswap32(v); +} +template void bswap(B &base, s32 &v) +{ + v = bswap32(v); +} +template void bswap(B &base, u32 &v) +{ + v = bswap32(v); +} +template void bswap(B &base, s16 &v) +{ + v = bswap16(v); +} +template void bswap(B &base, u16 &v) +{ + v = bswap16(v); +} +template void bswap(B &base, u8 &v) +{ + // no-op +} +template void bswap(B &base, s8 &v) +{ + // no-op +} +template void bswap(B &base, char &v) +{ + // no-op +} +template void bswap(B &base, Vec &vec) +{ + bswap(base, vec.x); + bswap(base, vec.y); + bswap(base, vec.z); +} +template void bswap(B &base, S16Vec &vec) +{ + bswap(base, vec.x); + bswap(base, vec.y); + bswap(base, vec.z); +} +template void bswap(B &base, Vec2f &vec) +{ + bswap(base, vec.x); + bswap(base, vec.y); +} + +template void bswap(B &base, AnimData32b &obj, AnimData &dest) +{ + bswap(base, obj.bankNum); + bswap(base, obj.patNum); + bswap(base, obj.bmpNum); + bswap(base, obj.useNum); + bswap(base, obj.bank); + bswap(base, obj.pat); + bswap(base, obj.bmp); + + dest.bankNum = obj.bankNum; + dest.patNum = obj.patNum; + dest.bmpNum = obj.bmpNum; + dest.useNum = obj.useNum; + dest.bank = reinterpret_cast(obj.bank); + dest.pat = reinterpret_cast(obj.pat); + dest.bmp = reinterpret_cast(obj.bmp); +} + +template void bswap(B &base, AnimBankData32b &obj, AnimBankData &dest) +{ + bswap(base, obj.timeNum); + bswap(base, obj.unk); + bswap(base, obj.frame); + + dest.timeNum = obj.timeNum; + dest.unk = obj.unk; + dest.frame = reinterpret_cast(obj.frame); +} + +template void bswap(B &base, AnimPatData32b &obj, AnimPatData &dest) +{ + bswap(base, obj.layerNum); + bswap(base, obj.centerX); + bswap(base, obj.centerY); + bswap(base, obj.sizeX); + bswap(base, obj.sizeY); + bswap(base, obj.layer); + + dest.layerNum = obj.layerNum; + dest.centerX = obj.centerX; + dest.centerY = obj.centerY; + dest.sizeX = obj.sizeX; + dest.sizeY = obj.sizeY; + dest.layer = reinterpret_cast(obj.layer); +} + +template void bswap(B &base, AnimBmpData32b &obj, AnimBmpData &dest) +{ + bswap(base, obj.pixSize); + bswap(base, obj.dataFmt); + bswap(base, obj.palNum); + bswap(base, obj.sizeX); + bswap(base, obj.sizeY); + bswap(base, obj.dataSize); + bswap(base, obj.palData); + bswap(base, obj.data); + + dest.pixSize = obj.pixSize; + dest.dataFmt = obj.dataFmt; + dest.palNum = obj.palNum; + dest.sizeX = obj.sizeX; + dest.sizeY = obj.sizeY; + dest.dataSize = obj.dataSize; + dest.palData = reinterpret_cast(obj.palData); + dest.data = reinterpret_cast(obj.data); +} + +template void bswap(B &base, AnimFrameData &obj) +{ + bswap(base, obj.pat); + bswap(base, obj.time); + bswap(base, obj.shiftX); + bswap(base, obj.shiftY); + bswap(base, obj.flip); + bswap(base, obj.pad); +} + +template void bswap(B &base, AnimLayerData &obj) +{ + bswap(base, obj.alpha); + bswap(base, obj.flip); + bswap(base, obj.bmpNo); + bswap(base, obj.startX); + bswap(base, obj.startY); + bswap(base, obj.sizeX); + bswap(base, obj.sizeY); + bswap(base, obj.shiftX); + bswap(base, obj.shiftY); + bswap_flat(base, obj.vtx, sizeof(obj.vtx) / sizeof(s16)); +} + +void byteswap_animdata(void *src, AnimData *dest) +{ + auto *anim = reinterpret_cast(src); + bswap(*anim, *anim, *dest); + sVisitedPtrs.clear(); +} + +void byteswap_animbankdata(void *src, AnimBankData *dest) +{ + auto *bank = reinterpret_cast(src); + bswap(*bank, *bank, *dest); + sVisitedPtrs.clear(); +} + +void byteswap_animpatdata(void *src, AnimPatData *dest) +{ + auto *pat = reinterpret_cast(src); + bswap(*pat, *pat, *dest); + sVisitedPtrs.clear(); +} + +void byteswap_animbmpdata(void *src, AnimBmpData *dest) +{ + auto *bmp = reinterpret_cast(src); + bswap(*bmp, *bmp, *dest); + sVisitedPtrs.clear(); +} + +void byteswap_animframedata(AnimFrameData *src) +{ + bswap(*src, *src); + sVisitedPtrs.clear(); +} + +void byteswap_animlayerdata(AnimLayerData *src) +{ + bswap(*src, *src); + sVisitedPtrs.clear(); +} diff --git a/src/port/dvd.c b/src/port/dvd.c index f1b67228..31795f2d 100644 --- a/src/port/dvd.c +++ b/src/port/dvd.c @@ -161,7 +161,8 @@ s32 DVDGetDriveStatus(void) return 0; } -void HuDvdErrorWatch(void) +s32 DVDGetCommandBlockStatus(const DVDCommandBlock *block) { - + // TODO + return 0; } diff --git a/src/port/stubs.c b/src/port/stubs.c index 17f6410d..52c6fe0a 100644 --- a/src/port/stubs.c +++ b/src/port/stubs.c @@ -669,13 +669,15 @@ void GXResetWriteGatherPipe(void) puts("GXResetWriteGatherPipe is a stub"); } +void ARQInit(void) { + puts("ARQInit is a stub"); +} + // Hudson void HuDvdErrDispInit(GXRenderModeObj *rmode, void *xfb1, void *xfb2) { } void HuAudInit(void) { } -void HuARInit(void) { } - void msmSysRegularProc(void) { puts("msmSysRegularProc is a stub"); @@ -689,6 +691,7 @@ void msmMusFdoutEnd(void) s32 HuSoftResetButtonCheck(void) { puts("HuSoftResetButtonCheck is a stub"); + return 0; } void MGSeqMain(void)