From 32f42645d555d01fa3f9ac3839e789a05da5b973 Mon Sep 17 00:00:00 2001 From: Jonathan Hohle Date: Sun, 16 Jun 2024 20:11:42 -0700 Subject: [PATCH] Decompile main (#1326) Decompiles `main`. Co-authored-by: Jonathan Hohle --- config/symbols.us.main.txt | 3 ++ include/game.h | 3 ++ src/main/main.c | 73 +++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/config/symbols.us.main.txt b/config/symbols.us.main.txt index e948e71bd..b8715aea0 100644 --- a/config/symbols.us.main.txt +++ b/config/symbols.us.main.txt @@ -1,3 +1,6 @@ D_80010960 = 0x80010960; // force_migration:True D_80010964 = 0x80010964; // force_migration:True +main_fd = 0x800339F8; +main_cdlFile = 0x800339FC; +main_cdlMode = 0x80033A14; g_MainGame = 0x800A0000; diff --git a/include/game.h b/include/game.h index fe8187a76..a97cd97ce 100644 --- a/include/game.h +++ b/include/game.h @@ -185,6 +185,7 @@ typedef struct Prim { #define WEAPON_1_START 0xF0 #ifndef VERSION_PC +#define DRA_PRG_PTR 0x800A0000 #define RIC_PRG_PTR 0x8013C000 #define FAMILIAR_PTR 0x80170000 #define WEAPON0_PTR 0x8017A000 @@ -199,6 +200,7 @@ typedef struct Prim { #define SIM_PTR 0x80280000 #else +#define DRA_PRG_PTR 0x800A0000 #define RIC_PRG_PTR 0x8013C000 #define FAMILIAR_PTR 0x80170000 #define WEAPON0_PTR 0x8017A000 @@ -334,6 +336,7 @@ typedef enum { Game_PrologueEnd, Game_MainMenu, Game_Ending, + Game_Boot, Game_99 = 99, } GameState; diff --git a/src/main/main.c b/src/main/main.c index 682dd2844..4681d0e1c 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -1,5 +1,76 @@ #include "common.h" +#include "game.h" -INCLUDE_ASM("main/nonmatchings/main", main); +extern void (*g_MainGame)(); +extern CdlFILE main_cdlFile; +extern u8 main_cdlMode[]; +extern s32 main_fd; + +void main(void) { + ResetCallback(); + InitHeap((void*)0x801F0000, 0x7800); + CdInit(); + + g_GameState = Game_Boot; + + do { + main_fd = open("sim:c:\\bin\\dra.bin", 1); + } while (main_fd == -1 && --g_GameState != Game_Init); + + if (g_GameState != Game_Init) { + read(main_fd, (void*)DRA_PRG_PTR, 0x130000); + close(main_fd); + g_UseDisk = 0; + main_fd = open("sim:c:\\bin\\dra.bio", 1); + + if (main_fd != -1) { + g_UseDisk = 2; + } + + do { + main_fd = open("sim:c:\\bin\\f_map.bin", 1); + } while (main_fd < 0); + + read(main_fd, (void*)CASTLE_MAP_PTR, 0x8000); + close(main_fd); + } else { + main_search_loop_1: + while (!CdSearchFile(&main_cdlFile, "\\DRA.BIN;1")) { + } + + *main_cdlMode = CdlModeSpeed | CdlModeSpeedNormal; + CdControl(CdlSetmode, main_cdlMode, NULL); + + while (CdSync(1, NULL) != 2) { + } + + g_GameState = (main_cdlFile.size + 0x7ff) >> 0xB; + CdControl(CdlSetloc, &main_cdlFile, NULL); + CdRead(g_GameState, (u32*)DRA_PRG_PTR, 0x80); + // block until read is complete + main_fd = CdReadSync(0, NULL); + if (main_fd < 0) { + goto main_search_loop_1; + } + + g_UseDisk = 1; + + main_search_loop_2: + if (!CdSearchFile(&main_cdlFile, "\\F_MAP.BIN;1")) { + goto main_search_loop_2; + } else { + g_GameState = (main_cdlFile.size + 0x7ff) >> 0xB; + } + CdControl(CdlSetloc, &main_cdlFile, NULL); + CdRead(g_GameState, (u32*)CASTLE_MAP_PTR, 0x80); + // block until read is complete + main_fd = CdReadSync(0, NULL); + + if (main_fd < 0) { + goto main_search_loop_2; + } + } + g_MainGame(); +} const char a0123456789abcd[] = "0123456789ABCDEF\0\0\0\0";