Пример #1
0
		// ---------------------------------

		static public Cart Load(byte[] crtFile)
		{
			Cart result = null;
			MemoryStream mem = new MemoryStream(crtFile);
			BinaryReader reader = new BinaryReader(mem);

			if (new string(reader.ReadChars(16)) == "C64 CARTRIDGE   ")
			{
				List<int> chipAddress = new List<int>();
				List<int> chipBank = new List<int>();
				List<byte[]> chipData = new List<byte[]>();
				List<int> chipType = new List<int>();

				int headerLength = ReadCRTInt(reader);
				int version = ReadCRTShort(reader);
				int mapper = ReadCRTShort(reader);
				bool exrom = (reader.ReadByte() != 0);
				bool 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")
					{
						int chipLength = ReadCRTInt(reader);
						chipType.Add(ReadCRTShort(reader));
						chipBank.Add(ReadCRTShort(reader));
						chipAddress.Add(ReadCRTShort(reader));
						int chipDataLength = ReadCRTShort(reader);
						chipData.Add(reader.ReadBytes(chipDataLength));
						chipLength -= (chipDataLength + 0x10);
						if (chipLength > 0)
							reader.ReadBytes(chipLength);
					}
				}

				if (chipData.Count > 0)
				{
					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;
		}
Пример #2
0
        // ---------------------------------

        static public Cart Load(byte[] crtFile)
        {
            Cart         result = null;
            MemoryStream mem    = new MemoryStream(crtFile);
            BinaryReader reader = new BinaryReader(mem);

            if (new string(reader.ReadChars(16)) == "C64 CARTRIDGE   ")
            {
                List <int>    chipAddress = new List <int>();
                List <int>    chipBank    = new List <int>();
                List <byte[]> chipData    = new List <byte[]>();
                List <int>    chipType    = new List <int>();

                int  headerLength = ReadCRTInt(reader);
                int  version      = ReadCRTShort(reader);
                int  mapper       = ReadCRTShort(reader);
                bool exrom        = (reader.ReadByte() != 0);
                bool 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")
                    {
                        int chipLength = ReadCRTInt(reader);
                        chipType.Add(ReadCRTShort(reader));
                        chipBank.Add(ReadCRTShort(reader));
                        chipAddress.Add(ReadCRTShort(reader));
                        int chipDataLength = ReadCRTShort(reader);
                        chipData.Add(reader.ReadBytes(chipDataLength));
                        chipLength -= (chipDataLength + 0x10);
                        if (chipLength > 0)
                        {
                            reader.ReadBytes(chipLength);
                        }
                    }
                }

                if (chipData.Count > 0)
                {
                    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);
        }