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