From c4d04ffe268a406822ef37ead3aac4ba3eda4a44 Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Fri, 8 Sep 2017 18:36:06 -0230 Subject: [PATCH] CartCDF and friends refactoring (have similar functionality): - rework music fetcher code; there can never be a negative cycle count - use get/put double Serializer methods for doubles. --- src/emucore/CartBUS.cxx | 27 ++++++++++++--------------- src/emucore/CartBUS.hxx | 4 ++-- src/emucore/CartCDF.cxx | 20 ++++++++------------ src/emucore/CartCDF.hxx | 2 -- src/emucore/CartCTY.cxx | 29 +++++++++++++---------------- src/emucore/CartCTY.hxx | 4 ++-- src/emucore/CartDPC.cxx | 29 +++++++++-------------------- src/emucore/CartDPC.hxx | 4 ++-- src/emucore/CartDPCPlus.cxx | 34 +++++++++++++++------------------- src/emucore/CartDPCPlus.hxx | 10 +++++----- 10 files changed, 68 insertions(+), 95 deletions(-) diff --git a/src/emucore/CartBUS.cxx b/src/emucore/CartBUS.cxx index 8b89f0d9d..9046c3fd6 100644 --- a/src/emucore/CartBUS.cxx +++ b/src/emucore/CartBUS.cxx @@ -43,7 +43,7 @@ CartridgeBUS::CartridgeBUS(const BytePtr& image, uInt32 size, const Settings& settings) : Cartridge(settings), - mySystemCycles(0), + myAudioCycles(0), myARMCycles(0), myFractionalClocks(0.0) { @@ -77,8 +77,7 @@ void CartridgeBUS::reset() initializeRAM(myBUSRAM+2048, 8192-2048); // Update cycles to the current system cycles - mySystemCycles = mySystem->cycles(); - myARMCycles = mySystem->cycles(); + myAudioCycles = myARMCycles = 0; myFractionalClocks = 0.0; setInitialState(); @@ -135,20 +134,18 @@ void CartridgeBUS::install(System& system) inline void CartridgeBUS::updateMusicModeDataFetchers() { // Calculate the number of cycles since the last update - Int32 cycles = Int32(mySystem->cycles() - mySystemCycles); - mySystemCycles = mySystem->cycles(); + uInt32 cycles = uInt32(mySystem->cycles() - myAudioCycles); + myAudioCycles = mySystem->cycles(); // Calculate the number of BUS OSC clocks since the last update double clocks = ((20000.0 * cycles) / 1193191.66666667) + myFractionalClocks; - Int32 wholeClocks = Int32(clocks); + uInt32 wholeClocks = uInt32(clocks); myFractionalClocks = clocks - double(wholeClocks); - if(wholeClocks <= 0) - return; - // Let's update counters and flags of the music mode data fetchers - for(int x = 0; x <= 2; ++x) - myMusicCounters[x] += myMusicFrequencies[x] * wholeClocks; + if(wholeClocks > 0) + for(int x = 0; x <= 2; ++x) + myMusicCounters[x] += myMusicFrequencies[x] * wholeClocks; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -562,8 +559,8 @@ bool CartridgeBUS::save(Serializer& out) const out.putShort(myJMPoperandAddress); // Save cycles and clocks - out.putLong(mySystemCycles); - out.putInt((uInt32)(myFractionalClocks * 100000000.0)); + out.putLong(myAudioCycles); + out.putDouble(myFractionalClocks); out.putLong(myARMCycles); // Audio info @@ -606,8 +603,8 @@ bool CartridgeBUS::load(Serializer& in) myJMPoperandAddress = in.getShort(); // Get system cycles and fractional clocks - mySystemCycles = in.getLong(); - myFractionalClocks = (double)in.getInt() / 100000000.0; + myAudioCycles = in.getLong(); + myFractionalClocks = in.getDouble(); myARMCycles = in.getLong(); // Audio info diff --git a/src/emucore/CartBUS.hxx b/src/emucore/CartBUS.hxx index ce8dc161a..cfe9503c3 100644 --- a/src/emucore/CartBUS.hxx +++ b/src/emucore/CartBUS.hxx @@ -244,8 +244,8 @@ class CartridgeBUS : public Cartridge // *and* the next two bytes in ROM are 00 00 uInt16 myJMPoperandAddress; - // System cycle count when the last update to music data fetchers occurred - uInt64 mySystemCycles; + // System cycle count from when the last update to music data fetchers occurred + uInt64 myAudioCycles; // ARM cycle count from when the last callFunction() occurred uInt64 myARMCycles; diff --git a/src/emucore/CartCDF.cxx b/src/emucore/CartCDF.cxx index 1c55bf0da..6e9aceb38 100644 --- a/src/emucore/CartCDF.cxx +++ b/src/emucore/CartCDF.cxx @@ -81,9 +81,7 @@ void CartridgeCDF::reset() { initializeRAM(myCDFRAM+2048, 8192-2048); - // Update cycles to the current system cycles - myAudioCycles = mySystem->cycles(); - myARMCycles = mySystem->cycles(); + myAudioCycles = myARMCycles = 0; myFractionalClocks = 0.0; setInitialState(); @@ -137,20 +135,18 @@ void CartridgeCDF::install(System& system) inline void CartridgeCDF::updateMusicModeDataFetchers() { // Calculate the number of cycles since the last update - Int32 cycles = Int32(mySystem->cycles() - myAudioCycles); + uInt32 cycles = uInt32(mySystem->cycles() - myAudioCycles); myAudioCycles = mySystem->cycles(); // Calculate the number of CDF OSC clocks since the last update double clocks = ((20000.0 * cycles) / 1193191.66666667) + myFractionalClocks; - Int32 wholeClocks = Int32(clocks); + uInt32 wholeClocks = uInt32(clocks); myFractionalClocks = clocks - double(wholeClocks); - if(wholeClocks <= 0) - return; - // Let's update counters and flags of the music mode data fetchers - for(int x = 0; x <= 2; ++x) - myMusicCounters[x] += myMusicFrequencies[x] * wholeClocks; + if(wholeClocks > 0) + for(int x = 0; x <= 2; ++x) + myMusicCounters[x] += myMusicFrequencies[x] * wholeClocks; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -512,7 +508,7 @@ bool CartridgeCDF::save(Serializer& out) const // Save cycles and clocks out.putLong(myAudioCycles); - out.putInt((uInt32)(myFractionalClocks * 100000000.0)); + out.putDouble(myFractionalClocks); out.putLong(myARMCycles); } catch(...) @@ -555,7 +551,7 @@ bool CartridgeCDF::load(Serializer& in) // Get cycles and clocks myAudioCycles = in.getLong(); - myFractionalClocks = (double)in.getInt() / 100000000.0; + myFractionalClocks = in.getDouble(); myARMCycles = in.getLong(); } catch(...) diff --git a/src/emucore/CartCDF.hxx b/src/emucore/CartCDF.hxx index bdb08cbd2..34d4e9bf9 100644 --- a/src/emucore/CartCDF.hxx +++ b/src/emucore/CartCDF.hxx @@ -138,8 +138,6 @@ class CartridgeCDF : public Cartridge */ string name() const override { return "CartridgeCDF"; } - // uInt8 busOverdrive(uInt16 address) override; - /** Used for Thumbulator to pass values back to the cartridge */ diff --git a/src/emucore/CartCTY.cxx b/src/emucore/CartCTY.cxx index cf26e2dff..7071dbfae 100644 --- a/src/emucore/CartCTY.cxx +++ b/src/emucore/CartCTY.cxx @@ -31,7 +31,7 @@ CartridgeCTY::CartridgeCTY(const BytePtr& image, uInt32 size, myLDAimmediate(false), myRandomNumber(0x2B435044), myRamAccessTimeout(0), - mySystemCycles(0), + myAudioCycles(0), myFractionalClocks(0.0), myBankOffset(0) { @@ -53,8 +53,7 @@ void CartridgeCTY::reset() myRAM[0] = myRAM[1] = myRAM[2] = myRAM[3] = 0xFF; - // Update cycles to the current system cycles - mySystemCycles = mySystem->cycles(); + myAudioCycles = 0; myFractionalClocks = 0.0; // Upon reset we switch to the startup bank @@ -289,8 +288,8 @@ bool CartridgeCTY::save(Serializer& out) const out.putShort(myCounter); out.putBool(myLDAimmediate); out.putInt(myRandomNumber); - out.putLong(mySystemCycles); - out.putInt(uInt32(myFractionalClocks * 100000000.0)); + out.putLong(myAudioCycles); + out.putDouble(myFractionalClocks); } catch(...) @@ -318,8 +317,8 @@ bool CartridgeCTY::load(Serializer& in) myCounter = in.getShort(); myLDAimmediate = in.getBool(); myRandomNumber = in.getInt(); - mySystemCycles = in.getLong(); - myFractionalClocks = double(in.getInt()) / 100000000.0; + myAudioCycles = in.getLong(); + myFractionalClocks = in.getDouble(); } catch(...) { @@ -506,18 +505,16 @@ void CartridgeCTY::wipeAllScores() inline void CartridgeCTY::updateMusicModeDataFetchers() { // Calculate the number of cycles since the last update - Int32 cycles = Int32(mySystem->cycles() - mySystemCycles); - mySystemCycles = mySystem->cycles(); + uInt32 cycles = uInt32(mySystem->cycles() - myAudioCycles); + myAudioCycles = mySystem->cycles(); - // Calculate the number of DPC OSC clocks since the last update + // Calculate the number of CTY OSC clocks since the last update double clocks = ((20000.0 * cycles) / 1193191.66666667) + myFractionalClocks; - Int32 wholeClocks = Int32(clocks); + uInt32 wholeClocks = uInt32(clocks); myFractionalClocks = clocks - double(wholeClocks); - if(wholeClocks <= 0) - return; - // Let's update counters and flags of the music mode data fetchers - for(int x = 0; x <= 2; ++x) - ; // myMusicCounters[x] += myMusicFrequencies[x] * wholeClocks; + if(wholeClocks > 0) + for(int x = 0; x <= 2; ++x) + ; //myMusicCounters[x] += myMusicFrequencies[x] * wholeClocks; } diff --git a/src/emucore/CartCTY.hxx b/src/emucore/CartCTY.hxx index 1e37a6e62..67b95ee26 100644 --- a/src/emucore/CartCTY.hxx +++ b/src/emucore/CartCTY.hxx @@ -292,8 +292,8 @@ class CartridgeCTY : public Cartridge // of internal RAM to Harmony cart EEPROM string myEEPROMFile; - // System cycle count when the last update to music data fetchers occurred - uInt64 mySystemCycles; + // System cycle count from when the last update to music data fetchers occurred + uInt64 myAudioCycles; // Fractional DPC music OSC clocks unused during the last update double myFractionalClocks; diff --git a/src/emucore/CartDPC.cxx b/src/emucore/CartDPC.cxx index 06b19588c..4c2a5e2a7 100644 --- a/src/emucore/CartDPC.cxx +++ b/src/emucore/CartDPC.cxx @@ -23,7 +23,7 @@ CartridgeDPC::CartridgeDPC(const BytePtr& image, uInt32 size, const Settings& settings) : Cartridge(settings), mySize(size), - mySystemCycles(0), + myAudioCycles(0), myFractionalClocks(0.0), myBankOffset(0) { @@ -54,8 +54,7 @@ CartridgeDPC::CartridgeDPC(const BytePtr& image, uInt32 size, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CartridgeDPC::reset() { - // Update cycles to the current system cycles - mySystemCycles = mySystem->cycles(); + myAudioCycles = 0; myFractionalClocks = 0.0; // Upon reset we switch to the startup bank @@ -98,18 +97,16 @@ inline void CartridgeDPC::clockRandomNumberGenerator() inline void CartridgeDPC::updateMusicModeDataFetchers() { // Calculate the number of cycles since the last update - Int32 cycles = Int32(mySystem->cycles() - mySystemCycles); - mySystemCycles = mySystem->cycles(); + uInt32 cycles = uInt32(mySystem->cycles() - myAudioCycles); + myAudioCycles = mySystem->cycles(); // Calculate the number of DPC OSC clocks since the last update double clocks = ((20000.0 * cycles) / 1193191.66666667) + myFractionalClocks; - Int32 wholeClocks = Int32(clocks); + uInt32 wholeClocks = uInt32(clocks); myFractionalClocks = clocks - double(wholeClocks); if(wholeClocks <= 0) - { return; - } // Let's update counters and flags of the music mode data fetchers for(int x = 5; x <= 7; ++x) @@ -124,24 +121,16 @@ inline void CartridgeDPC::updateMusicModeDataFetchers() { newLow -= (wholeClocks % top); if(newLow < 0) - { newLow += top; - } } else - { newLow = 0; - } // Update flag register for this data fetcher if(newLow <= myBottoms[x]) - { myFlags[x] = 0x00; - } else if(newLow <= myTops[x]) - { myFlags[x] = 0xff; - } myCounters[x] = (myCounters[x] & 0x0700) | uInt16(newLow); } @@ -474,8 +463,8 @@ bool CartridgeDPC::save(Serializer& out) const // The random number generator register out.putByte(myRandomNumber); - out.putLong(mySystemCycles); - out.putInt(uInt32(myFractionalClocks * 100000000.0)); + out.putLong(myAudioCycles); + out.putDouble(myFractionalClocks); } catch(...) { @@ -517,8 +506,8 @@ bool CartridgeDPC::load(Serializer& in) myRandomNumber = in.getByte(); // Get system cycles and fractional clocks - mySystemCycles = in.getLong(); - myFractionalClocks = double(in.getInt()) / 100000000.0; + myAudioCycles = in.getLong(); + myFractionalClocks = in.getDouble(); } catch(...) { diff --git a/src/emucore/CartDPC.hxx b/src/emucore/CartDPC.hxx index 2b4838bc0..e05e5544c 100644 --- a/src/emucore/CartDPC.hxx +++ b/src/emucore/CartDPC.hxx @@ -195,8 +195,8 @@ class CartridgeDPC : public Cartridge // The random number generator register uInt8 myRandomNumber; - // System cycle count when the last update to music data fetchers occurred - uInt64 mySystemCycles; + // System cycle count from when the last update to music data fetchers occurred + uInt64 myAudioCycles; // Fractional DPC music OSC clocks unused during the last update double myFractionalClocks; diff --git a/src/emucore/CartDPCPlus.cxx b/src/emucore/CartDPCPlus.cxx index 9188014c0..4f73227c6 100644 --- a/src/emucore/CartDPCPlus.cxx +++ b/src/emucore/CartDPCPlus.cxx @@ -30,9 +30,9 @@ CartridgeDPCPlus::CartridgeDPCPlus(const BytePtr& image, uInt32 size, myFastFetch(false), myLDAimmediate(false), myParameterPointer(0), - mySystemCycles(0), - myFractionalClocks(0.0), + myAudioCycles(0), myARMCycles(0), + myFractionalClocks(0.0), myBankOffset(0) { // Image is always 32K, but in the case of ROM > 29K, the image is @@ -70,9 +70,7 @@ CartridgeDPCPlus::CartridgeDPCPlus(const BytePtr& image, uInt32 size, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CartridgeDPCPlus::reset() { - // Update cycles to the current system cycles - mySystemCycles = mySystem->cycles(); - myARMCycles = mySystem->cycles(); + myAudioCycles = myARMCycles = 0; myFractionalClocks = 0.0; setInitialState(); @@ -145,20 +143,18 @@ inline void CartridgeDPCPlus::priorClockRandomNumberGenerator() inline void CartridgeDPCPlus::updateMusicModeDataFetchers() { // Calculate the number of cycles since the last update - Int32 cycles = Int32(mySystem->cycles() - mySystemCycles); - mySystemCycles = mySystem->cycles(); + uInt32 cycles = uInt32(mySystem->cycles() - myAudioCycles); + myAudioCycles = mySystem->cycles(); - // Calculate the number of DPC OSC clocks since the last update + // Calculate the number of DPC+ OSC clocks since the last update double clocks = ((20000.0 * cycles) / 1193191.66666667) + myFractionalClocks; - Int32 wholeClocks = Int32(clocks); + uInt32 wholeClocks = uInt32(clocks); myFractionalClocks = clocks - double(wholeClocks); - if(wholeClocks <= 0) - return; - // Let's update counters and flags of the music mode data fetchers - for(int x = 0; x <= 2; ++x) - myMusicCounters[x] += myMusicFrequencies[x] * wholeClocks; + if(wholeClocks > 0) + for(int x = 0; x <= 2; ++x) + myMusicCounters[x] += myMusicFrequencies[x] * wholeClocks; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -682,8 +678,8 @@ bool CartridgeDPCPlus::save(Serializer& out) const out.putInt(myRandomNumber); // Get system cycles and fractional clocks - out.putLong(mySystemCycles); - out.putInt(uInt32(myFractionalClocks * 100000000.0)); + out.putLong(myAudioCycles); + out.putDouble(myFractionalClocks); // Clock info for Thumbulator out.putLong(myARMCycles); @@ -745,9 +741,9 @@ bool CartridgeDPCPlus::load(Serializer& in) // The random number generator register myRandomNumber = in.getInt(); - // Get system cycles and fractional clocks - mySystemCycles = in.getLong(); - myFractionalClocks = double(in.getInt()) / 100000000.0; + // Get audio cycles and fractional clocks + myAudioCycles = in.getLong(); + myFractionalClocks = in.getDouble(); // Clock info for Thumbulator myARMCycles = in.getLong(); diff --git a/src/emucore/CartDPCPlus.hxx b/src/emucore/CartDPCPlus.hxx index 3007c5b8c..37d7ad2fe 100644 --- a/src/emucore/CartDPCPlus.hxx +++ b/src/emucore/CartDPCPlus.hxx @@ -259,15 +259,15 @@ class CartridgeDPCPlus : public Cartridge // The random number generator register uInt32 myRandomNumber; - // System cycle count when the last update to music data fetchers occurred - uInt64 mySystemCycles; - - // Fractional DPC music OSC clocks unused during the last update - double myFractionalClocks; + // System cycle count from when the last update to music data fetchers occurred + uInt64 myAudioCycles; // System cycle count when the last Thumbulator::run() occurred uInt64 myARMCycles; + // Fractional DPC music OSC clocks unused during the last update + double myFractionalClocks; + // Indicates the offset into the ROM image (aligns to current bank) uInt16 myBankOffset;