public static CartridgeDevice Load(byte[] crtFile) { using (var mem = new MemoryStream(crtFile)) { var reader = new BinaryReader(mem); if (new string(reader.ReadChars(16)) != "C64 CARTRIDGE ") { return null; } var chipAddress = new List<int>(); var chipBank = new List<int>(); var chipData = new List<int[]>(); var chipType = new List<int>(); var headerLength = ReadCRTInt(reader); var version = ReadCRTShort(reader); var mapper = ReadCRTShort(reader); var exrom = reader.ReadByte() != 0; var game = reader.ReadByte() != 0; // reserved reader.ReadBytes(6); // cartridge name reader.ReadBytes(0x20); // skip extra header bytes if (headerLength > 0x40) { reader.ReadBytes(headerLength - 0x40); } // read chips while (reader.PeekChar() >= 0) { if (new string(reader.ReadChars(4)) != "CHIP") { break; } var chipLength = ReadCRTInt(reader); chipType.Add(ReadCRTShort(reader)); chipBank.Add(ReadCRTShort(reader)); chipAddress.Add(ReadCRTShort(reader)); var chipDataLength = ReadCRTShort(reader); chipData.Add(reader.ReadBytes(chipDataLength).Select(x => (int)x).ToArray()); chipLength -= chipDataLength + 0x10; if (chipLength > 0) reader.ReadBytes(chipLength); } if (chipData.Count <= 0) { return null; } CartridgeDevice result; switch (mapper) { case 0x0000: // Standard Cartridge result = new Mapper0000(chipAddress, chipData, game, exrom); break; case 0x0001: // Action Replay (4.2 and up) result = new Mapper0001(chipAddress, chipBank, chipData); break; case 0x0005: // Ocean result = new Mapper0005(chipAddress, chipBank, chipData); break; case 0x000A: // Epyx FastLoad result = new Mapper000A(chipData); break; case 0x000B: // Westermann Learning result = new Mapper000B(chipAddress, chipData); break; case 0x000F: // C64 Game System / System 3 result = new Mapper000F(chipAddress, chipBank, chipData); break; case 0x0011: // Dinamic result = new Mapper0011(chipAddress, chipBank, chipData); break; case 0x0012: // Zaxxon / Super Zaxxon result = new Mapper0012(chipAddress, chipBank, chipData); break; case 0x0013: // Domark result = new Mapper0013(chipAddress, chipBank, chipData); break; case 0x0020: // EasyFlash result = new Mapper0020(chipAddress, chipBank, chipData); break; case 0x002B: // Prophet 64 result = new Mapper002B(chipAddress, chipBank, chipData); break; default: throw new Exception("This cartridge file uses an unrecognized mapper: " + mapper); } result.HardReset(); return result; } }
// --------------------------------- public static CartridgeDevice Load(byte[] crtFile) { var mem = new MemoryStream(crtFile); var reader = new BinaryReader(mem); if (new string(reader.ReadChars(16)) != "C64 CARTRIDGE ") { return null; } var chipAddress = new List<int>(); var chipBank = new List<int>(); var chipData = new List<int[]>(); var chipType = new List<int>(); var headerLength = ReadCRTInt(reader); var version = ReadCRTShort(reader); var mapper = ReadCRTShort(reader); var exrom = reader.ReadByte() != 0; var game = reader.ReadByte() != 0; // reserved reader.ReadBytes(6); // cartridge name reader.ReadBytes(0x20); // skip extra header bytes if (headerLength > 0x40) { reader.ReadBytes(headerLength - 0x40); } // read chips while (reader.PeekChar() >= 0) { if (new string(reader.ReadChars(4)) != "CHIP") { break; } var chipLength = ReadCRTInt(reader); chipType.Add(ReadCRTShort(reader)); chipBank.Add(ReadCRTShort(reader)); chipAddress.Add(ReadCRTShort(reader)); var chipDataLength = ReadCRTShort(reader); chipData.Add(reader.ReadBytes(chipDataLength).Select(x => (int)x).ToArray()); chipLength -= chipDataLength + 0x10; if (chipLength > 0) reader.ReadBytes(chipLength); } if (chipData.Count <= 0) { return null; } CartridgeDevice result; switch (mapper) { case 0x0000: result = new Mapper0000(chipAddress, chipBank, chipData, game, exrom); break; case 0x0005: result = new Mapper0005(chipAddress, chipBank, chipData); break; case 0x000B: result = new Mapper000B(chipAddress, chipBank, chipData); break; case 0x000F: result = new Mapper000F(chipAddress, chipBank, chipData); break; case 0x0011: result = new Mapper0011(chipAddress, chipBank, chipData); break; case 0x0012: result = new Mapper0012(chipAddress, chipBank, chipData); break; case 0x0013: result = new Mapper0013(chipAddress, chipBank, chipData); break; case 0x0020: result = new Mapper0020(chipAddress, chipBank, chipData); break; default: throw new Exception("This cartridge file uses an unrecognized mapper: " + mapper); } result.HardReset(); return result; }