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