| 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 "CompuMate.hxx" |
| 19 | #include "System.hxx" |
| 20 | #include "M6532.hxx" |
| 21 | #include "CartCM.hxx" |
| 22 | |
| 23 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 24 | CartridgeCM::CartridgeCM(const ByteBuffer& image, size_t size, |
| 25 | const string& md5, const Settings& settings) |
| 26 | : Cartridge(settings, md5), |
| 27 | mySWCHA(0xFF), // portA is all 1's |
| 28 | myBankOffset(0) |
| 29 | { |
| 30 | // Copy the ROM image into my buffer |
| 31 | std::copy_n(image.get(), std::min(myImage.size(), size), myImage.begin()); |
| 32 | createCodeAccessBase(myImage.size()); |
| 33 | } |
| 34 | |
| 35 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 36 | void CartridgeCM::reset() |
| 37 | { |
| 38 | initializeRAM(myRAM.data(), myRAM.size()); |
| 39 | |
| 40 | // On powerup, the last bank of ROM is enabled and RAM is disabled |
| 41 | mySWCHA = 0xFF; |
| 42 | initializeStartBank(mySWCHA & 0x3); |
| 43 | |
| 44 | // Upon reset we switch to the startup bank |
| 45 | bank(startBank()); |
| 46 | } |
| 47 | |
| 48 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 49 | void CartridgeCM::install(System& system) |
| 50 | { |
| 51 | mySystem = &system; |
| 52 | |
| 53 | // Mirror all access in RIOT; by doing so we're taking responsibility |
| 54 | // for that address space in peek and poke below. |
| 55 | mySystem->m6532().installDelegate(system, *this); |
| 56 | |
| 57 | // Install pages for the startup bank |
| 58 | bank(startBank()); |
| 59 | } |
| 60 | |
| 61 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 62 | uInt8 CartridgeCM::peek(uInt16 address) |
| 63 | { |
| 64 | // NOTE: This does not handle accessing cart ROM/RAM, however, this method |
| 65 | // should never be called for ROM/RAM because of the way page accessing |
| 66 | // has been setup (it will only ever be called for RIOT reads) |
| 67 | return mySystem->m6532().peek(address); |
| 68 | } |
| 69 | |
| 70 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 71 | bool CartridgeCM::poke(uInt16 address, uInt8 value) |
| 72 | { |
| 73 | // NOTE: This could be called for RIOT writes or cart ROM writes |
| 74 | // In the latter case, the write is ignored |
| 75 | if(!(address & 0x1000)) |
| 76 | { |
| 77 | // RIOT mirroring, check bankswitch |
| 78 | if(address == 0x280) |
| 79 | { |
| 80 | mySWCHA = value; |
| 81 | bank(mySWCHA & 0x3); |
| 82 | if(myCompuMate) |
| 83 | { |
| 84 | uInt8& column = myCompuMate->column(); |
| 85 | if(value & 0x20) |
| 86 | column = 0; |
| 87 | if(value & 0x40) |
| 88 | column = (column + 1) % 10; |
| 89 | } |
| 90 | } |
| 91 | mySystem->m6532().poke(address, value); |
| 92 | } |
| 93 | return myBankChanged; |
| 94 | } |
| 95 | |
| 96 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 97 | uInt8 CartridgeCM::column() const |
| 98 | { |
| 99 | return myCompuMate->column(); |
| 100 | } |
| 101 | |
| 102 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 103 | bool CartridgeCM::bank(uInt16 bank) |
| 104 | { |
| 105 | if(bankLocked()) return false; |
| 106 | |
| 107 | // Remember what bank we're in |
| 108 | myBankOffset = bank << 12; |
| 109 | |
| 110 | // Although this scheme contains four 4K ROM banks and one 2K RAM bank, |
| 111 | // it's easier to think of things in terms of 2K slices, as follows: |
| 112 | // |
| 113 | // The lower 2K of cart address space always points to the lower 2K of the |
| 114 | // current ROM bank |
| 115 | // The upper 2K of cart address space can point to either the 2K of RAM or |
| 116 | // the upper 2K of the current ROM bank |
| 117 | |
| 118 | System::PageAccess access(this, System::PageAccessType::READ); |
| 119 | |
| 120 | // Lower 2K (always ROM) |
| 121 | for(uInt16 addr = 0x1000; addr < 0x1800; addr += System::PAGE_SIZE) |
| 122 | { |
| 123 | access.directPeekBase = &myImage[myBankOffset + (addr & 0x0FFF)]; |
| 124 | access.codeAccessBase = &myCodeAccessBase[myBankOffset + (addr & 0x0FFF)]; |
| 125 | mySystem->setPageAccess(addr, access); |
| 126 | } |
| 127 | |
| 128 | // Upper 2K (RAM or ROM) |
| 129 | for(uInt16 addr = 0x1800; addr < 0x2000; addr += System::PAGE_SIZE) |
| 130 | { |
| 131 | access.type = System::PageAccessType::READWRITE; |
| 132 | |
| 133 | if(mySWCHA & 0x10) |
| 134 | { |
| 135 | access.directPeekBase = &myImage[myBankOffset + (addr & 0x0FFF)]; |
| 136 | access.codeAccessBase = &myCodeAccessBase[myBankOffset + (addr & 0x0FFF)]; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | access.directPeekBase = &myRAM[addr & 0x7FF]; |
| 141 | access.codeAccessBase = &myCodeAccessBase[myBankOffset + (addr & 0x07FF)]; |
| 142 | } |
| 143 | |
| 144 | if((mySWCHA & 0x30) == 0x20) |
| 145 | access.directPokeBase = &myRAM[addr & 0x7FF]; |
| 146 | else |
| 147 | access.directPokeBase = nullptr; |
| 148 | |
| 149 | mySystem->setPageAccess(addr, access); |
| 150 | } |
| 151 | |
| 152 | return myBankChanged = true; |
| 153 | } |
| 154 | |
| 155 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 156 | uInt16 CartridgeCM::getBank(uInt16) const |
| 157 | { |
| 158 | return myBankOffset >> 12; |
| 159 | } |
| 160 | |
| 161 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 162 | uInt16 CartridgeCM::bankCount() const |
| 163 | { |
| 164 | // We report 4 banks (of ROM), even though RAM can overlap the upper 2K |
| 165 | // of cart address space at some times |
| 166 | // However, this RAM isn't enabled in the normal way that bankswitching |
| 167 | // works, so it is ignored here |
| 168 | return 4; |
| 169 | } |
| 170 | |
| 171 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 172 | bool CartridgeCM::patch(uInt16 address, uInt8 value) |
| 173 | { |
| 174 | if((mySWCHA & 0x30) == 0x20) |
| 175 | myRAM[address & 0x7FF] = value; |
| 176 | else |
| 177 | myImage[myBankOffset + address] = value; |
| 178 | |
| 179 | return myBankChanged = true; |
| 180 | } |
| 181 | |
| 182 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 183 | const uInt8* CartridgeCM::getImage(size_t& size) const |
| 184 | { |
| 185 | size = myImage.size(); |
| 186 | return myImage.data(); |
| 187 | } |
| 188 | |
| 189 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 190 | bool CartridgeCM::save(Serializer& out) const |
| 191 | { |
| 192 | try |
| 193 | { |
| 194 | out.putShort(myBankOffset); |
| 195 | out.putByte(mySWCHA); |
| 196 | out.putByte(myCompuMate->column()); |
| 197 | out.putByteArray(myRAM.data(), myRAM.size()); |
| 198 | } |
| 199 | catch(...) |
| 200 | { |
| 201 | cerr << "ERROR: CartridgeCM::save" << endl; |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 209 | bool CartridgeCM::load(Serializer& in) |
| 210 | { |
| 211 | try |
| 212 | { |
| 213 | myBankOffset = in.getShort(); |
| 214 | mySWCHA = in.getByte(); |
| 215 | myCompuMate->column() = in.getByte(); |
| 216 | in.getByteArray(myRAM.data(), myRAM.size()); |
| 217 | } |
| 218 | catch(...) |
| 219 | { |
| 220 | cerr << "ERROR: CartridgeCM::load" << endl; |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | // Remember what bank we were in |
| 225 | bank(myBankOffset >> 12); |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |