| 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 | #ifndef CARTRIDGE_DETECTOR_HXX |
| 19 | #define CARTRIDGE_DETECTOR_HXX |
| 20 | |
| 21 | class Cartridge; |
| 22 | class Properties; |
| 23 | |
| 24 | #include "Bankswitch.hxx" |
| 25 | #include "bspf.hxx" |
| 26 | #include "Settings.hxx" |
| 27 | |
| 28 | /** |
| 29 | Auto-detect cart type based on various attributes (file size, signatures, |
| 30 | filenames, etc) |
| 31 | |
| 32 | @author Stephen Anthony |
| 33 | */ |
| 34 | class CartDetector |
| 35 | { |
| 36 | public: |
| 37 | /** |
| 38 | Create a new cartridge object allocated on the heap. The |
| 39 | type of cartridge created depends on the properties object. |
| 40 | |
| 41 | @param image A pointer to the ROM image |
| 42 | @param size The size of the ROM image |
| 43 | @param md5 The md5sum for the given ROM image (can be updated) |
| 44 | @param dtype The detected bankswitch type of the ROM image |
| 45 | @param settings The settings container |
| 46 | @return Pointer to the new cartridge object allocated on the heap |
| 47 | */ |
| 48 | static unique_ptr<Cartridge> create(const FilesystemNode& file, |
| 49 | const ByteBuffer& image, size_t size, string& md5, |
| 50 | const string& dtype, Settings& settings); |
| 51 | |
| 52 | /** |
| 53 | Try to auto-detect the bankswitching type of the cartridge |
| 54 | |
| 55 | @param image A pointer to the ROM image |
| 56 | @param size The size of the ROM image |
| 57 | |
| 58 | @return The "best guess" for the cartridge type |
| 59 | */ |
| 60 | static Bankswitch::Type autodetectType(const ByteBuffer& image, size_t size); |
| 61 | |
| 62 | private: |
| 63 | /** |
| 64 | Create a cartridge from a multi-cart image pointer; internally this |
| 65 | takes a slice of the ROM image ues that for the cartridge. |
| 66 | |
| 67 | @param image A pointer to the complete ROM image |
| 68 | @param size The size of the ROM image slice |
| 69 | @param numroms The number of ROMs in the multicart |
| 70 | @param md5 The md5sum for the slice of the ROM image |
| 71 | @param type The detected type of the slice of the ROM image |
| 72 | @param id The ID for the slice of the ROM image |
| 73 | @param settings The settings container |
| 74 | |
| 75 | @return Pointer to the new cartridge object allocated on the heap |
| 76 | */ |
| 77 | static unique_ptr<Cartridge> |
| 78 | createFromMultiCart(const ByteBuffer& image, size_t& size, |
| 79 | uInt32 numroms, string& md5, Bankswitch::Type type, string& id, |
| 80 | Settings& settings); |
| 81 | |
| 82 | /** |
| 83 | Create a cartridge from the entire image pointer. |
| 84 | |
| 85 | @param image A pointer to the complete ROM image |
| 86 | @param size The size of the ROM image |
| 87 | @param type The bankswitch type of the ROM image |
| 88 | @param md5 The md5sum for the ROM image |
| 89 | @param settings The settings container |
| 90 | |
| 91 | @return Pointer to the new cartridge object allocated on the heap |
| 92 | */ |
| 93 | static unique_ptr<Cartridge> |
| 94 | createFromImage(const ByteBuffer& image, size_t size, Bankswitch::Type type, |
| 95 | const string& md5, Settings& settings); |
| 96 | |
| 97 | /** |
| 98 | Search the image for the specified byte signature |
| 99 | |
| 100 | @param image A pointer to the ROM image |
| 101 | @param imagesize The size of the ROM image |
| 102 | @param signature The byte sequence to search for |
| 103 | @param sigsize The number of bytes in the signature |
| 104 | @param minhits The minimum number of times a signature is to be found |
| 105 | |
| 106 | @return True if the signature was found at least 'minhits' time, else false |
| 107 | */ |
| 108 | static bool searchForBytes(const uInt8* image, size_t imagesize, |
| 109 | const uInt8* signature, uInt32 sigsize, |
| 110 | uInt32 minhits); |
| 111 | |
| 112 | /** |
| 113 | Returns true if the image is probably a SuperChip (128 bytes RAM) |
| 114 | Note: should be called only on ROMs with size multiple of 4K |
| 115 | */ |
| 116 | static bool isProbablySC(const ByteBuffer& image, size_t size); |
| 117 | |
| 118 | /** |
| 119 | Returns true if the image probably contains ARM code in the first 1K |
| 120 | */ |
| 121 | static bool isProbablyARM(const ByteBuffer& image, size_t size); |
| 122 | |
| 123 | /** |
| 124 | Returns true if the image is probably a 0840 bankswitching cartridge |
| 125 | */ |
| 126 | static bool isProbably0840(const ByteBuffer& image, size_t size); |
| 127 | |
| 128 | /** |
| 129 | Returns true if the image is probably a 3E bankswitching cartridge |
| 130 | */ |
| 131 | static bool isProbably3E(const ByteBuffer& image, size_t size); |
| 132 | |
| 133 | /** |
| 134 | Returns true if the image is probably a 3E+ bankswitching cartridge |
| 135 | */ |
| 136 | static bool isProbably3EPlus(const ByteBuffer& image, size_t size); |
| 137 | |
| 138 | /** |
| 139 | Returns true if the image is probably a 3F bankswitching cartridge |
| 140 | */ |
| 141 | static bool isProbably3F(const ByteBuffer& image, size_t size); |
| 142 | |
| 143 | /** |
| 144 | Returns true if the image is probably a 4A50 bankswitching cartridge |
| 145 | */ |
| 146 | static bool isProbably4A50(const ByteBuffer& image, size_t size); |
| 147 | |
| 148 | /** |
| 149 | Returns true if the image is probably a 4K SuperChip (128 bytes RAM) |
| 150 | */ |
| 151 | static bool isProbably4KSC(const ByteBuffer& image, size_t size); |
| 152 | |
| 153 | /** |
| 154 | Returns true if the image is probably a BF/BFSC bankswitching cartridge |
| 155 | */ |
| 156 | static bool isProbablyBF(const ByteBuffer& image, size_t size, Bankswitch::Type& type); |
| 157 | |
| 158 | /** |
| 159 | Returns true if the image is probably a BUS bankswitching cartridge |
| 160 | */ |
| 161 | static bool isProbablyBUS(const ByteBuffer& image, size_t size); |
| 162 | |
| 163 | /** |
| 164 | Returns true if the image is probably a CDF bankswitching cartridge |
| 165 | */ |
| 166 | static bool isProbablyCDF(const ByteBuffer& image, size_t size); |
| 167 | |
| 168 | /** |
| 169 | Returns true if the image is probably a CTY bankswitching cartridge |
| 170 | */ |
| 171 | static bool isProbablyCTY(const ByteBuffer& image, size_t size); |
| 172 | |
| 173 | /** |
| 174 | Returns true if the image is probably a CV bankswitching cartridge |
| 175 | */ |
| 176 | static bool isProbablyCV(const ByteBuffer& image, size_t size); |
| 177 | |
| 178 | /** |
| 179 | Returns true if the image is probably a CV+ bankswitching cartridge |
| 180 | */ |
| 181 | static bool isProbablyCVPlus(const ByteBuffer& image, size_t size); |
| 182 | |
| 183 | /** |
| 184 | Returns true if the image is probably a DASH bankswitching cartridge |
| 185 | */ |
| 186 | static bool isProbablyDASH(const ByteBuffer& image, size_t size); |
| 187 | |
| 188 | /** |
| 189 | Returns true if the image is probably a DF/DFSC bankswitching cartridge |
| 190 | */ |
| 191 | static bool isProbablyDF(const ByteBuffer& image, size_t size, Bankswitch::Type& type); |
| 192 | |
| 193 | /** |
| 194 | Returns true if the image is probably a DPC+ bankswitching cartridge |
| 195 | */ |
| 196 | static bool isProbablyDPCplus(const ByteBuffer& image, size_t size); |
| 197 | |
| 198 | /** |
| 199 | Returns true if the image is probably a E0 bankswitching cartridge |
| 200 | */ |
| 201 | static bool isProbablyE0(const ByteBuffer& image, size_t size); |
| 202 | |
| 203 | /** |
| 204 | Returns true if the image is probably a E7 bankswitching cartridge |
| 205 | */ |
| 206 | static bool isProbablyE7(const ByteBuffer& image, size_t size); |
| 207 | |
| 208 | /** |
| 209 | Returns true if the image is probably a E78K bankswitching cartridge |
| 210 | */ |
| 211 | static bool isProbablyE78K(const ByteBuffer& image, size_t size); |
| 212 | |
| 213 | /** |
| 214 | Returns true if the image is probably an EF/EFSC bankswitching cartridge |
| 215 | */ |
| 216 | static bool isProbablyEF(const ByteBuffer& image, size_t size, Bankswitch::Type& type); |
| 217 | |
| 218 | /** |
| 219 | Returns true if the image is probably an F6 bankswitching cartridge |
| 220 | */ |
| 221 | //static bool isProbablyF6(const ByteBuffer& image, size_t size); |
| 222 | |
| 223 | /** |
| 224 | Returns true if the image is probably an FA2 bankswitching cartridge |
| 225 | */ |
| 226 | static bool isProbablyFA2(const ByteBuffer& image, size_t size); |
| 227 | |
| 228 | /** |
| 229 | Returns true if the image is probably an FC bankswitching cartridge |
| 230 | */ |
| 231 | static bool isProbablyFC(const ByteBuffer& image, size_t size); |
| 232 | |
| 233 | /** |
| 234 | Returns true if the image is probably an FE bankswitching cartridge |
| 235 | */ |
| 236 | static bool isProbablyFE(const ByteBuffer& image, size_t size); |
| 237 | |
| 238 | /** |
| 239 | Returns true if the image is probably a MDM bankswitching cartridge |
| 240 | */ |
| 241 | static bool isProbablyMDM(const ByteBuffer& image, size_t size); |
| 242 | |
| 243 | /** |
| 244 | Returns true if the image is probably a SB bankswitching cartridge |
| 245 | */ |
| 246 | static bool isProbablySB(const ByteBuffer& image, size_t size); |
| 247 | |
| 248 | /** |
| 249 | Returns true if the image is probably a UA bankswitching cartridge |
| 250 | */ |
| 251 | static bool isProbablyUA(const ByteBuffer& image, size_t size); |
| 252 | |
| 253 | /** |
| 254 | Returns true if the image is probably a Wickstead Design bankswitching cartridge |
| 255 | */ |
| 256 | static bool isProbablyWD(const ByteBuffer& image, size_t size); |
| 257 | |
| 258 | /** |
| 259 | Returns true if the image is probably an X07 bankswitching cartridge |
| 260 | */ |
| 261 | static bool isProbablyX07(const ByteBuffer& image, size_t size); |
| 262 | |
| 263 | |
| 264 | private: |
| 265 | // Following constructors and assignment operators not supported |
| 266 | CartDetector() = delete; |
| 267 | CartDetector(const CartDetector&) = delete; |
| 268 | CartDetector(CartDetector&&) = delete; |
| 269 | CartDetector& operator=(const CartDetector&) = delete; |
| 270 | CartDetector& operator=(CartDetector&&) = delete; |
| 271 | }; |
| 272 | |
| 273 | #endif |
| 274 | |