Update psxmem.c
[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 #ifndef MAP_ANONYMOUS
35 #define MAP_ANONYMOUS MAP_ANON
36 #endif
37
38 void *(*psxMapHook)(unsigned long addr, size_t size, int is_fixed,
39                 enum psxMapTag tag);
40 void (*psxUnmapHook)(void *ptr, size_t size, enum psxMapTag tag);
41
42 void *psxMap(unsigned long addr, size_t size, int is_fixed,
43                 enum psxMapTag tag)
44 {
45 #ifdef LIGHTREC
46         int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
47 #else
48         int flags = MAP_PRIVATE | MAP_ANONYMOUS;
49 #endif
50
51         int try_ = 0;
52         unsigned long mask;
53         void *req, *ret;
54
55 retry:
56         if (psxMapHook != NULL) {
57                 ret = psxMapHook(addr, size, 0, tag);
58                 if (ret == NULL)
59                         return NULL;
60         }
61         else {
62                 /* avoid MAP_FIXED, it overrides existing mappings.. */
63                 /* if (is_fixed)
64                         flags |= MAP_FIXED; */
65
66                 req = (void *)addr;
67                 ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
68                 if (ret == MAP_FAILED)
69                         return NULL;
70         }
71
72         if (addr != 0 && ret != (void *)addr) {
73                 SysMessage("psxMap: warning: wanted to map @%08x, got %p\n",
74                         addr, ret);
75
76                 if (is_fixed) {
77                         psxUnmap(ret, size, tag);
78                         return NULL;
79                 }
80
81                 if (((addr ^ (unsigned long)ret) & ~0xff000000l) && try_ < 2)
82                 {
83                         psxUnmap(ret, size, tag);
84
85                         // try to use similarly aligned memory instead
86                         // (recompiler needs this)
87                         mask = try_ ? 0xffff : 0xffffff;
88                         addr = ((unsigned long)ret + mask) & ~mask;
89                         try_++;
90                         goto retry;
91                 }
92         }
93
94         return ret;
95 }
96
97 void psxUnmap(void *ptr, size_t size, enum psxMapTag tag)
98 {
99         if (psxUnmapHook != NULL) {
100                 psxUnmapHook(ptr, size, tag);
101                 return;
102         }
103
104         if (ptr)
105                 munmap(ptr, size);
106 }
107
108 s8 *psxM = NULL; // Kernel & User Memory (2 Meg)
109 s8 *psxP = NULL; // Parallel Port (64K)
110 s8 *psxR = NULL; // BIOS ROM (512K)
111 s8 *psxH = NULL; // Scratch Pad (1K) & Hardware Registers (8K)
112
113 u8 **psxMemWLUT = NULL;
114 u8 **psxMemRLUT = NULL;
115
116 /*  Playstation Memory Map (from Playstation doc by Joshua Walker)
117 0x0000_0000-0x0000_ffff         Kernel (64K)
118 0x0001_0000-0x001f_ffff         User Memory (1.9 Meg)
119
120 0x1f00_0000-0x1f00_ffff         Parallel Port (64K)
121
122 0x1f80_0000-0x1f80_03ff         Scratch Pad (1024 bytes)
123
124 0x1f80_1000-0x1f80_2fff         Hardware Registers (8K)
125
126 0x1fc0_0000-0x1fc7_ffff         BIOS (512K)
127
128 0x8000_0000-0x801f_ffff         Kernel and User Memory Mirror (2 Meg) Cached
129 0x9fc0_0000-0x9fc7_ffff         BIOS Mirror (512K) Cached
130
131 0xa000_0000-0xa01f_ffff         Kernel and User Memory Mirror (2 Meg) Uncached
132 0xbfc0_0000-0xbfc7_ffff         BIOS Mirror (512K) Uncached
133 */
134
135 int psxMemInit() {
136         int i;
137
138         psxMemRLUT = (u8 **)malloc(0x10000 * sizeof(void *));
139         psxMemWLUT = (u8 **)malloc(0x10000 * sizeof(void *));
140         memset(psxMemRLUT, 0, 0x10000 * sizeof(void *));
141         memset(psxMemWLUT, 0, 0x10000 * sizeof(void *));
142
143 #ifdef LIGHTREC
144         psxM = psxMap(0x30000000, 0x00210000, 1, MAP_TAG_RAM);
145 #else
146         psxM = psxMap(0x80000000, 0x00210000, 1, MAP_TAG_RAM);
147 #endif
148 #ifndef RAM_FIXED
149         if (psxM == NULL)
150                 psxM = psxMap(0x77000000, 0x00210000, 0, MAP_TAG_RAM);
151 #endif
152         if (psxM == NULL) {
153                 SysMessage(_("mapping main RAM failed"));
154                 return -1;
155         }
156
157         psxP = &psxM[0x200000];
158 #ifdef LIGHTREC
159         psxH = psxMap(0x4f800000, 0x10000, 0, MAP_TAG_OTHER);
160         psxR = psxMap(0x4fc00000, 0x80000, 0, MAP_TAG_OTHER);
161 #else
162         psxH = psxMap(0x1f800000, 0x10000, 0, MAP_TAG_OTHER);
163         psxR = psxMap(0x1fc00000, 0x80000, 0, MAP_TAG_OTHER);
164 #endif
165
166         if (psxMemRLUT == NULL || psxMemWLUT == NULL || 
167             psxR == NULL || psxP == NULL || psxH == NULL) {
168                 SysMessage(_("Error allocating memory!"));
169                 psxMemShutdown();
170                 return -1;
171         }
172
173 // MemR
174         for (i = 0; i < 0x80; i++) psxMemRLUT[i + 0x0000] = (u8 *)&psxM[(i & 0x1f) << 16];
175
176         memcpy(psxMemRLUT + 0x8000, psxMemRLUT, 0x80 * sizeof(void *));
177         memcpy(psxMemRLUT + 0xa000, psxMemRLUT, 0x80 * sizeof(void *));
178
179         psxMemRLUT[0x1f00] = (u8 *)psxP;
180         psxMemRLUT[0x1f80] = (u8 *)psxH;
181
182         for (i = 0; i < 0x08; i++) psxMemRLUT[i + 0x1fc0] = (u8 *)&psxR[i << 16];
183
184         memcpy(psxMemRLUT + 0x9fc0, psxMemRLUT + 0x1fc0, 0x08 * sizeof(void *));
185         memcpy(psxMemRLUT + 0xbfc0, psxMemRLUT + 0x1fc0, 0x08 * sizeof(void *));
186
187 // MemW
188         for (i = 0; i < 0x80; i++) psxMemWLUT[i + 0x0000] = (u8 *)&psxM[(i & 0x1f) << 16];
189
190         memcpy(psxMemWLUT + 0x8000, psxMemWLUT, 0x80 * sizeof(void *));
191         memcpy(psxMemWLUT + 0xa000, psxMemWLUT, 0x80 * sizeof(void *));
192
193         psxMemWLUT[0x1f00] = (u8 *)psxP;
194         psxMemWLUT[0x1f80] = (u8 *)psxH;
195
196         return 0;
197 }
198
199 void psxMemReset() {
200         FILE *f = NULL;
201         char bios[1024];
202
203         memset(psxM, 0, 0x00200000);
204         memset(psxP, 0xff, 0x00010000);
205
206         if (strcmp(Config.Bios, "HLE") != 0) {
207                 sprintf(bios, "%s/%s", Config.BiosDir, Config.Bios);
208                 f = fopen(bios, "rb");
209
210                 if (f == NULL) {
211                         SysMessage(_("Could not open BIOS:\"%s\". Enabling HLE Bios!\n"), bios);
212                         memset(psxR, 0, 0x80000);
213                         Config.HLE = TRUE;
214                 } else {
215                         fread(psxR, 1, 0x80000, f);
216                         fclose(f);
217                         Config.HLE = FALSE;
218                 }
219         } else Config.HLE = TRUE;
220 }
221
222 void psxMemShutdown() {
223         psxUnmap(psxM, 0x00210000, MAP_TAG_RAM); psxM = NULL;
224         psxUnmap(psxH, 0x10000, MAP_TAG_OTHER); psxH = NULL;
225         psxUnmap(psxR, 0x80000, MAP_TAG_OTHER); psxR = NULL;
226
227         free(psxMemRLUT); psxMemRLUT = NULL;
228         free(psxMemWLUT); psxMemWLUT = NULL;
229 }
230
231 static int writeok = 1;
232
233 u8 psxMemRead8(u32 mem) {
234         char *p;
235         u32 t;
236
237         t = mem >> 16;
238         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
239                 if ((mem & 0xffff) < 0x400)
240                         return psxHu8(mem);
241                 else
242                         return psxHwRead8(mem);
243         } else {
244                 p = (char *)(psxMemRLUT[t]);
245                 if (p != NULL) {
246                         if (Config.Debug)
247                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, R1);
248                         return *(u8 *)(p + (mem & 0xffff));
249                 } else {
250 #ifdef PSXMEM_LOG
251                         PSXMEM_LOG("err lb %8.8lx\n", mem);
252 #endif
253                         return 0;
254                 }
255         }
256 }
257
258 u16 psxMemRead16(u32 mem) {
259         char *p;
260         u32 t;
261
262         t = mem >> 16;
263         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
264                 if ((mem & 0xffff) < 0x400)
265                         return psxHu16(mem);
266                 else
267                         return psxHwRead16(mem);
268         } else {
269                 p = (char *)(psxMemRLUT[t]);
270                 if (p != NULL) {
271                         if (Config.Debug)
272                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, R2);
273                         return SWAPu16(*(u16 *)(p + (mem & 0xffff)));
274                 } else {
275 #ifdef PSXMEM_LOG
276                         PSXMEM_LOG("err lh %8.8lx\n", mem);
277 #endif
278                         return 0;
279                 }
280         }
281 }
282
283 u32 psxMemRead32(u32 mem) {
284         char *p;
285         u32 t;
286
287         t = mem >> 16;
288         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
289                 if ((mem & 0xffff) < 0x400)
290                         return psxHu32(mem);
291                 else
292                         return psxHwRead32(mem);
293         } else {
294                 p = (char *)(psxMemRLUT[t]);
295                 if (p != NULL) {
296                         if (Config.Debug)
297                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, R4);
298                         return SWAPu32(*(u32 *)(p + (mem & 0xffff)));
299                 } else {
300 #ifdef PSXMEM_LOG
301                         if (writeok) { PSXMEM_LOG("err lw %8.8lx\n", mem); }
302 #endif
303                         return 0;
304                 }
305         }
306 }
307
308 void psxMemWrite8(u32 mem, u8 value) {
309         char *p;
310         u32 t;
311
312         t = mem >> 16;
313         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
314                 if ((mem & 0xffff) < 0x400)
315                         psxHu8(mem) = value;
316                 else
317                         psxHwWrite8(mem, value);
318         } else {
319                 p = (char *)(psxMemWLUT[t]);
320                 if (p != NULL) {
321                         if (Config.Debug)
322                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, W1);
323                         *(u8 *)(p + (mem & 0xffff)) = value;
324 #ifdef PSXREC
325                         psxCpu->Clear((mem & (~3)), 1);
326 #endif
327                 } else {
328 #ifdef PSXMEM_LOG
329                         PSXMEM_LOG("err sb %8.8lx\n", mem);
330 #endif
331                 }
332         }
333 }
334
335 void psxMemWrite16(u32 mem, u16 value) {
336         char *p;
337         u32 t;
338
339         t = mem >> 16;
340         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
341                 if ((mem & 0xffff) < 0x400)
342                         psxHu16ref(mem) = SWAPu16(value);
343                 else
344                         psxHwWrite16(mem, value);
345         } else {
346                 p = (char *)(psxMemWLUT[t]);
347                 if (p != NULL) {
348                         if (Config.Debug)
349                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, W2);
350                         *(u16 *)(p + (mem & 0xffff)) = SWAPu16(value);
351 #ifdef PSXREC
352                         psxCpu->Clear((mem & (~3)), 1);
353 #endif
354                 } else {
355 #ifdef PSXMEM_LOG
356                         PSXMEM_LOG("err sh %8.8lx\n", mem);
357 #endif
358                 }
359         }
360 }
361
362 void psxMemWrite32(u32 mem, u32 value) {
363         char *p;
364         u32 t;
365
366 //      if ((mem&0x1fffff) == 0x71E18 || value == 0x48088800) SysPrintf("t2fix!!\n");
367         t = mem >> 16;
368         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
369                 if ((mem & 0xffff) < 0x400)
370                         psxHu32ref(mem) = SWAPu32(value);
371                 else
372                         psxHwWrite32(mem, value);
373         } else {
374                 p = (char *)(psxMemWLUT[t]);
375                 if (p != NULL) {
376                         if (Config.Debug)
377                                 DebugCheckBP((mem & 0xffffff) | 0x80000000, W4);
378                         *(u32 *)(p + (mem & 0xffff)) = SWAPu32(value);
379 #ifdef PSXREC
380                         psxCpu->Clear(mem, 1);
381 #endif
382                 } else {
383                         if (mem != 0xfffe0130) {
384 #ifdef PSXREC
385                                 if (!writeok)
386                                         psxCpu->Clear(mem, 1);
387 #endif
388
389 #ifdef PSXMEM_LOG
390                                 if (writeok) { PSXMEM_LOG("err sw %8.8lx\n", mem); }
391 #endif
392                         } else {
393                                 int i;
394
395                                 switch (value) {
396                                         case 0x800: case 0x804:
397                                                 if (writeok == 0) break;
398                                                 writeok = 0;
399                                                 memset(psxMemWLUT + 0x0000, 0, 0x80 * sizeof(void *));
400                                                 memset(psxMemWLUT + 0x8000, 0, 0x80 * sizeof(void *));
401                                                 memset(psxMemWLUT + 0xa000, 0, 0x80 * sizeof(void *));
402                                                 break;
403                                         case 0x00: case 0x1e988:
404                                                 if (writeok == 1) break;
405                                                 writeok = 1;
406                                                 for (i = 0; i < 0x80; i++) psxMemWLUT[i + 0x0000] = (void *)&psxM[(i & 0x1f) << 16];
407                                                 memcpy(psxMemWLUT + 0x8000, psxMemWLUT, 0x80 * sizeof(void *));
408                                                 memcpy(psxMemWLUT + 0xa000, psxMemWLUT, 0x80 * sizeof(void *));
409                                                 break;
410                                         default:
411 #ifdef PSXMEM_LOG
412                                                 PSXMEM_LOG("unk %8.8lx = %x\n", mem, value);
413 #endif
414                                                 break;
415                                 }
416                         }
417                 }
418         }
419 }
420
421 void *psxMemPointer(u32 mem) {
422         char *p;
423         u32 t;
424
425         t = mem >> 16;
426         if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
427                 if ((mem & 0xffff) < 0x400)
428                         return (void *)&psxH[mem];
429                 else
430                         return NULL;
431         } else {
432                 p = (char *)(psxMemWLUT[t]);
433                 if (p != NULL) {
434                         return (void *)(p + (mem & 0xffff));
435                 }
436                 return NULL;
437         }
438 }