| 1 | //============================================================================ |
| 2 | // |
| 3 | // SSSS tt lll lll |
| 4 | // SS SS tt ll ll |
| 5 | // SS tttttt eeee ll ll aaaa |
| 6 | // SSSS tt ee ee ll ll aa |
| 7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
| 8 | // SS SS tt ee ll ll aa aa |
| 9 | // SSSS ttt eeeee llll llll aaaaa |
| 10 | // |
| 11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
| 12 | // and the Stella Team |
| 13 | // |
| 14 | // See the file "License.txt" for information on usage and redistribution of |
| 15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 16 | //============================================================================ |
| 17 | |
| 18 | #include "System.hxx" |
| 19 | #include "CartMDM.hxx" |
| 20 | |
| 21 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 22 | CartridgeMDM::CartridgeMDM(const ByteBuffer& image, size_t size, |
| 23 | const string& md5, const Settings& settings) |
| 24 | : Cartridge(settings, md5), |
| 25 | mySize(size), |
| 26 | myBankOffset(0), |
| 27 | myBankingDisabled(false) |
| 28 | { |
| 29 | // Allocate array for the ROM image |
| 30 | myImage = make_unique<uInt8[]>(mySize); |
| 31 | |
| 32 | // Copy the ROM image into my buffer |
| 33 | std::copy_n(image.get(), mySize, myImage.get()); |
| 34 | createCodeAccessBase(mySize); |
| 35 | } |
| 36 | |
| 37 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 38 | void CartridgeMDM::reset() |
| 39 | { |
| 40 | initializeStartBank(0); |
| 41 | |
| 42 | // Upon reset we switch to the startup bank |
| 43 | bank(startBank()); |
| 44 | } |
| 45 | |
| 46 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 47 | void CartridgeMDM::install(System& system) |
| 48 | { |
| 49 | mySystem = &system; |
| 50 | |
| 51 | // Get the page accessing methods for the hot spots since they overlap |
| 52 | // areas within the TIA we'll need to forward requests to the TIA |
| 53 | myHotSpotPageAccess[0] = mySystem->getPageAccess(0x0800); |
| 54 | myHotSpotPageAccess[1] = mySystem->getPageAccess(0x0900); |
| 55 | myHotSpotPageAccess[2] = mySystem->getPageAccess(0x0A00); |
| 56 | myHotSpotPageAccess[3] = mySystem->getPageAccess(0x0B00); |
| 57 | myHotSpotPageAccess[4] = mySystem->getPageAccess(0x0C00); |
| 58 | myHotSpotPageAccess[5] = mySystem->getPageAccess(0x0D00); |
| 59 | myHotSpotPageAccess[6] = mySystem->getPageAccess(0x0E00); |
| 60 | myHotSpotPageAccess[7] = mySystem->getPageAccess(0x0F00); |
| 61 | |
| 62 | // Set the page accessing methods for the hot spots |
| 63 | System::PageAccess access(this, System::PageAccessType::READWRITE); |
| 64 | for(uInt16 addr = 0x0800; addr < 0x0BFF; addr += System::PAGE_SIZE) |
| 65 | mySystem->setPageAccess(addr, access); |
| 66 | |
| 67 | // Install pages for bank 0 |
| 68 | bank(startBank()); |
| 69 | } |
| 70 | |
| 71 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 72 | uInt8 CartridgeMDM::peek(uInt16 address) |
| 73 | { |
| 74 | // Because of the way we've set up accessing above, we can only |
| 75 | // get here when the addresses are from 0x800 - 0xBFF |
| 76 | if((address & 0x1C00) == 0x0800) |
| 77 | bank(address & 0x0FF); |
| 78 | |
| 79 | int hotspot = ((address & 0x0F00) >> 8) - 8; |
| 80 | return myHotSpotPageAccess[hotspot].device->peek(address); |
| 81 | } |
| 82 | |
| 83 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 84 | bool CartridgeMDM::poke(uInt16 address, uInt8 value) |
| 85 | { |
| 86 | // All possible addresses can appear here, but we only care |
| 87 | // about those below $1000 |
| 88 | if(!(address & 0x1000)) |
| 89 | { |
| 90 | if((address & 0x1C00) == 0x0800) |
| 91 | bank(address & 0x0FF); |
| 92 | |
| 93 | int hotspot = ((address & 0x0F00) >> 8) - 8; |
| 94 | myHotSpotPageAccess[hotspot].device->poke(address, value); |
| 95 | } |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 101 | bool CartridgeMDM::bank(uInt16 bank) |
| 102 | { |
| 103 | if(bankLocked() || myBankingDisabled) return false; |
| 104 | |
| 105 | // Remember what bank we're in |
| 106 | // Wrap around to a valid bank number if necessary |
| 107 | myBankOffset = (bank % bankCount()) << 12; |
| 108 | |
| 109 | // Setup the page access methods for the current bank |
| 110 | System::PageAccess access(this, System::PageAccessType::READ); |
| 111 | |
| 112 | // Map ROM image into the system |
| 113 | for(uInt16 addr = 0x1000; addr < 0x2000; addr += System::PAGE_SIZE) |
| 114 | { |
| 115 | access.directPeekBase = &myImage[myBankOffset + (addr & 0x0FFF)]; |
| 116 | access.codeAccessBase = &myCodeAccessBase[myBankOffset + (addr & 0x0FFF)]; |
| 117 | mySystem->setPageAccess(addr, access); |
| 118 | } |
| 119 | |
| 120 | // Accesses above bank 127 disable further bankswitching; we're only |
| 121 | // concerned with the lower byte |
| 122 | myBankingDisabled = myBankingDisabled || bank > 127; |
| 123 | return myBankChanged = true; |
| 124 | } |
| 125 | |
| 126 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 127 | uInt16 CartridgeMDM::getBank(uInt16) const |
| 128 | { |
| 129 | return myBankOffset >> 12; |
| 130 | } |
| 131 | |
| 132 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 133 | uInt16 CartridgeMDM::bankCount() const |
| 134 | { |
| 135 | return uInt16(mySize >> 12); |
| 136 | } |
| 137 | |
| 138 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 139 | bool CartridgeMDM::patch(uInt16 address, uInt8 value) |
| 140 | { |
| 141 | myImage[myBankOffset + (address & 0x0FFF)] = value; |
| 142 | return myBankChanged = true; |
| 143 | } |
| 144 | |
| 145 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 146 | const uInt8* CartridgeMDM::getImage(size_t& size) const |
| 147 | { |
| 148 | size = mySize; |
| 149 | return myImage.get(); |
| 150 | } |
| 151 | |
| 152 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 153 | bool CartridgeMDM::save(Serializer& out) const |
| 154 | { |
| 155 | try |
| 156 | { |
| 157 | out.putInt(myBankOffset); |
| 158 | } |
| 159 | catch(...) |
| 160 | { |
| 161 | cerr << "ERROR: CartridgeMDM::save" << endl; |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 169 | bool CartridgeMDM::load(Serializer& in) |
| 170 | { |
| 171 | try |
| 172 | { |
| 173 | myBankOffset = in.getInt(); |
| 174 | } |
| 175 | catch(...) |
| 176 | { |
| 177 | cerr << "ERROR: CartridgeMDM::load" << endl; |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | // Remember what bank we were in |
| 182 | bank(myBankOffset >> 12); |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |