From cae62cd6756905ee98175699f02ee13a93a3ea3d Mon Sep 17 00:00:00 2001 From: ekeeke Date: Sat, 23 Mar 2019 13:55:44 +0100 Subject: [PATCH] [Core/CD] fixed Main-CPU/Sub-CPU synchronization edge case --- core/cd_hw/scd.c | 14 +++++++++++++- core/mem68k.c | 28 ++++++++++++++++++++++++++-- core/mem68k.h | 2 +- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/core/cd_hw/scd.c b/core/cd_hw/scd.c index 9f1b022..42192c2 100644 --- a/core/cd_hw/scd.c +++ b/core/cd_hw/scd.c @@ -445,10 +445,16 @@ static void s68k_poll_sync(unsigned int reg_mask) /* relative MAIN-CPU cycle counter */ unsigned int cycles = (s68k.cycles * MCYCLES_PER_LINE) / SCYCLES_PER_LINE; - /* sync MAIN-CPU with SUB-CPU */ if (!m68k.stopped) { + /* save current MAIN-CPU end cycle count (recursive execution is possible) */ + int end_cycle = m68k.cycle_end; + + /* sync MAIN-CPU with SUB-CPU */ m68k_run(cycles); + + /* restore MAIN-CPU end cycle count */ + m68k.cycle_end = end_cycle; } /* MAIN-CPU idle on register polling ? */ @@ -669,8 +675,14 @@ static unsigned int scd_read_word(unsigned int address) /* relative MAIN-CPU cycle counter */ unsigned int cycles = (s68k.cycles * MCYCLES_PER_LINE) / SCYCLES_PER_LINE; + /* save current MAIN-CPU end cycle count (recursive execution is possible) */ + int end_cycle = m68k.cycle_end; + /* sync MAIN-CPU with SUB-CPU (Mighty Morphin Power Rangers) */ m68k_run(cycles); + + /* restore MAIN-CPU end cycle count */ + m68k.cycle_end = end_cycle; } s68k_poll_detect(3 << (address & 0x1e)); diff --git a/core/mem68k.c b/core/mem68k.c index 9099e49..23f0d67 100644 --- a/core/mem68k.c +++ b/core/mem68k.c @@ -3,7 +3,7 @@ * Main 68k bus handlers * * Copyright (C) 1998-2003 Charles Mac Donald (original code) - * Copyright (C) 2007-2017 Eke-Eke (Genesis Plus GX) + * Copyright (C) 2007-2019 Eke-Eke (Genesis Plus GX) * * Redistribution and use of this code or any derivative works are permitted * provided that the following conditions are met: @@ -268,10 +268,16 @@ static void m68k_poll_sync(unsigned int reg_mask) /* relative SUB-CPU cycle counter */ unsigned int cycles = (m68k.cycles * SCYCLES_PER_LINE) / MCYCLES_PER_LINE; - /* sync SUB-CPU with MAIN-CPU */ if (!s68k.stopped) { + /* save current SUB-CPU end cycle count (recursive execution is possible) */ + int end_cycle = s68k.cycle_end; + + /* sync SUB-CPU with MAIN-CPU */ s68k_run(cycles); + + /* restore SUB-CPU end cycle count */ + s68k.cycle_end = end_cycle; } /* SUB-CPU idle on register polling ? */ @@ -354,8 +360,14 @@ unsigned int ctrl_io_read_byte(unsigned int address) /* relative SUB-CPU cycle counter */ unsigned int cycles = (m68k.cycles * SCYCLES_PER_LINE) / MCYCLES_PER_LINE; + /* save current SUB-CPU end cycle count (recursive execution is possible) */ + int end_cycle = s68k.cycle_end; + /* sync SUB-CPU with MAIN-CPU (Dracula Unleashed w/ Sega CD Model 2 Boot ROM) */ s68k_run(cycles); + + /* restore SUB-CPU end cycle count */ + s68k.cycle_end = end_cycle; } m68k_poll_detect(1<<0x0f); @@ -527,8 +539,14 @@ unsigned int ctrl_io_read_word(unsigned int address) /* relative SUB-CPU cycle counter */ unsigned int cycles = (m68k.cycles * SCYCLES_PER_LINE) / MCYCLES_PER_LINE; + /* save current SUB-CPU end cycle count (recursive execution is possible) */ + int end_cycle = s68k.cycle_end; + /* sync SUB-CPU with MAIN-CPU (Soul Star) */ s68k_run(cycles); + + /* restore SUB-CPU end cycle count */ + s68k.cycle_end = end_cycle; } m68k_poll_detect(3 << (index - 0x10)); @@ -646,8 +664,14 @@ void ctrl_io_write_byte(unsigned int address, unsigned int data) /* relative SUB-CPU cycle counter */ unsigned int cycles = (m68k.cycles * SCYCLES_PER_LINE) / MCYCLES_PER_LINE; + /* save current SUB-CPU end cycle count (recursive execution is possible) */ + int end_cycle = s68k.cycle_end; + /* sync SUB-CPU with MAIN-CPU (Earnest Evans, Fhey Area) */ s68k_run(cycles); + + /* restore SUB-CPU end cycle count */ + s68k.cycle_end = end_cycle; } /* set IFL2 flag */ diff --git a/core/mem68k.h b/core/mem68k.h index 4148ac9..2d325e9 100644 --- a/core/mem68k.h +++ b/core/mem68k.h @@ -3,7 +3,7 @@ * Main 68k bus handlers * * Copyright (C) 1998-2003 Charles Mac Donald (original code) - * Copyright (C) 2007-2017 Eke-Eke (Genesis Plus GX) + * Copyright (C) 2007-2019 Eke-Eke (Genesis Plus GX) * * Redistribution and use of this code or any derivative works are permitted * provided that the following conditions are met: