Refactor the MBC implementation to allow a single point of definition.

Signed-off-by: Adrian.Conlon <adrian.conlon@gmail.com>
This commit is contained in:
Adrian.Conlon 2017-08-24 10:28:31 +01:00
parent da96454185
commit 448ee2f09f
5 changed files with 24 additions and 26 deletions

View File

@ -182,7 +182,7 @@ namespace EightBit {
void loadBootRom(const std::string& path); void loadBootRom(const std::string& path);
void loadGameRom(const std::string& path); void loadGameRom(const std::string& path);
virtual uint8_t peek(uint16_t address) const; virtual uint8_t peek(uint16_t address);
virtual void poke(uint16_t address, uint8_t value); virtual void poke(uint16_t address, uint8_t value);
virtual uint8_t& reference(); virtual uint8_t& reference();
@ -213,5 +213,7 @@ namespace EightBit {
void checkTimer(int cycles); void checkTimer(int cycles);
void validateCartridgeType(); void validateCartridgeType();
uint8_t& reference(uint16_t address, bool& rom);
}; };
} }

View File

@ -40,18 +40,8 @@ void EightBit::Bus::loadGameRom(const std::string& path) {
validateCartridgeType(); validateCartridgeType();
} }
uint8_t& EightBit::Bus::reference() { uint8_t& EightBit::Bus::reference(uint16_t address, bool& rom) {
auto effective = effectiveAddress(ADDRESS().word); rom = true;
if ((effective < 0x100) && bootRomEnabled())
return placeDATA(m_bootRom[effective]);
if ((effective < 0x4000) && gameRomEnabled())
return placeDATA(m_gameRom[effective]);
if ((effective < 0x8000) && gameRomEnabled())
return placeDATA(m_gameRom[effective + (m_romBank * 0x4000)]);
return Memory::reference();
}
uint8_t EightBit::Bus::peek(uint16_t address) const {
auto effective = effectiveAddress(address); auto effective = effectiveAddress(address);
if ((effective < 0x100) && bootRomEnabled()) if ((effective < 0x100) && bootRomEnabled())
return m_bootRom[effective]; return m_bootRom[effective];
@ -59,18 +49,24 @@ uint8_t EightBit::Bus::peek(uint16_t address) const {
return m_gameRom[effective]; return m_gameRom[effective];
if ((effective < 0x8000) && gameRomEnabled()) if ((effective < 0x8000) && gameRomEnabled())
return m_gameRom[effective + (m_romBank * 0x4000)]; return m_gameRom[effective + (m_romBank * 0x4000)];
rom = false;
return m_bus[effective]; return m_bus[effective];
} }
uint8_t& EightBit::Bus::reference() {
bool rom;
auto& value = reference(ADDRESS().word, rom);
return rom ? placeDATA(value) : referenceDATA(value);
}
uint8_t EightBit::Bus::peek(uint16_t address) {
bool rom;
return reference(address, rom);
}
void EightBit::Bus::poke(uint16_t address, uint8_t value) { void EightBit::Bus::poke(uint16_t address, uint8_t value) {
auto effective = effectiveAddress(address); bool rom;
if ((effective < 0x100) && bootRomEnabled()) reference(address, rom) = value;
m_bootRom[effective] = value;
if ((effective < 0x4000) && gameRomEnabled())
m_gameRom[effective] = value;
if ((effective < 0x8000) && gameRomEnabled())
m_gameRom[effective + (m_romBank * 0x4000)] = value;
m_bus[effective] = value;
} }
void EightBit::Bus::Bus_WrittenByte(const AddressEventArgs& e) { void EightBit::Bus::Bus_WrittenByte(const AddressEventArgs& e) {

View File

@ -53,10 +53,10 @@ namespace EightBit {
return DATA(); return DATA();
} }
virtual uint8_t peek(uint16_t address) const; virtual uint8_t peek(uint16_t address);
virtual void poke(uint16_t address, uint8_t value); virtual void poke(uint16_t address, uint8_t value);
virtual uint16_t peekWord(uint16_t address) const; virtual uint16_t peekWord(uint16_t address);
virtual int effectiveAddress(int address) const { virtual int effectiveAddress(int address) const {
return address & m_addressMask; return address & m_addressMask;

View File

@ -44,7 +44,7 @@ namespace EightBit {
static int promoteNibble(int value) { return value << 4; } static int promoteNibble(int value) { return value << 4; }
static int demoteNibble(int value) { return highNibble(value); } static int demoteNibble(int value) { return highNibble(value); }
const Memory& getMemory() const { return m_memory; } Memory& getMemory() { return m_memory; }
register16_t& PC() { return pc; } register16_t& PC() { return pc; }

View File

@ -19,7 +19,7 @@ EightBit::Memory::Memory(uint16_t addressMask)
EightBit::Memory::~Memory() { EightBit::Memory::~Memory() {
} }
uint8_t EightBit::Memory::peek(uint16_t address) const { uint8_t EightBit::Memory::peek(uint16_t address) {
return m_bus[address]; return m_bus[address];
} }
@ -27,7 +27,7 @@ void EightBit::Memory::poke(uint16_t address, uint8_t value) {
m_bus[address] = value; m_bus[address] = value;
} }
uint16_t EightBit::Memory::peekWord(uint16_t address) const { uint16_t EightBit::Memory::peekWord(uint16_t address) {
register16_t returned; register16_t returned;
returned.low = peek(address); returned.low = peek(address);
returned.high = peek(address + 1); returned.high = peek(address + 1);