From 3162a7104cbb9c1046a3d780dfc74bbc684bdc5b Mon Sep 17 00:00:00 2001 From: notaz Date: Wed, 4 Oct 2017 02:23:27 +0300 Subject: [PATCH] adjust z80 timing a bit --- pico/memory.c | 18 +++++++++--------- pico/pico.c | 21 ++++++++------------- pico/pico_cmn.c | 3 ++- pico/pico_int.h | 23 +++++++++++++---------- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/pico/memory.c b/pico/memory.c index 858ab65..436f545 100644 --- a/pico/memory.c +++ b/pico/memory.c @@ -331,11 +331,10 @@ NOINLINE void io_ports_write(u32 a, u32 d) Pico.ioports[a] = d; } -// lame.. static int z80_cycles_from_68k(void) { - return z80_cycle_aim - + cycles_68k_to_z80(SekCyclesDone() - last_z80_sync); + int m68k_cnt = SekCyclesDone() - timing.m68c_frame_start; + return cycles_68k_to_z80(m68k_cnt); } void NOINLINE ctl_write_z80busreq(u32 d) @@ -346,7 +345,7 @@ void NOINLINE ctl_write_z80busreq(u32 d) { if (d) { - z80_cycle_cnt = z80_cycles_from_68k(); + timing.z80c_cnt = z80_cycles_from_68k() + 2; } else { @@ -378,7 +377,7 @@ void NOINLINE ctl_write_z80reset(u32 d) } else { - z80_cycle_cnt = z80_cycles_from_68k(); + timing.z80c_cnt = z80_cycles_from_68k() + 2; z80_reset(); } Pico.m.z80_reset = d; @@ -896,10 +895,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 = timing.z80_scanline * 488 * 7; + while (mclk_z80 - mclk_line >= 488 * 7) + timing.z80_scanline++, mclk_line += 488 * 7; + return timing.z80_scanline; } return Pico.m.scanline; diff --git a/pico/pico.c b/pico/pico.c index f0c54d1..0404367 100644 --- a/pico/pico.c +++ b/pico/pico.c @@ -23,6 +23,8 @@ int PicoAutoRgnOrder; struct PicoSRAM SRam; int emustatus; // rapid_ym2612, multi_ym_updates +struct PicoTiming timing; + void (*PicoWriteSound)(int len) = NULL; // called at the best time to send sound buffer (PsndOut) to hardware void (*PicoResetHook)(void) = NULL; void (*PicoLineHook)(void) = NULL; @@ -278,31 +280,24 @@ PICO_INTERNAL int CheckDMA(void) #include "pico_cmn.c" -unsigned int last_z80_sync; /* in 68k cycles */ -int z80_cycle_cnt; -int z80_cycle_aim; -int z80_scanline; -int z80_scanline_cycles; /* cycles done until z80_scanline */ - /* sync z80 to 68k */ PICO_INTERNAL void PicoSyncZ80(unsigned int m68k_cycles_done) { int m68k_cnt; int cnt; - m68k_cnt = m68k_cycles_done - last_z80_sync; - z80_cycle_aim += cycles_68k_to_z80(m68k_cnt); - cnt = z80_cycle_aim - z80_cycle_cnt; - last_z80_sync = m68k_cycles_done; + m68k_cnt = m68k_cycles_done - timing.m68c_frame_start; + timing.z80c_aim = cycles_68k_to_z80(m68k_cnt); + cnt = timing.z80c_aim - timing.z80c_cnt; pprof_start(z80); elprintf(EL_BUSREQ, "z80 sync %i (%u|%u -> %u|%u)", cnt, - z80_cycle_cnt, z80_cycle_cnt / 228, - z80_cycle_aim, z80_cycle_aim / 228); + timing.z80c_cnt, timing.z80c_cnt * 15 / 7 / 488, + timing.z80c_aim, timing.z80c_aim * 15 / 7 / 488); if (cnt > 0) - z80_cycle_cnt += z80_run(cnt); + timing.z80c_cnt += z80_run(cnt); pprof_end(z80); } diff --git a/pico/pico_cmn.c b/pico/pico_cmn.c index b44bfba..01d57a7 100644 --- a/pico/pico_cmn.c +++ b/pico/pico_cmn.c @@ -78,7 +78,6 @@ static int PicoFrameHints(void) int hint; // Hint counter pevt_log_m68k_o(EVT_FRAME_START); - pv->v_counter = Pico.m.scanline = 0; if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled // draw a frame just after vblank in alternative render mode @@ -91,6 +90,8 @@ static int PicoFrameHints(void) } else skip=PicoSkipFrame; + timing.m68c_frame_start = SekCyclesDone(); + pv->v_counter = Pico.m.scanline = 0; z80_resetCycles(); PsndStartFrame(); diff --git a/pico/pico_int.h b/pico/pico_int.h index 743ecb4..89cccc7 100644 --- a/pico/pico_int.h +++ b/pico/pico_int.h @@ -211,20 +211,13 @@ extern struct DrZ80 drZ80; #define Z80_STATE_SIZE 0x60 -extern unsigned int last_z80_sync; -extern int z80_cycle_cnt; /* 'done' z80 cycles before z80_run() */ -extern int z80_cycle_aim; -extern int z80_scanline; -extern int z80_scanline_cycles; /* cycles done until z80_scanline */ - #define z80_resetCycles() \ - last_z80_sync = SekCyclesDone(); \ - z80_cycle_cnt = z80_cycle_aim = z80_scanline = z80_scanline_cycles = 0; + timing.z80c_cnt = timing.z80c_aim = timing.z80_scanline = 0 #define z80_cyclesDone() \ - (z80_cycle_aim - z80_cyclesLeft) + (timing.z80c_aim - z80_cyclesLeft) -#define cycles_68k_to_z80(x) ((x)*957 >> 11) +#define cycles_68k_to_z80(x) ((x) * 3823 >> 13) // ----------------------- SH2 CPU ----------------------- @@ -599,6 +592,15 @@ struct Pico32xMem unsigned short pwm_fifo[2][4]; // [0] - current raw, others - fifo entries }; +struct PicoTiming +{ + unsigned int m68c_frame_start; // m68k cycles + unsigned int z80c_cnt; // z80 cycles done (this frame) + unsigned int z80c_aim; + int z80_scanline; +}; +extern struct PicoTiming timing; + // area.c extern void (*PicoLoadStateHook)(void); @@ -1092,3 +1094,4 @@ void pevt_dump(void); #endif // PICO_INTERNAL_INCLUDED +// vim:shiftwidth=2:ts=2:expandtab -- 2.39.2