Exemplo n.º 1
0
        private static void CheckRAMAdapter(FamicomDumperConnection dumper)
        {
            // Just simple test that RAM adapter is connected
            bool ramAdapterPresent = true;

            dumper.WriteCpu(0x4023, 0x01);       // enable disk registers
            dumper.WriteCpu(0x4026, 0x00);
            dumper.WriteCpu(0x4025, 0b00100110); // reset
            dumper.WriteCpu(0x0000, 0xFF);       // to prevent open bus read
            var ext = dumper.ReadCpu(0x4033);

            if (ext != 0x00)
            {
                ramAdapterPresent = false;
            }
            dumper.WriteCpu(0x4026, 0xFF);
            dumper.WriteCpu(0x0000, 0x00); // to prevent open bus read
            ext = dumper.ReadCpu(0x4033);
            if ((ext & 0x7F) != 0x7F)
            {
                ramAdapterPresent = false;
            }
            if (!ramAdapterPresent)
            {
                throw new IOException("RAM adapter IO error, is it connected?");
            }
        }
Exemplo n.º 2
0
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            //dumper.WritePrg(0xFFC9, 1);
            dumper.WriteCpu((ushort)(0x5000), (byte)0x00);
            dumper.WriteCpu((ushort)(0x5001), (byte)0x08);
            dumper.WriteCpu((ushort)(0x5002), (byte)0xF8);
            dumper.WriteCpu((ushort)(0x5003), (byte)0x00);
            dumper.WriteCpu((ushort)(0x5004), (byte)0x00);
            dumper.WriteCpu((ushort)(0x5005), (byte)0x00);
            dumper.WriteCpu((ushort)(0x5006), (byte)0x02);
            dumper.WriteCpu((ushort)(0x5007), (byte)0x82);

            byte banks = (byte)(size / 0x4000);

            Console.Write("Reading last PRG bank... ");
            byte[] lastBank = dumper.ReadCpu(0xC000, 0x4000);
            Console.WriteLine("OK");
            for (int bank = 0; bank < banks - 1; bank++)
            {
                Console.Write("Reading PRG bank #{0}... ", bank);
                // Avoiding bus conflicts
                for (int i = 0; i < lastBank.Length; i++)
                {
                    if (lastBank[i] == bank)
                    {
                        dumper.WriteCpu((ushort)(0xC000 + i), (byte)bank);
                        break;
                    }
                }
                data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
                Console.WriteLine("OK");
            }
            data.AddRange(lastBank);
        }
Exemplo n.º 3
0
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            byte banks = (byte)(size / 0x4000);

            Console.Write("Reading last PRG bank... ");
            byte[] lastBank = dumper.ReadCpu(0xC000, 0x4000);
            Console.WriteLine("OK");
            for (int bank = 0; bank < banks - 1; bank++)
            {
                Console.Write("Reading PRG bank #{0}... ", bank);
                // Avoiding bus conflicts
                bool noBusConflict = false;
                for (int i = 0; i < lastBank.Length; i++)
                {
                    if (lastBank[i] == bank)
                    {
                        dumper.WriteCpu((ushort)(0xC000 + i), (byte)bank);
                        noBusConflict = true;
                        break;
                    }
                }
                if (!noBusConflict) // Whatever...
                {
                    dumper.WriteCpu((ushort)0x8000, (byte)bank);
                }
                data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
                Console.WriteLine("OK");
            }
            data.AddRange(lastBank);
        }
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            dumper.Reset();
            int outbanks = size / (128 * 1024);

            int outbankSize = 512;

            for (int outbank = 0; outbank < outbanks; outbank += outbankSize / 128)
            {
                byte r0 = (byte)((outbank & 0x07) | ((outbank & 0xc0) >> 2));
                byte r1 = (byte)(((outbank & 0x30) >> 2) | ((outbank << 1) & 0x10));
                byte r2 = 0;
                byte r3 = 0;
                dumper.WriteCpu(0x6000, new byte[] { r0 });
                dumper.WriteCpu(0x6001, new byte[] { r1 });
                dumper.WriteCpu(0x6002, new byte[] { r2 });
                dumper.WriteCpu(0x6003, new byte[] { r3 });

                int banks = outbankSize * 1024 / 0x2000;
                for (int bank = 0; bank < banks - 2; bank += 2)
                {
                    Console.Write("Reading PRG banks #{2}|{0} and #{2}|{1}... ", bank, bank + 1, outbank);
                    dumper.WriteCpu(0x8000, new byte[] { 6, (byte)(bank) });
                    dumper.WriteCpu(0x8000, new byte[] { 7, (byte)(bank | 1) });
                    data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
                    Console.WriteLine("OK");
                }
                Console.Write("Reading last PRG banks #{2}|{0} and #{2}|{1}... ", banks - 2, banks - 1, outbank);
                data.AddRange(dumper.ReadCpu(0xC000, 0x4000));
                Console.WriteLine("OK");
            }
        }
Exemplo n.º 5
0
        public static void PasswordProgramm(FamicomDumperConnection dumper, byte[] password)
        {
            if (password.Length != 8)
            {
                throw new InvalidDataException("Invalid password length");
            }
            Console.Write("Programming password... ");
            // Password Protection Set Entry
            dumper.WriteCpu(0x8AAA, 0xAA);
            dumper.WriteCpu(0x8555, 0x55);
            dumper.WriteCpu(0x8AAA, 0x60);
            try
            {
                for (byte i = 0; i < password.Length; i++)
                {
                    dumper.WriteCpu(0x8000, 0xA0);
                    dumper.WriteCpu((ushort)(0x8000 + i), password[i]);
                }
                var verify = dumper.ReadCpu(0x8000, 8);
                for (byte i = 0; i < password.Length; i++)
                {
                    if (password[i] != verify[i])
                    {
                        throw new InvalidDataException("Password verification failed");
                    }
                }
            }
            finally
            {
                ResetFlash(dumper);
            }
            Console.WriteLine("OK");

            Console.Write("Programming lock register... ");
            // Lock Register Set Entry
            dumper.WriteCpu(0x8AAA, 0xAA);
            dumper.WriteCpu(0x8555, 0x55);
            dumper.WriteCpu(0x8AAA, 0x40);
            try
            {
                // Bits Program
                dumper.WriteCpu(0x8000, 0xA0);
                dumper.WriteCpu(0x8000, (byte)(1 << 2) ^ 0xFF); // password protection
                var r = dumper.ReadCpu(0x8000);
                if ((r & 7) != 3)
                {
                    throw new InvalidDataException("Lock bit verification failed");
                }
            }
            finally
            {
                ResetFlash(dumper);
            }
            Console.WriteLine("OK");
        }
Exemplo n.º 6
0
 public static void LockBitsCheckPrint(FamicomDumperConnection dumper)
 {
     try
     {
         // Lock Register Set Entry
         dumper.WriteCpu(0x8AAA, 0xAA);
         dumper.WriteCpu(0x8555, 0x55);
         dumper.WriteCpu(0x8AAA, 0x40);
         var lockRegister = dumper.ReadCpu(0x8000);
         if ((lockRegister & 1) == 0)
         {
             Console.WriteLine("WARNING: Secured Silicon Sector Protection Bit is set!");
         }
         if ((lockRegister & 2) == 0)
         {
             Console.WriteLine("WARNING: Persistent Protection Mode Lock Bit is set!");
         }
         if ((lockRegister & 4) == 0)
         {
             Console.WriteLine("WARNING: Password Protection Mode Lock Bit is set!");
         }
     }
     finally
     {
         ResetFlash(dumper);
     }
 }
Exemplo n.º 7
0
 public static void PPBSet(FamicomDumperConnection dumper)
 {
     Console.Write("Writing PPB for sector... ");
     // PPB Command Set Entry
     dumper.WriteCpu(0x8AAA, 0xAA);
     dumper.WriteCpu(0x8555, 0x55);
     dumper.WriteCpu(0x8AAA, 0xC0);
     // PPB Program
     dumper.WriteCpu(0x8000, 0xA0);
     dumper.WriteCpu(0x8000, 0x00);
     // Check
     try
     {
         DateTime startTime = DateTime.Now;
         while (true)
         {
             byte b = dumper.ReadCpu(0x8000);
             if (b == 0x00)
             {
                 break;
             }
             if ((DateTime.Now - startTime).TotalMilliseconds >= 1500)
             {
                 throw new IOException("PPB write failed");
             }
         }
     }
     finally
     {
         ResetFlash(dumper);
     }
     Console.WriteLine("OK");
 }
Exemplo n.º 8
0
 public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
 {
     Console.Write("Dumping PRG... ");
     prg = dumper.ReadCpu(0x8000, size);
     data.AddRange(prg);
     Console.WriteLine("OK");
 }
Exemplo n.º 9
0
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            dumper.WriteCpu((ushort)(0x5000), (byte)0x00);
            dumper.WriteCpu((ushort)(0x5001), (byte)0x10);
            dumper.WriteCpu((ushort)(0x5002), (byte)0x10);
            dumper.WriteCpu((ushort)(0x5006), (byte)0x07);
            dumper.WriteCpu((ushort)(0x5007), (byte)0x03);
            byte banks = (byte)(size / 0x8000);

            for (int bank = 0; bank < banks; bank++)
            {
                Console.Write("Reading PRG bank #{0}... ", bank);
                // TODO: избежать конфликтов шины
                // Avoiding bus conflicts

                /*
                 * for (int i = 0; i < lastBank.Length; i++)
                 * {
                 *  if (lastBank[i] == bank)
                 *  {
                 *      break;
                 *  }
                 * }
                 */
                dumper.WriteCpu((ushort)(0x8000), (byte)bank);
                data.AddRange(dumper.ReadCpu(0x8000, 0x8000));
                Console.WriteLine("OK");
            }
        }
Exemplo n.º 10
0
 public static void PPBClear(FamicomDumperConnection dumper)
 {
     LockBitsCheckPrint(dumper);
     PPBLockBitCheckPrint(dumper);
     Console.Write($"Erasing all PBBs... ");
     // PPB Command Set Entry
     dumper.WriteCpu(0x8AAA, 0xAA);
     dumper.WriteCpu(0x8555, 0x55);
     dumper.WriteCpu(0x8AAA, 0xC0);
     // All PPB Erase
     dumper.WriteCpu(0x8000, 0x80);
     dumper.WriteCpu(0x8000, 0x30);
     // Check
     try
     {
         DateTime startTime = DateTime.Now;
         while (true)
         {
             byte b = dumper.ReadCpu(0x8000);
             if (b == 0x01)
             {
                 break;
             }
             if ((DateTime.Now - startTime).TotalMilliseconds >= 1500)
             {
                 throw new IOException("PPB clear failed");
             }
         }
     }
     finally
     {
         ResetFlash(dumper);
     }
     Console.WriteLine("OK");
 }
Exemplo n.º 11
0
 public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
 {
     //dumper.WritePrg((ushort)(0x5001), (byte)0x02);
     Console.Write("Reading PRG... ");
     data.AddRange(dumper.ReadCpu(0x8000, size));
     Console.WriteLine("OK");
 }
Exemplo n.º 12
0
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            byte banks = (byte)(size / 0x2000);

            for (byte bank = 0; bank < banks - 2; bank += 2)
            {
                Console.Write("Reading PRG banks #{0} and #{1}... ", bank, bank + 1);
                dumper.WriteCpu(0x8000, new byte[] { 6, bank });
                dumper.WriteCpu(0x8000, new byte[] { 7, (byte)(bank | 1) });
                data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
                Console.WriteLine("OK");
            }
            Console.Write("Reading last PRG banks #{0} and #{1}... ", banks - 2, banks - 1);
            data.AddRange(dumper.ReadCpu(0xC000, 0x4000));
            Console.WriteLine("OK");
        }
Exemplo n.º 13
0
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            dumper.Reset();
            version = CoolboyWriter.DetectVersion(dumper);
            UInt16 coolboyReg = (UInt16)(version == 2 ? 0x5000 : 0x6000);
            int    banks      = size / 0x4000;

            for (int bank = 0; bank < banks; bank++)
            {
                byte r0 = (byte)(((bank >> 3) & 0x07)          // 5, 4, 3 bits
                                 | (((bank >> 9) & 0x03) << 4) // 10, 9 bits
                                 | (1 << 6));                  // resets 4th mask bit
                byte r1 = (byte)((((bank >> 7) & 0x03) << 2)   // 8, 7
                                 | (((bank >> 6) & 1) << 4)    // 6
                                 | (1 << 7));                  // resets 5th mask bit
                byte r2 = 0;
                byte r3 = (byte)((1 << 4)                      // NROM mode
                                 | ((bank & 7) << 1));         // 2, 1, 0 bits
                dumper.WriteCpu(coolboyReg, new byte[] { r0, r1, r2, r3 });

                Console.Write("Reading PRG banks #{0}/{1}... ", bank, banks);
                data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
                Console.WriteLine("OK");
            }
        }
Exemplo n.º 14
0
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            dumper.Reset();
            int banks = size / 0x4000;

            for (int bank = 0; bank < banks; bank++)
            {
                byte r0 = (byte)(((bank >> 3) & 0x07)          // 5, 4, 3 bits
                                 | (((bank >> 9) & 0x03) << 4) // 10, 9 bits
                                 | (1 << 6));                  // resets 4th mask bit
                byte r1 = (byte)((((bank >> 7) & 0x03) << 2)   // 8, 7
                                 | (((bank >> 6) & 1) << 4)    // 6
                                 | (1 << 7));                  // resets 5th mask bit
                byte r2 = 0;
                byte r3 = (byte)((1 << 4)                      // NROM mode
                                 | ((bank & 7) << 1));         // 2, 1, 0 bits
                dumper.WriteCpu(0x6000, new byte[] { r0 });
                dumper.WriteCpu(0x6001, new byte[] { r1 });
                dumper.WriteCpu(0x6002, new byte[] { r2 });
                dumper.WriteCpu(0x6003, new byte[] { r3 });

                Console.Write("Reading PRG banks #{0}/{1}... ", bank, banks);
                data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
                Console.WriteLine("OK");
            }
        }
Exemplo n.º 15
0
        public static void DumpFDS(FamicomDumperConnection dumper, string fileName, byte sides = 1, bool dumpHiddenFiles = true, bool useHeader = true)
        {
            if (dumper.ProtocolVersion < 3)
            {
                throw new NotSupportedException("Dumper firmware version is too old, update it to read/write FDS cards");
            }
            CheckRAMAdapter(dumper);

            var sideImages = new List <FdsDiskSide>();

            for (int side = 1; side <= sides; side++)
            {
                var driveStatus = dumper.ReadCpu(0x4032);
                if ((driveStatus & 1) != 0)
                {
                    Console.Write($"Please set disk card, side #{side}... ");
                    while ((driveStatus & 1) != 0)
                    {
                        Thread.Sleep(100);
                        driveStatus = dumper.ReadCpu(0x4032);
                    }
                    Console.WriteLine("OK");
                }
                var sideImage = DumpFDSSide(dumper, dumpHiddenFiles, printDiskInfo: true);
                sideImages.Add(sideImage);

                if (side < sides)
                {
                    driveStatus = dumper.ReadCpu(0x4032);
                    if ((driveStatus & 1) == 0)
                    {
                        Console.Write($"Please remove disk card... ");
                        while ((driveStatus & 1) == 0)
                        {
                            Thread.Sleep(100);
                            driveStatus = dumper.ReadCpu(0x4032);
                        }
                        Console.WriteLine("OK");
                    }
                }
            }
            Console.Write($"Saving to {fileName}... ");
            var fdsImage = new FdsFile(sideImages);

            fdsImage.Save(fileName, useHeader);
            Console.WriteLine("OK");
        }
Exemplo n.º 16
0
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            dumper.WriteCpu(0x8000, 0x80);
            WriteMMC1(dumper, 0x8000, 0x0C);

            byte banks = (byte)(size / 0x4000);

            for (byte bank = 0; bank < banks - 1; bank++)
            {
                Console.Write("Reading PRG bank #{0}... ", bank);
                WriteMMC1(dumper, 0xE000, bank);
                data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
                Console.WriteLine("OK");
            }
            if (banks > 0)
            {
                Console.Write("Reading last PRG bank #{0}... ", banks - 1);
                data.AddRange(dumper.ReadCpu(0xC000, 0x4000));
                Console.WriteLine("OK");
            }
        }
Exemplo n.º 17
0
        static void ReadPrgRam(FamicomDumperConnection dumper, string fileName, string mapperName)
        {
            var mapper = GetMapper(mapperName);

            if (mapper.Number >= 0)
            {
                Console.WriteLine($"Using mapper: #{mapper.Number} ({mapper.Name})");
            }
            else
            {
                Console.WriteLine($"Using mapper: {mapper.Name}");
            }
            mapper.EnablePrgRam(dumper);
            Console.Write("Reading PRG RAM... ");
            var prgram = dumper.ReadCpu(0x6000, 0x2000);

            Console.WriteLine("OK");
            Console.Write($"Saving to {fileName}... ");
            File.WriteAllBytes(fileName, prgram);
            Console.WriteLine("OK");
            dumper.ReadCpu(0x0, 1); // to avoid corruption
            Reset(dumper);
        }
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            byte outbanks = (byte)(size / (256 * 1024));

            for (byte outbank = 0; outbank < outbanks; outbank += 1)
            {
                dumper.Reset();
                dumper.WriteCpu(0xA001, 0x80); // RAM protect
                dumper.WriteCpu((ushort)(0x6828 | (outbank << 1)), 0x00);
                dumper.WriteCpu(0xA001, 0);    // disable W-RAM
                byte banks = 32;               //(byte)(size / 0x2000);
                for (byte bank = 0; bank < banks - 2; bank += 2)
                {
                    Console.Write("Reading PRG banks #{2}|{0} and #{2}|{1}... ", bank, bank + 1, outbank);
                    dumper.WriteCpu(0x8000, new byte[] { 6, bank });
                    dumper.WriteCpu(0x8000, new byte[] { 7, (byte)(bank | 1) });
                    data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
                    Console.WriteLine("OK");
                }
                Console.Write("Reading last PRG banks #{2}|{0} and #{2}|{1}... ", banks - 2, banks - 1, outbank);
                data.AddRange(dumper.ReadCpu(0xC000, 0x4000));
                Console.WriteLine("OK");
            }
        }
Exemplo n.º 19
0
 public static byte PPBRead(FamicomDumperConnection dumper)
 {
     try
     {
         // PPB Command Set Entry
         dumper.WriteCpu(0x8AAA, 0xAA);
         dumper.WriteCpu(0x8555, 0x55);
         dumper.WriteCpu(0x8AAA, 0xC0);
         // PPB Status Read
         return(dumper.ReadCpu(0x8000));
     }
     finally
     {
         ResetFlash(dumper);
     }
 }
Exemplo n.º 20
0
        static void TestBattery(FamicomDumperConnection dumper, string mapperName)
        {
            var mapper = GetMapper(mapperName);

            if (mapper.Number >= 0)
            {
                Console.WriteLine($"Using mapper: #{mapper.Number} ({mapper.Name})");
            }
            else
            {
                Console.WriteLine($"Using mapper: {mapper.Name}");
            }
            mapper.EnablePrgRam(dumper);
            var rnd  = new Random();
            var data = new byte[0x2000];

            rnd.NextBytes(data);
            Console.Write("Writing PRG RAM... ");
            dumper.WriteCpu(0x6000, data);
            Reset(dumper);
            Console.WriteLine("Replug cartridge and press any key");
            Console.ReadKey();
            Console.WriteLine();
            mapper.EnablePrgRam(dumper);
            Console.Write("Reading PRG RAM... ");
            var  rdata = dumper.ReadCpu(0x6000, 0x2000);
            bool ok    = true;

            for (int b = 0; b < 0x2000; b++)
            {
                if (data[b] != rdata[b])
                {
                    Console.WriteLine($"Mismatch at {b:X4}: {rdata[b]:X2} != {data[b]:X2}");
                    ok = false;
                }
            }
            if (ok)
            {
                Console.WriteLine("OK!");
            }
            else
            {
                throw new VerificationException("Failed!");
            }
        }
Exemplo n.º 21
0
 public static CFIInfo GetCFIInfo(FamicomDumperConnection dumper)
 {
     try
     {
         dumper.WriteCpu(0x8AAA, 0x98); // CFI mode
         var cfiRaw = dumper.ReadCpu(0x8000, 0x100);
         if (cfiRaw[0x20] != 0x51 || cfiRaw[0x22] != 0x52 || cfiRaw[0x24] != 0x59)
         {
             throw new IOException("Can't enter CFI mode. Invalid flash memory? Broken cartridge? Is it inserted?");
         }
         var cfi = new CFIInfo(cfiRaw, CFIInfo.ParseMode.Every2Bytes);
         return(cfi);
     }
     finally
     {
         dumper.WriteCpu(0x8000, 0xF0);
     }
 }
Exemplo n.º 22
0
        static void TestPrgRam(FamicomDumperConnection dumper, string mapperName, int count = -1)
        {
            var mapper = GetMapper(mapperName);

            if (mapper.Number >= 0)
            {
                Console.WriteLine($"Using mapper: #{mapper.Number} ({mapper.Name})");
            }
            else
            {
                Console.WriteLine($"Using mapper: {mapper.Name}");
            }
            mapper.EnablePrgRam(dumper);
            var rnd = new Random();

            while (count != 0)
            {
                var data = new byte[0x2000];
                rnd.NextBytes(data);
                Console.Write("Writing PRG RAM... ");
                dumper.WriteCpu(0x6000, data);
                Console.Write("Reading PRG RAM... ");
                var  rdata = dumper.ReadCpu(0x6000, 0x2000);
                bool ok    = true;
                for (int b = 0; b < 0x2000; b++)
                {
                    if (data[b] != rdata[b])
                    {
                        Console.WriteLine($"Mismatch at {b:X4}: {rdata[b]:X2} != {data[b]:X2}");
                        ok = false;
                    }
                }
                if (!ok)
                {
                    File.WriteAllBytes("prgramgood.bin", data);
                    Console.WriteLine("prgramgood.bin writed");
                    File.WriteAllBytes("prgrambad.bin", rdata);
                    Console.WriteLine("prgrambad.bin writed");
                    throw new VerificationException("Failed!");
                }
                Console.WriteLine("OK!");
                count--;
            }
        }
Exemplo n.º 23
0
        public static void TestPrgRam(FamicomDumperConnection dumper, int count = -1)
        {
            Program.Reset(dumper);
            dumper.WriteCpu(0x5007, 0x01); // enable PRG RAM
            var rnd = new Random();

            while (count != 0)
            {
                var data = new byte[][] { new byte[0x2000], new byte[0x2000], new byte[0x2000], new byte[0x2000] };
                for (byte bank = 0; bank < 4; bank++)
                {
                    Console.WriteLine($"Writing PRG RAM, bank #{bank}/{4}... ");
                    rnd.NextBytes(data[bank]);
                    dumper.WriteCpu(0x5005, bank);
                    dumper.WriteCpu(0x6000, data[bank]);
                }
                for (byte bank = 0; bank < 4; bank++)
                {
                    Console.Write($"Reading PRG RAM, bank #{bank}/{4}... ");
                    dumper.WriteCpu(0x5005, bank);
                    var  rdata = dumper.ReadCpu(0x6000, 0x2000);
                    bool ok    = true;
                    for (int b = 0; b < 0x2000; b++)
                    {
                        if (data[bank][b] != rdata[b])
                        {
                            Console.WriteLine($"Mismatch at {b:X4}: {rdata[b]:X2} != {data[bank][b]:X2}");
                            ok = false;
                        }
                    }
                    if (!ok)
                    {
                        File.WriteAllBytes("prgramgood.bin", data[bank]);
                        Console.WriteLine("prgramgood.bin writed");
                        File.WriteAllBytes("prgrambad.bin", rdata);
                        Console.WriteLine("prgrambad.bin writed");
                        throw new IOException("Test failed");
                    }
                    Console.WriteLine("OK");
                }
                count--;
            }
        }
Exemplo n.º 24
0
 public static void PPBLockBitCheckPrint(FamicomDumperConnection dumper)
 {
     try
     {
         // PPB Lock Command Set Entry
         dumper.WriteCpu(0x8AAA, 0xAA);
         dumper.WriteCpu(0x8555, 0x55);
         dumper.WriteCpu(0x8AAA, 0x50);
         var ppbLockStatus = dumper.ReadCpu(0x8000);
         if (ppbLockStatus == 0)
         {
             Console.WriteLine("WARNING: PPB Lock Bit is set!");
         }
     }
     finally
     {
         ResetFlash(dumper);
     }
 }
Exemplo n.º 25
0
        public void DumpPrg(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            int prgBanks = size / 0x8000;

            Console.Write("Reset... ");
            dumper.Reset();
            Console.WriteLine("OK");
            dumper.WriteCpu(0x5002, 0xFE); // mask = 8K
            for (int bank = 0; bank < prgBanks; bank++)
            {
                byte r0 = (byte)(bank >> 7);
                byte r1 = (byte)(bank << 1);
                dumper.WriteCpu(0x5000, r0);
                dumper.WriteCpu(0x5001, r1);

                Console.Write("Reading PRG bank #{0}/{1}... ", bank, prgBanks);
                data.AddRange(dumper.ReadCpu(0x8000, 0x8000));
                Console.WriteLine("OK");
            }
            Console.WriteLine("Done!");
        }
Exemplo n.º 26
0
        public void DumpChr(FamicomDumperConnection dumper, List <byte> data, int size)
        {
            if (prg == null)
            {
                prg = dumper.ReadCpu(0x8000, DefaultPrgSize);
            }
            byte banks = (byte)(size / 0x2000);

            for (int bank = 0; bank < banks; bank++)
            {
                Console.Write("Dumping CHR bank #{0}... ", bank);
                for (int i = 0; i < prg.Length; i++)
                {
                    if (prg[i] == bank)
                    {
                        dumper.WriteCpu((ushort)(0x8000 + i), (byte)bank);
                        break;
                    }
                }
                byte[] d = dumper.ReadPpu(0x0000, 0x2000);
                data.AddRange(d);
                Console.WriteLine("OK");
            }
        }
Exemplo n.º 27
0
        public static void FindBads(FamicomDumperConnection dumper, bool silent)
        {
            Program.Reset(dumper);
            dumper.WriteCpu(0x5007, 0x04); // enable PRG write
            dumper.WriteCpu(0x5002, 0xFE); // mask = 32K
            try
            {
                PPBClear(dumper);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}. Lets try anyway.");
            }
            dumper.WriteCpu(0x5000, 0);
            dumper.WriteCpu(0x5001, 0);
            var cfi = FlashHelper.GetCFIInfo(dumper);

            Console.WriteLine($"Device size: {cfi.DeviceSize / 1024 / 1024} MByte / {cfi.DeviceSize / 1024 / 1024 * 8} Mbit");
            uint banks = cfi.DeviceSize / BANK_SIZE;

            FlashHelper.LockBitsCheckPrint(dumper);

            Console.Write("Erasing sector #0... ");
            dumper.EraseCpuFlashSector();
            Console.WriteLine("OK");
            var data = new byte[BANK_SIZE];

            new Random().NextBytes(data);
            Console.Write("Writing sector #0 for test... ");
            dumper.WriteCpuFlash(0x0000, data);
            Console.WriteLine("OK");
            Console.Write("Reading sector #0 for test... ");
            var datar = dumper.ReadCpu(0x8000, BANK_SIZE);

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] != datar[i])
                {
                    throw new VerificationException("Check failed");
                }
            }
            Console.WriteLine("OK");

            var writeStartTime = DateTime.Now;
            var lastSectorTime = DateTime.Now;
            var timeEstimated  = new TimeSpan();
            var badSectors     = new List <int>();

            for (int bank = 0; bank < banks; bank += 4)
            {
                byte r0 = (byte)(bank >> 7);
                byte r1 = (byte)(bank << 1);
                dumper.WriteCpu(0x5000, r0);
                dumper.WriteCpu(0x5001, r1);

                timeEstimated  = new TimeSpan((DateTime.Now - lastSectorTime).Ticks * (banks - bank) / 4);
                timeEstimated  = timeEstimated.Add(DateTime.Now - writeStartTime);
                lastSectorTime = DateTime.Now;
                var timePassed = DateTime.Now - writeStartTime;
                Console.Write($"Erasing sector #{bank / 4}/{banks / 4} ({100 * bank / banks}%, {timePassed.Hours:D2}:{timePassed.Minutes:D2}:{timePassed.Seconds:D2}/{timeEstimated.Hours:D2}:{timeEstimated.Minutes:D2}:{timeEstimated.Seconds:D2})... ");
                try
                {
                    dumper.EraseCpuFlashSector();
                    Console.WriteLine("OK");
                }
                catch
                {
                    Console.WriteLine("ERROR!");
                    if (!silent)
                    {
                        Program.PlayErrorSound();
                    }
                    Console.Write("Trying again... ");
                    Program.Reset(dumper);
                    dumper.WriteCpu(0x5007, 0x04); // enable PRG write
                    dumper.WriteCpu(0x5002, 0xFE); // mask = 32K
                    dumper.WriteCpu(0x5000, r0);
                    dumper.WriteCpu(0x5001, r1);
                    try
                    {
                        dumper.EraseCpuFlashSector();
                        Console.WriteLine("OK");
                    }
                    catch
                    {
                        Console.WriteLine($"ERROR! Sector #{bank / 4} is bad.");
                        if (!silent)
                        {
                            Program.PlayErrorSound();
                        }
                        badSectors.Add(bank / 4);
                    }
                }
            }
            if (badSectors.Count > 0)
            {
                foreach (var bad in badSectors)
                {
                    Console.WriteLine($"Bad sector: {bad}");
                }
                throw new IOException("Bad sectors found");
            }
            else
            {
                Console.WriteLine("There is no bad sectors");
            }
        }
Exemplo n.º 28
0
 public LuaMapper()
 {
     script = new Script();
     script.Globals["ReadPrg"] = script.Globals["ReadCpu"] = (Func <UInt16, int, List <byte> >) delegate(UInt16 address, int length)
     {
         if (Verbose)
         {
             Console.WriteLine("Reading {0} bytes from CPU:${1:X4}", length, address);
         }
         var result = new List <byte>();
         result.AddRange(dumper.ReadCpu(address, length));
         return(result);
     };
     script.Globals["WritePrg"] = script.Globals["WriteCpu"] = (Action <UInt16, List <byte> >) delegate(UInt16 address, List <byte> data)
     {
         if (Verbose)
         {
             var a = address;
             foreach (var v in data)
             {
                 Console.WriteLine("CPU write ${0:X2} => ${1:X4}", v, a);
                 a++;
             }
         }
         dumper.WriteCpu(address, data.ToArray());
     };
     script.Globals["AddPrg"] = script.Globals["AddPrgResult"] = (Action <List <byte> >) delegate(List <byte> r)
     {
         resultPrg.AddRange(r);
     };
     script.Globals["ReadAddPrg"] = script.Globals["ReadAddCpu"] = (Action <UInt16, int>) delegate(UInt16 address, int length)
     {
         if (Verbose)
         {
             Console.WriteLine("Reading {0} bytes from CPU:${1:X4}", length, address);
         }
         resultPrg.AddRange(dumper.ReadCpu(address, length));
     };
     script.Globals["ReadChr"] = script.Globals["ReadPpu"] = (Func <UInt16, int, List <byte> >) delegate(UInt16 address, int length)
     {
         if (Verbose)
         {
             Console.WriteLine("Reading {0} bytes from PPU:${1:X4}", length, address);
         }
         var result = new List <byte>();
         result.AddRange(dumper.ReadPpu(address, length));
         return(result);
     };
     script.Globals["WriteChr"] = script.Globals["WritePpu"] = (Action <UInt16, List <byte> >) delegate(UInt16 address, List <byte> data)
     {
         if (Verbose)
         {
             var a = address;
             foreach (var v in data)
             {
                 Console.WriteLine("PPU write ${0:X2} => ${1:X4}", v, a);
                 a++;
             }
         }
         dumper.WritePpu(address, data.ToArray());
     };
     script.Globals["ReadAddChr"] = script.Globals["ReadAddPpu"] = (Action <UInt16, int>) delegate(UInt16 address, int length)
     {
         if (Verbose)
         {
             Console.WriteLine("Reading {0} bytes from PPU:${1:$X4}", length, address);
         }
         resultChr.AddRange(dumper.ReadPpu(address, length));
     };
     script.Globals["AddChr"] = script.Globals["AddChrResult"] = (Action <List <byte> >) delegate(List <byte> r)
     {
         resultChr.AddRange(r);
     };
     script.Globals["Reset"] = (Action) delegate
     {
         if (Verbose)
         {
             Console.Write("Reset... ");
         }
         dumper.Reset();
         if (Verbose)
         {
             Console.WriteLine("OK");
         }
     };
     script.Globals["WriteFile"] = (Action <string, List <byte> >) delegate(string filename, List <byte> data)
     {
         if (Verbose)
         {
             Console.Write("Writing data to \"{0}\"... ", Path.GetFileName(filename));
         }
         File.WriteAllBytes(filename, data.ToArray());
         if (Verbose)
         {
             Console.WriteLine("OK");
         }
     };
     script.Globals["WriteNes"] = (WriteNesDelegate) delegate(string filename, List <byte> prgData, List <byte> chrData, byte mapper, bool vertical)
     {
         if (Verbose)
         {
             Console.Write("Writing data to NES file \"{0}\" (mapper={1}, mirroring={2})... ", Path.GetFileName(filename), mapper, vertical ? "vertical" : "horizontal");
         }
         var nesFile = new NesFile();
         nesFile.PRG       = prgData.ToArray();
         nesFile.CHR       = chrData.ToArray();
         nesFile.Mapper    = 0;
         nesFile.Mirroring = vertical ? NesFile.MirroringType.Vertical : NesFile.MirroringType.Horizontal;
         nesFile.Save(filename);
         if (Verbose)
         {
             Console.WriteLine("OK");
         }
     };
     script.Globals["Error"] = (Action <string>) delegate(string message)
     {
         throw new Exception(message);
     };
 }
Exemplo n.º 29
0
        public static void WriteFDS(FamicomDumperConnection dumper, string fileName, bool needCheck = false)
        {
            if (dumper.ProtocolVersion < 3)
            {
                throw new NotSupportedException("Dumper firmware version is too old, update it to read/write FDS cards");
            }
            var oldTimeout = dumper.Timeout;

            try
            {
                dumper.Timeout = 30000;
                CheckRAMAdapter(dumper);

                var rom = new FdsFile(fileName);

                for (int sideNumber = 0; sideNumber < rom.Sides.Count; sideNumber++)
                {
                    var driveStatus = dumper.ReadCpu(0x4032);
                    if ((driveStatus & 1) != 0)
                    {
                        Console.Write($"Please set disk card, side #{sideNumber + 1}... ");
                        while ((driveStatus & 1) != 0)
                        {
                            Thread.Sleep(100);
                            driveStatus = dumper.ReadCpu(0x4032);
                        }
                        Console.WriteLine("OK");
                    }

                    PrintDiskHeaderInfo(rom.Sides[sideNumber].DiskInfoBlock);
                    Console.WriteLine($"Number of non-hidden files: {rom.Sides[sideNumber].FileAmount}");
                    Console.WriteLine($"Number of hidden files: {rom.Sides[sideNumber].Files.Count - rom.Sides[sideNumber].FileAmount}");
                    var blocks = rom.Sides[sideNumber].GetBlocks().ToArray();
                    Console.WriteLine($"Total blocks to write: {blocks.Length}");
                    byte blocksWrited = 0;
                    while (blocksWrited < blocks.Length)
                    {
                        uint totalSize     = 1;
                        var  blockIDs      = new List <byte>();
                        var  blocksToWrite = new List <IFdsBlock>();

                        for (byte i = blocksWrited; i < blocks.Count(); i++)
                        {
                            if (totalSize + blocks[i].Length + 3 <= dumper.MaxWritePacketSize)
                            {
                                blocksToWrite.Add(blocks[i]);
                                blockIDs.Add(i);
                                totalSize += blocks[i].Length + 3;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (!blocksToWrite.Any())
                        {
                            throw new OutOfMemoryException("Dumper has not enoght memory to write such big block");
                        }
                        Console.Write($"Writing block(s): {string.Join(", ", blockIDs)}... ");
                        dumper.WriteFdsBlocks(blockIDs.ToArray(), blocksToWrite.ToArray());
                        Console.WriteLine("OK");
                        blocksWrited += (byte)blocksToWrite.Count;
                    }

                    if (needCheck)
                    {
                        Console.WriteLine("Starting verification process");
                        var hiddenFiles = rom.Sides[sideNumber].Files.Count > rom.Sides[sideNumber].FileAmount;
                        var sideImage   = DumpFDSSide(dumper, dumpHiddenFiles: hiddenFiles, printDiskInfo: false);
                        if (!sideImage.DiskInfoBlock.Equals(rom.Sides[sideNumber].DiskInfoBlock))
                        {
                            throw new IOException("Disk info block verification failed");
                        }
                        if (!sideImage.FileAmount.Equals(rom.Sides[sideNumber].FileAmount))
                        {
                            throw new IOException("File amount block verification failed");
                        }
                        if (sideImage.Files.Count < rom.Sides[sideNumber].Files.Count)
                        {
                            throw new IOException($"Invalid file count: {sideImage.Files.Count} < {rom.Sides[sideNumber].Files.Count}");
                        }
                        for (int f = 0; f < rom.Sides[sideNumber].Files.Count; f++)
                        {
                            if (!sideImage.Files[f].HeaderBlock.Equals(rom.Sides[sideNumber].Files[f].HeaderBlock))
                            {
                                throw new IOException($"File #{f} header block verification failed");
                            }
                            if (!sideImage.Files[f].DataBlock.Equals(rom.Sides[sideNumber].Files[f].DataBlock))
                            {
                                throw new IOException($"File #{f} data block verification failed");
                            }
                        }
                        Console.WriteLine("Verification successful.");
                    }

                    if (sideNumber + 1 < rom.Sides.Count)
                    {
                        driveStatus = dumper.ReadCpu(0x4032);
                        if ((driveStatus & 1) == 0)
                        {
                            Console.Write($"Please remove disk card... ");
                            while ((driveStatus & 1) == 0)
                            {
                                Thread.Sleep(100);
                                driveStatus = dumper.ReadCpu(0x4032);
                            }
                            Console.WriteLine("OK");
                        }
                    }
                }
            }
            finally
            {
                dumper.Timeout = oldTimeout;
            }
        }