X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=Pico%2FMemory.c;h=755b5b5046b01468683cccaa696f26bc14b7b0b0;hb=e4fb433cd685d06ddbf0ec367c19a38b75d6ac68;hp=29f0470a78bd0e0626307764c0573bd3451d3eec;hpb=9037e45d9f2752fdd6ebbc01328148839403a84f;p=picodrive.git diff --git a/Pico/Memory.c b/Pico/Memory.c index 29f0470..755b5b5 100644 --- a/Pico/Memory.c +++ b/Pico/Memory.c @@ -67,7 +67,7 @@ static __inline int PicoMemBase(u32 pc) #endif -static u32 PicoCheckPc(u32 pc) +PICO_INTERNAL u32 PicoCheckPc(u32 pc) { u32 ret=0; #if defined(EMU_C68K) @@ -687,6 +687,302 @@ static void m68k_mem_setup(void) #endif // EMU_M68K +// ----------------------------------------------------------------- + +extern const unsigned short vcounts[]; + +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; + } + + if (Pico.m.scanline != -1) + return Pico.m.scanline; + + return vcounts[SekCyclesDone()>>8]; +} + +/* probably not should be in this file, but it's near related code here */ +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) + ym2612.OPN.ST.status |= 1; + + if ((mode_old & 8) && xcycles > 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; + + if ((mode_old ^ mode_new) & 1) // turning on/off + { + if (mode_old & 1) { + timer_a_offset = timer_a_next_oflow - xcycles; + timer_a_next_oflow = TIMER_NO_OFLOW; + } + else + timer_a_next_oflow = xcycles + timer_a_offset; + } + if (mode_new & 1) + elprintf(EL_YMTIMER, "timer a upd to %i @ %i", 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; + + if ((mode_old ^ mode_new) & 2) + { + if (mode_old & 2) { + timer_b_offset = timer_b_next_oflow - xcycles; + timer_b_next_oflow = TIMER_NO_OFLOW; + } + else + timer_b_next_oflow = xcycles + timer_b_offset; + } + if (mode_new & 2) + elprintf(EL_YMTIMER, "timer b upd to %i @ %i", timer_b_next_oflow>>8, z80_cycles); +} + +// ym2612 DAC and timer I/O handlers for z80 +int ym2612_write_local(u32 a, u32 d, int is_from_z80) +{ + int addr; + + a &= 3; + 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); + ym2612.dacout = ((int)d - 0x80) << 6; + if (PsndOut && ym2612.dacen && scanline >= PsndDacLine) + PsndDoDAC(scanline); + return 0; + } + + switch (a) + { + case 0: /* address port 0 */ + ym2612.OPN.ST.address = d; + ym2612.addr_A1 = 0; +#ifdef __GP2X__ + if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, -1); +#endif + return 0; + + case 1: /* data port 0 */ + if (ym2612.addr_A1 != 0) + return 0; + + addr = ym2612.OPN.ST.address; + ym2612.REGS[addr] = d; + + switch (addr) + { + case 0x24: // timer A High 8 + case 0x25: { // timer A Low 2 + int TAnew = (addr == 0x24) ? ((ym2612.OPN.ST.TA & 0x03)|(((int)d)<<2)) + : ((ym2612.OPN.ST.TA & 0x3fc)|(d&3)); + if (ym2612.OPN.ST.TA != TAnew) + { + //elprintf(EL_STATUS, "timer a set %i", TAnew); + ym2612.OPN.ST.TA = TAnew; + //ym2612.OPN.ST.TAC = (1024-TAnew)*18; + //ym2612.OPN.ST.TAT = 0; + timer_a_step = timer_a_offset = 16466 * (1024 - TAnew); + if (ym2612.OPN.ST.mode & 1) { + int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone()); + timer_a_next_oflow = (cycles << 8) + timer_a_step; + } + elprintf(EL_YMTIMER, "timer a set to %i, %i", 1024 - TAnew, timer_a_next_oflow>>8); + } + return 0; + } + case 0x26: // timer B + if (ym2612.OPN.ST.TB != d) { + //elprintf(EL_STATUS, "timer b set %i", d); + ym2612.OPN.ST.TB = d; + //ym2612.OPN.ST.TBC = (256-d) * 288; + //ym2612.OPN.ST.TBT = 0; + timer_b_step = timer_b_offset = 262800 * (256 - d); // 262881 + 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; + } + elprintf(EL_YMTIMER, "timer b set to %i, %i", 256 - d, 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()); + ym2612.OPN.ST.mode = d; + + elprintf(EL_YMTIMER, "st mode %02x", d); + ym2612_sync_timers(cycles, old_mode, d); + + /* reset Timer a flag */ + if (d & 0x10) + ym2612.OPN.ST.status &= ~1; + + /* reset Timer b flag */ + if (d & 0x20) + ym2612.OPN.ST.status &= ~2; + + if ((d ^ old_mode) & 0xc0) { +#ifdef __GP2X__ + if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, get_scanline(is_from_z80)); +#endif + return 1; + } + return 0; + } + case 0x2b: { /* DAC Sel (YM2612) */ + int scanline = get_scanline(is_from_z80); + ym2612.dacen = d & 0x80; + if (d & 0x80) PsndDacLine = scanline; +#ifdef __GP2X__ + if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, scanline); +#endif + return 0; + } + } + break; + + case 2: /* address port 1 */ + ym2612.OPN.ST.address = d; + ym2612.addr_A1 = 1; +#ifdef __GP2X__ + if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, -1); +#endif + return 0; + + case 3: /* data port 1 */ + if (ym2612.addr_A1 != 1) + return 0; + + addr = ym2612.OPN.ST.address | 0x100; + ym2612.REGS[addr] = d; + break; + } + +#ifdef __GP2X__ + if (PicoOpt & POPT_EXT_FM) + return YM2612Write_940(a, d, get_scanline(is_from_z80)); +#endif + return YM2612Write_(a, d); +} + + +#define ym2612_read_local() \ + if (xcycles >= timer_a_next_oflow) \ + ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 1; \ + if (xcycles >= timer_b_next_oflow) \ + ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 2 + +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); + return ym2612.OPN.ST.status; +} + +u32 ym2612_read_local_68k(void) +{ + int xcycles = cycles_68k_to_z80(SekCyclesDone()) << 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); + return ym2612.OPN.ST.status; +} + +void ym2612_pack_state(void) +{ + // timers are saved as tick counts, in 16.16 int format + 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); + 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) + YM2612PicoStateSave2_940(tat, tbt); + else +#endif + YM2612PicoStateSave2(tat, tbt); +} + +void ym2612_unpack_state(void) +{ + int i, ret, tac, tat, tbc, tbt; + YM2612PicoStateLoad(); + + // feed all the registers and update internal state + for (i = 0x20; i < 0xA0; i++) { + ym2612_write_local(0, i, 0); + ym2612_write_local(1, ym2612.REGS[i], 0); + } + for (i = 0x30; i < 0xA0; i++) { + ym2612_write_local(2, i, 0); + ym2612_write_local(3, ym2612.REGS[i|0x100], 0); + } + for (i = 0xAF; i >= 0xA0; i--) { // must apply backwards + ym2612_write_local(2, i, 0); + ym2612_write_local(3, ym2612.REGS[i|0x100], 0); + ym2612_write_local(0, i, 0); + ym2612_write_local(1, ym2612.REGS[i], 0); + } + for (i = 0xB0; i < 0xB8; i++) { + ym2612_write_local(0, i, 0); + ym2612_write_local(1, ym2612.REGS[i], 0); + ym2612_write_local(2, i, 0); + ym2612_write_local(3, ym2612.REGS[i|0x100], 0); + } + +#ifdef __GP2X__ + if (PicoOpt & POPT_EXT_FM) + ret = YM2612PicoStateLoad2_940(&tat, &tbt); + else +#endif + ret = YM2612PicoStateLoad2(&tat, &tbt); + if (ret != 0) { + elprintf(EL_STATUS, "old ym2612 state"); + return; // no saved timers + } + + tac = (1024 - ym2612.OPN.ST.TA) << 16; + tbc = (256 - ym2612.OPN.ST.TB) << 16; + if (ym2612.OPN.ST.mode & 1) + timer_a_next_oflow = (double)(tac - tat) / (double)tac * timer_a_step; + else + timer_a_next_oflow = TIMER_NO_OFLOW; + if (ym2612.OPN.ST.mode & 2) + timer_b_next_oflow = (double)(tbc - tbt) / (double)tbc * 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); +} + // ----------------------------------------------------------------- // z80 memhandlers @@ -696,7 +992,7 @@ PICO_INTERNAL unsigned char z80_read(unsigned short a) if ((a>>13)==2) // 0x4000-0x5fff (Charles MacDonald) { - if (PicoOpt&POPT_EN_FM) ret = (u8) YM2612Read(); + if (PicoOpt&POPT_EN_FM) ret = ym2612_read_local_z80(); return ret; } @@ -707,9 +1003,12 @@ PICO_INTERNAL unsigned char z80_read(unsigned short a) addr68k=Pico.m.z80_bank68k<<15; addr68k+=a&0x7fff; + if (addr68k < Pico.romsize) { ret = Pico.rom[addr68k^1]; goto bnkend; } + elprintf(EL_ANOMALY, "z80->68k upper read [%06x] %02x", addr68k, ret); if (PicoAHW & PAHW_MCD) ret = PicoReadM68k8(addr68k); else ret = PicoRead8(addr68k); +bnkend: elprintf(EL_Z80BNK, "z80->68k r8 [%06x] %02x", addr68k, ret); return ret; } @@ -729,7 +1028,7 @@ PICO_INTERNAL_ASM void z80_write(unsigned int a, unsigned char data) { if ((a>>13)==2) // 0x4000-0x5fff (Charles MacDonald) { - if(PicoOpt&POPT_EN_FM) emustatus|=YM2612Write(a, data) & 1; + if(PicoOpt&POPT_EN_FM) emustatus|=ym2612_write_local(a, data, 1) & 1; return; }