psxbios: Merge upstream fix for Deliverevent in firstfile
[pcsx_rearmed.git] / libpcsxcore / psxbios.c
index 044a19a..010a229 100644 (file)
@@ -1610,7 +1610,6 @@ void psxBios_WaitEvent() { // 0a
 
        v0 = 0;
        pc0 = ra;
-       ResetIoCycle();
 }
 
 void psxBios_TestEvent() { // 0b
@@ -1668,8 +1667,20 @@ void psxBios_OpenTh() { // 0e
        int th;
 
        for (th=1; th<8; th++)
+       {
                if (Thread[th].status == 0) break;
 
+       }
+       if (th == 8) {
+               // Feb 2019 - Added out-of-bounds fix caught by cppcheck:
+               // When no free TCB is found, return 0xffffffff according to Nocash doc.
+#ifdef PSXBIOS_LOG
+               PSXBIOS_LOG("\t%s() WARNING! No Free TCBs found!\n", __func__);
+#endif
+               v0 = 0xffffffff;
+               pc0 = ra;
+               return;
+       }
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x0e], th);
 #endif
@@ -1761,9 +1772,10 @@ void psxBios_StopPAD() { // 14
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x14]);
 #endif
-
+       if (pad_buf == 0){
        pad_buf1 = NULL;
        pad_buf2 = NULL;
+       }
        pc0 = ra;
 }
 
@@ -2098,15 +2110,16 @@ void psxBios_firstfile() { // 42
                pfile = ffile+5;
                nfile = 1;
                if (!strncmp(pa0, "bu00", 4)) {
+                       // firstfile() calls _card_read() internally, so deliver it's event
+                       DeliverEvent(0x11, 0x2);
                        bufile(1);
                } else if (!strncmp(pa0, "bu10", 4)) {
+                       // firstfile() calls _card_read() internally, so deliver it's event
+                       DeliverEvent(0x11, 0x2);
                        bufile(2);
                }
        }
 
-       // firstfile() calls _card_read() internally, so deliver it's event
-       DeliverEvent(0x11, 0x2);
-
        pc0 = ra;
 }