From 3fe06faf65533250526c92ad22878aab4629d23f Mon Sep 17 00:00:00 2001 From: tomcw Date: Sat, 12 Feb 2022 18:42:58 +0000 Subject: [PATCH] MB/6522: remove the legacy g_nMBTimerDevice variable. Fix ASSERT from previous commit as SY6522.Reset() needs CriticalSection. --- source/6522.cpp | 15 +-------------- source/CPU.cpp | 20 +++++++++++++++----- source/CPU.h | 1 + source/Mockingboard.cpp | 22 ++++++++++++++++------ 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/source/6522.cpp b/source/6522.cpp index 0d45bed9..587d6e50 100644 --- a/source/6522.cpp +++ b/source/6522.cpp @@ -35,11 +35,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "SynchronousEventManager.h" #include "YamlHelper.h" -// TODO: remove these (just including for now to allow TU to compile) -extern UINT g_nMBTimerDevice; -static const UINT kTIMERDEVICE_INVALID = -1; -static const UINT kAY8910Number = 0; // needed? - void SY6522::Reset(const bool powerCycle) { if (powerCycle) @@ -49,6 +44,7 @@ void SY6522::Reset(const bool powerCycle) // . NB. if it's too small (< ~$0007) then MB detection routines will fail! } + CpuCreateCriticalSection(); // Reset() called by SY6522 global ctor, so explicitly create CPU's CriticalSection Write(rACR, 0x00); // ACR = 0x00: T1 one-shot mode Write(rIFR, 0x7f); // IFR = 0x7F: de-assert any IRQs Write(rIER, 0x7f); // IER = 0x7F: disable all IRQs @@ -64,11 +60,6 @@ void SY6522::Reset(const bool powerCycle) void SY6522::StartTimer1(void) { m_timer1Active = true; - - if (m_regs.IER & IxR_TIMER1) // Using 6522 interrupt - g_nMBTimerDevice = kAY8910Number; - else if (m_regs.ACR & ACR_RM_FREERUNNING) // Polling 6522 IFR (GH#496) - g_nMBTimerDevice = kAY8910Number; } // The assumption was that timer1 was only active if IER.TIMER1=1 @@ -79,13 +70,11 @@ void SY6522::StartTimer1_LoadStateV1(void) return; m_timer1Active = true; - g_nMBTimerDevice = kAY8910Number; } void SY6522::StopTimer1(void) { m_timer1Active = false; - g_nMBTimerDevice = kTIMERDEVICE_INVALID; } //----------------------------------------------------------------------------- @@ -94,8 +83,6 @@ void SY6522::StartTimer2(void) { m_timer2Active = true; - // NB. Can't mimic StartTimer1() as that would stomp on global state - // TODO: Switch to per-device state } void SY6522::StopTimer2(void) diff --git a/source/CPU.cpp b/source/CPU.cpp index 2d1813c5..b4f56d96 100644 --- a/source/CPU.cpp +++ b/source/CPU.cpp @@ -633,6 +633,20 @@ DWORD CpuExecute(const DWORD uCycles, const bool bVideoUpdate) //=========================================================================== +// Called by: +// . CpuInitialize() +// . SY6522.Reset() +void CpuCreateCriticalSection(void) +{ + if (!g_bCritSectionValid) + { + InitializeCriticalSection(&g_CriticalSection); + g_bCritSectionValid = true; + } +} + +//=========================================================================== + // Called from RepeatInitialization(): // 1) FrameCreateWindow() -> WM_CREATE // - done to init g_CriticalSection @@ -645,11 +659,7 @@ void CpuInitialize(bool reset) if (reset) CpuReset(); - if (!g_bCritSectionValid) - { - InitializeCriticalSection(&g_CriticalSection); - g_bCritSectionValid = true; - } + CpuCreateCriticalSection(); CpuIrqReset(); CpuNmiReset(); diff --git a/source/CPU.h b/source/CPU.h index 96bf8705..96c69eb4 100644 --- a/source/CPU.h +++ b/source/CPU.h @@ -32,6 +32,7 @@ void CpuDestroy (); void CpuCalcCycles(ULONG nExecutedCycles); DWORD CpuExecute(const DWORD uCycles, const bool bVideoUpdate); ULONG CpuGetCyclesThisVideoFrame(ULONG nExecutedCycles); +void CpuCreateCriticalSection(void); void CpuInitialize(bool reset); void CpuSetupBenchmark (); void CpuIrqReset(); diff --git a/source/Mockingboard.cpp b/source/Mockingboard.cpp index edac7861..82643999 100644 --- a/source/Mockingboard.cpp +++ b/source/Mockingboard.cpp @@ -148,7 +148,6 @@ static SyncEvent* g_syncEvent[kNumSyncEvents]; // Timer vars static const UINT kTIMERDEVICE_INVALID = -1; -UINT g_nMBTimerDevice = kTIMERDEVICE_INVALID; // SY6522 device# which is generating timer IRQ static UINT64 g_uLastCumulativeCycles = 0; static const DWORD SAMPLE_RATE = 44100; // Use a base freq so that DirectX (or sound h/w) doesn't have to up/down-sample @@ -188,6 +187,16 @@ static int MB_SyncEventCallback(int id, int cycles, ULONG uExecutedCycles); //--------------------------------------------------------------------------- +static bool IsAnyTimer1Active(void) +{ + bool active = false; + for (UINT i = 0; i < NUM_AY8910; i++) + active |= g_MB[i].sy6522.IsTimer1Active(); + return active; +} + +//--------------------------------------------------------------------------- + void MB_Get6522IrqDescription(std::string& desc) { for (UINT i=0; i