refactor CartSB

This commit is contained in:
thrust26 2020-04-06 09:42:13 +02:00
parent 4390a779af
commit 53387c4b13
3 changed files with 30 additions and 180 deletions

View File

@ -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"

View File

@ -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<uInt8[]>(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;
}

View File

@ -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<System::PageAccess, 8> myHotSpotPageAccess;