lightrec: try to clean up intergarion, fix -O0
[pcsx_rearmed.git] / libpcsxcore / misc.c
index 8911bac..fba1112 100644 (file)
 * Miscellaneous functions, including savestates and CD-ROM loading.
 */
 
+#include <stddef.h>
 #include "misc.h"
 #include "cdrom.h"
 #include "mdec.h"
 #include "gpu.h"
 #include "ppf.h"
+#include "database.h"
+#include "lightrec/plugin.h"
 #include <zlib.h>
 
 char CdromId[10] = "";
@@ -252,7 +255,7 @@ int LoadCdrom() {
                incTime();
                READTRACK();
 
-               if (ptr != NULL) memcpy(ptr, buf+12, 2048);
+               if (ptr != INVALID_PTR) memcpy(ptr, buf+12, 2048);
 
                tmpHead.t_size -= 2048;
                tmpHead.t_addr += 2048;
@@ -298,7 +301,7 @@ int LoadCdromFile(const char *filename, EXE_HEADER *head) {
                READTRACK();
 
                mem = PSXM(addr);
-               if (mem)
+               if (mem != INVALID_PTR)
                        memcpy(mem, buf + 12, 2048);
 
                size -= 2048;
@@ -388,17 +391,25 @@ int CheckCdrom() {
                strcpy(CdromId, "SLUS99999");
 
        if (Config.PsxAuto) { // autodetect system (pal or ntsc)
-               if (CdromId[2] == 'e' || CdromId[2] == 'E')
+               if (
+                       /* Make sure Wild Arms SCUS-94608 is not detected as a PAL game. */
+                       ((CdromId[0] == 's' || CdromId[0] == 'S') && (CdromId[2] == 'e' || CdromId[2] == 'E')) ||
+                       !strncmp(CdromId, "DTLS3035", 8) ||
+                       !strncmp(CdromId, "PBPX95001", 9) || // according to redump.org, these PAL
+                       !strncmp(CdromId, "PBPX95007", 9) || // discs have a non-standard ID;
+                       !strncmp(CdromId, "PBPX95008", 9))   // add more serials if they are discovered.
                        Config.PsxType = PSX_TYPE_PAL; // pal
                else Config.PsxType = PSX_TYPE_NTSC; // ntsc
        }
 
        if (CdromLabel[0] == ' ') {
-               memcpy(CdromLabel, CdromId, 9);
+               strncpy(CdromLabel, CdromId, 9);
        }
        SysPrintf(_("CD-ROM Label: %.32s\n"), CdromLabel);
        SysPrintf(_("CD-ROM ID: %.9s\n"), CdromId);
        SysPrintf(_("CD-ROM EXE Name: %.255s\n"), exename);
+       
+       Apply_Hacks_Cdrom();
 
        BuildPPFCache();
 
@@ -479,7 +490,7 @@ int Load(const char *ExePath) {
                                section_address = SWAP32(tmpHead.t_addr);
                                section_size = SWAP32(tmpHead.t_size);
                                mem = PSXM(section_address);
-                               if (mem != NULL) {
+                               if (mem != INVALID_PTR) {
                                        fseek(tmpFile, 0x800, SEEK_SET);
                                        fread_to_ram(mem, section_size, 1, tmpFile);
                                        psxCpu->Clear(section_address, section_size / 4);
@@ -508,7 +519,7 @@ int Load(const char *ExePath) {
                                                        EMU_LOG("Loading %08X bytes from %08X to %08X\n", section_size, ftell(tmpFile), section_address);
 #endif
                                                        mem = PSXM(section_address);
-                                                       if (mem != NULL) {
+                                                       if (mem != INVALID_PTR) {
                                                                fread_to_ram(mem, section_size, 1, tmpFile);
                                                                psxCpu->Clear(section_address, section_size / 4);
                                                        }
@@ -605,6 +616,9 @@ int SaveState(const char *file) {
 
        new_dyna_before_save();
 
+       if (drc_is_lightrec() && Config.Cpu != CPU_INTERPRETER)
+               lightrec_plugin_prepare_save_state();
+
        SaveFuncs.write(f, (void *)PcsxHeader, 32);
        SaveFuncs.write(f, (void *)&SaveVersion, sizeof(u32));
        SaveFuncs.write(f, (void *)&Config.HLE, sizeof(boolean));
@@ -621,7 +635,8 @@ int SaveState(const char *file) {
        SaveFuncs.write(f, psxM, 0x00200000);
        SaveFuncs.write(f, psxR, 0x00080000);
        SaveFuncs.write(f, psxH, 0x00010000);
-       SaveFuncs.write(f, (void *)&psxRegs, sizeof(psxRegs));
+       // only partial save of psxRegisters to maintain savestate compat
+       SaveFuncs.write(f, &psxRegs, offsetof(psxRegisters, gteBusyCycle));
 
        // gpu
        gpufP = (GPUFreeze_t *)malloc(sizeof(GPUFreeze_t));
@@ -679,18 +694,18 @@ int LoadState(const char *file) {
        if (Config.HLE)
                psxBiosInit();
 
-#if defined(LIGHTREC)
-       if (Config.Cpu != CPU_INTERPRETER)
-               psxCpu->Clear(0, UINT32_MAX); //clear all
-       else
-#endif
-       psxCpu->Reset();
+       if (!drc_is_lightrec() || Config.Cpu == CPU_INTERPRETER)
+               psxCpu->Reset();
        SaveFuncs.seek(f, 128 * 96 * 3, SEEK_CUR);
 
        SaveFuncs.read(f, psxM, 0x00200000);
        SaveFuncs.read(f, psxR, 0x00080000);
        SaveFuncs.read(f, psxH, 0x00010000);
-       SaveFuncs.read(f, (void *)&psxRegs, sizeof(psxRegs));
+       SaveFuncs.read(f, &psxRegs, offsetof(psxRegisters, gteBusyCycle));
+       psxRegs.gteBusyCycle = psxRegs.cycle;
+
+       if (drc_is_lightrec() && Config.Cpu != CPU_INTERPRETER)
+               lightrec_plugin_prepare_load_state();
 
        if (Config.HLE)
                psxBiosFreeze(0);
@@ -777,7 +792,7 @@ int RecvPcsxInfo() {
        NET_recvData(&Config.Cpu, sizeof(Config.Cpu), PSE_NET_BLOCKING);
        if (tmp != Config.Cpu) {
                psxCpu->Shutdown();
-#if defined(NEW_DYNAREC) || defined(LIGHTREC)
+#ifndef DRC_DISABLE
                if (Config.Cpu == CPU_INTERPRETER) psxCpu = &psxInt;
                else psxCpu = &psxRec;
 #else