(3DS) Set pthread stack size to 12MB - possible solution for crashes
[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"
ce0e7ac9 31
32#include "memmap.h"
ef79bbde
P
33
34#ifndef MAP_ANONYMOUS
35#define MAP_ANONYMOUS MAP_ANON
36#endif
37
87e5b45f 38void *(*psxMapHook)(unsigned long addr, size_t size, int is_fixed,
39 enum psxMapTag tag);
40void (*psxUnmapHook)(void *ptr, size_t size, enum psxMapTag tag);
41
42void *psxMap(unsigned long addr, size_t size, int is_fixed,
43 enum psxMapTag tag)
44{
a62012a7 45#ifdef LIGHTREC
38c19eff 46 int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
a62012a7
T
47#else
48 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
49#endif
50
5644b26c 51 int try_ = 0;
b0dd9956 52 unsigned long mask;
87e5b45f 53 void *req, *ret;
54
b0dd9956 55retry:
56 if (psxMapHook != NULL) {
85f23982 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;
b0dd9956 70 }
87e5b45f 71
b0dd9956 72 if (addr != 0 && ret != (void *)addr) {
73 SysMessage("psxMap: warning: wanted to map @%08x, got %p\n",
74 addr, ret);
75
0069615f 76 if (is_fixed) {
77 psxUnmap(ret, size, tag);
78 return NULL;
79 }
80
5644b26c 81 if (((addr ^ (unsigned long)ret) & ~0xff000000l) && try_ < 2)
b0dd9956 82 {
83 psxUnmap(ret, size, tag);
84
85 // try to use similarly aligned memory instead
86 // (recompiler needs this)
5644b26c 87 mask = try_ ? 0xffff : 0xffffff;
88 addr = ((unsigned long)ret + mask) & ~mask;
89 try_++;
b0dd9956 90 goto retry;
91 }
92 }
87e5b45f 93
94 return ret;
95}
96
97void psxUnmap(void *ptr, size_t size, enum psxMapTag tag)
98{
99 if (psxUnmapHook != NULL) {
100 psxUnmapHook(ptr, size, tag);
101 return;
102 }
103
88901dd2 104 if (ptr)
105 munmap(ptr, size);
87e5b45f 106}
107
ef79bbde
P
108s8 *psxM = NULL; // Kernel & User Memory (2 Meg)
109s8 *psxP = NULL; // Parallel Port (64K)
110s8 *psxR = NULL; // BIOS ROM (512K)
111s8 *psxH = NULL; // Scratch Pad (1K) & Hardware Registers (8K)
112
113u8 **psxMemWLUT = NULL;
114u8 **psxMemRLUT = NULL;
115
116/* Playstation Memory Map (from Playstation doc by Joshua Walker)
1170x0000_0000-0x0000_ffff Kernel (64K)
1180x0001_0000-0x001f_ffff User Memory (1.9 Meg)
119
1200x1f00_0000-0x1f00_ffff Parallel Port (64K)
121
1220x1f80_0000-0x1f80_03ff Scratch Pad (1024 bytes)
123
1240x1f80_1000-0x1f80_2fff Hardware Registers (8K)
125
1260x1fc0_0000-0x1fc7_ffff BIOS (512K)
127
1280x8000_0000-0x801f_ffff Kernel and User Memory Mirror (2 Meg) Cached
1290x9fc0_0000-0x9fc7_ffff BIOS Mirror (512K) Cached
130
1310xa000_0000-0xa01f_ffff Kernel and User Memory Mirror (2 Meg) Uncached
1320xbfc0_0000-0xbfc7_ffff BIOS Mirror (512K) Uncached
133*/
134
135int 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
a62012a7 143#ifdef LIGHTREC
38c19eff 144 psxM = psxMap(0x30000000, 0x00210000, 1, MAP_TAG_RAM);
a62012a7
T
145#else
146 psxM = psxMap(0x80000000, 0x00210000, 1, MAP_TAG_RAM);
147#endif
a327ad27 148#ifndef RAM_FIXED
a4874585 149 if (psxM == NULL)
f23d3386 150 psxM = psxMap(0x77000000, 0x00210000, 0, MAP_TAG_RAM);
a327ad27 151#endif
87e5b45f 152 if (psxM == NULL) {
a327ad27 153 SysMessage(_("mapping main RAM failed"));
154 return -1;
155 }
ef79bbde
P
156
157 psxP = &psxM[0x200000];
a62012a7 158#ifdef LIGHTREC
38c19eff
PC
159 psxH = psxMap(0x4f800000, 0x10000, 0, MAP_TAG_OTHER);
160 psxR = psxMap(0x4fc00000, 0x80000, 0, MAP_TAG_OTHER);
a62012a7
T
161#else
162 psxH = psxMap(0x1f800000, 0x10000, 0, MAP_TAG_OTHER);
163 psxR = psxMap(0x1fc00000, 0x80000, 0, MAP_TAG_OTHER);
164#endif
ef79bbde
P
165
166 if (psxMemRLUT == NULL || psxMemWLUT == NULL ||
6d760c92 167 psxR == NULL || psxP == NULL || psxH == NULL) {
ef79bbde 168 SysMessage(_("Error allocating memory!"));
88901dd2 169 psxMemShutdown();
ef79bbde
P
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
199void psxMemReset() {
200 FILE *f = NULL;
201 char bios[1024];
202
203 memset(psxM, 0, 0x00200000);
23643ee7 204 memset(psxP, 0xff, 0x00010000);
ef79bbde
P
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
222void psxMemShutdown() {
88901dd2 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;
ef79bbde 226
88901dd2 227 free(psxMemRLUT); psxMemRLUT = NULL;
228 free(psxMemWLUT); psxMemWLUT = NULL;
ef79bbde
P
229}
230
231static int writeok = 1;
232
233u8 psxMemRead8(u32 mem) {
234 char *p;
235 u32 t;
236
237 t = mem >> 16;
9dd7d179 238 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
239 if ((mem & 0xffff) < 0x400)
ef79bbde
P
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
258u16 psxMemRead16(u32 mem) {
259 char *p;
260 u32 t;
261
262 t = mem >> 16;
9dd7d179 263 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
264 if ((mem & 0xffff) < 0x400)
ef79bbde
P
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
283u32 psxMemRead32(u32 mem) {
284 char *p;
285 u32 t;
286
287 t = mem >> 16;
9dd7d179 288 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
289 if ((mem & 0xffff) < 0x400)
ef79bbde
P
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
308void psxMemWrite8(u32 mem, u8 value) {
309 char *p;
310 u32 t;
311
312 t = mem >> 16;
9dd7d179 313 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
314 if ((mem & 0xffff) < 0x400)
ef79bbde
P
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
335void psxMemWrite16(u32 mem, u16 value) {
336 char *p;
337 u32 t;
338
339 t = mem >> 16;
9dd7d179 340 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
341 if ((mem & 0xffff) < 0x400)
ef79bbde
P
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
3e31e934 352 psxCpu->Clear((mem & (~3)), 1);
ef79bbde
P
353#endif
354 } else {
355#ifdef PSXMEM_LOG
356 PSXMEM_LOG("err sh %8.8lx\n", mem);
357#endif
358 }
359 }
360}
361
362void 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;
9dd7d179 368 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
369 if ((mem & 0xffff) < 0x400)
ef79bbde
P
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
421void *psxMemPointer(u32 mem) {
422 char *p;
423 u32 t;
424
425 t = mem >> 16;
9dd7d179 426 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
427 if ((mem & 0xffff) < 0x400)
ef79bbde
P
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}