From 6a131b037d9133d6bffcb8e4e67940fcf069e539 Mon Sep 17 00:00:00 2001 From: notaz Date: Tue, 24 Oct 2023 01:22:27 +0300 Subject: [PATCH] save some things that weren't saved but should have been libretro/pcsx_rearmed#274 --- frontend/libretro.c | 18 ++++++++++++++---- libpcsxcore/misc.c | 41 +++++++++++++++++++++++++++++++++++++++++ libpcsxcore/misc.h | 1 + 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/frontend/libretro.c b/frontend/libretro.c index a30a1ab2..d74a7b41 100644 --- a/frontend/libretro.c +++ b/frontend/libretro.c @@ -98,6 +98,7 @@ static bool show_advanced_gpu_peops_settings = true; static bool show_advanced_gpu_unai_settings = true; #endif static float mouse_sensitivity = 1.0f; +static unsigned int disk_current_index; typedef enum { @@ -1070,14 +1071,24 @@ static void save_close(void *file) bool retro_serialize(void *data, size_t size) { - int ret = SaveState(data); + int ret; + CdromFrontendId = disk_current_index; + ret = SaveState(data); return ret == 0 ? true : false; } +static bool disk_set_image_index(unsigned int index); + bool retro_unserialize(const void *data, size_t size) { - int ret = LoadState(data); - return ret == 0 ? true : false; + int ret; + CdromFrontendId = -1; + ret = LoadState(data); + if (ret) + return false; + if (CdromFrontendId != -1 && CdromFrontendId != disk_current_index) + disk_set_image_index(CdromFrontendId); + return true; } /* cheats */ @@ -1143,7 +1154,6 @@ finish: static unsigned int disk_initial_index; static char disk_initial_path[PATH_MAX]; static bool disk_ejected; -static unsigned int disk_current_index; static unsigned int disk_count; static struct disks_state { diff --git a/libpcsxcore/misc.c b/libpcsxcore/misc.c index 9486d23b..526ebd43 100644 --- a/libpcsxcore/misc.c +++ b/libpcsxcore/misc.c @@ -35,6 +35,7 @@ char CdromId[10] = ""; char CdromLabel[33] = ""; +int CdromFrontendId; // for frontend use // PSX Executable types #define PSX_EXE 1 @@ -640,7 +641,21 @@ static const char PcsxHeader[32] = "STv4 PCSX v" PCSX_VERSION; // If you make changes to the savestate version, please increment the value below. static const u32 SaveVersion = 0x8b410006; +#define MISC_MAGIC 0x4353494d +struct misc_save_data { + u32 magic; + u32 gteBusyCycle; + u32 muldivBusyCycle; + u32 biuReg; + u32 biosBranchCheck; + u32 gpuIdleAfter; + u32 gpuSr; + u32 frame_counter; + int CdromFrontendId; +}; + int SaveState(const char *file) { + struct misc_save_data *misc = (void *)(psxH + 0xf000); void *f; GPUFreeze_t *gpufP = NULL; SPUFreezeHdr_t spufH; @@ -649,6 +664,19 @@ int SaveState(const char *file) { int result = -1; int Size; + assert(!psxRegs.branching); + assert(!psxRegs.cpuInRecursion); + assert(!misc->magic); + misc->magic = MISC_MAGIC; + misc->gteBusyCycle = psxRegs.gteBusyCycle; + misc->muldivBusyCycle = psxRegs.muldivBusyCycle; + misc->biuReg = psxRegs.biuReg; + misc->biosBranchCheck = psxRegs.biosBranchCheck; + misc->gpuIdleAfter = psxRegs.gpuIdleAfter; + misc->gpuSr = HW_GPU_STATUS; + misc->frame_counter = frame_counter; + misc->CdromFrontendId = CdromFrontendId; + f = SaveFuncs.open(file, "wb"); if (f == NULL) return -1; @@ -700,11 +728,13 @@ int SaveState(const char *file) { result = 0; cleanup: + memset(misc, 0, sizeof(*misc)); SaveFuncs.close(f); return result; } int LoadState(const char *file) { + struct misc_save_data *misc = (void *)(psxH + 0xf000); u32 biosBranchCheckOld = psxRegs.biosBranchCheck; void *f; GPUFreeze_t *gpufP = NULL; @@ -740,6 +770,16 @@ int LoadState(const char *file) { psxRegs.biosBranchCheck = ~0; psxRegs.gpuIdleAfter = psxRegs.cycle - 1; HW_GPU_STATUS &= SWAP32(~PSXGPU_nBUSY); + if (misc->magic == MISC_MAGIC) { + psxRegs.gteBusyCycle = misc->gteBusyCycle; + psxRegs.muldivBusyCycle = misc->muldivBusyCycle; + psxRegs.biuReg = misc->biuReg; + psxRegs.biosBranchCheck = misc->biosBranchCheck; + psxRegs.gpuIdleAfter = misc->gpuIdleAfter; + HW_GPU_STATUS = misc->gpuSr; + frame_counter = misc->frame_counter; + CdromFrontendId = misc->CdromFrontendId; + } psxCpu->Notify(R3000ACPU_NOTIFY_AFTER_LOAD, NULL); @@ -776,6 +816,7 @@ int LoadState(const char *file) { result = 0; cleanup: + memset(misc, 0, sizeof(*misc)); SaveFuncs.close(f); return result; } diff --git a/libpcsxcore/misc.h b/libpcsxcore/misc.h index c5eb327a..a1c36bb9 100644 --- a/libpcsxcore/misc.h +++ b/libpcsxcore/misc.h @@ -55,6 +55,7 @@ typedef struct { extern char CdromId[10]; extern char CdromLabel[33]; +extern int CdromFrontendId; // for frontend use void BiosBootBypass(); -- 2.39.5