Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Program.cs « FamicomDumper - github.com/ClusterM/famicom-dumper-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 29673ef854fb95afe70a7745a6497d558d2039c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/* Famicom Dumper/Programmer
 *
 * Copyright notice for this file:
 *  Copyright (C) 2021 Cluster
 *  https://clusterrr.com
 *  clusterrr@clusterrr.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using com.clusterrr.Famicom.Containers;
using com.clusterrr.Famicom.Dumper.FlashWriters;
using com.clusterrr.Famicom.DumperConnection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Emit;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Media;
using System.Reflection;
using System.Reflection.Metadata;
using System.Runtime.Versioning;
using System.Security;
using System.Text.Json;
using System.Text.RegularExpressions;

namespace com.clusterrr.Famicom.Dumper
{
    public class Program
    {

        public const string REPO_PATH = "https://github.com/ClusterM/famicom-dumper-client";
        public const int DEFAULT_GRPC_PORT = 26673;
        public static DateTime BUILD_TIME = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(long.Parse(Properties.Resources.buildtime.Trim()));

        static int Main(string[] args)
        {
            Console.WriteLine($"Famicom Dumper Client v{Assembly.GetExecutingAssembly().GetName()?.Version?.Major}.{Assembly.GetExecutingAssembly().GetName()?.Version?.Minor}");
#if DEBUG
            Console.WriteLine($"  Commit {Properties.Resources.gitCommit} @ {REPO_PATH}");
            Console.WriteLine($"  Debug version, build time: {BUILD_TIME.ToLocalTime()}");
#endif
            Console.WriteLine("  (c) Alexey 'Cluster' Avdyukhin / https://clusterrr.com / clusterrr@clusterrr.com");
            Console.WriteLine("");
#if DEBUG
            var stopwatch = new Stopwatch();
            stopwatch.Start();
#endif
            string port = "auto";
            string? mapperName = null;
            string? psize = null;
            string? csize = null;
            string? filename = null;
            bool battery = false;
            string? csFile = null;
            string[] csArgs = Array.Empty<string>();

            string? unifName = null;
            string? unifAuthor = null;

            string? prgRamSize = null;
            string? prgNvRamSize = null;
            string? chrRamSize = null;
            string? chrNvRamSize = null;

            byte fdsSides = 1;
            byte fdsSkipSides = 0;
            bool fdsUseHeader = true;
            bool fdsDumpHiddenFiles = false;

            bool reset = false;
            bool silent = true;
            bool needCheck = false;
            bool writePBBs = false;
            List<int> badSectors = new();
            bool ignoreBadSectors = false;

            int coolboySubmapper = -1;

            int tcpPort = DEFAULT_GRPC_PORT;
            string? remoteHost = null;

            try
            {
                if (args.Length == 0 || args.Contains("help") || args.Contains("--help"))
                {
                    PrintHelp();
                    return 0;
                }

                string command = args[0].ToLower();

                for (int i = 1; i < args.Length; i++)
                {
                    string param = args[i];
                    if (param == "-")
                    {
                        csArgs = args.Skip(i + 1).ToArray();
                        break;
                    }
                    while (param.StartsWith("-")) param = param[1..];
                    string value = i < args.Length - 1 ? args[i + 1] : "";
                    switch (param.ToLower())
                    {
                        case "p":
                        case "port":
                            port = value;
                            i++;
                            break;
                        case "mappers":
                            // Meh... using static field
                            Scripting.MappersSearchDirectories = new string[] { value };
                            i++;
                            break;
                        case "m":
                        case "mapper":
                            mapperName = value;
                            i++;
                            break;
                        case "f":
                        case "file":
                            filename = value;
                            i++;
                            break;
                        case "fds-sides":
                            fdsSides = byte.Parse(value);
                            i++;
                            break;
                        case "fds-skip-sides":
                            fdsSkipSides = byte.Parse(value);
                            i++;
                            break;
                        case "fds-no-header":
                            fdsUseHeader = false;
                            break;
                        case "fds-dump-hidden":
                            fdsDumpHiddenFiles = true;
                            break;
                        case "csfile":
                        case "cs-file":
                        case "scriptfile":
                        case "script-file":
                            csFile = value;
                            i++;
                            break;
                        case "psize":
                        case "prg-size":
                            psize = value;
                            i++;
                            break;
                        case "csize":
                        case "chr-size":
                            csize = value;
                            i++;
                            break;
                        case "prg-ram-size":
                            prgRamSize = value;
                            i++;
                            break;
                        case "prg-nvram-size":
                            prgNvRamSize = value;
                            i++;
                            break;
                        case "chr-ram-size":
                            chrRamSize = value;
                            i++;
                            break;
                        case "chr-nvram-size":
                            chrNvRamSize = value;
                            i++;
                            break;
                        case "battery":
                            battery = true;
                            break;
                        case "unifname":
                        case "unif-name":
                            unifName = value;
                            i++;
                            break;
                        case "unifauthor":
                        case "unif-author":
                            unifAuthor = value;
                            i++;
                            break;
                        case "reset":
                            reset = true;
                            break;
                        case "sound":
                            silent = false;
                            break;
                        case "check":
                        case "verify":
                            needCheck = true;
                            break;
                        case "lock":
                            writePBBs = true;
                            break;
                        case "badsectors":
                        case "bad-sectors":
                            foreach (var v in value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                                badSectors.Add(int.Parse(v));
                            i++;
                            break;
                        case "tcpport":
                        case "tcp-port":
                            tcpPort = int.Parse(value);
                            i++;
                            break;
                        case "host":
                            remoteHost = value;
                            i++;
                            break;
                        case "ignorebadsectors":
                        case "ignore-bad-sectors":
                            ignoreBadSectors = true;
                            break;
                        case "coolboy-submapper":
                            coolboySubmapper = byte.Parse(value);
                            i++;
                            break;
                        default:
                            Console.WriteLine("Unknown option: " + param);
                            PrintHelp();
                            return 2;
                    }
                }

                if (command == "list-mappers")
                {
                    Scripting.ListMappers();
                    return 0;
                }

                IFamicomDumperConnectionExt dumper;
                if (string.IsNullOrEmpty(remoteHost))
                {
                    // Using local dumper
                    var localDumper = new FamicomDumperLocal();
                    localDumper.Open(port);
                    dumper = localDumper;
                }
                else
                {
                    // Using remote dumper
                    dumper = new FamicomDumperClient($"http://{remoteHost}:{tcpPort}");
                }

                Console.Write("Dumper initialization... ");
                bool initResult = dumper.Init();
                if (!initResult) throw new IOException("Can't init dumper");
                Console.WriteLine("OK");
#if DEBUG
                Console.WriteLine($"Protocol version: {dumper.ProtocolVersion}");
#endif
                if (dumper.HardwareVersion != null)
                    Console.WriteLine($"Dumper hardware version: {dumper.HardwareVersion.Major}.{dumper.HardwareVersion.Minor}" +
                        $"{((dumper.HardwareVersion.Build != 0) ? new string((char)dumper.HardwareVersion.Build, 1) : "")}");
                if (dumper.FirmwareVersion != null)
                    Console.WriteLine($"Dumper firmware version: {dumper.FirmwareVersion.Major}.{dumper.FirmwareVersion.Minor}" +
                        $"{((dumper.FirmwareVersion.Build != 0) ? new string((char)dumper.FirmwareVersion.Build, 1) : "")}");

                try
                {
                    if (reset)
                        Reset(dumper);
                    if (dumper.ProtocolVersion >= 4)
                        dumper.SetCoolboyGpioMode(false);

                    if (!string.IsNullOrEmpty(csFile))
                    {
                        Scripting.CompileAndExecute(csFile, dumper, filename, mapperName,
                            ParseSize(psize), ParseSize(csize),
                            ParseSize(prgRamSize), ParseSize(chrRamSize),
                            ParseSize(prgNvRamSize), ParseSize(chrNvRamSize),
                            unifName, unifAuthor, battery, csArgs);
                    }

                    switch (command)
                    {
                        case "reset":
                            if (!reset)
                                Reset(dumper);
                            break;
                        case "list-mappers":
                            Scripting.ListMappers();
                            break;
                        case "dump":
                            Dump(dumper, filename ?? "output.nes", mapperName,
                                ParseSize(psize), ParseSize(csize),
                                ParseSize(prgRamSize), ParseSize(prgNvRamSize),
                                ParseSize(chrRamSize), ParseSize(chrNvRamSize),
                                unifName, unifAuthor, battery);
                            break;
                        case "dump-fds":
                            FDS.DumpFDS(dumper, filename ?? "output.fds", fdsSides, fdsDumpHiddenFiles, fdsUseHeader);
                            break;
                        case "read-prg-ram":
                        case "dump-prg-ram":
                        case "dump-sram":
                            ReadPrgRam(dumper, filename ?? "savegame.sav", mapperName);
                            break;
                        case "write-fds":
                            if (string.IsNullOrEmpty(filename))
                                throw new ArgumentException("Please specify ROM filename using --file argument");
                            FDS.WriteFDS(dumper, filename, needCheck, fdsSkipSides);
                            break;
                        case "write-prg-ram":
                        case "write-sram":
                            if (string.IsNullOrEmpty(filename))
                                throw new ArgumentException("Please specify ROM filename using --file argument");
                            WritePrgRam(dumper, filename, mapperName);
                            break;
                        case "write-coolboy":
                        case "write-coolboy-direct":
                            if (string.IsNullOrEmpty(filename))
                                throw new ArgumentException("Please specify ROM filename using --file argument");
                            new CoolboyWriter(dumper, coolboyGpioMode: false, coolboySubmapper).Write(filename, badSectors, silent, needCheck, writePBBs, ignoreBadSectors);
                            break;
                        case "write-coolboy-gpio":
                            if (string.IsNullOrEmpty(filename))
                                throw new ArgumentException("Please specify ROM filename using --file argument");
                            new CoolboyWriter(dumper, coolboyGpioMode: true, coolboySubmapper).Write(filename, badSectors, silent, needCheck, writePBBs, ignoreBadSectors);
                            break;
                        case "write-coolgirl":
                            if (string.IsNullOrEmpty(filename))
                                throw new ArgumentException("Please specify ROM filename using --file argument");
                            new CoolgirlWriter(dumper).Write(filename, badSectors, silent, needCheck, writePBBs, ignoreBadSectors);
                            break;
                        case "write-unrom512":
                            if (string.IsNullOrEmpty(filename))
                                throw new ArgumentException("Please specify ROM filename using --file argument");
                            new Unrom512Writer(dumper).Write(filename, badSectors, silent, needCheck, writePBBs, ignoreBadSectors);
                            break;
                        case "info-coolboy":
                            new CoolboyWriter(dumper, coolboyGpioMode: false, coolboySubmapper).PrintFlashInfo();
                            break;
                        case "info-coolboy-gpio":
                            new CoolboyWriter(dumper, coolboyGpioMode: true, coolboySubmapper).PrintFlashInfo();
                            break;
                        case "info-coolgirl":
                            new CoolgirlWriter(dumper).PrintFlashInfo();
                            break;
                        case "info-unrom512":
                            new Unrom512Writer(dumper).PrintFlashInfo();
                            break;
                        case "script":
                            if (string.IsNullOrEmpty(csFile))
                                throw new ArgumentException("Please specify C# script using --cs-file argument");
                            break;
                        case "server":
                            if (dumper is FamicomDumperLocal d)
                                StartServer(d, tcpPort);
                            break;
                        default:
                            Console.WriteLine("Unknown command: " + command);
                            PrintHelp();
                            return 2;
                    }
#if DEBUG
                    var timePassed = stopwatch.Elapsed;
                    if (timePassed.TotalMinutes >= 60)
                        Console.WriteLine($"Done in {timePassed.Hours}:{timePassed.Minutes:D2}:{timePassed.Seconds:D2}");
                    else if (timePassed.TotalSeconds >= 10)
                        Console.WriteLine($"Done in {timePassed.Minutes:D2}:{timePassed.Seconds:D2}");
                    else
                        Console.WriteLine($"Done in {(int)timePassed.TotalMilliseconds}ms");
#else
                    Console.WriteLine("Done.");
#endif
                    if (!silent) PlayDoneSound();
                }
                finally
                {
                    dumper.Dispose();
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine($"ERROR {ex.GetType()}: {ex.Message}{ex.StackTrace}");
#else
                Console.WriteLine($"ERROR: {ex.Message}");
#endif
                if (!silent) PlayErrorSound();
                return 1;
            }
            return 0;
        }

        static int ParseSize(string? size)
        {
            if (string.IsNullOrEmpty(size)) return -1;
            size = size.ToUpper();
            int mul = 1;
            while (size.Contains("K"))
            {
                size = size.Replace("K", "");
                mul *= 1024;
            }
            while (size.Contains("M"))
            {
                size = size.Replace("M", "");
                mul *= 1024 * 1024;
            }
            return int.Parse(size) * mul;
        }

        #region Sounds
        [SupportedOSPlatform("windows")]
        static void PlayErrorSoundWav()
        {
            var errorSound = new SoundPlayer(Properties.Resources.ErrorSound);
            errorSound.PlaySync();
        }

        static void PlayBeepSound()
        {
            Console.Beep();
        }

        public static void PlayErrorSound()
        {
            if (OperatingSystem.IsWindows())
                PlayErrorSoundWav();
            else
                PlayBeepSound();
        }

        [SupportedOSPlatform("windows")]
        static void PlayDoneSoundWav()
        {
            var doneSound = new SoundPlayer(Properties.Resources.DoneSound);
            doneSound.PlaySync();
        }

        public static void PlayDoneSound()
        {
            if (OperatingSystem.IsWindows())
            {
                PlayDoneSoundWav();
            }
            else
            {
                PlayBeepSound();
                PlayBeepSound();
                PlayBeepSound();
            }
        }
        #endregion

        static void PrintHelp()
        {
            Console.WriteLine($"Usage: {Path.GetFileName(Process.GetCurrentProcess().MainModule?.FileName)} <command> [<options>] [- <cs_script_arguments>]");
            Console.WriteLine();
            Console.WriteLine("Available commands:");
            Console.WriteLine(" {0,-30}{1}", "list-mappers", "list available mappers to dump");
            Console.WriteLine(" {0,-30}{1}", "dump", "dump cartridge");
            Console.WriteLine(" {0,-30}{1}", "server", "start gRPC server");
            Console.WriteLine(" {0,-30}{1}", "script", "execute C# script specified by --cs-file option");
            Console.WriteLine(" {0,-30}{1}", "reset", "simulate reset (M2 goes to Z-state for a second)");
            Console.WriteLine(" {0,-30}{1}", "dump-fds", "dump FDS card using RAM adapter and FDS drive");
            Console.WriteLine(" {0,-30}{1}", "write-fds", "write FDS card using RAM adapter and FDS drive");
            Console.WriteLine(" {0,-30}{1}", "read-prg-ram", "read PRG RAM (battery backed save if exists)");
            Console.WriteLine(" {0,-30}{1}", "write-prg-ram", "write PRG RAM");
            Console.WriteLine(" {0,-30}{1}", "write-coolboy", "write COOLBOY cartridge");
            Console.WriteLine(" {0,-30}{1}", "write-coolboy-gpio", "write COOLBOY cartridge using dumper's GPIO pins");
            Console.WriteLine(" {0,-30}{1}", "write-coolgirl", "write COOLGIRL cartridge");
            Console.WriteLine(" {0,-30}{1}", "write-unrom512", "write UNROM-512 cartridge");
            Console.WriteLine(" {0,-30}{1}", "info-coolboy", "show information about COOLBOY cartridge's flash memory");
            Console.WriteLine(" {0,-30}{1}", "info-coolboy-gpio", "show information about COOLBOY cartridge's flash memory using dumper's GPIO pins");
            Console.WriteLine(" {0,-30}{1}", "info-coolgirl", "show information about COOLGIRL cartridge's flash memory");
            Console.WriteLine(" {0,-30}{1}", "info-unrom512", "show information about UNROM-512 cartridge's flash memory");
            Console.WriteLine();
            Console.WriteLine("Available options:");
            Console.WriteLine(" {0,-30}{1}", "--port <com>", "serial port of dumper or serial number of dumper device, default - auto");
            Console.WriteLine(" {0,-30}{1}", "--tcp-port <port>", $"TCP port for gRPC communication, default - {DEFAULT_GRPC_PORT}");
            Console.WriteLine(" {0,-30}{1}", "--host <host>", "enable gRPC client and connect to specified host");
            Console.WriteLine(" {0,-30}{1}", "--mappers <directory>", "directory to search mapper scripts");
            Console.WriteLine(" {0,-30}{1}", "--mapper <mapper>", "number, name or path to C# script of mapper for dumping, default - 0 (NROM)");
            Console.WriteLine(" {0,-30}{1}", "--file <output.nes>", "output/input filename (.nes, .fds, .sav, etc.)");
            Console.WriteLine(" {0,-30}{1}", "--prg-size <size>", "size of PRG memory to dump, you can use \"K\" or \"M\" suffixes");
            Console.WriteLine(" {0,-30}{1}", "--chr-size <size>", "size of CHR memory to dump, you can use \"K\" or \"M\" suffixes");
            Console.WriteLine(" {0,-30}{1}", "--prg-ram-size <size>", "size of PRG RAM memory for NES 2.0 header, you can use \"K\" or \"M\" suffixes");
            Console.WriteLine(" {0,-30}{1}", "--chr-ram-size <size>", "size of CHR RAM memory for NES 2.0 header, you can use \"K\" or \"M\" suffixes");
            Console.WriteLine(" {0,-30}{1}", "--prg-nvram-size <size>", "size of PRG NVRAM memory for NES 2.0 header, you can use \"K\" or \"M\" suffixes");
            Console.WriteLine(" {0,-30}{1}", "--chr-nvram-size <size>", "size of CHR NVRAM memory for NES 2.0 header, you can use \"K\" or \"M\" suffixes");
            Console.WriteLine(" {0,-30}{1}", "--battery", "set \"battery\" flag in ROM header after dumping");
            Console.WriteLine(" {0,-30}{1}", "--unif-name <name>", "internal ROM name for UNIF dumps");
            Console.WriteLine(" {0,-30}{1}", "--unif-author <name>", "author of dump name for UNIF dumps");
            Console.WriteLine(" {0,-30}{1}", "--fds-sides <sides>", "number of FDS sides to dump (default - 1)");
            Console.WriteLine(" {0,-30}{1}", "--fds-skip-sides <sides>", "number of FDS sides to skip while writing (default - 0)");
            Console.WriteLine(" {0,-30}{1}", "--fds-no-header", "do not add header to output file during FDS dumping");
            Console.WriteLine(" {0,-30}{1}", "--fds-dump-hidden", "try to dump hidden files during FDS dumping (used for some copy-protected games)");
            Console.WriteLine(" {0,-30}{1}", "--coolboy-submapper <submapper>", "submapper number to use while writing COOLBOY (default - auto, based on a ROM header)");
            Console.WriteLine(" {0,-30}{1}", "--reset", "simulate reset first");
            Console.WriteLine(" {0,-30}{1}", "--cs-file <C#_file>", "execute C# script from file");
            Console.WriteLine(" {0,-30}{1}", "--bad-sectors <bad_sectors>", "comma separated list of bad sectors for COOLBOY/COOLGIRL writing");
            Console.WriteLine(" {0,-30}{1}", "--ignore-bad-sectors", "ignore bad sectors while writing COOLBOY/COOLGIRL");
            Console.WriteLine(" {0,-30}{1}", "--verify", "verify COOLBOY/COOLGIRL/UNROM-512/FDS after writing");
            Console.WriteLine(" {0,-30}{1}", "--lock", "write-protect COOLBOY/COOLGIRL sectors after writing");
            Console.WriteLine(" {0,-30}{1}", "--sound", "play sound when done or error occured");
        }

        static public void Reset(IFamicomDumperConnectionExt dumper)
        {
            Console.Write("Reset... ");
            dumper.Reset();
            Console.WriteLine("OK");
        }

        static void Dump(IFamicomDumperConnectionExt dumper, string fileName, string? mapperName, int prgSize, int chrSize, int
            prgRamSize, int prgNvRamSize, int chrRamSize, int chrNvRamSize, string? unifName, string? unifAuthor, bool battery)
        {
            var mapper = Scripting.GetMapper(mapperName);
            if (mapper.Number >= 0)
                Console.WriteLine($"Using mapper: " +
                    $"#{mapper.Number}{(mapper.Submapper > 0 ? $".{mapper.Submapper}" : "" )}" +
                    $"{(!string.IsNullOrEmpty(mapper.Name) ? $" ({mapper.Name})" : "")}");
            else
                Console.WriteLine($"Using UNIF mapper: {mapper.Name}");

            // Get optional properties
            // int DefaultPrgRamSize -> PRG RAM size
            if (prgRamSize < 0) {
                var method = mapper.GetType().GetProperty(
                        "DefaultPrgRamSize", BindingFlags.Instance | BindingFlags.Public,
                        null, typeof(int), Array.Empty<Type>(),
                        Array.Empty<ParameterModifier>());
                if (method != null) prgRamSize = (int)method.GetValue(mapper)!;
            }
            // int DefaultChrRamSize -> CHR RAM size
            if (chrRamSize < 0)
            {
                var method = mapper.GetType().GetProperty(
                        "DefaultChrRamSize", BindingFlags.Instance | BindingFlags.Public,
                        null, typeof(int), Array.Empty<Type>(),
                        Array.Empty<ParameterModifier>());
                if (method != null) chrRamSize = (int)method.GetValue(mapper)!;
            }
            // int DefaultPrgNvramSize -> PRG NVRAM size
            if (prgNvRamSize < 0)
            {
                var method = mapper.GetType().GetProperty(
                        "DefaultPrgNvramSize", BindingFlags.Instance | BindingFlags.Public,
                        null, typeof(int), Array.Empty<Type>(),
                        Array.Empty<ParameterModifier>());
                if (method != null) prgNvRamSize = (int)method.GetValue(mapper)!;
            }
            // int DefaultChrNvramSize -> CHR NVRAM size
            if (chrNvRamSize < 0)
            {
                var method = mapper.GetType().GetProperty(
                        "DefaultChrNvramSize", BindingFlags.Instance | BindingFlags.Public,
                        null, typeof(int), Array.Empty<Type>(),
                        Array.Empty<ParameterModifier>());
                if (method != null) chrNvRamSize = (int)method.GetValue(mapper)!;
            }

            Console.WriteLine("Dumping...");
            var prg = new List<byte>();
            var chr = new List<byte>();
            prgSize = prgSize >= 0 ? prgSize : mapper.DefaultPrgSize;
            chrSize = chrSize >= 0 ? chrSize : mapper.DefaultChrSize;
            if (prgSize > 0)
            {
                Console.WriteLine($"PRG memory size: {prgSize / 1024}KB");
                mapper.DumpPrg(dumper, prg, prgSize);
                while (prg.Count % 0x4000 != 0) prg.Add(0);
            }
            if (chrSize > 0)
            {
                Console.WriteLine($"CHR memory size: {chrSize / 1024}KB");
                mapper.DumpChr(dumper, chr, chrSize);
                while (chr.Count % 0x2000 != 0) chr.Add(0);
            }
            var mirroring = mapper.GetMirroring(dumper);
            Console.WriteLine($"Mirroring: {mirroring}");
            var ext = Path.GetExtension(fileName);
            switch (ext.ToLower())
            {
                case ".nes":
                    // Using iNES or NES 2.0 container
                    var nesFile = new NesFile();
                    nesFile.Version = ((mapper.Number > 255) || (mapper.Submapper >= 0)
                            || (prgRamSize >= 0) || (prgNvRamSize >= 0)
                            || (chrRamSize >= 0) || (chrNvRamSize >= 0))
                        ? NesFile.iNesVersion.NES20 : NesFile.iNesVersion.iNES;
                    Console.Write($"Saving ROM as {(nesFile.Version switch { NesFile.iNesVersion.NES20 => "NES 2.0", _ => "iNES" })} file: {fileName}... ");
                    if (mapper.Number < 0)
                        throw new NotSupportedException("Can't save ROM as .nes file: mapper number unknown");
                    nesFile.Mapper = (ushort)mapper.Number;
                    nesFile.Submapper = mapper.Submapper;
                    nesFile.Mirroring = mirroring;
                    nesFile.PRG = prg.ToArray();
                    nesFile.CHR = chr.ToArray();
                    nesFile.PrgRamSize = (uint)Math.Max(0, prgRamSize);
                    nesFile.PrgNvRamSize = (uint)Math.Max(0, prgNvRamSize);
                    nesFile.ChrRamSize = (uint)Math.Max(0, chrRamSize);
                    nesFile.ChrNvRamSize = (uint)Math.Max(0, chrNvRamSize);
                    nesFile.Battery = battery;
                    nesFile.Save(fileName);
                    break;
                case ".unf":
                case ".unif":
                    // Using UNIF container
                    Console.Write($"Saving ROM as UNIF file: {fileName}... ");
                    if (string.IsNullOrEmpty(mapper.UnifName))
                        throw new NotSupportedException("Can't save ROM as UNIF file - mapper code name unknown");
                    var unifFile = new UnifFile
                    {
                        Version = 5,
                        Mapper = mapper.UnifName
                    };
                    if (unifName != null)
                        unifFile.GameName = unifName;
                    unifFile.PRG0 = prg.ToArray();
                    if (chr.Count > 0)
                        unifFile.CHR0 = chr.ToArray();
                    unifFile.Mirroring = mirroring;
                    unifFile.Battery = battery;
                    if (!string.IsNullOrEmpty(unifAuthor))
                        unifFile.DumperName = unifAuthor;
                    unifFile.DumpingSoftware = $"Famicom Dumper by Cluster / {REPO_PATH}";
                    unifFile.Save(fileName);
                    break;
                case ".bin":
                    // Save as raw binary file
                    Console.Write($"Saving as raw binary file: {fileName}... ");
                    File.WriteAllBytes(fileName, prg.ToArray());
                    if (chr.Count > 0) Console.Write("WARNING! CHR is not saved! PRG only. ");
                    break;
                default:
                    throw new NotSupportedException($"Unknown extention {ext}, can't determine container type");
            }
            Console.WriteLine("OK");
        }

        static void ReadPrgRam(IFamicomDumperConnectionExt dumper, string fileName, string? mapperName)
        {
            var mapper = Scripting.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(0); // to avoid corruption
            Reset(dumper);
        }

        static void WritePrgRam(IFamicomDumperConnectionExt dumper, string fileName, string? mapperName)
        {
            var mapper = Scripting.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("Writing PRG RAM... ");
            var prgram = File.ReadAllBytes(fileName);
            dumper.WriteCpu(0x6000, prgram);
            Console.WriteLine("OK");
            dumper.ReadCpu(0); // to avoid corruption
            Reset(dumper);
        }

        static void StartServer(FamicomDumperLocal dumper, int tcpPort)
        {
            Console.WriteLine($"Listening port {tcpPort}, press Ctrl-C to stop");
            FamicomDumperService.StartServer(dumper, $"http://0.0.0.0:{tcpPort}");
        }
    }
}