Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ClusterM committed Apr 29, 2021
1 parent 5daa29f commit 3b3cdfe
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 58 deletions.
30 changes: 15 additions & 15 deletions FamicomDumper/CoolboyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ public static byte DetectVersion(FamicomDumperConnection dumper)
byte version;
Console.Write("Detecting COOLBOY version... ");
// 0th CHR bank using both methods
dumper.WriteCpu(0x5000, new byte[] { 0, 0, 0, 0x10 });
dumper.WriteCpu(0x6000, new byte[] { 0, 0, 0, 0x10 });
dumper.WriteCpu(0x5000, 0, 0, 0, 0x10);
dumper.WriteCpu(0x6000, 0, 0, 0, 0x10);
// Writing 0
dumper.WritePpu(0x0000, new byte[] { 0 });
dumper.WritePpu(0x0000, 0);
// First CHR bank using both methods
dumper.WriteCpu(0x5000, new byte[] { 0, 0, 1, 0x10 });
dumper.WriteCpu(0x6000, new byte[] { 0, 0, 1, 0x10 });
dumper.WriteCpu(0x5000, 0, 0, 1, 0x10);
dumper.WriteCpu(0x6000, 0, 0, 1, 0x10);
// Writing 1
dumper.WritePpu(0x0000, new byte[] { 1 });
dumper.WritePpu(0x0000, 1);
// 0th bank using first method
dumper.WriteCpu(0x6000, new byte[] { 0, 0, 0, 0x10 });
byte v6000 = dumper.ReadPpu(0x0000, 1)[0];
dumper.WriteCpu(0x6000, 0, 0, 0, 0x10);
byte v6000 = dumper.ReadPpu(0x0000);
// return
dumper.WriteCpu(0x6000, new byte[] { 0, 0, 1, 0x10 });
dumper.WriteCpu(0x6000, 0, 0, 1, 0x10);
// 0th bank using second method
dumper.WriteCpu(0x5000, new byte[] { 0, 0, 0, 0x10 });
byte v5000 = dumper.ReadPpu(0x0000, 1)[0];
dumper.WriteCpu(0x5000, 0, 0, 0, 0x10);
byte v5000 = dumper.ReadPpu(0x0000);

if (v6000 == 0 && v5000 == 1)
version = 1;
Expand All @@ -58,7 +58,7 @@ public static void PrintFlashInfo(FamicomDumperConnection dumper)
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 });
dumper.WriteCpu(CoolboyReg, r0, r1, r2, r3);
var cfi = FlashHelper.GetCFIInfo(dumper);
FlashHelper.PrintCFIInfo(cfi);
FlashHelper.LockBitsCheckPrint(dumper);
Expand Down Expand Up @@ -130,7 +130,7 @@ public static void Write(FamicomDumperConnection dumper, string fileName, IEnume
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 });
dumper.WriteCpu(coolboyReg, r0, r1, r2, r3);

var data = new byte[BANK_SIZE];
int pos = bank * BANK_SIZE;
Expand Down Expand Up @@ -209,7 +209,7 @@ public static void Write(FamicomDumperConnection dumper, string fileName, IEnume
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 });
dumper.WriteCpu(coolboyReg, r0, r1, r2, r3);

var data = new byte[BANK_SIZE];
int pos = bank * BANK_SIZE;
Expand Down Expand Up @@ -269,7 +269,7 @@ public static void PPBClear(FamicomDumperConnection dumper, ushort coolboyReg)
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 });
dumper.WriteCpu(coolboyReg, r0, r1, r2, r3);

FlashHelper.PPBClear(dumper);
}
Expand Down
20 changes: 10 additions & 10 deletions FamicomDumper/FDS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public static void WriteFDS(FamicomDumperConnection dumper, string fileName, boo

for (int sideNumber = 0; sideNumber < rom.Sides.Count; sideNumber++)
{
var driveStatus = dumper.ReadCpu(0x4032, 1)[0];
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, 1)[0];
driveStatus = dumper.ReadCpu(0x4032);
}
Console.WriteLine("OK");
}
Expand Down Expand Up @@ -89,14 +89,14 @@ public static void WriteFDS(FamicomDumperConnection dumper, string fileName, boo

if (sideNumber + 1 < rom.Sides.Count)
{
driveStatus = dumper.ReadCpu(0x4032, 1)[0];
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, 1)[0];
driveStatus = dumper.ReadCpu(0x4032);
}
Console.WriteLine("OK");
}
Expand All @@ -118,14 +118,14 @@ public static void DumpFDS(FamicomDumperConnection dumper, string fileName, byte
var sideImages = new List<FdsDiskSide>();
for (int side = 1; side <= sides; side++)
{
var driveStatus = dumper.ReadCpu(0x4032, 1)[0];
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, 1)[0];
driveStatus = dumper.ReadCpu(0x4032);
}
Console.WriteLine("OK");
}
Expand All @@ -134,14 +134,14 @@ public static void DumpFDS(FamicomDumperConnection dumper, string fileName, byte

if (side < sides)
{
driveStatus = dumper.ReadCpu(0x4032, 1)[0];
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, 1)[0];
driveStatus = dumper.ReadCpu(0x4032);
}
Console.WriteLine("OK");
}
Expand Down Expand Up @@ -189,11 +189,11 @@ private static void CheckRAMAdapter(FamicomDumperConnection dumper)
dumper.WriteCpu(0x4026, 0x00);
dumper.WriteCpu(0x4025, 0b00100110); // reset
dumper.WriteCpu(0x0000, 0xFF); // to prevent open bus read
var ext = dumper.ReadCpu(0x4033, 1)[0];
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, 1)[0];
ext = dumper.ReadCpu(0x4033);
if ((ext & 0x7F) != 0x7F) ramAdapterPresent = false;
if (!ramAdapterPresent) throw new IOException("RAM adapter IO error, is it connected?");
}
Expand Down
12 changes: 6 additions & 6 deletions FamicomDumper/FlashHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void PasswordProgramm(FamicomDumperConnection dumper, byte[] passw
// Bits Program
dumper.WriteCpu(0x8000, 0xA0);
dumper.WriteCpu(0x8000, (byte)(1 << 2) ^ 0xFF); // password protection
var r = dumper.ReadCpu(0x8000, 1)[0];
var r = dumper.ReadCpu(0x8000);
if ((r & 7) != 3)
throw new InvalidDataException("Lock bit verification failed");
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public static void LockBitsCheckPrint(FamicomDumperConnection dumper)
dumper.WriteCpu(0x8AAA, 0xAA);
dumper.WriteCpu(0x8555, 0x55);
dumper.WriteCpu(0x8AAA, 0x40);
var lockRegister = dumper.ReadCpu(0x8000, 1)[0];
var lockRegister = dumper.ReadCpu(0x8000);
if ((lockRegister & 1) == 0)
Console.WriteLine("WARNING: Secured Silicon Sector Protection Bit is set!");
if ((lockRegister & 2) == 0)
Expand All @@ -173,7 +173,7 @@ public static void PPBLockBitCheckPrint(FamicomDumperConnection dumper)
dumper.WriteCpu(0x8AAA, 0xAA);
dumper.WriteCpu(0x8555, 0x55);
dumper.WriteCpu(0x8AAA, 0x50);
var ppbLockStatus = dumper.ReadCpu(0x8000, 1)[0];
var ppbLockStatus = dumper.ReadCpu(0x8000);
if (ppbLockStatus == 0)
Console.WriteLine("WARNING: PPB Lock Bit is set!");
}
Expand All @@ -192,7 +192,7 @@ public static byte PPBRead(FamicomDumperConnection dumper)
dumper.WriteCpu(0x8555, 0x55);
dumper.WriteCpu(0x8AAA, 0xC0);
// PPB Status Read
return dumper.ReadCpu(0x8000, 1)[0];
return dumper.ReadCpu(0x8000);
}
finally
{
Expand All @@ -216,7 +216,7 @@ public static void PPBSet(FamicomDumperConnection dumper)
DateTime startTime = DateTime.Now;
while (true)
{
byte b = dumper.ReadCpu(0x8000, 1)[0];
byte b = dumper.ReadCpu(0x8000);
if (b == 0x00)
break;
if ((DateTime.Now - startTime).TotalMilliseconds >= 1500)
Expand Down Expand Up @@ -248,7 +248,7 @@ public static void PPBClear(FamicomDumperConnection dumper)
DateTime startTime = DateTime.Now;
while (true)
{
byte b = dumper.ReadCpu(0x8000, 1)[0];
byte b = dumper.ReadCpu(0x8000);
if (b == 0x01)
break;
if ((DateTime.Now - startTime).TotalMilliseconds >= 1500)
Expand Down
12 changes: 6 additions & 6 deletions FamicomDumper/mappers/BMC-10-24-C-A1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public void DumpPrg(IFamicomDumperConnection dumper, List<byte> data, int size)
for (var bank = 0; bank < banks; bank += 2)
{
Console.Write($"Reading PRG banks #{outbank}|{bank} and #{outbank}|{bank + 1}... ");
dumper.WriteCpu(0x8000, new byte[] { 6, (byte)bank });
dumper.WriteCpu(0x8000, new byte[] { 7, (byte)(bank | 1) });
dumper.WriteCpu(0x8000, 6, (byte)bank);
dumper.WriteCpu(0x8000, 7, (byte)(bank | 1));
data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
Console.WriteLine("OK");
}
Expand All @@ -67,10 +67,10 @@ public void DumpChr(IFamicomDumperConnection dumper, List<byte> data, int size)
for (var bank = 0; bank < banks; bank += 4)
{
Console.Write($"Reading CHR banks #{outbank}|{bank}, #{outbank}|{bank + 1}, #{outbank}|{bank + 2}, #{outbank}|{bank + 3}... ");
dumper.WriteCpu(0x8000, new byte[] { 2, (byte)bank });
dumper.WriteCpu(0x8000, new byte[] { 3, (byte)(bank | 1) });
dumper.WriteCpu(0x8000, new byte[] { 4, (byte)(bank | 2) });
dumper.WriteCpu(0x8000, new byte[] { 5, (byte)(bank | 3) });
dumper.WriteCpu(0x8000, 2, (byte)bank);
dumper.WriteCpu(0x8000, 3, (byte)(bank | 1));
dumper.WriteCpu(0x8000, 4, (byte)(bank | 2));
dumper.WriteCpu(0x8000, 5, (byte)(bank | 3));
data.AddRange(dumper.ReadPpu(0x1000, 0x1000));
Console.WriteLine("OK");
}
Expand Down
24 changes: 12 additions & 12 deletions FamicomDumper/mappers/Coolboy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ public static byte DetectVersion(IFamicomDumperConnection dumper)
byte version;
Console.Write("Detecting COOLBOY version... ");
// 0th CHR bank using both methods
dumper.WriteCpu(0x5000, new byte[] { 0, 0, 0, 0x10 });
dumper.WriteCpu(0x6000, new byte[] { 0, 0, 0, 0x10 });
dumper.WriteCpu(0x5000, 0, 0, 0, 0x10);
dumper.WriteCpu(0x6000, 0, 0, 0, 0x10);
// Writing 0
dumper.WritePpu(0x0000, new byte[] { 0 });
dumper.WritePpu(0x0000, 0);
// First CHR bank using both methods
dumper.WriteCpu(0x5000, new byte[] { 0, 0, 1, 0x10 });
dumper.WriteCpu(0x6000, new byte[] { 0, 0, 1, 0x10 });
dumper.WriteCpu(0x5000, 0, 0, 1, 0x10);
dumper.WriteCpu(0x6000, 0, 0, 1, 0x10);
// Writing 1
dumper.WritePpu(0x0000, new byte[] { 1 });
dumper.WritePpu(0x0000, 1);
// 0th bank using first method
dumper.WriteCpu(0x6000, new byte[] { 0, 0, 0, 0x10 });
byte v6000 = dumper.ReadPpu(0x0000, 1)[0];
dumper.WriteCpu(0x6000, 0, 0, 0, 0x10);
byte v6000 = dumper.ReadPpu(0x0000);
// return
dumper.WriteCpu(0x6000, new byte[] { 0, 0, 1, 0x10 });
dumper.WriteCpu(0x6000, 0, 0, 1, 0x10);
// 0th bank using second method
dumper.WriteCpu(0x5000, new byte[] { 0, 0, 0, 0x10 });
byte v5000 = dumper.ReadPpu(0x0000, 1)[0];
dumper.WriteCpu(0x5000, 0, 0, 0, 0x10);
byte v5000 = dumper.ReadPpu(0x0000);

if (v6000 == 0 && v5000 == 1)
version = 1;
Expand Down Expand Up @@ -92,7 +92,7 @@ public void DumpPrg(IFamicomDumperConnection dumper, List<byte> data, int size)
var r2 = (byte)0;
var r3 = (byte)((1 << 4) // NROM mode
| ((bank & 7) << 1)); // 2, 1, 0 bits
dumper.WriteCpu(coolboyReg, new byte[] { r0, r1, r2, r3 });
dumper.WriteCpu(coolboyReg, r0, r1, r2, r3 );

Console.Write($"Reading PRG bank #{bank}/{banks}... ");
data.AddRange(dumper.ReadCpu(0x8000, 0x4000));
Expand Down
4 changes: 2 additions & 2 deletions FamicomDumper/mappers/MMC3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void DumpPrg(IFamicomDumperConnection dumper, List<byte> data, int size)
for (var bank = 0; bank < banks; bank++)
{
Console.Write($"Reading PRG banks #{bank}/{banks}... ");
dumper.WriteCpu(0x8000, new byte[] { 6, (byte)bank });
dumper.WriteCpu(0x8000, 6, (byte)bank);
data.AddRange(dumper.ReadCpu(0x8000, 0x2000));
Console.WriteLine("OK");
}
Expand All @@ -51,7 +51,7 @@ public void DumpChr(IFamicomDumperConnection dumper, List<byte> data, int size)
for (var bank = 0; bank < banks; bank++)
{
Console.Write($"Reading CHR banks #{bank}/{banks}... ");
dumper.WriteCpu(0x8000, new byte[] { 2, (byte)bank });
dumper.WriteCpu(0x8000, 2, (byte)bank);
data.AddRange(dumper.ReadPpu(0x1000, 0x0400));
Console.WriteLine("OK");
}
Expand Down
2 changes: 1 addition & 1 deletion FamicomDumper/mappers/Sunsoft5A-5B-FME7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ public NesFile.MirroringType GetMirroring(IFamicomDumperConnection dumper)
{
return NesFile.MirroringType.MapperControlled;
}
}
}
4 changes: 2 additions & 2 deletions FamicomDumper/scripts/DemoScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void Run(IFamicomDumperConnection dumper, string[] args)
{
if (bank > 256) throw new InvalidDataException("Bank number out of range, did you actually insert the MMC3 cartridge?");
Console.Write($"Reading PRG bank #{bank}... ");
dumper.WriteCpu(0x8000, new byte[] { 6, (byte)bank });
dumper.WriteCpu(0x8000, 6, (byte)bank);
var data = dumper.ReadCpu(0x8000, 0x2000);
Console.WriteLine("OK");
if (bank == 0)
Expand All @@ -34,7 +34,7 @@ void Run(IFamicomDumperConnection dumper, string[] args)
{
if (bank > 256) throw new InvalidDataException("Bank number out of range, did you actually insert the MMC3 cartridge?");
Console.Write($"Reading CHR bank #{bank}... ");
dumper.WriteCpu(0x8000, new byte[] { 2, (byte)bank });
dumper.WriteCpu(0x8000, 2, (byte)bank);
var data = dumper.ReadPpu(0x1000, 0x0400);
Console.WriteLine("OK");
if (bank == 0)
Expand Down
8 changes: 4 additions & 4 deletions FamicomDumper/scripts/FdsSpeedMeasure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ void Run(IFamicomDumperConnection dumper)
dumper.WriteCpu(0x4026, 0x00);
dumper.WriteCpu(0x4025, /*0b00100110*/ 0x26); // reset
dumper.WriteCpu(0x0000, 0xFF); // to prevent open bus read
var ext = dumper.ReadCpu(0x4033, 1)[0];
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, 1)[0];
ext = dumper.ReadCpu(0x4033);
if ((ext & 0x7F) != 0x7F) ramAdapterPresent = false;
if (!ramAdapterPresent) throw new IOException("Famicom Disk System RAM adapter IO error, is it connected?");

dumper.WriteCpu(0x4025, 0b00100110); // reset
dumper.WriteCpu(0x4025, 0b00100101); // enable motor without data transfer
Thread.Sleep(100);
// Check battery health
ext = dumper.ReadCpu(0x4033, 1)[0];
ext = dumper.ReadCpu(0x4033);
if ((ext & 0x80) == 0) throw new IOException("Battery voltage is low or power supply is not connected");

Console.WriteLine("Measuring FDS drive speed, make sure that disk card is inserted and wait. Press ENTER to stop.");
Expand All @@ -75,7 +75,7 @@ async Task SpeedMeasureLoop(IFamicomDumperConnection dumper, CancellationToken c
dumper.WriteCpu(0x4025, 0b00100101); // enable motor without data transfer

// Wait for ready state
while ((dumper.ReadCpu(0x4032, 1)[0] & 2) != 0)
while ((dumper.ReadCpu(0x4032) & 2) != 0)
{
if (cancellationToken.IsCancellationRequested) return;
await Task.Delay(1);
Expand Down

0 comments on commit 3b3cdfe

Please sign in to comment.