3 * (c) Copyright Dave, 2004
\r
4 * (C) notaz, 2006-2010
\r
6 * This work is licensed under the terms of MAME license.
\r
7 * See COPYING file in the top-level directory.
\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
27 // workaround bug (segfault) in
\r
28 // Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
\r
31 uptr addr = (uptr)func_or_mh;
\r
32 int mask = (1 << shift) - 1;
\r
35 if ((start_addr & mask) != 0 || (end_addr & mask) != mask) {
\r
36 elprintf(EL_STATUS|EL_ANOMALY, "xmap_set: tried to map bad range: %06x-%06x",
\r
37 start_addr, end_addr);
\r
42 elprintf(EL_STATUS|EL_ANOMALY, "xmap_set: ptr is not aligned: %08lx", addr);
\r
49 for (i = start_addr >> shift; i <= end_addr >> shift; i++) {
\r
56 void z80_map_set(uptr *map, int start_addr, int end_addr,
\r
57 const void *func_or_mh, int is_func)
\r
59 xmap_set(map, Z80_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func);
\r
62 void cpu68k_map_set(uptr *map, int start_addr, int end_addr,
\r
63 const void *func_or_mh, int is_func)
\r
65 xmap_set(map, M68K_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func);
\r
68 // more specialized/optimized function (does same as above)
\r
69 void cpu68k_map_all_ram(int start_addr, int end_addr, void *ptr, int is_sub)
\r
71 uptr *r8map, *r16map, *w8map, *w16map;
\r
72 uptr addr = (uptr)ptr;
\r
73 int shift = M68K_MEM_SHIFT;
\r
77 r8map = m68k_read8_map;
\r
78 r16map = m68k_read16_map;
\r
79 w8map = m68k_write8_map;
\r
80 w16map = m68k_write16_map;
\r
82 r8map = s68k_read8_map;
\r
83 r16map = s68k_read16_map;
\r
84 w8map = s68k_write8_map;
\r
85 w16map = s68k_write16_map;
\r
90 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
91 r8map[i] = r16map[i] = w8map[i] = w16map[i] = addr;
\r
94 static u32 m68k_unmapped_read8(u32 a)
\r
96 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
\r
97 return 0; // assume pulldown, as if MegaCD2 was attached
\r
100 static u32 m68k_unmapped_read16(u32 a)
\r
102 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
\r
106 static void m68k_unmapped_write8(u32 a, u32 d)
\r
108 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
\r
111 static void m68k_unmapped_write16(u32 a, u32 d)
\r
113 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
\r
116 void m68k_map_unmap(int start_addr, int end_addr)
\r
119 // workaround bug (segfault) in
\r
120 // Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
\r
124 int shift = M68K_MEM_SHIFT;
\r
127 addr = (uptr)m68k_unmapped_read8;
\r
128 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
129 m68k_read8_map[i] = (addr >> 1) | MAP_FLAG;
\r
131 addr = (uptr)m68k_unmapped_read16;
\r
132 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
133 m68k_read16_map[i] = (addr >> 1) | MAP_FLAG;
\r
135 addr = (uptr)m68k_unmapped_write8;
\r
136 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
137 m68k_write8_map[i] = (addr >> 1) | MAP_FLAG;
\r
139 addr = (uptr)m68k_unmapped_write16;
\r
140 for (i = start_addr >> shift; i <= end_addr >> shift; i++)
\r
141 m68k_write16_map[i] = (addr >> 1) | MAP_FLAG;
\r
144 MAKE_68K_READ8(m68k_read8, m68k_read8_map)
\r
145 MAKE_68K_READ16(m68k_read16, m68k_read16_map)
\r
146 MAKE_68K_READ32(m68k_read32, m68k_read16_map)
\r
147 MAKE_68K_WRITE8(m68k_write8, m68k_write8_map)
\r
148 MAKE_68K_WRITE16(m68k_write16, m68k_write16_map)
\r
149 MAKE_68K_WRITE32(m68k_write32, m68k_write16_map)
\r
151 // -----------------------------------------------------------------
\r
153 static u32 ym2612_read_local_68k(void);
\r
154 static int ym2612_write_local(u32 a, u32 d, int is_from_z80);
\r
155 static void z80_mem_setup(void);
\r
157 #ifdef _ASM_MEMORY_C
\r
158 u32 PicoRead8_sram(u32 a);
\r
159 u32 PicoRead16_sram(u32 a);
\r
162 #ifdef EMU_CORE_DEBUG
\r
163 u32 lastread_a, lastread_d[16]={0,}, lastwrite_cyc_d[16]={0,}, lastwrite_mus_d[16]={0,};
\r
164 int lrp_cyc=0, lrp_mus=0, lwp_cyc=0, lwp_mus=0;
\r
165 extern unsigned int ppop;
\r
169 void log_io(unsigned int addr, int bits, int rw);
\r
170 #elif defined(_MSC_VER)
\r
173 #define log_io(...)
\r
176 #if defined(EMU_C68K)
\r
177 void cyclone_crashed(u32 pc, struct Cyclone *context)
\r
179 elprintf(EL_STATUS|EL_ANOMALY, "%c68k crash detected @ %06x",
\r
180 context == &PicoCpuCM68k ? 'm' : 's', pc);
\r
181 context->membase = (u32)Pico.rom;
\r
182 context->pc = (u32)Pico.rom + Pico.romsize;
\r
186 // -----------------------------------------------------------------
\r
189 static u32 read_pad_3btn(int i, u32 out_bits)
\r
191 u32 pad = ~PicoPadInt[i]; // Get inverse of pad MXYZ SACB RLDU
\r
194 if (out_bits & 0x40) // TH
\r
195 value = pad & 0x3f; // ?1CB RLDU
\r
197 value = ((pad & 0xc0) >> 2) | (pad & 3); // ?0SA 00DU
\r
199 value |= out_bits & 0x40;
\r
203 static u32 read_pad_6btn(int i, u32 out_bits)
\r
205 u32 pad = ~PicoPadInt[i]; // Get inverse of pad MXYZ SACB RLDU
\r
206 int phase = Pico.m.padTHPhase[i];
\r
209 if (phase == 2 && !(out_bits & 0x40)) {
\r
210 value = (pad & 0xc0) >> 2; // ?0SA 0000
\r
213 else if(phase == 3) {
\r
214 if (out_bits & 0x40)
\r
215 return (pad & 0x30) | ((pad >> 8) & 0xf); // ?1CB MXYZ
\r
217 return ((pad & 0xc0) >> 2) | 0x0f; // ?0SA 1111
\r
221 if (out_bits & 0x40) // TH
\r
222 value = pad & 0x3f; // ?1CB RLDU
\r
224 value = ((pad & 0xc0) >> 2) | (pad & 3); // ?0SA 00DU
\r
227 value |= out_bits & 0x40;
\r
231 static u32 read_nothing(int i, u32 out_bits)
\r
236 typedef u32 (port_read_func)(int index, u32 out_bits);
\r
238 static port_read_func *port_readers[3] = {
\r
244 static NOINLINE u32 port_read(int i)
\r
246 u32 data_reg = Pico.ioports[i + 1];
\r
247 u32 ctrl_reg = Pico.ioports[i + 4] | 0x80;
\r
250 out = data_reg & ctrl_reg;
\r
251 out |= 0x7f & ~ctrl_reg; // pull-ups
\r
253 in = port_readers[i](i, out);
\r
255 return (in & ~ctrl_reg) | (data_reg & ctrl_reg);
\r
258 void PicoSetInputDevice(int port, enum input_device device)
\r
260 port_read_func *func;
\r
262 if (port < 0 || port > 2)
\r
266 case PICO_INPUT_PAD_3BTN:
\r
267 func = read_pad_3btn;
\r
270 case PICO_INPUT_PAD_6BTN:
\r
271 func = read_pad_6btn;
\r
275 func = read_nothing;
\r
279 port_readers[port] = func;
\r
282 NOINLINE u32 io_ports_read(u32 a)
\r
287 case 0: d = Pico.m.hardware; break; // Hardware value (Version register)
\r
288 case 1: d = port_read(0); break;
\r
289 case 2: d = port_read(1); break;
\r
290 case 3: d = port_read(2); break;
\r
291 default: d = Pico.ioports[a]; break; // IO ports can be used as RAM
\r
296 NOINLINE void io_ports_write(u32 a, u32 d)
\r
300 // 6 button gamepad: if TH went from 0 to 1, gamepad changes state
\r
301 if (1 <= a && a <= 2)
\r
303 Pico.m.padDelay[a - 1] = 0;
\r
304 if (!(Pico.ioports[a] & 0x40) && (d & 0x40))
\r
305 Pico.m.padTHPhase[a - 1]++;
\r
308 // certain IO ports can be used as RAM
\r
309 Pico.ioports[a] = d;
\r
313 static int z80_cycles_from_68k(void)
\r
315 return z80_cycle_aim
\r
316 + cycles_68k_to_z80(SekCyclesDone() - last_z80_sync);
\r
319 void NOINLINE ctl_write_z80busreq(u32 d)
\r
322 elprintf(EL_BUSREQ, "set_zrun: %i->%i [%i] @%06x", Pico.m.z80Run, d, SekCyclesDone(), SekPc);
\r
323 if (d ^ Pico.m.z80Run)
\r
327 z80_cycle_cnt = z80_cycles_from_68k();
\r
331 if ((PicoOpt&POPT_EN_Z80) && !Pico.m.z80_reset) {
\r
333 PicoSyncZ80(SekCyclesDone());
\r
334 pprof_end_sub(m68k);
\r
341 void NOINLINE ctl_write_z80reset(u32 d)
\r
344 elprintf(EL_BUSREQ, "set_zreset: %i->%i [%i] @%06x", Pico.m.z80_reset, d, SekCyclesDone(), SekPc);
\r
345 if (d ^ Pico.m.z80_reset)
\r
349 if ((PicoOpt&POPT_EN_Z80) && Pico.m.z80Run) {
\r
351 PicoSyncZ80(SekCyclesDone());
\r
352 pprof_end_sub(m68k);
\r
359 z80_cycle_cnt = z80_cycles_from_68k();
\r
362 Pico.m.z80_reset = d;
\r
366 // -----------------------------------------------------------------
\r
368 #ifndef _ASM_MEMORY_C
\r
370 // cart (save) RAM area (usually 0x200000 - ...)
\r
371 static u32 PicoRead8_sram(u32 a)
\r
374 if (SRam.start <= a && a <= SRam.end && (Pico.m.sram_reg & SRR_MAPPED))
\r
376 if (SRam.flags & SRF_EEPROM) {
\r
381 d = *(u8 *)(SRam.data - SRam.start + a);
\r
382 elprintf(EL_SRAMIO, "sram r8 [%06x] %02x @ %06x", a, d, SekPc);
\r
386 // XXX: this is banking unfriendly
\r
387 if (a < Pico.romsize)
\r
388 return Pico.rom[a ^ 1];
\r
390 return m68k_unmapped_read8(a);
\r
393 static u32 PicoRead16_sram(u32 a)
\r
396 if (SRam.start <= a && a <= SRam.end && (Pico.m.sram_reg & SRR_MAPPED))
\r
398 if (SRam.flags & SRF_EEPROM)
\r
401 u8 *pm = (u8 *)(SRam.data - SRam.start + a);
\r
405 elprintf(EL_SRAMIO, "sram r16 [%06x] %04x @ %06x", a, d, SekPc);
\r
409 if (a < Pico.romsize)
\r
410 return *(u16 *)(Pico.rom + a);
\r
412 return m68k_unmapped_read16(a);
\r
415 #endif // _ASM_MEMORY_C
\r
417 static void PicoWrite8_sram(u32 a, u32 d)
\r
419 if (a > SRam.end || a < SRam.start || !(Pico.m.sram_reg & SRR_MAPPED)) {
\r
420 m68k_unmapped_write8(a, d);
\r
424 elprintf(EL_SRAMIO, "sram w8 [%06x] %02x @ %06x", a, d & 0xff, SekPc);
\r
425 if (SRam.flags & SRF_EEPROM)
\r
427 EEPROM_write8(a, d);
\r
430 u8 *pm = (u8 *)(SRam.data - SRam.start + a);
\r
431 if (*pm != (u8)d) {
\r
438 static void PicoWrite16_sram(u32 a, u32 d)
\r
440 if (a > SRam.end || a < SRam.start || !(Pico.m.sram_reg & SRR_MAPPED)) {
\r
441 m68k_unmapped_write16(a, d);
\r
445 elprintf(EL_SRAMIO, "sram w16 [%06x] %04x @ %06x", a, d & 0xffff, SekPc);
\r
446 if (SRam.flags & SRF_EEPROM)
\r
451 // XXX: hardware could easily use MSB too..
\r
452 u8 *pm = (u8 *)(SRam.data - SRam.start + a);
\r
453 if (*pm != (u8)d) {
\r
460 // z80 area (0xa00000 - 0xa0ffff)
\r
461 // TODO: verify mirrors VDP and bank reg (bank area mirroring verified)
\r
462 static u32 PicoRead8_z80(u32 a)
\r
465 if ((Pico.m.z80Run & 1) || Pico.m.z80_reset) {
\r
466 elprintf(EL_ANOMALY, "68k z80 read with no bus! [%06x] @ %06x", a, SekPc);
\r
467 // open bus. Pulled down if MegaCD2 is attached.
\r
471 if ((a & 0x4000) == 0x0000)
\r
472 d = Pico.zram[a & 0x1fff];
\r
473 else if ((a & 0x6000) == 0x4000) // 0x4000-0x5fff
\r
474 d = ym2612_read_local_68k();
\r
476 elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc);
\r
480 static u32 PicoRead16_z80(u32 a)
\r
482 u32 d = PicoRead8_z80(a);
\r
483 return d | (d << 8);
\r
486 static void PicoWrite8_z80(u32 a, u32 d)
\r
488 if ((Pico.m.z80Run & 1) || Pico.m.z80_reset) {
\r
489 // verified on real hw
\r
490 elprintf(EL_ANOMALY, "68k z80 write with no bus or reset! [%06x] %02x @ %06x", a, d&0xff, SekPc);
\r
494 if ((a & 0x4000) == 0x0000) { // z80 RAM
\r
495 SekCyclesBurnRun(2); // FIXME hack
\r
496 Pico.zram[a & 0x1fff] = (u8)d;
\r
499 if ((a & 0x6000) == 0x4000) { // FM Sound
\r
500 if (PicoOpt & POPT_EN_FM)
\r
501 emustatus |= ym2612_write_local(a&3, d&0xff, 0)&1;
\r
504 // TODO: probably other VDP access too? Maybe more mirrors?
\r
505 if ((a & 0x7ff9) == 0x7f11) { // PSG Sound
\r
506 if (PicoOpt & POPT_EN_PSG)
\r
510 if ((a & 0x7f00) == 0x6000) // Z80 BANK register
\r
512 Pico.m.z80_bank68k >>= 1;
\r
513 Pico.m.z80_bank68k |= d << 8;
\r
514 Pico.m.z80_bank68k &= 0x1ff; // 9 bits and filled in the new top one
\r
515 elprintf(EL_Z80BNK, "z80 bank=%06x", Pico.m.z80_bank68k << 15);
\r
518 elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %02x @ %06x", a, d&0xff, SekPc);
\r
521 static void PicoWrite16_z80(u32 a, u32 d)
\r
523 // for RAM, only most significant byte is sent
\r
524 // TODO: verify remaining accesses
\r
525 PicoWrite8_z80(a, d >> 8);
\r
528 #ifndef _ASM_MEMORY_C
\r
530 // IO/control area (0xa10000 - 0xa1ffff)
\r
531 u32 PicoRead8_io(u32 a)
\r
535 if ((a & 0xffe0) == 0x0000) { // I/O ports
\r
536 d = io_ports_read(a);
\r
540 // faking open bus (MegaCD pulldowns don't work here curiously)
\r
541 d = Pico.m.rotate++;
\r
544 if ((a & 0xfc00) == 0x1000) {
\r
545 // bit8 seems to be readable in this range
\r
549 if ((a & 0xff01) == 0x1100) { // z80 busreq (verified)
\r
550 d |= (Pico.m.z80Run | Pico.m.z80_reset) & 1;
\r
551 elprintf(EL_BUSREQ, "get_zrun: %02x [%i] @%06x", d, SekCyclesDone(), SekPc);
\r
556 if (PicoOpt & POPT_EN_32X) {
\r
557 d = PicoRead8_32x(a);
\r
561 d = m68k_unmapped_read8(a);
\r
566 u32 PicoRead16_io(u32 a)
\r
570 if ((a & 0xffe0) == 0x0000) { // I/O ports
\r
571 d = io_ports_read(a);
\r
577 d = (Pico.m.rotate += 0x41);
\r
578 d ^= (d << 5) ^ (d << 8);
\r
580 // bit8 seems to be readable in this range
\r
581 if ((a & 0xfc00) == 0x1000) {
\r
584 if ((a & 0xff00) == 0x1100) { // z80 busreq
\r
585 d |= ((Pico.m.z80Run | Pico.m.z80_reset) & 1) << 8;
\r
586 elprintf(EL_BUSREQ, "get_zrun: %04x [%i] @%06x", d, SekCyclesDone(), SekPc);
\r
591 if (PicoOpt & POPT_EN_32X) {
\r
592 d = PicoRead16_32x(a);
\r
596 d = m68k_unmapped_read16(a);
\r
601 void PicoWrite8_io(u32 a, u32 d)
\r
603 if ((a & 0xffe1) == 0x0001) { // I/O ports (verified: only LSB!)
\r
604 io_ports_write(a, d);
\r
607 if ((a & 0xff01) == 0x1100) { // z80 busreq
\r
608 ctl_write_z80busreq(d);
\r
611 if ((a & 0xff01) == 0x1200) { // z80 reset
\r
612 ctl_write_z80reset(d);
\r
615 if (a == 0xa130f1) { // sram access register
\r
616 elprintf(EL_SRAMIO, "sram reg=%02x", d);
\r
617 Pico.m.sram_reg &= ~(SRR_MAPPED|SRR_READONLY);
\r
618 Pico.m.sram_reg |= (u8)(d & 3);
\r
621 if (PicoOpt & POPT_EN_32X) {
\r
622 PicoWrite8_32x(a, d);
\r
626 m68k_unmapped_write8(a, d);
\r
629 void PicoWrite16_io(u32 a, u32 d)
\r
631 if ((a & 0xffe0) == 0x0000) { // I/O ports (verified: only LSB!)
\r
632 io_ports_write(a, d);
\r
635 if ((a & 0xff00) == 0x1100) { // z80 busreq
\r
636 ctl_write_z80busreq(d >> 8);
\r
639 if ((a & 0xff00) == 0x1200) { // z80 reset
\r
640 ctl_write_z80reset(d >> 8);
\r
643 if (a == 0xa130f0) { // sram access register
\r
644 elprintf(EL_SRAMIO, "sram reg=%02x", d);
\r
645 Pico.m.sram_reg &= ~(SRR_MAPPED|SRR_READONLY);
\r
646 Pico.m.sram_reg |= (u8)(d & 3);
\r
649 if (PicoOpt & POPT_EN_32X) {
\r
650 PicoWrite16_32x(a, d);
\r
653 m68k_unmapped_write16(a, d);
\r
656 #endif // _ASM_MEMORY_C
\r
658 // VDP area (0xc00000 - 0xdfffff)
\r
659 // TODO: verify if lower byte goes to PSG on word writes
\r
660 static u32 PicoRead8_vdp(u32 a)
\r
662 if ((a & 0x00e0) == 0x0000)
\r
663 return PicoVideoRead8(a);
\r
665 elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc);
\r
669 static u32 PicoRead16_vdp(u32 a)
\r
671 if ((a & 0x00e0) == 0x0000)
\r
672 return PicoVideoRead(a);
\r
674 elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc);
\r
678 static void PicoWrite8_vdp(u32 a, u32 d)
\r
680 if ((a & 0x00f9) == 0x0011) { // PSG Sound
\r
681 if (PicoOpt & POPT_EN_PSG)
\r
685 if ((a & 0x00e0) == 0x0000) {
\r
687 PicoVideoWrite(a, d | (d << 8));
\r
691 elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %02x @%06x", a, d & 0xff, SekPc);
\r
694 static void PicoWrite16_vdp(u32 a, u32 d)
\r
696 if ((a & 0x00f9) == 0x0010) { // PSG Sound
\r
697 if (PicoOpt & POPT_EN_PSG)
\r
701 if ((a & 0x00e0) == 0x0000) {
\r
702 PicoVideoWrite(a, d);
\r
706 elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %04x @%06x", a, d & 0xffff, SekPc);
\r
709 // -----------------------------------------------------------------
\r
712 static void m68k_mem_setup(void);
\r
715 PICO_INTERNAL void PicoMemSetup(void)
\r
719 // setup the memory map
\r
720 cpu68k_map_set(m68k_read8_map, 0x000000, 0xffffff, m68k_unmapped_read8, 1);
\r
721 cpu68k_map_set(m68k_read16_map, 0x000000, 0xffffff, m68k_unmapped_read16, 1);
\r
722 cpu68k_map_set(m68k_write8_map, 0x000000, 0xffffff, m68k_unmapped_write8, 1);
\r
723 cpu68k_map_set(m68k_write16_map, 0x000000, 0xffffff, m68k_unmapped_write16, 1);
\r
726 // align to bank size. We know ROM loader allocated enough for this
\r
727 mask = (1 << M68K_MEM_SHIFT) - 1;
\r
728 rs = (Pico.romsize + mask) & ~mask;
\r
729 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico.rom, 0);
\r
730 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico.rom, 0);
\r
732 // Common case of on-cart (save) RAM, usually at 0x200000-...
\r
733 if ((SRam.flags & SRF_ENABLED) && SRam.data != NULL) {
\r
734 rs = SRam.end - SRam.start;
\r
735 rs = (rs + mask) & ~mask;
\r
736 if (SRam.start + rs >= 0x1000000)
\r
737 rs = 0x1000000 - SRam.start;
\r
738 cpu68k_map_set(m68k_read8_map, SRam.start, SRam.start + rs - 1, PicoRead8_sram, 1);
\r
739 cpu68k_map_set(m68k_read16_map, SRam.start, SRam.start + rs - 1, PicoRead16_sram, 1);
\r
740 cpu68k_map_set(m68k_write8_map, SRam.start, SRam.start + rs - 1, PicoWrite8_sram, 1);
\r
741 cpu68k_map_set(m68k_write16_map, SRam.start, SRam.start + rs - 1, PicoWrite16_sram, 1);
\r
745 cpu68k_map_set(m68k_read8_map, 0xa00000, 0xa0ffff, PicoRead8_z80, 1);
\r
746 cpu68k_map_set(m68k_read16_map, 0xa00000, 0xa0ffff, PicoRead16_z80, 1);
\r
747 cpu68k_map_set(m68k_write8_map, 0xa00000, 0xa0ffff, PicoWrite8_z80, 1);
\r
748 cpu68k_map_set(m68k_write16_map, 0xa00000, 0xa0ffff, PicoWrite16_z80, 1);
\r
750 // IO/control region
\r
751 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_io, 1);
\r
752 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_io, 1);
\r
753 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_io, 1);
\r
754 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_io, 1);
\r
757 for (a = 0xc00000; a < 0xe00000; a += 0x010000) {
\r
758 if ((a & 0xe700e0) != 0xc00000)
\r
760 cpu68k_map_set(m68k_read8_map, a, a + 0xffff, PicoRead8_vdp, 1);
\r
761 cpu68k_map_set(m68k_read16_map, a, a + 0xffff, PicoRead16_vdp, 1);
\r
762 cpu68k_map_set(m68k_write8_map, a, a + 0xffff, PicoWrite8_vdp, 1);
\r
763 cpu68k_map_set(m68k_write16_map, a, a + 0xffff, PicoWrite16_vdp, 1);
\r
766 // RAM and it's mirrors
\r
767 for (a = 0xe00000; a < 0x1000000; a += 0x010000) {
\r
768 cpu68k_map_set(m68k_read8_map, a, a + 0xffff, Pico.ram, 0);
\r
769 cpu68k_map_set(m68k_read16_map, a, a + 0xffff, Pico.ram, 0);
\r
770 cpu68k_map_set(m68k_write8_map, a, a + 0xffff, Pico.ram, 0);
\r
771 cpu68k_map_set(m68k_write16_map, a, a + 0xffff, Pico.ram, 0);
\r
774 // Setup memory callbacks:
\r
776 PicoCpuCM68k.read8 = (void *)m68k_read8_map;
\r
777 PicoCpuCM68k.read16 = (void *)m68k_read16_map;
\r
778 PicoCpuCM68k.read32 = (void *)m68k_read16_map;
\r
779 PicoCpuCM68k.write8 = (void *)m68k_write8_map;
\r
780 PicoCpuCM68k.write16 = (void *)m68k_write16_map;
\r
781 PicoCpuCM68k.write32 = (void *)m68k_write16_map;
\r
782 PicoCpuCM68k.checkpc = NULL; /* unused */
\r
783 PicoCpuCM68k.fetch8 = NULL;
\r
784 PicoCpuCM68k.fetch16 = NULL;
\r
785 PicoCpuCM68k.fetch32 = NULL;
\r
788 PicoCpuFM68k.read_byte = m68k_read8;
\r
789 PicoCpuFM68k.read_word = m68k_read16;
\r
790 PicoCpuFM68k.read_long = m68k_read32;
\r
791 PicoCpuFM68k.write_byte = m68k_write8;
\r
792 PicoCpuFM68k.write_word = m68k_write16;
\r
793 PicoCpuFM68k.write_long = m68k_write32;
\r
795 // setup FAME fetchmap
\r
798 // by default, point everything to first 64k of ROM
\r
799 for (i = 0; i < M68K_FETCHBANK1; i++)
\r
800 PicoCpuFM68k.Fetch[i] = (unsigned long)Pico.rom - (i<<(24-FAMEC_FETCHBITS));
\r
802 for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < Pico.romsize; i++)
\r
803 PicoCpuFM68k.Fetch[i] = (unsigned long)Pico.rom;
\r
805 for (i = M68K_FETCHBANK1*14/16; i < M68K_FETCHBANK1; i++)
\r
806 PicoCpuFM68k.Fetch[i] = (unsigned long)Pico.ram - (i<<(24-FAMEC_FETCHBITS));
\r
817 unsigned int (*pm68k_read_memory_8) (unsigned int address) = NULL;
\r
818 unsigned int (*pm68k_read_memory_16)(unsigned int address) = NULL;
\r
819 unsigned int (*pm68k_read_memory_32)(unsigned int address) = NULL;
\r
820 void (*pm68k_write_memory_8) (unsigned int address, unsigned char value) = NULL;
\r
821 void (*pm68k_write_memory_16)(unsigned int address, unsigned short value) = NULL;
\r
822 void (*pm68k_write_memory_32)(unsigned int address, unsigned int value) = NULL;
\r
824 /* it appears that Musashi doesn't always mask the unused bits */
\r
825 unsigned int m68k_read_memory_8 (unsigned int address) { return pm68k_read_memory_8 (address) & 0xff; }
\r
826 unsigned int m68k_read_memory_16(unsigned int address) { return pm68k_read_memory_16(address) & 0xffff; }
\r
827 unsigned int m68k_read_memory_32(unsigned int address) { return pm68k_read_memory_32(address); }
\r
828 void m68k_write_memory_8 (unsigned int address, unsigned int value) { pm68k_write_memory_8 (address, (u8)value); }
\r
829 void m68k_write_memory_16(unsigned int address, unsigned int value) { pm68k_write_memory_16(address,(u16)value); }
\r
830 void m68k_write_memory_32(unsigned int address, unsigned int value) { pm68k_write_memory_32(address, value); }
\r
832 static void m68k_mem_setup(void)
\r
834 pm68k_read_memory_8 = m68k_read8;
\r
835 pm68k_read_memory_16 = m68k_read16;
\r
836 pm68k_read_memory_32 = m68k_read32;
\r
837 pm68k_write_memory_8 = m68k_write8;
\r
838 pm68k_write_memory_16 = m68k_write16;
\r
839 pm68k_write_memory_32 = m68k_write32;
\r
844 // -----------------------------------------------------------------
\r
846 static int get_scanline(int is_from_z80)
\r
849 int cycles = z80_cyclesDone();
\r
850 while (cycles - z80_scanline_cycles >= 228)
\r
851 z80_scanline++, z80_scanline_cycles += 228;
\r
852 return z80_scanline;
\r
855 return Pico.m.scanline;
\r
858 /* probably should not be in this file, but it's near related code here */
\r
859 void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new)
\r
861 int xcycles = z80_cycles << 8;
\r
863 /* check for overflows */
\r
864 if ((mode_old & 4) && xcycles > timer_a_next_oflow)
\r
865 ym2612.OPN.ST.status |= 1;
\r
867 if ((mode_old & 8) && xcycles > timer_b_next_oflow)
\r
868 ym2612.OPN.ST.status |= 2;
\r
870 /* update timer a */
\r
872 while (xcycles > timer_a_next_oflow)
\r
873 timer_a_next_oflow += timer_a_step;
\r
875 if ((mode_old ^ mode_new) & 1) // turning on/off
\r
878 timer_a_next_oflow = TIMER_NO_OFLOW;
\r
880 timer_a_next_oflow = xcycles + timer_a_step;
\r
883 elprintf(EL_YMTIMER, "timer a upd to %i @ %i", timer_a_next_oflow>>8, z80_cycles);
\r
885 /* update timer b */
\r
887 while (xcycles > timer_b_next_oflow)
\r
888 timer_b_next_oflow += timer_b_step;
\r
890 if ((mode_old ^ mode_new) & 2)
\r
893 timer_b_next_oflow = TIMER_NO_OFLOW;
\r
895 timer_b_next_oflow = xcycles + timer_b_step;
\r
898 elprintf(EL_YMTIMER, "timer b upd to %i @ %i", timer_b_next_oflow>>8, z80_cycles);
\r
901 // ym2612 DAC and timer I/O handlers for z80
\r
902 static int ym2612_write_local(u32 a, u32 d, int is_from_z80)
\r
907 if (a == 1 && ym2612.OPN.ST.address == 0x2a) /* DAC data */
\r
909 int scanline = get_scanline(is_from_z80);
\r
910 //elprintf(EL_STATUS, "%03i -> %03i dac w %08x z80 %i", PsndDacLine, scanline, d, is_from_z80);
\r
911 ym2612.dacout = ((int)d - 0x80) << 6;
\r
912 if (PsndOut && ym2612.dacen && scanline >= PsndDacLine)
\r
913 PsndDoDAC(scanline);
\r
919 case 0: /* address port 0 */
\r
920 ym2612.OPN.ST.address = d;
\r
921 ym2612.addr_A1 = 0;
\r
923 if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, -1);
\r
927 case 1: /* data port 0 */
\r
928 if (ym2612.addr_A1 != 0)
\r
931 addr = ym2612.OPN.ST.address;
\r
932 ym2612.REGS[addr] = d;
\r
936 case 0x24: // timer A High 8
\r
937 case 0x25: { // timer A Low 2
\r
938 int TAnew = (addr == 0x24) ? ((ym2612.OPN.ST.TA & 0x03)|(((int)d)<<2))
\r
939 : ((ym2612.OPN.ST.TA & 0x3fc)|(d&3));
\r
940 if (ym2612.OPN.ST.TA != TAnew)
\r
942 //elprintf(EL_STATUS, "timer a set %i", TAnew);
\r
943 ym2612.OPN.ST.TA = TAnew;
\r
944 //ym2612.OPN.ST.TAC = (1024-TAnew)*18;
\r
945 //ym2612.OPN.ST.TAT = 0;
\r
946 timer_a_step = TIMER_A_TICK_ZCYCLES * (1024 - TAnew);
\r
947 if (ym2612.OPN.ST.mode & 1) {
\r
948 // this is not right, should really be done on overflow only
\r
949 int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k();
\r
950 timer_a_next_oflow = (cycles << 8) + timer_a_step;
\r
952 elprintf(EL_YMTIMER, "timer a set to %i, %i", 1024 - TAnew, timer_a_next_oflow>>8);
\r
956 case 0x26: // timer B
\r
957 if (ym2612.OPN.ST.TB != d) {
\r
958 //elprintf(EL_STATUS, "timer b set %i", d);
\r
959 ym2612.OPN.ST.TB = d;
\r
960 //ym2612.OPN.ST.TBC = (256-d) * 288;
\r
961 //ym2612.OPN.ST.TBT = 0;
\r
962 timer_b_step = TIMER_B_TICK_ZCYCLES * (256 - d); // 262800
\r
963 if (ym2612.OPN.ST.mode & 2) {
\r
964 int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k();
\r
965 timer_b_next_oflow = (cycles << 8) + timer_b_step;
\r
967 elprintf(EL_YMTIMER, "timer b set to %i, %i", 256 - d, timer_b_next_oflow>>8);
\r
970 case 0x27: { /* mode, timer control */
\r
971 int old_mode = ym2612.OPN.ST.mode;
\r
972 int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k();
\r
973 ym2612.OPN.ST.mode = d;
\r
975 elprintf(EL_YMTIMER, "st mode %02x", d);
\r
976 ym2612_sync_timers(cycles, old_mode, d);
\r
978 /* reset Timer a flag */
\r
980 ym2612.OPN.ST.status &= ~1;
\r
982 /* reset Timer b flag */
\r
984 ym2612.OPN.ST.status &= ~2;
\r
986 if ((d ^ old_mode) & 0xc0) {
\r
988 if (PicoOpt & POPT_EXT_FM) return YM2612Write_940(a, d, get_scanline(is_from_z80));
\r
994 case 0x2b: { /* DAC Sel (YM2612) */
\r
995 int scanline = get_scanline(is_from_z80);
\r
996 ym2612.dacen = d & 0x80;
\r
997 if (d & 0x80) PsndDacLine = scanline;
\r
999 if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, scanline);
\r
1006 case 2: /* address port 1 */
\r
1007 ym2612.OPN.ST.address = d;
\r
1008 ym2612.addr_A1 = 1;
\r
1010 if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, -1);
\r
1014 case 3: /* data port 1 */
\r
1015 if (ym2612.addr_A1 != 1)
\r
1018 addr = ym2612.OPN.ST.address | 0x100;
\r
1019 ym2612.REGS[addr] = d;
\r
1024 if (PicoOpt & POPT_EXT_FM)
\r
1025 return YM2612Write_940(a, d, get_scanline(is_from_z80));
\r
1027 return YM2612Write_(a, d);
\r
1031 #define ym2612_read_local() \
\r
1032 if (xcycles >= timer_a_next_oflow) \
\r
1033 ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 1; \
\r
1034 if (xcycles >= timer_b_next_oflow) \
\r
1035 ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 2
\r
1037 static u32 ym2612_read_local_z80(void)
\r
1039 int xcycles = z80_cyclesDone() << 8;
\r
1041 ym2612_read_local();
\r
1043 elprintf(EL_YMTIMER, "timer z80 read %i, sched %i, %i @ %i|%i", ym2612.OPN.ST.status,
\r
1044 timer_a_next_oflow>>8, timer_b_next_oflow>>8, xcycles >> 8, (xcycles >> 8) / 228);
\r
1045 return ym2612.OPN.ST.status;
\r
1048 static u32 ym2612_read_local_68k(void)
\r
1050 int xcycles = z80_cycles_from_68k() << 8;
\r
1052 ym2612_read_local();
\r
1054 elprintf(EL_YMTIMER, "timer 68k read %i, sched %i, %i @ %i|%i", ym2612.OPN.ST.status,
\r
1055 timer_a_next_oflow>>8, timer_b_next_oflow>>8, xcycles >> 8, (xcycles >> 8) / 228);
\r
1056 return ym2612.OPN.ST.status;
\r
1059 void ym2612_pack_state(void)
\r
1061 // timers are saved as tick counts, in 16.16 int format
\r
1062 int tac, tat = 0, tbc, tbt = 0;
\r
1063 tac = 1024 - ym2612.OPN.ST.TA;
\r
1064 tbc = 256 - ym2612.OPN.ST.TB;
\r
1065 if (timer_a_next_oflow != TIMER_NO_OFLOW)
\r
1066 tat = (int)((double)(timer_a_step - timer_a_next_oflow) / (double)timer_a_step * tac * 65536);
\r
1067 if (timer_b_next_oflow != TIMER_NO_OFLOW)
\r
1068 tbt = (int)((double)(timer_b_step - timer_b_next_oflow) / (double)timer_b_step * tbc * 65536);
\r
1069 elprintf(EL_YMTIMER, "save: timer a %i/%i", tat >> 16, tac);
\r
1070 elprintf(EL_YMTIMER, "save: timer b %i/%i", tbt >> 16, tbc);
\r
1073 if (PicoOpt & POPT_EXT_FM)
\r
1074 YM2612PicoStateSave2_940(tat, tbt);
\r
1077 YM2612PicoStateSave2(tat, tbt);
\r
1080 void ym2612_unpack_state(void)
\r
1082 int i, ret, tac, tat, tbc, tbt;
\r
1083 YM2612PicoStateLoad();
\r
1085 // feed all the registers and update internal state
\r
1086 for (i = 0x20; i < 0xA0; i++) {
\r
1087 ym2612_write_local(0, i, 0);
\r
1088 ym2612_write_local(1, ym2612.REGS[i], 0);
\r
1090 for (i = 0x30; i < 0xA0; i++) {
\r
1091 ym2612_write_local(2, i, 0);
\r
1092 ym2612_write_local(3, ym2612.REGS[i|0x100], 0);
\r
1094 for (i = 0xAF; i >= 0xA0; i--) { // must apply backwards
\r
1095 ym2612_write_local(2, i, 0);
\r
1096 ym2612_write_local(3, ym2612.REGS[i|0x100], 0);
\r
1097 ym2612_write_local(0, i, 0);
\r
1098 ym2612_write_local(1, ym2612.REGS[i], 0);
\r
1100 for (i = 0xB0; i < 0xB8; i++) {
\r
1101 ym2612_write_local(0, i, 0);
\r
1102 ym2612_write_local(1, ym2612.REGS[i], 0);
\r
1103 ym2612_write_local(2, i, 0);
\r
1104 ym2612_write_local(3, ym2612.REGS[i|0x100], 0);
\r
1108 if (PicoOpt & POPT_EXT_FM)
\r
1109 ret = YM2612PicoStateLoad2_940(&tat, &tbt);
\r
1112 ret = YM2612PicoStateLoad2(&tat, &tbt);
\r
1114 elprintf(EL_STATUS, "old ym2612 state");
\r
1115 return; // no saved timers
\r
1118 tac = (1024 - ym2612.OPN.ST.TA) << 16;
\r
1119 tbc = (256 - ym2612.OPN.ST.TB) << 16;
\r
1120 if (ym2612.OPN.ST.mode & 1)
\r
1121 timer_a_next_oflow = (int)((double)(tac - tat) / (double)tac * timer_a_step);
\r
1123 timer_a_next_oflow = TIMER_NO_OFLOW;
\r
1124 if (ym2612.OPN.ST.mode & 2)
\r
1125 timer_b_next_oflow = (int)((double)(tbc - tbt) / (double)tbc * timer_b_step);
\r
1127 timer_b_next_oflow = TIMER_NO_OFLOW;
\r
1128 elprintf(EL_YMTIMER, "load: %i/%i, timer_a_next_oflow %i", tat>>16, tac>>16, timer_a_next_oflow >> 8);
\r
1129 elprintf(EL_YMTIMER, "load: %i/%i, timer_b_next_oflow %i", tbt>>16, tbc>>16, timer_b_next_oflow >> 8);
\r
1132 #if defined(NO_32X) && defined(_ASM_MEMORY_C)
\r
1133 // referenced by asm code
\r
1134 u32 PicoRead8_32x(u32 a) { return 0; }
\r
1135 u32 PicoRead16_32x(u32 a) { return 0; }
\r
1136 void PicoWrite8_32x(u32 a, u32 d) {}
\r
1137 void PicoWrite16_32x(u32 a, u32 d) {}
\r
1140 // -----------------------------------------------------------------
\r
1141 // z80 memhandlers
\r
1143 static unsigned char z80_md_vdp_read(unsigned short a)
\r
1146 elprintf(EL_ANOMALY, "z80 invalid r8 [%06x] %02x", a, 0xff);
\r
1150 static unsigned char z80_md_bank_read(unsigned short a)
\r
1152 unsigned int addr68k;
\r
1153 unsigned char ret;
\r
1155 addr68k = Pico.m.z80_bank68k<<15;
\r
1156 addr68k += a & 0x7fff;
\r
1158 ret = m68k_read8(addr68k);
\r
1160 elprintf(EL_Z80BNK, "z80->68k r8 [%06x] %02x", addr68k, ret);
\r
1164 static void z80_md_ym2612_write(unsigned int a, unsigned char data)
\r
1166 if (PicoOpt & POPT_EN_FM)
\r
1167 emustatus |= ym2612_write_local(a, data, 1) & 1;
\r
1170 static void z80_md_vdp_br_write(unsigned int a, unsigned char data)
\r
1172 // TODO: allow full VDP access
\r
1173 if ((a&0xfff9) == 0x7f11) // 7f11 7f13 7f15 7f17
\r
1175 if (PicoOpt & POPT_EN_PSG)
\r
1176 SN76496Write(data);
\r
1180 if ((a>>8) == 0x60)
\r
1182 Pico.m.z80_bank68k >>= 1;
\r
1183 Pico.m.z80_bank68k |= data << 8;
\r
1184 Pico.m.z80_bank68k &= 0x1ff; // 9 bits and filled in the new top one
\r
1188 elprintf(EL_ANOMALY, "z80 invalid w8 [%06x] %02x", a, data);
\r
1191 static void z80_md_bank_write(unsigned int a, unsigned char data)
\r
1193 unsigned int addr68k;
\r
1195 addr68k = Pico.m.z80_bank68k << 15;
\r
1196 addr68k += a & 0x7fff;
\r
1198 elprintf(EL_Z80BNK, "z80->68k w8 [%06x] %02x", addr68k, data);
\r
1199 m68k_write8(addr68k, data);
\r
1202 // -----------------------------------------------------------------
\r
1204 static unsigned char z80_md_in(unsigned short p)
\r
1206 elprintf(EL_ANOMALY, "Z80 port %04x read", p);
\r
1210 static void z80_md_out(unsigned short p, unsigned char d)
\r
1212 elprintf(EL_ANOMALY, "Z80 port %04x write %02x", p, d);
\r
1215 static void z80_mem_setup(void)
\r
1217 z80_map_set(z80_read_map, 0x0000, 0x1fff, Pico.zram, 0);
\r
1218 z80_map_set(z80_read_map, 0x2000, 0x3fff, Pico.zram, 0);
\r
1219 z80_map_set(z80_read_map, 0x4000, 0x5fff, ym2612_read_local_z80, 1);
\r
1220 z80_map_set(z80_read_map, 0x6000, 0x7fff, z80_md_vdp_read, 1);
\r
1221 z80_map_set(z80_read_map, 0x8000, 0xffff, z80_md_bank_read, 1);
\r
1223 z80_map_set(z80_write_map, 0x0000, 0x1fff, Pico.zram, 0);
\r
1224 z80_map_set(z80_write_map, 0x2000, 0x3fff, Pico.zram, 0);
\r
1225 z80_map_set(z80_write_map, 0x4000, 0x5fff, z80_md_ym2612_write, 1);
\r
1226 z80_map_set(z80_write_map, 0x6000, 0x7fff, z80_md_vdp_br_write, 1);
\r
1227 z80_map_set(z80_write_map, 0x8000, 0xffff, z80_md_bank_write, 1);
\r
1230 drZ80.z80_in = z80_md_in;
\r
1231 drZ80.z80_out = z80_md_out;
\r
1234 Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (FPTR)Pico.zram); // main RAM
\r
1235 Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (FPTR)Pico.zram); // mirror
\r
1236 Cz80_Set_INPort(&CZ80, z80_md_in);
\r
1237 Cz80_Set_OUTPort(&CZ80, z80_md_out);
\r
1241 // vim:shiftwidth=2:ts=2:expandtab
\r