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