Merge Icache emulation from PCSX Redux + Senquack changes from PCSX4ALL
[pcsx_rearmed.git] / libpcsxcore / psxmem.c
1 /***************************************************************************
2  *   Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team              *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.           *
18  ***************************************************************************/
19
20 /*
21 * PSX memory functions.
22 */
23
24 // TODO: Implement caches & cycle penalty.
25
26 #include "psxmem.h"
27 #include "psxmem_map.h"
28 #include "r3000a.h"
29 #include "psxhw.h"
30 #include "debug.h"
31
32 #include "memmap.h"
33
34 #ifdef USE_LIBRETRO_VFS
35 #include <streams/file_stream_transforms.h>
36 #endif
37
38 #ifndef MAP_ANONYMOUS
39 #define MAP_ANONYMOUS MAP_ANON
40 #endif
41
42 boolean writeok = TRUE;
43
44 #ifndef NDEBUG
45 #include "debug.h"
46 #else
47 void DebugCheckBP(u32 address, enum breakpoint_types type) {}
48 #endif
49
50 void *(*psxMapHook)(unsigned long addr, size_t size, int is_fixed,
51                 enum psxMapTag tag);
52 void (*psxUnmapHook)(void *ptr, size_t size, enum psxMapTag tag);
53
54 void *psxMap(unsigned long addr, size_t size, int is_fixed,
55                 enum psxMapTag tag)
56 {
57 #ifdef LIGHTREC
58 #ifdef MAP_FIXED_NOREPLACE
59         int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE;
60 #else
61         int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
62 #endif
63 #else
64         int flags = MAP_PRIVATE | MAP_ANONYMOUS;
65 #endif
66
67         int try_ = 0;
68         unsigned long mask;
69         void *req, *ret;
70
71 retry:
72         if (psxMapHook != NULL) {
73                 ret = psxMapHook(addr, size, 0, tag);
74                 if (ret == NULL)
75                         return NULL;
76         }
77         else {
78                 /* avoid MAP_FIXED, it overrides existing mappings.. */
79                 /* if (is_fixed)
80                         flags |= MAP_FIXED; */
81
82                 req = (void *)addr;
83                 ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
84                 if (ret == MAP_FAILED)
85                         return NULL;
86         }
87
88         if (addr != 0 && ret != (void *)addr) {
89                 SysMessage("psxMap: warning: wanted to map @%08x, got %p\n",
90                         addr, ret);
91
92                 if (is_fixed) {
93                         psxUnmap(ret, size, tag);
94                         return NULL;
95                 }
96
97                 if (((addr ^ (unsigned long)ret) & ~0xff000000l) && try_ < 2)
98                 {
99                         psxUnmap(ret, size, tag);
100
101                         // try to use similarly aligned memory instead
102                         // (recompiler needs this)
103                         mask = try_ ? 0xffff : 0xffffff;
104                         addr = ((unsigned long)ret + mask) & ~mask;
105                         try_++;
106                         goto retry;
107                 }
108         }
109
110         return ret;
111 }
112
113 void psxUnmap(void *ptr, size_t size, enum psxMapTag tag)
114 {
115         if (psxUnmapHook != NULL) {
116                 psxUnmapHook(ptr, size, tag);
117                 return;
118         }
119
120         if (ptr)
121                 munmap(ptr, size);
122 }
123
124 s8 *psxM = NULL; // Kernel & User Memory (2 Meg)
125 s8 *psxP = NULL; // Parallel Port (64K)
126 s8 *psxR = NULL; // BIOS ROM (512K)
127 s8 *psxH = NULL; // Scratch Pad (1K) & Hardware Registers (8K)
128
129 u8 **psxMemWLUT = NULL;
130 u8 **psxMemRLUT = NULL;
131
132 /*  Playstation Memory Map (from Playstation doc by Joshua Walker)
133 0x0000_0000-0x0000_ffff         Kernel (64K)
134 0x0001_0000-0x001f_ffff         User Memory (1.9 Meg)
135
136 0x1f00_0000-0x1f00_ffff         Parallel Port (64K)
137
138 0x1f80_0000-0x1f80_03ff         Scratch Pad (1024 bytes)
139
140 0x1f80_1000-0x1f80_2fff         Hardware Registers (8K)
141
142 0x1fc0_0000-0x1fc7_ffff         BIOS (512K)
143
144 0x8000_0000-0x801f_ffff         Kernel and User Memory Mirror (2 Meg) Cached
145 0x9fc0_0000-0x9fc7_ffff         BIOS Mirror (512K) Cached
146
147 0xa000_0000-0xa01f_ffff         Kernel and User Memory Mirror (2 Meg) Uncached
148 0xbfc0_0000-0xbfc7_ffff         BIOS Mirror (512K) Uncached
149 */
150
151 int psxMemInit() {
152         int i;
153
154         psxMemRLUT = (u8 **)malloc(0x10000 * sizeof(void *));
155         psxMemWLUT = (u8 **)malloc(0x10000 * sizeof(void *));
156         memset(psxMemRLUT, 0, 0x10000 * sizeof(void *));
157         memset(psxMemWLUT, 0, 0x10000 * sizeof(void *));
158
159 #ifdef LIGHTREC
160         psxM = psxMap(0x30000000, 0x00210000, 1, MAP_TAG_RAM);
161         if (psxM == NULL)
162                 psxM = psxMap(0x70000000, 0x00210000, 1, MAP_TAG_RAM);
163
164 #else
165         psxM = psxMap(0x80000000, 0x00210000, 1, MAP_TAG_RAM);
166 #endif
167 #ifndef RAM_FIXED
168         if (psxM == NULL)
169                 psxM = psxMap(0x77000000, 0x00210000, 0, MAP_TAG_RAM);
170 #endif
171         if (psxM == NULL) {
172                 SysMessage(_("mapping main RAM failed"));
173                 return -1;
174         }
175
176         psxP = &psxM[0x200000];
177 #ifdef LIGHTREC
178         psxH = psxMap(0x4f800000, 0x10000, 0, MAP_TAG_OTHER);
179         if (psxH == NULL)
180                 psxH = psxMap(0x8f800000, 0x10000, 0, MAP_TAG_OTHER);
181
182         psxR = psxMap(0x4fc00000, 0x80000, 0, MAP_TAG_OTHER);
183         if (psxR == NULL)
184                 psxR = psxMap(0x8fc00000, 0x80000, 0, MAP_TAG_OTHER);
185 #else
186         psxH = psxMap(0x1f800000, 0x10000, 0, MAP_TAG_OTHER);
187         psxR = psxMap(0x1fc00000, 0x80000, 0, MAP_TAG_OTHER);
188 #endif
189
190         if (psxMemRLUT == NULL || psxMemWLUT == NULL || 
191             psxR == NULL || psxP == NULL || psxH == NULL) {
192                 SysMessage(_("Error allocating memory!"));
193                 psxMemShutdown();
194                 return -1;
195         }
196
197 // MemR
198         for (i = 0; i < 0x80; i++) psxMemRLUT[i + 0x0000] = (u8 *)&psxM[(i & 0x1f) << 16];
199
200         memcpy(psxMemRLUT + 0x8000, psxMemRLUT, 0x80 * sizeof(void *));
201         memcpy(psxMemRLUT + 0xa000, psxMemRLUT, 0x80 * sizeof(void *));
202
203         psxMemRLUT[0x1f00] = (u8 *)psxP;
204         psxMemRLUT[0x1f80] = (u8 *)psxH;
205
206         for (i = 0; i < 0x08; i++) psxMemRLUT[i + 0x1fc0] = (u8 *)&psxR[i << 16];
207
208         memcpy(psxMemRLUT + 0x9fc0, psxMemRLUT + 0x1fc0, 0x08 * sizeof(void *));
209         memcpy(psxMemRLUT + 0xbfc0, psxMemRLUT + 0x1fc0, 0x08 * sizeof(void *));
210
211 // MemW
212         for (i = 0; i < 0x80; i++) psxMemWLUT[i + 0x0000] = (u8 *)&psxM[(i & 0x1f) << 16];
213
214         memcpy(psxMemWLUT + 0x8000, psxMemWLUT, 0x80 * sizeof(void *));
215         memcpy(psxMemWLUT + 0xa000, psxMemWLUT, 0x80 * sizeof(void *));
216
217         psxMemWLUT[0x1f00] = (u8 *)psxP;
218         psxMemWLUT[0x1f80] = (u8 *)psxH;
219
220         return 0;
221 }
222
223 void psxMemReset() {
224         FILE *f = NULL;
225         char bios[1024];
226
227         memset(psxM, 0, 0x00200000);
228         memset(psxP, 0xff, 0x00010000);
229
230         Config.HLE = TRUE;
231
232         if (strcmp(Config.Bios, "HLE") != 0) {
233                 sprintf(bios, "%s/%s", Config.BiosDir, Config.Bios);
234                 f = fopen(bios, "rb");
235
236                 if (f == NULL) {
237                         SysMessage(_("Could not open BIOS:\"%s\". Enabling HLE Bios!\n"), bios);
238                         memset(psxR, 0, 0x80000);
239                 } else {
240                         if (fread(psxR, 1, 0x80000, f) == 0x80000) {
241                                 Config.HLE = FALSE;
242                         } else {
243                                 SysMessage(_("The selected BIOS:\"%s\" is of wrong size. Enabling HLE Bios!\n"), bios);
244                         }
245                         fclose(f);
246                 }
247         }
248 }
249
250 void psxMemShutdown() {
251         psxUnmap(psxM, 0x00210000, MAP_TAG_RAM); psxM = NULL;
252         psxUnmap(psxH, 0x10000, MAP_TAG_OTHER); psxH = NULL;
253         psxUnmap(psxR, 0x80000, MAP_TAG_OTHER); psxR = NULL;
254
255         free(psxMemRLUT); psxMemRLUT = NULL;
256         free(psxMemWLUT); psxMemWLUT = NULL;
257 }
258
259 u8 psxMemRead8(u32 mem) {
260         char *p;
261         u32 t;
262
263         t = mem >> 16;
264         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
265                 if ((mem & 0xffff) < 0x400)
266                         return psxHu8(mem);
267                 else
268                         return psxHwRead8(mem);
269         } else {
270                 p = (char *)(psxMemRLUT[t]);
271                 if (p != NULL) {
272                         if (Config.Debug)
273                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, R1);
274                         return *(u8 *)(p + (mem & 0xffff));
275                 } else {
276 #ifdef PSXMEM_LOG
277                         PSXMEM_LOG("err lb %8.8lx\n", mem);
278 #endif
279                         return 0;
280                 }
281         }
282 }
283
284 u16 psxMemRead16(u32 mem) {
285         char *p;
286         u32 t;
287
288         t = mem >> 16;
289         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
290                 if ((mem & 0xffff) < 0x400)
291                         return psxHu16(mem);
292                 else
293                         return psxHwRead16(mem);
294         } else {
295                 p = (char *)(psxMemRLUT[t]);
296                 if (p != NULL) {
297                         if (Config.Debug)
298                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, R2);
299                         return SWAPu16(*(u16 *)(p + (mem & 0xffff)));
300                 } else {
301 #ifdef PSXMEM_LOG
302                         PSXMEM_LOG("err lh %8.8lx\n", mem);
303 #endif
304                         return 0;
305                 }
306         }
307 }
308
309 u32 psxMemRead32(u32 mem) {
310         char *p;
311         u32 t;
312
313         t = mem >> 16;
314         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
315                 if ((mem & 0xffff) < 0x400)
316                         return psxHu32(mem);
317                 else
318                         return psxHwRead32(mem);
319         } else {
320                 p = (char *)(psxMemRLUT[t]);
321                 if (p != NULL) {
322                         if (Config.Debug)
323                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, R4);
324                         return SWAPu32(*(u32 *)(p + (mem & 0xffff)));
325                 } else {
326 #ifdef PSXMEM_LOG
327                         if (writeok) { PSXMEM_LOG("err lw %8.8lx\n", mem); }
328 #endif
329                         return 0;
330                 }
331         }
332 }
333
334 void psxMemWrite8(u32 mem, u8 value) {
335         char *p;
336         u32 t;
337
338         t = mem >> 16;
339         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
340                 if ((mem & 0xffff) < 0x400)
341                         psxHu8(mem) = value;
342                 else
343                         psxHwWrite8(mem, value);
344         } else {
345                 p = (char *)(psxMemWLUT[t]);
346                 if (p != NULL) {
347                         if (Config.Debug)
348                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, W1);
349                         *(u8 *)(p + (mem & 0xffff)) = value;
350 #ifdef PSXREC
351                         psxCpu->Clear((mem & (~3)), 1);
352 #endif
353                 } else {
354 #ifdef PSXMEM_LOG
355                         PSXMEM_LOG("err sb %8.8lx\n", mem);
356 #endif
357                 }
358         }
359 }
360
361 void psxMemWrite16(u32 mem, u16 value) {
362         char *p;
363         u32 t;
364
365         t = mem >> 16;
366         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
367                 if ((mem & 0xffff) < 0x400)
368                         psxHu16ref(mem) = SWAPu16(value);
369                 else
370                         psxHwWrite16(mem, value);
371         } else {
372                 p = (char *)(psxMemWLUT[t]);
373                 if (p != NULL) {
374                         if (Config.Debug)
375                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, W2);
376                         *(u16 *)(p + (mem & 0xffff)) = SWAPu16(value);
377 #ifdef PSXREC
378                         psxCpu->Clear((mem & (~3)), 1);
379 #endif
380                 } else {
381 #ifdef PSXMEM_LOG
382                         PSXMEM_LOG("err sh %8.8lx\n", mem);
383 #endif
384                 }
385         }
386 }
387
388 void psxMemWrite32(u32 mem, u32 value) {
389         char *p;
390 #if defined(ICACHE_EMULATION)
391         /*  Stores in PS1 code during cache isolation invalidate cachelines.
392          * It is assumed that cache-flush routines write to the lowest 4KB of
393          * address space for Icache, or 1KB for Dcache/scratchpad.
394          *  Originally, stores had to check 'writeok' in psxRegs struct before
395          * writing to RAM. To eliminate this necessity, we could simply patch the
396          * BIOS 0x44 FlushCache() A0 jumptable entry. Unfortunately, this won't
397          * work for some games that use less-buggy non-BIOS cache-flush routines
398          * like '007 Tomorrow Never Dies', often provided by SN-systems, the PS1
399          * toolchain provider.
400          *  Instead, we backup the lowest 64KB PS1 RAM when the cache is isolated.
401          * All stores write to RAM regardless of cache state. Thus, cache-flush
402          * routines temporarily trash the lowest 4KB of PS1 RAM. Fortunately, they
403          * ran in a 'critical section' with interrupts disabled, so there's little
404          * worry of PS1 code ever reading the trashed contents.
405          *  We point the relevant portions of psxMemRLUT[] to the 64KB backup while
406          * cache is isolated. This is in case the dynarec needs to recompile some
407          * code during isolation. As long as it reads code using psxMemRLUT[] ptrs,
408          * it should never see trashed RAM contents.
409          *
410          * -senquack, mips dynarec team, 2017
411          */
412         static u32 mem_bak[0x10000/4];
413 #endif
414         u32 t;
415         u32 m = mem & 0xffff;
416 //      if ((mem&0x1fffff) == 0x71E18 || value == 0x48088800) SysPrintf("t2fix!!\n");
417         t = mem >> 16;
418         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
419                 if (m < 0x400)
420                         psxHu32ref(mem) = SWAPu32(value);
421                 else
422                         psxHwWrite32(mem, value);
423         } else {
424                 p = (char *)(psxMemWLUT[t]);
425                 if (p != NULL) {
426                         if (Config.Debug)
427                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, W4);
428                         *(u32 *)(p + (mem & 0xffff)) = SWAPu32(value);
429 #ifdef PSXREC
430                         psxCpu->Clear(mem, 1);
431 #endif
432                 } else {
433                         if (mem != 0xfffe0130) {
434 #ifdef PSXREC
435                                 if (!writeok)
436                                         psxCpu->Clear(mem, 1);
437 #endif
438
439 #ifdef PSXMEM_LOG
440                                 if (writeok) { PSXMEM_LOG("err sw %8.8lx\n", mem); }
441 #endif
442                         } else {
443                                 int i;
444
445                                 switch (value) {
446                                         case 0x800: case 0x804:
447                                                 if (writeok == FALSE) break;
448                                                 writeok = FALSE;
449                                                 memset(psxMemWLUT + 0x0000, 0, 0x80 * sizeof(void *));
450                                                 memset(psxMemWLUT + 0x8000, 0, 0x80 * sizeof(void *));
451                                                 memset(psxMemWLUT + 0xa000, 0, 0x80 * sizeof(void *));
452 #ifdef ICACHE_EMULATION
453                                                 /* Cache is now isolated, pending cache-flush sequence:
454                                                 *  Backup lower 64KB of PS1 RAM, adjust psxMemRLUT[].
455                                                 */
456                                                 memcpy((void*)mem_bak, (void*)psxM, sizeof(mem_bak));
457                                                 psxMemRLUT[0x0000] = psxMemRLUT[0x0020] = psxMemRLUT[0x0040] = psxMemRLUT[0x0060] = (u8 *)mem_bak;
458                                                 psxMemRLUT[0x8000] = psxMemRLUT[0x8020] = psxMemRLUT[0x8040] = psxMemRLUT[0x8060] = (u8 *)mem_bak;
459                                                 psxMemRLUT[0xa000] = psxMemRLUT[0xa020] = psxMemRLUT[0xa040] = psxMemRLUT[0xa060] = (u8 *)mem_bak;
460                                                 psxCpu->Notify(R3000ACPU_NOTIFY_CACHE_ISOLATED, NULL);
461 #endif
462                                                 break;
463                                         case 0x00: case 0x1e988:
464                                                 if (writeok == TRUE) break;
465                                                 writeok = TRUE;
466                                                 for (i = 0; i < 0x80; i++) psxMemWLUT[i + 0x0000] = (void *)&psxM[(i & 0x1f) << 16];
467                                                 memcpy(psxMemWLUT + 0x8000, psxMemWLUT, 0x80 * sizeof(void *));
468                                                 memcpy(psxMemWLUT + 0xa000, psxMemWLUT, 0x80 * sizeof(void *));
469 #ifdef ICACHE_EMULATION
470                                                 /* Cache is now unisolated:
471                                                 * Restore lower 64KB RAM contents and psxMemRLUT[].
472                                                 */
473                                                 memcpy((void*)psxM, (void*)mem_bak, sizeof(mem_bak));
474                                                 psxMemRLUT[0x0000] = psxMemRLUT[0x0020] = psxMemRLUT[0x0040] = psxMemRLUT[0x0060] = (u8 *)psxM;
475                                                 psxMemRLUT[0x8000] = psxMemRLUT[0x8020] = psxMemRLUT[0x8040] = psxMemRLUT[0x8060] = (u8 *)psxM;
476                                                 psxMemRLUT[0xa000] = psxMemRLUT[0xa020] = psxMemRLUT[0xa040] = psxMemRLUT[0xa060] = (u8 *)psxM;
477                                                 /* Dynarecs might take this opportunity to flush their code cache */
478                                                 psxCpu->Notify(R3000ACPU_NOTIFY_CACHE_UNISOLATED, NULL);
479 #endif
480                                                 break;
481                                         default:
482 #ifdef PSXMEM_LOG
483                                                 PSXMEM_LOG("unk %8.8lx = %x\n", mem, value);
484 #endif
485                                                 break;
486                                 }
487                         }
488                 }
489         }
490 }
491
492 void *psxMemPointer(u32 mem) {
493         char *p;
494         u32 t;
495
496         t = mem >> 16;
497         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
498                 if ((mem & 0xffff) < 0x400)
499                         return (void *)&psxH[mem];
500                 else
501                         return NULL;
502         } else {
503                 p = (char *)(psxMemWLUT[t]);
504                 if (p != NULL) {
505                         return (void *)(p + (mem & 0xffff));
506                 }
507                 return NULL;
508         }
509 }