b37985e91cdedf0bb4444220e93045ce86e56584
[picodrive.git] / pico / carthw / svp / memory.c
1 /*
2  * The SVP chip emulator, mem I/O stuff
3  *
4  * Copyright (c) GraÅžvydas "notaz" Ignotas, 2008
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of the organization nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "../../pico_int.h"
30 #include "../../memory.h"
31
32 // for wait loop det
33 static void PicoWrite16_dram(u32 a, u32 d)
34 {
35   a &= ~0xfe0000;
36
37   if (d != 0) {
38     if      (a == 0xfe06) // 30fe06
39       svp->ssp1601.emu_status &= ~SSP_WAIT_30FE06;
40     else if (a == 0xfe08)
41       svp->ssp1601.emu_status &= ~SSP_WAIT_30FE08;
42   }
43
44   ((u16 *)svp->dram)[a / 2] = d;
45 }
46
47 // "cell arrange" 1: 390000-39ffff
48 static u32 PicoRead16_svpca1(u32 a)
49 {
50   // this is 68k code rewritten
51   u32 a1 = a >> 1;
52   a1 = (a1 & 0x7001) | ((a1 & 0x3e) << 6) | ((a1 & 0xfc0) >> 5);
53   return ((u16 *)svp->dram)[a1];
54 }
55
56 // "cell arrange" 2: 3a0000-3affff
57 static u32 PicoRead16_svpca2(u32 a)
58 {
59   u32 a1 = a >> 1;
60   a1 = (a1 & 0x7801) | ((a1 & 0x1e) << 6) | ((a1 & 0x7e0) >> 4);
61   return ((u16 *)svp->dram)[a1];
62 }
63
64 // IO/control area (0xa10000 - 0xa1ffff)
65 static u32 PicoRead16_svpr(u32 a)
66 {
67   u32 d = 0;
68
69   // regs
70   if ((a & ~0x0f) == 0xa15000) {
71     switch (a & 0xf) {
72       case 0:
73       case 2:
74         d = svp->ssp1601.gr[SSP_XST].h;
75         break;
76
77       case 4:
78         d = svp->ssp1601.gr[SSP_PM0].h;
79         svp->ssp1601.gr[SSP_PM0].h &= ~1;
80         break;
81     }
82
83 #if EL_LOGMASK & EL_SVP
84     {
85       static int a15004_looping = 0;
86       if (a == 0xa15004 && (d & 1))
87         a15004_looping = 0;
88
89       if (!a15004_looping)
90         elprintf(EL_SVP, "SVP r%i: [%06x] %04x @%06x", realsize, a, d, SekPc);
91
92       if (a == 0xa15004 && !(d&1)) {
93         if (!a15004_looping)
94           elprintf(EL_SVP, "SVP det TIGHT loop: a15004");
95         a15004_looping = 1;
96       }
97       else
98         a15004_looping = 0;
99     }
100 #endif
101     return d;
102   }
103
104   //if (a == 0x30fe02 && d == 0)
105   //  elprintf(EL_ANOMALY, "SVP lag?");
106
107   return PicoRead16_io(a);
108 }
109
110 // used in VR test mode
111 static u32 PicoRead8_svpr(u32 a)
112 {
113   u32 d;
114
115   if ((a & ~0x0f) != 0xa15000)
116     return PicoRead8_io(a);
117
118   d = PicoRead16_svpr(a & ~1);
119   if (!(a & 1))
120     d >>= 8;
121   return d;
122 }
123
124 static void PicoWrite16_svpr(u32 a, u32 d)
125 {
126   elprintf(EL_SVP, "SVP w16: [%06x] %04x @%06x", a, d, SekPc);
127
128   if ((a & ~0x0f) == 0xa15000) {
129     if (a == 0xa15000 || a == 0xa15002) {
130       // just guessing here
131       svp->ssp1601.gr[SSP_XST].h = d;
132       svp->ssp1601.gr[SSP_PM0].h |= 2;
133       svp->ssp1601.emu_status &= ~SSP_WAIT_PM0;
134     }
135     //else if (a == 0xa15006) svp->ssp1601.gr[SSP_PM0].h = d | (d << 1);
136     // 0xa15006 probably has 'halt'
137     return;
138   }
139
140   PicoWrite16_io(a, d);
141 }
142
143 void PicoSVPMemSetup(void)
144 {
145   // 68k memmap:
146   // DRAM
147   cpu68k_map_set(m68k_read8_map,   0x300000, 0x31ffff, svp->dram, 0);
148   cpu68k_map_set(m68k_read16_map,  0x300000, 0x31ffff, svp->dram, 0);
149   cpu68k_map_set(m68k_write8_map,  0x300000, 0x31ffff, svp->dram, 0);
150   cpu68k_map_set(m68k_write16_map, 0x300000, 0x31ffff, svp->dram, 0);
151   cpu68k_map_set(m68k_write16_map, 0x300000, 0x30ffff, PicoWrite16_dram, 1);
152
153   // DRAM (cell arrange)
154   cpu68k_map_set(m68k_read16_map,  0x390000, 0x39ffff, PicoRead16_svpca1, 1);
155   cpu68k_map_set(m68k_read16_map,  0x3a0000, 0x3affff, PicoRead16_svpca2, 1);
156
157   // regs
158   cpu68k_map_set(m68k_read8_map,   0xa10000, 0xa1ffff, PicoRead8_svpr, 1);
159   cpu68k_map_set(m68k_read16_map,  0xa10000, 0xa1ffff, PicoRead16_svpr, 1);
160   cpu68k_map_set(m68k_write8_map,  0xa10000, 0xa1ffff, PicoWrite8_io, 1); // PicoWrite8_svpr
161   cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_svpr, 1);
162 }
163