X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=pcsx_rearmed.git;a=blobdiff_plain;f=libpcsxcore%2Fpsxmem.c;h=7001744b54d9069eef194207f923d9ce7df0a3bc;hp=27663f1fd685bc4fbea50bc24756ad5d9bc19685;hb=650adfd2da779ba8855623362c2900583e22931e;hpb=8ad120c9c4dca424feac32098cb4af6a2c8f641f diff --git a/libpcsxcore/psxmem.c b/libpcsxcore/psxmem.c index 27663f1f..7001744b 100644 --- a/libpcsxcore/psxmem.c +++ b/libpcsxcore/psxmem.c @@ -42,22 +42,48 @@ void *psxMap(unsigned long addr, size_t size, int is_fixed, enum psxMapTag tag) { int flags = MAP_PRIVATE | MAP_ANONYMOUS; + int tried_to_align = 0; + unsigned long mask; void *req, *ret; - if (psxMapHook != NULL) - return psxMapHook(addr, size, is_fixed, tag); +retry: + if (psxMapHook != NULL) { + ret = psxMapHook(addr, size, is_fixed, tag); + goto out; + } - if (is_fixed) - flags |= MAP_FIXED; + /* avoid MAP_FIXED, it overrides existing mappings.. */ + /* if (is_fixed) + flags |= MAP_FIXED; */ req = (void *)addr; ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0); if (ret == MAP_FAILED) return NULL; - if (req != NULL && ret != req) - SysMessage("psxMap: warning: wanted to map @%p, got %p\n", - req, ret); +out: + if (addr != 0 && ret != (void *)addr) { + SysMessage("psxMap: warning: wanted to map @%08x, got %p\n", + addr, ret); + + if (is_fixed) { + psxUnmap(ret, size, tag); + return NULL; + } + + if (ret != NULL && ((addr ^ (long)ret) & 0x00ffffff) + && !tried_to_align) + { + psxUnmap(ret, size, tag); + + // try to use similarly aligned memory instead + // (recompiler needs this) + mask = (addr - 1) & ~addr & 0x07ffffff; + addr = (unsigned long)(ret + mask) & ~mask; + tried_to_align = 1; + goto retry; + } + } return ret; } @@ -69,7 +95,8 @@ void psxUnmap(void *ptr, size_t size, enum psxMapTag tag) return; } - munmap(ptr, size); + if (ptr) + munmap(ptr, size); } s8 *psxM = NULL; // Kernel & User Memory (2 Meg) @@ -109,8 +136,13 @@ int psxMemInit() { psxM = psxMap(0x80000000, 0x00210000, 1, MAP_TAG_RAM); #ifndef RAM_FIXED +#ifdef __BLACKBERRY_QNX__ if (psxM == NULL) - psxM = psxMap(0x70000000, 0x00210000, 0, MAP_TAG_RAM); + psxM = psxMap(0x77000000, 0x00210000, 0, MAP_TAG_RAM); +#else + if (psxM == NULL) + psxM = psxMap(0x78000000, 0x00210000, 0, MAP_TAG_RAM); +#endif #endif if (psxM == NULL) { SysMessage(_("mapping main RAM failed")); @@ -118,12 +150,13 @@ int psxMemInit() { } psxP = &psxM[0x200000]; - psxH = psxMap(0x1f800000, 0x10000, 1, MAP_TAG_OTHER); + 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 != (void *)0x1f800000) { + psxR == NULL || psxP == NULL || psxH == NULL) { SysMessage(_("Error allocating memory!")); + psxMemShutdown(); return -1; } @@ -177,12 +210,12 @@ void psxMemReset() { } void psxMemShutdown() { - psxUnmap(psxM, 0x00210000, MAP_TAG_RAM); - psxUnmap(psxH, 0x1f800000, MAP_TAG_OTHER); - psxUnmap(psxR, 0x80000, MAP_TAG_OTHER); + psxUnmap(psxM, 0x00210000, MAP_TAG_RAM); psxM = NULL; + psxUnmap(psxH, 0x10000, MAP_TAG_OTHER); psxH = NULL; + psxUnmap(psxR, 0x80000, MAP_TAG_OTHER); psxR = NULL; - free(psxMemRLUT); - free(psxMemWLUT); + free(psxMemRLUT); psxMemRLUT = NULL; + free(psxMemWLUT); psxMemWLUT = NULL; } static int writeok = 1; @@ -192,8 +225,8 @@ u8 psxMemRead8(u32 mem) { u32 t; t = mem >> 16; - if (t == 0x1f80) { - if (mem < 0x1f801000) + if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) { + if ((mem & 0xffff) < 0x400) return psxHu8(mem); else return psxHwRead8(mem); @@ -217,8 +250,8 @@ u16 psxMemRead16(u32 mem) { u32 t; t = mem >> 16; - if (t == 0x1f80) { - if (mem < 0x1f801000) + if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) { + if ((mem & 0xffff) < 0x400) return psxHu16(mem); else return psxHwRead16(mem); @@ -242,8 +275,8 @@ u32 psxMemRead32(u32 mem) { u32 t; t = mem >> 16; - if (t == 0x1f80) { - if (mem < 0x1f801000) + if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) { + if ((mem & 0xffff) < 0x400) return psxHu32(mem); else return psxHwRead32(mem); @@ -267,8 +300,8 @@ void psxMemWrite8(u32 mem, u8 value) { u32 t; t = mem >> 16; - if (t == 0x1f80) { - if (mem < 0x1f801000) + if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) { + if ((mem & 0xffff) < 0x400) psxHu8(mem) = value; else psxHwWrite8(mem, value); @@ -294,8 +327,8 @@ void psxMemWrite16(u32 mem, u16 value) { u32 t; t = mem >> 16; - if (t == 0x1f80) { - if (mem < 0x1f801000) + if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) { + if ((mem & 0xffff) < 0x400) psxHu16ref(mem) = SWAPu16(value); else psxHwWrite16(mem, value); @@ -322,8 +355,8 @@ void psxMemWrite32(u32 mem, u32 value) { // if ((mem&0x1fffff) == 0x71E18 || value == 0x48088800) SysPrintf("t2fix!!\n"); t = mem >> 16; - if (t == 0x1f80) { - if (mem < 0x1f801000) + if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) { + if ((mem & 0xffff) < 0x400) psxHu32ref(mem) = SWAPu32(value); else psxHwWrite32(mem, value); @@ -380,8 +413,8 @@ void *psxMemPointer(u32 mem) { u32 t; t = mem >> 16; - if (t == 0x1f80) { - if (mem < 0x1f801000) + if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) { + if ((mem & 0xffff) < 0x400) return (void *)&psxH[mem]; else return NULL;