1 // Memory I/O handlers for Sega/Mega CD.
\r
2 // (c) Copyright 2007-2009, Grazvydas "notaz" Ignotas
\r
4 #include "../pico_int.h"
\r
5 #include "../memory.h"
\r
10 unsigned long s68k_read8_map [0x1000000 >> M68K_MEM_SHIFT];
\r
11 unsigned long s68k_read16_map [0x1000000 >> M68K_MEM_SHIFT];
\r
12 unsigned long s68k_write8_map [0x1000000 >> M68K_MEM_SHIFT];
\r
13 unsigned long s68k_write16_map[0x1000000 >> M68K_MEM_SHIFT];
\r
15 MAKE_68K_READ8(s68k_read8, s68k_read8_map)
\r
16 MAKE_68K_READ16(s68k_read16, s68k_read16_map)
\r
17 MAKE_68K_READ32(s68k_read32, s68k_read16_map)
\r
18 MAKE_68K_WRITE8(s68k_write8, s68k_write8_map)
\r
19 MAKE_68K_WRITE16(s68k_write16, s68k_write16_map)
\r
20 MAKE_68K_WRITE32(s68k_write32, s68k_write16_map)
\r
22 // -----------------------------------------------------------------
\r
24 // provided by ASM code:
\r
25 #ifdef _ASM_CD_MEMORY_C
\r
26 u32 PicoReadM68k8_io(u32 a);
\r
27 u32 PicoReadM68k16_io(u32 a);
\r
28 void PicoWriteM68k8_io(u32 a, u32 d);
\r
29 void PicoWriteM68k16_io(u32 a, u32 d);
\r
31 u32 PicoReadS68k8_pr(u32 a);
\r
32 u32 PicoReadS68k16_pr(u32 a);
\r
33 void PicoWriteS68k8_pr(u32 a, u32 d);
\r
34 void PicoWriteS68k16_pr(u32 a, u32 d);
\r
36 u32 PicoReadM68k8_cell0(u32 a);
\r
37 u32 PicoReadM68k8_cell1(u32 a);
\r
38 u32 PicoReadM68k16_cell0(u32 a);
\r
39 u32 PicoReadM68k16_cell1(u32 a);
\r
40 void PicoWriteM68k8_cell0(u32 a, u32 d);
\r
41 void PicoWriteM68k8_cell1(u32 a, u32 d);
\r
42 void PicoWriteM68k16_cell0(u32 a, u32 d);
\r
43 void PicoWriteM68k16_cell1(u32 a, u32 d);
\r
45 u32 PicoReadS68k8_dec0(u32 a);
\r
46 u32 PicoReadS68k8_dec1(u32 a);
\r
47 u32 PicoReadS68k16_dec0(u32 a);
\r
48 u32 PicoReadS68k16_dec1(u32 a);
\r
49 void PicoWriteS68k8_dec_m0b0(u32 a, u32 d);
\r
50 void PicoWriteS68k8_dec_m1b0(u32 a, u32 d);
\r
51 void PicoWriteS68k8_dec_m2b0(u32 a, u32 d);
\r
52 void PicoWriteS68k8_dec_m0b1(u32 a, u32 d);
\r
53 void PicoWriteS68k8_dec_m1b1(u32 a, u32 d);
\r
54 void PicoWriteS68k8_dec_m2b1(u32 a, u32 d);
\r
55 void PicoWriteS68k16_dec_m0b0(u32 a, u32 d);
\r
56 void PicoWriteS68k16_dec_m1b0(u32 a, u32 d);
\r
57 void PicoWriteS68k16_dec_m2b0(u32 a, u32 d);
\r
58 void PicoWriteS68k16_dec_m0b1(u32 a, u32 d);
\r
59 void PicoWriteS68k16_dec_m1b1(u32 a, u32 d);
\r
60 void PicoWriteS68k16_dec_m2b1(u32 a, u32 d);
\r
63 static void remap_prg_window(void);
\r
64 static void remap_word_ram(int r3);
\r
67 #define POLL_LIMIT 16
\r
68 #define POLL_CYCLES 124
\r
69 unsigned int s68k_poll_adclk, s68k_poll_cnt;
\r
71 #ifndef _ASM_CD_MEMORY_C
\r
72 static u32 m68k_reg_read16(u32 a)
\r
79 d = ((Pico_mcd->s68k_regs[0x33]<<13)&0x8000) | Pico_mcd->m.busreq; // here IFL2 is always 0, just like in Gens
\r
82 d = (Pico_mcd->s68k_regs[a]<<8) | (Pico_mcd->s68k_regs[a+1]&0xc7);
\r
83 elprintf(EL_CDREG3, "m68k_regs r3: %02x @%06x", (u8)d, SekPc);
\r
86 d = Pico_mcd->s68k_regs[4]<<8;
\r
89 d = *(u16 *)(Pico_mcd->bios + 0x72);
\r
92 d = Read_CDC_Host(0);
\r
95 elprintf(EL_UIO, "m68k FIXME: reserved read");
\r
98 d = Pico_mcd->m.timer_stopwatch >> 16;
\r
99 elprintf(EL_CDREGS, "m68k stopwatch timer read (%04x)", d);
\r
104 // comm flag/cmd/status (0xE-0x2F)
\r
105 d = (Pico_mcd->s68k_regs[a]<<8) | Pico_mcd->s68k_regs[a+1];
\r
109 elprintf(EL_UIO, "m68k_regs FIXME invalid read @ %02x", a);
\r
117 #ifndef _ASM_CD_MEMORY_C
\r
120 void m68k_reg_write8(u32 a, u32 d)
\r
128 if ((d&1) && (Pico_mcd->s68k_regs[0x33]&(1<<2))) { elprintf(EL_INTS, "m68k: s68k irq 2"); SekInterruptS68k(2); }
\r
132 if (!(d&1)) Pico_mcd->m.state_flags |= 1; // reset pending, needed to be sure we fetch the right vectors on reset
\r
133 if ( (Pico_mcd->m.busreq&1) != (d&1)) elprintf(EL_INTSW, "m68k: s68k reset %i", !(d&1));
\r
134 if ( (Pico_mcd->m.busreq&2) != (d&2)) elprintf(EL_INTSW, "m68k: s68k brq %i", (d&2)>>1);
\r
135 if ((Pico_mcd->m.state_flags&1) && (d&3)==1) {
\r
136 SekResetS68k(); // S68k comes out of RESET or BRQ state
\r
137 Pico_mcd->m.state_flags&=~1;
\r
138 elprintf(EL_CDREGS, "m68k: resetting s68k, cycles=%i", SekCyclesLeft);
\r
141 d |= 2; // verified: reset also gives bus
\r
142 if ((d ^ Pico_mcd->m.busreq) & 2)
\r
143 remap_prg_window();
\r
144 Pico_mcd->m.busreq = d;
\r
147 elprintf(EL_CDREGS, "m68k: prg wp=%02x", d);
\r
148 Pico_mcd->s68k_regs[2] = d; // really use s68k side register
\r
151 dold = Pico_mcd->s68k_regs[3];
\r
152 elprintf(EL_CDREG3, "m68k_regs w3: %02x @%06x", (u8)d, SekPc);
\r
153 //if ((Pico_mcd->s68k_regs[3]&4) != (d&4)) dprintf("m68k: ram mode %i mbit", (d&4) ? 1 : 2);
\r
154 //if ((Pico_mcd->s68k_regs[3]&2) != (d&2)) dprintf("m68k: %s", (d&4) ? ((d&2) ? "word swap req" : "noop?") :
\r
155 // ((d&2) ? "word ram to s68k" : "word ram to m68k"));
\r
156 if (dold & 4) { // 1M mode
\r
157 d ^= 2; // writing 0 to DMNA actually sets it, 1 does nothing
\r
159 if ((d ^ dold) & d & 2) { // DMNA is being set
\r
160 dold &= ~1; // return word RAM to s68k
\r
161 /* Silpheed hack: bset(w3), r3, btst, bne, r3 */
\r
162 SekEndRun(20+16+10+12+16);
\r
165 Pico_mcd->s68k_regs[3] = (d & 0xc2) | (dold & 0x1f);
\r
166 if ((d ^ dold) & 0xc0) {
\r
167 elprintf(EL_CDREGS, "m68k: prg bank: %i -> %i", (Pico_mcd->s68k_regs[a]>>6), ((d>>6)&3));
\r
168 remap_prg_window();
\r
170 #ifdef USE_POLL_DETECT
\r
171 if ((s68k_poll_adclk&0xfe) == 2 && s68k_poll_cnt > POLL_LIMIT) {
\r
172 SekSetStopS68k(0); s68k_poll_adclk = 0;
\r
173 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);
\r
178 Pico_mcd->bios[0x72 + 1] = d; // simple hint vector changer
\r
181 Pico_mcd->bios[0x72] = d;
\r
182 elprintf(EL_CDREGS, "hint vector set to %04x%04x",
\r
183 ((u16 *)Pico_mcd->bios)[0x70/2], ((u16 *)Pico_mcd->bios)[0x72/2]);
\r
186 d = (d << 1) | ((d >> 7) & 1); // rol8 1 (special case)
\r
188 //dprintf("m68k: comm flag: %02x", d);
\r
189 Pico_mcd->s68k_regs[0xe] = d;
\r
190 #ifdef USE_POLL_DETECT
\r
191 if ((s68k_poll_adclk&0xfe) == 0xe && s68k_poll_cnt > POLL_LIMIT) {
\r
192 SekSetStopS68k(0); s68k_poll_adclk = 0;
\r
193 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);
\r
199 if ((a&0xf0) == 0x10) {
\r
200 Pico_mcd->s68k_regs[a] = d;
\r
201 #ifdef USE_POLL_DETECT
\r
202 if ((a&0xfe) == (s68k_poll_adclk&0xfe) && s68k_poll_cnt > POLL_LIMIT) {
\r
203 SekSetStopS68k(0); s68k_poll_adclk = 0;
\r
204 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);
\r
210 elprintf(EL_UIO, "m68k FIXME: invalid write? [%02x] %02x", a, d);
\r
213 #ifndef _ASM_CD_MEMORY_C
\r
216 u32 s68k_poll_detect(u32 a, u32 d)
\r
218 #ifdef USE_POLL_DETECT
\r
219 // needed mostly for Cyclone, which doesn't always check it's cycle counter
\r
220 if (SekIsStoppedS68k()) return d;
\r
221 // polling detection
\r
222 if (a == (s68k_poll_adclk&0xff)) {
\r
223 unsigned int clkdiff = SekCyclesDoneS68k() - (s68k_poll_adclk>>8);
\r
224 if (clkdiff <= POLL_CYCLES) {
\r
226 //printf("-- diff: %u, cnt = %i\n", clkdiff, s68k_poll_cnt);
\r
227 if (s68k_poll_cnt > POLL_LIMIT) {
\r
229 elprintf(EL_CDPOLL, "s68k poll detected @ %06x, a=%02x", SekPcS68k, a);
\r
231 s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a;
\r
235 s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a;
\r
241 #define READ_FONT_DATA(basemask) \
\r
243 unsigned int fnt = *(unsigned int *)(Pico_mcd->s68k_regs + 0x4c); \
\r
244 unsigned int col0 = (fnt >> 8) & 0x0f, col1 = (fnt >> 12) & 0x0f; \
\r
245 if (fnt & (basemask << 0)) d = col1 ; else d = col0; \
\r
246 if (fnt & (basemask << 1)) d |= col1 << 4; else d |= col0 << 4; \
\r
247 if (fnt & (basemask << 2)) d |= col1 << 8; else d |= col0 << 8; \
\r
248 if (fnt & (basemask << 3)) d |= col1 << 12; else d |= col0 << 12; \
\r
252 #ifndef _ASM_CD_MEMORY_C
\r
255 u32 s68k_reg_read16(u32 a)
\r
261 return ((Pico_mcd->s68k_regs[0]&3)<<8) | 1; // ver = 0, not in reset state
\r
263 d = (Pico_mcd->s68k_regs[2]<<8) | (Pico_mcd->s68k_regs[3]&0x1f);
\r
264 elprintf(EL_CDREG3, "s68k_regs r3: %02x @%06x", (u8)d, SekPcS68k);
\r
265 return s68k_poll_detect(a, d);
\r
267 return CDC_Read_Reg();
\r
269 return Read_CDC_Host(1); // Gens returns 0 here on byte reads
\r
271 d = Pico_mcd->m.timer_stopwatch >> 16;
\r
272 elprintf(EL_CDREGS, "s68k stopwatch timer read (%04x)", d);
\r
275 elprintf(EL_CDREGS, "s68k int3 timer read (%02x)", Pico_mcd->s68k_regs[31]);
\r
276 return Pico_mcd->s68k_regs[31];
\r
277 case 0x34: // fader
\r
278 return 0; // no busy bit
\r
279 case 0x50: // font data (check: Lunar 2, Silpheed)
\r
280 READ_FONT_DATA(0x00100000);
\r
283 READ_FONT_DATA(0x00010000);
\r
286 READ_FONT_DATA(0x10000000);
\r
289 READ_FONT_DATA(0x01000000);
\r
293 d = (Pico_mcd->s68k_regs[a]<<8) | Pico_mcd->s68k_regs[a+1];
\r
295 if (a >= 0x0e && a < 0x30)
\r
296 return s68k_poll_detect(a, d);
\r
301 #ifndef _ASM_CD_MEMORY_C
\r
304 void s68k_reg_write8(u32 a, u32 d)
\r
306 // Warning: d might have upper bits set
\r
309 return; // only m68k can change WP
\r
311 int dold = Pico_mcd->s68k_regs[3];
\r
312 elprintf(EL_CDREG3, "s68k_regs w3: %02x @%06x", (u8)d, SekPcS68k);
\r
317 if ((d ^ dold) & 0x1d) {
\r
318 d &= ~2; // in case of mode or bank change we clear DMNA (m68k req) bit
\r
322 elprintf(EL_CDREG3, "wram mode 2M->1M");
\r
323 wram_2M_to_1M(Pico_mcd->word_ram2M);
\r
329 elprintf(EL_CDREG3, "wram mode 1M->2M");
\r
330 if (!(d&1)) { // it didn't set the ret bit, which means it doesn't want to give WRAM to m68k
\r
332 d |= (dold&1) ? 2 : 1; // then give it to the one which had bank0 in 1M mode
\r
334 wram_1M_to_2M(Pico_mcd->word_ram2M);
\r
337 // s68k can only set RET, writing 0 has no effect
\r
338 else if ((dold ^ d) & d & 1) { // RET being set
\r
339 SekEndRunS68k(20+16+10+12+16); // see DMNA case
\r
343 d &= ~2; // DMNA clears
\r
348 elprintf(EL_CDREGS, "s68k CDC dest: %x", d&7);
\r
349 Pico_mcd->s68k_regs[4] = (Pico_mcd->s68k_regs[4]&0xC0) | (d&7); // CDC mode
\r
352 //dprintf("s68k CDC reg addr: %x", d&0xf);
\r
358 elprintf(EL_CDREGS, "s68k set CDC dma addr");
\r
362 elprintf(EL_CDREGS, "s68k set stopwatch timer");
\r
363 Pico_mcd->m.timer_stopwatch = 0;
\r
366 Pico_mcd->s68k_regs[0xf] = (d>>1) | (d<<7); // ror8 1, Gens note: Dragons lair
\r
369 elprintf(EL_CDREGS, "s68k set int3 timer: %02x", d);
\r
370 Pico_mcd->m.timer_int3 = (d & 0xff) << 16;
\r
372 case 0x33: // IRQ mask
\r
373 elprintf(EL_CDREGS, "s68k irq mask: %02x", d);
\r
374 if ((d&(1<<4)) && (Pico_mcd->s68k_regs[0x37]&4) && !(Pico_mcd->s68k_regs[0x33]&(1<<4))) {
\r
375 CDD_Export_Status();
\r
378 case 0x34: // fader
\r
379 Pico_mcd->s68k_regs[a] = (u8) d & 0x7f;
\r
382 return; // d/m bit is unsetable
\r
384 u32 d_old = Pico_mcd->s68k_regs[0x37];
\r
385 Pico_mcd->s68k_regs[0x37] = d&7;
\r
386 if ((d&4) && !(d_old&4)) {
\r
387 CDD_Export_Status();
\r
392 Pico_mcd->s68k_regs[a] = (u8) d;
\r
393 CDD_Import_Command();
\r
397 if ((a&0x1f0) == 0x10 || (a >= 0x38 && a < 0x42))
\r
399 elprintf(EL_UIO, "s68k FIXME: invalid write @ %02x?", a);
\r
403 Pico_mcd->s68k_regs[a] = (u8) d;
\r
406 // -----------------------------------------------------------------
\r
408 // -----------------------------------------------------------------
\r
410 #ifndef _ASM_CD_MEMORY_C
\r
411 #include "cell_map.c"
\r
413 // WORD RAM, cell aranged area (220000 - 23ffff)
\r
414 static u32 PicoReadM68k8_cell0(u32 a)
\r
416 a = (a&3) | (cell_map(a >> 2) << 2); // cell arranged
\r
417 return Pico_mcd->word_ram1M[0][a ^ 1];
\r
420 static u32 PicoReadM68k8_cell1(u32 a)
\r
422 a = (a&3) | (cell_map(a >> 2) << 2);
\r
423 return Pico_mcd->word_ram1M[1][a ^ 1];
\r
426 static u32 PicoReadM68k16_cell0(u32 a)
\r
428 a = (a&2) | (cell_map(a >> 2) << 2);
\r
429 return *(u16 *)(Pico_mcd->word_ram1M[0] + a);
\r
432 static u32 PicoReadM68k16_cell1(u32 a)
\r
434 a = (a&2) | (cell_map(a >> 2) << 2);
\r
435 return *(u16 *)(Pico_mcd->word_ram1M[1] + a);
\r
438 static void PicoWriteM68k8_cell0(u32 a, u32 d)
\r
440 a = (a&3) | (cell_map(a >> 2) << 2);
\r
441 Pico_mcd->word_ram1M[0][a ^ 1] = d;
\r
444 static void PicoWriteM68k8_cell1(u32 a, u32 d)
\r
446 a = (a&3) | (cell_map(a >> 2) << 2);
\r
447 Pico_mcd->word_ram1M[1][a ^ 1] = d;
\r
450 static void PicoWriteM68k16_cell0(u32 a, u32 d)
\r
452 a = (a&3) | (cell_map(a >> 2) << 2);
\r
453 *(u16 *)(Pico_mcd->word_ram1M[0] + a) = d;
\r
456 static void PicoWriteM68k16_cell1(u32 a, u32 d)
\r
458 a = (a&3) | (cell_map(a >> 2) << 2);
\r
459 *(u16 *)(Pico_mcd->word_ram1M[1] + a) = d;
\r
463 // RAM cart (40000 - 7fffff, optional)
\r
464 static u32 PicoReadM68k8_ramc(u32 a)
\r
467 if (a == 0x400001) {
\r
468 if (SRam.data != NULL)
\r
473 if ((a & 0xfe0000) == 0x600000) {
\r
474 if (SRam.data != NULL)
\r
475 d = SRam.data[((a >> 1) & 0xffff) + 0x2000];
\r
480 return Pico_mcd->m.bcram_reg;
\r
482 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
\r
486 static u32 PicoReadM68k16_ramc(u32 a)
\r
488 elprintf(EL_ANOMALY, "ramcart r16: [%06x] @%06x", a, SekPcS68k);
\r
489 return PicoReadM68k8_ramc(a + 1);
\r
492 static void PicoWriteM68k8_ramc(u32 a, u32 d)
\r
494 if ((a & 0xfe0000) == 0x600000) {
\r
495 if (SRam.data != NULL && (Pico_mcd->m.bcram_reg & 1)) {
\r
496 SRam.data[((a>>1) & 0xffff) + 0x2000] = d;
\r
502 if (a == 0x7fffff) {
\r
503 Pico_mcd->m.bcram_reg = d;
\r
507 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
\r
510 static void PicoWriteM68k16_ramc(u32 a, u32 d)
\r
512 elprintf(EL_ANOMALY, "ramcart w16: [%06x] %04x @%06x", a, d, SekPcS68k);
\r
513 PicoWriteM68k8_ramc(a + 1, d);
\r
516 // IO/control/cd registers (a10000 - ...)
\r
517 #ifndef _ASM_CD_MEMORY_C
\r
518 static u32 PicoReadM68k8_io(u32 a)
\r
521 if ((a & 0xff00) == 0x2000) { // a12000 - a120ff
\r
522 d = m68k_reg_read16(a); // TODO: m68k_reg_read8
\r
526 elprintf(EL_CDREGS, "m68k_regs r8: [%02x] %02x @%06x", a & 0x3f, d, SekPc);
\r
530 // fallback to default MD handler
\r
531 return PicoRead8_io(a);
\r
534 static u32 PicoReadM68k16_io(u32 a)
\r
537 if ((a & 0xff00) == 0x2000) {
\r
538 d = m68k_reg_read16(a);
\r
539 elprintf(EL_CDREGS, "m68k_regs r16: [%02x] %04x @%06x", a & 0x3f, d, SekPc);
\r
543 return PicoRead16_io(a);
\r
546 static void PicoWriteM68k8_io(u32 a, u32 d)
\r
548 if ((a & 0xff00) == 0x2000) { // a12000 - a120ff
\r
549 elprintf(EL_CDREGS, "m68k_regs w8: [%02x] %02x @%06x", a&0x3f, d, SekPc);
\r
550 m68k_reg_write8(a, d);
\r
554 PicoWrite16_io(a, d);
\r
557 static void PicoWriteM68k16_io(u32 a, u32 d)
\r
559 if ((a & 0xff00) == 0x2000) { // a12000 - a120ff
\r
560 elprintf(EL_CDREGS, "m68k_regs w16: [%02x] %04x @%06x", a&0x3f, d, SekPc);
\r
562 if (a == 0xe) { // special case, 2 byte writes would be handled differently
\r
563 Pico_mcd->s68k_regs[0xe] = d >> 8;
\r
564 #ifdef USE_POLL_DETECT
\r
565 if ((s68k_poll_adclk&0xfe) == 0xe && s68k_poll_cnt > POLL_LIMIT) {
\r
566 SekSetStopS68k(0); s68k_poll_adclk = 0;
\r
567 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);
\r
573 m68k_reg_write8(a, d >> 8);
\r
574 m68k_reg_write8(a + 1, d & 0xff);
\r
578 PicoWrite16_io(a, d);
\r
582 // -----------------------------------------------------------------
\r
584 // -----------------------------------------------------------------
\r
586 static u32 s68k_unmapped_read8(u32 a)
\r
588 elprintf(EL_UIO, "s68k unmapped r8 [%06x] @%06x", a, SekPc);
\r
592 static u32 s68k_unmapped_read16(u32 a)
\r
594 elprintf(EL_UIO, "s68k unmapped r16 [%06x] @%06x", a, SekPc);
\r
598 static void s68k_unmapped_write8(u32 a, u32 d)
\r
600 elprintf(EL_UIO, "s68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
\r
603 static void s68k_unmapped_write16(u32 a, u32 d)
\r
605 elprintf(EL_UIO, "s68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
\r
608 // PRG RAM protected range (000000 - 00ff00)?
\r
609 // XXX verify: ff00 or 1fe00 max?
\r
610 static void PicoWriteS68k8_prgwp(u32 a, u32 d)
\r
612 if (a >= (Pico_mcd->s68k_regs[2] << 8))
\r
613 Pico_mcd->prg_ram[a ^ 1] = d;
\r
616 static void PicoWriteS68k16_prgwp(u32 a, u32 d)
\r
618 if (a >= (Pico_mcd->s68k_regs[2] << 8))
\r
619 *(u16 *)(Pico_mcd->prg_ram + a) = d;
\r
622 #ifndef _ASM_CD_MEMORY_C
\r
624 // decode (080000 - 0bffff, in 1M mode)
\r
625 static u32 PicoReadS68k8_dec0(u32 a)
\r
627 u32 d = Pico_mcd->word_ram1M[0][((a >> 1) ^ 1) & 0x1ffff];
\r
635 static u32 PicoReadS68k8_dec1(u32 a)
\r
637 u32 d = Pico_mcd->word_ram1M[1][((a >> 1) ^ 1) & 0x1ffff];
\r
645 static u32 PicoReadS68k16_dec0(u32 a)
\r
647 u32 d = Pico_mcd->word_ram1M[0][((a >> 1) ^ 1) & 0x1ffff];
\r
653 static u32 PicoReadS68k16_dec1(u32 a)
\r
655 u32 d = Pico_mcd->word_ram1M[1][((a >> 1) ^ 1) & 0x1ffff];
\r
661 /* check: jaguar xj 220 (draws entire world using decode) */
\r
662 #define mk_decode_w8(bank) \
\r
663 static void PicoWriteS68k8_dec_m0b##bank(u32 a, u32 d) \
\r
665 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
668 *pd = (*pd & 0x0f) | (d << 4); \
\r
670 *pd = (*pd & 0xf0) | (d & 0x0f); \
\r
673 static void PicoWriteS68k8_dec_m1b##bank(u32 a, u32 d) \
\r
675 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
676 u8 mask = (a & 1) ? 0x0f : 0xf0; \
\r
678 if (!(*pd & mask) && (d & 0x0f)) /* underwrite */ \
\r
679 PicoWriteS68k8_dec_m0b##bank(a, d); \
\r
682 static void PicoWriteS68k8_dec_m2b##bank(u32 a, u32 d) /* ...and m3? */ \
\r
684 if (d & 0x0f) /* overwrite */ \
\r
685 PicoWriteS68k8_dec_m0b##bank(a, d); \
\r
691 #define mk_decode_w16(bank) \
\r
692 static void PicoWriteS68k16_dec_m0b##bank(u32 a, u32 d) \
\r
694 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
697 *pd = d | (d >> 4); \
\r
700 static void PicoWriteS68k16_dec_m1b##bank(u32 a, u32 d) \
\r
702 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
704 d &= 0x0f0f; /* underwrite */ \
\r
705 if (!(*pd & 0xf0)) *pd |= d >> 4; \
\r
706 if (!(*pd & 0x0f)) *pd |= d; \
\r
709 static void PicoWriteS68k16_dec_m2b##bank(u32 a, u32 d) \
\r
711 u8 *pd = &Pico_mcd->word_ram1M[bank][((a >> 1) ^ 1) & 0x1ffff]; \
\r
713 d &= 0x0f0f; /* overwrite */ \
\r
716 if (!(d & 0xf0)) d |= *pd & 0xf0; \
\r
717 if (!(d & 0x0f)) d |= *pd & 0x0f; \
\r
726 // backup RAM (fe0000 - feffff)
\r
727 static u32 PicoReadS68k8_bram(u32 a)
\r
729 return Pico_mcd->bram[(a>>1)&0x1fff];
\r
732 static u32 PicoReadS68k16_bram(u32 a)
\r
735 elprintf(EL_ANOMALY, "FIXME: s68k_bram r16: [%06x] @%06x", a, SekPcS68k);
\r
736 a = (a >> 1) & 0x1fff;
\r
737 d = Pico_mcd->bram[a++];
\r
738 d|= Pico_mcd->bram[a++] << 8; // probably wrong, TODO: verify
\r
742 static void PicoWriteS68k8_bram(u32 a, u32 d)
\r
744 Pico_mcd->bram[(a >> 1) & 0x1fff] = d;
\r
748 static void PicoWriteS68k16_bram(u32 a, u32 d)
\r
750 elprintf(EL_ANOMALY, "s68k_bram w16: [%06x] %04x @%06x", a, d, SekPcS68k);
\r
751 a = (a >> 1) & 0x1fff;
\r
752 Pico_mcd->bram[a++] = d;
\r
753 Pico_mcd->bram[a++] = d >> 8; // TODO: verify..
\r
757 #ifndef _ASM_CD_MEMORY_C
\r
759 // PCM and registers (ff0000 - ffffff)
\r
760 static u32 PicoReadS68k8_pr(u32 a)
\r
765 if ((a & 0xfe00) == 0x8000) {
\r
767 elprintf(EL_CDREGS, "s68k_regs r8: [%02x] @ %06x", a, SekPcS68k);
\r
768 if (a >= 0x0e && a < 0x30) {
\r
769 d = Pico_mcd->s68k_regs[a];
\r
770 s68k_poll_detect(a, d);
\r
771 elprintf(EL_CDREGS, "ret = %02x", (u8)d);
\r
774 else if (a >= 0x58 && a < 0x68)
\r
775 d = gfx_cd_read(a & ~1);
\r
776 else d = s68k_reg_read16(a & ~1);
\r
779 elprintf(EL_CDREGS, "ret = %02x", (u8)d);
\r
784 // XXX: verify: probably odd addrs only?
\r
785 if ((a & 0x8000) == 0x0000) {
\r
788 d = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a >> 1) & 0xfff];
\r
789 else if (a >= 0x20) {
\r
791 d = Pico_mcd->pcm.ch[a>>2].addr >> PCM_STEP_SHIFT;
\r
798 return s68k_unmapped_read8(a);
\r
801 static u32 PicoReadS68k16_pr(u32 a)
\r
806 if ((a & 0xfe00) == 0x8000) {
\r
808 elprintf(EL_CDREGS, "s68k_regs r16: [%02x] @ %06x", a, SekPcS68k);
\r
809 if (0x58 <= a && a < 0x68)
\r
810 d = gfx_cd_read(a);
\r
811 else d = s68k_reg_read16(a);
\r
812 elprintf(EL_CDREGS, "ret = %04x", d);
\r
817 if ((a & 0x8000) == 0x0000) {
\r
818 //elprintf(EL_ANOMALY, "FIXME: s68k_pcm r16: [%06x] @%06x", a, SekPcS68k);
\r
821 d = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff];
\r
822 else if (a >= 0x20) {
\r
824 d = Pico_mcd->pcm.ch[a>>2].addr >> PCM_STEP_SHIFT;
\r
825 if (a & 2) d >>= 8;
\r
827 elprintf(EL_CDREGS, "ret = %04x", d);
\r
831 return s68k_unmapped_read16(a);
\r
834 static void PicoWriteS68k8_pr(u32 a, u32 d)
\r
837 if ((a & 0xfe00) == 0x8000) {
\r
839 elprintf(EL_CDREGS, "s68k_regs w8: [%02x] %02x @ %06x", a, d, SekPcS68k);
\r
840 if (0x58 <= a && a < 0x68)
\r
841 gfx_cd_write16(a&~1, (d<<8)|d);
\r
842 else s68k_reg_write8(a,d);
\r
847 if ((a & 0x8000) == 0x0000) {
\r
850 Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff] = d;
\r
852 pcm_write(a>>1, d);
\r
856 s68k_unmapped_write8(a, d);
\r
859 static void PicoWriteS68k16_pr(u32 a, u32 d)
\r
862 if ((a & 0xfe00) == 0x8000) {
\r
864 elprintf(EL_CDREGS, "s68k_regs w16: [%02x] %04x @ %06x", a, d, SekPcS68k);
\r
865 if (a >= 0x58 && a < 0x68)
\r
866 gfx_cd_write16(a, d);
\r
869 // special case, 2 byte writes would be handled differently
\r
871 Pico_mcd->s68k_regs[0xf] = d;
\r
874 s68k_reg_write8(a, d >> 8);
\r
875 s68k_reg_write8(a + 1, d & 0xff);
\r
881 if ((a & 0x8000) == 0x0000) {
\r
884 Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff] = d;
\r
886 pcm_write(a>>1, d & 0xff);
\r
890 s68k_unmapped_write16(a, d);
\r
895 static const void *m68k_cell_read8[] = { PicoReadM68k8_cell0, PicoReadM68k8_cell1 };
\r
896 static const void *m68k_cell_read16[] = { PicoReadM68k16_cell0, PicoReadM68k16_cell1 };
\r
897 static const void *m68k_cell_write8[] = { PicoWriteM68k8_cell0, PicoWriteM68k8_cell1 };
\r
898 static const void *m68k_cell_write16[] = { PicoWriteM68k16_cell0, PicoWriteM68k16_cell1 };
\r
900 static const void *s68k_dec_read8[] = { PicoReadS68k8_dec0, PicoReadS68k8_dec1 };
\r
901 static const void *s68k_dec_read16[] = { PicoReadS68k16_dec0, PicoReadS68k16_dec1 };
\r
903 static const void *s68k_dec_write8[2][4] = {
\r
904 { PicoWriteS68k8_dec_m0b0, PicoWriteS68k8_dec_m1b0, PicoWriteS68k8_dec_m2b0, PicoWriteS68k8_dec_m2b0 },
\r
905 { PicoWriteS68k8_dec_m0b1, PicoWriteS68k8_dec_m1b1, PicoWriteS68k8_dec_m2b1, PicoWriteS68k8_dec_m2b1 },
\r
908 static const void *s68k_dec_write16[2][4] = {
\r
909 { PicoWriteS68k16_dec_m0b0, PicoWriteS68k16_dec_m1b0, PicoWriteS68k16_dec_m2b0, PicoWriteS68k16_dec_m2b0 },
\r
910 { PicoWriteS68k16_dec_m0b1, PicoWriteS68k16_dec_m1b1, PicoWriteS68k16_dec_m2b1, PicoWriteS68k16_dec_m2b1 },
\r
913 // -----------------------------------------------------------------
\r
915 static void remap_prg_window(void)
\r
918 if (Pico_mcd->m.busreq & 2) {
\r
919 void *bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3] >> 6];
\r
920 cpu68k_map_all_ram(0x020000, 0x03ffff, bank, 0);
\r
923 m68k_map_unmap(0x020000, 0x03ffff);
\r
927 static void remap_word_ram(int r3)
\r
933 // 2M mode. XXX: allowing access in all cases for simplicity
\r
934 bank = Pico_mcd->word_ram2M;
\r
935 cpu68k_map_all_ram(0x200000, 0x23ffff, bank, 0);
\r
936 cpu68k_map_all_ram(0x080000, 0x0bffff, bank, 1);
\r
937 // TODO: handle 0x0c0000
\r
941 int m = (r3 & 0x18) >> 3;
\r
942 bank = Pico_mcd->word_ram1M[b0];
\r
943 cpu68k_map_all_ram(0x200000, 0x21ffff, bank, 0);
\r
944 bank = Pico_mcd->word_ram1M[b0 ^ 1];
\r
945 cpu68k_map_all_ram(0x0c0000, 0x0effff, bank, 1);
\r
946 // "cell arrange" on m68k
\r
947 cpu68k_map_set(m68k_read8_map, 0x220000, 0x23ffff, m68k_cell_read8[b0], 1);
\r
948 cpu68k_map_set(m68k_read16_map, 0x220000, 0x23ffff, m68k_cell_read16[b0], 1);
\r
949 cpu68k_map_set(m68k_write8_map, 0x220000, 0x23ffff, m68k_cell_write8[b0], 1);
\r
950 cpu68k_map_set(m68k_write16_map, 0x220000, 0x23ffff, m68k_cell_write16[b0], 1);
\r
951 // "decode format" on s68k
\r
952 cpu68k_map_set(s68k_read8_map, 0x080000, 0x0bffff, s68k_dec_read8[b0 ^ 1], 1);
\r
953 cpu68k_map_set(s68k_read16_map, 0x080000, 0x0bffff, s68k_dec_read16[b0 ^ 1], 1);
\r
954 cpu68k_map_set(s68k_write8_map, 0x080000, 0x0bffff, s68k_dec_write8[b0 ^ 1][m], 1);
\r
955 cpu68k_map_set(s68k_write16_map, 0x080000, 0x0bffff, s68k_dec_write16[b0 ^ 1][m], 1);
\r
959 // update fetchmap..
\r
963 for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x240000; i++)
\r
964 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram2M - 0x200000;
\r
968 for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x220000; i++)
\r
969 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram1M[r3 & 1] - 0x200000;
\r
970 for (i = M68K_FETCHBANK1*0x0c/0x100; (i<<(24-FAMEC_FETCHBITS)) < 0x0e0000; i++)
\r
971 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram1M[(r3&1)^1] - 0x0c0000;
\r
976 void PicoMemStateLoaded(void)
\r
978 int r3 = Pico_mcd->s68k_regs[3];
\r
980 /* after load events */
\r
981 if (r3 & 4) // 1M mode?
\r
982 wram_2M_to_1M(Pico_mcd->word_ram2M);
\r
983 remap_word_ram(r3);
\r
984 remap_prg_window();
\r
986 // restore hint vector
\r
987 *(unsigned short *)(Pico_mcd->bios + 0x72) = Pico_mcd->m.hint_vector;
\r
991 static void m68k_mem_setup_cd(void);
\r
994 PICO_INTERNAL void PicoMemSetupCD(void)
\r
996 // setup default main68k map
\r
999 // main68k map (BIOS mapped by PicoMemSetup()):
\r
1001 if (PicoOpt & POPT_EN_MCD_RAMCART) {
\r
1002 cpu68k_map_set(m68k_read8_map, 0x400000, 0x7fffff, PicoReadM68k8_ramc, 1);
\r
1003 cpu68k_map_set(m68k_read16_map, 0x400000, 0x7fffff, PicoReadM68k16_ramc, 1);
\r
1004 cpu68k_map_set(m68k_write8_map, 0x400000, 0x7fffff, PicoWriteM68k8_ramc, 1);
\r
1005 cpu68k_map_set(m68k_write16_map, 0x400000, 0x7fffff, PicoWriteM68k16_ramc, 1);
\r
1009 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoReadM68k8_io, 1);
\r
1010 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoReadM68k16_io, 1);
\r
1011 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWriteM68k8_io, 1);
\r
1012 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWriteM68k16_io, 1);
\r
1015 cpu68k_map_set(s68k_read8_map, 0x000000, 0xffffff, s68k_unmapped_read8, 1);
\r
1016 cpu68k_map_set(s68k_read16_map, 0x000000, 0xffffff, s68k_unmapped_read16, 1);
\r
1017 cpu68k_map_set(s68k_write8_map, 0x000000, 0xffffff, s68k_unmapped_write8, 1);
\r
1018 cpu68k_map_set(s68k_write16_map, 0x000000, 0xffffff, s68k_unmapped_write16, 1);
\r
1021 cpu68k_map_set(s68k_read8_map, 0x000000, 0x07ffff, Pico_mcd->prg_ram, 0);
\r
1022 cpu68k_map_set(s68k_read16_map, 0x000000, 0x07ffff, Pico_mcd->prg_ram, 0);
\r
1023 cpu68k_map_set(s68k_write8_map, 0x000000, 0x07ffff, Pico_mcd->prg_ram, 0);
\r
1024 cpu68k_map_set(s68k_write16_map, 0x000000, 0x07ffff, Pico_mcd->prg_ram, 0);
\r
1025 cpu68k_map_set(s68k_write8_map, 0x000000, 0x00ffff, PicoWriteS68k8_prgwp, 1);
\r
1026 cpu68k_map_set(s68k_write16_map, 0x000000, 0x00ffff, PicoWriteS68k16_prgwp, 1);
\r
1029 cpu68k_map_set(s68k_read8_map, 0xfe0000, 0xfeffff, PicoReadS68k8_bram, 1);
\r
1030 cpu68k_map_set(s68k_read16_map, 0xfe0000, 0xfeffff, PicoReadS68k16_bram, 1);
\r
1031 cpu68k_map_set(s68k_write8_map, 0xfe0000, 0xfeffff, PicoWriteS68k8_bram, 1);
\r
1032 cpu68k_map_set(s68k_write16_map, 0xfe0000, 0xfeffff, PicoWriteS68k16_bram, 1);
\r
1035 cpu68k_map_set(s68k_read8_map, 0xff0000, 0xffffff, PicoReadS68k8_pr, 1);
\r
1036 cpu68k_map_set(s68k_read16_map, 0xff0000, 0xffffff, PicoReadS68k16_pr, 1);
\r
1037 cpu68k_map_set(s68k_write8_map, 0xff0000, 0xffffff, PicoWriteS68k8_pr, 1);
\r
1038 cpu68k_map_set(s68k_write16_map, 0xff0000, 0xffffff, PicoWriteS68k16_pr, 1);
\r
1041 remap_word_ram(1);
\r
1045 PicoCpuCS68k.read8 = (void *)s68k_read8_map;
\r
1046 PicoCpuCS68k.read16 = (void *)s68k_read16_map;
\r
1047 PicoCpuCS68k.read32 = (void *)s68k_read16_map;
\r
1048 PicoCpuCS68k.write8 = (void *)s68k_write8_map;
\r
1049 PicoCpuCS68k.write16 = (void *)s68k_write16_map;
\r
1050 PicoCpuCS68k.write32 = (void *)s68k_write16_map;
\r
1051 PicoCpuCS68k.checkpc = NULL; /* unused */
\r
1052 PicoCpuCS68k.fetch8 = NULL;
\r
1053 PicoCpuCS68k.fetch16 = NULL;
\r
1054 PicoCpuCS68k.fetch32 = NULL;
\r
1058 PicoCpuFS68k.read_byte = s68k_read8;
\r
1059 PicoCpuFS68k.read_word = s68k_read16;
\r
1060 PicoCpuFS68k.read_long = s68k_read32;
\r
1061 PicoCpuFS68k.write_byte = s68k_write8;
\r
1062 PicoCpuFS68k.write_word = s68k_write16;
\r
1063 PicoCpuFS68k.write_long = s68k_write32;
\r
1065 // setup FAME fetchmap
\r
1069 // by default, point everything to fitst 64k of ROM (BIOS)
\r
1070 for (i = 0; i < M68K_FETCHBANK1; i++)
\r
1071 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom - (i<<(24-FAMEC_FETCHBITS));
\r
1072 // now real ROM (BIOS)
\r
1073 for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < Pico.romsize; i++)
\r
1074 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom;
\r
1076 for (i = M68K_FETCHBANK1*14/16; i < M68K_FETCHBANK1; i++)
\r
1077 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.ram - (i<<(24-FAMEC_FETCHBITS));
\r
1079 // PRG RAM is default
\r
1080 for (i = 0; i < M68K_FETCHBANK1; i++)
\r
1081 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->prg_ram - (i<<(24-FAMEC_FETCHBITS));
\r
1083 for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0x80000; i++)
\r
1084 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->prg_ram;
\r
1085 // WORD RAM 2M area
\r
1086 for (i = M68K_FETCHBANK1*0x08/0x100; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0xc0000; i++)
\r
1087 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram2M - 0x80000;
\r
1088 // remap_word_ram() will setup word ram for both
\r
1092 m68k_mem_setup_cd();
\r
1095 // m68k_poll_addr = m68k_poll_cnt = 0;
\r
1096 s68k_poll_adclk = s68k_poll_cnt = 0;
\r
1101 u32 m68k_read8(u32 a);
\r
1102 u32 m68k_read16(u32 a);
\r
1103 u32 m68k_read32(u32 a);
\r
1104 void m68k_write8(u32 a, u8 d);
\r
1105 void m68k_write16(u32 a, u16 d);
\r
1106 void m68k_write32(u32 a, u32 d);
\r
1108 static unsigned int PicoReadCD8w (unsigned int a) {
\r
1109 return m68ki_cpu_p == &PicoCpuMS68k ? s68k_read8(a) : m68k_read8(a);
\r
1111 static unsigned int PicoReadCD16w(unsigned int a) {
\r
1112 return m68ki_cpu_p == &PicoCpuMS68k ? s68k_read16(a) : m68k_read16(a);
\r
1114 static unsigned int PicoReadCD32w(unsigned int a) {
\r
1115 return m68ki_cpu_p == &PicoCpuMS68k ? s68k_read32(a) : m68k_read32(a);
\r
1117 static void PicoWriteCD8w (unsigned int a, unsigned char d) {
\r
1118 if (m68ki_cpu_p == &PicoCpuMS68k) s68k_write8(a, d); else m68k_write8(a, d);
\r
1120 static void PicoWriteCD16w(unsigned int a, unsigned short d) {
\r
1121 if (m68ki_cpu_p == &PicoCpuMS68k) s68k_write16(a, d); else m68k_write16(a, d);
\r
1123 static void PicoWriteCD32w(unsigned int a, unsigned int d) {
\r
1124 if (m68ki_cpu_p == &PicoCpuMS68k) s68k_write32(a, d); else m68k_write32(a, d);
\r
1127 extern unsigned int (*pm68k_read_memory_8) (unsigned int address);
\r
1128 extern unsigned int (*pm68k_read_memory_16)(unsigned int address);
\r
1129 extern unsigned int (*pm68k_read_memory_32)(unsigned int address);
\r
1130 extern void (*pm68k_write_memory_8) (unsigned int address, unsigned char value);
\r
1131 extern void (*pm68k_write_memory_16)(unsigned int address, unsigned short value);
\r
1132 extern void (*pm68k_write_memory_32)(unsigned int address, unsigned int value);
\r
1134 static void m68k_mem_setup_cd(void)
\r
1136 pm68k_read_memory_8 = PicoReadCD8w;
\r
1137 pm68k_read_memory_16 = PicoReadCD16w;
\r
1138 pm68k_read_memory_32 = PicoReadCD32w;
\r
1139 pm68k_write_memory_8 = PicoWriteCD8w;
\r
1140 pm68k_write_memory_16 = PicoWriteCD16w;
\r
1141 pm68k_write_memory_32 = PicoWriteCD32w;
\r
1143 #endif // EMU_M68K
\r