overwrite dynarec related code with upstream version
[pcsx_rearmed.git] / libpcsxcore / misc.c
index 82e6b8b..be501a2 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 <zlib.h>
 
 char CdromId[10] = "";
@@ -243,6 +245,7 @@ int LoadCdrom() {
        tmpHead.t_addr = SWAP32(tmpHead.t_addr);
 
        psxCpu->Clear(tmpHead.t_addr, tmpHead.t_size / 4);
+       psxCpu->Reset();
 
        // Read the rest of the main executable
        while (tmpHead.t_size & ~2047) {
@@ -290,6 +293,7 @@ int LoadCdromFile(const char *filename, EXE_HEADER *head) {
        addr = head->t_addr;
 
        psxCpu->Clear(addr, size / 4);
+       psxCpu->Reset();
 
        while (size & ~2047) {
                incTime();
@@ -386,7 +390,13 @@ 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
        }
@@ -397,6 +407,8 @@ int CheckCdrom() {
        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();
 
@@ -411,7 +423,9 @@ static int PSXGetFileType(FILE *f) {
 
        current = ftell(f);
        fseek(f, 0L, SEEK_SET);
-       fread(mybuf, 2048, 1, f);
+       if (fread(&mybuf, sizeof(mybuf), 1, f) != sizeof(mybuf))
+               goto io_fail;
+       
        fseek(f, current, SEEK_SET);
 
        exe_hdr = (EXE_HEADER *)mybuf;
@@ -426,6 +440,12 @@ static int PSXGetFileType(FILE *f) {
                return COFF_EXE;
 
        return INVALID_EXE;
+
+io_fail:
+#ifndef NDEBUG
+       SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
+#endif
+       return INVALID_EXE;
 }
 
 // temporary pandora workaround..
@@ -464,7 +484,8 @@ int Load(const char *ExePath) {
                type = PSXGetFileType(tmpFile);
                switch (type) {
                        case PSX_EXE:
-                               fread(&tmpHead,sizeof(EXE_HEADER),1,tmpFile);
+                               if (fread(&tmpHead, sizeof(EXE_HEADER), 1, tmpFile) != sizeof(EXE_HEADER))
+                                       goto fail_io;
                                section_address = SWAP32(tmpHead.t_addr);
                                section_size = SWAP32(tmpHead.t_size);
                                mem = PSXM(section_address);
@@ -473,7 +494,6 @@ int Load(const char *ExePath) {
                                        fread_to_ram(mem, section_size, 1, tmpFile);
                                        psxCpu->Clear(section_address, section_size / 4);
                                }
-                               fclose(tmpFile);
                                psxRegs.pc = SWAP32(tmpHead.pc0);
                                psxRegs.GPR.n.gp = SWAP32(tmpHead.gp0);
                                psxRegs.GPR.n.sp = SWAP32(tmpHead.s_addr);
@@ -484,11 +504,14 @@ int Load(const char *ExePath) {
                        case CPE_EXE:
                                fseek(tmpFile, 6, SEEK_SET); /* Something tells me we should go to 4 and read the "08 00" here... */
                                do {
-                                       fread(&opcode, 1, 1, tmpFile);
+                                       if (fread(&opcode, sizeof(opcode), 1, tmpFile) != sizeof(opcode))
+                                               goto fail_io;
                                        switch (opcode) {
                                                case 1: /* Section loading */
-                                                       fread(&section_address, 4, 1, tmpFile);
-                                                       fread(&section_size, 4, 1, tmpFile);
+                                                       if (fread(&section_address, sizeof(section_address), 1, tmpFile) != sizeof(section_address))
+                                                               goto fail_io;
+                                                       if (fread(&section_size, sizeof(section_size), 1, tmpFile) != sizeof(section_size))
+                                                               goto fail_io;
                                                        section_address = SWAPu32(section_address);
                                                        section_size = SWAPu32(section_size);
 #ifdef EMU_LOG
@@ -502,7 +525,8 @@ int Load(const char *ExePath) {
                                                        break;
                                                case 3: /* register loading (PC only?) */
                                                        fseek(tmpFile, 2, SEEK_CUR); /* unknown field */
-                                                       fread(&psxRegs.pc, 4, 1, tmpFile);
+                                                       if (fread(&psxRegs.pc, sizeof(psxRegs.pc), 1, tmpFile) != sizeof(psxRegs.pc))
+                                                               goto fail_io;
                                                        psxRegs.pc = SWAPu32(psxRegs.pc);
                                                        break;
                                                case 0: /* End of file */
@@ -531,7 +555,15 @@ int Load(const char *ExePath) {
                CdromLabel[0] = '\0';
        }
 
+       fclose(tmpFile);
        return retval;
+
+fail_io:
+#ifndef NDEBUG
+       SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
+#endif
+       fclose(tmpFile);
+       return -1;
 }
 
 // STATES
@@ -599,7 +631,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));
@@ -657,13 +690,19 @@ 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();
        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 (Config.HLE)
                psxBiosFreeze(0);
@@ -750,7 +789,7 @@ int RecvPcsxInfo() {
        NET_recvData(&Config.Cpu, sizeof(Config.Cpu), PSE_NET_BLOCKING);
        if (tmp != Config.Cpu) {
                psxCpu->Shutdown();
-#ifdef PSXREC
+#ifndef DRC_DISABLE
                if (Config.Cpu == CPU_INTERPRETER) psxCpu = &psxInt;
                else psxCpu = &psxRec;
 #else