Skip to content

Commit

Permalink
save some things that weren't saved but should have been
Browse files Browse the repository at this point in the history
  • Loading branch information
notaz committed Oct 23, 2023
1 parent 9454d33 commit 6a131b0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
18 changes: 14 additions & 4 deletions frontend/libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -1143,7 +1154,6 @@ void retro_cheat_set(unsigned index, bool enabled, const char *code)
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
{
Expand Down
41 changes: 41 additions & 0 deletions libpcsxcore/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

char CdromId[10] = "";
char CdromLabel[33] = "";
int CdromFrontendId; // for frontend use

// PSX Executable types
#define PSX_EXE 1
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -776,6 +816,7 @@ int LoadState(const char *file) {

result = 0;
cleanup:
memset(misc, 0, sizeof(*misc));
SaveFuncs.close(f);
return result;
}
Expand Down
1 change: 1 addition & 0 deletions libpcsxcore/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ typedef struct {

extern char CdromId[10];
extern char CdromLabel[33];
extern int CdromFrontendId; // for frontend use

void BiosBootBypass();

Expand Down

0 comments on commit 6a131b0

Please sign in to comment.