stella/src/emucore/CartE7.cxx

315 lines
8.8 KiB
C++

//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2017 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#include "System.hxx"
#include "CartE7.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CartridgeE7::CartridgeE7(const BytePtr& image, uInt32 size,
const Settings& settings)
: Cartridge(settings),
myCurrentRAM(0)
{
// Copy the ROM image into my buffer
memcpy(myImage, image.get(), std::min(16384u, size));
createCodeAccessBase(16384 + 2048);
// Remember startup bank
myStartBank = 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CartridgeE7::reset()
{
initializeRAM(myRAM, 2048);
// Install some default banks for the RAM and first segment
bankRAM(0);
bank(myStartBank);
myBankChanged = true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CartridgeE7::install(System& system)
{
mySystem = &system;
System::PageAccess access(this, System::PA_READ);
// Set the page accessing methods for the hot spots
for(uInt16 addr = (0x1FE0 & ~System::PAGE_MASK); addr < 0x2000;
addr += System::PAGE_SIZE)
{
access.codeAccessBase = &myCodeAccessBase[8128];
mySystem->setPageAccess(addr, access);
}
// Setup the second segment to always point to the last ROM slice
for(uInt16 addr = 0x1A00; addr < (0x1FE0U & ~System::PAGE_MASK);
addr += System::PAGE_SIZE)
{
access.directPeekBase = &myImage[7 * 2048 + (addr & 0x07FF)];
access.codeAccessBase = &myCodeAccessBase[7 * 2048 + (addr & 0x07FF)];
mySystem->setPageAccess(addr, access);
}
myCurrentSlice[1] = 7;
// Install some default banks for the RAM and first segment
bankRAM(0);
bank(myStartBank);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt8 CartridgeE7::peek(uInt16 address)
{
uInt16 peekAddress = address;
address &= 0x0FFF;
// Switch banks if necessary
if((address >= 0x0FE0) && (address <= 0x0FE7))
{
bank(address & 0x0007);
}
else if((address >= 0x0FE8) && (address <= 0x0FEB))
{
bankRAM(address & 0x0003);
}
if((myCurrentSlice[0] == 7) && (address < 0x0400))
{
// Reading from the 1K write port @ $1000 triggers an unwanted write
uInt8 value = mySystem->getDataBusState(0xFF);
if(bankLocked())
return value;
else
{
triggerReadFromWritePort(peekAddress);
return myRAM[address & 0x03FF] = value;
}
}
else if((address >= 0x0800) && (address <= 0x08FF))
{
// Reading from the 256B write port @ $1800 triggers an unwanted write
uInt8 value = mySystem->getDataBusState(0xFF);
if(bankLocked())
return value;
else
{
triggerReadFromWritePort(peekAddress);
return myRAM[1024 + (myCurrentRAM << 8) + (address & 0x00FF)] = value;
}
}
else
return myImage[(myCurrentSlice[address >> 11] << 11) + (address & 0x07FF)];
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartridgeE7::poke(uInt16 address, uInt8)
{
address &= 0x0FFF;
// Switch banks if necessary
if((address >= 0x0FE0) && (address <= 0x0FE7))
{
bank(address & 0x0007);
}
else if((address >= 0x0FE8) && (address <= 0x0FEB))
{
bankRAM(address & 0x0003);
}
// NOTE: This does not handle writing to RAM, however, this
// method should never be called for RAM because of the
// way page accessing has been setup
return false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CartridgeE7::bankRAM(uInt16 bank)
{
if(bankLocked()) return;
// Remember what bank we're in
myCurrentRAM = bank;
uInt16 offset = bank << 8;
// Setup the page access methods for the current bank
System::PageAccess access(this, System::PA_WRITE);
// Set the page accessing method for the 256 bytes of RAM writing pages
for(uInt16 addr = 0x1800; addr < 0x1900; addr += System::PAGE_SIZE)
{
access.directPokeBase = &myRAM[1024 + offset + (addr & 0x00FF)];
access.codeAccessBase = &myCodeAccessBase[8192 + 1024 + offset + (addr & 0x00FF)];
mySystem->setPageAccess(addr, access);
}
// Set the page accessing method for the 256 bytes of RAM reading pages
access.directPokeBase = nullptr;
access.type = System::PA_READ;
for(uInt16 addr = 0x1900; addr < 0x1A00; addr += System::PAGE_SIZE)
{
access.directPeekBase = &myRAM[1024 + offset + (addr & 0x00FF)];
access.codeAccessBase = &myCodeAccessBase[8192 + 1024 + offset + (addr & 0x00FF)];
mySystem->setPageAccess(addr, access);
}
myBankChanged = true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartridgeE7::bank(uInt16 slice)
{
if(bankLocked()) return false;
// Remember what bank we're in
myCurrentSlice[0] = slice;
uInt16 offset = slice << 11;
// Setup the page access methods for the current bank
if(slice != 7)
{
System::PageAccess access(this, System::PA_READ);
// Map ROM image into first segment
for(uInt16 addr = 0x1000; addr < 0x1800; addr += System::PAGE_SIZE)
{
access.directPeekBase = &myImage[offset + (addr & 0x07FF)];
access.codeAccessBase = &myCodeAccessBase[offset + (addr & 0x07FF)];
mySystem->setPageAccess(addr, access);
}
}
else
{
System::PageAccess access(this, System::PA_WRITE);
// Set the page accessing method for the 1K slice of RAM writing pages
for(uInt16 addr = 0x1000; addr < 0x1400; addr += System::PAGE_SIZE)
{
access.directPokeBase = &myRAM[addr & 0x03FF];
access.codeAccessBase = &myCodeAccessBase[8192 + (addr & 0x03FF)];
mySystem->setPageAccess(addr, access);
}
// Set the page accessing method for the 1K slice of RAM reading pages
access.directPokeBase = nullptr;
access.type = System::PA_READ;
for(uInt16 addr = 0x1400; addr < 0x1800; addr += System::PAGE_SIZE)
{
access.directPeekBase = &myRAM[addr & 0x03FF];
access.codeAccessBase = &myCodeAccessBase[8192 + (addr & 0x03FF)];
mySystem->setPageAccess(addr, access);
}
}
return myBankChanged = true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 CartridgeE7::getBank() const
{
return myCurrentSlice[0];
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 CartridgeE7::bankCount() const
{
return 8;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartridgeE7::patch(uInt16 address, uInt8 value)
{
address = address & 0x0FFF;
if(address < 0x0800)
{
if(myCurrentSlice[0] == 7)
{
// Normally, a write to the read port won't do anything
// However, the patch command is special in that ignores such
// cart restrictions
myRAM[address & 0x03FF] = value;
}
else
myImage[(myCurrentSlice[0] << 11) + (address & 0x07FF)] = value;
}
else if(address < 0x0900)
{
// Normally, a write to the read port won't do anything
// However, the patch command is special in that ignores such
// cart restrictions
myRAM[1024 + (myCurrentRAM << 8) + (address & 0x00FF)] = value;
}
else
myImage[(myCurrentSlice[address >> 11] << 11) + (address & 0x07FF)] = value;
return myBankChanged = true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const uInt8* CartridgeE7::getImage(uInt32& size) const
{
size = 16384;
return myImage;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartridgeE7::save(Serializer& out) const
{
try
{
out.putString(name());
out.putShortArray(myCurrentSlice, 2);
out.putShort(myCurrentRAM);
out.putByteArray(myRAM, 2048);
}
catch(...)
{
cerr << "ERROR: CartridgeE7::save" << endl;
return false;
}
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartridgeE7::load(Serializer& in)
{
try
{
if(in.getString() != name())
return false;
in.getShortArray(myCurrentSlice, 2);
myCurrentRAM = in.getShort();
in.getByteArray(myRAM, 2048);
}
catch(...)
{
cerr << "ERROR: CartridgeE7::load" << endl;
return false;
}
// Set up the previously used banks for the RAM and segment
bankRAM(myCurrentRAM);
bank(myCurrentSlice[0]);
return true;
}