Finish migrating to new mem handling. Make carthw db external.
[picodrive.git] / pico / carthw / svp / memory.c
1 // The SVP chip emulator, mem I/O stuff
2
3 // (c) Copyright 2008, Grazvydas "notaz" Ignotas
4 // Free for non-commercial use.
5
6 // For commercial use, separate licencing terms must be obtained.
7
8
9 #include "../../pico_int.h"
10 #include "../../memory.h"
11
12 /*
13   // "cell arrange" 1: 390000-39ffff
14   else if ((a & 0xff0000) == 0x390000) {
15     // this is rewritten 68k code
16     unsigned int a1 = a >> 1;
17     a1 = (a1 & 0x7001) | ((a1 & 0x3e) << 6) | ((a1 & 0xfc0) >> 5);
18     d = ((u16 *)svp->dram)[a1];
19   }
20
21   // "cell arrange" 2: 3a0000-3affff
22   else if ((a & 0xff0000) == 0x3a0000) {
23     // this is rewritten 68k code
24     unsigned int a1 = a >> 1;
25     a1 = (a1 & 0x7801) | ((a1 & 0x1e) << 6) | ((a1 & 0x7e0) >> 4);
26     d = ((u16 *)svp->dram)[a1];
27   }
28 */
29
30 // IO/control area (0xa10000 - 0xa1ffff)
31 static u32 PicoRead16_svpr(u32 a)
32 {
33   u32 d = 0;
34
35   // regs
36   if ((a & 0xfffff0) == 0xa15000) {
37     switch (a & 0xf) {
38       case 0:
39       case 2:
40         d = svp->ssp1601.gr[SSP_XST].h;
41         break;
42
43       case 4:
44         d = svp->ssp1601.gr[SSP_PM0].h;
45         svp->ssp1601.gr[SSP_PM0].h &= ~1;
46         break;
47     }
48
49 #if EL_LOGMASK & EL_SVP
50     {
51       static int a15004_looping = 0;
52       if (a == 0xa15004 && (d & 1))
53         a15004_looping = 0;
54
55       if (!a15004_looping)
56         elprintf(EL_SVP, "SVP r%i: [%06x] %04x @%06x", realsize, a, d, SekPc);
57
58       if (a == 0xa15004 && !(d&1)) {
59         if (!a15004_looping)
60           elprintf(EL_SVP, "SVP det TIGHT loop: a15004");
61         a15004_looping = 1;
62       }
63       else
64         a15004_looping = 0;
65     }
66 #endif
67     return d;
68   }
69
70   //if (a == 0x30fe02 && d == 0)
71   //  elprintf(EL_ANOMALY, "SVP lag?");
72
73   return PicoRead16_io(a);
74 }
75
76 static void PicoWrite16_svpr(u32 a, u32 d)
77 {
78   elprintf(EL_SVP, "SVP w16: [%06x] %04x @%06x", a, d, SekPc);
79
80   if ((a & 0xfffff0) == 0xa15000) {
81     if (a == 0xa15000 || a == 0xa15002) {
82       // just guessing here
83       svp->ssp1601.gr[SSP_XST].h = d;
84       svp->ssp1601.gr[SSP_PM0].h |= 2;
85       svp->ssp1601.emu_status &= ~SSP_WAIT_PM0;
86     }
87     //else if (a == 0xa15006) svp->ssp1601.gr[SSP_PM0].h = d | (d << 1);
88     // 0xa15006 probably has 'halt'
89     return;
90   }
91
92   PicoWrite16_io(a, d);
93 /*
94   if (a == 0x30fe06 && d != 0)
95     svp->ssp1601.emu_status &= ~SSP_WAIT_30FE06;
96
97   if (a == 0x30fe08 && d != 0)
98     svp->ssp1601.emu_status &= ~SSP_WAIT_30FE08;
99 */
100 }
101
102 void PicoSVPMemSetup(void)
103 {
104   // 68k memmap:
105   // DRAM
106   cpu68k_map_set(m68k_read8_map,   0x300000, 0x31ffff, svp->dram, 0);
107   cpu68k_map_set(m68k_read16_map,  0x300000, 0x31ffff, svp->dram, 0);
108   cpu68k_map_set(m68k_write8_map,  0x300000, 0x31ffff, svp->dram, 0);
109   cpu68k_map_set(m68k_write16_map, 0x300000, 0x31ffff, svp->dram, 0);
110
111   // DRAM (cell arrange) - TODO
112
113   // regs
114   cpu68k_map_set(m68k_read8_map,   0xa10000, 0xa1ffff, PicoRead8_io, 1); // PicoRead8_svpr
115   cpu68k_map_set(m68k_read16_map,  0xa10000, 0xa1ffff, PicoRead16_svpr, 1);
116   cpu68k_map_set(m68k_write8_map,  0xa10000, 0xa1ffff, PicoWrite8_io, 1); // PicoWrite8_svpr
117   cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_svpr, 1);
118 }
119