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