1 /***************************************************************************
2 * Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team *
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. *
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. *
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 ***************************************************************************/
21 * PSX memory functions.
24 // TODO: Implement caches & cycle penalty.
27 #include "psxmem_map.h"
35 #define MAP_ANONYMOUS MAP_ANON
38 void *(*psxMapHook)(unsigned long addr, size_t size, int is_fixed,
40 void (*psxUnmapHook)(void *ptr, size_t size, enum psxMapTag tag);
42 void *psxMap(unsigned long addr, size_t size, int is_fixed,
46 int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
48 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
56 if (psxMapHook != NULL) {
57 ret = psxMapHook(addr, size, 0, tag);
62 /* avoid MAP_FIXED, it overrides existing mappings.. */
64 flags |= MAP_FIXED; */
67 ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
68 if (ret == MAP_FAILED)
72 if (addr != 0 && ret != (void *)addr) {
73 SysMessage("psxMap: warning: wanted to map @%08x, got %p\n",
77 psxUnmap(ret, size, tag);
81 if (((addr ^ (unsigned long)ret) & ~0xff000000l) && try_ < 2)
83 psxUnmap(ret, size, tag);
85 // try to use similarly aligned memory instead
86 // (recompiler needs this)
87 mask = try_ ? 0xffff : 0xffffff;
88 addr = ((unsigned long)ret + mask) & ~mask;
97 void psxUnmap(void *ptr, size_t size, enum psxMapTag tag)
99 if (psxUnmapHook != NULL) {
100 psxUnmapHook(ptr, size, tag);
108 s8 *psxM = NULL; // Kernel & User Memory (2 Meg)
109 s8 *psxP = NULL; // Parallel Port (64K)
110 s8 *psxR = NULL; // BIOS ROM (512K)
111 s8 *psxH = NULL; // Scratch Pad (1K) & Hardware Registers (8K)
113 u8 **psxMemWLUT = NULL;
114 u8 **psxMemRLUT = NULL;
116 /* Playstation Memory Map (from Playstation doc by Joshua Walker)
117 0x0000_0000-0x0000_ffff Kernel (64K)
118 0x0001_0000-0x001f_ffff User Memory (1.9 Meg)
120 0x1f00_0000-0x1f00_ffff Parallel Port (64K)
122 0x1f80_0000-0x1f80_03ff Scratch Pad (1024 bytes)
124 0x1f80_1000-0x1f80_2fff Hardware Registers (8K)
126 0x1fc0_0000-0x1fc7_ffff BIOS (512K)
128 0x8000_0000-0x801f_ffff Kernel and User Memory Mirror (2 Meg) Cached
129 0x9fc0_0000-0x9fc7_ffff BIOS Mirror (512K) Cached
131 0xa000_0000-0xa01f_ffff Kernel and User Memory Mirror (2 Meg) Uncached
132 0xbfc0_0000-0xbfc7_ffff BIOS Mirror (512K) Uncached
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 *));
144 psxM = psxMap(0x30000000, 0x00210000, 1, MAP_TAG_RAM);
146 psxM = psxMap(0x80000000, 0x00210000, 1, MAP_TAG_RAM);
150 psxM = psxMap(0x77000000, 0x00210000, 0, MAP_TAG_RAM);
153 SysMessage(_("mapping main RAM failed"));
157 psxP = &psxM[0x200000];
159 psxH = psxMap(0x4f800000, 0x10000, 0, MAP_TAG_OTHER);
160 psxR = psxMap(0x4fc00000, 0x80000, 0, MAP_TAG_OTHER);
162 psxH = psxMap(0x1f800000, 0x10000, 0, MAP_TAG_OTHER);
163 psxR = psxMap(0x1fc00000, 0x80000, 0, MAP_TAG_OTHER);
166 if (psxMemRLUT == NULL || psxMemWLUT == NULL ||
167 psxR == NULL || psxP == NULL || psxH == NULL) {
168 SysMessage(_("Error allocating memory!"));
174 for (i = 0; i < 0x80; i++) psxMemRLUT[i + 0x0000] = (u8 *)&psxM[(i & 0x1f) << 16];
176 memcpy(psxMemRLUT + 0x8000, psxMemRLUT, 0x80 * sizeof(void *));
177 memcpy(psxMemRLUT + 0xa000, psxMemRLUT, 0x80 * sizeof(void *));
179 psxMemRLUT[0x1f00] = (u8 *)psxP;
180 psxMemRLUT[0x1f80] = (u8 *)psxH;
182 for (i = 0; i < 0x08; i++) psxMemRLUT[i + 0x1fc0] = (u8 *)&psxR[i << 16];
184 memcpy(psxMemRLUT + 0x9fc0, psxMemRLUT + 0x1fc0, 0x08 * sizeof(void *));
185 memcpy(psxMemRLUT + 0xbfc0, psxMemRLUT + 0x1fc0, 0x08 * sizeof(void *));
188 for (i = 0; i < 0x80; i++) psxMemWLUT[i + 0x0000] = (u8 *)&psxM[(i & 0x1f) << 16];
190 memcpy(psxMemWLUT + 0x8000, psxMemWLUT, 0x80 * sizeof(void *));
191 memcpy(psxMemWLUT + 0xa000, psxMemWLUT, 0x80 * sizeof(void *));
193 psxMemWLUT[0x1f00] = (u8 *)psxP;
194 psxMemWLUT[0x1f80] = (u8 *)psxH;
203 memset(psxM, 0, 0x00200000);
204 memset(psxP, 0xff, 0x00010000);
206 if (strcmp(Config.Bios, "HLE") != 0) {
207 sprintf(bios, "%s/%s", Config.BiosDir, Config.Bios);
208 f = fopen(bios, "rb");
211 SysMessage(_("Could not open BIOS:\"%s\". Enabling HLE Bios!\n"), bios);
212 memset(psxR, 0, 0x80000);
215 fread(psxR, 1, 0x80000, f);
219 } else Config.HLE = TRUE;
222 void psxMemShutdown() {
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;
227 free(psxMemRLUT); psxMemRLUT = NULL;
228 free(psxMemWLUT); psxMemWLUT = NULL;
231 static int writeok = 1;
233 u8 psxMemRead8(u32 mem) {
238 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
239 if ((mem & 0xffff) < 0x400)
242 return psxHwRead8(mem);
244 p = (char *)(psxMemRLUT[t]);
247 DebugCheckBP((mem & 0xffffff) | 0x80000000, R1);
248 return *(u8 *)(p + (mem & 0xffff));
251 PSXMEM_LOG("err lb %8.8lx\n", mem);
258 u16 psxMemRead16(u32 mem) {
263 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
264 if ((mem & 0xffff) < 0x400)
267 return psxHwRead16(mem);
269 p = (char *)(psxMemRLUT[t]);
272 DebugCheckBP((mem & 0xffffff) | 0x80000000, R2);
273 return SWAPu16(*(u16 *)(p + (mem & 0xffff)));
276 PSXMEM_LOG("err lh %8.8lx\n", mem);
283 u32 psxMemRead32(u32 mem) {
288 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
289 if ((mem & 0xffff) < 0x400)
292 return psxHwRead32(mem);
294 p = (char *)(psxMemRLUT[t]);
297 DebugCheckBP((mem & 0xffffff) | 0x80000000, R4);
298 return SWAPu32(*(u32 *)(p + (mem & 0xffff)));
301 if (writeok) { PSXMEM_LOG("err lw %8.8lx\n", mem); }
308 void psxMemWrite8(u32 mem, u8 value) {
313 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
314 if ((mem & 0xffff) < 0x400)
317 psxHwWrite8(mem, value);
319 p = (char *)(psxMemWLUT[t]);
322 DebugCheckBP((mem & 0xffffff) | 0x80000000, W1);
323 *(u8 *)(p + (mem & 0xffff)) = value;
325 psxCpu->Clear((mem & (~3)), 1);
329 PSXMEM_LOG("err sb %8.8lx\n", mem);
335 void psxMemWrite16(u32 mem, u16 value) {
340 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
341 if ((mem & 0xffff) < 0x400)
342 psxHu16ref(mem) = SWAPu16(value);
344 psxHwWrite16(mem, value);
346 p = (char *)(psxMemWLUT[t]);
349 DebugCheckBP((mem & 0xffffff) | 0x80000000, W2);
350 *(u16 *)(p + (mem & 0xffff)) = SWAPu16(value);
352 psxCpu->Clear((mem & (~3)), 1);
356 PSXMEM_LOG("err sh %8.8lx\n", mem);
362 void psxMemWrite32(u32 mem, u32 value) {
366 // if ((mem&0x1fffff) == 0x71E18 || value == 0x48088800) SysPrintf("t2fix!!\n");
368 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
369 if ((mem & 0xffff) < 0x400)
370 psxHu32ref(mem) = SWAPu32(value);
372 psxHwWrite32(mem, value);
374 p = (char *)(psxMemWLUT[t]);
377 DebugCheckBP((mem & 0xffffff) | 0x80000000, W4);
378 *(u32 *)(p + (mem & 0xffff)) = SWAPu32(value);
380 psxCpu->Clear(mem, 1);
383 if (mem != 0xfffe0130) {
386 psxCpu->Clear(mem, 1);
390 if (writeok) { PSXMEM_LOG("err sw %8.8lx\n", mem); }
396 case 0x800: case 0x804:
397 if (writeok == 0) break;
399 memset(psxMemWLUT + 0x0000, 0, 0x80 * sizeof(void *));
400 memset(psxMemWLUT + 0x8000, 0, 0x80 * sizeof(void *));
401 memset(psxMemWLUT + 0xa000, 0, 0x80 * sizeof(void *));
403 case 0x00: case 0x1e988:
404 if (writeok == 1) break;
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 *));
412 PSXMEM_LOG("unk %8.8lx = %x\n", mem, value);
421 void *psxMemPointer(u32 mem) {
426 if (t == 0x1f80 || t == 0x9f80 || t == 0xbf80) {
427 if ((mem & 0xffff) < 0x400)
428 return (void *)&psxH[mem];
432 p = (char *)(psxMemWLUT[t]);
434 return (void *)(p + (mem & 0xffff));