Some cleanup of the Random class, and converting several classes

in emucore to use references instead of pointers.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@3017 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2014-10-26 15:54:02 +00:00
parent 1a09d37d1b
commit af33538d5a
20 changed files with 85 additions and 141 deletions

View File

@ -23,7 +23,7 @@
#include "BankRomCheat.hxx" #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) : Cheat(os, name, code)
{ {
if(myCode.length() == 7) 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 // Back up original data; we need this if the cheat is ever disabled
for(int i = 0; i < count; ++i) 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() bool BankRomCheat::disable()
{ {
int oldBank = myOSystem->console().cartridge().getBank(); int oldBank = myOSystem.console().cartridge().getBank();
myOSystem->console().cartridge().bank(bank); myOSystem.console().cartridge().bank(bank);
for(int i = 0; i < count; ++i) 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; return myEnabled = false;
} }
@ -70,13 +70,13 @@ void BankRomCheat::evaluate()
{ {
if(!myEnabled) if(!myEnabled)
{ {
int oldBank = myOSystem->console().cartridge().getBank(); int oldBank = myOSystem.console().cartridge().getBank();
myOSystem->console().cartridge().bank(bank); myOSystem.console().cartridge().bank(bank);
for(int i = 0; i < count; ++i) 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; myEnabled = true;
} }

View File

@ -25,13 +25,12 @@
class BankRomCheat : public Cheat class BankRomCheat : public Cheat
{ {
public: public:
BankRomCheat(OSystem* os, const string& name, const string& code); BankRomCheat(OSystem& os, const string& name, const string& code);
~BankRomCheat(); ~BankRomCheat();
virtual bool enable(); bool enable();
virtual bool disable(); bool disable();
void evaluate();
virtual void evaluate();
private: private:
uInt8 savedRom[16]; uInt8 savedRom[16];

View File

@ -28,7 +28,7 @@ class OSystem;
class Cheat class Cheat
{ {
public: public:
Cheat(OSystem* osystem, const string& name, const string& code) Cheat(OSystem& osystem, const string& name, const string& code)
: myOSystem(osystem), : myOSystem(osystem),
myName(name), myName(name),
myCode(code), myCode(code),
@ -68,7 +68,7 @@ class Cheat
} }
protected: protected:
OSystem* myOSystem; OSystem& myOSystem;
string myName; string myName;
string myCode; string myCode;

View File

@ -31,7 +31,7 @@
#include "CheatManager.hxx" #include "CheatManager.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CheatManager::CheatManager(OSystem* osystem) CheatManager::CheatManager(OSystem& osystem)
: myOSystem(osystem), : myOSystem(osystem),
myCurrentCheat(""), myCurrentCheat(""),
myListIsDirty(false) myListIsDirty(false)
@ -235,7 +235,7 @@ void CheatManager::enable(const string& code, bool enable)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatManager::loadCheatDatabase() void CheatManager::loadCheatDatabase()
{ {
const string& cheatfile = myOSystem->cheatFile(); const string& cheatfile = myOSystem.cheatFile();
ifstream in(cheatfile.c_str(), ios::in); ifstream in(cheatfile.c_str(), ios::in);
if(!in) if(!in)
return; return;
@ -276,7 +276,7 @@ void CheatManager::saveCheatDatabase()
if(!myListIsDirty) if(!myListIsDirty)
return; return;
const string& cheatfile = myOSystem->cheatFile(); const string& cheatfile = myOSystem.cheatFile();
ofstream out(cheatfile.c_str(), ios::out); ofstream out(cheatfile.c_str(), ios::out);
if(!out) if(!out)
return; return;
@ -298,9 +298,9 @@ void CheatManager::loadCheats(const string& md5sum)
// Set up any cheatcodes that was on the command line // Set up any cheatcodes that was on the command line
// (and remove the key from the settings, so they won't get set again) // (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 != "") if(cheats != "")
myOSystem->settings().setValue("cheat", ""); myOSystem.settings().setValue("cheat", "");
CheatCodeMap::iterator iter = myCheatMap.find(md5sum); CheatCodeMap::iterator iter = myCheatMap.find(md5sum);
if(iter == myCheatMap.end() && cheats == "") if(iter == myCheatMap.end() && cheats == "")

View File

@ -42,7 +42,7 @@ typedef map<string,string> CheatCodeMap;
class CheatManager class CheatManager
{ {
public: public:
CheatManager(OSystem* osystem); CheatManager(OSystem& osystem);
~CheatManager(); ~CheatManager();
/** /**
@ -152,7 +152,7 @@ class CheatManager
void clear(); void clear();
private: private:
OSystem* myOSystem; OSystem& myOSystem;
CheatList myCheatList; CheatList myCheatList;
CheatList myPerFrameList; CheatList myPerFrameList;

View File

@ -23,7 +23,7 @@
#include "CheetahCheat.hxx" #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) : Cheat(os, name, code)
{ {
address = 0xf000 + unhex(code.substr(0, 3)); 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 // Back up original data; we need this if the cheat is ever disabled
for(int i = 0; i < count; ++i) 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() bool CheetahCheat::disable()
{ {
for(int i = 0; i < count; ++i) 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; return myEnabled = false;
} }
@ -62,7 +62,7 @@ void CheetahCheat::evaluate()
if(!myEnabled) if(!myEnabled)
{ {
for(int i = 0; i < count; ++i) for(int i = 0; i < count; ++i)
myOSystem->console().cartridge().patch(address + i, value); myOSystem.console().cartridge().patch(address + i, value);
myEnabled = true; myEnabled = true;
} }

View File

@ -25,13 +25,12 @@
class CheetahCheat : public Cheat class CheetahCheat : public Cheat
{ {
public: public:
CheetahCheat(OSystem* os, const string& name, const string& code); CheetahCheat(OSystem& os, const string& name, const string& code);
~CheetahCheat(); ~CheetahCheat();
virtual bool enable(); bool enable();
virtual bool disable(); bool disable();
void evaluate();
virtual void evaluate();
private: private:
uInt8 savedRom[16]; uInt8 savedRom[16];

View File

@ -25,7 +25,7 @@
#include "RamCheat.hxx" #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) : Cheat(os, name, code)
{ {
address = (uInt16) unhex(myCode.substr(0, 2)); address = (uInt16) unhex(myCode.substr(0, 2));
@ -43,7 +43,7 @@ bool RamCheat::enable()
if(!myEnabled) if(!myEnabled)
{ {
myEnabled = true; myEnabled = true;
myOSystem->cheat().addPerFrame(this, myEnabled); myOSystem.cheat().addPerFrame(this, myEnabled);
} }
return myEnabled; return myEnabled;
} }
@ -54,7 +54,7 @@ bool RamCheat::disable()
if(myEnabled) if(myEnabled)
{ {
myEnabled = false; myEnabled = false;
myOSystem->cheat().addPerFrame(this, myEnabled); myOSystem.cheat().addPerFrame(this, myEnabled);
} }
return myEnabled; return myEnabled;
} }
@ -62,5 +62,5 @@ bool RamCheat::disable()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void RamCheat::evaluate() void RamCheat::evaluate()
{ {
myOSystem->console().system().poke(address, value); myOSystem.console().system().poke(address, value);
} }

View File

@ -25,13 +25,12 @@
class RamCheat : public Cheat class RamCheat : public Cheat
{ {
public: public:
RamCheat(OSystem* os, const string& name, const string& code); RamCheat(OSystem& os, const string& name, const string& code);
virtual ~RamCheat(); virtual ~RamCheat();
virtual bool enable(); bool enable();
virtual bool disable(); bool disable();
void evaluate();
virtual void evaluate();
private: private:
uInt16 address; uInt16 address;

View File

@ -87,7 +87,7 @@ Console::Console(OSystem& osystem, Cartridge& cart, const Properties& props)
mySwitches = new Switches(myEvent, myProperties); mySwitches = new Switches(myEvent, myProperties);
// Construct the system and components // Construct the system and components
mySystem = new System(); mySystem = new System(osystem);
// The real controllers for this console will be added later // The real controllers for this console will be added later
// For now, we just add dummy joystick controllers, since autodetection // For now, we just add dummy joystick controllers, since autodetection

View File

@ -116,6 +116,7 @@ OSystem::OSystem()
myBuildInfo = info.str(); myBuildInfo = info.str();
mySettings = MediaFactory::createSettings(*this); mySettings = MediaFactory::createSettings(*this);
myRandom = new Random(*this);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -150,6 +151,7 @@ OSystem::~OSystem()
delete myFrameBuffer; delete myFrameBuffer;
delete mySound; delete mySound;
delete myEventHandler; delete myEventHandler;
delete myRandom;
delete mySettings; delete mySettings;
} }
@ -183,10 +185,10 @@ bool OSystem::create()
myEventHandler->initialize(); myEventHandler->initialize();
// Create a properties set for us to use and set it up // Create a properties set for us to use and set it up
myPropSet = new PropertiesSet(this); myPropSet = new PropertiesSet(propertiesFile());
#ifdef CHEATCODE_SUPPORT #ifdef CHEATCODE_SUPPORT
myCheatManager = new CheatManager(this); myCheatManager = new CheatManager(*this);
myCheatManager->loadCheatDatabase(); myCheatManager->loadCheatDatabase();
#endif #endif
@ -216,7 +218,7 @@ bool OSystem::create()
#endif #endif
// Let the random class know about us; it needs access to getTicks() // Let the random class know about us; it needs access to getTicks()
Random::setSystem(this); myRandom->initSeed();
// Create PNG handler // Create PNG handler
myPNGLib = new PNGLibrary(*myFrameBuffer); myPNGLib = new PNGLibrary(*myFrameBuffer);

View File

@ -29,6 +29,7 @@ class Launcher;
class Menu; class Menu;
class Properties; class Properties;
class PropertiesSet; class PropertiesSet;
class Random;
class SerialPort; class SerialPort;
class Settings; class Settings;
class Sound; class Sound;
@ -108,6 +109,13 @@ class OSystem
*/ */
Settings& settings() const { return *mySettings; } 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. Get the set of game properties for the system.
@ -461,6 +469,9 @@ class OSystem
// Pointer to the Settings object // Pointer to the Settings object
Settings* mySettings; Settings* mySettings;
// Pointer to the Random object
Random* myRandom;
// Pointer to the PropertiesSet object // Pointer to the PropertiesSet object
PropertiesSet* myPropSet; PropertiesSet* myPropSet;

View File

@ -24,17 +24,14 @@
#include "bspf.hxx" #include "bspf.hxx"
#include "DefProps.hxx" #include "DefProps.hxx"
#include "OSystem.hxx"
#include "Props.hxx" #include "Props.hxx"
#include "Settings.hxx"
#include "PropsSet.hxx" #include "PropsSet.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertiesSet::PropertiesSet(OSystem* osystem) PropertiesSet::PropertiesSet(const string& propsfile)
: myOSystem(osystem)
{ {
load(myOSystem->propertiesFile()); load(propsfile);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -42,10 +42,9 @@ class PropertiesSet
{ {
public: public:
/** /**
Create an empty properties set object using the md5 as the Create a properties set object from the specified properties file.
key to the BST.
*/ */
PropertiesSet(OSystem* osystem); PropertiesSet(const string& propsfile);
/** /**
Destructor Destructor
@ -125,9 +124,6 @@ class PropertiesSet
private: private:
typedef map<string, Properties> PropsList; typedef map<string, Properties> PropsList;
// The parent system for this object
OSystem* myOSystem;
// The properties read from an external 'stella.pro' file // The properties read from an external 'stella.pro' file
PropsList myExternalProps; PropsList myExternalProps;

View File

@ -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 <time.h>
#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;

View File

@ -20,9 +20,10 @@
#ifndef RANDOM_HXX #ifndef RANDOM_HXX
#define RANDOM_HXX #define RANDOM_HXX
class OSystem; #include <time.h>
#include "bspf.hxx" #include "bspf.hxx"
#include "OSystem.hxx"
/** /**
This is a quick-and-dirty random number generator. It is based on 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 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, Re-initialize the random number generator with a new seed,
to generate a different set of random numbers. 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 Answer the next random number from the random number generator
@return A random number @return A random number
*/ */
uInt32 next(); uInt32 next()
{
/** return (myValue = (myValue * 2416 + 374441) % 1771875);
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; }
private: private:
// Set the OSystem we're using
const OSystem& myOSystem;
// Indicates the next random number // Indicates the next random number
uInt32 myValue; uInt32 myValue;
// Set the OSystem we're using
static const OSystem* ourSystem;
}; };
#endif #endif

View File

@ -27,8 +27,9 @@
#include "System.hxx" #include "System.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
System::System() System::System(const OSystem& osystem)
: myNumberOfDevices(0), : myOSystem(osystem),
myNumberOfDevices(0),
myM6502(0), myM6502(0),
myTIA(0), myTIA(0),
myCycles(0), myCycles(0),
@ -36,8 +37,8 @@ System::System()
myDataBusLocked(false), myDataBusLocked(false),
mySystemInAutodetect(false) mySystemInAutodetect(false)
{ {
// Create a new random number generator // Re-initialize random generator
myRandom = new Random(); randGenerator().initSeed();
// Allocate page table and dirty list // Allocate page table and dirty list
myPageAccessTable = new PageAccess[NUM_PAGES]; myPageAccessTable = new PageAccess[NUM_PAGES];
@ -70,9 +71,6 @@ System::~System()
// Free my page access table and dirty list // Free my page access table and dirty list
delete[] myPageAccessTable; delete[] myPageAccessTable;
delete[] myPageIsDirtyTable; delete[] myPageIsDirtyTable;
// Free the random number generator
delete myRandom;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -339,6 +337,7 @@ bool System::load(Serializer& in)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
System::System(const System& s) System::System(const System& s)
: myOSystem(s.myOSystem)
{ {
assert(false); assert(false);
} }

View File

@ -52,7 +52,7 @@ class System : public Serializable
Create a new system with an addressing space of 2^13 bytes and Create a new system with an addressing space of 2^13 bytes and
pages of 2^6 bytes. pages of 2^6 bytes.
*/ */
System(); System(const OSystem& osystem);
/** /**
Destructor Destructor
@ -146,7 +146,7 @@ class System : public Serializable
@return The random generator @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 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 // For the pins that are floating, randomly decide which are high or low
// Otherwise, they're specifically driven high // 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"; } string name() const { return "System"; }
private: private:
const OSystem& myOSystem;
// Pointer to a dynamically allocated array of PageAccess structures // Pointer to a dynamically allocated array of PageAccess structures
PageAccess* myPageAccessTable; PageAccess* myPageAccessTable;
@ -423,10 +425,6 @@ class System : public Serializable
// TIA device attached to the system or the null pointer // TIA device attached to the system or the null pointer
TIA* myTIA; 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 // Number of system cycles executed since the last reset
uInt32 myCycles; uInt32 myCycles;

View File

@ -231,14 +231,6 @@ class TIA : public Device
uInt32 clocksThisLine() const uInt32 clocksThisLine() const
{ return ((mySystem->cycles() * 3) - myClockWhenFrameStarted) % 228; } { 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 Answers the total number of scanlines the TIA generated in producing
the current frame buffer. For partial frames, this will be the the current frame buffer. For partial frames, this will be the

View File

@ -64,7 +64,6 @@ MODULE_OBJS := \
src/emucore/Paddles.o \ src/emucore/Paddles.o \
src/emucore/Props.o \ src/emucore/Props.o \
src/emucore/PropsSet.o \ src/emucore/PropsSet.o \
src/emucore/Random.o \
src/emucore/SaveKey.o \ src/emucore/SaveKey.o \
src/emucore/Serializer.o \ src/emucore/Serializer.o \
src/emucore/Settings.o \ src/emucore/Settings.o \