diff --git a/src/common/TimerManager.hxx b/src/common/TimerManager.hxx index 631c92076..a11ea2a7c 100644 --- a/src/common/TimerManager.hxx +++ b/src/common/TimerManager.hxx @@ -119,6 +119,22 @@ class TimerManager // Returns lazily initialized singleton static TimerManager& global(); + /** + This method returns number of ticks in microseconds since some + pre-defined time in the past. *NOTE*: it is necessary that this + pre-defined time exists between runs of the application, and must + be (relatively) unique. For example, the time since the system + started running is not a good choice, since it can be duplicated. + The current implementation uses time since the UNIX epoch. + + @return Current time in microseconds. + */ + static uInt64 getTicks() { + using namespace std::chrono; + return duration_cast > > + (system_clock::now().time_since_epoch()).count(); + } + private: using Lock = std::mutex; using ScopedLock = std::unique_lock; diff --git a/src/debugger/DebuggerParser.cxx b/src/debugger/DebuggerParser.cxx index 7f29228ff..b1f92579b 100644 --- a/src/debugger/DebuggerParser.cxx +++ b/src/debugger/DebuggerParser.cxx @@ -34,6 +34,7 @@ #include "RomWidget.hxx" #include "ProgressDialog.hxx" #include "PackedBitArray.hxx" +#include "TimerManager.hxx" #include "Vec.hxx" #include "Base.hxx" @@ -1066,7 +1067,8 @@ void DebuggerParser::executeDump() } else { - file << std::hex << std::setw(8) << std::setfill('0') << uInt32(debugger.myOSystem.getTicks() / 1000); + file << std::hex << std::setw(8) << std::setfill('0') + << uInt32(TimerManager::getTicks() / 1000); } file << ".dump"; FilesystemNode node(file.str()); @@ -1161,7 +1163,8 @@ void DebuggerParser::executeExec() } else { ostringstream prefix; - prefix << std::hex << std::setw(8) << std::setfill('0') << uInt32(debugger.myOSystem.getTicks()/1000); + prefix << std::hex << std::setw(8) << std::setfill('0') + << uInt32(TimerManager::getTicks()/1000); execPrefix = prefix.str(); } ++execDepth; diff --git a/src/debugger/gui/TiaOutputWidget.cxx b/src/debugger/gui/TiaOutputWidget.cxx index 366024ef3..e68940400 100644 --- a/src/debugger/gui/TiaOutputWidget.cxx +++ b/src/debugger/gui/TiaOutputWidget.cxx @@ -30,6 +30,7 @@ #include "TIADebug.hxx" #include "TIASurface.hxx" #include "TIA.hxx" +#include "TimerManager.hxx" #include "TiaOutputWidget.hxx" @@ -70,7 +71,8 @@ void TiaOutputWidget::saveSnapshot(int execDepth, const string& execPrefix) if (execDepth > 0 && !execPrefix.empty()) { sspath << execPrefix << "_"; } - sspath << std::hex << std::setw(8) << std::setfill('0') << uInt32(instance().getTicks()/1000) << ".png"; + sspath << std::hex << std::setw(8) << std::setfill('0') + << uInt32(TimerManager::getTicks()/1000) << ".png"; const uInt32 width = instance().console().tia().width(), height = instance().console().tia().height(); diff --git a/src/emucore/Cart.hxx b/src/emucore/Cart.hxx index b5436708b..2aa768067 100644 --- a/src/emucore/Cart.hxx +++ b/src/emucore/Cart.hxx @@ -216,7 +216,7 @@ class Cartridge : public Device /** Setter for injecting OSystem. */ - virtual void setOSystem(OSystem* osystem) {}; + virtual void setOSystem(OSystem* osystem) {} protected: /** diff --git a/src/emucore/CartCTY.cxx b/src/emucore/CartCTY.cxx index 2cde4f8d5..1abaa3675 100644 --- a/src/emucore/CartCTY.cxx +++ b/src/emucore/CartCTY.cxx @@ -18,6 +18,7 @@ #include "OSystem.hxx" #include "Serializer.hxx" #include "System.hxx" +#include "TimerManager.hxx" #include "CartCTY.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -381,7 +382,7 @@ uInt8 CartridgeCTY::ramReadWrite() if(index < 7) { // Add 0.5 s delay for read - myRamAccessTimeout = myOSystem->getTicks() + 500000; + myRamAccessTimeout = TimerManager::getTicks() + 500000; loadTune(index); } break; @@ -389,7 +390,7 @@ uInt8 CartridgeCTY::ramReadWrite() if(index < 4) { // Add 0.5 s delay for read - myRamAccessTimeout = myOSystem->getTicks() + 500000; + myRamAccessTimeout = TimerManager::getTicks() + 500000; loadScore(index); } break; @@ -397,13 +398,13 @@ uInt8 CartridgeCTY::ramReadWrite() if(index < 4) { // Add 1 s delay for write - myRamAccessTimeout = myOSystem->getTicks() + 1000000; + myRamAccessTimeout = TimerManager::getTicks() + 1000000; saveScore(index); } break; case 4: // Wipe all score tables // Add 1 s delay for write - myRamAccessTimeout = myOSystem->getTicks() + 1000000; + myRamAccessTimeout = TimerManager::getTicks() + 1000000; wipeAllScores(); break; } @@ -413,7 +414,7 @@ uInt8 CartridgeCTY::ramReadWrite() else { // Have we reached the timeout value yet? - if(myOSystem->getTicks() >= myRamAccessTimeout) + if(TimerManager::getTicks() >= myRamAccessTimeout) { myRamAccessTimeout = 0; // Turn off timer myRAM[0] = 0; // Successful operation diff --git a/src/emucore/CartCTY.hxx b/src/emucore/CartCTY.hxx index a8a4313d1..fd11fb5ca 100644 --- a/src/emucore/CartCTY.hxx +++ b/src/emucore/CartCTY.hxx @@ -116,7 +116,7 @@ class CartridgeCTY : public Cartridge @param image Pointer to the ROM image @param size The size of the ROM image @param md5 The md5sum of the ROM image - @param osystem A reference to the OSystem currently in use + @param settings A reference to the settings object */ CartridgeCTY(const BytePtr& image, uInt32 size, const string& md5, const Settings& settings); diff --git a/src/emucore/CartFA2.cxx b/src/emucore/CartFA2.cxx index ac209d559..f7d48307a 100644 --- a/src/emucore/CartFA2.cxx +++ b/src/emucore/CartFA2.cxx @@ -18,6 +18,7 @@ #include "OSystem.hxx" #include "Serializer.hxx" #include "System.hxx" +#include "TimerManager.hxx" #include "CartFA2.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -333,7 +334,7 @@ uInt8 CartridgeFA2::ramReadWrite() if(myRamAccessTimeout == 0) { // Remember when the first access was made - myRamAccessTimeout = myOSystem->getTicks(); + myRamAccessTimeout = TimerManager::getTicks(); // We go ahead and do the access now, and only return when a sufficient // amount of time has passed @@ -372,7 +373,7 @@ uInt8 CartridgeFA2::ramReadWrite() else { // Have we reached the timeout value yet? - if(myOSystem->getTicks() >= myRamAccessTimeout) + if(TimerManager::getTicks() >= myRamAccessTimeout) { myRamAccessTimeout = 0; // Turn off timer myRAM[255] = 0; // Successful operation diff --git a/src/emucore/CartFA2.hxx b/src/emucore/CartFA2.hxx index aac80af13..9ce7ea918 100644 --- a/src/emucore/CartFA2.hxx +++ b/src/emucore/CartFA2.hxx @@ -56,7 +56,7 @@ class CartridgeFA2 : public Cartridge @param image Pointer to the ROM image @param size The size of the ROM image @param md5 The md5sum of the ROM image - @param osystem A reference to the OSystem currently in use + @param settings A reference to the settings object */ CartridgeFA2(const BytePtr& image, uInt32 size, const string& md5, const Settings& settings); diff --git a/src/emucore/OSystem.cxx b/src/emucore/OSystem.cxx index d3844a0de..e9baa7f4f 100644 --- a/src/emucore/OSystem.cxx +++ b/src/emucore/OSystem.cxx @@ -158,7 +158,7 @@ bool OSystem::create() mySerialPort = MediaFactory::createSerialPort(); // Create random number generator - myRandom = make_unique(uInt32(getTicks())); + myRandom = make_unique(uInt32(TimerManager::getTicks())); // Create PNG handler myPNGLib = make_unique(*this); @@ -595,12 +595,6 @@ string OSystem::getROMInfo(const Console& console) return buf.str(); } -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -uInt64 OSystem::getTicks() const -{ - return duration_cast > >(system_clock::now().time_since_epoch()).count(); -} - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - float OSystem::frameRate() const { @@ -692,7 +686,7 @@ void OSystem::mainLoop() { bool wasEmulation = myEventHandler->state() == EventHandlerState::EMULATION; - myEventHandler->poll(getTicks()); + myEventHandler->poll(TimerManager::getTicks()); if(myQuitLoop) break; // Exit if the user wants to quit if (!wasEmulation && myEventHandler->state() == EventHandlerState::EMULATION) { diff --git a/src/emucore/OSystem.hxx b/src/emucore/OSystem.hxx index 5a2d6c99d..04533bfdf 100644 --- a/src/emucore/OSystem.hxx +++ b/src/emucore/OSystem.hxx @@ -392,6 +392,8 @@ class OSystem */ void resetFps(); + float frameRate() const; + /** Attempt to override the base directory that will be used by derived classes, and use this one instead. Note that this is only a hint; @@ -410,20 +412,6 @@ class OSystem // The following methods are system-specific and can be overrided in // derived classes. Otherwise, the base methods will be used. ////////////////////////////////////////////////////////////////////// - /** - This method returns number of ticks in microseconds since some - pre-defined time in the past. *NOTE*: it is necessary that this - pre-defined time exists between runs of the application, and must - be (relatively) unique. For example, the time since the system - started running is not a good choice, since it can be duplicated. - The current implementation uses time since the UNIX epoch. - - @return Current time in microseconds. - */ - virtual uInt64 getTicks() const; - - float frameRate() const; - /** This method runs the main loop. Since different platforms may use different timing methods and/or algorithms, this method can diff --git a/src/emucore/System.cxx b/src/emucore/System.cxx index 3bf711e5c..f17319f3e 100644 --- a/src/emucore/System.cxx +++ b/src/emucore/System.cxx @@ -23,6 +23,7 @@ #include "M6532.hxx" #include "TIA.hxx" #include "Cart.hxx" +#include "TimerManager.hxx" #include "System.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -39,7 +40,7 @@ System::System(const OSystem& osystem, M6502& m6502, M6532& m6532, mySystemInAutodetect(false) { // Re-initialize random generator - randGenerator().initSeed(uInt32(myOSystem.getTicks())); + randGenerator().initSeed(uInt32(TimerManager::getTicks())); // Initialize page access table PageAccess access(&myNullDevice, System::PA_READ); diff --git a/src/gui/ListWidget.cxx b/src/gui/ListWidget.cxx index b7ccadbc4..f7fd609b2 100644 --- a/src/gui/ListWidget.cxx +++ b/src/gui/ListWidget.cxx @@ -24,8 +24,8 @@ #include "Dialog.hxx" #include "FrameBuffer.hxx" #include "StellaKeys.hxx" +#include "TimerManager.hxx" #include "ListWidget.hxx" -#include "bspf.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ListWidget::ListWidget(GuiObject* boss, const GUI::Font& font, @@ -256,7 +256,7 @@ bool ListWidget::handleText(char text) // Only works in a useful fashion if the list entries are sorted. // TODO: Maybe this should be off by default, and instead we add a // method "enableQuickSelect()" or so ? - uInt64 time = instance().getTicks() / 1000; + uInt64 time = TimerManager::getTicks() / 1000; if (_quickSelectTime < time) _quickSelectStr = text; else