From 53387c4b13ef1f686b498af24deadf9e1f2c9084 Mon Sep 17 00:00:00 2001 From: thrust26 Date: Mon, 6 Apr 2020 09:42:13 +0200 Subject: [PATCH] refactor CartSB --- src/debugger/gui/CartSBWidget.cxx | 5 +- src/emucore/CartSB.cxx | 132 +++++------------------------- src/emucore/CartSB.hxx | 73 ++--------------- 3 files changed, 30 insertions(+), 180 deletions(-) diff --git a/src/debugger/gui/CartSBWidget.cxx b/src/debugger/gui/CartSBWidget.cxx index a7bf7e5b2..78d352879 100644 --- a/src/debugger/gui/CartSBWidget.cxx +++ b/src/debugger/gui/CartSBWidget.cxx @@ -26,10 +26,11 @@ CartridgeSBWidget::CartridgeSBWidget( : CartDebugWidget(boss, lfont, nfont, x, y, w, h), myCart(cart) { - size_t size = myCart.mySize; - VariantList items; ostringstream info, bank; + size_t size; + + myCart.getImage(size); info << "SB SUPERbanking, 32 or 64 4K banks\n" << "Hotspots are from $800 to $" << Common::Base::HEX2 << (0x800 + myCart.bankCount() - 1) << ", including\n" diff --git a/src/emucore/CartSB.cxx b/src/emucore/CartSB.cxx index 44ff9381f..58dd94522 100644 --- a/src/emucore/CartSB.cxx +++ b/src/emucore/CartSB.cxx @@ -21,30 +21,14 @@ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CartridgeSB::CartridgeSB(const ByteBuffer& image, size_t size, const string& md5, const Settings& settings) - : Cartridge(settings, md5), - mySize(size) + : CartridgeEnhanced(image, size, md5, settings) { - // Allocate array for the ROM image - myImage = make_unique(mySize); - - // Copy the ROM image into my buffer - std::copy_n(image.get(), mySize, myImage.get()); - createRomAccessArrays(mySize); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void CartridgeSB::reset() -{ - initializeStartBank(bankCount() - 1); - - // Upon reset we switch to the startup bank - bank(startBank()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CartridgeSB::install(System& system) { - mySystem = &system; + CartridgeEnhanced::install(system); // Get the page accessing methods for the hot spots since they overlap // areas within the TIA we'll need to forward requests to the TIA @@ -62,19 +46,27 @@ void CartridgeSB::install(System& system) // Set the page accessing methods for the hot spots for(uInt16 addr = 0x0800; addr < 0x0FFF; addr += System::PAGE_SIZE) mySystem->setPageAccess(addr, access); - - // Install pages for startup bank - bank(startBank()); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +bool CartridgeSB::checkSwitchBank(uInt16 address, uInt8) +{ + // Switch banks if necessary + if((address & 0x1800) == 0x0800) + { + bank(address & startBank()); + return true; + } + return false; +} + + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - uInt8 CartridgeSB::peek(uInt16 address) { - address &= (0x17FF + (mySize >> 12)); + address &= (0x17FF + bankCount()); - // Switch banks if necessary - if ((address & 0x1800) == 0x0800) - bank(address & startBank()); + checkSwitchBank(address); if(!(address & 0x1000)) { @@ -90,11 +82,9 @@ uInt8 CartridgeSB::peek(uInt16 address) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool CartridgeSB::poke(uInt16 address, uInt8 value) { - address &= (0x17FF + (mySize >> 12)); + address &= (0x17FF + bankCount()); - // Switch banks if necessary - if((address & 0x1800) == 0x0800) - bank(address & startBank()); + checkSwitchBank(address); if(!(address & 0x1000)) { @@ -105,87 +95,3 @@ bool CartridgeSB::poke(uInt16 address, uInt8 value) } return false; } - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool CartridgeSB::bank(uInt16 bank) -{ - if(bankLocked()) return false; - - // Remember what bank we're in - myBankOffset = bank << 12; - - // Setup the page access methods for the current bank - System::PageAccess access(this, System::PageAccessType::READ); - - // Map ROM image into the system - for(uInt16 addr = 0x1000; addr < 0x2000; addr += System::PAGE_SIZE) - { - access.directPeekBase = &myImage[myBankOffset + (addr & 0x0FFF)]; - access.romAccessBase = &myRomAccessBase[myBankOffset + (addr & 0x0FFF)]; - access.romPeekCounter = &myRomAccessCounter[myBankOffset + (addr & 0x0FFF)]; - access.romPokeCounter = &myRomAccessCounter[myBankOffset + (addr & 0x0FFF) + myAccessSize]; - mySystem->setPageAccess(addr, access); - } - return myBankChanged = true; -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -uInt16 CartridgeSB::getBank(uInt16) const -{ - return myBankOffset >> 12; -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -uInt16 CartridgeSB::bankCount() const -{ - return uInt16(mySize >> 12); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool CartridgeSB::patch(uInt16 address, uInt8 value) -{ - myImage[myBankOffset + (address & 0x0FFF)] = value; - return myBankChanged = true; -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -const uInt8* CartridgeSB::getImage(size_t& size) const -{ - size = mySize; - return myImage.get(); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool CartridgeSB::save(Serializer& out) const -{ - try - { - out.putInt(myBankOffset); - } - catch(...) - { - cerr << "ERROR: CartridgeSB::save" << endl; - return false; - } - - return true; -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool CartridgeSB::load(Serializer& in) -{ - try - { - myBankOffset = in.getInt(); - } - catch(...) - { - cerr << "ERROR: CartridgeSB::load" << endl; - return false; - } - - // Remember what bank we were in - bank(myBankOffset >> 12); - - return true; -} diff --git a/src/emucore/CartSB.hxx b/src/emucore/CartSB.hxx index fe8f8b75d..3682dbb04 100644 --- a/src/emucore/CartSB.hxx +++ b/src/emucore/CartSB.hxx @@ -19,7 +19,7 @@ #define CARTRIDGESB_HXX #include "bspf.hxx" -#include "Cart.hxx" +#include "CartEnhanced.hxx" #include "System.hxx" #ifdef DEBUGGER_SUPPORT #include "CartSBWidget.hxx" @@ -31,9 +31,9 @@ (32 banks) and $800 - $83F (64 banks). All mirrors up to $FFF are also used ($900, $A00, ...). - @author Fred X. Quimby + @author Fred X. Quimby, Thomas Jentzsch */ -class CartridgeSB : public Cartridge +class CartridgeSB : public CartridgeEnhanced { friend class CartridgeSBWidget; @@ -51,11 +51,6 @@ class CartridgeSB : public Cartridge virtual ~CartridgeSB() = default; public: - /** - Reset device to its power-on state - */ - void reset() override; - /** Install cartridge in the specified system. Invoked by the system when the cartridge is attached to it. @@ -64,58 +59,6 @@ class CartridgeSB : public Cartridge */ void install(System& system) override; - /** - Install pages for the specified bank in the system. - - @param bank The bank that should be installed in the system - */ - bool bank(uInt16 bank) override; - - /** - Get the current bank. - - @param address The address to use when querying the bank - */ - uInt16 getBank(uInt16 address = 0) const override; - - /** - Query the number of banks supported by the cartridge. - */ - uInt16 bankCount() const override; - - /** - Patch the cartridge ROM. - - @param address The ROM address to patch - @param value The value to place into the address - @return Success or failure of the patch operation - */ - bool patch(uInt16 address, uInt8 value) override; - - /** - Access the internal ROM image for this cartridge. - - @param size Set to the size of the internal ROM image data - @return A pointer to the internal ROM image data - */ - const uInt8* getImage(size_t& size) const override; - - /** - Save the current state of this cart to the given Serializer. - - @param out The Serializer object to use - @return False on any errors, else true - */ - bool save(Serializer& out) const override; - - /** - Load the current state of this cart from the given Serializer. - - @param in The Serializer object to use - @return False on any errors, else true - */ - bool load(Serializer& in) override; - /** Get a descriptor for the device name (used in error checking). @@ -153,13 +96,13 @@ class CartridgeSB : public Cartridge bool poke(uInt16 address, uInt8 value) override; private: - // The 128-256K ROM image and size of the cartridge - ByteBuffer myImage; - size_t mySize{0}; + bool checkSwitchBank(uInt16 address, uInt8 value = 0) override; - // Indicates the offset into the ROM image (aligns to current bank) - uInt32 myBankOffset{0}; + uInt16 hotspot() const override { return 0x0840; } + uInt16 getStartBank() const override { return bankCount() - 1; } + + private: // Previous Device's page access std::array myHotSpotPageAccess;