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