From 8c019bcd357dd3fc370aac6db5f1addd67188eb1 Mon Sep 17 00:00:00 2001 From: tomcw Date: Sun, 14 Sep 2014 21:23:54 +0100 Subject: [PATCH] More main-loop reduction & removal of redundant globals. Fixed JoyUpdateButtonLatch() to debounce in 5mecs instead of 5secs! SpkrToggle() was using redundant global ('cyclenum') when using the PC speaker. . I reworked the code (as it looked out-of-date), but probably better to just remove all the PC speaker support. --- source/Applewin.cpp | 51 +++++++-------------------------------------- source/Applewin.h | 2 -- source/CPU.cpp | 24 +++++++++++++-------- source/Joystick.cpp | 20 ++++++++++++------ source/Joystick.h | 2 +- source/Speaker.cpp | 9 +++++--- source/Video.cpp | 2 +- 7 files changed, 45 insertions(+), 65 deletions(-) diff --git a/source/Applewin.cpp b/source/Applewin.cpp index da398bff..a4d5ee05 100644 --- a/source/Applewin.cpp +++ b/source/Applewin.cpp @@ -35,6 +35,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "DiskImage.h" #include "Frame.h" #include "Harddisk.h" +#include "Joystick.h" #include "Log.h" #include "Memory.h" #include "Mockingboard.h" @@ -61,8 +62,6 @@ TCHAR *g_pAppTitle = TITLE_APPLE_2E_ENHANCED; eApple2Type g_Apple2Type = A2TYPE_APPLE2EENHANCED; -DWORD cumulativecycles = 0; // Wraps after ~1hr 9mins -DWORD cyclenum = 0; // Used by SpkrToggle() for non-wave sound bool g_bFullSpeed = false; //Pravets 8A/C variables @@ -76,7 +75,6 @@ HINSTANCE g_hInstance = (HINSTANCE)0; AppMode_e g_nAppMode = MODE_LOGO; static bool g_bLoadedSaveState = false; -static int lastmode = MODE_LOGO; TCHAR g_sProgramDir[MAX_PATH] = TEXT(""); // Directory of where AppleWin executable resides TCHAR g_sDebugDir [MAX_PATH] = TEXT(""); // TODO: Not currently used TCHAR g_sScreenShotDir[MAX_PATH] = TEXT(""); // TODO: Not currently used @@ -115,17 +113,6 @@ CSpeech g_Speech; //=========================================================================== -#define DBG_CALC_FREQ 0 -#if DBG_CALC_FREQ -const UINT MAX_CNT = 256; -double g_fDbg[MAX_CNT]; -UINT g_nIdx = 0; -double g_fMeanPeriod,g_fMeanFreq; -ULONGLONG g_nPerfFreq = 0; -#endif - -//--------------------------------------------------------------------------- - bool GetLoadedSaveStateFlag(void) { return g_bLoadedSaveState; @@ -227,15 +214,15 @@ void ContinueExecution(void) if (nCyclesToExecute < 0) nCyclesToExecute = 0; - cyclenum = CpuExecute(nCyclesToExecute); - g_dwCyclesThisFrame += cyclenum; + const DWORD uActualCyclesExecuted = CpuExecute(nCyclesToExecute); + g_dwCyclesThisFrame += uActualCyclesExecuted; - DiskUpdatePosition(cyclenum); - JoyUpdatePosition(); + DiskUpdatePosition(uActualCyclesExecuted); + JoyUpdateButtonLatch(nExecutionPeriodUsec); // Button latch time is independent of CPU clock frequency - SpkrUpdate(cyclenum); - sg_SSC.CommUpdate(cyclenum); - PrintUpdate(cyclenum); + SpkrUpdate(uActualCyclesExecuted); + sg_SSC.CommUpdate(uActualCyclesExecuted); + PrintUpdate(uActualCyclesExecuted); // @@ -249,23 +236,6 @@ void ContinueExecution(void) if (!g_bFullSpeed) { SysClk_WaitTimer(); - -#if DBG_CALC_FREQ - if (g_nPerfFreq) - { - QueryPerformanceCounter((LARGE_INTEGER*)&nTime1); - LONGLONG nTimeDiff = nTime1 - nTime0; - double fTime = (double)nTimeDiff / (double)(LONGLONG)g_nPerfFreq; - - g_fDbg[g_nIdx] = fTime; - g_nIdx = (g_nIdx+1) & (MAX_CNT-1); - g_fMeanPeriod = 0.0; - for(UINT n=0; n> 8) PUSH(regs.pc & 0xFF) EF_TO_AF @@ -369,7 +373,9 @@ static __forceinline void IRQ(ULONG& uExecutedCycles, UINT& uExtraCycles, BOOL& if(g_bmIRQ && !(regs.ps & AF_INTERRUPT)) { // IRQ signals are deasserted when a specific r/w operation is done on device +#ifdef _DEBUG g_nCycleIrqStart = g_nCumulativeCycles + uExecutedCycles; +#endif PUSH(regs.pc >> 8) PUSH(regs.pc & 0xFF) EF_TO_AF diff --git a/source/Joystick.cpp b/source/Joystick.cpp index 3b232378..00141aa5 100644 --- a/source/Joystick.cpp +++ b/source/Joystick.cpp @@ -45,7 +45,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "Configuration\PropertySheet.h" -#define BUTTONTIME 5000 // TODO: Describe this magic number +#define BUTTONTIME 5000 // This is the latch (debounce) time in usecs for the joystick buttons enum {DEVICE_NONE=0, DEVICE_JOYSTICK, DEVICE_KEYBOARD, DEVICE_MOUSE}; @@ -82,7 +82,7 @@ static POINT keyvalue[9] = {{PDL_MIN,PDL_MAX}, {PDL_CENTRAL,PDL_MAX}, {PDL {PDL_MIN,PDL_CENTRAL},{PDL_CENTRAL,PDL_CENTRAL},{PDL_MAX,PDL_CENTRAL}, {PDL_MIN,PDL_MIN}, {PDL_CENTRAL,PDL_MIN}, {PDL_MAX,PDL_MIN}}; -static DWORD buttonlatch[3] = {0,0,0}; +static int buttonlatch[3] = {0,0,0}; static BOOL joybutton[3] = {0,0,0}; static int joyshrx[2] = {8,8}; @@ -703,11 +703,19 @@ void JoySetPosition(int xvalue, int xrange, int yvalue, int yrange) } //=========================================================================== -void JoyUpdatePosition() + +// Update the latch (debounce) time for each button +void JoyUpdateButtonLatch(const UINT nExecutionPeriodUsec) { - if (buttonlatch[0]) --buttonlatch[0]; - if (buttonlatch[1]) --buttonlatch[1]; - if (buttonlatch[2]) --buttonlatch[2]; + for (UINT i=0; i<3; i++) + { + if (buttonlatch[i]) + { + buttonlatch[i] -= nExecutionPeriodUsec; + if (buttonlatch[i] < 0) + buttonlatch[i] = 0; + } + } } //=========================================================================== diff --git a/source/Joystick.h b/source/Joystick.h index 32cfeb72..9889c2c9 100644 --- a/source/Joystick.h +++ b/source/Joystick.h @@ -15,7 +15,7 @@ void JoyReset(); void JoySetButton(eBUTTON,eBUTTONSTATE); BOOL JoySetEmulationType(HWND,DWORD,int, const bool bMousecardActive); void JoySetPosition(int,int,int,int); -void JoyUpdatePosition(); +void JoyUpdateButtonLatch(const UINT nExecutionPeriodUsec); BOOL JoyUsingMouse(); BOOL JoyUsingKeyboard(); BOOL JoyUsingKeyboardCursors(); diff --git a/source/Speaker.cpp b/source/Speaker.cpp index 288c9c60..3608ecd6 100644 --- a/source/Speaker.cpp +++ b/source/Speaker.cpp @@ -479,7 +479,9 @@ BYTE __stdcall SpkrToggle (WORD, WORD, BYTE, BYTE, ULONG nCyclesLeft) if (lastcyclenum) { toggles++; - DWORD delta = cyclenum-lastcyclenum; + //DWORD delta = cyclenum-lastcyclenum; // [TC: 14/09/2014] Looks broken, since 'cyclenum' is cycles executed in previous call to CpuExecute() + CpuCalcCycles(nCyclesLeft); + DWORD delta = (DWORD)g_nCumulativeCycles - lastcyclenum; // DETERMINE WHETHER WE ARE PLAYING A SOUND EFFECT if (directio && @@ -493,7 +495,8 @@ BYTE __stdcall SpkrToggle (WORD, WORD, BYTE, BYTE, ULONG nCyclesLeft) lastdelta[0] = delta; totaldelta += delta; } - lastcyclenum = cyclenum; + //lastcyclenum = cyclenum; + lastcyclenum = (DWORD)g_nCumulativeCycles; } @@ -610,7 +613,7 @@ void SpkrUpdate_Timer() nSamplesUsed = Spkr_SubmitWaveBuffer_FullSpeed(g_pSpeakerBuffer, g_nBufferIdx); _ASSERT(nSamplesUsed <= g_nBufferIdx); - memmove(g_pSpeakerBuffer, &g_pSpeakerBuffer[nSamplesUsed], g_nBufferIdx-nSamplesUsed); // FIXME-TC: _Size * 2 + memmove(g_pSpeakerBuffer, &g_pSpeakerBuffer[nSamplesUsed], g_nBufferIdx-nSamplesUsed); // FIXME-TC: _Size * 2 (GH#213?) g_nBufferIdx -= nSamplesUsed; } } diff --git a/source/Video.cpp b/source/Video.cpp index 0bb423fc..c983ee0a 100644 --- a/source/Video.cpp +++ b/source/Video.cpp @@ -2106,7 +2106,7 @@ void VideoBenchmark () { DWORD executedcycles = CpuExecute(103); cycles -= executedcycles; DiskUpdatePosition(executedcycles); - JoyUpdatePosition(); + JoyUpdateButtonLatch(executedcycles); } } if (cycle & 1)