From e0c4dac19c0ca240f4aec518d036fa5f5276fdb5 Mon Sep 17 00:00:00 2001 From: kub Date: Thu, 22 Jun 2023 22:12:50 +0002 Subject: [PATCH] md, improve z80/68k synchronization --- pico/memory.c | 32 +++++++++++++++++--------------- pico/pico_int.h | 5 +++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/pico/memory.c b/pico/memory.c index 74628290..3267a16a 100644 --- a/pico/memory.c +++ b/pico/memory.c @@ -1334,12 +1334,22 @@ void PicoWrite16_32x(u32 a, u32 d) {} // ----------------------------------------------------------------- // z80 memhandlers +static void access_68k_bus(int delay) // bus delay as Q8 +{ + // 68k bus access delay for z80. The fractional part needs to be accumulated + // until an additional cycle is full. That is then added to the integer part. + Pico.t.z80_busdelay = (delay&0xff) + (Pico.t.z80_busdelay&0xff); // accumulate + z80_subCLeft((delay>>8) + (Pico.t.z80_busdelay>>8)); + // don't use SekCyclesBurn(7) here since the Z80 doesn't run in cycle lock to + // the 68K. Count the stolen cycles to be accounted later in the 68k CPU runs + Pico.t.z80_buscycles += 7; +} + static unsigned char z80_md_vdp_read(unsigned short a) { if ((a & 0xff00) == 0x7f00) { - static int f; f = (f&0xff) + 0x8c; // 0.6 - z80_subCLeft(2+(f>>8)); // 3.3 per kabuto, but notaz' test implies 2.6 ?!? - Pico.t.z80_buscycles += 7; + // 68k bus access delay=3.3 per kabuto, for notaz picotest 2.4<=delay<2.55? + access_68k_bus(0x280); // Q8, picotest: 0x266(>=2.4) - 0x28b(<2.55) switch (a & 0x0d) { @@ -1363,12 +1373,8 @@ static unsigned char z80_md_bank_read(unsigned short a) unsigned int addr68k; unsigned char ret; - // account for 68K bus access on both CPUs. - static int f; f = (f&0xff) + 0x4c; // 0.3 - z80_subCLeft(3+(f>>8)); // 3.3 per kabuto - // don't use SekCyclesBurn(7) here since the Z80 doesn't run in cycle lock to - // the 68K. Count the stolen cycles to be accounted later in the 68k CPU runs - Pico.t.z80_buscycles += 7; + // 68k bus access delay=3.3 per kabuto, but for notaz picotest 3.03.0)-0x34c(<3.3) addr68k = Pico.m.z80_bank68k << 15; addr68k |= a & 0x7fff; @@ -1409,12 +1415,8 @@ static void z80_md_bank_write(unsigned int a, unsigned char data) { unsigned int addr68k; - // account for 68K bus access on both CPUs. - static int f; f = (f&0xff) + 0x4c; // 0.3 - z80_subCLeft(3+(f>>8)); // 3.3 per kabuto - // don't use SekCyclesBurn(7) here since the Z80 doesn't run in cycle lock to - // the 68K. Count the stolen cycles to be accounted later in the 68K CPU runs - Pico.t.z80_buscycles += 7; + // 68k bus access delay=3.3 per kabuto, but for notaz picotest 3.03.0)-0x34c(<3.3) addr68k = Pico.m.z80_bank68k << 15; addr68k += a & 0x7fff; diff --git a/pico/pico_int.h b/pico/pico_int.h index f6618b9d..055f5ee9 100644 --- a/pico/pico_int.h +++ b/pico/pico_int.h @@ -204,8 +204,8 @@ extern struct DrZ80 drZ80; #define z80_cyclesDone() \ (Pico.t.z80c_aim - z80_cyclesLeft) -// one line has 488 68K cycles and 228 Z80 cycles, 228/488*8192=3827 -#define cycles_68k_to_z80(x) ((x) * 3847 >> 13) +// 68k clock = OSC/7, z80 clock = OSC/15, 68k:z80 ratio = 7/15*8192=3822.9 +#define cycles_68k_to_z80(x) ((x) * 3823 >> 13) // ----------------------- SH2 CPU ----------------------- @@ -449,6 +449,7 @@ struct PicoTiming unsigned int z80c_line_start; int z80_scanline; int z80_buscycles; + int z80_busdelay; int timer_a_next_oflow, timer_a_step; // in z80 cycles int timer_b_next_oflow, timer_b_step; -- 2.39.5