refactor Cart0840

This commit is contained in:
thrust26 2020-04-04 23:04:41 +02:00
parent 7d0c82825e
commit ac4edcfc1e
12 changed files with 45 additions and 211 deletions

View File

@ -21,25 +21,14 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cartridge0840::Cartridge0840(const ByteBuffer& image, size_t size, Cartridge0840::Cartridge0840(const ByteBuffer& image, size_t size,
const string& md5, const Settings& settings) const string& md5, const Settings& settings)
: Cartridge(settings, md5) : CartridgeEnhanced(image, size, md5, settings)
{ {
// Copy the ROM image into my buffer
std::copy_n(image.get(), std::min(myImage.size(), size), myImage.begin());
createRomAccessArrays(myImage.size());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Cartridge0840::reset()
{
// Upon reset we switch to the startup bank
initializeStartBank(0);
bank(startBank());
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Cartridge0840::install(System& system) void Cartridge0840::install(System& system)
{ {
mySystem = &system; CartridgeEnhanced::install(system);
// Get the page accessing methods for the hot spots since they overlap // 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 // areas within the TIA we'll need to forward requests to the TIA
@ -56,32 +45,34 @@ void Cartridge0840::install(System& system)
System::PageAccess access(this, System::PageAccessType::READ); System::PageAccess access(this, System::PageAccessType::READ);
for(uInt16 addr = 0x0800; addr < 0x0FFF; addr += System::PAGE_SIZE) for(uInt16 addr = 0x0800; addr < 0x0FFF; addr += System::PAGE_SIZE)
mySystem->setPageAccess(addr, access); mySystem->setPageAccess(addr, access);
}
// Install pages for bank 0 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bank(startBank()); bool Cartridge0840::checkSwitchBank(uInt16 address, uInt8)
{
// Switch banks if necessary
switch(address & 0x1840)
{
case 0x0800:
// Set the current bank to the lower 4k bank
bank(0);
return true;
case 0x0840:
// Set the current bank to the upper 4k bank
bank(1);
return true;
default:
break;
}
return false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt8 Cartridge0840::peek(uInt16 address) uInt8 Cartridge0840::peek(uInt16 address)
{ {
address &= 0x1840; checkSwitchBank(address);
// Switch banks if necessary
switch(address)
{
case 0x0800:
// Set the current bank to the lower 4k bank
bank(0);
break;
case 0x0840:
// Set the current bank to the upper 4k bank
bank(1);
break;
default:
break;
}
// Because of the way we've set up accessing above, we can only // Because of the way we've set up accessing above, we can only
// get here when the addresses are from 0x800 - 0xFFF // get here when the addresses are from 0x800 - 0xFFF
@ -92,24 +83,7 @@ uInt8 Cartridge0840::peek(uInt16 address)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cartridge0840::poke(uInt16 address, uInt8 value) bool Cartridge0840::poke(uInt16 address, uInt8 value)
{ {
address &= 0x1840; checkSwitchBank(address);
// Switch banks if necessary
switch(address)
{
case 0x0800:
// Set the current bank to the lower 4k bank
bank(0);
break;
case 0x0840:
// Set the current bank to the upper 4k bank
bank(1);
break;
default:
break;
}
// Because of the way accessing is set up, we will may get here by // Because of the way accessing is set up, we will may get here by
// doing a write to 0x800 - 0xFFF or cart; we ignore the cart write // doing a write to 0x800 - 0xFFF or cart; we ignore the cart write
@ -121,85 +95,3 @@ bool Cartridge0840::poke(uInt16 address, uInt8 value)
return false; return false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cartridge0840::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)];
mySystem->setPageAccess(addr, access);
}
return myBankChanged = true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 Cartridge0840::getBank(uInt16) const
{
return myBankOffset >> 12;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 Cartridge0840::bankCount() const
{
return 2;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cartridge0840::patch(uInt16 address, uInt8 value)
{
myImage[myBankOffset + (address & 0x0fff)] = value;
return myBankChanged = true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const uInt8* Cartridge0840::getImage(size_t& size) const
{
size = myImage.size();
return myImage.data();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cartridge0840::save(Serializer& out) const
{
try
{
out.putShort(myBankOffset);
}
catch(...)
{
cerr << "ERROR: Cartridge0840::save" << endl;
return false;
}
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cartridge0840::load(Serializer& in)
{
try
{
myBankOffset = in.getShort();
}
catch(...)
{
cerr << "ERROR: Cartridge0840::load" << endl;
return false;
}
// Remember what bank we were in
bank(myBankOffset);
return true;
}

View File

@ -19,7 +19,7 @@
#define CARTRIDGE0840_HXX #define CARTRIDGE0840_HXX
#include "bspf.hxx" #include "bspf.hxx"
#include "Cart.hxx" #include "CartEnhanced.hxx"
#include "System.hxx" #include "System.hxx"
#ifdef DEBUGGER_SUPPORT #ifdef DEBUGGER_SUPPORT
#include "Cart0840Widget.hxx" #include "Cart0840Widget.hxx"
@ -30,9 +30,9 @@
are two 4K banks, which are switched by accessing $0800 (bank 0) and are two 4K banks, which are switched by accessing $0800 (bank 0) and
$0840 (bank 1). $0840 (bank 1).
@author Fred X. Quimby @author Fred X. Quimby, Thomas Jentzsch
*/ */
class Cartridge0840 : public Cartridge class Cartridge0840 : public CartridgeEnhanced
{ {
friend class Cartridge0840Widget; friend class Cartridge0840Widget;
@ -50,11 +50,6 @@ class Cartridge0840 : public Cartridge
virtual ~Cartridge0840() = default; virtual ~Cartridge0840() = default;
public: public:
/**
Reset device to its power-on state
*/
void reset() override;
/** /**
Install cartridge in the specified system. Invoked by the system Install cartridge in the specified system. Invoked by the system
when the cartridge is attached to it. when the cartridge is attached to it.
@ -63,58 +58,6 @@ class Cartridge0840 : public Cartridge
*/ */
void install(System& system) override; 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). Get a descriptor for the device name (used in error checking).
@ -152,12 +95,11 @@ class Cartridge0840 : public Cartridge
bool poke(uInt16 address, uInt8 value) override; bool poke(uInt16 address, uInt8 value) override;
private: private:
// The 8K ROM image of the cartridge bool checkSwitchBank(uInt16 address, uInt8 value = 0) override;
std::array<uInt8, 8_KB> myImage;
// Indicates the offset into the ROM image (aligns to current bank) uInt16 hotspot() const override { return 0x0840; }
uInt16 myBankOffset{0};
private:
// Previous Device's page access // Previous Device's page access
std::array<System::PageAccess, 8> myHotSpotPageAccess; std::array<System::PageAccess, 8> myHotSpotPageAccess;

View File

@ -73,7 +73,7 @@ class CartridgeBF : public CartridgeEnhanced
private: private:
bool checkSwitchBank(uInt16 address, uInt8 value = 0) override; bool checkSwitchBank(uInt16 address, uInt8 value = 0) override;
uInt16 romHotspot() const override { return 0x1F80; } uInt16 hotspot() const override { return 0x1F80; }
uInt16 getStartBank() const override { return 1; } uInt16 getStartBank() const override { return 1; }

View File

@ -73,7 +73,7 @@ class CartridgeDF : public CartridgeEnhanced
private: private:
bool checkSwitchBank(uInt16 address, uInt8 value = 0) override; bool checkSwitchBank(uInt16 address, uInt8 value = 0) override;
uInt16 romHotspot() const override { return 0x1FC0; } uInt16 hotspot() const override { return 0x1FC0; }
uInt16 getStartBank() const override { return 15; } uInt16 getStartBank() const override { return 15; }

View File

@ -86,7 +86,7 @@ class CartridgeE0 : public CartridgeEnhanced
private: private:
bool checkSwitchBank(uInt16 address, uInt8 = 0) override; bool checkSwitchBank(uInt16 address, uInt8 = 0) override;
uInt16 romHotspot() const override { return 0x1FE0; } uInt16 hotspot() const override { return 0x1FE0; }
private: private:
// log(ROM bank segment size) / log(2) // log(ROM bank segment size) / log(2)

View File

@ -73,7 +73,7 @@ class CartridgeEF : public CartridgeEnhanced
private: private:
bool checkSwitchBank(uInt16 address, uInt8 value = 0) override; bool checkSwitchBank(uInt16 address, uInt8 value = 0) override;
uInt16 romHotspot() const override { return 0x1FE0; } uInt16 hotspot() const override { return 0x1FE0; }
uInt16 getStartBank() const override { return 1; } uInt16 getStartBank() const override { return 1; }

View File

@ -102,7 +102,7 @@ uInt8 CartridgeEnhanced::peek(uInt16 address)
{ {
uInt16 peekAddress = address; uInt16 peekAddress = address;
if (romHotspot()) if (hotspot())
checkSwitchBank(address & 0x0FFF); checkSwitchBank(address & 0x0FFF);
address &= myBankMask; address &= myBankMask;
@ -146,13 +146,13 @@ bool CartridgeEnhanced::bank(uInt16 bank, uInt16 segment)
// Remember what bank is in which segment // Remember what bank is in which segment
uInt32 bankOffset = myCurrentSegOffset[segment] = bank << myBankShift; uInt32 bankOffset = myCurrentSegOffset[segment] = bank << myBankShift;
uInt16 segmentOffset = segment << myBankShift; uInt16 segmentOffset = segment << myBankShift;
uInt16 romHotspot = this->romHotspot(); uInt16 hotspot = this->hotspot();
uInt16 hotSpotAddr; uInt16 hotSpotAddr;
uInt16 fromAddr = (segmentOffset + 0x1000 + myRamSize * 2) & ~System::PAGE_MASK; uInt16 fromAddr = (segmentOffset + 0x1000 + myRamSize * 2) & ~System::PAGE_MASK;
uInt16 toAddr = (segmentOffset + 0x1000 + myBankSize) & ~System::PAGE_MASK; uInt16 toAddr = (segmentOffset + 0x1000 + myBankSize) & ~System::PAGE_MASK;
if(romHotspot) if(hotspot)
hotSpotAddr = (romHotspot & ~System::PAGE_MASK); hotSpotAddr = (hotspot & ~System::PAGE_MASK);
else else
hotSpotAddr = 0xFFFF; // none hotSpotAddr = 0xFFFF; // none

View File

@ -203,9 +203,9 @@ class CartridgeEnhanced : public Cartridge
/** /**
Get the hotspot in ROM address space. Get the hotspot in ROM address space.
@return The first hotspot address in ROM space or 0 @return The first hotspot address (ususally in ROM) space or 0
*/ */
virtual uInt16 romHotspot() const { return 0; } virtual uInt16 hotspot() const { return 0; }
// TODO: handle cases where there the hotspots cover multiple pages // TODO: handle cases where there the hotspots cover multiple pages
private: private:

View File

@ -70,7 +70,7 @@ class CartridgeF0 : public CartridgeEnhanced
private: private:
bool checkSwitchBank(uInt16 address, uInt8 value = 0) override; bool checkSwitchBank(uInt16 address, uInt8 value = 0) override;
uInt16 romHotspot() const override { return 0x1FF0; } uInt16 hotspot() const override { return 0x1FF0; }
uInt16 getStartBank() const override { return 15; } uInt16 getStartBank() const override { return 15; }

View File

@ -69,7 +69,7 @@ class CartridgeF4 : public CartridgeEnhanced
private: private:
bool checkSwitchBank(uInt16 address, uInt8 value = 0) override; bool checkSwitchBank(uInt16 address, uInt8 value = 0) override;
uInt16 romHotspot() const override { return 0x1FF4; } uInt16 hotspot() const override { return 0x1FF4; }
private: private:
// Following constructors and assignment operators not supported // Following constructors and assignment operators not supported

View File

@ -69,7 +69,7 @@ class CartridgeF6 : public CartridgeEnhanced
private: private:
bool checkSwitchBank(uInt16 address, uInt8 value = 0) override; bool checkSwitchBank(uInt16 address, uInt8 value = 0) override;
uInt16 romHotspot() const override { return 0x1FF6; } uInt16 hotspot() const override { return 0x1FF6; }
private: private:
// Following constructors and assignment operators not supported // Following constructors and assignment operators not supported

View File

@ -69,7 +69,7 @@ class CartridgeF8 : public CartridgeEnhanced
private: private:
bool checkSwitchBank(uInt16 address, uInt8 value = 0) override; bool checkSwitchBank(uInt16 address, uInt8 value = 0) override;
uInt16 romHotspot() const override { return 0x1FF8; } uInt16 hotspot() const override { return 0x1FF8; }
uInt16 getStartBank() const override { return 1; } uInt16 getStartBank() const override { return 1; }