From 448ee2f09f1c1bc67555ff2c8be45e368dea028c Mon Sep 17 00:00:00 2001 From: "Adrian.Conlon" Date: Thu, 24 Aug 2017 10:28:31 +0100 Subject: [PATCH] Refactor the MBC implementation to allow a single point of definition. Signed-off-by: Adrian.Conlon --- LR35902/inc/Bus.h | 4 +++- LR35902/src/Bus.cpp | 36 ++++++++++++++++-------------------- inc/Memory.h | 4 ++-- inc/Processor.h | 2 +- src/Memory.cpp | 4 ++-- 5 files changed, 24 insertions(+), 26 deletions(-) diff --git a/LR35902/inc/Bus.h b/LR35902/inc/Bus.h index 5651afa..c215723 100644 --- a/LR35902/inc/Bus.h +++ b/LR35902/inc/Bus.h @@ -182,7 +182,7 @@ namespace EightBit { void loadBootRom(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 uint8_t& reference(); @@ -213,5 +213,7 @@ namespace EightBit { void checkTimer(int cycles); void validateCartridgeType(); + + uint8_t& reference(uint16_t address, bool& rom); }; } \ No newline at end of file diff --git a/LR35902/src/Bus.cpp b/LR35902/src/Bus.cpp index 4e697d1..fc9687a 100644 --- a/LR35902/src/Bus.cpp +++ b/LR35902/src/Bus.cpp @@ -40,18 +40,8 @@ void EightBit::Bus::loadGameRom(const std::string& path) { validateCartridgeType(); } -uint8_t& EightBit::Bus::reference() { - auto effective = effectiveAddress(ADDRESS().word); - 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 { +uint8_t& EightBit::Bus::reference(uint16_t address, bool& rom) { + rom = true; auto effective = effectiveAddress(address); if ((effective < 0x100) && bootRomEnabled()) return m_bootRom[effective]; @@ -59,18 +49,24 @@ uint8_t EightBit::Bus::peek(uint16_t address) const { return m_gameRom[effective]; if ((effective < 0x8000) && gameRomEnabled()) return m_gameRom[effective + (m_romBank * 0x4000)]; + rom = false; 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) { - auto effective = effectiveAddress(address); - if ((effective < 0x100) && bootRomEnabled()) - 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; + bool rom; + reference(address, rom) = value; } void EightBit::Bus::Bus_WrittenByte(const AddressEventArgs& e) { diff --git a/inc/Memory.h b/inc/Memory.h index 488ed44..70216d7 100644 --- a/inc/Memory.h +++ b/inc/Memory.h @@ -53,10 +53,10 @@ namespace EightBit { 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 uint16_t peekWord(uint16_t address) const; + virtual uint16_t peekWord(uint16_t address); virtual int effectiveAddress(int address) const { return address & m_addressMask; diff --git a/inc/Processor.h b/inc/Processor.h index 1bb556f..9dae3af 100644 --- a/inc/Processor.h +++ b/inc/Processor.h @@ -44,7 +44,7 @@ namespace EightBit { static int promoteNibble(int value) { return value << 4; } 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; } diff --git a/src/Memory.cpp b/src/Memory.cpp index 73d8815..3e486f5 100644 --- a/src/Memory.cpp +++ b/src/Memory.cpp @@ -19,7 +19,7 @@ EightBit::Memory::Memory(uint16_t addressMask) EightBit::Memory::~Memory() { } -uint8_t EightBit::Memory::peek(uint16_t address) const { +uint8_t EightBit::Memory::peek(uint16_t address) { return m_bus[address]; } @@ -27,7 +27,7 @@ void EightBit::Memory::poke(uint16_t address, uint8_t 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; returned.low = peek(address); returned.high = peek(address + 1);