From 9964070b85e4ffb1ee6ec9e28399decec3729d2a Mon Sep 17 00:00:00 2001 From: "Adrian.Conlon" Date: Thu, 24 Aug 2017 11:28:01 +0100 Subject: [PATCH] Refactor to allow peek/poke/reference to share a common implementation as much as possible. Signed-off-by: Adrian.Conlon --- LR35902/inc/Bus.h | 7 +------ LR35902/src/Bus.cpp | 18 +----------------- inc/Memory.h | 19 ++++++++++++------- src/Memory.cpp | 6 ++++-- 4 files changed, 18 insertions(+), 32 deletions(-) diff --git a/LR35902/inc/Bus.h b/LR35902/inc/Bus.h index c215723..2d7ce74 100644 --- a/LR35902/inc/Bus.h +++ b/LR35902/inc/Bus.h @@ -182,11 +182,6 @@ namespace EightBit { void loadBootRom(const std::string& path); void loadGameRom(const std::string& path); - virtual uint8_t peek(uint16_t address); - virtual void poke(uint16_t address, uint8_t value); - - virtual uint8_t& reference(); - private: std::vector m_bootRom; std::vector m_gameRom; @@ -214,6 +209,6 @@ namespace EightBit { void validateCartridgeType(); - uint8_t& reference(uint16_t address, bool& rom); + virtual 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 fc9687a..9d4baab 100644 --- a/LR35902/src/Bus.cpp +++ b/LR35902/src/Bus.cpp @@ -53,22 +53,6 @@ uint8_t& EightBit::Bus::reference(uint16_t address, bool& rom) { 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) { - bool rom; - reference(address, rom) = value; -} - void EightBit::Bus::Bus_WrittenByte(const AddressEventArgs& e) { const auto address = e.getAddress(); @@ -90,7 +74,7 @@ void EightBit::Bus::Bus_WrittenByte(const AddressEventArgs& e) { m_disableBootRom = value != 0; break; case BASE + DIV: - reference() = 0; + Memory::reference() = 0; m_timerCounter = 0; break; } diff --git a/inc/Memory.h b/inc/Memory.h index 70216d7..000d368 100644 --- a/inc/Memory.h +++ b/inc/Memory.h @@ -31,7 +31,6 @@ namespace EightBit { class Memory { public: - Memory(uint16_t addressMask); virtual ~Memory(); @@ -53,10 +52,10 @@ namespace EightBit { return DATA(); } - virtual uint8_t peek(uint16_t address); - virtual void poke(uint16_t address, uint8_t value); + uint8_t peek(uint16_t address); + void poke(uint16_t address, uint8_t value); - virtual uint16_t peekWord(uint16_t address); + uint16_t peekWord(uint16_t address); virtual int effectiveAddress(int address) const { return address & m_addressMask; @@ -120,9 +119,15 @@ namespace EightBit { WrittenByte.fire(AddressEventArgs(ADDRESS().word, DATA())); } - virtual uint8_t& reference() { - auto effective = effectiveAddress(ADDRESS().word); - return m_locked[effective] ? placeDATA(m_bus[effective]) : referenceDATA(m_bus[effective]); + virtual uint8_t& reference(uint16_t address, bool& rom) { + rom = m_locked[address]; + return m_bus[address]; + } + + uint8_t& reference() { + bool rom; + auto& value = reference(ADDRESS().word, rom); + return rom ? placeDATA(value) : referenceDATA(value); } int loadMemory(const std::string& path, uint16_t offset); diff --git a/src/Memory.cpp b/src/Memory.cpp index 3e486f5..ffd2cd6 100644 --- a/src/Memory.cpp +++ b/src/Memory.cpp @@ -20,11 +20,13 @@ EightBit::Memory::~Memory() { } uint8_t EightBit::Memory::peek(uint16_t address) { - return m_bus[address]; + bool rom; + return reference(address, rom); } void EightBit::Memory::poke(uint16_t address, uint8_t value) { - m_bus[address] = value; + bool rom; + reference(address, rom) = value; } uint16_t EightBit::Memory::peekWord(uint16_t address) {