cheat: add some undo logic
[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"
27#include "r3000a.h"
28#include "psxhw.h"
fc8145b7 29#include "debug.h"
ef79bbde
P
30#include <sys/mman.h>
31
32#ifndef MAP_ANONYMOUS
33#define MAP_ANONYMOUS MAP_ANON
34#endif
35
36s8 *psxM = NULL; // Kernel & User Memory (2 Meg)
37s8 *psxP = NULL; // Parallel Port (64K)
38s8 *psxR = NULL; // BIOS ROM (512K)
39s8 *psxH = NULL; // Scratch Pad (1K) & Hardware Registers (8K)
40
41u8 **psxMemWLUT = NULL;
42u8 **psxMemRLUT = NULL;
43
44/* Playstation Memory Map (from Playstation doc by Joshua Walker)
450x0000_0000-0x0000_ffff Kernel (64K)
460x0001_0000-0x001f_ffff User Memory (1.9 Meg)
47
480x1f00_0000-0x1f00_ffff Parallel Port (64K)
49
500x1f80_0000-0x1f80_03ff Scratch Pad (1024 bytes)
51
520x1f80_1000-0x1f80_2fff Hardware Registers (8K)
53
540x1fc0_0000-0x1fc7_ffff BIOS (512K)
55
560x8000_0000-0x801f_ffff Kernel and User Memory Mirror (2 Meg) Cached
570x9fc0_0000-0x9fc7_ffff BIOS Mirror (512K) Cached
58
590xa000_0000-0xa01f_ffff Kernel and User Memory Mirror (2 Meg) Uncached
600xbfc0_0000-0xbfc7_ffff BIOS Mirror (512K) Uncached
61*/
62
63int 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
ffb0b9e0 71 psxM = mmap((void *)0x80000000, 0x00210000,
6e1a0c4d 72 PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
a327ad27 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 }
ef79bbde
P
82
83 psxP = &psxM[0x200000];
ffb0b9e0 84 psxH = mmap((void *)0x1f800000, 0x00010000,
6e1a0c4d 85 PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
ef79bbde 86
6e1a0c4d 87 psxR = mmap((void *)0x1fc00000, 0x80000,
a06c1d6e 88 PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
ef79bbde
P
89
90 if (psxMemRLUT == NULL || psxMemWLUT == NULL ||
a327ad27 91 psxR == MAP_FAILED ||
ffb0b9e0 92 psxP == NULL || psxH != (void *)0x1f800000) {
ef79bbde
P
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
123void 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
146void psxMemShutdown() {
6e1a0c4d 147 munmap(psxM, 0x00210000);
148 munmap(psxH, 0x1f800000);
a06c1d6e 149 munmap(psxR, 0x80000);
ef79bbde 150
ef79bbde
P
151 free(psxMemRLUT);
152 free(psxMemWLUT);
153}
154
155static int writeok = 1;
156
157u8 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
182u16 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
207u32 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
232void 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
259void 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
3e31e934 276 psxCpu->Clear((mem & (~3)), 1);
ef79bbde
P
277#endif
278 } else {
279#ifdef PSXMEM_LOG
280 PSXMEM_LOG("err sh %8.8lx\n", mem);
281#endif
282 }
283 }
284}
285
286void 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
345void *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}