public override Task <ReadCrcResponse> ReadCpuCrc(ReadRequest request, ServerCallContext context) { var result = new ReadCrcResponse(); try { if (request.Length > 1) { Console.Write($"Reading CRC of 0x{request.Address:X4}-0x{request.Address + request.Length - 1:X4} @ CPU... "); } else { Console.Write($"Reading CRC of 0x{request.Address:X4} @ CPU... "); } result.Crc = dumper.ReadCpuCrc((ushort)request.Address, (ushort)request.Length); Console.WriteLine($"{result.Crc:X4}"); } catch (Exception ex) { PrintError(ex); result.ErrorInfo = new ErrorInfo() { ExceptionName = ex.GetType().ToString(), ExceptionMessage = ex.Message }; } return(Task.FromResult(result)); }
public static void Write(IFamicomDumperConnectionExt dumper, string fileName, IEnumerable <int> badSectors, bool silent, bool needCheck = false, bool writePBBs = false, bool ignoreBadSectors = false) { byte[] PRG; if (Path.GetExtension(fileName).ToLower() == ".bin") { PRG = File.ReadAllBytes(fileName); } else { try { var nesFile = new NesFile(fileName); PRG = nesFile.PRG.ToArray(); } catch { var nesFile = new UnifFile(fileName); PRG = nesFile.Fields["PRG0"]; } } int banks = PRG.Length / BANK_SIZE; Program.Reset(dumper); var version = DetectVersion(dumper); var coolboyReg = (ushort)(version == 2 ? 0x5000 : 0x6000); FlashHelper.ResetFlash(dumper); var cfi = FlashHelper.GetCFIInfo(dumper); Console.WriteLine($"Device size: {cfi.DeviceSize / 1024 / 1024} MByte / {cfi.DeviceSize / 1024 / 1024 * 8} Mbit"); Console.WriteLine($"Maximum number of bytes in multi-byte program: {cfi.MaximumNumberOfBytesInMultiProgram}"); if (dumper.ProtocolVersion >= 3) { dumper.SetMaximumNumberOfBytesInMultiProgram(cfi.MaximumNumberOfBytesInMultiProgram); } FlashHelper.LockBitsCheckPrint(dumper); if (PRG.Length > cfi.DeviceSize) { throw new ArgumentOutOfRangeException("PRG.Length", "This ROM is too big for this cartridge"); } try { PPBClear(dumper, coolboyReg); } catch (Exception ex) { if (!silent) { Program.PlayErrorSound(); } Console.WriteLine($"ERROR! {ex.Message}. Lets continue anyway."); } var writeStartTime = DateTime.Now; var lastSectorTime = DateTime.Now; var timeEstimated = new TimeSpan(); int totalErrorCount = 0; int currentErrorCount = 0; var newBadSectorsList = new List <int>(badSectors); for (int bank = 0; bank < banks; bank++) { while (badSectors.Contains(bank / 8) || newBadSectorsList.Contains(bank / 8)) { bank += 8; // bad sector :( } try { 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, r0, r1, r2, r3); var data = new byte[BANK_SIZE]; int pos = bank * BANK_SIZE; if (pos % (128 * 1024) == 0) { timeEstimated = new TimeSpan((DateTime.Now - lastSectorTime).Ticks * (banks - bank) / 8); timeEstimated = timeEstimated.Add(DateTime.Now - writeStartTime); lastSectorTime = DateTime.Now; Console.Write($"Erasing sector #{bank / 8}... "); dumper.EraseFlashSector(); Console.WriteLine("OK"); } Array.Copy(PRG, pos, data, 0, data.Length); var timePassed = DateTime.Now - writeStartTime; Console.Write($"Writing bank #{bank}/{banks} ({100 * bank / banks}%, {timePassed.Hours:D2}:{timePassed.Minutes:D2}:{timePassed.Seconds:D2}/{timeEstimated.Hours:D2}:{timeEstimated.Minutes:D2}:{timeEstimated.Seconds:D2})... "); dumper.WriteFlash(0x0000, data); Console.WriteLine("OK"); if ((bank % 8 == 7) || (bank == banks - 1)) // After last bank in sector { if (writePBBs) { FlashHelper.PPBSet(dumper); } currentErrorCount = 0; } } catch (Exception ex) { totalErrorCount++; currentErrorCount++; Console.WriteLine($"Error {ex.GetType()}: {ex.Message}"); if (!silent) { Program.PlayErrorSound(); } if (currentErrorCount >= 5) { if (!ignoreBadSectors) { throw; } else { newBadSectorsList.Add(bank / 8); currentErrorCount = 0; Console.WriteLine($"Lets skip sector #{bank / 8}"); } } else { Console.WriteLine("Lets try again"); } bank = (bank & ~7) - 1; Program.Reset(dumper); FlashHelper.ResetFlash(dumper); continue; } } if (totalErrorCount > 0) { Console.WriteLine($"Write error count: {totalErrorCount}"); } if (newBadSectorsList.Any()) { Console.WriteLine($"Can't write sectors: {string.Join(", ", newBadSectorsList.OrderBy(s => s))}"); } var wrongCrcSectorsList = new List <int>(); if (needCheck) { Console.WriteLine("Starting verification process"); Program.Reset(dumper); var readStartTime = DateTime.Now; lastSectorTime = DateTime.Now; timeEstimated = new TimeSpan(); for (int bank = 0; bank < banks; bank++) { while (badSectors.Contains(bank / 8)) { bank += 8; // bad sector :( } 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, r0, r1, r2, r3); int pos = bank * BANK_SIZE; if (pos % (128 * 1024) == 0) { timeEstimated = new TimeSpan((DateTime.Now - lastSectorTime).Ticks * (banks - bank) / 8); timeEstimated = timeEstimated.Add(DateTime.Now - readStartTime); lastSectorTime = DateTime.Now; } ushort crc = Crc16Calculator.CalculateCRC16(PRG, pos, BANK_SIZE); var timePassed = DateTime.Now - readStartTime; Console.Write($"Reading CRC of bank #{bank}/{banks} ({100 * bank / banks}%, {timePassed.Hours:D2}:{timePassed.Minutes:D2}:{timePassed.Seconds:D2}/{timeEstimated.Hours:D2}:{timeEstimated.Minutes:D2}:{timeEstimated.Seconds:D2})... "); var crcr = dumper.ReadCpuCrc(0x8000, BANK_SIZE); if (crcr != crc) { Console.WriteLine($"Verification failed: {crcr:X4} != {crc:X4}"); if (!silent) { Program.PlayErrorSound(); } wrongCrcSectorsList.Add(bank / 8); } else { Console.WriteLine($"OK (CRC = {crcr:X4})"); } } if (totalErrorCount > 0) { Console.WriteLine($"Write error count: {totalErrorCount}"); } if (newBadSectorsList.Any()) { Console.WriteLine($"Can't write sectors: {string.Join(", ", newBadSectorsList.OrderBy(s => s))}"); } if (wrongCrcSectorsList.Any()) { Console.WriteLine($"Sectors with wrong CRC: {string.Join(", ", wrongCrcSectorsList.Distinct().OrderBy(s => s))}"); } } if (newBadSectorsList.Any() || wrongCrcSectorsList.Any()) { throw new IOException("Cartridge is not writed correctly"); } }