Fix corrupted memory card saves when using HLE
[pcsx_rearmed.git] / libpcsxcore / psxbios.c
index 3247402..ecd4264 100644 (file)
@@ -346,6 +346,7 @@ static inline void LoadRegs() {
        ptr = Mcd##mcd##Data + offset; \
        memcpy(ptr, Ra1, length); \
        FDesc[1 + mcd].offset += length; \
+       SaveMcd(Config.Mcd##mcd, Mcd##mcd##Data, offset, length); \
        if (FDesc[1 + mcd].mode & 0x8000) { \
        DeliverEvent(0x11, 0x2); /* 0xf0000011, 0x0004 */ \
        DeliverEvent(0x81, 0x2); /* 0xf4000001, 0x0004 */ \
@@ -1593,11 +1594,8 @@ void psxBios__card_info() { // ab
                break;
        }
        
-//     DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
-//     DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
-       DeliverEvent(0x81, 0x2); // 0xf4000001, 0x0004
+       DeliverEvent(0x11, 0x2); // 0xf4000001, 0x0004
        DeliverEvent(0x81, ret); // 0xf4000001, 0x0004
-
        v0 = 1; pc0 = ra;
 }
 
@@ -1878,12 +1876,10 @@ void psxBios_CloseTh() { // 0f
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x0f], th);
 #endif
-
-       if (Thread[th].status == 0) {
-               v0 = 0;
-       } else {
+       /* The return value is always 1 (even if the handle was already closed). */
+       v0 = 1;
+       if (Thread[th].status != 0) {
                Thread[th].status = 0;
-               v0 = 1;
        }
 
        pc0 = ra;
@@ -1899,14 +1895,11 @@ void psxBios_ChangeTh() { // 10
 #ifdef PSXBIOS_LOG
 //     PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x10], th);
 #endif
-
+       /* The return value is always 1. */
+       v0 = 1;
        if (Thread[th].status == 0 || CurThread == th) {
-               v0 = 0;
-
                pc0 = ra;
        } else {
-               v0 = 1;
-
                if (Thread[CurThread].status == 2) {
                        Thread[CurThread].status = 1;
                        Thread[CurThread].func = ra;
@@ -2458,8 +2451,11 @@ void psxBios__card_write() { // 0x4e
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s: %x,%x,%x\n", biosB0n[0x4e], a0, a1, a2);
 #endif
-       /* Function also accepts sector 400h (a bug) */
-       if (!(a1 <= 0x400))
+       /*
+       Function also accepts sector 400h (a bug).
+       But notaz said we shouldn't allow sector 400h because it can corrupt the emulator.
+       */
+       if (!(a1 <= 0x3FF))
        {
                /* Invalid sectors */
                v0 = 0; pc0 = ra;
@@ -2491,8 +2487,11 @@ void psxBios__card_read() { // 0x4f
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x4f]);
 #endif
-       /* Function also accepts sector 400h (a bug) */
-       if (!(a1 <= 0x400))
+       /*
+       Function also accepts sector 400h (a bug).
+       But notaz said we shouldn't allow sector 400h because it can corrupt the emulator.
+       */
+       if (!(a1 <= 0x3FF))
        {
                /* Invalid sectors */
                v0 = 0; pc0 = ra;
@@ -3089,18 +3088,19 @@ void psxBiosInit() {
        
        /*      Some games like R-Types, CTR, Fade to Black read from adress 0x00000000 due to uninitialized pointers.
                See Garbage Area at Address 00000000h in Nocash PSX Specfications for more information.
-               R-type will work if PsxM[0] is non-zero. (Meaning that it should not be 00000003h as he implies).
-               Crash Team Racing will refuse to boot if psxM[2] and psx[3] are not set to 0.
-               Fade to Black can crash upon memory card access if byte 5 is set to the wrong value.
+               Here are some examples of games not working with this fix in place :
+               R-type won't get past the Irem logo if not implemented.
+               Crash Team Racing will softlock after the Sony logo.
+       */
+       
+       psxMu32ref(0x0000) = SWAPu32(0x00000003);
+       /*
+       But overwritten by 00000003h after soon.
+       psxMu32ref(0x0000) = SWAPu32(0x00001A3C);
        */
-       psxM[0] = SWAPu32(0x3C);
-       psxM[1] = SWAPu32(0x1A);
-       psxM[2] = SWAPu32(0x00);
-       psxM[3] = SWAPu32(0x00);
-       psxM[4] = SWAPu32(0x27);
-       psxM[5] = SWAPu32(0x5A);
-       psxM[6] = SWAPu32(0x0C);
-       psxM[7] = SWAPu32(0x80);
+       psxMu32ref(0x0004) = SWAPu32(0x800C5A27);
+       psxMu32ref(0x0008) = SWAPu32(0x08000403);
+       psxMu32ref(0x000C) = SWAPu32(0x00000000);
 }
 
 void psxBiosShutdown() {