reduce some code duplication
[pcsx_rearmed.git] / libpcsxcore / new_dynarec / pcsxmem.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2010-2011
3  *
4  * This work is licensed under the terms of GNU GPL version 2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7
8 #include <stdio.h>
9 #include "../psxhw.h"
10 #include "../cdrom.h"
11 #include "../mdec.h"
12 #include "../gpu.h"
13 #include "../psxmem_map.h"
14 #include "emu_if.h"
15 #include "pcsxmem.h"
16
17 #ifdef __thumb__
18 #error the dynarec is incompatible with Thumb functions,
19 #error please add -marm to compile flags
20 #endif
21
22 //#define memprintf printf
23 #define memprintf(...)
24
25 static uintptr_t *mem_readtab;
26 static uintptr_t *mem_writetab;
27 static uintptr_t mem_iortab[(1+2+4) * 0x1000 / 4];
28 static uintptr_t mem_iowtab[(1+2+4) * 0x1000 / 4];
29 static uintptr_t mem_ffrtab[(1+2+4) * 0x1000 / 4];
30 static uintptr_t mem_ffwtab[(1+2+4) * 0x1000 / 4];
31 //static uintptr_t mem_unmrtab[(1+2+4) * 0x1000 / 4];
32 static uintptr_t mem_unmwtab[(1+2+4) * 0x1000 / 4];
33
34 static
35 #ifdef __clang__
36 // When this is called in a loop, and 'h' is a function pointer, clang will crash.
37 __attribute__ ((noinline))
38 #endif
39 void map_item(uintptr_t *out, const void *h, uintptr_t flag)
40 {
41         uintptr_t hv = (uintptr_t)h;
42         if (hv & 1) {
43                 SysPrintf("FATAL: %p has LSB set\n", h);
44                 abort();
45         }
46         *out = (hv >> 1) | (flag << (sizeof(hv) * 8 - 1));
47 }
48
49 // size must be power of 2, at least 4k
50 #define map_l1_mem(tab, i, addr, size, base) \
51         map_item(&tab[((u32)(addr) >> 12) + i], \
52                  (u8 *)(base) - (u32)((addr) + ((i << 12) & ~(size - 1))), 0)
53
54 #define IOMEM32(a) (((a) & 0xfff) / 4)
55 #define IOMEM16(a) (0x1000/4 + (((a) & 0xfff) / 2))
56 #define IOMEM8(a)  (0x1000/4 + 0x1000/2 + ((a) & 0xfff))
57
58 u32 zero_mem[0x1000/4];
59 static u32 ffff_mem[0x1000/4];
60
61 static u32 read_mem_dummy(u32 addr)
62 {
63         // use 'addr' and not 'address', yes the api is weird...
64         memprintf("unmapped r %08x @%08x %u\n", addr, psxRegs.pc, psxRegs.cycle);
65         return 0xffffffff;
66 }
67
68 static void write_mem_dummy(u32 data)
69 {
70         if (!(psxRegs.CP0.n.SR & (1 << 16)))
71                 memprintf("unmapped w %08x, %08x @%08x %u\n",
72                           address, data, psxRegs.pc, psxRegs.cycle);
73 }
74
75 /* IO handlers */
76 static u32 io_read_sio16()
77 {
78         return sioRead8() | (sioRead8() << 8);
79 }
80
81 static u32 io_read_sio32()
82 {
83         return sioRead8() | (sioRead8() << 8) | (sioRead8() << 16) | (sioRead8() << 24);
84 }
85
86 static void io_write_sio16(u32 value)
87 {
88         sioWrite8((unsigned char)value);
89         sioWrite8((unsigned char)(value>>8));
90 }
91
92 static void io_write_sio32(u32 value)
93 {
94         sioWrite8((unsigned char)value);
95         sioWrite8((unsigned char)(value >>  8));
96         sioWrite8((unsigned char)(value >> 16));
97         sioWrite8((unsigned char)(value >> 24));
98 }
99
100 #if !defined(DRC_DBG) && defined(__arm__)
101
102 static void map_rcnt_rcount0(u32 mode)
103 {
104         if (mode & 0x100) { // pixel clock
105                 map_item(&mem_iortab[IOMEM32(0x1100)], rcnt0_read_count_m1, 1);
106                 map_item(&mem_iortab[IOMEM16(0x1100)], rcnt0_read_count_m1, 1);
107         }
108         else {
109                 map_item(&mem_iortab[IOMEM32(0x1100)], rcnt0_read_count_m0, 1);
110                 map_item(&mem_iortab[IOMEM16(0x1100)], rcnt0_read_count_m0, 1);
111         }
112 }
113
114 static void map_rcnt_rcount1(u32 mode)
115 {
116         if (mode & 0x100) { // hcnt
117                 map_item(&mem_iortab[IOMEM32(0x1110)], rcnt1_read_count_m1, 1);
118                 map_item(&mem_iortab[IOMEM16(0x1110)], rcnt1_read_count_m1, 1);
119         }
120         else {
121                 map_item(&mem_iortab[IOMEM32(0x1110)], rcnt1_read_count_m0, 1);
122                 map_item(&mem_iortab[IOMEM16(0x1110)], rcnt1_read_count_m0, 1);
123         }
124 }
125
126 static void map_rcnt_rcount2(u32 mode)
127 {
128         if (mode & 0x01) { // gate
129                 map_item(&mem_iortab[IOMEM32(0x1120)], &psxH[0x1000], 0);
130                 map_item(&mem_iortab[IOMEM16(0x1120)], &psxH[0x1000], 0);
131         }
132         else if (mode & 0x200) { // clk/8
133                 map_item(&mem_iortab[IOMEM32(0x1120)], rcnt2_read_count_m1, 1);
134                 map_item(&mem_iortab[IOMEM16(0x1120)], rcnt2_read_count_m1, 1);
135         }
136         else {
137                 map_item(&mem_iortab[IOMEM32(0x1120)], rcnt2_read_count_m0, 1);
138                 map_item(&mem_iortab[IOMEM16(0x1120)], rcnt2_read_count_m0, 1);
139         }
140 }
141
142 #else
143 #define map_rcnt_rcount0(mode)
144 #define map_rcnt_rcount1(mode)
145 #define map_rcnt_rcount2(mode)
146 #endif
147
148 #define make_rcnt_funcs(i) \
149 static u32 io_rcnt_read_count##i()  { return psxRcntRcount(i); } \
150 static u32 io_rcnt_read_mode##i()   { return psxRcntRmode(i); } \
151 static u32 io_rcnt_read_target##i() { return psxRcntRtarget(i); } \
152 static void io_rcnt_write_count##i(u32 val)  { psxRcntWcount(i, val & 0xffff); } \
153 static void io_rcnt_write_mode##i(u32 val)   { psxRcntWmode(i, val); map_rcnt_rcount##i(val); } \
154 static void io_rcnt_write_target##i(u32 val) { psxRcntWtarget(i, val & 0xffff); }
155
156 make_rcnt_funcs(0)
157 make_rcnt_funcs(1)
158 make_rcnt_funcs(2)
159
160 #define make_dma_func(n) \
161 static void io_write_chcr##n(u32 value) \
162 { \
163         HW_DMA##n##_CHCR = value; \
164         if (value & 0x01000000 && HW_DMA_PCR & (8 << (n * 4))) { \
165                 psxDma##n(HW_DMA##n##_MADR, HW_DMA##n##_BCR, value); \
166         } \
167 }
168
169 make_dma_func(0)
170 make_dma_func(1)
171 make_dma_func(2)
172 make_dma_func(3)
173 make_dma_func(4)
174 make_dma_func(6)
175
176 static void io_spu_write16(u32 value)
177 {
178         // meh
179         SPU_writeRegister(address, value, psxRegs.cycle);
180 }
181
182 static void io_spu_write32(u32 value)
183 {
184         SPUwriteRegister wfunc = SPU_writeRegister;
185         u32 a = address;
186
187         wfunc(a, value & 0xffff, psxRegs.cycle);
188         wfunc(a + 2, value >> 16, psxRegs.cycle);
189 }
190
191 static u32 io_gpu_read_status(void)
192 {
193         u32 v;
194
195         // meh2, syncing for img bit, might want to avoid it..
196         gpuSyncPluginSR();
197         v = HW_GPU_STATUS;
198
199         // XXX: because of large timeslices can't use hSyncCount, using rough
200         // approximization instead. Perhaps better use hcounter code here or something.
201         if (hSyncCount < 240 && (HW_GPU_STATUS & PSXGPU_ILACE_BITS) != PSXGPU_ILACE_BITS)
202                 v |= PSXGPU_LCF & (psxRegs.cycle << 20);
203         return v;
204 }
205
206 static void io_gpu_write_status(u32 value)
207 {
208         GPU_writeStatus(value);
209         gpuSyncPluginSR();
210 }
211
212 void new_dyna_pcsx_mem_isolate(int enable)
213 {
214         int i;
215
216         // note: apparently 0xa0000000 uncached access still works,
217         // at least read does for sure, so assume write does too
218         memprintf("mem isolate %d\n", enable);
219         if (enable) {
220                 for (i = 0; i < (0x800000 >> 12); i++) {
221                         map_item(&mem_writetab[0x80000|i], mem_unmwtab, 1);
222                         map_item(&mem_writetab[0x00000|i], mem_unmwtab, 1);
223                         //map_item(&mem_writetab[0xa0000|i], mem_unmwtab, 1);
224                 }
225         }
226         else {
227                 for (i = 0; i < (0x800000 >> 12); i++) {
228                         map_l1_mem(mem_writetab, i, 0x80000000, 0x200000, psxM);
229                         map_l1_mem(mem_writetab, i, 0x00000000, 0x200000, psxM);
230                         map_l1_mem(mem_writetab, i, 0xa0000000, 0x200000, psxM);
231                 }
232         }
233 }
234
235 static u32 read_biu(u32 addr)
236 {
237         if (addr != 0xfffe0130)
238                 return read_mem_dummy(addr);
239
240  FILE *f = fopen("/tmp/psxbiu.bin", "wb");
241  fwrite(psxM, 1, 0x200000, f);
242  fclose(f);
243         memprintf("read_biu  %08x @%08x %u\n",
244                 psxRegs.biuReg, psxRegs.pc, psxRegs.cycle);
245         return psxRegs.biuReg;
246 }
247
248 static void write_biu(u32 value)
249 {
250         if (address != 0xfffe0130) {
251                 write_mem_dummy(value);
252                 return;
253         }
254
255         memprintf("write_biu %08x @%08x %u\n", value, psxRegs.pc, psxRegs.cycle);
256         psxRegs.biuReg = value;
257 }
258
259 void new_dyna_pcsx_mem_load_state(void)
260 {
261         map_rcnt_rcount0(rcnts[0].mode);
262         map_rcnt_rcount1(rcnts[1].mode);
263         map_rcnt_rcount2(rcnts[2].mode);
264 }
265
266 int pcsxmem_is_handler_dynamic(unsigned int addr)
267 {
268         if ((addr & 0xfffff000) != 0x1f801000)
269                 return 0;
270
271         addr &= 0xffff;
272         return addr == 0x1100 || addr == 0x1110 || addr == 0x1120;
273 }
274
275 void new_dyna_pcsx_mem_init(void)
276 {
277         int i;
278
279         memset(ffff_mem, 0xff, sizeof(ffff_mem));
280
281         // have to map these further to keep tcache close to .text
282         mem_readtab = psxMap(0x08000000, 0x200000 * sizeof(mem_readtab[0]), 0, MAP_TAG_LUTS);
283         if (mem_readtab == NULL) {
284                 SysPrintf("failed to map mem tables\n");
285                 exit(1);
286         }
287         mem_writetab = mem_readtab + 0x100000;
288
289         // 1st level lookup:
290         //   0: direct mem
291         //   1: use 2nd lookup
292         // 2nd level lookup:
293         //   0: direct mem variable
294         //   1: memhandler
295
296         // default/unmapped memhandlers
297         for (i = 0; i < 0x100000; i++) {
298                 //map_item(&mem_readtab[i], mem_unmrtab, 1);
299                 map_l1_mem(mem_readtab, i, 0, 0x1000, ffff_mem);
300                 map_item(&mem_writetab[i], mem_unmwtab, 1);
301         }
302
303         // RAM and it's mirrors
304         for (i = 0; i < (0x800000 >> 12); i++) {
305                 map_l1_mem(mem_readtab,  i, 0x80000000, 0x200000, psxM);
306                 map_l1_mem(mem_readtab,  i, 0x00000000, 0x200000, psxM);
307                 map_l1_mem(mem_readtab,  i, 0xa0000000, 0x200000, psxM);
308         }
309         new_dyna_pcsx_mem_isolate(0);
310
311         // BIOS and it's mirrors
312         for (i = 0; i < (0x80000 >> 12); i++) {
313                 map_l1_mem(mem_readtab, i, 0x1fc00000, 0x80000, psxR);
314                 map_l1_mem(mem_readtab, i, 0xbfc00000, 0x80000, psxR);
315         }
316
317         // scratchpad
318         map_l1_mem(mem_readtab, 0, 0x1f800000, 0x1000, psxH);
319         map_l1_mem(mem_readtab, 0, 0x9f800000, 0x1000, psxH);
320         map_l1_mem(mem_writetab, 0, 0x1f800000, 0x1000, psxH);
321         map_l1_mem(mem_writetab, 0, 0x9f800000, 0x1000, psxH);
322
323         // I/O
324         map_item(&mem_readtab[0x1f801000u >> 12], mem_iortab, 1);
325         map_item(&mem_readtab[0x9f801000u >> 12], mem_iortab, 1);
326         map_item(&mem_readtab[0xbf801000u >> 12], mem_iortab, 1);
327         map_item(&mem_writetab[0x1f801000u >> 12], mem_iowtab, 1);
328         map_item(&mem_writetab[0x9f801000u >> 12], mem_iowtab, 1);
329         map_item(&mem_writetab[0xbf801000u >> 12], mem_iowtab, 1);
330
331         // L2
332         // unmapped tables
333         for (i = 0; i < (1+2+4) * 0x1000 / 4; i++)
334                 map_item(&mem_unmwtab[i], write_mem_dummy, 1);
335
336         // fill IO tables
337         for (i = 0; i < 0x1000/4; i++) {
338                 map_item(&mem_iortab[i], &psxH[0x1000], 0);
339                 map_item(&mem_iowtab[i], &psxH[0x1000], 0);
340         }
341         for (; i < 0x1000/4 + 0x1000/2; i++) {
342                 map_item(&mem_iortab[i], &psxH[0x1000], 0);
343                 map_item(&mem_iowtab[i], &psxH[0x1000], 0);
344         }
345         for (; i < 0x1000/4 + 0x1000/2 + 0x1000; i++) {
346                 map_item(&mem_iortab[i], &psxH[0x1000], 0);
347                 map_item(&mem_iowtab[i], &psxH[0x1000], 0);
348         }
349
350         map_item(&mem_iortab[IOMEM32(0x1040)], io_read_sio32, 1);
351         map_item(&mem_iortab[IOMEM16(0x1044)], sioReadStat16, 1);
352         map_item(&mem_iortab[IOMEM32(0x1100)], io_rcnt_read_count0, 1);
353         map_item(&mem_iortab[IOMEM32(0x1104)], io_rcnt_read_mode0, 1);
354         map_item(&mem_iortab[IOMEM32(0x1108)], io_rcnt_read_target0, 1);
355         map_item(&mem_iortab[IOMEM32(0x1110)], io_rcnt_read_count1, 1);
356         map_item(&mem_iortab[IOMEM32(0x1114)], io_rcnt_read_mode1, 1);
357         map_item(&mem_iortab[IOMEM32(0x1118)], io_rcnt_read_target1, 1);
358         map_item(&mem_iortab[IOMEM32(0x1120)], io_rcnt_read_count2, 1);
359         map_item(&mem_iortab[IOMEM32(0x1124)], io_rcnt_read_mode2, 1);
360         map_item(&mem_iortab[IOMEM32(0x1128)], io_rcnt_read_target2, 1);
361 //      map_item(&mem_iortab[IOMEM32(0x1810)], GPU_readData, 1);
362         map_item(&mem_iortab[IOMEM32(0x1814)], io_gpu_read_status, 1);
363         map_item(&mem_iortab[IOMEM32(0x1820)], mdecRead0, 1);
364         map_item(&mem_iortab[IOMEM32(0x1824)], mdecRead1, 1);
365
366         map_item(&mem_iortab[IOMEM16(0x1040)], io_read_sio16, 1);
367         map_item(&mem_iortab[IOMEM16(0x1044)], sioReadStat16, 1);
368         map_item(&mem_iortab[IOMEM16(0x1048)], sioReadMode16, 1);
369         map_item(&mem_iortab[IOMEM16(0x104a)], sioReadCtrl16, 1);
370         map_item(&mem_iortab[IOMEM16(0x104e)], sioReadBaud16, 1);
371         map_item(&mem_iortab[IOMEM16(0x1100)], io_rcnt_read_count0, 1);
372         map_item(&mem_iortab[IOMEM16(0x1104)], io_rcnt_read_mode0, 1);
373         map_item(&mem_iortab[IOMEM16(0x1108)], io_rcnt_read_target0, 1);
374         map_item(&mem_iortab[IOMEM16(0x1110)], io_rcnt_read_count1, 1);
375         map_item(&mem_iortab[IOMEM16(0x1114)], io_rcnt_read_mode1, 1);
376         map_item(&mem_iortab[IOMEM16(0x1118)], io_rcnt_read_target1, 1);
377         map_item(&mem_iortab[IOMEM16(0x1120)], io_rcnt_read_count2, 1);
378         map_item(&mem_iortab[IOMEM16(0x1124)], io_rcnt_read_mode2, 1);
379         map_item(&mem_iortab[IOMEM16(0x1128)], io_rcnt_read_target2, 1);
380
381         map_item(&mem_iortab[IOMEM8(0x1040)], sioRead8, 1);
382         map_item(&mem_iortab[IOMEM8(0x1800)], cdrRead0, 1);
383         map_item(&mem_iortab[IOMEM8(0x1801)], cdrRead1, 1);
384         map_item(&mem_iortab[IOMEM8(0x1802)], cdrRead2, 1);
385         map_item(&mem_iortab[IOMEM8(0x1803)], cdrRead3, 1);
386
387         // write(u32 data)
388         map_item(&mem_iowtab[IOMEM32(0x1040)], io_write_sio32, 1);
389         map_item(&mem_iowtab[IOMEM32(0x1070)], psxHwWriteIstat, 1);
390         map_item(&mem_iowtab[IOMEM32(0x1074)], psxHwWriteImask, 1);
391         map_item(&mem_iowtab[IOMEM32(0x1088)], io_write_chcr0, 1);
392         map_item(&mem_iowtab[IOMEM32(0x1098)], io_write_chcr1, 1);
393         map_item(&mem_iowtab[IOMEM32(0x10a8)], io_write_chcr2, 1);
394         map_item(&mem_iowtab[IOMEM32(0x10b8)], io_write_chcr3, 1);
395         map_item(&mem_iowtab[IOMEM32(0x10c8)], io_write_chcr4, 1);
396         map_item(&mem_iowtab[IOMEM32(0x10e8)], io_write_chcr6, 1);
397         map_item(&mem_iowtab[IOMEM32(0x10f4)], psxHwWriteDmaIcr32, 1);
398         map_item(&mem_iowtab[IOMEM32(0x1100)], io_rcnt_write_count0, 1);
399         map_item(&mem_iowtab[IOMEM32(0x1104)], io_rcnt_write_mode0, 1);
400         map_item(&mem_iowtab[IOMEM32(0x1108)], io_rcnt_write_target0, 1);
401         map_item(&mem_iowtab[IOMEM32(0x1110)], io_rcnt_write_count1, 1);
402         map_item(&mem_iowtab[IOMEM32(0x1114)], io_rcnt_write_mode1, 1);
403         map_item(&mem_iowtab[IOMEM32(0x1118)], io_rcnt_write_target1, 1);
404         map_item(&mem_iowtab[IOMEM32(0x1120)], io_rcnt_write_count2, 1);
405         map_item(&mem_iowtab[IOMEM32(0x1124)], io_rcnt_write_mode2, 1);
406         map_item(&mem_iowtab[IOMEM32(0x1128)], io_rcnt_write_target2, 1);
407 //      map_item(&mem_iowtab[IOMEM32(0x1810)], GPU_writeData, 1);
408         map_item(&mem_iowtab[IOMEM32(0x1814)], io_gpu_write_status, 1);
409         map_item(&mem_iowtab[IOMEM32(0x1820)], mdecWrite0, 1);
410         map_item(&mem_iowtab[IOMEM32(0x1824)], mdecWrite1, 1);
411
412         map_item(&mem_iowtab[IOMEM16(0x1040)], io_write_sio16, 1);
413         map_item(&mem_iowtab[IOMEM16(0x1044)], sioWriteStat16, 1);
414         map_item(&mem_iowtab[IOMEM16(0x1048)], sioWriteMode16, 1);
415         map_item(&mem_iowtab[IOMEM16(0x104a)], sioWriteCtrl16, 1);
416         map_item(&mem_iowtab[IOMEM16(0x104e)], sioWriteBaud16, 1);
417         map_item(&mem_iowtab[IOMEM16(0x1070)], psxHwWriteIstat, 1);
418         map_item(&mem_iowtab[IOMEM16(0x1074)], psxHwWriteImask, 1);
419         map_item(&mem_iowtab[IOMEM16(0x1100)], io_rcnt_write_count0, 1);
420         map_item(&mem_iowtab[IOMEM16(0x1104)], io_rcnt_write_mode0, 1);
421         map_item(&mem_iowtab[IOMEM16(0x1108)], io_rcnt_write_target0, 1);
422         map_item(&mem_iowtab[IOMEM16(0x1110)], io_rcnt_write_count1, 1);
423         map_item(&mem_iowtab[IOMEM16(0x1114)], io_rcnt_write_mode1, 1);
424         map_item(&mem_iowtab[IOMEM16(0x1118)], io_rcnt_write_target1, 1);
425         map_item(&mem_iowtab[IOMEM16(0x1120)], io_rcnt_write_count2, 1);
426         map_item(&mem_iowtab[IOMEM16(0x1124)], io_rcnt_write_mode2, 1);
427         map_item(&mem_iowtab[IOMEM16(0x1128)], io_rcnt_write_target2, 1);
428
429         map_item(&mem_iowtab[IOMEM8(0x1040)], sioWrite8, 1);
430         map_item(&mem_iowtab[IOMEM8(0x1800)], cdrWrite0, 1);
431         map_item(&mem_iowtab[IOMEM8(0x1801)], cdrWrite1, 1);
432         map_item(&mem_iowtab[IOMEM8(0x1802)], cdrWrite2, 1);
433         map_item(&mem_iowtab[IOMEM8(0x1803)], cdrWrite3, 1);
434
435         for (i = 0x1c00; i < 0x2000; i += 2) {
436                 map_item(&mem_iowtab[IOMEM16(i)], io_spu_write16, 1);
437                 map_item(&mem_iowtab[IOMEM32(i)], io_spu_write32, 1);
438         }
439
440         // misc
441         map_item(&mem_readtab[0xfffe0130u >> 12], mem_ffrtab, 1);
442         map_item(&mem_writetab[0xfffe0130u >> 12], mem_ffwtab, 1);
443         for (i = 0; i < 0x1000/4 + 0x1000/2 + 0x1000; i++) {
444                 map_item(&mem_ffrtab[i], read_biu, 1);
445                 map_item(&mem_ffwtab[i], write_biu, 1);
446         }
447
448         mem_rtab = mem_readtab;
449         mem_wtab = mem_writetab;
450
451         new_dyna_pcsx_mem_load_state();
452 }
453
454 void new_dyna_pcsx_mem_reset(void)
455 {
456         int i;
457
458         // plugins might change so update the pointers
459         map_item(&mem_iortab[IOMEM32(0x1810)], GPU_readData, 1);
460
461         for (i = 0x1c00; i < 0x2000; i += 2)
462                 map_item(&mem_iortab[IOMEM16(i)], SPU_readRegister, 1);
463
464         map_item(&mem_iowtab[IOMEM32(0x1810)], GPU_writeData, 1);
465 }
466
467 void new_dyna_pcsx_mem_shutdown(void)
468 {
469         psxUnmap(mem_readtab, 0x200000 * sizeof(mem_readtab[0]), MAP_TAG_LUTS);
470         mem_writetab = mem_readtab = NULL;
471 }