psxbios: Add checks for bzero.
[pcsx_rearmed.git] / libpcsxcore / psxbios.c
index 4624bf4..c362a75 100644 (file)
@@ -317,6 +317,112 @@ static inline void LoadRegs() {
 //               System calls A0             */
 
 
+#define buread(Ra1, mcd, length) { \
+       SysPrintf("read %d: %x,%x (%s)\n", FDesc[1 + mcd].mcfile, FDesc[1 + mcd].offset, a2, Mcd##mcd##Data + 128 * FDesc[1 + mcd].mcfile + 0xa); \
+       ptr = Mcd##mcd##Data + 8192 * FDesc[1 + mcd].mcfile + FDesc[1 + mcd].offset; \
+       memcpy(Ra1, ptr, length); \
+       DeliverEvent(0x11, 0x2); /* 0xf0000011, 0x0004 */ \
+       DeliverEvent(0x81, 0x2); /* 0xf4000001, 0x0004 */ \
+       if (FDesc[1 + mcd].mode & 0x8000) v0 = 0; \
+       else v0 = length; \
+       FDesc[1 + mcd].offset += v0; \
+}
+
+#define buwrite(Ra1, mcd, length) { \
+       u32 offset =  + 8192 * FDesc[1 + mcd].mcfile + FDesc[1 + mcd].offset; \
+       SysPrintf("write %d: %x,%x\n", FDesc[1 + mcd].mcfile, FDesc[1 + mcd].offset, a2); \
+       ptr = Mcd##mcd##Data + offset; \
+       memcpy(ptr, Ra1, length); \
+       DeliverEvent(0x11, 0x2); /* 0xf0000011, 0x0004 */ \
+       DeliverEvent(0x81, 0x2); /* 0xf4000001, 0x0004 */ \
+       FDesc[1 + mcd].offset += length; \
+       if (FDesc[1 + mcd].mode & 0x8000) v0 = 0; \
+       else v0 = length; \
+}
+
+
+/* Internally redirects to "FileRead(fd,tempbuf,1)".*/
+/* For some strange reason, the returned character is sign-expanded; */
+/* So if a return value of FFFFFFFFh could mean either character FFh, or error. */
+/* TODO FIX ME : Properly implement this behaviour */
+void psxBios_getc(void) // 0x03, 0x35
+{
+       char *ptr;
+       void *pa1 = Ra1;
+#ifdef PSXBIOS_LOG
+       PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x03]);
+#endif
+       v0 = -1;
+
+       if (pa1) {
+               switch (a0) {
+                       case 2: buread(pa1, 1, 1); break;
+                       case 3: buread(pa1, 2, 1); break;
+               }
+       }
+
+       pc0 = ra;
+}
+
+/* Copy of psxBios_write, except size is 1. */
+void psxBios_putc(void) // 0x09, 0x3B
+{
+       char *ptr;
+       void *pa1 = Ra1;
+#ifdef PSXBIOS_LOG
+       PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x09]);
+#endif
+       v0 = -1;
+       if (!pa1) {
+               pc0 = ra;
+               return;
+       }
+
+       if (a0 == 1) { // stdout
+               char *ptr = (char *)pa1;
+
+               v0 = a2;
+               while (a2 > 0) {
+                       printf("%c", *ptr++); a2--;
+               }
+               pc0 = ra; return;
+       }
+
+       switch (a0) {
+               case 2: buwrite(pa1, 1, 1); break;
+               case 3: buwrite(pa1, 2, 1); break;
+       }
+
+       pc0 = ra;
+}
+
+void psxBios_todigit(void) // 0x0a
+{
+       int c = a0;
+#ifdef PSXBIOS_LOG
+       PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x0a]);
+#endif
+       c &= 0xFF;
+       if (c >= 0x30 && c < 0x3A) {
+               c -= 0x30;
+       }
+       else if (c > 0x60 && c < 0x7B) {
+               c -= 0x20;
+       }
+       else if (c > 0x40 && c < 0x5B) {
+               c = c - 0x41 + 10;
+       }
+       else if (c >= 0x80) {
+               c = -1;
+       }
+       else
+       {
+               c = 0x0098967F;
+       }
+       v0 = c;
+       pc0 = ra;
+}
+
 void psxBios_abs() { // 0x0e
        if ((s32)a0 < 0) v0 = -(s32)a0;
        else v0 = a0;
@@ -414,7 +520,12 @@ void psxBios_strncat() { // 0x16
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s: %s (%x), %s (%x), %d\n", biosA0n[0x16], Ra0, a0, Ra1, a1, a2);
 #endif
-
+       if (a0 == 0 || a1 == 0)
+       {
+               v0 = 0;
+               pc0 = ra;
+               return;
+       }
        while (*p1++);
        --p1;
        while ((*p1++ = *p2++) != '\0') {
@@ -499,7 +610,13 @@ void psxBios_strlen() { // 0x1b
 
 void psxBios_index() { // 0x1c
        char *p = (char *)Ra0;
-
+       if (a0 == 0)
+       {
+               v0 = 0;
+               pc0 = ra;
+               return;
+       }
+       
        do {
                if (*p == a1) {
                        v0 = a0 + (p - (char *)Ra0);
@@ -515,7 +632,11 @@ void psxBios_rindex() { // 0x1d
        char *p = (char *)Ra0;
 
        v0 = 0;
-
+       if (a0 == 0)
+       {
+               pc0 = ra;
+               return;
+       }
        do {
                if (*p == a1)
                        v0 = a0 + (p - (char *)Ra0);
@@ -618,15 +739,34 @@ void psxBios_tolower() { // 0x26
 
 void psxBios_bcopy() { // 0x27
        char *p1 = (char *)Ra1, *p2 = (char *)Ra0;
+       v0 = a0;
+       if (a0 == 0 || a2 > 0x7FFFFFFF)
+       {
+               pc0 = ra;
+               return;
+       }
        while ((s32)a2-- > 0) *p1++ = *p2++;
-
+       a2 = 0;
        pc0 = ra;
 }
 
 void psxBios_bzero() { // 0x28
        char *p = (char *)Ra0;
+       v0 = a0;
+       /* Same as memset here (See memset below) */
+       if (a1 > 0x7FFFFFFF || a1 == 0)
+       {
+               v0 = 0;
+               pc0 = ra;
+               return;
+       }
+       else if (a0 == 0)
+       {
+               pc0 = ra;
+               return;
+       }
        while ((s32)a1-- > 0) *p++ = '\0';
-
+       a1 = 0;
        pc0 = ra;
 }
 
@@ -926,9 +1066,24 @@ void psxBios_realloc() { // 0x38
 #endif
 
        a0 = block;
-       psxBios_free();
-       a0 = size;
-       psxBios_malloc();
+       /* If "old_buf" is zero, executes malloc(new_size), and returns r2=new_buf (or 0=failed). */
+       if (block == 0)
+       {
+               psxBios_malloc();
+       }
+       /* Else, if "new_size" is zero, executes free(old_buf), and returns r2=garbage. */
+       else if (size == 0)
+       {
+               psxBios_free();
+       }
+       /* Else, executes malloc(new_size), bcopy(old_buf,new_buf,new_size), and free(old_buf), and returns r2=new_buf (or 0=failed). */
+       /* Note that it is not quite implemented this way here. */
+       else
+       {
+               psxBios_free();
+               a0 = size;
+               psxBios_malloc();
+       }
 }
 
 
@@ -1191,7 +1346,7 @@ void psxBios_sys_a0_4c() { // 0x4c GPU relate
        GPU_writeData(0x0400000);
        GPU_writeData(0x0200000);
        GPU_writeData(0x0100000);
-
+       v0 = 0x1f801814;
        pc0 = ra;
 }
 
@@ -1279,11 +1434,29 @@ void psxBios__card_info() { // ab
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s: %x\n", biosA0n[0xab], a0);
 #endif
-
+       u32 ret;
        card_active_chan = a0;
 
+       switch (card_active_chan) 
+       {
+       case 0x00: case 0x01: case 0x02: case 0x03:
+               ret = Config.Mcd1[0] ? 0x2 : 0x8;
+               break;
+       case 0x10: case 0x11: case 0x12: case 0x13:
+               ret = Config.Mcd2[0] ? 0x2 : 0x8;
+               break;
+       default:
+#ifdef PSXBIOS_LOG
+               PSXBIOS_LOG("psxBios_%s: UNKNOWN PORT 0x%x\n", biosA0n[0xab], card_active_chan);
+#endif
+               ret = 0x11;
+               break;
+       }
+       
+//     DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
 //     DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
        DeliverEvent(0x81, 0x2); // 0xf4000001, 0x0004
+       DeliverEvent(0x81, ret); // 0xf4000001, 0x0004
 
        v0 = 1; pc0 = ra;
 }
@@ -1445,14 +1618,27 @@ void psxBios_WaitEvent() { // 0a
 
        ev   = a0 & 0xff;
        spec = (a0 >> 8) & 0xff;
-
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s %x,%x\n", biosB0n[0x0a], ev, spec);
 #endif
+       if (Event[ev][spec].status == EvStUNUSED)
+       {
+               v0 = 0;
+               pc0 = ra;       
+               return;
+       }
 
-       Event[ev][spec].status = EvStACTIVE;
+       if (Event[ev][spec].status == EvStALREADY) 
+       {
+               /* Callback events (mode=EvMdINTR) do never set the ready flag (and thus WaitEvent would hang forever). */
+               if (!(Event[ev][spec].mode == EvMdINTR)) Event[ev][spec].status = EvStACTIVE;
+               v0 = 1;
+               pc0 = ra;
+               return;
+       }
 
-       v0 = 1; pc0 = ra;
+       v0 = 0;
+       pc0 = ra;
 }
 
 void psxBios_TestEvent() { // 0b
@@ -1510,8 +1696,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
@@ -1603,9 +1801,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;
 }
 
@@ -1613,10 +1812,17 @@ void psxBios_PAD_init() { // 15
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x15]);
 #endif
+       if (!(a0 == 0x20000000 || a0 == 0x20000001))
+       {
+               v0 = 0;
+               pc0 = ra;
+               return;
+       }
        psxHwWrite16(0x1f801074, (u16)(psxHwRead16(0x1f801074) | 0x1));
        pad_buf = (int *)Ra1;
        *pad_buf = -1;
        psxRegs.CP0.n.Status |= 0x401;
+       v0 = 2;
        pc0 = ra;
 }
 
@@ -1674,43 +1880,70 @@ void psxBios_UnDeliverEvent() { // 0x20
        pc0 = ra;
 }
 
-#define buopen(mcd) { \
-       strcpy(FDesc[1 + mcd].name, Ra0+5); \
-       FDesc[1 + mcd].offset = 0; \
-       FDesc[1 + mcd].mode   = a1; \
- \
-       for (i=1; i<16; i++) { \
-               ptr = Mcd##mcd##Data + 128 * i; \
-               if ((*ptr & 0xF0) != 0x50) continue; \
-               if (strcmp(FDesc[1 + mcd].name, ptr+0xa)) continue; \
-               FDesc[1 + mcd].mcfile = i; \
-               SysPrintf("open %s\n", ptr+0xa); \
-               v0 = 1 + mcd; \
-               break; \
-       } \
-       if (a1 & 0x200 && v0 == -1) { /* FCREAT */ \
-               for (i=1; i<16; i++) { \
-                       int j, xor = 0; \
- \
-                       ptr = Mcd##mcd##Data + 128 * i; \
-                       if ((*ptr & 0xF0) == 0x50) continue; \
-                       ptr[0] = 0x50 | (u8)(a1 >> 16); \
-                       ptr[4] = 0x00; \
-                       ptr[5] = 0x20; \
-                       ptr[6] = 0x00; \
-                       ptr[7] = 0x00; \
-                       ptr[8] = 'B'; \
-                       ptr[9] = 'I'; \
-                       strcpy(ptr+0xa, FDesc[1 + mcd].name); \
-                       for (j=0; j<127; j++) xor^= ptr[j]; \
-                       ptr[127] = xor; \
-                       FDesc[1 + mcd].mcfile = i; \
-                       SysPrintf("openC %s\n", ptr); \
-                       v0 = 1 + mcd; \
-                       SaveMcd(Config.Mcd##mcd, Mcd##mcd##Data, 128 * i, 128); \
-                       break; \
-               } \
-       } \
+char ffile[64], *pfile;
+int nfile;
+static void buopen(int mcd, u8 *ptr, u8 *cfg)
+{
+       int i;
+       u8 *fptr = ptr;
+
+       strcpy(FDesc[1 + mcd].name, Ra0+5);
+       FDesc[1 + mcd].offset = 0;
+       FDesc[1 + mcd].mode   = a1;
+
+       for (i=1; i<16; i++) {
+               fptr += 128;
+               if ((*fptr & 0xF0) != 0x50) continue;
+               if (strcmp(FDesc[1 + mcd].name, fptr+0xa)) continue;
+               FDesc[1 + mcd].mcfile = i;
+               SysPrintf("open %s\n", fptr+0xa);
+               v0 = 1 + mcd;
+               break;
+       }
+       if (a1 & 0x200 && v0 == -1) { /* FCREAT */
+               fptr = ptr;
+               for (i=1; i<16; i++) {
+                       int j, xor, nblk = a1 >> 16;
+                       u8 *pptr, *fptr2;
+
+                       fptr += 128;
+                       if ((*fptr & 0xF0) != 0xa0) continue;
+
+                       FDesc[1 + mcd].mcfile = i;
+                       fptr[0] = 0x51;
+                       fptr[4] = 0x00;
+                       fptr[5] = 0x20 * nblk;
+                       fptr[6] = 0x00;
+                       fptr[7] = 0x00;
+                       strcpy(fptr+0xa, FDesc[1 + mcd].name);
+                       pptr = fptr2 = fptr;
+                       for(j=2; j<=nblk; j++) {
+                               int k;
+                               for(i++; i<16; i++) {
+                                       fptr2 += 128;
+                                       
+                                       memset(fptr2, 0, 128);
+                                       fptr2[0] = j < nblk ? 0x52 : 0x53;
+                                       pptr[8] = i - 1;
+                                       pptr[9] = 0;
+                                       for (k=0, xor=0; k<127; k++) xor^= pptr[k];
+                                       pptr[127] = xor;
+                                       pptr = fptr2;
+                                       break;
+                               }
+                               /* shouldn't this return ENOSPC if i == 16? */
+                       }
+                       pptr[8] = pptr[9] = 0xff;
+                       for (j=0, xor=0; j<127; j++) xor^= pptr[j];
+                       pptr[127] = xor;
+                       SysPrintf("openC %s %d\n", ptr, nblk);
+                       v0 = 1 + mcd;
+                       /* just go ahead and resave them all */
+                       SaveMcd(cfg, ptr, 128, 128 * 15);
+                       break;
+               }
+               /* shouldn't this return ENOSPC if i == 16? */
+       }
 }
 
 /*
@@ -1730,11 +1963,11 @@ void psxBios_open() { // 0x32
 
        if (pa0) {
                if (!strncmp(pa0, "bu00", 4)) {
-                       buopen(1);
+                       buopen(1, Mcd1Data, Config.Mcd1);
                }
 
                if (!strncmp(pa0, "bu10", 4)) {
-                       buopen(2);
+                       buopen(2, Mcd2Data, Config.Mcd2);
                }
        }
 
@@ -1767,16 +2000,6 @@ void psxBios_lseek() { // 0x33
        pc0 = ra;
 }
 
-#define buread(Ra1, mcd) { \
-       SysPrintf("read %d: %x,%x (%s)\n", FDesc[1 + mcd].mcfile, FDesc[1 + mcd].offset, a2, Mcd##mcd##Data + 128 * FDesc[1 + mcd].mcfile + 0xa); \
-       ptr = Mcd##mcd##Data + 8192 * FDesc[1 + mcd].mcfile + FDesc[1 + mcd].offset; \
-       memcpy(Ra1, ptr, a2); \
-       if (FDesc[1 + mcd].mode & 0x8000) v0 = 0; \
-       else v0 = a2; \
-       FDesc[1 + mcd].offset += v0; \
-       DeliverEvent(0x11, 0x2); /* 0xf0000011, 0x0004 */ \
-       DeliverEvent(0x81, 0x2); /* 0xf4000001, 0x0004 */ \
-}
 
 /*
  *     int read(int fd , void *buf , int nbytes);
@@ -1794,27 +2017,14 @@ void psxBios_read() { // 0x34
 
        if (pa1) {
                switch (a0) {
-                       case 2: buread(pa1, 1); break;
-                       case 3: buread(pa1, 2); break;
+                       case 2: buread(pa1, 1, a2); break;
+                       case 3: buread(pa1, 2, a2); break;
                }
        }
                
        pc0 = ra;
 }
 
-#define buwrite(Ra1, mcd) { \
-       u32 offset =  + 8192 * FDesc[1 + mcd].mcfile + FDesc[1 + mcd].offset; \
-       SysPrintf("write %d: %x,%x\n", FDesc[1 + mcd].mcfile, FDesc[1 + mcd].offset, a2); \
-       ptr = Mcd##mcd##Data + offset; \
-       memcpy(ptr, Ra1, a2); \
-       FDesc[1 + mcd].offset += a2; \
-       SaveMcd(Config.Mcd##mcd, Mcd##mcd##Data, offset, a2); \
-       if (FDesc[1 + mcd].mode & 0x8000) v0 = 0; \
-       else v0 = a2; \
-       DeliverEvent(0x11, 0x2); /* 0xf0000011, 0x0004 */ \
-       DeliverEvent(0x81, 0x2); /* 0xf4000001, 0x0004 */ \
-}
-
 /*
  *     int write(int fd , void *buf , int nbytes);
  */
@@ -1844,8 +2054,8 @@ void psxBios_write() { // 0x35/0x03
        }
 
        switch (a0) {
-               case 2: buwrite(pa1, 1); break;
-               case 3: buwrite(pa1, 2); break;
+               case 2: buwrite(pa1, 1, a2); break;
+               case 3: buwrite(pa1, 2, a2); break;
        }
 
        pc0 = ra;
@@ -1881,17 +2091,18 @@ int nfile;
        while (nfile < 16) { \
                int match=1; \
  \
-               ptr = Mcd##mcd##Data + 128 * nfile; \
+               ptr = Mcd##mcd##Data + 128 * (nfile + 1); \
                nfile++; \
                if ((*ptr & 0xF0) != 0x50) continue; \
+               /* Bug link files show up as free block. */ \
+               if (!ptr[0xa]) continue; \
                ptr+= 0xa; \
                if (pfile[0] == 0) { \
                        strncpy(dir->name, ptr, sizeof(dir->name)); \
                        dir->name[sizeof(dir->name) - 1] = '\0'; \
                } else for (i=0; i<20; i++) { \
                        if (pfile[i] == ptr[i]) { \
-                               dir->name[i] = ptr[i]; \
-                               if (ptr[i] == 0) break; else continue; } \
+                                                               dir->name[i] = ptr[i]; continue; } \
                        if (pfile[i] == '?') { \
                                dir->name[i] = ptr[i]; continue; } \
                        if (pfile[i] == '*') { \
@@ -1899,7 +2110,7 @@ int nfile;
                        match = 0; break; \
                } \
                SysPrintf("%d : %s = %s + %s (match=%d)\n", nfile, dir->name, pfile, ptr, match); \
-               if (match == 0) continue; \
+               if (match == 0) { continue; } \
                dir->size = 8192; \
                v0 = _dir; \
                break; \
@@ -1928,15 +2139,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;
 }
 
@@ -2092,7 +2304,13 @@ 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))
+       {
+               /* Invalid sectors */
+               v0 = 0; pc0 = ra;
+               return;
+       }
        card_active_chan = a0;
        port = a0 >> 4;
 
@@ -2119,7 +2337,13 @@ 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))
+       {
+               /* Invalid sectors */
+               v0 = 0; pc0 = ra;
+               return;
+       }
        card_active_chan = a0;
        port = a0 >> 4;
 
@@ -2312,9 +2536,9 @@ void psxBiosInit() {
        //biosA0[0x05] = psxBios_ioctl;
        //biosA0[0x06] = psxBios_exit;
        //biosA0[0x07] = psxBios_sys_a0_07;
-       //biosA0[0x08] = psxBios_getc;
-       //biosA0[0x09] = psxBios_putc;
-       //biosA0[0x0a] = psxBios_todigit;
+       biosA0[0x08] = psxBios_getc;
+       biosA0[0x09] = psxBios_putc;
+       biosA0[0x0a] = psxBios_todigit;
        //biosA0[0x0b] = psxBios_atof;
        //biosA0[0x0c] = psxBios_strtoul;
        //biosA0[0x0d] = psxBios_strtol;
@@ -2841,8 +3065,9 @@ void psxBiosException() {
 #endif
                        switch (a0) {
                                case 1: // EnterCritical - disable irq's
-                                       psxRegs.CP0.n.Status &= ~0x404; 
-v0=1;  // HDHOSHY experimental patch: Spongebob, Coldblood, fearEffect, Medievil2, Martian Gothic
+                                       /* Fixes Medievil 2 not loading up new game, Digimon World not booting up and possibly others */
+                                       v0 = (psxRegs.CP0.n.Status & 0x404) == 0x404;
+                                       psxRegs.CP0.n.Status &= ~0x404;
                                        break;
 
                                case 2: // ExitCritical - enable irq's