//============================================================================ // // 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-2010 by Bradford W. Mott 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. // // $Id$ //============================================================================ #include #include #include "System.hxx" #include "TIA.hxx" #include "Cart3F.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cartridge3F::Cartridge3F(const uInt8* image, uInt32 size) : mySize(size) { // Allocate array for the ROM image myImage = new uInt8[mySize]; // Copy the ROM image into my buffer memcpy(myImage, image, mySize); // Remember startup bank myStartBank = 0; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cartridge3F::~Cartridge3F() { delete[] myImage; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Cartridge3F::reset() { // We'll map the startup bank into the first segment upon reset bank(myStartBank); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Cartridge3F::install(System& system) { mySystem = &system; uInt16 shift = mySystem->pageShift(); uInt16 mask = mySystem->pageMask(); // Make sure the system we're being installed in has a page size that'll work assert((0x1800 & mask) == 0); // Set the page accessing methods for the hot spots (for 100% emulation // we need to chain any accesses below 0x40 to the TIA. Our poke() method // does this via mySystem->tiaPoke(...), at least until we come up with a // cleaner way to do it). System::PageAccess access; for(uInt32 i = 0x00; i < 0x40; i += (1 << shift)) { access.device = this; access.directPeekBase = 0; access.directPokeBase = 0; mySystem->setPageAccess(i >> shift, access); } // Setup the second segment to always point to the last ROM slice for(uInt32 j = 0x1800; j < 0x2000; j += (1 << shift)) { access.device = this; access.directPeekBase = &myImage[(mySize - 2048) + (j & 0x07FF)]; access.directPokeBase = 0; mySystem->setPageAccess(j >> shift, access); } // Install pages for startup bank into the first segment bank(myStartBank); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - uInt8 Cartridge3F::peek(uInt16 address) { address &= 0x0FFF; if(address < 0x0800) { return myImage[(address & 0x07FF) + (myCurrentBank << 11)]; } else { return myImage[(address & 0x07FF) + mySize - 2048]; } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Cartridge3F::poke(uInt16 address, uInt8 value) { address &= 0x0FFF; // Switch banks if necessary if(address <= 0x003F) { bank(value); } // Pass the poke through to the TIA. In a real Atari, both the cart and the // TIA see the address lines, and both react accordingly. In Stella, each // 64-byte chunk of address space is "owned" by only one device. If we // don't chain the poke to the TIA, then the TIA can't see it... mySystem->tia().poke(address, value); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Cartridge3F::bank(uInt16 bank) { if(bankLocked()) return; // Make sure the bank they're asking for is reasonable if(((uInt32)bank << 11) < mySize) { myCurrentBank = bank; } else { // Oops, the bank they're asking for isn't valid so let's wrap it // around to a valid bank number myCurrentBank = bank % (mySize >> 11); } uInt32 offset = myCurrentBank << 11; uInt16 shift = mySystem->pageShift(); // Setup the page access methods for the current bank System::PageAccess access; access.device = this; access.directPokeBase = 0; // Map ROM image into the system for(uInt32 address = 0x1000; address < 0x1800; address += (1 << shift)) { access.directPeekBase = &myImage[offset + (address & 0x07FF)]; mySystem->setPageAccess(address >> shift, access); } myBankChanged = true; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int Cartridge3F::bank() { return myCurrentBank; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int Cartridge3F::bankCount() { return mySize >> 11; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool Cartridge3F::patch(uInt16 address, uInt8 value) { address &= 0x0FFF; if(address < 0x0800) myImage[(address & 0x07FF) + (myCurrentBank << 11)] = value; else myImage[(address & 0x07FF) + mySize - 2048] = value; return myBankChanged = true; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - uInt8* Cartridge3F::getImage(int& size) { size = mySize; return myImage; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool Cartridge3F::save(Serializer& out) const { const string& cart = name(); try { out.putString(cart); out.putInt(myCurrentBank); } catch(const char* msg) { cerr << "ERROR: Cartridge3F::save" << endl << " " << msg << endl; return false; } return true; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool Cartridge3F::load(Serializer& in) { const string& cart = name(); try { if(in.getString() != cart) return false; myCurrentBank = (uInt16) in.getInt(); } catch(const char* msg) { cerr << "ERROR: Cartridge3F::load" << endl << " " << msg << endl; return false; } // Now, go to the current bank bank(myCurrentBank); return true; }