2 * Memory I/O handlers for Sega/Mega CD.
\r
3 * (C) notaz, 2007-2009
\r
5 * This work is licensed under the terms of MAME license.
\r
6 * See COPYING file in the top-level directory.
\r
9 #include "../pico_int.h"
\r
10 #include "../memory.h"
\r
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
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
27 // -----------------------------------------------------------------
\r
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
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
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
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
68 static void remap_prg_window(void);
\r
69 static void remap_word_ram(int r3);
\r
72 #define POLL_LIMIT 16
\r
73 #define POLL_CYCLES 124
\r
74 unsigned int s68k_poll_adclk, s68k_poll_cnt;
\r
76 #ifndef _ASM_CD_MEMORY_C
\r
77 static u32 m68k_reg_read16(u32 a)
\r
84 d = ((Pico_mcd->s68k_regs[0x33]<<13)&0x8000) | Pico_mcd->m.busreq; // here IFL2 is always 0, just like in Gens
\r
87 d = (Pico_mcd->s68k_regs[a]<<8) | (Pico_mcd->s68k_regs[a+1]&0xc7);
\r
88 elprintf(EL_CDREG3, "m68k_regs r3: %02x @%06x", (u8)d, SekPc);
\r
91 d = Pico_mcd->s68k_regs[4]<<8;
\r
94 d = *(u16 *)(Pico_mcd->bios + 0x72);
\r
97 d = Read_CDC_Host(0);
\r
100 elprintf(EL_UIO, "m68k FIXME: reserved read");
\r
103 d = Pico_mcd->m.timer_stopwatch >> 16;
\r
104 elprintf(EL_CDREGS, "m68k stopwatch timer read (%04x)", d);
\r
109 // comm flag/cmd/status (0xE-0x2F)
\r
110 d = (Pico_mcd->s68k_regs[a]<<8) | Pico_mcd->s68k_regs[a+1];
\r
114 elprintf(EL_UIO, "m68k_regs FIXME invalid read @ %02x", a);
\r
122 #ifndef _ASM_CD_MEMORY_C
\r
125 void m68k_reg_write8(u32 a, u32 d)
\r
133 if ((d&1) && (Pico_mcd->s68k_regs[0x33]&(1<<2))) { elprintf(EL_INTS, "m68k: s68k irq 2"); SekInterruptS68k(2); }
\r
137 if (!(d&1)) Pico_mcd->m.state_flags |= 1; // reset pending, needed to be sure we fetch the right vectors on reset
\r
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
140 if ((Pico_mcd->m.state_flags&1) && (d&3)==1) {
\r
141 SekResetS68k(); // S68k comes out of RESET or BRQ state
\r
142 Pico_mcd->m.state_flags&=~1;
\r
143 elprintf(EL_CDREGS, "m68k: resetting s68k, cycles=%i", SekCyclesLeft);
\r
146 d |= 2; // verified: reset also gives bus
\r
147 if ((d ^ Pico_mcd->m.busreq) & 2)
\r
148 remap_prg_window();
\r
149 Pico_mcd->m.busreq = d;
\r
152 elprintf(EL_CDREGS, "m68k: prg wp=%02x", d);
\r
153 Pico_mcd->s68k_regs[2] = d; // really use s68k side register
\r
156 dold = Pico_mcd->s68k_regs[3];
\r
157 elprintf(EL_CDREG3, "m68k_regs w3: %02x @%06x", (u8)d, SekPc);
\r
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
161 if (dold & 4) { // 1M mode
\r
162 d ^= 2; // writing 0 to DMNA actually sets it, 1 does nothing
\r
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
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
173 remap_prg_window();
\r
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
178 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);
\r
183 Pico_mcd->bios[0x72 + 1] = d; // simple hint vector changer
\r
186 Pico_mcd->bios[0x72] = d;
\r
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
191 d = (d << 1) | ((d >> 7) & 1); // rol8 1 (special case)
\r
193 //dprintf("m68k: comm flag: %02x", d);
\r
194 Pico_mcd->s68k_regs[0xe] = d;
\r
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
198 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);
\r
204 if ((a&0xf0) == 0x10) {
\r
205 Pico_mcd->s68k_regs[a] = d;
\r
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
209 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);
\r
215 elprintf(EL_UIO, "m68k FIXME: invalid write? [%02x] %02x", a, d);
\r
218 #ifndef _ASM_CD_MEMORY_C
\r
221 u32 s68k_poll_detect(u32 a, u32 d)
\r
223 #ifdef USE_POLL_DETECT
\r
224 // needed mostly for Cyclone, which doesn't always check it's cycle counter
\r
225 if (SekIsStoppedS68k()) return d;
\r
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
231 //printf("-- diff: %u, cnt = %i\n", clkdiff, s68k_poll_cnt);
\r
232 if (s68k_poll_cnt > POLL_LIMIT) {
\r
234 elprintf(EL_CDPOLL, "s68k poll detected @ %06x, a=%02x", SekPcS68k, a);
\r
236 s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a;
\r
240 s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a;
\r
246 #define READ_FONT_DATA(basemask) \
\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
257 #ifndef _ASM_CD_MEMORY_C
\r
260 u32 s68k_reg_read16(u32 a)
\r
266 return ((Pico_mcd->s68k_regs[0]&3)<<8) | 1; // ver = 0, not in reset state
\r
268 d = (Pico_mcd->s68k_regs[2]<<8) | (Pico_mcd->s68k_regs[3]&0x1f);
\r
269 elprintf(EL_CDREG3, "s68k_regs r3: %02x @%06x", (u8)d, SekPcS68k);
\r
270 return s68k_poll_detect(a, d);
\r
272 return CDC_Read_Reg();
\r
274 return Read_CDC_Host(1); // Gens returns 0 here on byte reads
\r
276 d = Pico_mcd->m.timer_stopwatch >> 16;
\r
277 elprintf(EL_CDREGS, "s68k stopwatch timer read (%04x)", d);
\r
280 elprintf(EL_CDREGS, "s68k int3 timer read (%02x)", Pico_mcd->s68k_regs[31]);
\r
281 return Pico_mcd->s68k_regs[31];
\r
282 case 0x34: // fader
\r
283 return 0; // no busy bit
\r
284 case 0x50: // font data (check: Lunar 2, Silpheed)
\r
285 READ_FONT_DATA(0x00100000);
\r
288 READ_FONT_DATA(0x00010000);
\r
291 READ_FONT_DATA(0x10000000);
\r
294 READ_FONT_DATA(0x01000000);
\r
298 d = (Pico_mcd->s68k_regs[a]<<8) | Pico_mcd->s68k_regs[a+1];
\r
300 if (a >= 0x0e && a < 0x30)
\r
301 return s68k_poll_detect(a, d);
\r
306 #ifndef _ASM_CD_MEMORY_C
\r
309 void s68k_reg_write8(u32 a, u32 d)
\r
311 // Warning: d might have upper bits set
\r
314 return; // only m68k can change WP
\r
316 int dold = Pico_mcd->s68k_regs[3];
\r
317 elprintf(EL_CDREG3, "s68k_regs w3: %02x @%06x", (u8)d, SekPcS68k);
\r
322 if ((d ^ dold) & 0x1d) {
\r
323 d &= ~2; // in case of mode or bank change we clear DMNA (m68k req) bit
\r
327 elprintf(EL_CDREG3, "wram mode 2M->1M");
\r
328 wram_2M_to_1M(Pico_mcd->word_ram2M);
\r
334 elprintf(EL_CDREG3, "wram mode 1M->2M");
\r
335 if (!(d&1)) { // it didn't set the ret bit, which means it doesn't want to give WRAM to m68k
\r
337 d |= (dold&1) ? 2 : 1; // then give it to the one which had bank0 in 1M mode
\r
339 wram_1M_to_2M(Pico_mcd->word_ram2M);
\r
342 // s68k can only set RET, writing 0 has no effect
\r
343 else if ((dold ^ d) & d & 1) { // RET being set
\r
344 SekEndRunS68k(20+16+10+12+16); // see DMNA case
\r
348 d &= ~2; // DMNA clears
\r
353 elprintf(EL_CDREGS, "s68k CDC dest: %x", d&7);
\r
354 Pico_mcd->s68k_regs[4] = (Pico_mcd->s68k_regs[4]&0xC0) | (d&7); // CDC mode
\r
357 //dprintf("s68k CDC reg addr: %x", d&0xf);
\r
363 elprintf(EL_CDREGS, "s68k set CDC dma addr");
\r
367 elprintf(EL_CDREGS, "s68k set stopwatch timer");
\r
368 Pico_mcd->m.timer_stopwatch = 0;
\r
371 Pico_mcd->s68k_regs[0xf] = (d>>1) | (d<<7); // ror8 1, Gens note: Dragons lair
\r
374 elprintf(EL_CDREGS, "s68k set int3 timer: %02x", d);
\r
375 Pico_mcd->m.timer_int3 = (d & 0xff) << 16;
\r
377 case 0x33: // IRQ mask
\r
378 elprintf(EL_CDREGS, "s68k irq mask: %02x", d);
\r
379 if ((d&(1<<4)) && (Pico_mcd->s68k_regs[0x37]&4) && !(Pico_mcd->s68k_regs[0x33]&(1<<4))) {
\r
380 CDD_Export_Status();
\r
383 case 0x34: // fader
\r
384 Pico_mcd->s68k_regs[a] = (u8) d & 0x7f;
\r
387 return; // d/m bit is unsetable
\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
392 CDD_Export_Status();
\r
397 Pico_mcd->s68k_regs[a] = (u8) d;
\r
398 CDD_Import_Command();
\r
402 if ((a&0x1f0) == 0x10 || (a >= 0x38 && a < 0x42))
\r
404 elprintf(EL_UIO, "s68k FIXME: invalid write @ %02x?", a);
\r
408 Pico_mcd->s68k_regs[a] = (u8) d;
\r
411 // -----------------------------------------------------------------
\r
413 // -----------------------------------------------------------------
\r
415 #ifndef _ASM_CD_MEMORY_C
\r
416 #include "cell_map.c"
\r
418 // WORD RAM, cell aranged area (220000 - 23ffff)
\r
419 static u32 PicoReadM68k8_cell0(u32 a)
\r
421 a = (a&3) | (cell_map(a >> 2) << 2); // cell arranged
\r
422 return Pico_mcd->word_ram1M[0][a ^ 1];
\r
425 static u32 PicoReadM68k8_cell1(u32 a)
\r
427 a = (a&3) | (cell_map(a >> 2) << 2);
\r
428 return Pico_mcd->word_ram1M[1][a ^ 1];
\r
431 static u32 PicoReadM68k16_cell0(u32 a)
\r
433 a = (a&2) | (cell_map(a >> 2) << 2);
\r
434 return *(u16 *)(Pico_mcd->word_ram1M[0] + a);
\r
437 static u32 PicoReadM68k16_cell1(u32 a)
\r
439 a = (a&2) | (cell_map(a >> 2) << 2);
\r
440 return *(u16 *)(Pico_mcd->word_ram1M[1] + a);
\r
443 static void PicoWriteM68k8_cell0(u32 a, u32 d)
\r
445 a = (a&3) | (cell_map(a >> 2) << 2);
\r
446 Pico_mcd->word_ram1M[0][a ^ 1] = d;
\r
449 static void PicoWriteM68k8_cell1(u32 a, u32 d)
\r
451 a = (a&3) | (cell_map(a >> 2) << 2);
\r
452 Pico_mcd->word_ram1M[1][a ^ 1] = d;
\r
455 static void PicoWriteM68k16_cell0(u32 a, u32 d)
\r
457 a = (a&3) | (cell_map(a >> 2) << 2);
\r
458 *(u16 *)(Pico_mcd->word_ram1M[0] + a) = d;
\r
461 static void PicoWriteM68k16_cell1(u32 a, u32 d)
\r
463 a = (a&3) | (cell_map(a >> 2) << 2);
\r
464 *(u16 *)(Pico_mcd->word_ram1M[1] + a) = d;
\r
468 // RAM cart (40000 - 7fffff, optional)
\r
469 static u32 PicoReadM68k8_ramc(u32 a)
\r
472 if (a == 0x400001) {
\r
473 if (SRam.data != NULL)
\r
478 if ((a & 0xfe0000) == 0x600000) {
\r
479 if (SRam.data != NULL)
\r
480 d = SRam.data[((a >> 1) & 0xffff) + 0x2000];
\r
485 return Pico_mcd->m.bcram_reg;
\r
487 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
\r
491 static u32 PicoReadM68k16_ramc(u32 a)
\r
493 elprintf(EL_ANOMALY, "ramcart r16: [%06x] @%06x", a, SekPcS68k);
\r
494 return PicoReadM68k8_ramc(a + 1);
\r
497 static void PicoWriteM68k8_ramc(u32 a, u32 d)
\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
507 if (a == 0x7fffff) {
\r
508 Pico_mcd->m.bcram_reg = d;
\r
512 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
\r
515 static void PicoWriteM68k16_ramc(u32 a, u32 d)
\r
517 elprintf(EL_ANOMALY, "ramcart w16: [%06x] %04x @%06x", a, d, SekPcS68k);
\r
518 PicoWriteM68k8_ramc(a + 1, d);
\r
521 // IO/control/cd registers (a10000 - ...)
\r
522 #ifndef _ASM_CD_MEMORY_C
\r
523 static u32 PicoReadM68k8_io(u32 a)
\r
526 if ((a & 0xff00) == 0x2000) { // a12000 - a120ff
\r
527 d = m68k_reg_read16(a); // TODO: m68k_reg_read8
\r
531 elprintf(EL_CDREGS, "m68k_regs r8: [%02x] %02x @%06x", a & 0x3f, d, SekPc);
\r
535 // fallback to default MD handler
\r
536 return PicoRead8_io(a);
\r
539 static u32 PicoReadM68k16_io(u32 a)
\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
548 return PicoRead16_io(a);
\r
551 static void PicoWriteM68k8_io(u32 a, u32 d)
\r
553 if ((a & 0xff00) == 0x2000) { // a12000 - a120ff
\r
554 elprintf(EL_CDREGS, "m68k_regs w8: [%02x] %02x @%06x", a&0x3f, d, SekPc);
\r
555 m68k_reg_write8(a, d);
\r
559 PicoWrite16_io(a, d);
\r
562 static void PicoWriteM68k16_io(u32 a, u32 d)
\r
564 if ((a & 0xff00) == 0x2000) { // a12000 - a120ff
\r
565 elprintf(EL_CDREGS, "m68k_regs w16: [%02x] %04x @%06x", a&0x3f, d, SekPc);
\r
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
571 SekSetStopS68k(0); s68k_poll_adclk = 0;
\r
572 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);
\r
578 m68k_reg_write8(a, d >> 8);
\r
579 m68k_reg_write8(a + 1, d & 0xff);
\r
583 PicoWrite16_io(a, d);
\r
587 // -----------------------------------------------------------------
\r
589 // -----------------------------------------------------------------
\r
591 static u32 s68k_unmapped_read8(u32 a)
\r
593 elprintf(EL_UIO, "s68k unmapped r8 [%06x] @%06x", a, SekPc);
\r
597 static u32 s68k_unmapped_read16(u32 a)
\r
599 elprintf(EL_UIO, "s68k unmapped r16 [%06x] @%06x", a, SekPc);
\r
603 static void s68k_unmapped_write8(u32 a, u32 d)
\r
605 elprintf(EL_UIO, "s68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
\r
608 static void s68k_unmapped_write16(u32 a, u32 d)
\r
610 elprintf(EL_UIO, "s68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
\r
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
617 if (a >= (Pico_mcd->s68k_regs[2] << 8))
\r
618 Pico_mcd->prg_ram[a ^ 1] = d;
\r
621 static void PicoWriteS68k16_prgwp(u32 a, u32 d)
\r
623 if (a >= (Pico_mcd->s68k_regs[2] << 8))
\r
624 *(u16 *)(Pico_mcd->prg_ram + a) = d;
\r
627 #ifndef _ASM_CD_MEMORY_C
\r
629 // decode (080000 - 0bffff, in 1M mode)
\r
630 static u32 PicoReadS68k8_dec0(u32 a)
\r
632 u32 d = Pico_mcd->word_ram1M[0][((a >> 1) ^ 1) & 0x1ffff];
\r
640 static u32 PicoReadS68k8_dec1(u32 a)
\r
642 u32 d = Pico_mcd->word_ram1M[1][((a >> 1) ^ 1) & 0x1ffff];
\r
650 static u32 PicoReadS68k16_dec0(u32 a)
\r
652 u32 d = Pico_mcd->word_ram1M[0][((a >> 1) ^ 1) & 0x1ffff];
\r
658 static u32 PicoReadS68k16_dec1(u32 a)
\r
660 u32 d = Pico_mcd->word_ram1M[1][((a >> 1) ^ 1) & 0x1ffff];
\r
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
670 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
673 *pd = (*pd & 0x0f) | (d << 4); \
\r
675 *pd = (*pd & 0xf0) | (d & 0x0f); \
\r
678 static void PicoWriteS68k8_dec_m1b##bank(u32 a, u32 d) \
\r
680 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
681 u8 mask = (a & 1) ? 0x0f : 0xf0; \
\r
683 if (!(*pd & mask) && (d & 0x0f)) /* underwrite */ \
\r
684 PicoWriteS68k8_dec_m0b##bank(a, d); \
\r
687 static void PicoWriteS68k8_dec_m2b##bank(u32 a, u32 d) /* ...and m3? */ \
\r
689 if (d & 0x0f) /* overwrite */ \
\r
690 PicoWriteS68k8_dec_m0b##bank(a, d); \
\r
696 #define mk_decode_w16(bank) \
\r
697 static void PicoWriteS68k16_dec_m0b##bank(u32 a, u32 d) \
\r
699 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
702 *pd = d | (d >> 4); \
\r
705 static void PicoWriteS68k16_dec_m1b##bank(u32 a, u32 d) \
\r
707 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
709 d &= 0x0f0f; /* underwrite */ \
\r
710 if (!(*pd & 0xf0)) *pd |= d >> 4; \
\r
711 if (!(*pd & 0x0f)) *pd |= d; \
\r
714 static void PicoWriteS68k16_dec_m2b##bank(u32 a, u32 d) \
\r
716 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
718 d &= 0x0f0f; /* overwrite */ \
\r
721 if (!(d & 0xf0)) d |= *pd & 0xf0; \
\r
722 if (!(d & 0x0f)) d |= *pd & 0x0f; \
\r
731 // backup RAM (fe0000 - feffff)
\r
732 static u32 PicoReadS68k8_bram(u32 a)
\r
734 return Pico_mcd->bram[(a>>1)&0x1fff];
\r
737 static u32 PicoReadS68k16_bram(u32 a)
\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
747 static void PicoWriteS68k8_bram(u32 a, u32 d)
\r
749 Pico_mcd->bram[(a >> 1) & 0x1fff] = d;
\r
753 static void PicoWriteS68k16_bram(u32 a, u32 d)
\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
762 #ifndef _ASM_CD_MEMORY_C
\r
764 // PCM and registers (ff0000 - ffffff)
\r
765 static u32 PicoReadS68k8_pr(u32 a)
\r
770 if ((a & 0xfe00) == 0x8000) {
\r
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
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
784 elprintf(EL_CDREGS, "ret = %02x", (u8)d);
\r
789 // XXX: verify: probably odd addrs only?
\r
790 if ((a & 0x8000) == 0x0000) {
\r
793 d = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a >> 1) & 0xfff];
\r
794 else if (a >= 0x20) {
\r
796 d = Pico_mcd->pcm.ch[a>>2].addr >> PCM_STEP_SHIFT;
\r
803 return s68k_unmapped_read8(a);
\r
806 static u32 PicoReadS68k16_pr(u32 a)
\r
811 if ((a & 0xfe00) == 0x8000) {
\r
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
822 if ((a & 0x8000) == 0x0000) {
\r
823 //elprintf(EL_ANOMALY, "FIXME: s68k_pcm r16: [%06x] @%06x", a, SekPcS68k);
\r
826 d = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff];
\r
827 else if (a >= 0x20) {
\r
829 d = Pico_mcd->pcm.ch[a>>2].addr >> PCM_STEP_SHIFT;
\r
830 if (a & 2) d >>= 8;
\r
832 elprintf(EL_CDREGS, "ret = %04x", d);
\r
836 return s68k_unmapped_read16(a);
\r
839 static void PicoWriteS68k8_pr(u32 a, u32 d)
\r
842 if ((a & 0xfe00) == 0x8000) {
\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
852 if ((a & 0x8000) == 0x0000) {
\r
855 Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff] = d;
\r
857 pcm_write(a>>1, d);
\r
861 s68k_unmapped_write8(a, d);
\r
864 static void PicoWriteS68k16_pr(u32 a, u32 d)
\r
867 if ((a & 0xfe00) == 0x8000) {
\r
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
874 // special case, 2 byte writes would be handled differently
\r
876 Pico_mcd->s68k_regs[0xf] = d;
\r
879 s68k_reg_write8(a, d >> 8);
\r
880 s68k_reg_write8(a + 1, d & 0xff);
\r
886 if ((a & 0x8000) == 0x0000) {
\r
889 Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff] = d;
\r
891 pcm_write(a>>1, d & 0xff);
\r
895 s68k_unmapped_write16(a, d);
\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
905 static const void *s68k_dec_read8[] = { PicoReadS68k8_dec0, PicoReadS68k8_dec1 };
\r
906 static const void *s68k_dec_read16[] = { PicoReadS68k16_dec0, PicoReadS68k16_dec1 };
\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
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
918 // -----------------------------------------------------------------
\r
920 static void remap_prg_window(void)
\r
923 if (Pico_mcd->m.busreq & 2) {
\r
924 void *bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3] >> 6];
\r
925 cpu68k_map_all_ram(0x020000, 0x03ffff, bank, 0);
\r
928 m68k_map_unmap(0x020000, 0x03ffff);
\r
932 static void remap_word_ram(int r3)
\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
946 int m = (r3 & 0x18) >> 3;
\r
947 bank = Pico_mcd->word_ram1M[b0];
\r
948 cpu68k_map_all_ram(0x200000, 0x21ffff, bank, 0);
\r
949 bank = Pico_mcd->word_ram1M[b0 ^ 1];
\r
950 cpu68k_map_all_ram(0x0c0000, 0x0effff, bank, 1);
\r
951 // "cell arrange" on m68k
\r
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
956 // "decode format" on s68k
\r
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
964 // update fetchmap..
\r
968 for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x240000; i++)
\r
969 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram2M - 0x200000;
\r
973 for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x220000; i++)
\r
974 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram1M[r3 & 1] - 0x200000;
\r
975 for (i = M68K_FETCHBANK1*0x0c/0x100; (i<<(24-FAMEC_FETCHBITS)) < 0x0e0000; i++)
\r
976 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram1M[(r3&1)^1] - 0x0c0000;
\r
981 void PicoMemStateLoaded(void)
\r
983 int r3 = Pico_mcd->s68k_regs[3];
\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
991 // restore hint vector
\r
992 *(unsigned short *)(Pico_mcd->bios + 0x72) = Pico_mcd->m.hint_vector;
\r
996 static void m68k_mem_setup_cd(void);
\r
999 PICO_INTERNAL void PicoMemSetupCD(void)
\r
1001 // setup default main68k map
\r
1004 // main68k map (BIOS mapped by PicoMemSetup()):
\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
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
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
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
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
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
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
1046 remap_word_ram(1);
\r
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
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
1070 // setup FAME fetchmap
\r
1074 // by default, point everything to fitst 64k of ROM (BIOS)
\r
1075 for (i = 0; i < M68K_FETCHBANK1; i++)
\r
1076 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom - (i<<(24-FAMEC_FETCHBITS));
\r
1077 // now real ROM (BIOS)
\r
1078 for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < Pico.romsize; i++)
\r
1079 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom;
\r
1081 for (i = M68K_FETCHBANK1*14/16; i < M68K_FETCHBANK1; i++)
\r
1082 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.ram - (i<<(24-FAMEC_FETCHBITS));
\r
1084 // PRG RAM is default
\r
1085 for (i = 0; i < M68K_FETCHBANK1; i++)
\r
1086 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->prg_ram - (i<<(24-FAMEC_FETCHBITS));
\r
1088 for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0x80000; i++)
\r
1089 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->prg_ram;
\r
1090 // WORD RAM 2M area
\r
1091 for (i = M68K_FETCHBANK1*0x08/0x100; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0xc0000; i++)
\r
1092 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram2M - 0x80000;
\r
1093 // remap_word_ram() will setup word ram for both
\r
1097 m68k_mem_setup_cd();
\r
1100 // m68k_poll_addr = m68k_poll_cnt = 0;
\r
1101 s68k_poll_adclk = s68k_poll_cnt = 0;
\r
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
1113 static unsigned int PicoReadCD8w (unsigned int a) {
\r
1114 return m68ki_cpu_p == &PicoCpuMS68k ? s68k_read8(a) : m68k_read8(a);
\r
1116 static unsigned int PicoReadCD16w(unsigned int a) {
\r
1117 return m68ki_cpu_p == &PicoCpuMS68k ? s68k_read16(a) : m68k_read16(a);
\r
1119 static unsigned int PicoReadCD32w(unsigned int a) {
\r
1120 return m68ki_cpu_p == &PicoCpuMS68k ? s68k_read32(a) : m68k_read32(a);
\r
1122 static void PicoWriteCD8w (unsigned int a, unsigned char d) {
\r
1123 if (m68ki_cpu_p == &PicoCpuMS68k) s68k_write8(a, d); else m68k_write8(a, d);
\r
1125 static void PicoWriteCD16w(unsigned int a, unsigned short d) {
\r
1126 if (m68ki_cpu_p == &PicoCpuMS68k) s68k_write16(a, d); else m68k_write16(a, d);
\r
1128 static void PicoWriteCD32w(unsigned int a, unsigned int d) {
\r
1129 if (m68ki_cpu_p == &PicoCpuMS68k) s68k_write32(a, d); else m68k_write32(a, d);
\r
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
1139 static void m68k_mem_setup_cd(void)
\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
1148 #endif // EMU_M68K
\r