X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pico%2Fmemory.c;h=a31a08e993ba6b8b6fb5c00c69edaf4cefda9107;hb=ee3c39efd2cf51cd654b6240f6fb595673f10f45;hp=2eaac2affd97195cb571c44437f4f53681da2aa4;hpb=45f2f245f51ef0c0d37df3c998595c132bfcaffa;p=picodrive.git diff --git a/pico/memory.c b/pico/memory.c index 2eaac2a..a31a08e 100644 --- a/pico/memory.c +++ b/pico/memory.c @@ -1,11 +1,11 @@ -// This is part of Pico Library - -// (c) Copyright 2004 Dave, All rights reserved. -// (c) Copyright 2006-2009 notaz, All rights reserved. -// Free for non-commercial use. - -// For commercial use, separate licencing terms must be obtained. - +/* + * memory handling + * (c) Copyright Dave, 2004 + * (C) notaz, 2006-2010 + * + * This work is licensed under the terms of MAME license. + * See COPYING file in the top-level directory. + */ #include "pico_int.h" #include "memory.h" @@ -15,15 +15,20 @@ extern unsigned int lastSSRamWrite; // used by serial eeprom code -unsigned long m68k_read8_map [0x1000000 >> M68K_MEM_SHIFT]; -unsigned long m68k_read16_map [0x1000000 >> M68K_MEM_SHIFT]; -unsigned long m68k_write8_map [0x1000000 >> M68K_MEM_SHIFT]; -unsigned long m68k_write16_map[0x1000000 >> M68K_MEM_SHIFT]; +uptr m68k_read8_map [0x1000000 >> M68K_MEM_SHIFT]; +uptr m68k_read16_map [0x1000000 >> M68K_MEM_SHIFT]; +uptr m68k_write8_map [0x1000000 >> M68K_MEM_SHIFT]; +uptr m68k_write16_map[0x1000000 >> M68K_MEM_SHIFT]; -static void xmap_set(unsigned long *map, int shift, int start_addr, int end_addr, - void *func_or_mh, int is_func) +static void xmap_set(uptr *map, int shift, int start_addr, int end_addr, + const void *func_or_mh, int is_func) { - unsigned long addr = (unsigned long)func_or_mh; +#ifdef __clang__ + // workaround bug (segfault) in + // Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn) + volatile +#endif + uptr addr = (uptr)func_or_mh; int mask = (1 << shift) - 1; int i; @@ -44,27 +49,38 @@ static void xmap_set(unsigned long *map, int shift, int start_addr, int end_addr for (i = start_addr >> shift; i <= end_addr >> shift; i++) { map[i] = addr >> 1; if (is_func) - map[i] |= 1 << (sizeof(addr) * 8 - 1); + map[i] |= MAP_FLAG; } } -void z80_map_set(unsigned long *map, int start_addr, int end_addr, - void *func_or_mh, int is_func) +void z80_map_set(uptr *map, int start_addr, int end_addr, + const void *func_or_mh, int is_func) { xmap_set(map, Z80_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func); } -void cpu68k_map_set(unsigned long *map, int start_addr, int end_addr, - void *func_or_mh, int is_func) +void cpu68k_map_set(uptr *map, int start_addr, int end_addr, + const void *func_or_mh, int is_func) { xmap_set(map, M68K_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func); +#ifdef EMU_F68K + // setup FAME fetchmap + if (!is_func) + { + int shiftout = 24 - FAMEC_FETCHBITS; + int i = start_addr >> shiftout; + uptr base = (uptr)func_or_mh - (i << shiftout); + for (; i <= (end_addr >> shiftout); i++) + PicoCpuFM68k.Fetch[i] = base; + } +#endif } // more specialized/optimized function (does same as above) void cpu68k_map_all_ram(int start_addr, int end_addr, void *ptr, int is_sub) { - unsigned long *r8map, *r16map, *w8map, *w16map; - unsigned long addr = (unsigned long)ptr; + uptr *r8map, *r16map, *w8map, *w16map; + uptr addr = (uptr)ptr; int shift = M68K_MEM_SHIFT; int i; @@ -84,6 +100,17 @@ void cpu68k_map_all_ram(int start_addr, int end_addr, void *ptr, int is_sub) addr >>= 1; for (i = start_addr >> shift; i <= end_addr >> shift; i++) r8map[i] = r16map[i] = w8map[i] = w16map[i] = addr; +#ifdef EMU_F68K + // setup FAME fetchmap + { + M68K_CONTEXT *ctx = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k; + int shiftout = 24 - FAMEC_FETCHBITS; + i = start_addr >> shiftout; + addr = (uptr)ptr - (i << shiftout); + for (; i <= (end_addr >> shiftout); i++) + ctx->Fetch[i] = addr; + } +#endif } static u32 m68k_unmapped_read8(u32 a) @@ -110,25 +137,30 @@ static void m68k_unmapped_write16(u32 a, u32 d) void m68k_map_unmap(int start_addr, int end_addr) { - unsigned long addr; +#ifdef __clang__ + // workaround bug (segfault) in + // Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn) + volatile +#endif + uptr addr; int shift = M68K_MEM_SHIFT; int i; - addr = (unsigned long)m68k_unmapped_read8; + addr = (uptr)m68k_unmapped_read8; for (i = start_addr >> shift; i <= end_addr >> shift; i++) - m68k_read8_map[i] = (addr >> 1) | (1 << 31); + m68k_read8_map[i] = (addr >> 1) | MAP_FLAG; - addr = (unsigned long)m68k_unmapped_read16; + addr = (uptr)m68k_unmapped_read16; for (i = start_addr >> shift; i <= end_addr >> shift; i++) - m68k_read16_map[i] = (addr >> 1) | (1 << 31); + m68k_read16_map[i] = (addr >> 1) | MAP_FLAG; - addr = (unsigned long)m68k_unmapped_write8; + addr = (uptr)m68k_unmapped_write8; for (i = start_addr >> shift; i <= end_addr >> shift; i++) - m68k_write8_map[i] = (addr >> 1) | (1 << 31); + m68k_write8_map[i] = (addr >> 1) | MAP_FLAG; - addr = (unsigned long)m68k_unmapped_write16; + addr = (uptr)m68k_unmapped_write16; for (i = start_addr >> shift; i <= end_addr >> shift; i++) - m68k_write16_map[i] = (addr >> 1) | (1 << 31); + m68k_write16_map[i] = (addr >> 1) | MAP_FLAG; } MAKE_68K_READ8(m68k_read8, m68k_read8_map) @@ -144,6 +176,10 @@ static u32 ym2612_read_local_68k(void); static int ym2612_write_local(u32 a, u32 d, int is_from_z80); static void z80_mem_setup(void); +#ifdef _ASM_MEMORY_C +u32 PicoRead8_sram(u32 a); +u32 PicoRead16_sram(u32 a); +#endif #ifdef EMU_CORE_DEBUG u32 lastread_a, lastread_d[16]={0,}, lastwrite_cyc_d[16]={0,}, lastwrite_mus_d[16]={0,}; @@ -160,264 +196,232 @@ void log_io(unsigned int addr, int bits, int rw); #endif #if defined(EMU_C68K) -static __inline int PicoMemBase(u32 pc) +void cyclone_crashed(u32 pc, struct Cyclone *context) { - int membase=0; + elprintf(EL_STATUS|EL_ANOMALY, "%c68k crash detected @ %06x", + context == &PicoCpuCM68k ? 'm' : 's', pc); + context->membase = (u32)Pico.rom; + context->pc = (u32)Pico.rom + Pico.romsize; +} +#endif - if (pc> 2) | (pad & 3); // ?0SA 00DU - return membase; + value |= out_bits & 0x40; + return value; } -#endif - -PICO_INTERNAL u32 PicoCheckPc(u32 pc) +static u32 read_pad_6btn(int i, u32 out_bits) { - u32 ret=0; -#if defined(EMU_C68K) - pc-=PicoCpuCM68k.membase; // Get real pc -// pc&=0xfffffe; - pc&=~1; - if ((pc<<8) == 0) - { - elprintf(EL_STATUS|EL_ANOMALY, "%i:%03i: game crash detected @ %06x\n", - Pico.m.frame_count, Pico.m.scanline, SekPc); - return (int)Pico.rom + Pico.romsize; // common crash condition, may happen with bad ROMs + u32 pad = ~PicoIn.padInt[i]; // Get inverse of pad MXYZ SACB RLDU + int phase = Pico.m.padTHPhase[i]; + u32 value; + + if (phase == 2 && !(out_bits & 0x40)) { + value = (pad & 0xc0) >> 2; // ?0SA 0000 + goto out; + } + else if(phase == 3) { + if (out_bits & 0x40) + return (pad & 0x30) | ((pad >> 8) & 0xf); // ?1CB MXYZ + else + return ((pad & 0xc0) >> 2) | 0x0f; // ?0SA 1111 + goto out; } - PicoCpuCM68k.membase=PicoMemBase(pc&0x00ffffff); - PicoCpuCM68k.membase-=pc&0xff000000; + if (out_bits & 0x40) // TH + value = pad & 0x3f; // ?1CB RLDU + else + value = ((pad & 0xc0) >> 2) | (pad & 3); // ?0SA 00DU - ret = PicoCpuCM68k.membase+pc; -#endif - return ret; +out: + value |= out_bits & 0x40; + return value; } - -PICO_INTERNAL void PicoInitPc(u32 pc) +static u32 read_nothing(int i, u32 out_bits) { - PicoCheckPc(pc); + return 0xff; } -// ----------------------------------------------------------------- -// memmap helpers +typedef u32 (port_read_func)(int index, u32 out_bits); + +static port_read_func *port_readers[3] = { + read_pad_3btn, + read_pad_3btn, + read_nothing +}; -static int PadRead(int i) +static NOINLINE u32 port_read(int i) { - int pad,value,data_reg; - pad=~PicoPadInt[i]; // Get inverse of pad MXYZ SACB RLDU - data_reg=Pico.ioports[i+1]; + u32 data_reg = PicoMem.ioports[i + 1]; + u32 ctrl_reg = PicoMem.ioports[i + 4] | 0x80; + u32 in, out; - // orr the bits, which are set as output - value = data_reg&(Pico.ioports[i+4]|0x80); + out = data_reg & ctrl_reg; + out |= 0x7f & ~ctrl_reg; // pull-ups - if (PicoOpt & POPT_6BTN_PAD) - { - int phase = Pico.m.padTHPhase[i]; - - if(phase == 2 && !(data_reg&0x40)) { // TH - value|=(pad&0xc0)>>2; // ?0SA 0000 - return value; - } else if(phase == 3) { - if(data_reg&0x40) - value|=(pad&0x30)|((pad>>8)&0xf); // ?1CB MXYZ - else - value|=((pad&0xc0)>>2)|0x0f; // ?0SA 1111 - return value; - } - } + in = port_readers[i](i, out); + + return (in & ~ctrl_reg) | (data_reg & ctrl_reg); +} - if(data_reg&0x40) // TH - value|=(pad&0x3f); // ?1CB RLDU - else value|=((pad&0xc0)>>2)|(pad&3); // ?0SA 00DU +void PicoSetInputDevice(int port, enum input_device device) +{ + port_read_func *func; + + if (port < 0 || port > 2) + return; + + switch (device) { + case PICO_INPUT_PAD_3BTN: + func = read_pad_3btn; + break; + + case PICO_INPUT_PAD_6BTN: + func = read_pad_6btn; + break; + + default: + func = read_nothing; + break; + } - return value; // will mirror later + port_readers[port] = func; } -static u32 io_ports_read(u32 a) +NOINLINE u32 io_ports_read(u32 a) { u32 d; a = (a>>1) & 0xf; switch (a) { case 0: d = Pico.m.hardware; break; // Hardware value (Version register) - case 1: d = PadRead(0); break; - case 2: d = PadRead(1); break; - default: d = Pico.ioports[a]; break; // IO ports can be used as RAM + case 1: d = port_read(0); break; + case 2: d = port_read(1); break; + case 3: d = port_read(2); break; + default: d = PicoMem.ioports[a]; break; // IO ports can be used as RAM } return d; } -static void io_ports_write(u32 a, u32 d) +NOINLINE void io_ports_write(u32 a, u32 d) { a = (a>>1) & 0xf; // 6 button gamepad: if TH went from 0 to 1, gamepad changes state - if (1 <= a && a <= 2 && (PicoOpt & POPT_6BTN_PAD)) + if (1 <= a && a <= 2) { Pico.m.padDelay[a - 1] = 0; - if (!(Pico.ioports[a] & 0x40) && (d & 0x40)) + if (!(PicoMem.ioports[a] & 0x40) && (d & 0x40)) Pico.m.padTHPhase[a - 1]++; } - // cartain IO ports can be used as RAM - Pico.ioports[a] = d; + // certain IO ports can be used as RAM + PicoMem.ioports[a] = d; } -static void ctl_write_z80busreq(u32 d) +static int z80_cycles_from_68k(void) +{ + int m68k_cnt = SekCyclesDone() - Pico.t.m68c_frame_start; + return cycles_68k_to_z80(m68k_cnt); +} + +void NOINLINE ctl_write_z80busreq(u32 d) { d&=1; d^=1; - elprintf(EL_BUSREQ, "set_zrun: %i->%i [%i] @%06x", Pico.m.z80Run, d, SekCyclesDone(), SekPc); + elprintf(EL_BUSREQ, "set_zrun: %i->%i [%u] @%06x", Pico.m.z80Run, d, SekCyclesDone(), SekPc); if (d ^ Pico.m.z80Run) { if (d) { - z80_cycle_cnt = cycles_68k_to_z80(SekCyclesDone()); + Pico.t.z80c_cnt = z80_cycles_from_68k() + 2; } else { - z80stopCycle = SekCyclesDone(); - if ((PicoOpt&POPT_EN_Z80) && !Pico.m.z80_reset) - PicoSyncZ80(z80stopCycle); + if ((PicoIn.opt & POPT_EN_Z80) && !Pico.m.z80_reset) { + pprof_start(m68k); + PicoSyncZ80(SekCyclesDone()); + pprof_end_sub(m68k); + } } Pico.m.z80Run = d; } } -static void ctl_write_z80reset(u32 d) +void NOINLINE ctl_write_z80reset(u32 d) { d&=1; d^=1; - elprintf(EL_BUSREQ, "set_zreset: %i->%i [%i] @%06x", Pico.m.z80_reset, d, SekCyclesDone(), SekPc); + elprintf(EL_BUSREQ, "set_zreset: %i->%i [%u] @%06x", Pico.m.z80_reset, d, SekCyclesDone(), SekPc); if (d ^ Pico.m.z80_reset) { if (d) { - if ((PicoOpt&POPT_EN_Z80) && Pico.m.z80Run) + if ((PicoIn.opt & POPT_EN_Z80) && Pico.m.z80Run) { + pprof_start(m68k); PicoSyncZ80(SekCyclesDone()); + pprof_end_sub(m68k); + } YM2612ResetChip(); timers_reset(); } else { - z80_cycle_cnt = cycles_68k_to_z80(SekCyclesDone()); + Pico.t.z80c_cnt = z80_cycles_from_68k() + 2; z80_reset(); } Pico.m.z80_reset = d; } } +static int get_scanline(int is_from_z80); -// for nonstandard reads -// TODO: mv to carthw -u32 OtherRead16End(u32 a, int realsize) +static void psg_write_68k(u32 d) { - u32 d=0; + // look for volume write and update if needed + if ((d & 0x90) == 0x90 && Pico.snd.psg_line < Pico.m.scanline) + PsndDoPSG(Pico.m.scanline); - // 32x test -/* - if (a == 0xa130ec) { d = 0x4d41; goto end; } // MA - else if (a == 0xa130ee) { d = 0x5253; goto end; } // RS - else if (a == 0xa15100) { d = 0x0080; goto end; } - else -*/ - - // for games with simple protection devices, discovered by Haze - // some dumb detection is used, but that should be enough to make things work - if ((a>>22) == 1 && Pico.romsize >= 512*1024) { - if (*(int *)(Pico.rom+0x123e4) == 0x00550c39 && *(int *)(Pico.rom+0x123e8) == 0x00000040) { // Super Bubble Bobble (Unl) [!] - if (a == 0x400000) { d=0x55<<8; goto end; } - else if (a == 0x400002) { d=0x0f<<8; goto end; } - } - else if (*(int *)(Pico.rom+0x008c4) == 0x66240055 && *(int *)(Pico.rom+0x008c8) == 0x00404df9) { // Smart Mouse (Unl) - if (a == 0x400000) { d=0x55<<8; goto end; } - else if (a == 0x400002) { d=0x0f<<8; goto end; } - else if (a == 0x400004) { d=0xaa<<8; goto end; } - else if (a == 0x400006) { d=0xf0<<8; goto end; } - } - else if (*(int *)(Pico.rom+0x00404) == 0x00a90600 && *(int *)(Pico.rom+0x00408) == 0x6708b013) { // King of Fighters '98, The (Unl) [!] - if (a == 0x480000 || a == 0x4800e0 || a == 0x4824a0 || a == 0x488880) { d=0xaa<<8; goto end; } - else if (a == 0x4a8820) { d=0x0a<<8; goto end; } - // there is also a read @ 0x4F8820 which needs 0, but that is returned in default case - } - else if (*(int *)(Pico.rom+0x01b24) == 0x004013f9 && *(int *)(Pico.rom+0x01b28) == 0x00ff0000) { // Mahjong Lover (Unl) [!] - if (a == 0x400000) { d=0x90<<8; goto end; } - else if (a == 0x401000) { d=0xd3<<8; goto end; } // this one doesn't seem to be needed, the code does 2 comparisons and only then - // checks the result, which is of the above one. Left it just in case. - } - else if (*(int *)(Pico.rom+0x05254) == 0x0c3962d0 && *(int *)(Pico.rom+0x05258) == 0x00400055) { // Elf Wor (Unl) - if (a == 0x400000) { d=0x55<<8; goto end; } - else if (a == 0x400004) { d=0xc9<<8; goto end; } // this check is done if the above one fails - else if (a == 0x400002) { d=0x0f<<8; goto end; } - else if (a == 0x400006) { d=0x18<<8; goto end; } // similar to above - } - // our default behaviour is to return whatever was last written a 0x400000-0x7fffff range (used by Squirrel King (R) [!]) - // Lion King II, The (Unl) [!] writes @ 400000 and wants to get that val @ 400002 and wites another val - // @ 400004 which is expected @ 400006, so we really remember 2 values here -/// d = Pico.m.prot_bytes[(a>>2)&1]<<8; - } - else if (a == 0xa13000 && Pico.romsize >= 1024*1024) { - if (*(int *)(Pico.rom+0xc8af0) == 0x30133013 && *(int *)(Pico.rom+0xc8af4) == 0x000f0240) { // Rockman X3 (Unl) [!] - d=0x0c; goto end; - } - else if (*(int *)(Pico.rom+0x28888) == 0x07fc0000 && *(int *)(Pico.rom+0x2888c) == 0x4eb94e75) { // Bug's Life, A (Unl) [!] - d=0x28; goto end; // does the check from RAM - } - else if (*(int *)(Pico.rom+0xc8778) == 0x30133013 && *(int *)(Pico.rom+0xc877c) == 0x000f0240) { // Super Mario Bros. (Unl) [!] - d=0x0c; goto end; // seems to be the same code as in Rockman X3 (Unl) [!] - } - else if (*(int *)(Pico.rom+0xf20ec) == 0x30143013 && *(int *)(Pico.rom+0xf20f0) == 0x000f0200) { // Super Mario 2 1998 (Unl) [!] - d=0x0a; goto end; - } - } - else if (a == 0xa13002) { // Pocket Monsters (Unl) - d=0x01; goto end; - } - else if (a == 0xa1303E) { // Pocket Monsters (Unl) - d=0x1f; goto end; - } - else if (a == 0x30fe02) { - // Virtua Racing - just for fun - // this seems to be some flag that SVP is ready or something similar - d=1; goto end; - } - -end: - elprintf(EL_UIO, "strange r%i: [%06x] %04x @%06x", realsize, a&0xffffff, d, SekPc); - return d; + SN76496Write(d); } -void OtherWrite8End(u32 a,u32 d,int realsize) +static void psg_write_z80(u32 d) { - // for games with simple protection devices, discovered by Haze - if ((a>>22) == 1) -;/// Pico.m.prot_bytes[(a>>2)&1] = (u8)d; + if ((d & 0x90) == 0x90) { + int scanline = get_scanline(1); + if (Pico.snd.psg_line < scanline) + PsndDoPSG(scanline); + } + + SN76496Write(d); } // ----------------------------------------------------------------- +#ifndef _ASM_MEMORY_C + // cart (save) RAM area (usually 0x200000 - ...) static u32 PicoRead8_sram(u32 a) { u32 d; - if (SRam.start <= a && a <= SRam.end && (Pico.m.sram_reg & SRR_MAPPED)) + if (Pico.sv.start <= a && a <= Pico.sv.end && (Pico.m.sram_reg & SRR_MAPPED)) { - if (SRam.flags & SRF_EEPROM) { + if (Pico.sv.flags & SRF_EEPROM) { d = EEPROM_read(); if (!(a & 1)) d >>= 8; } else - d = *(u8 *)(SRam.data - SRam.start + a); + d = *(u8 *)(Pico.sv.data - Pico.sv.start + a); elprintf(EL_SRAMIO, "sram r8 [%06x] %02x @ %06x", a, d, SekPc); return d; } @@ -432,12 +436,12 @@ static u32 PicoRead8_sram(u32 a) static u32 PicoRead16_sram(u32 a) { u32 d; - if (SRam.end >= a && a >= SRam.start && (Pico.m.sram_reg & SRR_MAPPED)) + if (Pico.sv.start <= a && a <= Pico.sv.end && (Pico.m.sram_reg & SRR_MAPPED)) { - if (SRam.flags & SRF_EEPROM) + if (Pico.sv.flags & SRF_EEPROM) d = EEPROM_read(); else { - u8 *pm = (u8 *)(SRam.data - SRam.start + a); + u8 *pm = (u8 *)(Pico.sv.data - Pico.sv.start + a); d = pm[0] << 8; d |= pm[1]; } @@ -451,22 +455,24 @@ static u32 PicoRead16_sram(u32 a) return m68k_unmapped_read16(a); } +#endif // _ASM_MEMORY_C + static void PicoWrite8_sram(u32 a, u32 d) { - if (a > SRam.end || a < SRam.start || !(Pico.m.sram_reg & SRR_MAPPED)) { + if (a > Pico.sv.end || a < Pico.sv.start || !(Pico.m.sram_reg & SRR_MAPPED)) { m68k_unmapped_write8(a, d); return; } elprintf(EL_SRAMIO, "sram w8 [%06x] %02x @ %06x", a, d & 0xff, SekPc); - if (SRam.flags & SRF_EEPROM) + if (Pico.sv.flags & SRF_EEPROM) { EEPROM_write8(a, d); } else { - u8 *pm = (u8 *)(SRam.data - SRam.start + a); + u8 *pm = (u8 *)(Pico.sv.data - Pico.sv.start + a); if (*pm != (u8)d) { - SRam.changed = 1; + Pico.sv.changed = 1; *pm = (u8)d; } } @@ -474,22 +480,25 @@ static void PicoWrite8_sram(u32 a, u32 d) static void PicoWrite16_sram(u32 a, u32 d) { - if (a > SRam.end || a < SRam.start || !(Pico.m.sram_reg & SRR_MAPPED)) { + if (a > Pico.sv.end || a < Pico.sv.start || !(Pico.m.sram_reg & SRR_MAPPED)) { m68k_unmapped_write16(a, d); return; } elprintf(EL_SRAMIO, "sram w16 [%06x] %04x @ %06x", a, d & 0xffff, SekPc); - if (SRam.flags & SRF_EEPROM) + if (Pico.sv.flags & SRF_EEPROM) { EEPROM_write16(d); } else { - // XXX: hardware could easily use MSB too.. - u8 *pm = (u8 *)(SRam.data - SRam.start + a); - if (*pm != (u8)d) { - SRam.changed = 1; - *pm = (u8)d; + u8 *pm = (u8 *)(Pico.sv.data - Pico.sv.start + a); + if (pm[0] != (u8)(d >> 8)) { + Pico.sv.changed = 1; + pm[0] = (u8)(d >> 8); + } + if (pm[1] != (u8)d) { + Pico.sv.changed = 1; + pm[1] = (u8)d; } } } @@ -506,7 +515,7 @@ static u32 PicoRead8_z80(u32 a) } if ((a & 0x4000) == 0x0000) - d = Pico.zram[a & 0x1fff]; + d = PicoMem.zram[a & 0x1fff]; else if ((a & 0x6000) == 0x4000) // 0x4000-0x5fff d = ym2612_read_local_68k(); else @@ -529,22 +538,19 @@ static void PicoWrite8_z80(u32 a, u32 d) } if ((a & 0x4000) == 0x0000) { // z80 RAM - SekCyclesBurn(2); // hack - Pico.zram[a & 0x1fff] = (u8)d; + PicoMem.zram[a & 0x1fff] = (u8)d; return; } if ((a & 0x6000) == 0x4000) { // FM Sound - if (PicoOpt & POPT_EN_FM) - emustatus |= ym2612_write_local(a&3, d&0xff, 0)&1; + if (PicoIn.opt & POPT_EN_FM) + Pico.m.status |= ym2612_write_local(a & 3, d & 0xff, 0) & 1; return; } // TODO: probably other VDP access too? Maybe more mirrors? if ((a & 0x7ff9) == 0x7f11) { // PSG Sound - if (PicoOpt & POPT_EN_PSG) - SN76496Write(d); + psg_write_68k(d); return; } -#if !defined(_ASM_MEMORY_C) || defined(_ASM_MEMORY_C_AMIPS) if ((a & 0x7f00) == 0x6000) // Z80 BANK register { Pico.m.z80_bank68k >>= 1; @@ -553,7 +559,6 @@ static void PicoWrite8_z80(u32 a, u32 d) elprintf(EL_Z80BNK, "z80 bank=%06x", Pico.m.z80_bank68k << 15); return; } -#endif elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %02x @ %06x", a, d&0xff, SekPc); } @@ -564,6 +569,8 @@ static void PicoWrite16_z80(u32 a, u32 d) PicoWrite8_z80(a, d >> 8); } +#ifndef _ASM_MEMORY_C + // IO/control area (0xa10000 - 0xa1ffff) u32 PicoRead8_io(u32 a) { @@ -578,17 +585,20 @@ u32 PicoRead8_io(u32 a) d = Pico.m.rotate++; d ^= d << 6; - // bit8 seems to be readable in this range - if ((a & 0xfc01) == 0x1000) - d &= ~0x01; + if ((a & 0xfc00) == 0x1000) { + // bit8 seems to be readable in this range + if (!(a & 1)) + d &= ~0x01; - if ((a & 0xff01) == 0x1100) { // z80 busreq (verified) - d |= (Pico.m.z80Run | Pico.m.z80_reset) & 1; - elprintf(EL_BUSREQ, "get_zrun: %02x [%i] @%06x", d, SekCyclesDone(), SekPc); + if ((a & 0xff01) == 0x1100) { // z80 busreq (verified) + d |= (Pico.m.z80Run | Pico.m.z80_reset) & 1; + elprintf(EL_BUSREQ, "get_zrun: %02x [%u] @%06x", d, SekCyclesDone(), SekPc); + } goto end; } - d = m68k_unmapped_read8(a); + d = PicoRead8_32x(a); + end: return d; } @@ -599,6 +609,7 @@ u32 PicoRead16_io(u32 a) if ((a & 0xffe0) == 0x0000) { // I/O ports d = io_ports_read(a); + d |= d << 8; goto end; } @@ -607,16 +618,18 @@ u32 PicoRead16_io(u32 a) d ^= (d << 5) ^ (d << 8); // bit8 seems to be readable in this range - if ((a & 0xfc00) == 0x1000) + if ((a & 0xfc00) == 0x1000) { d &= ~0x0100; - if ((a & 0xff00) == 0x1100) { // z80 busreq - d |= ((Pico.m.z80Run | Pico.m.z80_reset) & 1) << 8; - elprintf(EL_BUSREQ, "get_zrun: %04x [%i] @%06x", d, SekCyclesDone(), SekPc); + if ((a & 0xff00) == 0x1100) { // z80 busreq + d |= ((Pico.m.z80Run | Pico.m.z80_reset) & 1) << 8; + elprintf(EL_BUSREQ, "get_zrun: %04x [%u] @%06x", d, SekCyclesDone(), SekPc); + } goto end; } - d = m68k_unmapped_read16(a); + d = PicoRead16_32x(a); + end: return d; } @@ -641,7 +654,7 @@ void PicoWrite8_io(u32 a, u32 d) Pico.m.sram_reg |= (u8)(d & 3); return; } - m68k_unmapped_write8(a, d); + PicoWrite8_32x(a, d); } void PicoWrite16_io(u32 a, u32 d) @@ -664,15 +677,28 @@ void PicoWrite16_io(u32 a, u32 d) Pico.m.sram_reg |= (u8)(d & 3); return; } - m68k_unmapped_write16(a, d); + PicoWrite16_32x(a, d); } +#endif // _ASM_MEMORY_C + // VDP area (0xc00000 - 0xdfffff) // TODO: verify if lower byte goes to PSG on word writes -static u32 PicoRead8_vdp(u32 a) +u32 PicoRead8_vdp(u32 a) { - if ((a & 0x00e0) == 0x0000) - return PicoVideoRead8(a); + if ((a & 0x00f0) == 0x0000) { + switch (a & 0x0d) + { + case 0x00: return PicoVideoRead8DataH(); + case 0x01: return PicoVideoRead8DataL(); + case 0x04: return PicoVideoRead8CtlH(); + case 0x05: return PicoVideoRead8CtlL(); + case 0x08: + case 0x0c: return PicoVideoRead8HV_H(); + case 0x09: + case 0x0d: return PicoVideoRead8HV_L(); + } + } elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc); return 0; @@ -690,8 +716,7 @@ static u32 PicoRead16_vdp(u32 a) static void PicoWrite8_vdp(u32 a, u32 d) { if ((a & 0x00f9) == 0x0011) { // PSG Sound - if (PicoOpt & POPT_EN_PSG) - SN76496Write(d); + psg_write_68k(d); return; } if ((a & 0x00e0) == 0x0000) { @@ -705,11 +730,8 @@ static void PicoWrite8_vdp(u32 a, u32 d) static void PicoWrite16_vdp(u32 a, u32 d) { - if ((a & 0x00f9) == 0x0010) { // PSG Sound - if (PicoOpt & POPT_EN_PSG) - SN76496Write(d); - return; - } + if ((a & 0x00f9) == 0x0010) // PSG Sound + psg_write_68k(d); if ((a & 0x00e0) == 0x0000) { PicoVideoWrite(a, d); return; @@ -726,7 +748,7 @@ static void m68k_mem_setup(void); PICO_INTERNAL void PicoMemSetup(void) { - int mask, rs, a; + int mask, rs, sstart, a; // setup the memory map cpu68k_map_set(m68k_read8_map, 0x000000, 0xffffff, m68k_unmapped_read8, 1); @@ -742,15 +764,16 @@ PICO_INTERNAL void PicoMemSetup(void) cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico.rom, 0); // Common case of on-cart (save) RAM, usually at 0x200000-... - if ((SRam.flags & SRF_ENABLED) && SRam.data != NULL) { - rs = SRam.end - SRam.start; + if ((Pico.sv.flags & SRF_ENABLED) && Pico.sv.data != NULL) { + sstart = Pico.sv.start; + rs = Pico.sv.end - sstart; rs = (rs + mask) & ~mask; - if (SRam.start + rs >= 0x1000000) - rs = 0x1000000 - SRam.start; - cpu68k_map_set(m68k_read8_map, SRam.start, SRam.start + rs - 1, PicoRead8_sram, 1); - cpu68k_map_set(m68k_read16_map, SRam.start, SRam.start + rs - 1, PicoRead16_sram, 1); - cpu68k_map_set(m68k_write8_map, SRam.start, SRam.start + rs - 1, PicoWrite8_sram, 1); - cpu68k_map_set(m68k_write16_map, SRam.start, SRam.start + rs - 1, PicoWrite16_sram, 1); + if (sstart + rs >= 0x1000000) + rs = 0x1000000 - sstart; + cpu68k_map_set(m68k_read8_map, sstart, sstart + rs - 1, PicoRead8_sram, 1); + cpu68k_map_set(m68k_read16_map, sstart, sstart + rs - 1, PicoRead16_sram, 1); + cpu68k_map_set(m68k_write8_map, sstart, sstart + rs - 1, PicoWrite8_sram, 1); + cpu68k_map_set(m68k_write16_map, sstart, sstart + rs - 1, PicoWrite16_sram, 1); } // Z80 region @@ -777,21 +800,24 @@ PICO_INTERNAL void PicoMemSetup(void) // RAM and it's mirrors for (a = 0xe00000; a < 0x1000000; a += 0x010000) { - cpu68k_map_set(m68k_read8_map, a, a + 0xffff, Pico.ram, 0); - cpu68k_map_set(m68k_read16_map, a, a + 0xffff, Pico.ram, 0); - cpu68k_map_set(m68k_write8_map, a, a + 0xffff, Pico.ram, 0); - cpu68k_map_set(m68k_write16_map, a, a + 0xffff, Pico.ram, 0); + cpu68k_map_set(m68k_read8_map, a, a + 0xffff, PicoMem.ram, 0); + cpu68k_map_set(m68k_read16_map, a, a + 0xffff, PicoMem.ram, 0); + cpu68k_map_set(m68k_write8_map, a, a + 0xffff, PicoMem.ram, 0); + cpu68k_map_set(m68k_write16_map, a, a + 0xffff, PicoMem.ram, 0); } // Setup memory callbacks: #ifdef EMU_C68K - PicoCpuCM68k.checkpc = PicoCheckPc; - PicoCpuCM68k.fetch8 = PicoCpuCM68k.read8 = m68k_read8; - PicoCpuCM68k.fetch16 = PicoCpuCM68k.read16 = m68k_read16; - PicoCpuCM68k.fetch32 = PicoCpuCM68k.read32 = m68k_read32; - PicoCpuCM68k.write8 = m68k_write8; - PicoCpuCM68k.write16 = m68k_write16; - PicoCpuCM68k.write32 = m68k_write32; + PicoCpuCM68k.read8 = (void *)m68k_read8_map; + PicoCpuCM68k.read16 = (void *)m68k_read16_map; + PicoCpuCM68k.read32 = (void *)m68k_read16_map; + PicoCpuCM68k.write8 = (void *)m68k_write8_map; + PicoCpuCM68k.write16 = (void *)m68k_write16_map; + PicoCpuCM68k.write32 = (void *)m68k_write16_map; + PicoCpuCM68k.checkpc = NULL; /* unused */ + PicoCpuCM68k.fetch8 = NULL; + PicoCpuCM68k.fetch16 = NULL; + PicoCpuCM68k.fetch32 = NULL; #endif #ifdef EMU_F68K PicoCpuFM68k.read_byte = m68k_read8; @@ -805,14 +831,12 @@ PICO_INTERNAL void PicoMemSetup(void) { int i; // by default, point everything to first 64k of ROM - for (i = 0; i < M68K_FETCHBANK1; i++) - PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom - (i<<(24-FAMEC_FETCHBITS)); + for (i = 0; i < M68K_FETCHBANK1 * 0xe0 / 0x100; i++) + PicoCpuFM68k.Fetch[i] = (uptr)Pico.rom - (i<<(24-FAMEC_FETCHBITS)); // now real ROM for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < Pico.romsize; i++) - PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom; - // .. and RAM - for (i = M68K_FETCHBANK1*14/16; i < M68K_FETCHBANK1; i++) - PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.ram - (i<<(24-FAMEC_FETCHBITS)); + PicoCpuFM68k.Fetch[i] = (uptr)Pico.rom; + // RAM already set } #endif #ifdef EMU_M68K @@ -855,10 +879,11 @@ static void m68k_mem_setup(void) static int get_scanline(int is_from_z80) { if (is_from_z80) { - int cycles = z80_cyclesDone(); - while (cycles - z80_scanline_cycles >= 228) - z80_scanline++, z80_scanline_cycles += 228; - return z80_scanline; + int mclk_z80 = z80_cyclesDone() * 15; + int mclk_line = Pico.t.z80_scanline * 488 * 7; + while (mclk_z80 - mclk_line >= 488 * 7) + Pico.t.z80_scanline++, mclk_line += 488 * 7; + return Pico.t.z80_scanline; } return Pico.m.scanline; @@ -870,41 +895,41 @@ void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new) int xcycles = z80_cycles << 8; /* check for overflows */ - if ((mode_old & 4) && xcycles > timer_a_next_oflow) + if ((mode_old & 4) && xcycles > Pico.t.timer_a_next_oflow) ym2612.OPN.ST.status |= 1; - if ((mode_old & 8) && xcycles > timer_b_next_oflow) + if ((mode_old & 8) && xcycles > Pico.t.timer_b_next_oflow) ym2612.OPN.ST.status |= 2; /* update timer a */ if (mode_old & 1) - while (xcycles > timer_a_next_oflow) - timer_a_next_oflow += timer_a_step; + while (xcycles > Pico.t.timer_a_next_oflow) + Pico.t.timer_a_next_oflow += Pico.t.timer_a_step; if ((mode_old ^ mode_new) & 1) // turning on/off { if (mode_old & 1) - timer_a_next_oflow = TIMER_NO_OFLOW; + Pico.t.timer_a_next_oflow = TIMER_NO_OFLOW; else - timer_a_next_oflow = xcycles + timer_a_step; + Pico.t.timer_a_next_oflow = xcycles + Pico.t.timer_a_step; } if (mode_new & 1) - elprintf(EL_YMTIMER, "timer a upd to %i @ %i", timer_a_next_oflow>>8, z80_cycles); + elprintf(EL_YMTIMER, "timer a upd to %i @ %i", Pico.t.timer_a_next_oflow>>8, z80_cycles); /* update timer b */ if (mode_old & 2) - while (xcycles > timer_b_next_oflow) - timer_b_next_oflow += timer_b_step; + while (xcycles > Pico.t.timer_b_next_oflow) + Pico.t.timer_b_next_oflow += Pico.t.timer_b_step; if ((mode_old ^ mode_new) & 2) { if (mode_old & 2) - timer_b_next_oflow = TIMER_NO_OFLOW; + Pico.t.timer_b_next_oflow = TIMER_NO_OFLOW; else - timer_b_next_oflow = xcycles + timer_b_step; + Pico.t.timer_b_next_oflow = xcycles + Pico.t.timer_b_step; } if (mode_new & 2) - elprintf(EL_YMTIMER, "timer b upd to %i @ %i", timer_b_next_oflow>>8, z80_cycles); + elprintf(EL_YMTIMER, "timer b upd to %i @ %i", Pico.t.timer_b_next_oflow>>8, z80_cycles); } // ym2612 DAC and timer I/O handlers for z80 @@ -916,9 +941,9 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) if (a == 1 && ym2612.OPN.ST.address == 0x2a) /* DAC data */ { int scanline = get_scanline(is_from_z80); - //elprintf(EL_STATUS, "%03i -> %03i dac w %08x z80 %i", PsndDacLine, scanline, d, is_from_z80); + //elprintf(EL_STATUS, "%03i -> %03i dac w %08x z80 %i", Pico.snd.dac_line, scanline, d, is_from_z80); ym2612.dacout = ((int)d - 0x80) << 6; - if (PsndOut && ym2612.dacen && scanline >= PsndDacLine) + if (ym2612.dacen) PsndDoDAC(scanline); return 0; } @@ -929,7 +954,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) ym2612.OPN.ST.address = d; ym2612.addr_A1 = 0; #ifdef __GP2X__ - if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, -1); + if (PicoIn.opt & POPT_EXT_FM) YM2612Write_940(a, d, -1); #endif return 0; @@ -952,13 +977,13 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) ym2612.OPN.ST.TA = TAnew; //ym2612.OPN.ST.TAC = (1024-TAnew)*18; //ym2612.OPN.ST.TAT = 0; - timer_a_step = TIMER_A_TICK_ZCYCLES * (1024 - TAnew); + Pico.t.timer_a_step = TIMER_A_TICK_ZCYCLES * (1024 - TAnew); if (ym2612.OPN.ST.mode & 1) { // this is not right, should really be done on overflow only - int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone()); - timer_a_next_oflow = (cycles << 8) + timer_a_step; + int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k(); + Pico.t.timer_a_next_oflow = (cycles << 8) + Pico.t.timer_a_step; } - elprintf(EL_YMTIMER, "timer a set to %i, %i", 1024 - TAnew, timer_a_next_oflow>>8); + elprintf(EL_YMTIMER, "timer a set to %i, %i", 1024 - TAnew, Pico.t.timer_a_next_oflow>>8); } return 0; } @@ -968,17 +993,17 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) ym2612.OPN.ST.TB = d; //ym2612.OPN.ST.TBC = (256-d) * 288; //ym2612.OPN.ST.TBT = 0; - timer_b_step = TIMER_B_TICK_ZCYCLES * (256 - d); // 262800 + Pico.t.timer_b_step = TIMER_B_TICK_ZCYCLES * (256 - d); // 262800 if (ym2612.OPN.ST.mode & 2) { - int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone()); - timer_b_next_oflow = (cycles << 8) + timer_b_step; + int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k(); + Pico.t.timer_b_next_oflow = (cycles << 8) + Pico.t.timer_b_step; } - elprintf(EL_YMTIMER, "timer b set to %i, %i", 256 - d, timer_b_next_oflow>>8); + elprintf(EL_YMTIMER, "timer b set to %i, %i", 256 - d, Pico.t.timer_b_next_oflow>>8); } return 0; case 0x27: { /* mode, timer control */ int old_mode = ym2612.OPN.ST.mode; - int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone()); + int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k(); ym2612.OPN.ST.mode = d; elprintf(EL_YMTIMER, "st mode %02x", d); @@ -994,7 +1019,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) if ((d ^ old_mode) & 0xc0) { #ifdef __GP2X__ - if (PicoOpt & POPT_EXT_FM) return YM2612Write_940(a, d, get_scanline(is_from_z80)); + if (PicoIn.opt & POPT_EXT_FM) return YM2612Write_940(a, d, get_scanline(is_from_z80)); #endif return 1; } @@ -1002,10 +1027,12 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) } case 0x2b: { /* DAC Sel (YM2612) */ int scanline = get_scanline(is_from_z80); - ym2612.dacen = d & 0x80; - if (d & 0x80) PsndDacLine = scanline; + if (ym2612.dacen != (d & 0x80)) { + ym2612.dacen = d & 0x80; + Pico.snd.dac_line = scanline; + } #ifdef __GP2X__ - if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, scanline); + if (PicoIn.opt & POPT_EXT_FM) YM2612Write_940(a, d, scanline); #endif return 0; } @@ -1016,7 +1043,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) ym2612.OPN.ST.address = d; ym2612.addr_A1 = 1; #ifdef __GP2X__ - if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, -1); + if (PicoIn.opt & POPT_EXT_FM) YM2612Write_940(a, d, -1); #endif return 0; @@ -1030,7 +1057,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) } #ifdef __GP2X__ - if (PicoOpt & POPT_EXT_FM) + if (PicoIn.opt & POPT_EXT_FM) return YM2612Write_940(a, d, get_scanline(is_from_z80)); #endif return YM2612Write_(a, d); @@ -1038,30 +1065,32 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) #define ym2612_read_local() \ - if (xcycles >= timer_a_next_oflow) \ + if (xcycles >= Pico.t.timer_a_next_oflow) \ ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 1; \ - if (xcycles >= timer_b_next_oflow) \ + if (xcycles >= Pico.t.timer_b_next_oflow) \ ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 2 -static u32 MEMH_FUNC ym2612_read_local_z80(void) +static u32 ym2612_read_local_z80(void) { int xcycles = z80_cyclesDone() << 8; ym2612_read_local(); - elprintf(EL_YMTIMER, "timer z80 read %i, sched %i, %i @ %i|%i", ym2612.OPN.ST.status, - timer_a_next_oflow>>8, timer_b_next_oflow>>8, xcycles >> 8, (xcycles >> 8) / 228); + elprintf(EL_YMTIMER, "timer z80 read %i, sched %i, %i @ %i|%i", + ym2612.OPN.ST.status, Pico.t.timer_a_next_oflow >> 8, + Pico.t.timer_b_next_oflow >> 8, xcycles >> 8, (xcycles >> 8) / 228); return ym2612.OPN.ST.status; } static u32 ym2612_read_local_68k(void) { - int xcycles = cycles_68k_to_z80(SekCyclesDone()) << 8; + int xcycles = z80_cycles_from_68k() << 8; ym2612_read_local(); - elprintf(EL_YMTIMER, "timer 68k read %i, sched %i, %i @ %i|%i", ym2612.OPN.ST.status, - timer_a_next_oflow>>8, timer_b_next_oflow>>8, xcycles >> 8, (xcycles >> 8) / 228); + elprintf(EL_YMTIMER, "timer 68k read %i, sched %i, %i @ %i|%i", + ym2612.OPN.ST.status, Pico.t.timer_a_next_oflow >> 8, + Pico.t.timer_b_next_oflow >> 8, xcycles >> 8, (xcycles >> 8) / 228); return ym2612.OPN.ST.status; } @@ -1071,15 +1100,17 @@ void ym2612_pack_state(void) int tac, tat = 0, tbc, tbt = 0; tac = 1024 - ym2612.OPN.ST.TA; tbc = 256 - ym2612.OPN.ST.TB; - if (timer_a_next_oflow != TIMER_NO_OFLOW) - tat = (int)((double)(timer_a_step - timer_a_next_oflow) / (double)timer_a_step * tac * 65536); - if (timer_b_next_oflow != TIMER_NO_OFLOW) - tbt = (int)((double)(timer_b_step - timer_b_next_oflow) / (double)timer_b_step * tbc * 65536); + if (Pico.t.timer_a_next_oflow != TIMER_NO_OFLOW) + tat = (int)((double)(Pico.t.timer_a_step - Pico.t.timer_a_next_oflow) + / (double)Pico.t.timer_a_step * tac * 65536); + if (Pico.t.timer_b_next_oflow != TIMER_NO_OFLOW) + tbt = (int)((double)(Pico.t.timer_b_step - Pico.t.timer_b_next_oflow) + / (double)Pico.t.timer_b_step * tbc * 65536); elprintf(EL_YMTIMER, "save: timer a %i/%i", tat >> 16, tac); elprintf(EL_YMTIMER, "save: timer b %i/%i", tbt >> 16, tbc); #ifdef __GP2X__ - if (PicoOpt & POPT_EXT_FM) + if (PicoIn.opt & POPT_EXT_FM) YM2612PicoStateSave2_940(tat, tbt); else #endif @@ -1114,7 +1145,7 @@ void ym2612_unpack_state(void) } #ifdef __GP2X__ - if (PicoOpt & POPT_EXT_FM) + if (PicoIn.opt & POPT_EXT_FM) ret = YM2612PicoStateLoad2_940(&tat, &tbt); else #endif @@ -1127,35 +1158,59 @@ void ym2612_unpack_state(void) tac = (1024 - ym2612.OPN.ST.TA) << 16; tbc = (256 - ym2612.OPN.ST.TB) << 16; if (ym2612.OPN.ST.mode & 1) - timer_a_next_oflow = (int)((double)(tac - tat) / (double)tac * timer_a_step); + Pico.t.timer_a_next_oflow = (int)((double)(tac - tat) / (double)tac * Pico.t.timer_a_step); else - timer_a_next_oflow = TIMER_NO_OFLOW; + Pico.t.timer_a_next_oflow = TIMER_NO_OFLOW; if (ym2612.OPN.ST.mode & 2) - timer_b_next_oflow = (int)((double)(tbc - tbt) / (double)tbc * timer_b_step); + Pico.t.timer_b_next_oflow = (int)((double)(tbc - tbt) / (double)tbc * Pico.t.timer_b_step); else - timer_b_next_oflow = TIMER_NO_OFLOW; - elprintf(EL_YMTIMER, "load: %i/%i, timer_a_next_oflow %i", tat>>16, tac>>16, timer_a_next_oflow >> 8); - elprintf(EL_YMTIMER, "load: %i/%i, timer_b_next_oflow %i", tbt>>16, tbc>>16, timer_b_next_oflow >> 8); + Pico.t.timer_b_next_oflow = TIMER_NO_OFLOW; + elprintf(EL_YMTIMER, "load: %i/%i, timer_a_next_oflow %i", tat>>16, tac>>16, Pico.t.timer_a_next_oflow >> 8); + elprintf(EL_YMTIMER, "load: %i/%i, timer_b_next_oflow %i", tbt>>16, tbc>>16, Pico.t.timer_b_next_oflow >> 8); } +#if defined(NO_32X) && defined(_ASM_MEMORY_C) +// referenced by asm code +u32 PicoRead8_32x(u32 a) { return 0; } +u32 PicoRead16_32x(u32 a) { return 0; } +void PicoWrite8_32x(u32 a, u32 d) {} +void PicoWrite16_32x(u32 a, u32 d) {} +#endif + // ----------------------------------------------------------------- // z80 memhandlers -static unsigned char MEMH_FUNC z80_md_vdp_read(unsigned short a) +static unsigned char z80_md_vdp_read(unsigned short a) { - // TODO? + z80_subCLeft(2); + + if ((a & 0x00f0) == 0x0000) { + switch (a & 0x0d) + { + case 0x00: return PicoVideoRead8DataH(); + case 0x01: return PicoVideoRead8DataL(); + case 0x04: return PicoVideoRead8CtlH(); + case 0x05: return PicoVideoRead8CtlL(); + case 0x08: + case 0x0c: return get_scanline(1); // FIXME: make it proper + case 0x09: + case 0x0d: return Pico.m.rotate++; + } + } + elprintf(EL_ANOMALY, "z80 invalid r8 [%06x] %02x", a, 0xff); return 0xff; } -static unsigned char MEMH_FUNC z80_md_bank_read(unsigned short a) +static unsigned char z80_md_bank_read(unsigned short a) { - extern unsigned int PicoReadM68k8(unsigned int a); unsigned int addr68k; unsigned char ret; - addr68k = Pico.m.z80_bank68k<<15; - addr68k += a & 0x7fff; + z80_subCLeft(3); + + addr68k = Pico.m.z80_bank68k << 15; + addr68k |= a & 0x7fff; ret = m68k_read8(addr68k); @@ -1163,21 +1218,20 @@ static unsigned char MEMH_FUNC z80_md_bank_read(unsigned short a) return ret; } -static void MEMH_FUNC z80_md_ym2612_write(unsigned int a, unsigned char data) +static void z80_md_ym2612_write(unsigned int a, unsigned char data) { - if (PicoOpt & POPT_EN_FM) - emustatus |= ym2612_write_local(a, data, 1) & 1; + if (PicoIn.opt & POPT_EN_FM) + Pico.m.status |= ym2612_write_local(a, data, 1) & 1; } -static void MEMH_FUNC z80_md_vdp_br_write(unsigned int a, unsigned char data) +static void z80_md_vdp_br_write(unsigned int a, unsigned char data) { - // TODO: allow full VDP access if ((a&0xfff9) == 0x7f11) // 7f11 7f13 7f15 7f17 { - if (PicoOpt & POPT_EN_PSG) - SN76496Write(data); + psg_write_z80(data); return; } + // at least VDP data writes hang my machine if ((a>>8) == 0x60) { @@ -1190,9 +1244,8 @@ static void MEMH_FUNC z80_md_vdp_br_write(unsigned int a, unsigned char data) elprintf(EL_ANOMALY, "z80 invalid w8 [%06x] %02x", a, data); } -static void MEMH_FUNC z80_md_bank_write(unsigned int a, unsigned char data) +static void z80_md_bank_write(unsigned int a, unsigned char data) { - extern void PicoWriteM68k8(unsigned int a, unsigned char d); unsigned int addr68k; addr68k = Pico.m.z80_bank68k << 15; @@ -1217,14 +1270,14 @@ static void z80_md_out(unsigned short p, unsigned char d) static void z80_mem_setup(void) { - z80_map_set(z80_read_map, 0x0000, 0x1fff, Pico.zram, 0); - z80_map_set(z80_read_map, 0x2000, 0x3fff, Pico.zram, 0); + z80_map_set(z80_read_map, 0x0000, 0x1fff, PicoMem.zram, 0); + z80_map_set(z80_read_map, 0x2000, 0x3fff, PicoMem.zram, 0); z80_map_set(z80_read_map, 0x4000, 0x5fff, ym2612_read_local_z80, 1); z80_map_set(z80_read_map, 0x6000, 0x7fff, z80_md_vdp_read, 1); z80_map_set(z80_read_map, 0x8000, 0xffff, z80_md_bank_read, 1); - z80_map_set(z80_write_map, 0x0000, 0x1fff, Pico.zram, 0); - z80_map_set(z80_write_map, 0x2000, 0x3fff, Pico.zram, 0); + z80_map_set(z80_write_map, 0x0000, 0x1fff, PicoMem.zram, 0); + z80_map_set(z80_write_map, 0x2000, 0x3fff, PicoMem.zram, 0); z80_map_set(z80_write_map, 0x4000, 0x5fff, z80_md_ym2612_write, 1); z80_map_set(z80_write_map, 0x6000, 0x7fff, z80_md_vdp_br_write, 1); z80_map_set(z80_write_map, 0x8000, 0xffff, z80_md_bank_write, 1); @@ -1234,10 +1287,11 @@ static void z80_mem_setup(void) drZ80.z80_out = z80_md_out; #endif #ifdef _USE_CZ80 - Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (UINT32)Pico.zram); // main RAM - Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (UINT32)Pico.zram); // mirror + Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (FPTR)PicoMem.zram); // main RAM + Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (FPTR)PicoMem.zram); // mirror Cz80_Set_INPort(&CZ80, z80_md_in); Cz80_Set_OUTPort(&CZ80, z80_md_out); #endif } +// vim:shiftwidth=2:ts=2:expandtab