Add support for mapping emulated RAM to address 0x0
authorPaul Cercueil <paul@crapouillou.net>
Wed, 11 May 2022 15:24:03 +0000 (16:24 +0100)
committerPaul Cercueil <paul@crapouillou.net>
Wed, 11 May 2022 18:03:50 +0000 (19:03 +0100)
This requires a few changes, since a pointer whose value is 0x0 will be
detected as a NULL pointer. The read/write LUTs are now initialized with
0xff, and all pointers are now checked against a new INVALID_PTR macro.

Mapping the emulated RAM to the address 0x0 will allow Lightrec to
generate much better code.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
libpcsxcore/cdrom.c
libpcsxcore/misc.c
libpcsxcore/psxbios.c
libpcsxcore/psxdma.c
libpcsxcore/psxinterpreter.c
libpcsxcore/psxmem.c
libpcsxcore/psxmem.h

index 1646d0e..6f4027c 100644 (file)
@@ -1491,7 +1491,7 @@ void psxDma3(u32 madr, u32 bcr, u32 chcr) {
 
 
                        ptr = (u8 *)PSXM(madr);
-                       if (ptr == NULL) {
+                       if (ptr == INVALID_PTR) {
                                CDR_LOG("psxDma3() Log: *** DMA 3 *** NULL Pointer!\n");
                                break;
                        }
index a07ab19..94fac61 100644 (file)
@@ -254,7 +254,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;
@@ -300,7 +300,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;
@@ -489,7 +489,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);
@@ -518,7 +518,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);
                                                        }
index 373057d..5a8f71b 100644 (file)
@@ -373,7 +373,7 @@ void psxBios_getc(void) // 0x03, 0x35
 #endif
        v0 = -1;
 
-       if (pa1) {
+       if (pa1 != INVALID_PTR) {
                switch (a0) {
                        case 2: buread(pa1, 1, 1); break;
                        case 3: buread(pa1, 2, 1); break;
@@ -392,7 +392,7 @@ void psxBios_putc(void) // 0x09, 0x3B
        PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x09]);
 #endif
        v0 = -1;
-       if (!pa1) {
+       if (pa1 == INVALID_PTR) {
                pc0 = ra;
                return;
        }
@@ -1261,7 +1261,7 @@ void psxBios_printf() { // 0x3f
        void *psp;
 
        psp = PSXM(sp);
-       if (psp) {
+       if (psp != INVALID_PTR) {
                memcpy(save, psp, 4 * 4);
                psxMu32ref(sp) = SWAP32((u32)a0);
                psxMu32ref(sp + 4) = SWAP32((u32)a1);
@@ -2112,7 +2112,7 @@ void psxBios_open() { // 0x32
 
        v0 = -1;
 
-       if (pa0) {
+       if (pa0 != INVALID_PTR) {
                if (!strncmp(pa0, "bu00", 4)) {
                        buopen(1, Mcd1Data, Config.Mcd1);
                }
@@ -2166,7 +2166,7 @@ void psxBios_read() { // 0x34
 
        v0 = -1;
 
-       if (pa1) {
+       if (pa1 != INVALID_PTR) {
                switch (a0) {
                        case 2: buread(pa1, 1, a2); break;
                        case 3: buread(pa1, 2, a2); break;
@@ -2189,7 +2189,7 @@ void psxBios_write() { // 0x35/0x03
 #endif
 
        v0 = -1;
-       if (!pa1) {
+       if (pa1 == INVALID_PTR) {
                pc0 = ra;
                return;
        }
@@ -2293,7 +2293,7 @@ void psxBios_firstfile() { // 42
 
        v0 = 0;
 
-       if (pa0) {
+       if (pa0 != INVALID_PTR) {
                strcpy(ffile, pa0);
                pfile = ffile+5;
                nfile = 0;
@@ -2371,7 +2371,7 @@ void psxBios_rename() { // 44
 
        v0 = 0;
 
-       if (pa0 && pa1) {
+       if (pa0 != INVALID_PTR && pa1 != INVALID_PTR) {
                if (!strncmp(pa0, "bu00", 4) && !strncmp(pa1, "bu00", 4)) {
                        burename(1);
                }
@@ -2413,7 +2413,7 @@ void psxBios_delete() { // 45
 
        v0 = 0;
 
-       if (pa0) {
+       if (pa0 != INVALID_PTR) {
                if (!strncmp(pa0, "bu00", 4)) {
                        budelete(1);
                }
@@ -2476,7 +2476,7 @@ void psxBios__card_write() { // 0x4e
        card_active_chan = a0;
        port = a0 >> 4;
 
-       if (pa2) {
+       if (pa2 != INVALID_PTR) {
                if (port == 0) {
                        memcpy(Mcd1Data + a1 * 128, pa2, 128);
                        SaveMcd(Config.Mcd1, Mcd1Data, a1 * 128, 128);
@@ -2512,7 +2512,7 @@ void psxBios__card_read() { // 0x4f
        card_active_chan = a0;
        port = a0 >> 4;
 
-       if (pa2) {
+       if (pa2 != INVALID_PTR) {
                if (port == 0) {
                        memcpy(pa2, Mcd1Data + a1 * 128, 128);
                } else {
index 03ee563..0480ce6 100644 (file)
@@ -45,7 +45,7 @@ void psxDma4(u32 madr, u32 bcr, u32 chcr) { // SPU
                        PSXDMA_LOG("*** DMA4 SPU - mem2spu *** %x addr = %x size = %x\n", chcr, madr, bcr);
 #endif
                        ptr = (u16 *)PSXM(madr);
-                       if (ptr == NULL) {
+                       if (ptr == INVALID_PTR) {
 #ifdef CPU_LOG
                                CPU_LOG("*** DMA4 SPU - mem2spu *** NULL Pointer!!!\n");
 #endif
@@ -62,7 +62,7 @@ void psxDma4(u32 madr, u32 bcr, u32 chcr) { // SPU
                        PSXDMA_LOG("*** DMA4 SPU - spu2mem *** %x addr = %x size = %x\n", chcr, madr, bcr);
 #endif
                        ptr = (u16 *)PSXM(madr);
-                       if (ptr == NULL) {
+                       if (ptr == INVALID_PTR) {
 #ifdef CPU_LOG
                                CPU_LOG("*** DMA4 SPU - spu2mem *** NULL Pointer!!!\n");
 #endif
@@ -138,7 +138,7 @@ void psxDma2(u32 madr, u32 bcr, u32 chcr) { // GPU
                        PSXDMA_LOG("*** DMA2 GPU - vram2mem *** %lx addr = %lx size = %lx\n", chcr, madr, bcr);
 #endif
                        ptr = (u32 *)PSXM(madr);
-                       if (ptr == NULL) {
+                       if (ptr == INVALID_PTR) {
 #ifdef CPU_LOG
                                CPU_LOG("*** DMA2 GPU - vram2mem *** NULL Pointer!!!\n");
 #endif
@@ -160,7 +160,7 @@ void psxDma2(u32 madr, u32 bcr, u32 chcr) { // GPU
                        PSXDMA_LOG("*** DMA 2 - GPU mem2vram *** %lx addr = %lx size = %lx\n", chcr, madr, bcr);
 #endif
                        ptr = (u32 *)PSXM(madr);
-                       if (ptr == NULL) {
+                       if (ptr == INVALID_PTR) {
 #ifdef CPU_LOG
                                CPU_LOG("*** DMA2 GPU - mem2vram *** NULL Pointer!!!\n");
 #endif
@@ -228,7 +228,7 @@ void psxDma6(u32 madr, u32 bcr, u32 chcr) {
 #endif
 
        if (chcr == 0x11000002) {
-               if (mem == NULL) {
+               if (mem == INVALID_PTR) {
 #ifdef CPU_LOG
                        CPU_LOG("*** DMA6 OT *** NULL Pointer!!!\n");
 #endif
index dd732b0..e6f1587 100644 (file)
@@ -60,7 +60,7 @@ void (*psxCP2BSC[32])();
 static u32 fetchNoCache(u32 pc)
 {
        u32 *code = (u32 *)PSXM(pc);
-       return ((code == NULL) ? 0 : SWAP32(*code));
+       return ((code == INVALID_PTR) ? 0 : SWAP32(*code));
 }
 
 /*
@@ -83,7 +83,7 @@ static u32 fetchICache(u32 pc)
                if (((entry->tag ^ pc) & 0xfffffff0) != 0 || pc < entry->tag)
                {
                        u32 *code = (u32 *)PSXM(pc & ~0x0f);
-                       if (!code)
+                       if (code == INVALID_PTR)
                                return 0;
 
                        entry->tag = pc;
index 6f85f82..ffa0dd7 100644 (file)
@@ -63,7 +63,7 @@ retry:
        if (psxMapHook != NULL) {
                ret = psxMapHook(addr, size, 0, tag);
                if (ret == NULL)
-                       return NULL;
+                       return MAP_FAILED;
        }
        else {
                /* avoid MAP_FIXED, it overrides existing mappings.. */
@@ -73,7 +73,7 @@ retry:
                req = (void *)addr;
                ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
                if (ret == MAP_FAILED)
-                       return NULL;
+                       return ret;
        }
 
        if (addr != 0 && ret != (void *)addr) {
@@ -82,7 +82,7 @@ retry:
 
                if (is_fixed) {
                        psxUnmap(ret, size, tag);
-                       return NULL;
+                       return MAP_FAILED;
                }
 
                if (((addr ^ (unsigned long)ret) & ~0xff000000l) && try_ < 2)
@@ -142,15 +142,10 @@ u8 **psxMemRLUT = NULL;
 int psxMemInit() {
        int i;
 
-       psxMemRLUT = (u8 **)malloc(0x10000 * sizeof(void *));
-       psxMemWLUT = (u8 **)malloc(0x10000 * sizeof(void *));
-       memset(psxMemRLUT, 0, 0x10000 * sizeof(void *));
-       memset(psxMemWLUT, 0, 0x10000 * sizeof(void *));
-
        psxM = psxMap(0x80000000, 0x00210000, 1, MAP_TAG_RAM);
-       if (psxM == NULL)
+       if (psxM == MAP_FAILED)
                psxM = psxMap(0x77000000, 0x00210000, 0, MAP_TAG_RAM);
-       if (psxM == NULL) {
+       if (psxM == MAP_FAILED) {
                SysMessage(_("mapping main RAM failed"));
                return -1;
        }
@@ -159,13 +154,24 @@ int psxMemInit() {
        psxH = psxMap(0x1f800000, 0x10000, 0, MAP_TAG_OTHER);
        psxR = psxMap(0x1fc00000, 0x80000, 0, MAP_TAG_OTHER);
 
-       if (psxMemRLUT == NULL || psxMemWLUT == NULL || 
-           psxR == NULL || psxP == NULL || psxH == NULL) {
+       if (psxR == MAP_FAILED || psxH == MAP_FAILED) {
                SysMessage(_("Error allocating memory!"));
                psxMemShutdown();
                return -1;
        }
 
+       psxMemRLUT = (u8 **)malloc(0x10000 * sizeof(void *));
+       psxMemWLUT = (u8 **)malloc(0x10000 * sizeof(void *));
+
+       if (psxMemRLUT == NULL || psxMemWLUT == NULL) {
+               SysMessage(_("Error allocating memory!"));
+               psxMemShutdown();
+               return -1;
+       }
+
+       memset(psxMemRLUT, 0xff, 0x10000 * sizeof(void *));
+       memset(psxMemWLUT, 0xff, 0x10000 * sizeof(void *));
+
 // MemR
        for (i = 0; i < 0x80; i++) psxMemRLUT[i + 0x0000] = (u8 *)&psxM[(i & 0x1f) << 16];
 
@@ -190,7 +196,7 @@ int psxMemInit() {
        // NOTE: Not sure if this is needed to fix any games but seems wise,
        //       seeing as some games do read from PIO as part of copy-protection
        //       check. (See fix in psxMemReset() regarding psxP region reads).
-       psxMemWLUT[0x1f00] = NULL;
+       psxMemWLUT[0x1f00] = INVALID_PTR;
        psxMemWLUT[0x1f80] = (u8 *)psxH;
 
        return 0;
@@ -244,7 +250,7 @@ u8 psxMemRead8(u32 mem) {
                        return psxHwRead8(mem);
        } else {
                p = (char *)(psxMemRLUT[t]);
-               if (p != NULL) {
+               if (p != INVALID_PTR) {
                        if (Config.Debug)
                                DebugCheckBP((mem & 0xffffff) | 0x80000000, R1);
                        return *(u8 *)(p + (mem & 0xffff));
@@ -269,7 +275,7 @@ u16 psxMemRead16(u32 mem) {
                        return psxHwRead16(mem);
        } else {
                p = (char *)(psxMemRLUT[t]);
-               if (p != NULL) {
+               if (p != INVALID_PTR) {
                        if (Config.Debug)
                                DebugCheckBP((mem & 0xffffff) | 0x80000000, R2);
                        return SWAPu16(*(u16 *)(p + (mem & 0xffff)));
@@ -294,7 +300,7 @@ u32 psxMemRead32(u32 mem) {
                        return psxHwRead32(mem);
        } else {
                p = (char *)(psxMemRLUT[t]);
-               if (p != NULL) {
+               if (p != INVALID_PTR) {
                        if (Config.Debug)
                                DebugCheckBP((mem & 0xffffff) | 0x80000000, R4);
                        return SWAPu32(*(u32 *)(p + (mem & 0xffff)));
@@ -319,7 +325,7 @@ void psxMemWrite8(u32 mem, u8 value) {
                        psxHwWrite8(mem, value);
        } else {
                p = (char *)(psxMemWLUT[t]);
-               if (p != NULL) {
+               if (p != INVALID_PTR) {
                        if (Config.Debug)
                                DebugCheckBP((mem & 0xffffff) | 0x80000000, W1);
                        *(u8 *)(p + (mem & 0xffff)) = value;
@@ -346,7 +352,7 @@ void psxMemWrite16(u32 mem, u16 value) {
                        psxHwWrite16(mem, value);
        } else {
                p = (char *)(psxMemWLUT[t]);
-               if (p != NULL) {
+               if (p != INVALID_PTR) {
                        if (Config.Debug)
                                DebugCheckBP((mem & 0xffffff) | 0x80000000, W2);
                        *(u16 *)(p + (mem & 0xffff)) = SWAPu16(value);
@@ -374,7 +380,7 @@ void psxMemWrite32(u32 mem, u32 value) {
                        psxHwWrite32(mem, value);
        } else {
                p = (char *)(psxMemWLUT[t]);
-               if (p != NULL) {
+               if (p != INVALID_PTR) {
                        if (Config.Debug)
                                DebugCheckBP((mem & 0xffffff) | 0x80000000, W4);
                        *(u32 *)(p + (mem & 0xffff)) = SWAPu32(value);
@@ -398,9 +404,9 @@ void psxMemWrite32(u32 mem, u32 value) {
                                        case 0x800: case 0x804:
                                                if (writeok == 0) break;
                                                writeok = 0;
-                                               memset(psxMemWLUT + 0x0000, 0, 0x80 * sizeof(void *));
-                                               memset(psxMemWLUT + 0x8000, 0, 0x80 * sizeof(void *));
-                                               memset(psxMemWLUT + 0xa000, 0, 0x80 * sizeof(void *));
+                                               memset(psxMemWLUT + 0x0000, 0xff, 0x80 * sizeof(void *));
+                                               memset(psxMemWLUT + 0x8000, 0xff, 0x80 * sizeof(void *));
+                                               memset(psxMemWLUT + 0xa000, 0xff, 0x80 * sizeof(void *));
                                                /* Required for icache interpreter otherwise Armored Core won't boot on icache interpreter */
                                                psxCpu->Notify(R3000ACPU_NOTIFY_CACHE_ISOLATED, NULL);
                                                break;
@@ -436,7 +442,7 @@ void *psxMemPointer(u32 mem) {
                        return NULL;
        } else {
                p = (char *)(psxMemWLUT[t]);
-               if (p != NULL) {
+               if (p != INVALID_PTR) {
                        return (void *)(p + (mem & 0xffff));
                }
                return NULL;
index fbf5f67..a69b4a3 100644 (file)
@@ -49,6 +49,8 @@ extern "C" {
 
 #endif
 
+#define INVALID_PTR ((void *)-1)
+
 extern s8 *psxM;
 #define psxMs8(mem)            psxM[(mem) & 0x1fffff]
 #define psxMs16(mem)   (SWAP16(*(s16 *)&psxM[(mem) & 0x1fffff]))
@@ -112,7 +114,7 @@ extern s8 *psxH;
 extern u8 **psxMemWLUT;
 extern u8 **psxMemRLUT;
 
-#define PSXM(mem)              (psxMemRLUT[(mem) >> 16] == 0 ? NULL : (u8*)(psxMemRLUT[(mem) >> 16] + ((mem) & 0xffff)))
+#define PSXM(mem)              (psxMemRLUT[(mem) >> 16] == INVALID_PTR ? INVALID_PTR : (u8*)(psxMemRLUT[(mem) >> 16] + ((mem) & 0xffff)))
 #define PSXMs8(mem)            (*(s8 *)PSXM(mem))
 #define PSXMs16(mem)   (SWAP16(*(s16 *)PSXM(mem)))
 #define PSXMs32(mem)   (SWAP32(*(s32 *)PSXM(mem)))