Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Atari emulator by ~30% #265

Merged
merged 1 commit into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/emucore/Console.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Console::Console(OSystem* osystem, Cartridge* cart, const Properties& props)
mySwitches = new Switches(*myEvent, myProperties);

// Now, we can construct the system and components
mySystem = new System(13, 6);
mySystem = new System();

// Inform the controllers about the system
myControllers[0]->setSystem(mySystem);
Expand Down
64 changes: 3 additions & 61 deletions src/emucore/m6502/src/System.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,13 @@
using namespace std;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
System::System(uInt16 n, uInt16 m)
: myAddressMask((1 << n) - 1),
myPageShift(m),
myPageMask((1 << m) - 1),
myNumberOfPages(1 << (n - m)),
myNumberOfDevices(0),
System::System()
: myNumberOfDevices(0),
myM6502(0),
myTIA(0),
myCycles(0),
myDataBusState(0)
{
// Make sure the arguments are reasonable
assert((1 <= m) && (m <= n) && (n <= 16));

// Allocate page table
myPageAccessTable = new PageAccess[myNumberOfPages];

Expand Down Expand Up @@ -285,12 +278,7 @@ bool System::loadState(const string& md5sum, Deserializer& in)
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
System::System(const System& s)
: myAddressMask(s.myAddressMask),
myPageShift(s.myPageShift),
myPageMask(s.myPageMask),
myNumberOfPages(s.myNumberOfPages)
{
System::System(const System& s) {
assert(false);
}

Expand All @@ -302,52 +290,6 @@ System& System::operator = (const System&)
return *this;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt8 System::peek(uInt16 addr)
{
PageAccess& access = myPageAccessTable[(addr & myAddressMask) >> myPageShift];

uInt8 result;

// See if this page uses direct accessing or not
if(access.directPeekBase != 0)
{
result = *(access.directPeekBase + (addr & myPageMask));
}
else
{
result = access.device->peek(addr);
}

#ifdef DEBUGGER_SUPPORT
if(!myDataBusLocked)
#endif
myDataBusState = result;

return result;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void System::poke(uInt16 addr, uInt8 value)
{
PageAccess& access = myPageAccessTable[(addr & myAddressMask) >> myPageShift];

// See if this page uses direct accessing or not
if(access.directPokeBase != 0)
{
*(access.directPokeBase + (addr & myPageMask)) = value;
}
else
{
access.device->poke(addr, value);
}

#ifdef DEBUGGER_SUPPORT
if(!myDataBusLocked)
#endif
myDataBusState = value;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void System::lockDataBus()
{
Expand Down
72 changes: 56 additions & 16 deletions src/emucore/m6502/src/System.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ class System
{
public:
/**
Create a new system with an addressing space of 2^n bytes and
pages of 2^m bytes.

@param n Log base 2 of the addressing space size
@param m Log base 2 of the page size
Create a new system with an addressing space of 2^13 bytes and
pages of 2^6 bytes.
*/
System(uInt16 n, uInt16 m);
System();

/**
Destructor
Expand Down Expand Up @@ -187,7 +184,7 @@ class System
*/
uInt16 pageShift() const
{
return myPageShift;
return myPageSize;
}

/**
Expand Down Expand Up @@ -246,17 +243,57 @@ class System

@return The byte at the specified address
*/
uInt8 peek(uInt16 address);
inline uInt8 peek(uInt16 addr)
{
PageAccess& access = myPageAccessTable[(addr & myAddressMask) >> myPageSize];

uInt8 result;

// See if this page uses direct accessing or not
if(access.directPeekBase != 0)
{
result = *(access.directPeekBase + (addr & myPageMask));
}
else
{
result = access.device->peek(addr);
}

#ifdef DEBUGGER_SUPPORT
if(!myDataBusLocked)
#endif
myDataBusState = result;

return result;
}

/**
Change the byte at the specified address to the given value.
No masking of the address occurs before it's sent to the device
mapped at the address.

@param address The address where the value should be stored
@param addr The address where the value should be stored
@param value The value to be stored at the address
*/
void poke(uInt16 address, uInt8 value);
inline void poke(uInt16 addr, uInt8 value) {
PageAccess& access = myPageAccessTable[
(addr & myAddressMask) >> myPageSize];

// See if this page uses direct accessing or not
if(access.directPokeBase != 0)
{
*(access.directPokeBase + (addr & myPageMask)) = value;
}
else
{
access.device->poke(addr, value);
}

#ifdef DEBUGGER_SUPPORT
if(!myDataBusLocked)
#endif
myDataBusState = value;
}

/**
Lock/unlock the data bus. When the bus is locked, peek() and
Expand Down Expand Up @@ -316,17 +353,20 @@ class System
const PageAccess& getPageAccess(uInt16 page);

private:
// Mask to apply to an address before accessing memory
const uInt16 myAddressMask;
// Log base 2 of the addressing space size.
static constexpr uInt16 myAddressingSpace = 13;

// Log base 2 of the page size.
static constexpr uInt16 myPageSize = 6;

// Amount to shift an address by to determine what page it's on
const uInt16 myPageShift;
// Mask to apply to an address before accessing memory
static constexpr uInt16 myAddressMask = (1 << myAddressingSpace) - 1;

// Mask to apply to an address to obtain its page offset
const uInt16 myPageMask;
static constexpr uInt16 myPageMask = (1 << myPageSize) - 1;

// Number of pages in the system
const uInt16 myNumberOfPages;
static constexpr uInt16 myNumberOfPages = 1 << (myAddressingSpace - myPageSize);

// Pointer to a dynamically allocated array of PageAccess structures
PageAccess* myPageAccessTable;
Expand Down