diff --git a/include/global.h b/include/global.h index 63e854d328..ee8a2f4655 100644 --- a/include/global.h +++ b/include/global.h @@ -4,5 +4,6 @@ #include "structs.h" #include "functions.h" #include "variables.h" +#include "macros.h" #endif diff --git a/include/macros.h b/include/macros.h new file mode 100644 index 0000000000..59d2319aa7 --- /dev/null +++ b/include/macros.h @@ -0,0 +1,13 @@ +#ifndef _MACROS_H_ +#define _MACROS_H_ + +#define ARRAY_COUNT(arr) (s32)(sizeof(arr) / sizeof(arr[0])) +#define ARRAY_COUNTU(arr) (u32)(sizeof(arr) / sizeof(arr[0])) + +#define PHYSICAL_TO_VIRTUAL(addr) ((u32)(addr) + 0x80000000) +#define PHYSICAL_TO_VIRTUAL2(addr) ((u32)(addr) - 0x80000000) +#define SEGMENTED_TO_VIRTUAL(addr) (void*)(PHYSICAL_TO_VIRTUAL(gRspSegmentPhysAddrs[SEGMENT_NUMBER(addr)]) + SEGMENT_OFFSET(addr)) + +#define ALIGN16(val) (((val) + 0xF) & ~0xF) + +#endif diff --git a/include/structs.h b/include/structs.h index 65791cfa5d..9b69e69376 100644 --- a/include/structs.h +++ b/include/structs.h @@ -1468,7 +1468,7 @@ typedef struct { /* 0x009 */ u8 spawnedObjectCount; /* 0x00A */ u8 mainKeepIndex; /* 0x00B */ u8 keepObjectId; -/* 0x00C */ SceneObject objects[35]; +/* 0x00C */ SceneObject objects[35]; // TODO: OBJECT_EXCHANGE_BANK_MAX array size } SceneContext; // size = 0x958 typedef struct { diff --git a/include/variables.h b/include/variables.h index df5105ff9e..ac84cb3133 100644 --- a/include/variables.h +++ b/include/variables.h @@ -4109,7 +4109,7 @@ extern UNK_TYPE1 D_801F80F8; // D_801F80F8 extern u64 lastRenderFrameTimestamp; // D_801F8150 extern OSMesgQueue siEventCallbackQueue; // D_801F8160 extern OSMesg siEventCallbackBuffer[1]; // D_801F8178 -extern void* gRspSegmentPhysAddrs[16]; // D_801F8180 +extern u32 gRspSegmentPhysAddrs[16]; // D_801F8180 extern SchedThreadStruct schedContext; // D_801F81C0 extern OSMesgQueueListNode mainIrqmgrCallbackNode; // D_801F84F8 extern OSMesgQueue mainIrqmgrCallbackQueue; // D_801F8500 diff --git a/src/code/z_actor.c b/src/code/z_actor.c index 2a7abb4588..c792a6b1ea 100644 --- a/src/code/z_actor.c +++ b/src/code/z_actor.c @@ -215,9 +215,8 @@ void Actor_SetScale(Actor* actor, f32 scale) { } void Actor_SetObjectSegment(GlobalContext* ctxt, Actor* actor) { - // TODO: PHYSICAL_TO_VIRTUAL macro // TODO: Segment number enum - gRspSegmentPhysAddrs[6] = (void*)((u32)ctxt->sceneContext.objects[actor->objBankIndex].vramAddr + 0x80000000); + gRspSegmentPhysAddrs[6] = PHYSICAL_TO_VIRTUAL(ctxt->sceneContext.objects[actor->objBankIndex].vramAddr); } #ifdef NON_MATCHING diff --git a/src/code/z_lib.c b/src/code/z_lib.c index f4209e98b6..101af60d9a 100644 --- a/src/code/z_lib.c +++ b/src/code/z_lib.c @@ -607,9 +607,8 @@ f32 Lib_PushAwayVec3f(Vec3f* start, Vec3f* pusher, f32 distanceToApproach) { void Lib_Nop801004FC(void) {} void* Lib_PtrSegToVirt(void* ptr) { - // TODO: PHYSICAL_TO_VIRTUAL macro // UB to cast the pointer to u32 - return (void*)(((u32)gRspSegmentPhysAddrs[((u32)ptr << 4) >> 28] + ((u32)ptr & 0xFFFFFF)) + 0x80000000); + return (void*)PHYSICAL_TO_VIRTUAL((u32)gRspSegmentPhysAddrs[((u32)ptr << 4) >> 28] + ((u32)ptr & 0xFFFFFF)); } void* Lib_PtrSegToVirtNull(void* ptr) { @@ -618,17 +617,15 @@ void* Lib_PtrSegToVirtNull(void* ptr) { return ptr; } - // TODO: PHYSICAL_TO_VIRTUAL macro // UB to cast the pointer to u32 - return (void*)(((u32)gRspSegmentPhysAddrs[((u32)ptr << 4) >> 28] + ((u32)ptr & 0xFFFFFF)) + 0x80000000); + return (void*)PHYSICAL_TO_VIRTUAL((u32)gRspSegmentPhysAddrs[((u32)ptr << 4) >> 28] + ((u32)ptr & 0xFFFFFF)); } void* Lib_PtrSegToK0(void* ptr) { if (ptr == NULL) { return NULL; } else { - // TODO: PHYSICAL_TO_VIRTUAL macro - return (void*)((u32)ptr + 0x80000000); + return (void*)PHYSICAL_TO_VIRTUAL(ptr); } } @@ -636,7 +633,6 @@ void* Lib_PtrSegToK0Null(void* ptr) { if (ptr == NULL) { return NULL; } else { - // TODO: PHYSICAL_TO_VIRTUAL macro - return (void*)((u32)ptr + 0x80000000); + return (void*)PHYSICAL_TO_VIRTUAL(ptr); } } \ No newline at end of file diff --git a/src/code/z_room.c b/src/code/z_room.c index 5e8e6d42ed..390783018b 100644 --- a/src/code/z_room.c +++ b/src/code/z_room.c @@ -109,9 +109,8 @@ s32 Room_HandleLoadCallbacks(GlobalContext* ctxt, RoomContext* roomCtxt) { { roomCtxt->unk31 = 0; roomCtxt->currRoom.segment = roomCtxt->activeRoomVram; - // TODO: PHYSICAL_TO_VIRTUAL macro // TODO: Segment number enum - gRspSegmentPhysAddrs[3] = (void*)((u32)roomCtxt->activeRoomVram + 0x80000000); + gRspSegmentPhysAddrs[3] = PHYSICAL_TO_VIRTUAL(roomCtxt->activeRoomVram); Scene_ProcessHeader(ctxt, (SceneCmd*)roomCtxt->currRoom.segment); func_80123140(ctxt, (ActorPlayer*)ctxt->actorCtx.actorList[2].first); @@ -135,9 +134,8 @@ s32 Room_HandleLoadCallbacks(GlobalContext* ctxt, RoomContext* roomCtxt) { void Room_Draw(GlobalContext* ctxt, Room* room, u32 flags) { if (room->segment != NULL) { - // TODO: PHYSICAL_TO_VIRTUAL macro // TODO: Segment number enum - gRspSegmentPhysAddrs[3] = (void*)((u32)room->segment + 0x80000000); + gRspSegmentPhysAddrs[3] = PHYSICAL_TO_VIRTUAL(room->segment); roomDrawFuncs[room->mesh->type0.type](ctxt, room, flags); } return; diff --git a/src/code/z_scene.c b/src/code/z_scene.c index 79422d7ae8..9d0f89fd1c 100644 --- a/src/code/z_scene.c +++ b/src/code/z_scene.c @@ -5,10 +5,9 @@ TODO: There are a few issues left with this file, but many rely on larger structural project changes. I am avoiding these in the mean time in order to not break the Ghidra project structures. -We need a header file for just z_scene. Including relevant structs, Scene, and Object enums. +We need a header file for just z_scene. Including relevant structs, scene, and object enums. +Needs definition for OBJECT_EXCHANGE_BANK_MAX The .data, .bss, and .rodata sections are not migrated to this file yet. - -Additionally we need a macro header file for ALIGN16, PHYSICAL_TO_VIRTUAL, OBJECT_EXCHANGE_BANK_MAX and other global macros. */ s32 Scene_LoadObject(SceneContext* sceneCtxt, s16 id) { @@ -26,8 +25,8 @@ s32 Scene_LoadObject(SceneContext* sceneCtxt, s16 id) { // TODO: This 0x22 is OBJECT_EXCHANGE_BANK_MAX - 1 in OOT if (sceneCtxt->objectCount < 0x22) { sceneCtxt->objects[sceneCtxt->objectCount + 1].vramAddr = - // TODO: ALIGN16 macro here - (void*)((((u32)sceneCtxt->objects[sceneCtxt->objectCount].vramAddr + size) + 0xF) & ~0xF); + // UB to cast pointer to u32 + (void*)ALIGN16((u32)sceneCtxt->objects[sceneCtxt->objectCount].vramAddr + size); } sceneCtxt->objectCount++; @@ -66,9 +65,8 @@ void Scene_Init(GlobalContext* ctxt, SceneContext* sceneCtxt) { sceneCtxt->objectVramEnd = (void*)((u32)sceneCtxt->objectVramStart + spaceSize); // TODO: Second argument here is an object enum sceneCtxt->mainKeepIndex = Scene_LoadObject(sceneCtxt, 1); - // TODO: PHYSICAL_TO_VIRTUAL macro // TODO: Segment number enum - gRspSegmentPhysAddrs[4] = (void*)((u32)sceneCtxt->objects[sceneCtxt->mainKeepIndex].vramAddr + 0x80000000); + gRspSegmentPhysAddrs[4] = PHYSICAL_TO_VIRTUAL(sceneCtxt->objects[sceneCtxt->mainKeepIndex].vramAddr); } void Scene_ReloadUnloadedObjects(SceneContext* sceneCtxt) { @@ -148,7 +146,7 @@ void* func_8012F73C(SceneContext* sceneCtxt, s32 iParm2, s16 id) { // TODO: UB to cast void to u32 addr = ((u32)sceneCtxt->objects[iParm2].vramAddr) + vromSize; // TODO: This is ALIGN16 macro from OOT. Seems to be aligning an address to DMA - addr = (addr + 0xF) & ~0xF; + addr = ALIGN16(addr); // UB to cast u32 to pointer return (void*)addr; } @@ -236,10 +234,9 @@ void Scene_HeaderCommand07(GlobalContext* ctxt, SceneCmd* entry) { if (entry->specialFiles.keepObjectId != 0) { ctxt->sceneContext.keepObjectId = Scene_LoadObject(&ctxt->sceneContext, entry->specialFiles.keepObjectId); - // TODO: PHYSICAL_TO_VIRTUAL macro // TODO: Segment number enum - gRspSegmentPhysAddrs[5] = (void*)((u32)(ctxt->sceneContext.objects[ctxt->sceneContext.keepObjectId].vramAddr) - + 0x80000000); + gRspSegmentPhysAddrs[5] = + PHYSICAL_TO_VIRTUAL(ctxt->sceneContext.objects[ctxt->sceneContext.keepObjectId].vramAddr); } if (entry->specialFiles.cUpElfMsgNum != 0) {