diff --git a/src/cheat/BankRomCheat.cxx b/src/cheat/BankRomCheat.cxx index 5958f46ef..a3f1a022a 100644 --- a/src/cheat/BankRomCheat.cxx +++ b/src/cheat/BankRomCheat.cxx @@ -23,7 +23,7 @@ #include "BankRomCheat.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -BankRomCheat::BankRomCheat(OSystem* os, const string& name, const string& code) +BankRomCheat::BankRomCheat(OSystem& os, const string& name, const string& code) : Cheat(os, name, code) { if(myCode.length() == 7) @@ -36,7 +36,7 @@ BankRomCheat::BankRomCheat(OSystem* os, const string& name, const string& code) // Back up original data; we need this if the cheat is ever disabled for(int i = 0; i < count; ++i) - savedRom[i] = myOSystem->console().cartridge().peek(address + i); + savedRom[i] = myOSystem.console().cartridge().peek(address + i); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -54,13 +54,13 @@ bool BankRomCheat::enable() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool BankRomCheat::disable() { - int oldBank = myOSystem->console().cartridge().getBank(); - myOSystem->console().cartridge().bank(bank); + int oldBank = myOSystem.console().cartridge().getBank(); + myOSystem.console().cartridge().bank(bank); for(int i = 0; i < count; ++i) - myOSystem->console().cartridge().patch(address + i, savedRom[i]); + myOSystem.console().cartridge().patch(address + i, savedRom[i]); - myOSystem->console().cartridge().bank(oldBank); + myOSystem.console().cartridge().bank(oldBank); return myEnabled = false; } @@ -70,13 +70,13 @@ void BankRomCheat::evaluate() { if(!myEnabled) { - int oldBank = myOSystem->console().cartridge().getBank(); - myOSystem->console().cartridge().bank(bank); + int oldBank = myOSystem.console().cartridge().getBank(); + myOSystem.console().cartridge().bank(bank); for(int i = 0; i < count; ++i) - myOSystem->console().cartridge().patch(address + i, value); + myOSystem.console().cartridge().patch(address + i, value); - myOSystem->console().cartridge().bank(oldBank); + myOSystem.console().cartridge().bank(oldBank); myEnabled = true; } diff --git a/src/cheat/BankRomCheat.hxx b/src/cheat/BankRomCheat.hxx index 7703b07e9..62487db21 100644 --- a/src/cheat/BankRomCheat.hxx +++ b/src/cheat/BankRomCheat.hxx @@ -25,13 +25,12 @@ class BankRomCheat : public Cheat { public: - BankRomCheat(OSystem* os, const string& name, const string& code); + BankRomCheat(OSystem& os, const string& name, const string& code); ~BankRomCheat(); - virtual bool enable(); - virtual bool disable(); - - virtual void evaluate(); + bool enable(); + bool disable(); + void evaluate(); private: uInt8 savedRom[16]; diff --git a/src/cheat/Cheat.hxx b/src/cheat/Cheat.hxx index 32f12b92f..31eef3cfb 100644 --- a/src/cheat/Cheat.hxx +++ b/src/cheat/Cheat.hxx @@ -28,7 +28,7 @@ class OSystem; class Cheat { public: - Cheat(OSystem* osystem, const string& name, const string& code) + Cheat(OSystem& osystem, const string& name, const string& code) : myOSystem(osystem), myName(name), myCode(code), @@ -68,7 +68,7 @@ class Cheat } protected: - OSystem* myOSystem; + OSystem& myOSystem; string myName; string myCode; diff --git a/src/cheat/CheatManager.cxx b/src/cheat/CheatManager.cxx index a752f9a1c..3a686a4eb 100644 --- a/src/cheat/CheatManager.cxx +++ b/src/cheat/CheatManager.cxx @@ -31,7 +31,7 @@ #include "CheatManager.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -CheatManager::CheatManager(OSystem* osystem) +CheatManager::CheatManager(OSystem& osystem) : myOSystem(osystem), myCurrentCheat(""), myListIsDirty(false) @@ -235,7 +235,7 @@ void CheatManager::enable(const string& code, bool enable) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CheatManager::loadCheatDatabase() { - const string& cheatfile = myOSystem->cheatFile(); + const string& cheatfile = myOSystem.cheatFile(); ifstream in(cheatfile.c_str(), ios::in); if(!in) return; @@ -276,7 +276,7 @@ void CheatManager::saveCheatDatabase() if(!myListIsDirty) return; - const string& cheatfile = myOSystem->cheatFile(); + const string& cheatfile = myOSystem.cheatFile(); ofstream out(cheatfile.c_str(), ios::out); if(!out) return; @@ -298,9 +298,9 @@ void CheatManager::loadCheats(const string& md5sum) // Set up any cheatcodes that was on the command line // (and remove the key from the settings, so they won't get set again) - const string& cheats = myOSystem->settings().getString("cheat"); + const string& cheats = myOSystem.settings().getString("cheat"); if(cheats != "") - myOSystem->settings().setValue("cheat", ""); + myOSystem.settings().setValue("cheat", ""); CheatCodeMap::iterator iter = myCheatMap.find(md5sum); if(iter == myCheatMap.end() && cheats == "") diff --git a/src/cheat/CheatManager.hxx b/src/cheat/CheatManager.hxx index 26406b76a..3ab42a160 100644 --- a/src/cheat/CheatManager.hxx +++ b/src/cheat/CheatManager.hxx @@ -42,7 +42,7 @@ typedef map CheatCodeMap; class CheatManager { public: - CheatManager(OSystem* osystem); + CheatManager(OSystem& osystem); ~CheatManager(); /** @@ -152,7 +152,7 @@ class CheatManager void clear(); private: - OSystem* myOSystem; + OSystem& myOSystem; CheatList myCheatList; CheatList myPerFrameList; diff --git a/src/cheat/CheetahCheat.cxx b/src/cheat/CheetahCheat.cxx index 0fb23de9d..b446fa3f5 100644 --- a/src/cheat/CheetahCheat.cxx +++ b/src/cheat/CheetahCheat.cxx @@ -23,7 +23,7 @@ #include "CheetahCheat.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -CheetahCheat::CheetahCheat(OSystem* os, const string& name, const string& code) +CheetahCheat::CheetahCheat(OSystem& os, const string& name, const string& code) : Cheat(os, name, code) { address = 0xf000 + unhex(code.substr(0, 3)); @@ -32,7 +32,7 @@ CheetahCheat::CheetahCheat(OSystem* os, const string& name, const string& code) // Back up original data; we need this if the cheat is ever disabled for(int i = 0; i < count; ++i) - savedRom[i] = myOSystem->console().cartridge().peek(address + i); + savedRom[i] = myOSystem.console().cartridge().peek(address + i); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -51,7 +51,7 @@ bool CheetahCheat::enable() bool CheetahCheat::disable() { for(int i = 0; i < count; ++i) - myOSystem->console().cartridge().patch(address + i, savedRom[i]); + myOSystem.console().cartridge().patch(address + i, savedRom[i]); return myEnabled = false; } @@ -62,7 +62,7 @@ void CheetahCheat::evaluate() if(!myEnabled) { for(int i = 0; i < count; ++i) - myOSystem->console().cartridge().patch(address + i, value); + myOSystem.console().cartridge().patch(address + i, value); myEnabled = true; } diff --git a/src/cheat/CheetahCheat.hxx b/src/cheat/CheetahCheat.hxx index 90874d6da..2756ee67d 100644 --- a/src/cheat/CheetahCheat.hxx +++ b/src/cheat/CheetahCheat.hxx @@ -25,13 +25,12 @@ class CheetahCheat : public Cheat { public: - CheetahCheat(OSystem* os, const string& name, const string& code); + CheetahCheat(OSystem& os, const string& name, const string& code); ~CheetahCheat(); - virtual bool enable(); - virtual bool disable(); - - virtual void evaluate(); + bool enable(); + bool disable(); + void evaluate(); private: uInt8 savedRom[16]; diff --git a/src/cheat/RamCheat.cxx b/src/cheat/RamCheat.cxx index 675e98a84..1770a1afa 100644 --- a/src/cheat/RamCheat.cxx +++ b/src/cheat/RamCheat.cxx @@ -25,7 +25,7 @@ #include "RamCheat.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -RamCheat::RamCheat(OSystem* os, const string& name, const string& code) +RamCheat::RamCheat(OSystem& os, const string& name, const string& code) : Cheat(os, name, code) { address = (uInt16) unhex(myCode.substr(0, 2)); @@ -43,7 +43,7 @@ bool RamCheat::enable() if(!myEnabled) { myEnabled = true; - myOSystem->cheat().addPerFrame(this, myEnabled); + myOSystem.cheat().addPerFrame(this, myEnabled); } return myEnabled; } @@ -54,7 +54,7 @@ bool RamCheat::disable() if(myEnabled) { myEnabled = false; - myOSystem->cheat().addPerFrame(this, myEnabled); + myOSystem.cheat().addPerFrame(this, myEnabled); } return myEnabled; } @@ -62,5 +62,5 @@ bool RamCheat::disable() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void RamCheat::evaluate() { - myOSystem->console().system().poke(address, value); + myOSystem.console().system().poke(address, value); } diff --git a/src/cheat/RamCheat.hxx b/src/cheat/RamCheat.hxx index 1447febe1..90443427d 100644 --- a/src/cheat/RamCheat.hxx +++ b/src/cheat/RamCheat.hxx @@ -25,13 +25,12 @@ class RamCheat : public Cheat { public: - RamCheat(OSystem* os, const string& name, const string& code); + RamCheat(OSystem& os, const string& name, const string& code); virtual ~RamCheat(); - virtual bool enable(); - virtual bool disable(); - - virtual void evaluate(); + bool enable(); + bool disable(); + void evaluate(); private: uInt16 address; diff --git a/src/emucore/Console.cxx b/src/emucore/Console.cxx index d03a61441..e312b7926 100644 --- a/src/emucore/Console.cxx +++ b/src/emucore/Console.cxx @@ -87,7 +87,7 @@ Console::Console(OSystem& osystem, Cartridge& cart, const Properties& props) mySwitches = new Switches(myEvent, myProperties); // Construct the system and components - mySystem = new System(); + mySystem = new System(osystem); // The real controllers for this console will be added later // For now, we just add dummy joystick controllers, since autodetection diff --git a/src/emucore/OSystem.cxx b/src/emucore/OSystem.cxx index 05f22b732..14ecbe600 100644 --- a/src/emucore/OSystem.cxx +++ b/src/emucore/OSystem.cxx @@ -116,6 +116,7 @@ OSystem::OSystem() myBuildInfo = info.str(); mySettings = MediaFactory::createSettings(*this); + myRandom = new Random(*this); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -150,6 +151,7 @@ OSystem::~OSystem() delete myFrameBuffer; delete mySound; delete myEventHandler; + delete myRandom; delete mySettings; } @@ -183,10 +185,10 @@ bool OSystem::create() myEventHandler->initialize(); // Create a properties set for us to use and set it up - myPropSet = new PropertiesSet(this); + myPropSet = new PropertiesSet(propertiesFile()); #ifdef CHEATCODE_SUPPORT - myCheatManager = new CheatManager(this); + myCheatManager = new CheatManager(*this); myCheatManager->loadCheatDatabase(); #endif @@ -216,7 +218,7 @@ bool OSystem::create() #endif // Let the random class know about us; it needs access to getTicks() - Random::setSystem(this); + myRandom->initSeed(); // Create PNG handler myPNGLib = new PNGLibrary(*myFrameBuffer); diff --git a/src/emucore/OSystem.hxx b/src/emucore/OSystem.hxx index 612ab1cce..82eacffc6 100644 --- a/src/emucore/OSystem.hxx +++ b/src/emucore/OSystem.hxx @@ -29,6 +29,7 @@ class Launcher; class Menu; class Properties; class PropertiesSet; +class Random; class SerialPort; class Settings; class Sound; @@ -108,6 +109,13 @@ class OSystem */ Settings& settings() const { return *mySettings; } + /** + Get the random object of the system. + + @return The random object + */ + Random& random() const { return *myRandom; } + /** Get the set of game properties for the system. @@ -461,6 +469,9 @@ class OSystem // Pointer to the Settings object Settings* mySettings; + // Pointer to the Random object + Random* myRandom; + // Pointer to the PropertiesSet object PropertiesSet* myPropSet; diff --git a/src/emucore/PropsSet.cxx b/src/emucore/PropsSet.cxx index 08a446560..802568abb 100644 --- a/src/emucore/PropsSet.cxx +++ b/src/emucore/PropsSet.cxx @@ -24,17 +24,14 @@ #include "bspf.hxx" #include "DefProps.hxx" -#include "OSystem.hxx" #include "Props.hxx" -#include "Settings.hxx" #include "PropsSet.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -PropertiesSet::PropertiesSet(OSystem* osystem) - : myOSystem(osystem) +PropertiesSet::PropertiesSet(const string& propsfile) { - load(myOSystem->propertiesFile()); + load(propsfile); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/emucore/PropsSet.hxx b/src/emucore/PropsSet.hxx index 570b05d27..4d5db1791 100644 --- a/src/emucore/PropsSet.hxx +++ b/src/emucore/PropsSet.hxx @@ -42,10 +42,9 @@ class PropertiesSet { public: /** - Create an empty properties set object using the md5 as the - key to the BST. + Create a properties set object from the specified properties file. */ - PropertiesSet(OSystem* osystem); + PropertiesSet(const string& propsfile); /** Destructor @@ -125,9 +124,6 @@ class PropertiesSet private: typedef map PropsList; - // The parent system for this object - OSystem* myOSystem; - // The properties read from an external 'stella.pro' file PropsList myExternalProps; diff --git a/src/emucore/Random.cxx b/src/emucore/Random.cxx deleted file mode 100644 index 761977079..000000000 --- a/src/emucore/Random.cxx +++ /dev/null @@ -1,44 +0,0 @@ -//============================================================================ -// -// SSSS tt lll lll -// SS SS tt ll ll -// SS tttttt eeee ll ll aaaa -// SSSS tt ee ee ll ll aa -// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" -// SS SS tt ee ll ll aa aa -// SSSS ttt eeeee llll llll aaaaa -// -// Copyright (c) 1995-2014 by Bradford W. Mott, Stephen Anthony -// and the Stella Team -// -// See the file "License.txt" for information on usage and redistribution of -// this file, and for a DISCLAIMER OF ALL WARRANTIES. -// -// $Id$ -//============================================================================ - -#include - -#include "OSystem.hxx" -#include "Random.hxx" - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Random::Random() -{ - initSeed(); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void Random::initSeed() -{ - myValue = (uInt32) (ourSystem ? ourSystem->getTicks() : time(0)); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -uInt32 Random::next() -{ - return (myValue = (myValue * 2416 + 374441) % 1771875); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -const OSystem* Random::ourSystem = NULL; diff --git a/src/emucore/Random.hxx b/src/emucore/Random.hxx index ccbdd712b..dc9589182 100644 --- a/src/emucore/Random.hxx +++ b/src/emucore/Random.hxx @@ -20,9 +20,10 @@ #ifndef RANDOM_HXX #define RANDOM_HXX -class OSystem; +#include #include "bspf.hxx" +#include "OSystem.hxx" /** This is a quick-and-dirty random number generator. It is based on @@ -38,37 +39,33 @@ class Random /** Create a new random number generator */ - Random(); + Random(const OSystem& osystem) : myOSystem(osystem) { initSeed(); } - public: /** Re-initialize the random number generator with a new seed, to generate a different set of random numbers. */ - void initSeed(); + void initSeed() + { + myValue = (uInt32) myOSystem.getTicks(); + } /** Answer the next random number from the random number generator @return A random number */ - uInt32 next(); - - /** - Class method which sets the OSystem in use; the constructor will - use this to reseed the random number generator every time a new - instance is created - - @param system The system currently in use - */ - static void setSystem(const OSystem* system) { ourSystem = system; } + uInt32 next() + { + return (myValue = (myValue * 2416 + 374441) % 1771875); + } private: + // Set the OSystem we're using + const OSystem& myOSystem; + // Indicates the next random number uInt32 myValue; - - // Set the OSystem we're using - static const OSystem* ourSystem; }; #endif diff --git a/src/emucore/System.cxx b/src/emucore/System.cxx index 77d92a343..477cc0031 100644 --- a/src/emucore/System.cxx +++ b/src/emucore/System.cxx @@ -27,8 +27,9 @@ #include "System.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -System::System() - : myNumberOfDevices(0), +System::System(const OSystem& osystem) + : myOSystem(osystem), + myNumberOfDevices(0), myM6502(0), myTIA(0), myCycles(0), @@ -36,8 +37,8 @@ System::System() myDataBusLocked(false), mySystemInAutodetect(false) { - // Create a new random number generator - myRandom = new Random(); + // Re-initialize random generator + randGenerator().initSeed(); // Allocate page table and dirty list myPageAccessTable = new PageAccess[NUM_PAGES]; @@ -70,9 +71,6 @@ System::~System() // Free my page access table and dirty list delete[] myPageAccessTable; delete[] myPageIsDirtyTable; - - // Free the random number generator - delete myRandom; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -339,6 +337,7 @@ bool System::load(Serializer& in) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - System::System(const System& s) + : myOSystem(s.myOSystem) { assert(false); } diff --git a/src/emucore/System.hxx b/src/emucore/System.hxx index 4f18cc59e..9096d864f 100644 --- a/src/emucore/System.hxx +++ b/src/emucore/System.hxx @@ -52,7 +52,7 @@ class System : public Serializable Create a new system with an addressing space of 2^13 bytes and pages of 2^6 bytes. */ - System(); + System(const OSystem& osystem); /** Destructor @@ -146,7 +146,7 @@ class System : public Serializable @return The random generator */ - Random& randGenerator() const { return *myRandom; } + Random& randGenerator() const { return myOSystem.random(); } /** Get the null device associated with the system. Every system @@ -214,7 +214,7 @@ class System : public Serializable { // For the pins that are floating, randomly decide which are high or low // Otherwise, they're specifically driven high - return (myDataBusState | (myRandom->next() | hmask)) & zmask; + return (myDataBusState | (randGenerator().next() | hmask)) & zmask; } /** @@ -402,6 +402,8 @@ class System : public Serializable string name() const { return "System"; } private: + const OSystem& myOSystem; + // Pointer to a dynamically allocated array of PageAccess structures PageAccess* myPageAccessTable; @@ -423,10 +425,6 @@ class System : public Serializable // TIA device attached to the system or the null pointer TIA* myTIA; - // Many devices need a source of random numbers, usually for emulating - // unknown/undefined behaviour - Random* myRandom; - // Number of system cycles executed since the last reset uInt32 myCycles; diff --git a/src/emucore/TIA.hxx b/src/emucore/TIA.hxx index ec4b31266..485f1be1a 100644 --- a/src/emucore/TIA.hxx +++ b/src/emucore/TIA.hxx @@ -231,14 +231,6 @@ class TIA : public Device uInt32 clocksThisLine() const { return ((mySystem->cycles() * 3) - myClockWhenFrameStarted) % 228; } - /** - Answers the scanline at which the current frame began drawing. - - @return The starting scanline - */ - uInt32 startLine() const - { return myStartScanline; } - /** Answers the total number of scanlines the TIA generated in producing the current frame buffer. For partial frames, this will be the diff --git a/src/emucore/module.mk b/src/emucore/module.mk index 87be717c4..840af82d3 100644 --- a/src/emucore/module.mk +++ b/src/emucore/module.mk @@ -64,7 +64,6 @@ MODULE_OBJS := \ src/emucore/Paddles.o \ src/emucore/Props.o \ src/emucore/PropsSet.o \ - src/emucore/Random.o \ src/emucore/SaveKey.o \ src/emucore/Serializer.o \ src/emucore/Settings.o \