refactor CartMDM

make sure the banks are updated when stepping back
This commit is contained in:
thrust26 2020-04-06 09:21:32 +02:00
parent b52251da6e
commit 4390a779af
4 changed files with 31 additions and 118 deletions

View File

@ -27,9 +27,10 @@ CartridgeMDMWidget::CartridgeMDMWidget(
: CartDebugWidget(boss, lfont, nfont, x, y, w, h),
myCart(cart)
{
size_t size = myCart.mySize;
ostringstream info;
size_t size;
myCart.getImage(size);
info << "Menu Driven Megacart, containing up to 128 4K banks\n"
<< "Startup bank = " << cart.startBank() << "\n"
<< "\nBanks are selected by reading from $800 - $BFF, where the lower "

View File

@ -250,5 +250,9 @@ bool CartridgeEnhanced::load(Serializer& in)
cerr << "ERROR: " << name() << "::load" << endl;
return false;
}
// Restore bank sewgments
for(uInt16 i = 0; i < myBankSegs; ++i)
bank(getSegmentBank(i), i);
return true;
}

View File

@ -21,30 +21,14 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CartridgeMDM::CartridgeMDM(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 CartridgeMDM::reset()
{
initializeStartBank(0);
// Upon reset we switch to the startup bank
bank(startBank());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CartridgeMDM::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
@ -61,9 +45,18 @@ void CartridgeMDM::install(System& system)
System::PageAccess access(this, System::PageAccessType::READWRITE);
for(uInt16 addr = 0x0800; addr < 0x0BFF; addr += System::PAGE_SIZE)
mySystem->setPageAccess(addr, access);
}
// Install pages for bank 0
bank(startBank());
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartridgeMDM::checkSwitchBank(uInt16 address, uInt8)
{
// Switch banks if necessary
if((address & 0x1C00) == 0x0800)
{
bank(address & 0x0FF);
return true;
}
return false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -71,8 +64,8 @@ uInt8 CartridgeMDM::peek(uInt16 address)
{
// Because of the way we've set up accessing above, we can only
// get here when the addresses are from 0x800 - 0xBFF
if((address & 0x1C00) == 0x0800)
bank(address & 0x0FF);
checkSwitchBank(address);
int hotspot = ((address & 0x0F00) >> 8) - 8;
return myHotSpotPageAccess[hotspot].device->peek(address);
@ -85,8 +78,7 @@ bool CartridgeMDM::poke(uInt16 address, uInt8 value)
// about those below $1000
if(!(address & 0x1000))
{
if((address & 0x1C00) == 0x0800)
bank(address & 0x0FF);
checkSwitchBank(address);
int hotspot = ((address & 0x0F00) >> 8) - 8;
myHotSpotPageAccess[hotspot].device->poke(address, value);
@ -100,22 +92,7 @@ bool CartridgeMDM::bank(uInt16 bank)
{
if(bankLocked() || myBankingDisabled) return false;
// Remember what bank we're in
// Wrap around to a valid bank number if necessary
myBankOffset = (bank % bankCount()) << 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);
}
CartridgeEnhanced::bank(bank);
// Accesses above bank 127 disable further bankswitching; we're only
// concerned with the lower byte
@ -123,38 +100,12 @@ bool CartridgeMDM::bank(uInt16 bank)
return myBankChanged = true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 CartridgeMDM::getBank(uInt16) const
{
return myBankOffset >> 12;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 CartridgeMDM::bankCount() const
{
return uInt16(mySize >> 12);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartridgeMDM::patch(uInt16 address, uInt8 value)
{
myImage[myBankOffset + (address & 0x0FFF)] = value;
return myBankChanged = true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const uInt8* CartridgeMDM::getImage(size_t& size) const
{
size = mySize;
return myImage.get();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartridgeMDM::save(Serializer& out) const
{
CartridgeEnhanced::save(out);
try
{
out.putInt(myBankOffset);
out.putBool(myBankingDisabled);
}
catch(...)
@ -169,9 +120,9 @@ bool CartridgeMDM::save(Serializer& out) const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartridgeMDM::load(Serializer& in)
{
CartridgeEnhanced::load(in);
try
{
myBankOffset = in.getInt();
myBankingDisabled = in.getBool();
}
catch(...)
@ -180,8 +131,5 @@ bool CartridgeMDM::load(Serializer& in)
return false;
}
// Remember what bank we were in
bank(myBankOffset >> 12);
return true;
}

View File

@ -19,7 +19,7 @@
#define CARTRIDGEMDM_HXX
#include "bspf.hxx"
#include "Cart.hxx"
#include "CartEnhanced.hxx"
#include "System.hxx"
#ifdef DEBUGGER_SUPPORT
#include "CartMDMWidget.hxx"
@ -42,9 +42,9 @@
Therefore, there are 128 banks / 512K possible in total.
@author Stephen Anthony, based on 0840 scheme by Fred X. Quimby
@author Stephen Anthony, Thomas Jentzsch, based on 0840 scheme by Fred X. Quimby
*/
class CartridgeMDM : public Cartridge
class CartridgeMDM : public CartridgeEnhanced
{
friend class CartridgeMDMWidget;
@ -62,11 +62,6 @@ class CartridgeMDM : public Cartridge
virtual ~CartridgeMDM() = 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.
@ -82,35 +77,6 @@ class CartridgeMDM : public Cartridge
*/
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.
@ -164,18 +130,12 @@ class CartridgeMDM : public Cartridge
bool poke(uInt16 address, uInt8 value) override;
private:
// Pointer to a dynamically allocated ROM image of the cartridge
ByteBuffer myImage;
// Size of the ROM image
size_t mySize{0};
bool checkSwitchBank(uInt16 address, uInt8 value = 0) override;
private:
// Previous Device's page access
std::array<System::PageAccess, 8> myHotSpotPageAccess;
// Indicates the offset into the ROM image (aligns to current bank)
uInt32 myBankOffset{0};
// Indicates whether banking has been disabled due to a bankswitch
// above bank 127
bool myBankingDisabled{false};