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