diff --git a/src/emucore/Cart3EPlus.cxx b/src/emucore/Cart3EPlus.cxx index cd2527f17..31f4e9dd2 100644 --- a/src/emucore/Cart3EPlus.cxx +++ b/src/emucore/Cart3EPlus.cxx @@ -24,7 +24,7 @@ Cartridge3EPlus::Cartridge3EPlus(const ByteBuffer& image, size_t size, string_view md5, const Settings& settings, size_t bsSize) : Cartridge3E(image, size, md5, settings, - bsSize == 0 ? BSPF::nextMultipleOf(size, 1_KB) : bsSize) + bsSize == 0 ? std::max(4_KB, BSPF::nextMultipleOf(size, 1_KB)) : bsSize) { myBankShift = BANK_SHIFT; myRamSize = RAM_SIZE; diff --git a/src/emucore/Cart3EPlus.hxx b/src/emucore/Cart3EPlus.hxx index a34ac87eb..d20992a11 100644 --- a/src/emucore/Cart3EPlus.hxx +++ b/src/emucore/Cart3EPlus.hxx @@ -133,6 +133,12 @@ class Cartridge3EPlus: public Cartridge3E bool checkSwitchBank(uInt16 address, uInt8 value) override; + /** + Get the number of segments supported by the cartridge. + */ + uInt16 calcNumSegments() const override { return 4; } + + private: // log(ROM bank segment size) / log(2) static constexpr uInt16 BANK_SHIFT = 10; // = 1K = 0x0400 diff --git a/src/emucore/CartE7.hxx b/src/emucore/CartE7.hxx index 713c11085..bfb631333 100644 --- a/src/emucore/CartE7.hxx +++ b/src/emucore/CartE7.hxx @@ -213,7 +213,7 @@ class CartridgeE7 : public Cartridge private: // Size of RAM in the cart static constexpr uInt32 RAM_SIZE = 0x800; // 1K + 4 * 256B = 2K - // Number of segment within the 4K address space + // Number of segments within the 4K address space static constexpr uInt32 NUM_SEGMENTS = 2; /** diff --git a/src/emucore/CartEnhanced.cxx b/src/emucore/CartEnhanced.cxx index 744534677..a101ec702 100644 --- a/src/emucore/CartEnhanced.cxx +++ b/src/emucore/CartEnhanced.cxx @@ -37,11 +37,11 @@ CartridgeEnhanced::CartridgeEnhanced(const ByteBuffer& image, size_t size, << "), truncating " << (size - bsSize) << " bytes\n"; Logger::info(buf.str()); } - else if(size < mySize) + else if(size < bsSize) { ostringstream buf; - buf << "ROM smaller than expected (" << mySize << " > " << size - << "), appending " << (mySize - size) << " bytes\n"; + buf << "ROM smaller than expected (" << bsSize << " > " << size + << "), appending " << (bsSize - size) << " bytes\n"; Logger::info(buf.str()); } @@ -72,10 +72,7 @@ void CartridgeEnhanced::install(System& system) // calculate bank switching and RAM sizes and masks myBankSize = 1 << myBankShift; // e.g. = 2 ^ 12 = 4K = 0x1000 myBankMask = myBankSize - 1; // e.g. = 0x0FFF - // Either the bankswitching supports multiple segments - // or the ROM is < 4K (-> 1 segment) - myBankSegs = std::min(1 << (MAX_BANK_SHIFT - myBankShift), - static_cast(mySize) / myBankSize); // e.g. = 1 + myBankSegs = calcNumSegments(); // ROM has an offset if RAM inside a bank (e.g. for F8SC) myRomOffset = myRamBankCount > 0U ? 0U : static_cast(myRamSize * 2); myRamMask = ramSize - 1; // e.g. = 0xFFFF (doesn't matter for RAM size 0) @@ -339,6 +336,16 @@ uInt16 CartridgeEnhanced::ramBankCount() const return myRamBankCount; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +uInt16 CartridgeEnhanced::calcNumSegments() const +{ + + // Either the bankswitching supports multiple segments + // or the ROM is < 4K (-> 1 segment) + return std::min(1 << (MAX_BANK_SHIFT - myBankShift), + static_cast(mySize) / myBankSize); // e.g. = 1 +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool CartridgeEnhanced::isRamBank(uInt16 address) const {