From 274dd51a6084a1d485d5efa6c52239434bdd2b08 Mon Sep 17 00:00:00 2001 From: kub Date: Tue, 7 May 2024 23:10:27 +0200 Subject: [PATCH] core z80, cycle counting fixes (reset, bus request) --- pico/cd/mcd.c | 2 +- pico/debug.c | 2 +- pico/memory.c | 12 +++++++----- pico/pico_cmn.c | 4 ++-- pico/pico_int.h | 4 ++-- pico/sms.c | 2 +- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pico/cd/mcd.c b/pico/cd/mcd.c index 2fdbe725..60acd6df 100644 --- a/pico/cd/mcd.c +++ b/pico/cd/mcd.c @@ -312,7 +312,7 @@ static int SekSyncM68k(int once); void pcd_run_cpus_normal(int m68k_cycles) { // TODO this is suspicious. ~1 cycle refresh delay every 256 cycles? - SekAimM68k(m68k_cycles, 0x43); // Fhey area + SekAimM68k(m68k_cycles, 0x42); // Fhey area while (CYCLES_GT(Pico.t.m68c_aim, Pico.t.m68c_cnt)) { if (SekShouldInterrupt()) { diff --git a/pico/debug.c b/pico/debug.c index 5a85a30a..064e97a9 100644 --- a/pico/debug.c +++ b/pico/debug.c @@ -389,7 +389,6 @@ void PDebugZ80Frame(void) else lines = 262; - z80_resetCycles(Pico.t.z80c_aim); PsndStartFrame(); if (/*Pico.m.z80Run &&*/ !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) { @@ -407,6 +406,7 @@ void PDebugZ80Frame(void) PsndGetSamples(lines); timers_cycle(Pico.t.z80c_aim); + z80_resetCycles(); Pico.t.m68c_aim = Pico.t.m68c_cnt; } diff --git a/pico/memory.c b/pico/memory.c index 1bd230d0..0c06acae 100644 --- a/pico/memory.c +++ b/pico/memory.c @@ -530,20 +530,21 @@ void NOINLINE ctl_write_z80busreq(u32 d) { if (d) { - Pico.t.z80c_cnt = z80_cycles_from_68k() + (Pico.t.z80_busdelay >> 8); + Pico.t.z80c_aim = Pico.t.z80c_cnt = z80_cycles_from_68k() + (Pico.t.z80_busdelay >> 8) + 2; Pico.t.z80_busdelay &= 0xff; } else { if ((PicoIn.opt & POPT_EN_Z80) && !Pico.m.z80_reset) { - // Z80 grants bus 2 cycles after the next M cycle, even within an insn + // Z80 grants bus after the current M cycle, even within an insn // simulate this by accumulating the last insn overhang in busdelay - unsigned granted = z80_cycles_from_68k() + 6; + unsigned granted; pprof_start(m68k); PicoSyncZ80(SekCyclesDone()); + pprof_end_sub(m68k); + granted = Pico.t.z80c_aim + 6; // M cycle is 3-6 cycles Pico.t.z80_busdelay += (Pico.t.z80c_cnt - granted) << 8; Pico.t.z80c_cnt = granted; - pprof_end_sub(m68k); } } Pico.m.z80Run = d; @@ -563,12 +564,13 @@ void NOINLINE ctl_write_z80reset(u32 d) PicoSyncZ80(SekCyclesDone()); pprof_end_sub(m68k); } + Pico.t.z80_busdelay &= 0xff; // also resets bus request YM2612ResetChip(); timers_reset(); } else { - Pico.t.z80c_cnt = z80_cycles_from_68k() + 2; + Pico.t.z80c_aim = Pico.t.z80c_cnt = z80_cycles_from_68k() + 2; z80_reset(); } Pico.m.z80_reset = d; diff --git a/pico/pico_cmn.c b/pico/pico_cmn.c index 519aba35..af2e598c 100644 --- a/pico/pico_cmn.c +++ b/pico/pico_cmn.c @@ -81,7 +81,7 @@ static __inline void SekAimM68k(int cyc, int mult) static __inline void SekRunM68k(int cyc) { // TODO 0x100 would be 2 cycles/128, moreover far too sensitive - SekAimM68k(cyc, 0x10c); // OutRunners, testpico, VDPFIFOTesting + SekAimM68k(cyc, 0x108); // OutRunners, testpico, VDPFIFOTesting SekSyncM68k(0); } @@ -141,7 +141,6 @@ static int PicoFrameHints(void) skip = PicoIn.skipFrame; - z80_resetCycles(cycles_68k_to_z80(Pico.t.m68c_aim - Pico.t.m68c_frame_start)); Pico.t.m68c_frame_start = Pico.t.m68c_aim; PsndStartFrame(); @@ -340,6 +339,7 @@ static int PicoFrameHints(void) PsndGetSamples(y); timers_cycle(cycles_68k_to_z80(Pico.t.m68c_aim - Pico.t.m68c_frame_start)); + z80_resetCycles(); pv->hint_cnt = hint; diff --git a/pico/pico_int.h b/pico/pico_int.h index fbdeb873..995f8c16 100644 --- a/pico/pico_int.h +++ b/pico/pico_int.h @@ -201,8 +201,8 @@ extern struct DrZ80 drZ80; #define Z80_STATE_SIZE 0x60 -#define z80_resetCycles(aim) \ - Pico.t.z80c_cnt -= (aim < Pico.t.z80c_cnt ? aim : Pico.t.z80c_cnt), Pico.t.z80c_aim = Pico.t.z80_scanline = 0 +#define z80_resetCycles() \ + Pico.t.z80c_cnt -= Pico.t.z80c_aim, Pico.t.z80c_aim = Pico.t.z80_scanline = 0 #define z80_cyclesDone() \ (Pico.t.z80c_aim - z80_cyclesLeft) diff --git a/pico/sms.c b/pico/sms.c index e954bc8b..c56111c8 100644 --- a/pico/sms.c +++ b/pico/sms.c @@ -846,7 +846,6 @@ void PicoFrameMS(void) int nmi; int y; - z80_resetCycles(Pico.t.z80c_aim); PsndStartFrame(); // for SMS the pause button generates an NMI, for GG ths is not the case @@ -923,6 +922,7 @@ void PicoFrameMS(void) z80_exec(Pico.t.z80c_line_start + cycles_line); } + z80_resetCycles(); PsndGetSamplesMS(lines); } -- 2.39.5