cff531af |
1 | /*\r |
2 | * Memory I/O handlers for Sega/Mega CD.\r |
3 | * (C) notaz, 2007-2009\r |
4 | *\r |
5 | * This work is licensed under the terms of MAME license.\r |
6 | * See COPYING file in the top-level directory.\r |
7 | */\r |
cc68a136 |
8 | \r |
efcba75f |
9 | #include "../pico_int.h"\r |
af37bca8 |
10 | #include "../memory.h"\r |
cc68a136 |
11 | \r |
cb4a513a |
12 | #include "gfx_cd.h"\r |
4f265db7 |
13 | #include "pcm.h"\r |
cb4a513a |
14 | \r |
bcf65fd6 |
15 | uptr s68k_read8_map [0x1000000 >> M68K_MEM_SHIFT];\r |
16 | uptr s68k_read16_map [0x1000000 >> M68K_MEM_SHIFT];\r |
17 | uptr s68k_write8_map [0x1000000 >> M68K_MEM_SHIFT];\r |
18 | uptr s68k_write16_map[0x1000000 >> M68K_MEM_SHIFT];\r |
cc68a136 |
19 | \r |
af37bca8 |
20 | MAKE_68K_READ8(s68k_read8, s68k_read8_map)\r |
21 | MAKE_68K_READ16(s68k_read16, s68k_read16_map)\r |
22 | MAKE_68K_READ32(s68k_read32, s68k_read16_map)\r |
23 | MAKE_68K_WRITE8(s68k_write8, s68k_write8_map)\r |
24 | MAKE_68K_WRITE16(s68k_write16, s68k_write16_map)\r |
25 | MAKE_68K_WRITE32(s68k_write32, s68k_write16_map)\r |
b5e5172d |
26 | \r |
cc68a136 |
27 | // -----------------------------------------------------------------\r |
28 | \r |
0ace9b9a |
29 | // provided by ASM code:\r |
30 | #ifdef _ASM_CD_MEMORY_C\r |
31 | u32 PicoReadM68k8_io(u32 a);\r |
32 | u32 PicoReadM68k16_io(u32 a);\r |
33 | void PicoWriteM68k8_io(u32 a, u32 d);\r |
34 | void PicoWriteM68k16_io(u32 a, u32 d);\r |
35 | \r |
36 | u32 PicoReadS68k8_pr(u32 a);\r |
37 | u32 PicoReadS68k16_pr(u32 a);\r |
38 | void PicoWriteS68k8_pr(u32 a, u32 d);\r |
39 | void PicoWriteS68k16_pr(u32 a, u32 d);\r |
40 | \r |
41 | u32 PicoReadM68k8_cell0(u32 a);\r |
42 | u32 PicoReadM68k8_cell1(u32 a);\r |
43 | u32 PicoReadM68k16_cell0(u32 a);\r |
44 | u32 PicoReadM68k16_cell1(u32 a);\r |
45 | void PicoWriteM68k8_cell0(u32 a, u32 d);\r |
46 | void PicoWriteM68k8_cell1(u32 a, u32 d);\r |
47 | void PicoWriteM68k16_cell0(u32 a, u32 d);\r |
48 | void PicoWriteM68k16_cell1(u32 a, u32 d);\r |
49 | \r |
50 | u32 PicoReadS68k8_dec0(u32 a);\r |
51 | u32 PicoReadS68k8_dec1(u32 a);\r |
52 | u32 PicoReadS68k16_dec0(u32 a);\r |
53 | u32 PicoReadS68k16_dec1(u32 a);\r |
54 | void PicoWriteS68k8_dec_m0b0(u32 a, u32 d);\r |
55 | void PicoWriteS68k8_dec_m1b0(u32 a, u32 d);\r |
56 | void PicoWriteS68k8_dec_m2b0(u32 a, u32 d);\r |
57 | void PicoWriteS68k8_dec_m0b1(u32 a, u32 d);\r |
58 | void PicoWriteS68k8_dec_m1b1(u32 a, u32 d);\r |
59 | void PicoWriteS68k8_dec_m2b1(u32 a, u32 d);\r |
60 | void PicoWriteS68k16_dec_m0b0(u32 a, u32 d);\r |
61 | void PicoWriteS68k16_dec_m1b0(u32 a, u32 d);\r |
62 | void PicoWriteS68k16_dec_m2b0(u32 a, u32 d);\r |
63 | void PicoWriteS68k16_dec_m0b1(u32 a, u32 d);\r |
64 | void PicoWriteS68k16_dec_m1b1(u32 a, u32 d);\r |
65 | void PicoWriteS68k16_dec_m2b1(u32 a, u32 d);\r |
66 | #endif\r |
67 | \r |
68 | static void remap_prg_window(void);\r |
69 | static void remap_word_ram(int r3);\r |
70 | \r |
7a1f6e45 |
71 | // poller detection\r |
7a1f6e45 |
72 | #define POLL_LIMIT 16\r |
73 | #define POLL_CYCLES 124\r |
7a1f6e45 |
74 | unsigned int s68k_poll_adclk, s68k_poll_cnt;\r |
cc68a136 |
75 | \r |
4ff2d527 |
76 | #ifndef _ASM_CD_MEMORY_C\r |
cb4a513a |
77 | static u32 m68k_reg_read16(u32 a)\r |
cc68a136 |
78 | {\r |
79 | u32 d=0;\r |
80 | a &= 0x3e;\r |
cc68a136 |
81 | \r |
82 | switch (a) {\r |
672ad671 |
83 | case 0:\r |
c459aefd |
84 | d = ((Pico_mcd->s68k_regs[0x33]<<13)&0x8000) | Pico_mcd->m.busreq; // here IFL2 is always 0, just like in Gens\r |
672ad671 |
85 | goto end;\r |
cc68a136 |
86 | case 2:\r |
672ad671 |
87 | d = (Pico_mcd->s68k_regs[a]<<8) | (Pico_mcd->s68k_regs[a+1]&0xc7);\r |
af37bca8 |
88 | elprintf(EL_CDREG3, "m68k_regs r3: %02x @%06x", (u8)d, SekPc);\r |
cc68a136 |
89 | goto end;\r |
c459aefd |
90 | case 4:\r |
91 | d = Pico_mcd->s68k_regs[4]<<8;\r |
92 | goto end;\r |
93 | case 6:\r |
913ef4b7 |
94 | d = *(u16 *)(Pico_mcd->bios + 0x72);\r |
c459aefd |
95 | goto end;\r |
cc68a136 |
96 | case 8:\r |
cc68a136 |
97 | d = Read_CDC_Host(0);\r |
98 | goto end;\r |
c459aefd |
99 | case 0xA:\r |
ca61ee42 |
100 | elprintf(EL_UIO, "m68k FIXME: reserved read");\r |
c459aefd |
101 | goto end;\r |
cc68a136 |
102 | case 0xC:\r |
1cd356a3 |
103 | d = Pico_mcd->m.timer_stopwatch >> 16;\r |
af37bca8 |
104 | elprintf(EL_CDREGS, "m68k stopwatch timer read (%04x)", d);\r |
1cd356a3 |
105 | goto end;\r |
cc68a136 |
106 | }\r |
107 | \r |
cc68a136 |
108 | if (a < 0x30) {\r |
109 | // comm flag/cmd/status (0xE-0x2F)\r |
110 | d = (Pico_mcd->s68k_regs[a]<<8) | Pico_mcd->s68k_regs[a+1];\r |
111 | goto end;\r |
112 | }\r |
113 | \r |
ca61ee42 |
114 | elprintf(EL_UIO, "m68k_regs FIXME invalid read @ %02x", a);\r |
cc68a136 |
115 | \r |
116 | end:\r |
117 | \r |
cc68a136 |
118 | return d;\r |
119 | }\r |
4ff2d527 |
120 | #endif\r |
cc68a136 |
121 | \r |
4ff2d527 |
122 | #ifndef _ASM_CD_MEMORY_C\r |
123 | static\r |
124 | #endif\r |
125 | void m68k_reg_write8(u32 a, u32 d)\r |
cc68a136 |
126 | {\r |
af37bca8 |
127 | u32 dold;\r |
cc68a136 |
128 | a &= 0x3f;\r |
cc68a136 |
129 | \r |
130 | switch (a) {\r |
131 | case 0:\r |
672ad671 |
132 | d &= 1;\r |
ca61ee42 |
133 | if ((d&1) && (Pico_mcd->s68k_regs[0x33]&(1<<2))) { elprintf(EL_INTS, "m68k: s68k irq 2"); SekInterruptS68k(2); }\r |
c459aefd |
134 | return;\r |
cc68a136 |
135 | case 1:\r |
672ad671 |
136 | d &= 3;\r |
51a902ae |
137 | if (!(d&1)) Pico_mcd->m.state_flags |= 1; // reset pending, needed to be sure we fetch the right vectors on reset\r |
ef090115 |
138 | if ( (Pico_mcd->m.busreq&1) != (d&1)) elprintf(EL_INTSW, "m68k: s68k reset %i", !(d&1));\r |
139 | if ( (Pico_mcd->m.busreq&2) != (d&2)) elprintf(EL_INTSW, "m68k: s68k brq %i", (d&2)>>1);\r |
51a902ae |
140 | if ((Pico_mcd->m.state_flags&1) && (d&3)==1) {\r |
cc68a136 |
141 | SekResetS68k(); // S68k comes out of RESET or BRQ state\r |
4ff2d527 |
142 | Pico_mcd->m.state_flags&=~1;\r |
af37bca8 |
143 | elprintf(EL_CDREGS, "m68k: resetting s68k, cycles=%i", SekCyclesLeft);\r |
cc68a136 |
144 | }\r |
af37bca8 |
145 | if (!(d & 1))\r |
146 | d |= 2; // verified: reset also gives bus\r |
147 | if ((d ^ Pico_mcd->m.busreq) & 2)\r |
0ace9b9a |
148 | remap_prg_window();\r |
c459aefd |
149 | Pico_mcd->m.busreq = d;\r |
150 | return;\r |
672ad671 |
151 | case 2:\r |
af37bca8 |
152 | elprintf(EL_CDREGS, "m68k: prg wp=%02x", d);\r |
672ad671 |
153 | Pico_mcd->s68k_regs[2] = d; // really use s68k side register\r |
154 | return;\r |
af37bca8 |
155 | case 3:\r |
156 | dold = Pico_mcd->s68k_regs[3];\r |
157 | elprintf(EL_CDREG3, "m68k_regs w3: %02x @%06x", (u8)d, SekPc);\r |
672ad671 |
158 | //if ((Pico_mcd->s68k_regs[3]&4) != (d&4)) dprintf("m68k: ram mode %i mbit", (d&4) ? 1 : 2);\r |
159 | //if ((Pico_mcd->s68k_regs[3]&2) != (d&2)) dprintf("m68k: %s", (d&4) ? ((d&2) ? "word swap req" : "noop?") :\r |
160 | // ((d&2) ? "word ram to s68k" : "word ram to m68k"));\r |
ef090115 |
161 | if (dold & 4) { // 1M mode\r |
162 | d ^= 2; // writing 0 to DMNA actually sets it, 1 does nothing\r |
66fdc0f0 |
163 | } else {\r |
ef090115 |
164 | if ((d ^ dold) & d & 2) { // DMNA is being set\r |
165 | dold &= ~1; // return word RAM to s68k\r |
166 | /* Silpheed hack: bset(w3), r3, btst, bne, r3 */\r |
167 | SekEndRun(20+16+10+12+16);\r |
168 | }\r |
66fdc0f0 |
169 | }\r |
af37bca8 |
170 | Pico_mcd->s68k_regs[3] = (d & 0xc2) | (dold & 0x1f);\r |
171 | if ((d ^ dold) & 0xc0) {\r |
172 | elprintf(EL_CDREGS, "m68k: prg bank: %i -> %i", (Pico_mcd->s68k_regs[a]>>6), ((d>>6)&3));\r |
0ace9b9a |
173 | remap_prg_window();\r |
af37bca8 |
174 | }\r |
7a1f6e45 |
175 | #ifdef USE_POLL_DETECT\r |
176 | if ((s68k_poll_adclk&0xfe) == 2 && s68k_poll_cnt > POLL_LIMIT) {\r |
177 | SekSetStopS68k(0); s68k_poll_adclk = 0;\r |
8f8fe01e |
178 | elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);\r |
7a1f6e45 |
179 | }\r |
180 | #endif\r |
672ad671 |
181 | return;\r |
c459aefd |
182 | case 6:\r |
d1df8786 |
183 | Pico_mcd->bios[0x72 + 1] = d; // simple hint vector changer\r |
c459aefd |
184 | return;\r |
185 | case 7:\r |
d1df8786 |
186 | Pico_mcd->bios[0x72] = d;\r |
af37bca8 |
187 | elprintf(EL_CDREGS, "hint vector set to %04x%04x",\r |
188 | ((u16 *)Pico_mcd->bios)[0x70/2], ((u16 *)Pico_mcd->bios)[0x72/2]);\r |
c459aefd |
189 | return;\r |
7a1f6e45 |
190 | case 0xf:\r |
191 | d = (d << 1) | ((d >> 7) & 1); // rol8 1 (special case)\r |
cc68a136 |
192 | case 0xe:\r |
672ad671 |
193 | //dprintf("m68k: comm flag: %02x", d);\r |
cc68a136 |
194 | Pico_mcd->s68k_regs[0xe] = d;\r |
7a1f6e45 |
195 | #ifdef USE_POLL_DETECT\r |
196 | if ((s68k_poll_adclk&0xfe) == 0xe && s68k_poll_cnt > POLL_LIMIT) {\r |
197 | SekSetStopS68k(0); s68k_poll_adclk = 0;\r |
8f8fe01e |
198 | elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);\r |
7a1f6e45 |
199 | }\r |
200 | #endif\r |
c459aefd |
201 | return;\r |
672ad671 |
202 | }\r |
203 | \r |
204 | if ((a&0xf0) == 0x10) {\r |
cc68a136 |
205 | Pico_mcd->s68k_regs[a] = d;\r |
7a1f6e45 |
206 | #ifdef USE_POLL_DETECT\r |
207 | if ((a&0xfe) == (s68k_poll_adclk&0xfe) && s68k_poll_cnt > POLL_LIMIT) {\r |
208 | SekSetStopS68k(0); s68k_poll_adclk = 0;\r |
8f8fe01e |
209 | elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);\r |
7a1f6e45 |
210 | }\r |
211 | #endif\r |
672ad671 |
212 | return;\r |
cc68a136 |
213 | }\r |
214 | \r |
ca61ee42 |
215 | elprintf(EL_UIO, "m68k FIXME: invalid write? [%02x] %02x", a, d);\r |
cc68a136 |
216 | }\r |
217 | \r |
2433f409 |
218 | #ifndef _ASM_CD_MEMORY_C\r |
219 | static\r |
220 | #endif\r |
221 | u32 s68k_poll_detect(u32 a, u32 d)\r |
222 | {\r |
223 | #ifdef USE_POLL_DETECT\r |
ca61ee42 |
224 | // needed mostly for Cyclone, which doesn't always check it's cycle counter\r |
225 | if (SekIsStoppedS68k()) return d;\r |
2433f409 |
226 | // polling detection\r |
227 | if (a == (s68k_poll_adclk&0xff)) {\r |
228 | unsigned int clkdiff = SekCyclesDoneS68k() - (s68k_poll_adclk>>8);\r |
229 | if (clkdiff <= POLL_CYCLES) {\r |
230 | s68k_poll_cnt++;\r |
231 | //printf("-- diff: %u, cnt = %i\n", clkdiff, s68k_poll_cnt);\r |
232 | if (s68k_poll_cnt > POLL_LIMIT) {\r |
233 | SekSetStopS68k(1);\r |
8f8fe01e |
234 | elprintf(EL_CDPOLL, "s68k poll detected @ %06x, a=%02x", SekPcS68k, a);\r |
2433f409 |
235 | }\r |
236 | s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a;\r |
237 | return d;\r |
238 | }\r |
239 | }\r |
240 | s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a;\r |
241 | s68k_poll_cnt = 0;\r |
242 | #endif\r |
243 | return d;\r |
244 | }\r |
cc68a136 |
245 | \r |
913ef4b7 |
246 | #define READ_FONT_DATA(basemask) \\r |
247 | { \\r |
248 | unsigned int fnt = *(unsigned int *)(Pico_mcd->s68k_regs + 0x4c); \\r |
249 | unsigned int col0 = (fnt >> 8) & 0x0f, col1 = (fnt >> 12) & 0x0f; \\r |
250 | if (fnt & (basemask << 0)) d = col1 ; else d = col0; \\r |
251 | if (fnt & (basemask << 1)) d |= col1 << 4; else d |= col0 << 4; \\r |
252 | if (fnt & (basemask << 2)) d |= col1 << 8; else d |= col0 << 8; \\r |
253 | if (fnt & (basemask << 3)) d |= col1 << 12; else d |= col0 << 12; \\r |
254 | }\r |
255 | \r |
cc68a136 |
256 | \r |
4ff2d527 |
257 | #ifndef _ASM_CD_MEMORY_C\r |
258 | static\r |
259 | #endif\r |
260 | u32 s68k_reg_read16(u32 a)\r |
cc68a136 |
261 | {\r |
262 | u32 d=0;\r |
cc68a136 |
263 | \r |
cc68a136 |
264 | switch (a) {\r |
265 | case 0:\r |
7a1f6e45 |
266 | return ((Pico_mcd->s68k_regs[0]&3)<<8) | 1; // ver = 0, not in reset state\r |
672ad671 |
267 | case 2:\r |
2433f409 |
268 | d = (Pico_mcd->s68k_regs[2]<<8) | (Pico_mcd->s68k_regs[3]&0x1f);\r |
af37bca8 |
269 | elprintf(EL_CDREG3, "s68k_regs r3: %02x @%06x", (u8)d, SekPcS68k);\r |
2433f409 |
270 | return s68k_poll_detect(a, d);\r |
cc68a136 |
271 | case 6:\r |
7a1f6e45 |
272 | return CDC_Read_Reg();\r |
cc68a136 |
273 | case 8:\r |
7a1f6e45 |
274 | return Read_CDC_Host(1); // Gens returns 0 here on byte reads\r |
cc68a136 |
275 | case 0xC:\r |
4f265db7 |
276 | d = Pico_mcd->m.timer_stopwatch >> 16;\r |
af37bca8 |
277 | elprintf(EL_CDREGS, "s68k stopwatch timer read (%04x)", d);\r |
7a1f6e45 |
278 | return d;\r |
d1df8786 |
279 | case 0x30:\r |
af37bca8 |
280 | elprintf(EL_CDREGS, "s68k int3 timer read (%02x)", Pico_mcd->s68k_regs[31]);\r |
7a1f6e45 |
281 | return Pico_mcd->s68k_regs[31];\r |
cc68a136 |
282 | case 0x34: // fader\r |
7a1f6e45 |
283 | return 0; // no busy bit\r |
913ef4b7 |
284 | case 0x50: // font data (check: Lunar 2, Silpheed)\r |
285 | READ_FONT_DATA(0x00100000);\r |
7a1f6e45 |
286 | return d;\r |
913ef4b7 |
287 | case 0x52:\r |
288 | READ_FONT_DATA(0x00010000);\r |
7a1f6e45 |
289 | return d;\r |
913ef4b7 |
290 | case 0x54:\r |
291 | READ_FONT_DATA(0x10000000);\r |
7a1f6e45 |
292 | return d;\r |
913ef4b7 |
293 | case 0x56:\r |
294 | READ_FONT_DATA(0x01000000);\r |
7a1f6e45 |
295 | return d;\r |
cc68a136 |
296 | }\r |
297 | \r |
298 | d = (Pico_mcd->s68k_regs[a]<<8) | Pico_mcd->s68k_regs[a+1];\r |
299 | \r |
2433f409 |
300 | if (a >= 0x0e && a < 0x30)\r |
301 | return s68k_poll_detect(a, d);\r |
7a1f6e45 |
302 | \r |
cc68a136 |
303 | return d;\r |
304 | }\r |
305 | \r |
4ff2d527 |
306 | #ifndef _ASM_CD_MEMORY_C\r |
307 | static\r |
308 | #endif\r |
309 | void s68k_reg_write8(u32 a, u32 d)\r |
cc68a136 |
310 | {\r |
48e8482f |
311 | // Warning: d might have upper bits set\r |
cc68a136 |
312 | switch (a) {\r |
672ad671 |
313 | case 2:\r |
314 | return; // only m68k can change WP\r |
fa1e5e29 |
315 | case 3: {\r |
316 | int dold = Pico_mcd->s68k_regs[3];\r |
af37bca8 |
317 | elprintf(EL_CDREG3, "s68k_regs w3: %02x @%06x", (u8)d, SekPcS68k);\r |
672ad671 |
318 | d &= 0x1d;\r |
af37bca8 |
319 | d |= dold & 0xc2;\r |
320 | if (d & 4)\r |
39230401 |
321 | {\r |
0ace9b9a |
322 | if ((d ^ dold) & 0x1d) {\r |
4ff2d527 |
323 | d &= ~2; // in case of mode or bank change we clear DMNA (m68k req) bit\r |
0ace9b9a |
324 | remap_word_ram(d);\r |
4ff2d527 |
325 | }\r |
fa1e5e29 |
326 | if (!(dold & 4)) {\r |
af37bca8 |
327 | elprintf(EL_CDREG3, "wram mode 2M->1M");\r |
fa1e5e29 |
328 | wram_2M_to_1M(Pico_mcd->word_ram2M);\r |
4ff2d527 |
329 | }\r |
39230401 |
330 | }\r |
331 | else\r |
332 | {\r |
fa1e5e29 |
333 | if (dold & 4) {\r |
af37bca8 |
334 | elprintf(EL_CDREG3, "wram mode 1M->2M");\r |
4ff2d527 |
335 | if (!(d&1)) { // it didn't set the ret bit, which means it doesn't want to give WRAM to m68k\r |
336 | d &= ~3;\r |
337 | d |= (dold&1) ? 2 : 1; // then give it to the one which had bank0 in 1M mode\r |
338 | }\r |
fa1e5e29 |
339 | wram_1M_to_2M(Pico_mcd->word_ram2M);\r |
0ace9b9a |
340 | remap_word_ram(d);\r |
4ff2d527 |
341 | }\r |
ef090115 |
342 | // s68k can only set RET, writing 0 has no effect\r |
07ceafdb |
343 | else if ((dold ^ d) & d & 1) { // RET being set\r |
344 | SekEndRunS68k(20+16+10+12+16); // see DMNA case\r |
345 | } else\r |
346 | d |= dold & 1;\r |
347 | if (d & 1)\r |
348 | d &= ~2; // DMNA clears\r |
d0d47c5b |
349 | }\r |
672ad671 |
350 | break;\r |
fa1e5e29 |
351 | }\r |
cc68a136 |
352 | case 4:\r |
af37bca8 |
353 | elprintf(EL_CDREGS, "s68k CDC dest: %x", d&7);\r |
cc68a136 |
354 | Pico_mcd->s68k_regs[4] = (Pico_mcd->s68k_regs[4]&0xC0) | (d&7); // CDC mode\r |
355 | return;\r |
356 | case 5:\r |
c459aefd |
357 | //dprintf("s68k CDC reg addr: %x", d&0xf);\r |
cc68a136 |
358 | break;\r |
359 | case 7:\r |
360 | CDC_Write_Reg(d);\r |
361 | return;\r |
362 | case 0xa:\r |
af37bca8 |
363 | elprintf(EL_CDREGS, "s68k set CDC dma addr");\r |
cc68a136 |
364 | break;\r |
d1df8786 |
365 | case 0xc:\r |
4f265db7 |
366 | case 0xd:\r |
af37bca8 |
367 | elprintf(EL_CDREGS, "s68k set stopwatch timer");\r |
4f265db7 |
368 | Pico_mcd->m.timer_stopwatch = 0;\r |
369 | return;\r |
1cd356a3 |
370 | case 0xe:\r |
7a1f6e45 |
371 | Pico_mcd->s68k_regs[0xf] = (d>>1) | (d<<7); // ror8 1, Gens note: Dragons lair\r |
1cd356a3 |
372 | return;\r |
d1df8786 |
373 | case 0x31:\r |
af37bca8 |
374 | elprintf(EL_CDREGS, "s68k set int3 timer: %02x", d);\r |
48e8482f |
375 | Pico_mcd->m.timer_int3 = (d & 0xff) << 16;\r |
d1df8786 |
376 | break;\r |
cc68a136 |
377 | case 0x33: // IRQ mask\r |
af37bca8 |
378 | elprintf(EL_CDREGS, "s68k irq mask: %02x", d);\r |
cc68a136 |
379 | if ((d&(1<<4)) && (Pico_mcd->s68k_regs[0x37]&4) && !(Pico_mcd->s68k_regs[0x33]&(1<<4))) {\r |
380 | CDD_Export_Status();\r |
cc68a136 |
381 | }\r |
382 | break;\r |
383 | case 0x34: // fader\r |
384 | Pico_mcd->s68k_regs[a] = (u8) d & 0x7f;\r |
385 | return;\r |
672ad671 |
386 | case 0x36:\r |
387 | return; // d/m bit is unsetable\r |
388 | case 0x37: {\r |
389 | u32 d_old = Pico_mcd->s68k_regs[0x37];\r |
390 | Pico_mcd->s68k_regs[0x37] = d&7;\r |
391 | if ((d&4) && !(d_old&4)) {\r |
cc68a136 |
392 | CDD_Export_Status();\r |
cc68a136 |
393 | }\r |
672ad671 |
394 | return;\r |
395 | }\r |
cc68a136 |
396 | case 0x4b:\r |
397 | Pico_mcd->s68k_regs[a] = (u8) d;\r |
398 | CDD_Import_Command();\r |
399 | return;\r |
400 | }\r |
401 | \r |
1cd356a3 |
402 | if ((a&0x1f0) == 0x10 || (a >= 0x38 && a < 0x42))\r |
cc68a136 |
403 | {\r |
ca61ee42 |
404 | elprintf(EL_UIO, "s68k FIXME: invalid write @ %02x?", a);\r |
cc68a136 |
405 | return;\r |
406 | }\r |
407 | \r |
408 | Pico_mcd->s68k_regs[a] = (u8) d;\r |
409 | }\r |
410 | \r |
af37bca8 |
411 | // -----------------------------------------------------------------\r |
412 | // Main 68k\r |
413 | // -----------------------------------------------------------------\r |
cc68a136 |
414 | \r |
af37bca8 |
415 | #ifndef _ASM_CD_MEMORY_C\r |
416 | #include "cell_map.c"\r |
af37bca8 |
417 | \r |
418 | // WORD RAM, cell aranged area (220000 - 23ffff)\r |
0ace9b9a |
419 | static u32 PicoReadM68k8_cell0(u32 a)\r |
cc68a136 |
420 | {\r |
af37bca8 |
421 | a = (a&3) | (cell_map(a >> 2) << 2); // cell arranged\r |
0ace9b9a |
422 | return Pico_mcd->word_ram1M[0][a ^ 1];\r |
423 | }\r |
424 | \r |
425 | static u32 PicoReadM68k8_cell1(u32 a)\r |
426 | {\r |
427 | a = (a&3) | (cell_map(a >> 2) << 2);\r |
428 | return Pico_mcd->word_ram1M[1][a ^ 1];\r |
429 | }\r |
430 | \r |
431 | static u32 PicoReadM68k16_cell0(u32 a)\r |
432 | {\r |
433 | a = (a&2) | (cell_map(a >> 2) << 2);\r |
434 | return *(u16 *)(Pico_mcd->word_ram1M[0] + a);\r |
af37bca8 |
435 | }\r |
cc68a136 |
436 | \r |
0ace9b9a |
437 | static u32 PicoReadM68k16_cell1(u32 a)\r |
af37bca8 |
438 | {\r |
af37bca8 |
439 | a = (a&2) | (cell_map(a >> 2) << 2);\r |
0ace9b9a |
440 | return *(u16 *)(Pico_mcd->word_ram1M[1] + a);\r |
af37bca8 |
441 | }\r |
cc68a136 |
442 | \r |
0ace9b9a |
443 | static void PicoWriteM68k8_cell0(u32 a, u32 d)\r |
af37bca8 |
444 | {\r |
af37bca8 |
445 | a = (a&3) | (cell_map(a >> 2) << 2);\r |
0ace9b9a |
446 | Pico_mcd->word_ram1M[0][a ^ 1] = d;\r |
af37bca8 |
447 | }\r |
8022f53d |
448 | \r |
0ace9b9a |
449 | static void PicoWriteM68k8_cell1(u32 a, u32 d)\r |
af37bca8 |
450 | {\r |
af37bca8 |
451 | a = (a&3) | (cell_map(a >> 2) << 2);\r |
0ace9b9a |
452 | Pico_mcd->word_ram1M[1][a ^ 1] = d;\r |
af37bca8 |
453 | }\r |
454 | \r |
0ace9b9a |
455 | static void PicoWriteM68k16_cell0(u32 a, u32 d)\r |
456 | {\r |
457 | a = (a&3) | (cell_map(a >> 2) << 2);\r |
458 | *(u16 *)(Pico_mcd->word_ram1M[0] + a) = d;\r |
459 | }\r |
460 | \r |
461 | static void PicoWriteM68k16_cell1(u32 a, u32 d)\r |
462 | {\r |
463 | a = (a&3) | (cell_map(a >> 2) << 2);\r |
464 | *(u16 *)(Pico_mcd->word_ram1M[1] + a) = d;\r |
465 | }\r |
466 | #endif\r |
467 | \r |
af37bca8 |
468 | // RAM cart (40000 - 7fffff, optional)\r |
469 | static u32 PicoReadM68k8_ramc(u32 a)\r |
470 | {\r |
471 | u32 d = 0;\r |
472 | if (a == 0x400001) {\r |
473 | if (SRam.data != NULL)\r |
474 | d = 3; // 64k cart\r |
475 | return d;\r |
8022f53d |
476 | }\r |
477 | \r |
af37bca8 |
478 | if ((a & 0xfe0000) == 0x600000) {\r |
479 | if (SRam.data != NULL)\r |
480 | d = SRam.data[((a >> 1) & 0xffff) + 0x2000];\r |
481 | return d;\r |
8022f53d |
482 | }\r |
483 | \r |
af37bca8 |
484 | if (a == 0x7fffff)\r |
485 | return Pico_mcd->m.bcram_reg;\r |
cc68a136 |
486 | \r |
af37bca8 |
487 | elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);\r |
cc68a136 |
488 | return d;\r |
489 | }\r |
490 | \r |
af37bca8 |
491 | static u32 PicoReadM68k16_ramc(u32 a)\r |
cc68a136 |
492 | {\r |
af37bca8 |
493 | elprintf(EL_ANOMALY, "ramcart r16: [%06x] @%06x", a, SekPcS68k);\r |
494 | return PicoReadM68k8_ramc(a + 1);\r |
495 | }\r |
cc68a136 |
496 | \r |
af37bca8 |
497 | static void PicoWriteM68k8_ramc(u32 a, u32 d)\r |
498 | {\r |
499 | if ((a & 0xfe0000) == 0x600000) {\r |
500 | if (SRam.data != NULL && (Pico_mcd->m.bcram_reg & 1)) {\r |
501 | SRam.data[((a>>1) & 0xffff) + 0x2000] = d;\r |
8022f53d |
502 | SRam.changed = 1;\r |
503 | }\r |
504 | return;\r |
505 | }\r |
506 | \r |
af37bca8 |
507 | if (a == 0x7fffff) {\r |
508 | Pico_mcd->m.bcram_reg = d;\r |
8022f53d |
509 | return;\r |
510 | }\r |
511 | \r |
af37bca8 |
512 | elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);\r |
cc68a136 |
513 | }\r |
514 | \r |
af37bca8 |
515 | static void PicoWriteM68k16_ramc(u32 a, u32 d)\r |
cc68a136 |
516 | {\r |
af37bca8 |
517 | elprintf(EL_ANOMALY, "ramcart w16: [%06x] %04x @%06x", a, d, SekPcS68k);\r |
518 | PicoWriteM68k8_ramc(a + 1, d);\r |
cc68a136 |
519 | }\r |
520 | \r |
af37bca8 |
521 | // IO/control/cd registers (a10000 - ...)\r |
0ace9b9a |
522 | #ifndef _ASM_CD_MEMORY_C\r |
af37bca8 |
523 | static u32 PicoReadM68k8_io(u32 a)\r |
cc68a136 |
524 | {\r |
af37bca8 |
525 | u32 d;\r |
526 | if ((a & 0xff00) == 0x2000) { // a12000 - a120ff\r |
527 | d = m68k_reg_read16(a); // TODO: m68k_reg_read8\r |
528 | if (!(a & 1))\r |
529 | d >>= 8;\r |
530 | d &= 0xff;\r |
531 | elprintf(EL_CDREGS, "m68k_regs r8: [%02x] %02x @%06x", a & 0x3f, d, SekPc);\r |
532 | return d;\r |
533 | }\r |
534 | \r |
535 | // fallback to default MD handler\r |
536 | return PicoRead8_io(a);\r |
cc68a136 |
537 | }\r |
538 | \r |
af37bca8 |
539 | static u32 PicoReadM68k16_io(u32 a)\r |
cc68a136 |
540 | {\r |
af37bca8 |
541 | u32 d;\r |
542 | if ((a & 0xff00) == 0x2000) {\r |
543 | d = m68k_reg_read16(a);\r |
544 | elprintf(EL_CDREGS, "m68k_regs r16: [%02x] %04x @%06x", a & 0x3f, d, SekPc);\r |
545 | return d;\r |
b542be46 |
546 | }\r |
cc68a136 |
547 | \r |
af37bca8 |
548 | return PicoRead16_io(a);\r |
cc68a136 |
549 | }\r |
550 | \r |
af37bca8 |
551 | static void PicoWriteM68k8_io(u32 a, u32 d)\r |
cc68a136 |
552 | {\r |
af37bca8 |
553 | if ((a & 0xff00) == 0x2000) { // a12000 - a120ff\r |
554 | elprintf(EL_CDREGS, "m68k_regs w8: [%02x] %02x @%06x", a&0x3f, d, SekPc);\r |
2433f409 |
555 | m68k_reg_write8(a, d);\r |
556 | return;\r |
557 | }\r |
672ad671 |
558 | \r |
af37bca8 |
559 | PicoWrite16_io(a, d);\r |
cc68a136 |
560 | }\r |
ab0607f7 |
561 | \r |
af37bca8 |
562 | static void PicoWriteM68k16_io(u32 a, u32 d)\r |
cc68a136 |
563 | {\r |
af37bca8 |
564 | if ((a & 0xff00) == 0x2000) { // a12000 - a120ff\r |
565 | elprintf(EL_CDREGS, "m68k_regs w16: [%02x] %04x @%06x", a&0x3f, d, SekPc);\r |
566 | /* TODO FIXME?\r |
7a1f6e45 |
567 | if (a == 0xe) { // special case, 2 byte writes would be handled differently\r |
568 | Pico_mcd->s68k_regs[0xe] = d >> 8;\r |
569 | #ifdef USE_POLL_DETECT\r |
570 | if ((s68k_poll_adclk&0xfe) == 0xe && s68k_poll_cnt > POLL_LIMIT) {\r |
6cadc2da |
571 | SekSetStopS68k(0); s68k_poll_adclk = 0;\r |
8f8fe01e |
572 | elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);\r |
7a1f6e45 |
573 | }\r |
574 | #endif\r |
575 | return;\r |
576 | }\r |
af37bca8 |
577 | */\r |
578 | m68k_reg_write8(a, d >> 8);\r |
579 | m68k_reg_write8(a + 1, d & 0xff);\r |
b542be46 |
580 | return;\r |
581 | }\r |
582 | \r |
af37bca8 |
583 | PicoWrite16_io(a, d);\r |
cc68a136 |
584 | }\r |
0ace9b9a |
585 | #endif\r |
cc68a136 |
586 | \r |
721cd396 |
587 | // -----------------------------------------------------------------\r |
af37bca8 |
588 | // Sub 68k\r |
cc68a136 |
589 | // -----------------------------------------------------------------\r |
590 | \r |
af37bca8 |
591 | static u32 s68k_unmapped_read8(u32 a)\r |
cc68a136 |
592 | {\r |
af37bca8 |
593 | elprintf(EL_UIO, "s68k unmapped r8 [%06x] @%06x", a, SekPc);\r |
594 | return 0;\r |
cc68a136 |
595 | }\r |
596 | \r |
af37bca8 |
597 | static u32 s68k_unmapped_read16(u32 a)\r |
cc68a136 |
598 | {\r |
af37bca8 |
599 | elprintf(EL_UIO, "s68k unmapped r16 [%06x] @%06x", a, SekPc);\r |
600 | return 0;\r |
601 | }\r |
4f265db7 |
602 | \r |
af37bca8 |
603 | static void s68k_unmapped_write8(u32 a, u32 d)\r |
604 | {\r |
605 | elprintf(EL_UIO, "s68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);\r |
606 | }\r |
cc68a136 |
607 | \r |
af37bca8 |
608 | static void s68k_unmapped_write16(u32 a, u32 d)\r |
609 | {\r |
610 | elprintf(EL_UIO, "s68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);\r |
611 | }\r |
cc68a136 |
612 | \r |
0ace9b9a |
613 | // PRG RAM protected range (000000 - 00ff00)?\r |
614 | // XXX verify: ff00 or 1fe00 max?\r |
615 | static void PicoWriteS68k8_prgwp(u32 a, u32 d)\r |
616 | {\r |
617 | if (a >= (Pico_mcd->s68k_regs[2] << 8))\r |
618 | Pico_mcd->prg_ram[a ^ 1] = d;\r |
619 | }\r |
620 | \r |
621 | static void PicoWriteS68k16_prgwp(u32 a, u32 d)\r |
622 | {\r |
623 | if (a >= (Pico_mcd->s68k_regs[2] << 8))\r |
624 | *(u16 *)(Pico_mcd->prg_ram + a) = d;\r |
625 | }\r |
626 | \r |
627 | #ifndef _ASM_CD_MEMORY_C\r |
628 | \r |
af37bca8 |
629 | // decode (080000 - 0bffff, in 1M mode)\r |
0ace9b9a |
630 | static u32 PicoReadS68k8_dec0(u32 a)\r |
631 | {\r |
632 | u32 d = Pico_mcd->word_ram1M[0][((a >> 1) ^ 1) & 0x1ffff];\r |
633 | if (a & 1)\r |
634 | d &= 0x0f;\r |
635 | else\r |
636 | d >>= 4;\r |
637 | return d;\r |
638 | }\r |
639 | \r |
640 | static u32 PicoReadS68k8_dec1(u32 a)\r |
af37bca8 |
641 | {\r |
0ace9b9a |
642 | u32 d = Pico_mcd->word_ram1M[1][((a >> 1) ^ 1) & 0x1ffff];\r |
af37bca8 |
643 | if (a & 1)\r |
644 | d &= 0x0f;\r |
645 | else\r |
646 | d >>= 4;\r |
cc68a136 |
647 | return d;\r |
648 | }\r |
649 | \r |
0ace9b9a |
650 | static u32 PicoReadS68k16_dec0(u32 a)\r |
cc68a136 |
651 | {\r |
0ace9b9a |
652 | u32 d = Pico_mcd->word_ram1M[0][((a >> 1) ^ 1) & 0x1ffff];\r |
af37bca8 |
653 | d |= d << 4;\r |
654 | d &= ~0xf0;\r |
cc68a136 |
655 | return d;\r |
656 | }\r |
ab0607f7 |
657 | \r |
0ace9b9a |
658 | static u32 PicoReadS68k16_dec1(u32 a)\r |
0a051f55 |
659 | {\r |
0ace9b9a |
660 | u32 d = Pico_mcd->word_ram1M[1][((a >> 1) ^ 1) & 0x1ffff];\r |
661 | d |= d << 4;\r |
662 | d &= ~0xf0;\r |
663 | return d;\r |
0a051f55 |
664 | }\r |
665 | \r |
0ace9b9a |
666 | /* check: jaguar xj 220 (draws entire world using decode) */\r |
667 | #define mk_decode_w8(bank) \\r |
668 | static void PicoWriteS68k8_dec_m0b##bank(u32 a, u32 d) \\r |
669 | { \\r |
670 | u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \\r |
671 | \\r |
672 | if (!(a & 1)) \\r |
673 | *pd = (*pd & 0x0f) | (d << 4); \\r |
674 | else \\r |
675 | *pd = (*pd & 0xf0) | (d & 0x0f); \\r |
676 | } \\r |
677 | \\r |
678 | static void PicoWriteS68k8_dec_m1b##bank(u32 a, u32 d) \\r |
679 | { \\r |
680 | u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \\r |
681 | u8 mask = (a & 1) ? 0x0f : 0xf0; \\r |
682 | \\r |
683 | if (!(*pd & mask) && (d & 0x0f)) /* underwrite */ \\r |
684 | PicoWriteS68k8_dec_m0b##bank(a, d); \\r |
685 | } \\r |
686 | \\r |
687 | static void PicoWriteS68k8_dec_m2b##bank(u32 a, u32 d) /* ...and m3? */ \\r |
688 | { \\r |
689 | if (d & 0x0f) /* overwrite */ \\r |
690 | PicoWriteS68k8_dec_m0b##bank(a, d); \\r |
691 | }\r |
0a051f55 |
692 | \r |
0ace9b9a |
693 | mk_decode_w8(0)\r |
694 | mk_decode_w8(1)\r |
695 | \r |
696 | #define mk_decode_w16(bank) \\r |
697 | static void PicoWriteS68k16_dec_m0b##bank(u32 a, u32 d) \\r |
698 | { \\r |
699 | u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \\r |
700 | \\r |
701 | d &= 0x0f0f; \\r |
702 | *pd = d | (d >> 4); \\r |
703 | } \\r |
704 | \\r |
705 | static void PicoWriteS68k16_dec_m1b##bank(u32 a, u32 d) \\r |
706 | { \\r |
707 | u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \\r |
708 | \\r |
709 | d &= 0x0f0f; /* underwrite */ \\r |
710 | if (!(*pd & 0xf0)) *pd |= d >> 4; \\r |
711 | if (!(*pd & 0x0f)) *pd |= d; \\r |
712 | } \\r |
713 | \\r |
714 | static void PicoWriteS68k16_dec_m2b##bank(u32 a, u32 d) \\r |
715 | { \\r |
716 | u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \\r |
717 | \\r |
718 | d &= 0x0f0f; /* overwrite */ \\r |
719 | d |= d >> 4; \\r |
720 | \\r |
721 | if (!(d & 0xf0)) d |= *pd & 0xf0; \\r |
722 | if (!(d & 0x0f)) d |= *pd & 0x0f; \\r |
723 | *pd = d; \\r |
724 | }\r |
0a051f55 |
725 | \r |
0ace9b9a |
726 | mk_decode_w16(0)\r |
727 | mk_decode_w16(1)\r |
0a051f55 |
728 | \r |
0ace9b9a |
729 | #endif\r |
0a051f55 |
730 | \r |
af37bca8 |
731 | // backup RAM (fe0000 - feffff)\r |
732 | static u32 PicoReadS68k8_bram(u32 a)\r |
733 | {\r |
734 | return Pico_mcd->bram[(a>>1)&0x1fff];\r |
735 | }\r |
cc68a136 |
736 | \r |
af37bca8 |
737 | static u32 PicoReadS68k16_bram(u32 a)\r |
cc68a136 |
738 | {\r |
af37bca8 |
739 | u32 d;\r |
740 | elprintf(EL_ANOMALY, "FIXME: s68k_bram r16: [%06x] @%06x", a, SekPcS68k);\r |
741 | a = (a >> 1) & 0x1fff;\r |
742 | d = Pico_mcd->bram[a++];\r |
743 | d|= Pico_mcd->bram[a++] << 8; // probably wrong, TODO: verify\r |
744 | return d;\r |
745 | }\r |
cc68a136 |
746 | \r |
af37bca8 |
747 | static void PicoWriteS68k8_bram(u32 a, u32 d)\r |
748 | {\r |
749 | Pico_mcd->bram[(a >> 1) & 0x1fff] = d;\r |
750 | SRam.changed = 1;\r |
751 | }\r |
cc68a136 |
752 | \r |
af37bca8 |
753 | static void PicoWriteS68k16_bram(u32 a, u32 d)\r |
754 | {\r |
755 | elprintf(EL_ANOMALY, "s68k_bram w16: [%06x] %04x @%06x", a, d, SekPcS68k);\r |
756 | a = (a >> 1) & 0x1fff;\r |
757 | Pico_mcd->bram[a++] = d;\r |
758 | Pico_mcd->bram[a++] = d >> 8; // TODO: verify..\r |
759 | SRam.changed = 1;\r |
760 | }\r |
b5e5172d |
761 | \r |
0ace9b9a |
762 | #ifndef _ASM_CD_MEMORY_C\r |
763 | \r |
af37bca8 |
764 | // PCM and registers (ff0000 - ffffff)\r |
765 | static u32 PicoReadS68k8_pr(u32 a)\r |
766 | {\r |
767 | u32 d = 0;\r |
cc68a136 |
768 | \r |
769 | // regs\r |
af37bca8 |
770 | if ((a & 0xfe00) == 0x8000) {\r |
cb4a513a |
771 | a &= 0x1ff;\r |
af37bca8 |
772 | elprintf(EL_CDREGS, "s68k_regs r8: [%02x] @ %06x", a, SekPcS68k);\r |
773 | if (a >= 0x0e && a < 0x30) {\r |
774 | d = Pico_mcd->s68k_regs[a];\r |
775 | s68k_poll_detect(a, d);\r |
776 | elprintf(EL_CDREGS, "ret = %02x", (u8)d);\r |
777 | return d;\r |
d0d47c5b |
778 | }\r |
af37bca8 |
779 | else if (a >= 0x58 && a < 0x68)\r |
780 | d = gfx_cd_read(a & ~1);\r |
781 | else d = s68k_reg_read16(a & ~1);\r |
782 | if (!(a & 1))\r |
783 | d >>= 8;\r |
784 | elprintf(EL_CDREGS, "ret = %02x", (u8)d);\r |
785 | return d & 0xff;\r |
d0d47c5b |
786 | }\r |
787 | \r |
4f265db7 |
788 | // PCM\r |
0ace9b9a |
789 | // XXX: verify: probably odd addrs only?\r |
af37bca8 |
790 | if ((a & 0x8000) == 0x0000) {\r |
4f265db7 |
791 | a &= 0x7fff;\r |
792 | if (a >= 0x2000)\r |
af37bca8 |
793 | d = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a >> 1) & 0xfff];\r |
794 | else if (a >= 0x20) {\r |
795 | a &= 0x1e;\r |
796 | d = Pico_mcd->pcm.ch[a>>2].addr >> PCM_STEP_SHIFT;\r |
797 | if (a & 2)\r |
798 | d >>= 8;\r |
799 | }\r |
800 | return d & 0xff;\r |
ab0607f7 |
801 | }\r |
802 | \r |
af37bca8 |
803 | return s68k_unmapped_read8(a);\r |
cc68a136 |
804 | }\r |
805 | \r |
af37bca8 |
806 | static u32 PicoReadS68k16_pr(u32 a)\r |
cc68a136 |
807 | {\r |
af37bca8 |
808 | u32 d = 0;\r |
cc68a136 |
809 | \r |
810 | // regs\r |
af37bca8 |
811 | if ((a & 0xfe00) == 0x8000) {\r |
cb4a513a |
812 | a &= 0x1fe;\r |
af37bca8 |
813 | elprintf(EL_CDREGS, "s68k_regs r16: [%02x] @ %06x", a, SekPcS68k);\r |
814 | if (0x58 <= a && a < 0x68)\r |
815 | d = gfx_cd_read(a);\r |
816 | else d = s68k_reg_read16(a);\r |
817 | elprintf(EL_CDREGS, "ret = %04x", d);\r |
818 | return d;\r |
cc68a136 |
819 | }\r |
820 | \r |
af37bca8 |
821 | // PCM\r |
822 | if ((a & 0x8000) == 0x0000) {\r |
823 | //elprintf(EL_ANOMALY, "FIXME: s68k_pcm r16: [%06x] @%06x", a, SekPcS68k);\r |
824 | a &= 0x7fff;\r |
825 | if (a >= 0x2000)\r |
826 | d = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff];\r |
827 | else if (a >= 0x20) {\r |
828 | a &= 0x1e;\r |
829 | d = Pico_mcd->pcm.ch[a>>2].addr >> PCM_STEP_SHIFT;\r |
830 | if (a & 2) d >>= 8;\r |
d0d47c5b |
831 | }\r |
af37bca8 |
832 | elprintf(EL_CDREGS, "ret = %04x", d);\r |
833 | return d;\r |
d0d47c5b |
834 | }\r |
835 | \r |
af37bca8 |
836 | return s68k_unmapped_read16(a);\r |
837 | }\r |
838 | \r |
839 | static void PicoWriteS68k8_pr(u32 a, u32 d)\r |
840 | {\r |
841 | // regs\r |
842 | if ((a & 0xfe00) == 0x8000) {\r |
843 | a &= 0x1ff;\r |
844 | elprintf(EL_CDREGS, "s68k_regs w8: [%02x] %02x @ %06x", a, d, SekPcS68k);\r |
845 | if (0x58 <= a && a < 0x68)\r |
846 | gfx_cd_write16(a&~1, (d<<8)|d);\r |
847 | else s68k_reg_write8(a,d);\r |
d0d47c5b |
848 | return;\r |
849 | }\r |
850 | \r |
4f265db7 |
851 | // PCM\r |
af37bca8 |
852 | if ((a & 0x8000) == 0x0000) {\r |
4f265db7 |
853 | a &= 0x7fff;\r |
854 | if (a >= 0x2000)\r |
855 | Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff] = d;\r |
856 | else if (a < 0x12)\r |
af37bca8 |
857 | pcm_write(a>>1, d);\r |
ab0607f7 |
858 | return;\r |
859 | }\r |
860 | \r |
af37bca8 |
861 | s68k_unmapped_write8(a, d);\r |
cc68a136 |
862 | }\r |
ab0607f7 |
863 | \r |
af37bca8 |
864 | static void PicoWriteS68k16_pr(u32 a, u32 d)\r |
cc68a136 |
865 | {\r |
cc68a136 |
866 | // regs\r |
af37bca8 |
867 | if ((a & 0xfe00) == 0x8000) {\r |
cb4a513a |
868 | a &= 0x1fe;\r |
af37bca8 |
869 | elprintf(EL_CDREGS, "s68k_regs w16: [%02x] %04x @ %06x", a, d, SekPcS68k);\r |
870 | if (a >= 0x58 && a < 0x68)\r |
871 | gfx_cd_write16(a, d);\r |
872 | else {\r |
873 | if (a == 0xe) {\r |
874 | // special case, 2 byte writes would be handled differently\r |
875 | // TODO: verify\r |
876 | Pico_mcd->s68k_regs[0xf] = d;\r |
877 | return;\r |
878 | }\r |
879 | s68k_reg_write8(a, d >> 8);\r |
880 | s68k_reg_write8(a + 1, d & 0xff);\r |
d0d47c5b |
881 | }\r |
882 | return;\r |
883 | }\r |
884 | \r |
4f265db7 |
885 | // PCM\r |
af37bca8 |
886 | if ((a & 0x8000) == 0x0000) {\r |
4f265db7 |
887 | a &= 0x7fff;\r |
af37bca8 |
888 | if (a >= 0x2000)\r |
889 | Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff] = d;\r |
890 | else if (a < 0x12)\r |
891 | pcm_write(a>>1, d & 0xff);\r |
ab0607f7 |
892 | return;\r |
893 | }\r |
894 | \r |
af37bca8 |
895 | s68k_unmapped_write16(a, d);\r |
cc68a136 |
896 | }\r |
cc68a136 |
897 | \r |
0ace9b9a |
898 | #endif\r |
899 | \r |
900 | static const void *m68k_cell_read8[] = { PicoReadM68k8_cell0, PicoReadM68k8_cell1 };\r |
901 | static const void *m68k_cell_read16[] = { PicoReadM68k16_cell0, PicoReadM68k16_cell1 };\r |
902 | static const void *m68k_cell_write8[] = { PicoWriteM68k8_cell0, PicoWriteM68k8_cell1 };\r |
903 | static const void *m68k_cell_write16[] = { PicoWriteM68k16_cell0, PicoWriteM68k16_cell1 };\r |
904 | \r |
905 | static const void *s68k_dec_read8[] = { PicoReadS68k8_dec0, PicoReadS68k8_dec1 };\r |
906 | static const void *s68k_dec_read16[] = { PicoReadS68k16_dec0, PicoReadS68k16_dec1 };\r |
907 | \r |
908 | static const void *s68k_dec_write8[2][4] = {\r |
909 | { PicoWriteS68k8_dec_m0b0, PicoWriteS68k8_dec_m1b0, PicoWriteS68k8_dec_m2b0, PicoWriteS68k8_dec_m2b0 },\r |
910 | { PicoWriteS68k8_dec_m0b1, PicoWriteS68k8_dec_m1b1, PicoWriteS68k8_dec_m2b1, PicoWriteS68k8_dec_m2b1 },\r |
911 | };\r |
912 | \r |
913 | static const void *s68k_dec_write16[2][4] = {\r |
914 | { PicoWriteS68k16_dec_m0b0, PicoWriteS68k16_dec_m1b0, PicoWriteS68k16_dec_m2b0, PicoWriteS68k16_dec_m2b0 },\r |
915 | { PicoWriteS68k16_dec_m0b1, PicoWriteS68k16_dec_m1b1, PicoWriteS68k16_dec_m2b1, PicoWriteS68k16_dec_m2b1 },\r |
916 | };\r |
917 | \r |
cc68a136 |
918 | // -----------------------------------------------------------------\r |
919 | \r |
0ace9b9a |
920 | static void remap_prg_window(void)\r |
3aa1e148 |
921 | {\r |
af37bca8 |
922 | // PRG RAM\r |
923 | if (Pico_mcd->m.busreq & 2) {\r |
0ace9b9a |
924 | void *bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3] >> 6];\r |
af37bca8 |
925 | cpu68k_map_all_ram(0x020000, 0x03ffff, bank, 0);\r |
926 | }\r |
927 | else {\r |
928 | m68k_map_unmap(0x020000, 0x03ffff);\r |
929 | }\r |
0ace9b9a |
930 | }\r |
931 | \r |
932 | static void remap_word_ram(int r3)\r |
933 | {\r |
934 | void *bank;\r |
af37bca8 |
935 | \r |
936 | // WORD RAM\r |
937 | if (!(r3 & 4)) {\r |
938 | // 2M mode. XXX: allowing access in all cases for simplicity\r |
939 | bank = Pico_mcd->word_ram2M;\r |
940 | cpu68k_map_all_ram(0x200000, 0x23ffff, bank, 0);\r |
941 | cpu68k_map_all_ram(0x080000, 0x0bffff, bank, 1);\r |
942 | // TODO: handle 0x0c0000\r |
943 | }\r |
944 | else {\r |
0ace9b9a |
945 | int b0 = r3 & 1;\r |
946 | int m = (r3 & 0x18) >> 3;\r |
947 | bank = Pico_mcd->word_ram1M[b0];\r |
af37bca8 |
948 | cpu68k_map_all_ram(0x200000, 0x21ffff, bank, 0);\r |
0ace9b9a |
949 | bank = Pico_mcd->word_ram1M[b0 ^ 1];\r |
af37bca8 |
950 | cpu68k_map_all_ram(0x0c0000, 0x0effff, bank, 1);\r |
951 | // "cell arrange" on m68k\r |
0ace9b9a |
952 | cpu68k_map_set(m68k_read8_map, 0x220000, 0x23ffff, m68k_cell_read8[b0], 1);\r |
953 | cpu68k_map_set(m68k_read16_map, 0x220000, 0x23ffff, m68k_cell_read16[b0], 1);\r |
954 | cpu68k_map_set(m68k_write8_map, 0x220000, 0x23ffff, m68k_cell_write8[b0], 1);\r |
955 | cpu68k_map_set(m68k_write16_map, 0x220000, 0x23ffff, m68k_cell_write16[b0], 1);\r |
af37bca8 |
956 | // "decode format" on s68k\r |
0ace9b9a |
957 | cpu68k_map_set(s68k_read8_map, 0x080000, 0x0bffff, s68k_dec_read8[b0 ^ 1], 1);\r |
958 | cpu68k_map_set(s68k_read16_map, 0x080000, 0x0bffff, s68k_dec_read16[b0 ^ 1], 1);\r |
959 | cpu68k_map_set(s68k_write8_map, 0x080000, 0x0bffff, s68k_dec_write8[b0 ^ 1][m], 1);\r |
960 | cpu68k_map_set(s68k_write16_map, 0x080000, 0x0bffff, s68k_dec_write16[b0 ^ 1][m], 1);\r |
af37bca8 |
961 | }\r |
962 | \r |
3aa1e148 |
963 | #ifdef EMU_F68K\r |
964 | // update fetchmap..\r |
965 | int i;\r |
966 | if (!(r3 & 4))\r |
967 | {\r |
968 | for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x240000; i++)\r |
be26eb23 |
969 | PicoCpuFM68k.Fetch[i] = (unsigned long)Pico_mcd->word_ram2M - 0x200000;\r |
3aa1e148 |
970 | }\r |
971 | else\r |
972 | {\r |
973 | for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x220000; i++)\r |
be26eb23 |
974 | PicoCpuFM68k.Fetch[i] = (unsigned long)Pico_mcd->word_ram1M[r3 & 1] - 0x200000;\r |
3aa1e148 |
975 | for (i = M68K_FETCHBANK1*0x0c/0x100; (i<<(24-FAMEC_FETCHBITS)) < 0x0e0000; i++)\r |
be26eb23 |
976 | PicoCpuFS68k.Fetch[i] = (unsigned long)Pico_mcd->word_ram1M[(r3&1)^1] - 0x0c0000;\r |
3aa1e148 |
977 | }\r |
978 | #endif\r |
979 | }\r |
b837b69b |
980 | \r |
0ace9b9a |
981 | void PicoMemStateLoaded(void)\r |
982 | {\r |
983 | int r3 = Pico_mcd->s68k_regs[3];\r |
984 | \r |
985 | /* after load events */\r |
986 | if (r3 & 4) // 1M mode?\r |
987 | wram_2M_to_1M(Pico_mcd->word_ram2M);\r |
988 | remap_word_ram(r3);\r |
989 | remap_prg_window();\r |
990 | \r |
991 | // restore hint vector\r |
992 | *(unsigned short *)(Pico_mcd->bios + 0x72) = Pico_mcd->m.hint_vector;\r |
993 | }\r |
994 | \r |
9037e45d |
995 | #ifdef EMU_M68K\r |
996 | static void m68k_mem_setup_cd(void);\r |
997 | #endif\r |
998 | \r |
eff55556 |
999 | PICO_INTERNAL void PicoMemSetupCD(void)\r |
b837b69b |
1000 | {\r |
af37bca8 |
1001 | // setup default main68k map\r |
1002 | PicoMemSetup();\r |
1003 | \r |
af37bca8 |
1004 | // main68k map (BIOS mapped by PicoMemSetup()):\r |
1005 | // RAM cart\r |
1006 | if (PicoOpt & POPT_EN_MCD_RAMCART) {\r |
1007 | cpu68k_map_set(m68k_read8_map, 0x400000, 0x7fffff, PicoReadM68k8_ramc, 1);\r |
1008 | cpu68k_map_set(m68k_read16_map, 0x400000, 0x7fffff, PicoReadM68k16_ramc, 1);\r |
1009 | cpu68k_map_set(m68k_write8_map, 0x400000, 0x7fffff, PicoWriteM68k8_ramc, 1);\r |
1010 | cpu68k_map_set(m68k_write16_map, 0x400000, 0x7fffff, PicoWriteM68k16_ramc, 1);\r |
1011 | }\r |
1012 | \r |
1013 | // registers/IO:\r |
1014 | cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoReadM68k8_io, 1);\r |
1015 | cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoReadM68k16_io, 1);\r |
1016 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWriteM68k8_io, 1);\r |
1017 | cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWriteM68k16_io, 1);\r |
1018 | \r |
1019 | // sub68k map\r |
1020 | cpu68k_map_set(s68k_read8_map, 0x000000, 0xffffff, s68k_unmapped_read8, 1);\r |
1021 | cpu68k_map_set(s68k_read16_map, 0x000000, 0xffffff, s68k_unmapped_read16, 1);\r |
1022 | cpu68k_map_set(s68k_write8_map, 0x000000, 0xffffff, s68k_unmapped_write8, 1);\r |
1023 | cpu68k_map_set(s68k_write16_map, 0x000000, 0xffffff, s68k_unmapped_write16, 1);\r |
1024 | \r |
1025 | // PRG RAM\r |
1026 | cpu68k_map_set(s68k_read8_map, 0x000000, 0x07ffff, Pico_mcd->prg_ram, 0);\r |
1027 | cpu68k_map_set(s68k_read16_map, 0x000000, 0x07ffff, Pico_mcd->prg_ram, 0);\r |
1028 | cpu68k_map_set(s68k_write8_map, 0x000000, 0x07ffff, Pico_mcd->prg_ram, 0);\r |
1029 | cpu68k_map_set(s68k_write16_map, 0x000000, 0x07ffff, Pico_mcd->prg_ram, 0);\r |
0ace9b9a |
1030 | cpu68k_map_set(s68k_write8_map, 0x000000, 0x00ffff, PicoWriteS68k8_prgwp, 1);\r |
1031 | cpu68k_map_set(s68k_write16_map, 0x000000, 0x00ffff, PicoWriteS68k16_prgwp, 1);\r |
af37bca8 |
1032 | \r |
1033 | // BRAM\r |
1034 | cpu68k_map_set(s68k_read8_map, 0xfe0000, 0xfeffff, PicoReadS68k8_bram, 1);\r |
1035 | cpu68k_map_set(s68k_read16_map, 0xfe0000, 0xfeffff, PicoReadS68k16_bram, 1);\r |
1036 | cpu68k_map_set(s68k_write8_map, 0xfe0000, 0xfeffff, PicoWriteS68k8_bram, 1);\r |
1037 | cpu68k_map_set(s68k_write16_map, 0xfe0000, 0xfeffff, PicoWriteS68k16_bram, 1);\r |
1038 | \r |
1039 | // PCM, regs\r |
1040 | cpu68k_map_set(s68k_read8_map, 0xff0000, 0xffffff, PicoReadS68k8_pr, 1);\r |
1041 | cpu68k_map_set(s68k_read16_map, 0xff0000, 0xffffff, PicoReadS68k16_pr, 1);\r |
1042 | cpu68k_map_set(s68k_write8_map, 0xff0000, 0xffffff, PicoWriteS68k8_pr, 1);\r |
1043 | cpu68k_map_set(s68k_write16_map, 0xff0000, 0xffffff, PicoWriteS68k16_pr, 1);\r |
f53f286a |
1044 | \r |
0ace9b9a |
1045 | // RAMs\r |
1046 | remap_word_ram(1);\r |
1047 | \r |
b837b69b |
1048 | #ifdef EMU_C68K\r |
b837b69b |
1049 | // s68k\r |
5e89f0f5 |
1050 | PicoCpuCS68k.read8 = (void *)s68k_read8_map;\r |
1051 | PicoCpuCS68k.read16 = (void *)s68k_read16_map;\r |
1052 | PicoCpuCS68k.read32 = (void *)s68k_read16_map;\r |
1053 | PicoCpuCS68k.write8 = (void *)s68k_write8_map;\r |
1054 | PicoCpuCS68k.write16 = (void *)s68k_write16_map;\r |
1055 | PicoCpuCS68k.write32 = (void *)s68k_write16_map;\r |
1056 | PicoCpuCS68k.checkpc = NULL; /* unused */\r |
1057 | PicoCpuCS68k.fetch8 = NULL;\r |
1058 | PicoCpuCS68k.fetch16 = NULL;\r |
1059 | PicoCpuCS68k.fetch32 = NULL;\r |
b837b69b |
1060 | #endif\r |
3aa1e148 |
1061 | #ifdef EMU_F68K\r |
3aa1e148 |
1062 | // s68k\r |
af37bca8 |
1063 | PicoCpuFS68k.read_byte = s68k_read8;\r |
1064 | PicoCpuFS68k.read_word = s68k_read16;\r |
1065 | PicoCpuFS68k.read_long = s68k_read32;\r |
1066 | PicoCpuFS68k.write_byte = s68k_write8;\r |
1067 | PicoCpuFS68k.write_word = s68k_write16;\r |
1068 | PicoCpuFS68k.write_long = s68k_write32;\r |
3aa1e148 |
1069 | \r |
1070 | // setup FAME fetchmap\r |
1071 | {\r |
1072 | int i;\r |
1073 | // M68k\r |
1074 | // by default, point everything to fitst 64k of ROM (BIOS)\r |
1075 | for (i = 0; i < M68K_FETCHBANK1; i++)\r |
be26eb23 |
1076 | PicoCpuFM68k.Fetch[i] = (unsigned long)Pico.rom - (i<<(24-FAMEC_FETCHBITS));\r |
3aa1e148 |
1077 | // now real ROM (BIOS)\r |
1078 | for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < Pico.romsize; i++)\r |
be26eb23 |
1079 | PicoCpuFM68k.Fetch[i] = (unsigned long)Pico.rom;\r |
3aa1e148 |
1080 | // .. and RAM\r |
1081 | for (i = M68K_FETCHBANK1*14/16; i < M68K_FETCHBANK1; i++)\r |
be26eb23 |
1082 | PicoCpuFM68k.Fetch[i] = (unsigned long)Pico.ram - (i<<(24-FAMEC_FETCHBITS));\r |
3aa1e148 |
1083 | // S68k\r |
1084 | // PRG RAM is default\r |
1085 | for (i = 0; i < M68K_FETCHBANK1; i++)\r |
be26eb23 |
1086 | PicoCpuFS68k.Fetch[i] = (unsigned long)Pico_mcd->prg_ram - (i<<(24-FAMEC_FETCHBITS));\r |
3aa1e148 |
1087 | // real PRG RAM\r |
1088 | for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0x80000; i++)\r |
be26eb23 |
1089 | PicoCpuFS68k.Fetch[i] = (unsigned long)Pico_mcd->prg_ram;\r |
3aa1e148 |
1090 | // WORD RAM 2M area\r |
1091 | for (i = M68K_FETCHBANK1*0x08/0x100; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0xc0000; i++)\r |
be26eb23 |
1092 | PicoCpuFS68k.Fetch[i] = (unsigned long)Pico_mcd->word_ram2M - 0x80000;\r |
0ace9b9a |
1093 | // remap_word_ram() will setup word ram for both\r |
3aa1e148 |
1094 | }\r |
1095 | #endif\r |
9037e45d |
1096 | #ifdef EMU_M68K\r |
1097 | m68k_mem_setup_cd();\r |
1098 | #endif\r |
3aa1e148 |
1099 | \r |
7a1f6e45 |
1100 | // m68k_poll_addr = m68k_poll_cnt = 0;\r |
1101 | s68k_poll_adclk = s68k_poll_cnt = 0;\r |
b837b69b |
1102 | }\r |
1103 | \r |
1104 | \r |
cc68a136 |
1105 | #ifdef EMU_M68K\r |
af37bca8 |
1106 | u32 m68k_read8(u32 a);\r |
1107 | u32 m68k_read16(u32 a);\r |
1108 | u32 m68k_read32(u32 a);\r |
1109 | void m68k_write8(u32 a, u8 d);\r |
1110 | void m68k_write16(u32 a, u16 d);\r |
1111 | void m68k_write32(u32 a, u32 d);\r |
1112 | \r |
9037e45d |
1113 | static unsigned int PicoReadCD8w (unsigned int a) {\r |
af37bca8 |
1114 | return m68ki_cpu_p == &PicoCpuMS68k ? s68k_read8(a) : m68k_read8(a);\r |
cc68a136 |
1115 | }\r |
9037e45d |
1116 | static unsigned int PicoReadCD16w(unsigned int a) {\r |
af37bca8 |
1117 | return m68ki_cpu_p == &PicoCpuMS68k ? s68k_read16(a) : m68k_read16(a);\r |
cc68a136 |
1118 | }\r |
9037e45d |
1119 | static unsigned int PicoReadCD32w(unsigned int a) {\r |
af37bca8 |
1120 | return m68ki_cpu_p == &PicoCpuMS68k ? s68k_read32(a) : m68k_read32(a);\r |
cc68a136 |
1121 | }\r |
9037e45d |
1122 | static void PicoWriteCD8w (unsigned int a, unsigned char d) {\r |
af37bca8 |
1123 | if (m68ki_cpu_p == &PicoCpuMS68k) s68k_write8(a, d); else m68k_write8(a, d);\r |
cc68a136 |
1124 | }\r |
9037e45d |
1125 | static void PicoWriteCD16w(unsigned int a, unsigned short d) {\r |
af37bca8 |
1126 | if (m68ki_cpu_p == &PicoCpuMS68k) s68k_write16(a, d); else m68k_write16(a, d);\r |
cc68a136 |
1127 | }\r |
9037e45d |
1128 | static void PicoWriteCD32w(unsigned int a, unsigned int d) {\r |
af37bca8 |
1129 | if (m68ki_cpu_p == &PicoCpuMS68k) s68k_write32(a, d); else m68k_write32(a, d);\r |
cc68a136 |
1130 | }\r |
1131 | \r |
9037e45d |
1132 | extern unsigned int (*pm68k_read_memory_8) (unsigned int address);\r |
1133 | extern unsigned int (*pm68k_read_memory_16)(unsigned int address);\r |
1134 | extern unsigned int (*pm68k_read_memory_32)(unsigned int address);\r |
1135 | extern void (*pm68k_write_memory_8) (unsigned int address, unsigned char value);\r |
1136 | extern void (*pm68k_write_memory_16)(unsigned int address, unsigned short value);\r |
1137 | extern void (*pm68k_write_memory_32)(unsigned int address, unsigned int value);\r |
9037e45d |
1138 | \r |
1139 | static void m68k_mem_setup_cd(void)\r |
1140 | {\r |
1141 | pm68k_read_memory_8 = PicoReadCD8w;\r |
1142 | pm68k_read_memory_16 = PicoReadCD16w;\r |
1143 | pm68k_read_memory_32 = PicoReadCD32w;\r |
1144 | pm68k_write_memory_8 = PicoWriteCD8w;\r |
1145 | pm68k_write_memory_16 = PicoWriteCD16w;\r |
1146 | pm68k_write_memory_32 = PicoWriteCD32w;\r |
9037e45d |
1147 | }\r |
cc68a136 |
1148 | #endif // EMU_M68K\r |
1149 | \r |