1 // This is part of Pico Library
\r
3 // (c) Copyright 2004 Dave, All rights reserved.
\r
4 // (c) Copyright 2006-2009 notaz, All rights reserved.
\r
5 // Free for non-commercial use.
\r
7 // For commercial use, separate licencing terms must be obtained.
\r
10 #include "pico_int.h"
\r
13 #include "sound/ym2612.h"
\r
14 #include "sound/sn76496.h"
\r
16 extern unsigned int lastSSRamWrite; // used by serial eeprom code
\r
18 uptr m68k_read8_map [0x1000000 >> M68K_MEM_SHIFT];
\r
19 uptr m68k_read16_map [0x1000000 >> M68K_MEM_SHIFT];
\r
20 uptr m68k_write8_map [0x1000000 >> M68K_MEM_SHIFT];
\r
21 uptr m68k_write16_map[0x1000000 >> M68K_MEM_SHIFT];
\r
23 static void xmap_set(uptr *map, int shift, int start_addr, int end_addr,
\r
24 const void *func_or_mh, int is_func)
\r
26 uptr addr = (uptr)func_or_mh;
\r
27 int mask = (1 << shift) - 1;
\r
30 if ((start_addr & mask) != 0 || (end_addr & mask) != mask) {
\r
31 elprintf(EL_STATUS|EL_ANOMALY, "xmap_set: tried to map bad range: %06x-%06x",
\r
32 start_addr, end_addr);
\r
37 elprintf(EL_STATUS|EL_ANOMALY, "xmap_set: ptr is not aligned: %08lx", addr);
\r
44 for (i = start_addr >> shift; i <= end_addr >> shift; i++) {
\r
47 map[i] |= (uptr)1 << (sizeof(addr) * 8 - 1);
\r
51 void z80_map_set(uptr *map, int start_addr, int end_addr,
\r
52 const void *func_or_mh, int is_func)
\r
54 xmap_set(map, Z80_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func);
\r
57 void cpu68k_map_set(uptr *map, int start_addr, int end_addr,
\r
58 const void *func_or_mh, int is_func)
\r
60 xmap_set(map, M68K_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func);
\r
63 // more specialized/optimized function (does same as above)
\r
64 void cpu68k_map_all_ram(int start_addr, int end_addr, void *ptr, int is_sub)
\r
66 uptr *r8map, *r16map, *w8map, *w16map;
\r
67 uptr addr = (uptr)ptr;
\r
68 int shift = M68K_MEM_SHIFT;
\r
72 r8map = m68k_read8_map;
\r
73 r16map = m68k_read16_map;
\r
74 w8map = m68k_write8_map;
\r
75 w16map = m68k_write16_map;
\r
77 r8map = s68k_read8_map;
\r
78 r16map = s68k_read16_map;
\r
79 w8map = s68k_write8_map;
\r
80 w16map = s68k_write16_map;
\r
85 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
86 r8map[i] = r16map[i] = w8map[i] = w16map[i] = addr;
\r
89 static u32 m68k_unmapped_read8(u32 a)
\r
91 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
\r
92 return 0; // assume pulldown, as if MegaCD2 was attached
\r
95 static u32 m68k_unmapped_read16(u32 a)
\r
97 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
\r
101 static void m68k_unmapped_write8(u32 a, u32 d)
\r
103 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
\r
106 static void m68k_unmapped_write16(u32 a, u32 d)
\r
108 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
\r
111 void m68k_map_unmap(int start_addr, int end_addr)
\r
114 int shift = M68K_MEM_SHIFT;
\r
117 addr = (uptr)m68k_unmapped_read8;
\r
118 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
119 m68k_read8_map[i] = (addr >> 1) | (1 << 31);
\r
121 addr = (uptr)m68k_unmapped_read16;
\r
122 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
123 m68k_read16_map[i] = (addr >> 1) | (1 << 31);
\r
125 addr = (uptr)m68k_unmapped_write8;
\r
126 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
127 m68k_write8_map[i] = (addr >> 1) | (1 << 31);
\r
129 addr = (uptr)m68k_unmapped_write16;
\r
130 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
131 m68k_write16_map[i] = (addr >> 1) | (1 << 31);
\r
134 MAKE_68K_READ8(m68k_read8, m68k_read8_map)
\r
135 MAKE_68K_READ16(m68k_read16, m68k_read16_map)
\r
136 MAKE_68K_READ32(m68k_read32, m68k_read16_map)
\r
137 MAKE_68K_WRITE8(m68k_write8, m68k_write8_map)
\r
138 MAKE_68K_WRITE16(m68k_write16, m68k_write16_map)
\r
139 MAKE_68K_WRITE32(m68k_write32, m68k_write16_map)
\r
141 // -----------------------------------------------------------------
\r
143 static u32 ym2612_read_local_68k(void);
\r
144 static int ym2612_write_local(u32 a, u32 d, int is_from_z80);
\r
145 static void z80_mem_setup(void);
\r
147 #ifdef _ASM_MEMORY_C
\r
148 u32 PicoRead8_sram(u32 a);
\r
149 u32 PicoRead16_sram(u32 a);
\r
152 #ifdef EMU_CORE_DEBUG
\r
153 u32 lastread_a, lastread_d[16]={0,}, lastwrite_cyc_d[16]={0,}, lastwrite_mus_d[16]={0,};
\r
154 int lrp_cyc=0, lrp_mus=0, lwp_cyc=0, lwp_mus=0;
\r
155 extern unsigned int ppop;
\r
159 void log_io(unsigned int addr, int bits, int rw);
\r
160 #elif defined(_MSC_VER)
\r
163 #define log_io(...)
\r
166 #if defined(EMU_C68K)
\r
167 void cyclone_crashed(u32 pc, struct Cyclone *context)
\r
169 elprintf(EL_STATUS|EL_ANOMALY, "%c68k crash detected @ %06x\n",
\r
170 context == &PicoCpuCM68k ? 'm' : 's', pc);
\r
171 context->membase = (u32)Pico.rom;
\r
172 context->pc = (u32)Pico.rom + Pico.romsize;
\r
176 // -----------------------------------------------------------------
\r
179 #ifndef _ASM_MEMORY_C
\r
184 int pad,value,data_reg;
\r
185 pad=~PicoPadInt[i]; // Get inverse of pad MXYZ SACB RLDU
\r
186 data_reg=Pico.ioports[i+1];
\r
188 // orr the bits, which are set as output
\r
189 value = data_reg&(Pico.ioports[i+4]|0x80);
\r
191 if (PicoOpt & POPT_6BTN_PAD)
\r
193 int phase = Pico.m.padTHPhase[i];
\r
195 if(phase == 2 && !(data_reg&0x40)) { // TH
\r
196 value|=(pad&0xc0)>>2; // ?0SA 0000
\r
198 } else if(phase == 3) {
\r
200 value|=(pad&0x30)|((pad>>8)&0xf); // ?1CB MXYZ
\r
202 value|=((pad&0xc0)>>2)|0x0f; // ?0SA 1111
\r
207 if(data_reg&0x40) // TH
\r
208 value|=(pad&0x3f); // ?1CB RLDU
\r
209 else value|=((pad&0xc0)>>2)|(pad&3); // ?0SA 00DU
\r
211 return value; // will mirror later
\r
214 #ifndef _ASM_MEMORY_C
\r
216 static u32 io_ports_read(u32 a)
\r
221 case 0: d = Pico.m.hardware; break; // Hardware value (Version register)
\r
222 case 1: d = PadRead(0); break;
\r
223 case 2: d = PadRead(1); break;
\r
224 default: d = Pico.ioports[a]; break; // IO ports can be used as RAM
\r
229 static void NOINLINE io_ports_write(u32 a, u32 d)
\r
233 // 6 button gamepad: if TH went from 0 to 1, gamepad changes state
\r
234 if (1 <= a && a <= 2 && (PicoOpt & POPT_6BTN_PAD))
\r
236 Pico.m.padDelay[a - 1] = 0;
\r
237 if (!(Pico.ioports[a] & 0x40) && (d & 0x40))
\r
238 Pico.m.padTHPhase[a - 1]++;
\r
241 // certain IO ports can be used as RAM
\r
242 Pico.ioports[a] = d;
\r
245 #endif // _ASM_MEMORY_C
\r
247 void NOINLINE ctl_write_z80busreq(u32 d)
\r
250 elprintf(EL_BUSREQ, "set_zrun: %i->%i [%i] @%06x", Pico.m.z80Run, d, SekCyclesDone(), SekPc);
\r
251 if (d ^ Pico.m.z80Run)
\r
255 z80_cycle_cnt = cycles_68k_to_z80(SekCyclesDone());
\r
259 z80stopCycle = SekCyclesDone();
\r
260 if ((PicoOpt&POPT_EN_Z80) && !Pico.m.z80_reset) {
\r
262 PicoSyncZ80(z80stopCycle);
\r
263 pprof_end_sub(m68k);
\r
270 void NOINLINE ctl_write_z80reset(u32 d)
\r
273 elprintf(EL_BUSREQ, "set_zreset: %i->%i [%i] @%06x", Pico.m.z80_reset, d, SekCyclesDone(), SekPc);
\r
274 if (d ^ Pico.m.z80_reset)
\r
278 if ((PicoOpt&POPT_EN_Z80) && Pico.m.z80Run) {
\r
280 PicoSyncZ80(SekCyclesDone());
\r
281 pprof_end_sub(m68k);
\r
288 z80_cycle_cnt = cycles_68k_to_z80(SekCyclesDone());
\r
291 Pico.m.z80_reset = d;
\r
295 // -----------------------------------------------------------------
\r
297 #ifndef _ASM_MEMORY_C
\r
299 // cart (save) RAM area (usually 0x200000 - ...)
\r
300 static u32 PicoRead8_sram(u32 a)
\r
303 if (SRam.start <= a && a <= SRam.end && (Pico.m.sram_reg & SRR_MAPPED))
\r
305 if (SRam.flags & SRF_EEPROM) {
\r
310 d = *(u8 *)(SRam.data - SRam.start + a);
\r
311 elprintf(EL_SRAMIO, "sram r8 [%06x] %02x @ %06x", a, d, SekPc);
\r
315 // XXX: this is banking unfriendly
\r
316 if (a < Pico.romsize)
\r
317 return Pico.rom[a ^ 1];
\r
319 return m68k_unmapped_read8(a);
\r
322 static u32 PicoRead16_sram(u32 a)
\r
325 if (SRam.start <= a && a <= SRam.end && (Pico.m.sram_reg & SRR_MAPPED))
\r
327 if (SRam.flags & SRF_EEPROM)
\r
330 u8 *pm = (u8 *)(SRam.data - SRam.start + a);
\r
334 elprintf(EL_SRAMIO, "sram r16 [%06x] %04x @ %06x", a, d, SekPc);
\r
338 if (a < Pico.romsize)
\r
339 return *(u16 *)(Pico.rom + a);
\r
341 return m68k_unmapped_read16(a);
\r
344 #endif // _ASM_MEMORY_C
\r
346 static void PicoWrite8_sram(u32 a, u32 d)
\r
348 if (a > SRam.end || a < SRam.start || !(Pico.m.sram_reg & SRR_MAPPED)) {
\r
349 m68k_unmapped_write8(a, d);
\r
353 elprintf(EL_SRAMIO, "sram w8 [%06x] %02x @ %06x", a, d & 0xff, SekPc);
\r
354 if (SRam.flags & SRF_EEPROM)
\r
356 EEPROM_write8(a, d);
\r
359 u8 *pm = (u8 *)(SRam.data - SRam.start + a);
\r
360 if (*pm != (u8)d) {
\r
367 static void PicoWrite16_sram(u32 a, u32 d)
\r
369 if (a > SRam.end || a < SRam.start || !(Pico.m.sram_reg & SRR_MAPPED)) {
\r
370 m68k_unmapped_write16(a, d);
\r
374 elprintf(EL_SRAMIO, "sram w16 [%06x] %04x @ %06x", a, d & 0xffff, SekPc);
\r
375 if (SRam.flags & SRF_EEPROM)
\r
380 // XXX: hardware could easily use MSB too..
\r
381 u8 *pm = (u8 *)(SRam.data - SRam.start + a);
\r
382 if (*pm != (u8)d) {
\r
389 // z80 area (0xa00000 - 0xa0ffff)
\r
390 // TODO: verify mirrors VDP and bank reg (bank area mirroring verified)
\r
391 static u32 PicoRead8_z80(u32 a)
\r
394 if ((Pico.m.z80Run & 1) || Pico.m.z80_reset) {
\r
395 elprintf(EL_ANOMALY, "68k z80 read with no bus! [%06x] @ %06x", a, SekPc);
\r
396 // open bus. Pulled down if MegaCD2 is attached.
\r
400 if ((a & 0x4000) == 0x0000)
\r
401 d = Pico.zram[a & 0x1fff];
\r
402 else if ((a & 0x6000) == 0x4000) // 0x4000-0x5fff
\r
403 d = ym2612_read_local_68k();
\r
405 elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc);
\r
409 static u32 PicoRead16_z80(u32 a)
\r
411 u32 d = PicoRead8_z80(a);
\r
412 return d | (d << 8);
\r
415 static void PicoWrite8_z80(u32 a, u32 d)
\r
417 if ((Pico.m.z80Run & 1) || Pico.m.z80_reset) {
\r
418 // verified on real hw
\r
419 elprintf(EL_ANOMALY, "68k z80 write with no bus or reset! [%06x] %02x @ %06x", a, d&0xff, SekPc);
\r
423 if ((a & 0x4000) == 0x0000) { // z80 RAM
\r
424 SekCyclesBurn(2); // hack
\r
425 Pico.zram[a & 0x1fff] = (u8)d;
\r
428 if ((a & 0x6000) == 0x4000) { // FM Sound
\r
429 if (PicoOpt & POPT_EN_FM)
\r
430 emustatus |= ym2612_write_local(a&3, d&0xff, 0)&1;
\r
433 // TODO: probably other VDP access too? Maybe more mirrors?
\r
434 if ((a & 0x7ff9) == 0x7f11) { // PSG Sound
\r
435 if (PicoOpt & POPT_EN_PSG)
\r
439 if ((a & 0x7f00) == 0x6000) // Z80 BANK register
\r
441 Pico.m.z80_bank68k >>= 1;
\r
442 Pico.m.z80_bank68k |= d << 8;
\r
443 Pico.m.z80_bank68k &= 0x1ff; // 9 bits and filled in the new top one
\r
444 elprintf(EL_Z80BNK, "z80 bank=%06x", Pico.m.z80_bank68k << 15);
\r
447 elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %02x @ %06x", a, d&0xff, SekPc);
\r
450 static void PicoWrite16_z80(u32 a, u32 d)
\r
452 // for RAM, only most significant byte is sent
\r
453 // TODO: verify remaining accesses
\r
454 PicoWrite8_z80(a, d >> 8);
\r
457 #ifndef _ASM_MEMORY_C
\r
459 // IO/control area (0xa10000 - 0xa1ffff)
\r
460 u32 PicoRead8_io(u32 a)
\r
464 if ((a & 0xffe0) == 0x0000) { // I/O ports
\r
465 d = io_ports_read(a);
\r
469 // faking open bus (MegaCD pulldowns don't work here curiously)
\r
470 d = Pico.m.rotate++;
\r
473 if ((a & 0xfc00) == 0x1000) {
\r
474 // bit8 seems to be readable in this range
\r
478 if ((a & 0xff01) == 0x1100) { // z80 busreq (verified)
\r
479 d |= (Pico.m.z80Run | Pico.m.z80_reset) & 1;
\r
480 elprintf(EL_BUSREQ, "get_zrun: %02x [%i] @%06x", d, SekCyclesDone(), SekPc);
\r
485 if (PicoOpt & POPT_EN_32X) {
\r
486 d = PicoRead8_32x(a);
\r
490 d = m68k_unmapped_read8(a);
\r
495 u32 PicoRead16_io(u32 a)
\r
499 if ((a & 0xffe0) == 0x0000) { // I/O ports
\r
500 d = io_ports_read(a);
\r
506 d = (Pico.m.rotate += 0x41);
\r
507 d ^= (d << 5) ^ (d << 8);
\r
509 // bit8 seems to be readable in this range
\r
510 if ((a & 0xfc00) == 0x1000) {
\r
513 if ((a & 0xff00) == 0x1100) { // z80 busreq
\r
514 d |= ((Pico.m.z80Run | Pico.m.z80_reset) & 1) << 8;
\r
515 elprintf(EL_BUSREQ, "get_zrun: %04x [%i] @%06x", d, SekCyclesDone(), SekPc);
\r
520 if (PicoOpt & POPT_EN_32X) {
\r
521 d = PicoRead16_32x(a);
\r
525 d = m68k_unmapped_read16(a);
\r
530 void PicoWrite8_io(u32 a, u32 d)
\r
532 if ((a & 0xffe1) == 0x0001) { // I/O ports (verified: only LSB!)
\r
533 io_ports_write(a, d);
\r
536 if ((a & 0xff01) == 0x1100) { // z80 busreq
\r
537 ctl_write_z80busreq(d);
\r
540 if ((a & 0xff01) == 0x1200) { // z80 reset
\r
541 ctl_write_z80reset(d);
\r
544 if (a == 0xa130f1) { // sram access register
\r
545 elprintf(EL_SRAMIO, "sram reg=%02x", d);
\r
546 Pico.m.sram_reg &= ~(SRR_MAPPED|SRR_READONLY);
\r
547 Pico.m.sram_reg |= (u8)(d & 3);
\r
550 if (PicoOpt & POPT_EN_32X) {
\r
551 PicoWrite8_32x(a, d);
\r
555 m68k_unmapped_write8(a, d);
\r
558 void PicoWrite16_io(u32 a, u32 d)
\r
560 if ((a & 0xffe0) == 0x0000) { // I/O ports (verified: only LSB!)
\r
561 io_ports_write(a, d);
\r
564 if ((a & 0xff00) == 0x1100) { // z80 busreq
\r
565 ctl_write_z80busreq(d >> 8);
\r
568 if ((a & 0xff00) == 0x1200) { // z80 reset
\r
569 ctl_write_z80reset(d >> 8);
\r
572 if (a == 0xa130f0) { // sram access register
\r
573 elprintf(EL_SRAMIO, "sram reg=%02x", d);
\r
574 Pico.m.sram_reg &= ~(SRR_MAPPED|SRR_READONLY);
\r
575 Pico.m.sram_reg |= (u8)(d & 3);
\r
578 if (PicoOpt & POPT_EN_32X) {
\r
579 PicoWrite16_32x(a, d);
\r
582 m68k_unmapped_write16(a, d);
\r
585 #endif // _ASM_MEMORY_C
\r
587 // VDP area (0xc00000 - 0xdfffff)
\r
588 // TODO: verify if lower byte goes to PSG on word writes
\r
589 static u32 PicoRead8_vdp(u32 a)
\r
591 if ((a & 0x00e0) == 0x0000)
\r
592 return PicoVideoRead8(a);
\r
594 elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc);
\r
598 static u32 PicoRead16_vdp(u32 a)
\r
600 if ((a & 0x00e0) == 0x0000)
\r
601 return PicoVideoRead(a);
\r
603 elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc);
\r
607 static void PicoWrite8_vdp(u32 a, u32 d)
\r
609 if ((a & 0x00f9) == 0x0011) { // PSG Sound
\r
610 if (PicoOpt & POPT_EN_PSG)
\r
614 if ((a & 0x00e0) == 0x0000) {
\r
616 PicoVideoWrite(a, d | (d << 8));
\r
620 elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %02x @%06x", a, d & 0xff, SekPc);
\r
623 static void PicoWrite16_vdp(u32 a, u32 d)
\r
625 if ((a & 0x00f9) == 0x0010) { // PSG Sound
\r
626 if (PicoOpt & POPT_EN_PSG)
\r
630 if ((a & 0x00e0) == 0x0000) {
\r
631 PicoVideoWrite(a, d);
\r
635 elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %04x @%06x", a, d & 0xffff, SekPc);
\r
638 // -----------------------------------------------------------------
\r
641 static void m68k_mem_setup(void);
\r
644 PICO_INTERNAL void PicoMemSetup(void)
\r
648 // setup the memory map
\r
649 cpu68k_map_set(m68k_read8_map, 0x000000, 0xffffff, m68k_unmapped_read8, 1);
\r
650 cpu68k_map_set(m68k_read16_map, 0x000000, 0xffffff, m68k_unmapped_read16, 1);
\r
651 cpu68k_map_set(m68k_write8_map, 0x000000, 0xffffff, m68k_unmapped_write8, 1);
\r
652 cpu68k_map_set(m68k_write16_map, 0x000000, 0xffffff, m68k_unmapped_write16, 1);
\r
655 // align to bank size. We know ROM loader allocated enough for this
\r
656 mask = (1 << M68K_MEM_SHIFT) - 1;
\r
657 rs = (Pico.romsize + mask) & ~mask;
\r
658 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico.rom, 0);
\r
659 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico.rom, 0);
\r
661 // Common case of on-cart (save) RAM, usually at 0x200000-...
\r
662 if ((SRam.flags & SRF_ENABLED) && SRam.data != NULL) {
\r
663 rs = SRam.end - SRam.start;
\r
664 rs = (rs + mask) & ~mask;
\r
665 if (SRam.start + rs >= 0x1000000)
\r
666 rs = 0x1000000 - SRam.start;
\r
667 cpu68k_map_set(m68k_read8_map, SRam.start, SRam.start + rs - 1, PicoRead8_sram, 1);
\r
668 cpu68k_map_set(m68k_read16_map, SRam.start, SRam.start + rs - 1, PicoRead16_sram, 1);
\r
669 cpu68k_map_set(m68k_write8_map, SRam.start, SRam.start + rs - 1, PicoWrite8_sram, 1);
\r
670 cpu68k_map_set(m68k_write16_map, SRam.start, SRam.start + rs - 1, PicoWrite16_sram, 1);
\r
674 cpu68k_map_set(m68k_read8_map, 0xa00000, 0xa0ffff, PicoRead8_z80, 1);
\r
675 cpu68k_map_set(m68k_read16_map, 0xa00000, 0xa0ffff, PicoRead16_z80, 1);
\r
676 cpu68k_map_set(m68k_write8_map, 0xa00000, 0xa0ffff, PicoWrite8_z80, 1);
\r
677 cpu68k_map_set(m68k_write16_map, 0xa00000, 0xa0ffff, PicoWrite16_z80, 1);
\r
679 // IO/control region
\r
680 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_io, 1);
\r
681 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_io, 1);
\r
682 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_io, 1);
\r
683 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_io, 1);
\r
686 for (a = 0xc00000; a < 0xe00000; a += 0x010000) {
\r
687 if ((a & 0xe700e0) != 0xc00000)
\r
689 cpu68k_map_set(m68k_read8_map, a, a + 0xffff, PicoRead8_vdp, 1);
\r
690 cpu68k_map_set(m68k_read16_map, a, a + 0xffff, PicoRead16_vdp, 1);
\r
691 cpu68k_map_set(m68k_write8_map, a, a + 0xffff, PicoWrite8_vdp, 1);
\r
692 cpu68k_map_set(m68k_write16_map, a, a + 0xffff, PicoWrite16_vdp, 1);
\r
695 // RAM and it's mirrors
\r
696 for (a = 0xe00000; a < 0x1000000; a += 0x010000) {
\r
697 cpu68k_map_set(m68k_read8_map, a, a + 0xffff, Pico.ram, 0);
\r
698 cpu68k_map_set(m68k_read16_map, a, a + 0xffff, Pico.ram, 0);
\r
699 cpu68k_map_set(m68k_write8_map, a, a + 0xffff, Pico.ram, 0);
\r
700 cpu68k_map_set(m68k_write16_map, a, a + 0xffff, Pico.ram, 0);
\r
703 // Setup memory callbacks:
\r
705 PicoCpuCM68k.read8 = (void *)m68k_read8_map;
\r
706 PicoCpuCM68k.read16 = (void *)m68k_read16_map;
\r
707 PicoCpuCM68k.read32 = (void *)m68k_read16_map;
\r
708 PicoCpuCM68k.write8 = (void *)m68k_write8_map;
\r
709 PicoCpuCM68k.write16 = (void *)m68k_write16_map;
\r
710 PicoCpuCM68k.write32 = (void *)m68k_write16_map;
\r
711 PicoCpuCM68k.checkpc = NULL; /* unused */
\r
712 PicoCpuCM68k.fetch8 = NULL;
\r
713 PicoCpuCM68k.fetch16 = NULL;
\r
714 PicoCpuCM68k.fetch32 = NULL;
\r
717 PicoCpuFM68k.read_byte = m68k_read8;
\r
718 PicoCpuFM68k.read_word = m68k_read16;
\r
719 PicoCpuFM68k.read_long = m68k_read32;
\r
720 PicoCpuFM68k.write_byte = m68k_write8;
\r
721 PicoCpuFM68k.write_word = m68k_write16;
\r
722 PicoCpuFM68k.write_long = m68k_write32;
\r
724 // setup FAME fetchmap
\r
727 // by default, point everything to first 64k of ROM
\r
728 for (i = 0; i < M68K_FETCHBANK1; i++)
\r
729 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom - (i<<(24-FAMEC_FETCHBITS));
\r
731 for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < Pico.romsize; i++)
\r
732 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom;
\r
734 for (i = M68K_FETCHBANK1*14/16; i < M68K_FETCHBANK1; i++)
\r
735 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.ram - (i<<(24-FAMEC_FETCHBITS));
\r
746 unsigned int (*pm68k_read_memory_8) (unsigned int address) = NULL;
\r
747 unsigned int (*pm68k_read_memory_16)(unsigned int address) = NULL;
\r
748 unsigned int (*pm68k_read_memory_32)(unsigned int address) = NULL;
\r
749 void (*pm68k_write_memory_8) (unsigned int address, unsigned char value) = NULL;
\r
750 void (*pm68k_write_memory_16)(unsigned int address, unsigned short value) = NULL;
\r
751 void (*pm68k_write_memory_32)(unsigned int address, unsigned int value) = NULL;
\r
753 /* it appears that Musashi doesn't always mask the unused bits */
\r
754 unsigned int m68k_read_memory_8 (unsigned int address) { return pm68k_read_memory_8 (address) & 0xff; }
\r
755 unsigned int m68k_read_memory_16(unsigned int address) { return pm68k_read_memory_16(address) & 0xffff; }
\r
756 unsigned int m68k_read_memory_32(unsigned int address) { return pm68k_read_memory_32(address); }
\r
757 void m68k_write_memory_8 (unsigned int address, unsigned int value) { pm68k_write_memory_8 (address, (u8)value); }
\r
758 void m68k_write_memory_16(unsigned int address, unsigned int value) { pm68k_write_memory_16(address,(u16)value); }
\r
759 void m68k_write_memory_32(unsigned int address, unsigned int value) { pm68k_write_memory_32(address, value); }
\r
761 static void m68k_mem_setup(void)
\r
763 pm68k_read_memory_8 = m68k_read8;
\r
764 pm68k_read_memory_16 = m68k_read16;
\r
765 pm68k_read_memory_32 = m68k_read32;
\r
766 pm68k_write_memory_8 = m68k_write8;
\r
767 pm68k_write_memory_16 = m68k_write16;
\r
768 pm68k_write_memory_32 = m68k_write32;
\r
773 // -----------------------------------------------------------------
\r
775 static int get_scanline(int is_from_z80)
\r
778 int cycles = z80_cyclesDone();
\r
779 while (cycles - z80_scanline_cycles >= 228)
\r
780 z80_scanline++, z80_scanline_cycles += 228;
\r
781 return z80_scanline;
\r
784 return Pico.m.scanline;
\r
787 /* probably should not be in this file, but it's near related code here */
\r
788 void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new)
\r
790 int xcycles = z80_cycles << 8;
\r
792 /* check for overflows */
\r
793 if ((mode_old & 4) && xcycles > timer_a_next_oflow)
\r
794 ym2612.OPN.ST.status |= 1;
\r
796 if ((mode_old & 8) && xcycles > timer_b_next_oflow)
\r
797 ym2612.OPN.ST.status |= 2;
\r
799 /* update timer a */
\r
801 while (xcycles > timer_a_next_oflow)
\r
802 timer_a_next_oflow += timer_a_step;
\r
804 if ((mode_old ^ mode_new) & 1) // turning on/off
\r
807 timer_a_next_oflow = TIMER_NO_OFLOW;
\r
809 timer_a_next_oflow = xcycles + timer_a_step;
\r
812 elprintf(EL_YMTIMER, "timer a upd to %i @ %i", timer_a_next_oflow>>8, z80_cycles);
\r
814 /* update timer b */
\r
816 while (xcycles > timer_b_next_oflow)
\r
817 timer_b_next_oflow += timer_b_step;
\r
819 if ((mode_old ^ mode_new) & 2)
\r
822 timer_b_next_oflow = TIMER_NO_OFLOW;
\r
824 timer_b_next_oflow = xcycles + timer_b_step;
\r
827 elprintf(EL_YMTIMER, "timer b upd to %i @ %i", timer_b_next_oflow>>8, z80_cycles);
\r
830 // ym2612 DAC and timer I/O handlers for z80
\r
831 static int ym2612_write_local(u32 a, u32 d, int is_from_z80)
\r
836 if (a == 1 && ym2612.OPN.ST.address == 0x2a) /* DAC data */
\r
838 int scanline = get_scanline(is_from_z80);
\r
839 //elprintf(EL_STATUS, "%03i -> %03i dac w %08x z80 %i", PsndDacLine, scanline, d, is_from_z80);
\r
840 ym2612.dacout = ((int)d - 0x80) << 6;
\r
841 if (PsndOut && ym2612.dacen && scanline >= PsndDacLine)
\r
842 PsndDoDAC(scanline);
\r
848 case 0: /* address port 0 */
\r
849 ym2612.OPN.ST.address = d;
\r
850 ym2612.addr_A1 = 0;
\r
852 if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, -1);
\r
856 case 1: /* data port 0 */
\r
857 if (ym2612.addr_A1 != 0)
\r
860 addr = ym2612.OPN.ST.address;
\r
861 ym2612.REGS[addr] = d;
\r
865 case 0x24: // timer A High 8
\r
866 case 0x25: { // timer A Low 2
\r
867 int TAnew = (addr == 0x24) ? ((ym2612.OPN.ST.TA & 0x03)|(((int)d)<<2))
\r
868 : ((ym2612.OPN.ST.TA & 0x3fc)|(d&3));
\r
869 if (ym2612.OPN.ST.TA != TAnew)
\r
871 //elprintf(EL_STATUS, "timer a set %i", TAnew);
\r
872 ym2612.OPN.ST.TA = TAnew;
\r
873 //ym2612.OPN.ST.TAC = (1024-TAnew)*18;
\r
874 //ym2612.OPN.ST.TAT = 0;
\r
875 timer_a_step = TIMER_A_TICK_ZCYCLES * (1024 - TAnew);
\r
876 if (ym2612.OPN.ST.mode & 1) {
\r
877 // this is not right, should really be done on overflow only
\r
878 int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());
\r
879 timer_a_next_oflow = (cycles << 8) + timer_a_step;
\r
881 elprintf(EL_YMTIMER, "timer a set to %i, %i", 1024 - TAnew, timer_a_next_oflow>>8);
\r
885 case 0x26: // timer B
\r
886 if (ym2612.OPN.ST.TB != d) {
\r
887 //elprintf(EL_STATUS, "timer b set %i", d);
\r
888 ym2612.OPN.ST.TB = d;
\r
889 //ym2612.OPN.ST.TBC = (256-d) * 288;
\r
890 //ym2612.OPN.ST.TBT = 0;
\r
891 timer_b_step = TIMER_B_TICK_ZCYCLES * (256 - d); // 262800
\r
892 if (ym2612.OPN.ST.mode & 2) {
\r
893 int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());
\r
894 timer_b_next_oflow = (cycles << 8) + timer_b_step;
\r
896 elprintf(EL_YMTIMER, "timer b set to %i, %i", 256 - d, timer_b_next_oflow>>8);
\r
899 case 0x27: { /* mode, timer control */
\r
900 int old_mode = ym2612.OPN.ST.mode;
\r
901 int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());
\r
902 ym2612.OPN.ST.mode = d;
\r
904 elprintf(EL_YMTIMER, "st mode %02x", d);
\r
905 ym2612_sync_timers(cycles, old_mode, d);
\r
907 /* reset Timer a flag */
\r
909 ym2612.OPN.ST.status &= ~1;
\r
911 /* reset Timer b flag */
\r
913 ym2612.OPN.ST.status &= ~2;
\r
915 if ((d ^ old_mode) & 0xc0) {
\r
917 if (PicoOpt & POPT_EXT_FM) return YM2612Write_940(a, d, get_scanline(is_from_z80));
\r
923 case 0x2b: { /* DAC Sel (YM2612) */
\r
924 int scanline = get_scanline(is_from_z80);
\r
925 ym2612.dacen = d & 0x80;
\r
926 if (d & 0x80) PsndDacLine = scanline;
\r
928 if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, scanline);
\r
935 case 2: /* address port 1 */
\r
936 ym2612.OPN.ST.address = d;
\r
937 ym2612.addr_A1 = 1;
\r
939 if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, -1);
\r
943 case 3: /* data port 1 */
\r
944 if (ym2612.addr_A1 != 1)
\r
947 addr = ym2612.OPN.ST.address | 0x100;
\r
948 ym2612.REGS[addr] = d;
\r
953 if (PicoOpt & POPT_EXT_FM)
\r
954 return YM2612Write_940(a, d, get_scanline(is_from_z80));
\r
956 return YM2612Write_(a, d);
\r
960 #define ym2612_read_local() \
\r
961 if (xcycles >= timer_a_next_oflow) \
\r
962 ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 1; \
\r
963 if (xcycles >= timer_b_next_oflow) \
\r
964 ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 2
\r
966 static u32 ym2612_read_local_z80(void)
\r
968 int xcycles = z80_cyclesDone() << 8;
\r
970 ym2612_read_local();
\r
972 elprintf(EL_YMTIMER, "timer z80 read %i, sched %i, %i @ %i|%i", ym2612.OPN.ST.status,
\r
973 timer_a_next_oflow>>8, timer_b_next_oflow>>8, xcycles >> 8, (xcycles >> 8) / 228);
\r
974 return ym2612.OPN.ST.status;
\r
977 static u32 ym2612_read_local_68k(void)
\r
979 int xcycles = cycles_68k_to_z80(SekCyclesDone()) << 8;
\r
981 ym2612_read_local();
\r
983 elprintf(EL_YMTIMER, "timer 68k read %i, sched %i, %i @ %i|%i", ym2612.OPN.ST.status,
\r
984 timer_a_next_oflow>>8, timer_b_next_oflow>>8, xcycles >> 8, (xcycles >> 8) / 228);
\r
985 return ym2612.OPN.ST.status;
\r
988 void ym2612_pack_state(void)
\r
990 // timers are saved as tick counts, in 16.16 int format
\r
991 int tac, tat = 0, tbc, tbt = 0;
\r
992 tac = 1024 - ym2612.OPN.ST.TA;
\r
993 tbc = 256 - ym2612.OPN.ST.TB;
\r
994 if (timer_a_next_oflow != TIMER_NO_OFLOW)
\r
995 tat = (int)((double)(timer_a_step - timer_a_next_oflow) / (double)timer_a_step * tac * 65536);
\r
996 if (timer_b_next_oflow != TIMER_NO_OFLOW)
\r
997 tbt = (int)((double)(timer_b_step - timer_b_next_oflow) / (double)timer_b_step * tbc * 65536);
\r
998 elprintf(EL_YMTIMER, "save: timer a %i/%i", tat >> 16, tac);
\r
999 elprintf(EL_YMTIMER, "save: timer b %i/%i", tbt >> 16, tbc);
\r
1002 if (PicoOpt & POPT_EXT_FM)
\r
1003 YM2612PicoStateSave2_940(tat, tbt);
\r
1006 YM2612PicoStateSave2(tat, tbt);
\r
1009 void ym2612_unpack_state(void)
\r
1011 int i, ret, tac, tat, tbc, tbt;
\r
1012 YM2612PicoStateLoad();
\r
1014 // feed all the registers and update internal state
\r
1015 for (i = 0x20; i < 0xA0; i++) {
\r
1016 ym2612_write_local(0, i, 0);
\r
1017 ym2612_write_local(1, ym2612.REGS[i], 0);
\r
1019 for (i = 0x30; i < 0xA0; i++) {
\r
1020 ym2612_write_local(2, i, 0);
\r
1021 ym2612_write_local(3, ym2612.REGS[i|0x100], 0);
\r
1023 for (i = 0xAF; i >= 0xA0; i--) { // must apply backwards
\r
1024 ym2612_write_local(2, i, 0);
\r
1025 ym2612_write_local(3, ym2612.REGS[i|0x100], 0);
\r
1026 ym2612_write_local(0, i, 0);
\r
1027 ym2612_write_local(1, ym2612.REGS[i], 0);
\r
1029 for (i = 0xB0; i < 0xB8; i++) {
\r
1030 ym2612_write_local(0, i, 0);
\r
1031 ym2612_write_local(1, ym2612.REGS[i], 0);
\r
1032 ym2612_write_local(2, i, 0);
\r
1033 ym2612_write_local(3, ym2612.REGS[i|0x100], 0);
\r
1037 if (PicoOpt & POPT_EXT_FM)
\r
1038 ret = YM2612PicoStateLoad2_940(&tat, &tbt);
\r
1041 ret = YM2612PicoStateLoad2(&tat, &tbt);
\r
1043 elprintf(EL_STATUS, "old ym2612 state");
\r
1044 return; // no saved timers
\r
1047 tac = (1024 - ym2612.OPN.ST.TA) << 16;
\r
1048 tbc = (256 - ym2612.OPN.ST.TB) << 16;
\r
1049 if (ym2612.OPN.ST.mode & 1)
\r
1050 timer_a_next_oflow = (int)((double)(tac - tat) / (double)tac * timer_a_step);
\r
1052 timer_a_next_oflow = TIMER_NO_OFLOW;
\r
1053 if (ym2612.OPN.ST.mode & 2)
\r
1054 timer_b_next_oflow = (int)((double)(tbc - tbt) / (double)tbc * timer_b_step);
\r
1056 timer_b_next_oflow = TIMER_NO_OFLOW;
\r
1057 elprintf(EL_YMTIMER, "load: %i/%i, timer_a_next_oflow %i", tat>>16, tac>>16, timer_a_next_oflow >> 8);
\r
1058 elprintf(EL_YMTIMER, "load: %i/%i, timer_b_next_oflow %i", tbt>>16, tbc>>16, timer_b_next_oflow >> 8);
\r
1061 #if defined(NO_32X) && defined(_ASM_MEMORY_C)
\r
1062 // referenced by asm code
\r
1063 u32 PicoRead8_32x(u32 a) { return 0; }
\r
1064 u32 PicoRead16_32x(u32 a) { return 0; }
\r
1065 void PicoWrite8_32x(u32 a, u32 d) {}
\r
1066 void PicoWrite16_32x(u32 a, u32 d) {}
\r
1069 // -----------------------------------------------------------------
\r
1070 // z80 memhandlers
\r
1072 static unsigned char z80_md_vdp_read(unsigned short a)
\r
1075 elprintf(EL_ANOMALY, "z80 invalid r8 [%06x] %02x", a, 0xff);
\r
1079 static unsigned char z80_md_bank_read(unsigned short a)
\r
1081 unsigned int addr68k;
\r
1082 unsigned char ret;
\r
1084 addr68k = Pico.m.z80_bank68k<<15;
\r
1085 addr68k += a & 0x7fff;
\r
1087 ret = m68k_read8(addr68k);
\r
1089 elprintf(EL_Z80BNK, "z80->68k r8 [%06x] %02x", addr68k, ret);
\r
1093 static void z80_md_ym2612_write(unsigned int a, unsigned char data)
\r
1095 if (PicoOpt & POPT_EN_FM)
\r
1096 emustatus |= ym2612_write_local(a, data, 1) & 1;
\r
1099 static void z80_md_vdp_br_write(unsigned int a, unsigned char data)
\r
1101 // TODO: allow full VDP access
\r
1102 if ((a&0xfff9) == 0x7f11) // 7f11 7f13 7f15 7f17
\r
1104 if (PicoOpt & POPT_EN_PSG)
\r
1105 SN76496Write(data);
\r
1109 if ((a>>8) == 0x60)
\r
1111 Pico.m.z80_bank68k >>= 1;
\r
1112 Pico.m.z80_bank68k |= data << 8;
\r
1113 Pico.m.z80_bank68k &= 0x1ff; // 9 bits and filled in the new top one
\r
1117 elprintf(EL_ANOMALY, "z80 invalid w8 [%06x] %02x", a, data);
\r
1120 static void z80_md_bank_write(unsigned int a, unsigned char data)
\r
1122 unsigned int addr68k;
\r
1124 addr68k = Pico.m.z80_bank68k << 15;
\r
1125 addr68k += a & 0x7fff;
\r
1127 elprintf(EL_Z80BNK, "z80->68k w8 [%06x] %02x", addr68k, data);
\r
1128 m68k_write8(addr68k, data);
\r
1131 // -----------------------------------------------------------------
\r
1133 static unsigned char z80_md_in(unsigned short p)
\r
1135 elprintf(EL_ANOMALY, "Z80 port %04x read", p);
\r
1139 static void z80_md_out(unsigned short p, unsigned char d)
\r
1141 elprintf(EL_ANOMALY, "Z80 port %04x write %02x", p, d);
\r
1144 static void z80_mem_setup(void)
\r
1146 z80_map_set(z80_read_map, 0x0000, 0x1fff, Pico.zram, 0);
\r
1147 z80_map_set(z80_read_map, 0x2000, 0x3fff, Pico.zram, 0);
\r
1148 z80_map_set(z80_read_map, 0x4000, 0x5fff, ym2612_read_local_z80, 1);
\r
1149 z80_map_set(z80_read_map, 0x6000, 0x7fff, z80_md_vdp_read, 1);
\r
1150 z80_map_set(z80_read_map, 0x8000, 0xffff, z80_md_bank_read, 1);
\r
1152 z80_map_set(z80_write_map, 0x0000, 0x1fff, Pico.zram, 0);
\r
1153 z80_map_set(z80_write_map, 0x2000, 0x3fff, Pico.zram, 0);
\r
1154 z80_map_set(z80_write_map, 0x4000, 0x5fff, z80_md_ym2612_write, 1);
\r
1155 z80_map_set(z80_write_map, 0x6000, 0x7fff, z80_md_vdp_br_write, 1);
\r
1156 z80_map_set(z80_write_map, 0x8000, 0xffff, z80_md_bank_write, 1);
\r
1159 drZ80.z80_in = z80_md_in;
\r
1160 drZ80.z80_out = z80_md_out;
\r
1163 Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (FPTR)Pico.zram); // main RAM
\r
1164 Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (FPTR)Pico.zram); // mirror
\r
1165 Cz80_Set_INPort(&CZ80, z80_md_in);
\r
1166 Cz80_Set_OUTPort(&CZ80, z80_md_out);
\r