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"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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;
}

View File

@ -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];

View File

@ -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;

View File

@ -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 == "")

View File

@ -42,7 +42,7 @@ typedef map<string,string> 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;

View File

@ -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;
}

View File

@ -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];

View File

@ -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);
}

View File

@ -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;

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -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<string, Properties> PropsList;
// The parent system for this object
OSystem* myOSystem;
// The properties read from an external 'stella.pro' file
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
#define RANDOM_HXX
class OSystem;
#include <time.h>
#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

View File

@ -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);
}

View File

@ -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;

View File

@ -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

View File

@ -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 \