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

NesFile.cs - github.com/ClusterM/nes-containers.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8063d5bc503c146786d6893019a5ac7d8c2c15d4 (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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;

namespace com.clusterrr.Famicom.Containers
{
    /// <summary>
    /// iNES / NES 2.0 file container for NES/Famicom games
    /// </summary>
    public partial class NesFile
    {
        /// <summary>
        /// PRG data
        /// </summary>
        public byte[] PRG
        {
            get => prg;
            set => prg = (value ?? Array.Empty<byte>()).ToArray();
        }
        /// <summary>
        /// CHR data
        /// </summary>
        public byte[] CHR
        {
            get => chr;
            set => chr = (value ?? Array.Empty<byte>()).ToArray();
        }
        /// <summary>
        /// Trainer
        /// </summary>
        public byte[] Trainer
        {
            get => trainer;
            set
            {
                if (value != null && value.Count() != 0 && value.Count() > 512)
                    throw new ArgumentOutOfRangeException("Trainer size must be 512 bytes or less");
                chr = value ?? Array.Empty<byte>();
            }
        }
        /// <summary>
        /// Miscellaneous ROM (NES 2.0 only)
        /// </summary>
        public byte[] MiscellaneousROM
        {
            get => miscellaneousROM;
            set => miscellaneousROM = value ?? Array.Empty<byte>();
        }
        /// <summary>
        /// Mapper number
        /// </summary>
        public ushort Mapper { get; set; } = 0;
        /// <summary>
        /// Submapper number (NES 2.0 only)
        /// </summary>
        public byte Submapper { get; set; } = 0;
        /// <summary>
        /// Battery-backed (or other non-volatile memory) memory is present
        /// </summary>
        public bool Battery { get; set; } = false;
        private iNesVersion version = NesFile.iNesVersion.iNES;
        /// <summary>
        /// Version of .nes file format: iNES or NES 2.0
        /// </summary>
        public iNesVersion Version
        {
            get => version;
            set
            {
                if (value != iNesVersion.iNES && value != iNesVersion.NES20)
                    throw new ArgumentException("Only version 1 and 2 allowed", nameof(Version));
                version = value;
            }
        }
        /// <summary>
        /// PRG RAM Size (NES 2.0 only)
        /// </summary>
        public uint PrgRamSize { get; set; } = 0;
        private uint prgNvRamSize = 0;
        /// <summary>
        /// PRG NVRAM Size (NES 2.0 only)
        /// </summary>
        public uint PrgNvRamSize
        {
            get => prgNvRamSize; set
            {
                prgNvRamSize = value;
                if (prgNvRamSize > 0 || chrNvRamSize > 0)
                    Battery = true;
            }
        }
        /// <summary>
        /// CHR RAM Size (NES 2.0 only)
        /// </summary>
        public uint ChrRamSize { get; set; } = 0;
        private uint chrNvRamSize = 0;
        private byte[] prg = Array.Empty<byte>();
        private byte[] chr = Array.Empty<byte>();
        private byte[] trainer = Array.Empty<byte>();
        private byte[] miscellaneousROM = Array.Empty<byte>();

        /// <summary>
        /// CHR NVRAM Size (NES 2.0 only)
        /// </summary>
        public uint ChrNvRamSize
        {
            get => chrNvRamSize; set
            {
                chrNvRamSize = value;
                if (prgNvRamSize > 0 || chrNvRamSize > 0)
                    Battery = true;
            }
        }
        /// <summary>
        /// Mirroring type
        /// </summary>
        public MirroringType Mirroring { get; set; } = MirroringType.Horizontal;
        /// <summary>
        /// For non-homebrew NES/Famicom games, this field's value is always a function of the region in which a game was released (NES 2.0 only)
        /// </summary>        
        public Timing Region { get; set; } = Timing.Ntsc;
        /// <summary>
        /// Console type (NES 2.0 only)
        /// </summary>
        public ConsoleType Console { get; set; } = ConsoleType.Normal;
        /// <summary>
        /// Vs. System PPU type (used when Console is ConsoleType.VsSystem)
        /// </summary>
        public VsPpuType VsPpu { get; set; } = VsPpuType.RP2C03B;
        /// <summary>
        /// Vs. System hardware type (used when Console is ConsoleType.VsSystem)
        /// </summary>
        public VsHardwareType VsHardware { get; set; } = VsHardwareType.VsUnisystemNormal;
        /// <summary>
        /// Extended console type (used when Console is ConsoleType.Extended)
        /// </summary>
        public ExtendedConsoleType ExtendedConsole { get; set; } = ExtendedConsoleType.RegularNES;
        /// <summary>
        /// Default expansion device (NES 2.0 only)
        /// </summary>
        public ExpansionDevice DefaultExpansionDevice { get; set; } = ExpansionDevice.Unspecified;
        /// <summary>
        /// Miscellaneous ROMs сount (NES 2.0 only)
        /// </summary>
        public byte MiscellaneousROMsCount { get; set; } = 0;

        /// <summary>
        /// Version of iNES format
        /// </summary>
        public enum iNesVersion
        {
            /// <summary>
            /// Classic iNES format
            /// </summary>
            iNES = 1,
            /// <summary>
            /// NES 2.0 format
            /// </summary>
            NES20 = 2
        }

        /// <summary>
        /// Console type
        /// </summary>
        public enum ConsoleType
        {
            /// <summary>
            /// Nintendo Entertainment System/Family Computer
            /// </summary>
            Normal = 0,
            /// <summary>
            /// Nintendo Vs. System
            /// </summary>
            VsSystem = 1,
            /// <summary>
            /// Nintendo Playchoice 10
            /// </summary>
            Playchoice10 = 2,
            /// <summary>
            /// Extended Console Type
            /// </summary>
            Extended = 3
        }

        /// <summary>
        /// Vs. System PPU type
        /// </summary>
        public enum VsPpuType
        {
            /// <summary>
            /// RP2C03B
            /// </summary>
            RP2C03B = 0x00,
            /// <summary>
            /// RP2C03G
            /// </summary>
            RP2C03G = 0x01,
            /// <summary>
            /// RP2C04-0001
            /// </summary>
            RP2C04_0001 = 0x02,
            /// <summary>
            /// RP2C04-0002
            /// </summary>            
            RP2C04_0002 = 0x03,
            /// <summary>
            /// RP2C04-0003
            /// </summary>
            RP2C04_0003 = 0x04,
            /// <summary>
            /// RP2C04-0004
            /// </summary>
            RP2C04_0004 = 0x05,
            /// <summary>
            /// RC2C03B
            /// </summary>
            RC2C03B = 0x06,
            /// <summary>
            /// RC2C03C
            /// </summary>
            RC2C03C = 0x07,
            /// <summary>
            /// RC2C05-01 ($2002 AND $?? =$1B)
            /// </summary>
            RC2C05_01 = 0x08,
            /// <summary>
            /// RC2C05-02 ($2002 AND $3F =$3D)
            /// </summary>
            RC2C05_02 = 0x09,
            /// <summary>
            /// RC2C05-03 ($2002 AND $1F =$1C)
            /// </summary>
            RC2C05_03 = 0x0A,
            /// <summary>
            /// RC2C05-04 ($2002 AND $1F =$1B)
            /// </summary>
            RC2C05_04 = 0x0B,
            /// <summary>
            /// RC2C05-05 ($2002 AND $1F =unknown)
            /// </summary>
            RC2C05_05 = 0x0C
        };

        /// <summary>
        /// Vs. System hardware type
        /// </summary>
        public enum VsHardwareType
        {
            /// <summary>
            /// Vs. Unisystem (normal)
            /// </summary>
            VsUnisystemNormal = 0x00,
            /// <summary>
            /// Vs. Unisystem (RBI Baseball protection)
            /// </summary>
            VsUnisystemRBIBaseballProtection = 0x01,
            /// <summary>
            /// Vs. Unisystem (TKO Boxing protection)
            /// </summary>
            VsUnisystemTKOBoxingProtection = 0x02,
            /// <summary>
            /// Vs. Unisystem (Super Xevious protection)
            /// </summary>
            VsUnisystemSuperXeviousProtection = 0x03,
            /// <summary>
            /// Vs. Unisystem (Vs. Ice Climber Japan protection)
            /// </summary>
            VsUnisystemVsIceClimberJapanProtection = 0x04,
            /// <summary>
            /// Vs. Dual System (normal)
            /// </summary>
            VsDualSystemNormal = 0x05,
            /// <summary>
            /// Vs. Dual System (Raid on Bungeling Bay protection)
            /// </summary>
            VsDualSystemRaidOnBungelingBayProtection = 0x06
        }

        /// <summary>
        /// Extended console type
        /// </summary>
        public enum ExtendedConsoleType
        {
            /// <summary>
            /// Regular NES/Famicom/Dendy
            /// </summary>
            RegularNES = 0x00,
            /// <summary>
            /// Nintendo Vs. System
            /// </summary>
            NintendoVsSystem = 0x01,
            /// <summary>
            /// Playchoice 10
            /// </summary>
            Playchoice10 = 0x02,
            /// <summary>
            /// Regular Famiclone, but with CPU that supports Decimal Mode (e.g. Bit Corporation Creator)
            /// </summary>
            FamicloneWithDecimalMode = 0x03,
            /// <summary>
            /// V.R. Technology VT01 with monochrome palette
            /// </summary>
            VRTechnologyVT01Monochrome = 0x04,
            /// <summary>
            /// V.R. Technology VT01 with red/cyan STN palette
            /// </summary>
            VRTechnologyVT01WithRedCyanSTNPalette = 0x05,
            /// <summary>
            /// V.R. Technology VT02
            /// </summary>
            VRTechnologyVT02 = 0x06,
            /// <summary>
            /// V.R. Technology VT03
            /// </summary>
            VRTechnologyVT03 = 0x07,
            /// <summary>
            /// V.R. Technology VT09
            /// </summary>
            VRTechnologyVT09 = 0x08,
            /// <summary>
            /// V.R. Technology VT32
            /// </summary>
            VRTechnologyVT32 = 0x09,
            /// <summary>
            /// V.R. Technology VT369
            /// </summary>
            VRTechnologyVT369 = 0x0A,
            /// <summary>
            /// UMC UM6578
            /// </summary>
            UMC_UM6578 = 0x0B
        }

        /// <summary>
        /// Type of expansion device connected to console, source: https://www.nesdev.org/wiki/NES_2.0#Default_Expansion_Device
        /// </summary>
        public enum ExpansionDevice
        {
            /// <summary>
            /// Expansion device is not specified
            /// </summary>
            Unspecified = 0x00,
            /// <summary>
            /// Standard NES/Famicom controllers
            /// </summary>
            Standard = 0x01,
            /// <summary>
            /// NES Four Score/Satellite with two additional standard controllers
            /// </summary>
            NesFourScore = 0x02,
            /// <summary>
            /// Famicom Four Players Adapter with two additional standard controllers
            /// </summary>
            FamicomFourPlayersAdapter = 0x03,
            /// <summary>
            /// Vs. System
            /// </summary>
            VsSystem = 0x04,
            /// <summary>
            /// Vs. System with reversed inputs
            /// </summary>
            VsSystemWithReversedInputs = 0x05,
            /// <summary>
            /// Vs. Pinball (Japan)
            /// </summary>
            VsPinball = 0x06,
            /// <summary>
            /// Vs. Zapper
            /// </summary>
            VsZapper = 0x07,
            /// <summary>
            /// Zapper ($4017)
            /// </summary>
            Zapper = 0x08,
            /// <summary>
            /// Two Zappers
            /// </summary>
            TwoZappers = 0x09,
            /// <summary>
            /// Bandai Hyper Shot Lightgun
            /// </summary>
            BandaiHyperShotLightgun = 0x0A,
            /// <summary>
            /// Power Pad Side A
            /// </summary>
            PowerPadSideA = 0x0B,
            /// <summary>
            /// Power Pad Side B
            /// </summary>
            PowerPadSideB = 0x0C,
            /// <summary>
            /// Family Trainer Side A
            /// </summary>
            FamilyTrainerSideA = 0x0D,
            /// <summary>
            /// Family Trainer Side B
            /// </summary>
            FamilyTrainerSideB = 0x0E,
            /// <summary>
            /// Arkanoid Vaus Controller (NES)
            /// </summary>
            ArkanoidVausControllerNES = 0x0F,
            /// <summary>
            /// Arkanoid Vaus Controller (Famicom)
            /// </summary>
            ArkanoidVausControllerFamicom = 0x10,
            /// <summary>
            /// Two Vaus Controllers plus Famicom Data Recorder
            /// </summary>
            TwoVausControllersPlusFamicomDataRecorder = 0x11,
            /// <summary>
            /// Konami Hyper Shot Controller
            /// </summary>
            KonamiHyperShotController = 0x12,
            /// <summary>
            /// Coconuts Pachinko Controller
            /// </summary>
            CoconutsPachinkoController = 0x13,
            /// <summary>
            /// Exciting Boxing Punching Bag (Blowup Doll)
            /// </summary>
            ExcitingBoxingPunchingBag = 0x14,
            /// <summary>
            /// Jissen Mahjong Controller
            /// </summary>
            JissenMahjongController = 0x15,
            /// <summary>
            /// Party Tap
            /// </summary>
            PartyTap = 0x16,
            /// <summary>
            /// Oeka Kids Tablet
            /// </summary>
            OekaKidsTablet = 0x17,
            /// <summary>
            /// Sunsoft Barcode Battler
            /// </summary>
            SunsoftBarcodeBattler = 0x18,
            /// <summary>
            /// Miracle Piano Keyboard
            /// </summary>
            MiraclePianoKeyboard = 0x19,
            /// <summary>
            /// Pokkun Moguraa (Whack-a-Mole Mat and Mallet)
            /// </summary>
            PokkunMoguraa = 0x1A,
            /// <summary>
            /// Top Rider(Inflatable Bicycle)
            /// </summary>
            TopRider = 0x1B,
            /// <summary>
            /// Double-Fisted (Requires or allows use of two controllers by one player)
            /// </summary>
            DoubleFisted = 0x1C,
            /// <summary>
            /// Famicom 3D System
            /// </summary>
            Famicom3DSystem = 0x1D,
            /// <summary>
            /// Doremikko Keyboard
            /// </summary>
            DoremikkoKeyboard = 0x1E,
            /// <summary>
            /// R.O.B. Gyro Set
            /// </summary>
            RobGyroSet = 0x1F,
            /// <summary>
            /// Famicom Data Recorder (don't emulate keyboard)
            /// </summary>
            FamicomDataRecorder = 0x20,
            /// <summary>
            /// ASCII Turbo File
            /// </summary>
            ASCIITurboFile = 0x21,
            /// <summary>
            /// IGS Storage Battle Box
            /// </summary>
            IGSStorageBattleBox = 0x22,
            /// <summary>
            /// Family BASIC Keyboard plus Famicom Data Recorder
            /// </summary>
            FamilyBasicKeyboardPlusFamicomDataRecorder = 0x23,
            /// <summary>
            /// Dongda PEC-586 Keyboard
            /// </summary>
            DongdaPEC586Keyboard = 0x24,
            /// <summary>
            /// Bit Corp. Bit-79 Keyboard
            /// </summary>
            BitCorpBit79Keyboard = 0x25,
            /// <summary>
            /// Subor Keyboard
            /// </summary>
            SuborKeyboard = 0x26,
            /// <summary>
            /// Subor Keyboard plus mouse (3x8-bit protocol)
            /// </summary>
            SuborKeyboardPlusMouse3x8 = 0x27,
            /// <summary>
            /// Subor Keyboard plus mouse (24-bit protocol)
            /// </summary>
            SuborKeyboardPlusMouse24 = 0x28,
            /// <summary>
            /// SNES Mouse ($4017.d0)
            /// </summary>
            SnesMouse4017 = 0x29,
            /// <summary>
            /// Multicart
            /// </summary>
            Multicart = 0x2A,
            /// <summary>
            /// Two SNES controllers replacing the two standard NES controllers
            /// </summary>
            TwoSnesControllers = 0x2B,
            /// <summary>
            /// RacerMate Bicycle
            /// </summary>
            RacerMateBicycle = 0x2C,
            /// <summary>
            /// U-Force
            /// </summary>
            UForce = 0x2D,
            /// <summary>
            /// R.O.B. Stack-Up
            /// </summary>
            RobStackUp = 0x2E,
            /// <summary>
            /// City Patrolman Lightgun
            /// </summary>
            CityPatrolmanLightgun = 0x2F,
            /// <summary>
            /// Sharp C1 Cassette Interface
            /// </summary>
            SharpC1CassetteInterface = 0x30,
            /// <summary>
            /// Standard Controller with swapped Left-Right/Up-Down/B-A
            /// </summary>
            StandardControllerWithSwapped = 0x31,
            /// <summary>
            /// Excalibor Sudoku Pad
            /// </summary>
            ExcaliborSudokuPad = 0x32,
            /// <summary>
            /// ABL Pinball
            /// </summary>
            AblPinball = 0x33,
            /// <summary>
            /// Golden Nugget Casino extra buttons
            /// </summary>
            GoldenNuggetCasinoExtraButtons = 0x34,
        }

        /// <summary>
        /// Constructor to create empty NesFile object
        /// </summary>
        public NesFile()
        {
        }

        /// <summary>
        /// Create NesFile object from raw .nes file contents
        /// </summary>
        /// <param name="data">Raw .nes file data</param>
        public NesFile(byte[] data)
        {
            var header = new byte[16];
            Array.Copy(data, header, header.Length);
            if (header[0] != 'N' ||
                header[1] != 'E' ||
                header[2] != 'S' ||
                header[3] != 0x1A) throw new InvalidDataException("Invalid iNES header");

            if ((header[7] & 0x0C) == 0x08)
                Version = iNesVersion.NES20;
            else if (!(header[12] == 0 && header[13] == 0 && header[14] == 0 && header[15] == 0))
            {
                // archaic iNES
                header[7] = header[8] = header[9] = header[10] = header[11] = header[12] = header[13] = header[14] = header[15] = 0;
            }

            uint prgSize = 0;
            uint chrSize = 0;
            Mirroring = (MirroringType)(header[6] & 1);
            Battery = (header[6] & (1 << 1)) != 0;
            if ((header[6] & (1 << 2)) != 0)
                trainer = new byte[512];
            else
                trainer = Array.Empty<byte>();
            if ((header[6] & (1 << 3)) != 0)
                Mirroring = MirroringType.FourScreenVram;
            if (Version == iNesVersion.iNES)
            {
                prgSize = (uint)(header[4] * 0x4000);
                chrSize = (uint)(header[5] * 0x2000);
                Mapper = (byte)((header[6] >> 4) | (header[7] & 0xF0));
                Console = (ConsoleType)(header[7] & 3);
                PrgRamSize = (uint)(header[8] == 0 ? 0x2000 : header[8] * 0x2000);
            }
            else if (Version == iNesVersion.NES20) // NES 2.0
            {
                if ((header[9] & 0x0F) != 0x0F)
                    prgSize = (uint)((((header[9] & 0x0F) << 8) | header[4]) * 0x4000);
                else
                    prgSize = (uint)((1 << (header[4] >> 2)) * ((header[4] & 3) * 2 + 1)); // omg
                if ((header[9] & 0xF0) != 0xF0)
                    chrSize = (uint)((((header[9] & 0xF0) << 4) | header[5]) * 0x2000);
                else
                    chrSize = (uint)((1 << (header[5] >> 2)) * ((header[5] & 3) * 2 + 1));
                Mapper = (ushort)((header[6] >> 4) | (header[7] & 0xF0) | ((header[8] & 0x0F) << 8));
                Submapper = (byte)(header[8] >> 4);
                Console = (ConsoleType)(header[7] & 3);
                if ((header[10] & 0x0F) > 0)
                    PrgRamSize = (uint)(64 << (header[10] & 0x0F));
                if ((header[10] & 0xF0) > 0)
                    PrgNvRamSize = (uint)(64 << ((header[10] & 0xF0) >> 4));
                if ((header[11] & 0x0F) > 0)
                    ChrRamSize = (uint)(64 << (header[11] & 0x0F));
                if ((header[11] & 0xF0) > 0)
                    ChrNvRamSize = (uint)(64 << ((header[11] & 0xF0) >> 4));
                Region = (Timing)header[12];
                switch (Console)
                {
                    case ConsoleType.VsSystem:
                        VsPpu = (VsPpuType)(header[13] & 0x0F);
                        VsHardware = (VsHardwareType)(header[13] >> 4);
                        break;
                    case ConsoleType.Extended:
                        ExtendedConsole = (ExtendedConsoleType)(header[13] & 0x0F);
                        break;
                }
                MiscellaneousROMsCount = (byte)(header[14] & 3);
                DefaultExpansionDevice = (ExpansionDevice)(header[15] & 0x3F);
            }

            uint offset = (uint)header.Length;
            if (trainer.Length > 0)
            {
                if (offset < data.Length)
                    Array.Copy(data, offset, trainer, 0, Math.Max(0, Math.Min(trainer.Length, data.Length - offset)));
                offset += (uint)trainer.Length;
            }

            prg = new byte[prgSize];
            if (offset < data.Length)
                Array.Copy(data, offset, prg, 0, Math.Max(0, Math.Min(prgSize, data.Length - offset))); // Ignore end for some bad ROMs
            offset += prgSize;

            chr = new byte[chrSize];
            if (offset < data.Length)
                Array.Copy(data, offset, chr, 0, Math.Max(0, Math.Min(chrSize, data.Length - offset)));
            offset += chrSize;

            if (MiscellaneousROMsCount > 0)
            {
                MiscellaneousROM = new byte[data.Length - offset];
                Array.Copy(data, offset, miscellaneousROM, 0, miscellaneousROM.Length);
            }
            else
            {
                MiscellaneousROM = Array.Empty<byte>();
            }
        }

        /// <summary>
        /// Create NesFile object from the specified .nes file 
        /// </summary>
        /// <param name="fileName">Path to the .nes file</param>
        public NesFile(string fileName)
            : this(File.ReadAllBytes(fileName))
        {
        }

        /// <summary>
        /// Create NesFile object from raw .nes file contents
        /// </summary>
        /// <param name="data">Raw ROM data</param>
        /// <returns>NesFile object</returns>
        public static NesFile FromBytes(byte[] data) => new NesFile(data);

        /// <summary>
        /// Create NesFile object from the specified .nes file 
        /// </summary>
        /// <param name="filename">Path to the .nes file</param>
        /// <returns>NesFile object</returns>
        public static NesFile FromFile(string filename) => new NesFile(filename);

        /// <summary>
        /// Returns .nes file contents (header + PRG + CHR)
        /// </summary>
        /// <returns>.nes file contents</returns>
        public byte[] ToBytes()
        {
            var data = new List<byte>();
            var header = new byte[16];
            header[0] = (byte)'N';
            header[1] = (byte)'E';
            header[2] = (byte)'S';
            header[3] = 0x1A;
            ulong prgSizePadded, chrSizePadded;
            if (Version == iNesVersion.iNES)
            {
                if (Console == ConsoleType.Extended)
                    throw new InvalidDataException("Extended console type is supported by NES 2.0 only");
                if (Mapper > 255)
                    throw new InvalidDataException("Mapper number > 255 is supported by NES 2.0 only");
                if (Submapper != 0)
                    throw new InvalidDataException("Submapper number is supported by NES 2.0 only");
                var length16k = prg.Length / 0x4000;
                if (length16k > 0xFF) throw new ArgumentOutOfRangeException("PRG size is too big for iNES, use NES 2.0 instead");
                header[4] = (byte)Math.Ceiling((double)prg.Length / 0x4000);
                prgSizePadded = header[4] * 0x4000UL;
                var length8k = chr.Length / 0x2000;
                if (length8k > 0xFF) throw new ArgumentOutOfRangeException("CHR size is too big for iNES, use NES 2.0 instead");
                header[5] = (byte)Math.Ceiling((double)chr.Length / 0x2000);
                chrSizePadded = header[5] * 0x2000UL;
                switch (Mirroring)
                {
                    case MirroringType.Unknown:          // mirroring field ignored
                    case MirroringType.Horizontal:
                    case MirroringType.Vertical:
                    case MirroringType.FourScreenVram:
                    case MirroringType.MapperControlled: // mirroring field ignored
                        break;
                    default:
                        throw new InvalidDataException($"{Mirroring} mirroring is not supported by iNES");
                }
                // Hard-wired nametable mirroring type
                if (Mirroring == MirroringType.Vertical)
                    header[6] |= 1;
                // "Battery" and other non-volatile memory
                if (Battery)
                    header[6] |= (1 << 1);
                // 512-byte Trainer
                if (trainer.Length > 0)
                    header[6] |= (1 << 2);
                // Hard-wired four-screen mode
                if (Mirroring == MirroringType.FourScreenVram)
                    header[6] |= (1 << 3);
                // Mapper Number D0..D3
                header[6] |= (byte)(Mapper << 4);
                // Console type
                header[7] |= (byte)((byte)Console & 3);
                // Mapper Number D4..D7
                header[7] |= (byte)(Mapper & 0xF0);

                data.AddRange(header);
                if (trainer.Length > 0)
                {
                    data.AddRange(trainer);
                    if (trainer.Length < 512) data.AddRange(Enumerable.Repeat<byte>(0xFF, (int)512 - trainer.Length));
                }
                data.AddRange(prg);
                data.AddRange(Enumerable.Repeat<byte>(byte.MaxValue, (int)prgSizePadded - prg.Length));
                data.AddRange(chr);
                data.AddRange(Enumerable.Repeat<byte>(byte.MaxValue, (int)chrSizePadded - chr.Length));
            }
            else if (Version == iNesVersion.NES20)
            {
                var length16k = (uint)Math.Ceiling((double)prg.Length / 0x4000);
                if (length16k <= 0xEFF)
                {
                    header[4] = (byte)(length16k & 0xFF);
                    header[9] |= (byte)(length16k >> 8);
                    prgSizePadded = length16k * 0x4000;
                }
                else
                {
                    byte exponent, multiplier;
                    (exponent, multiplier, prgSizePadded) = SizeToExponent((ulong)prg.Length);
                    header[4] = (byte)((exponent << 2) | (multiplier & 3));
                    header[9] |= 0x0F;
                }
                var length8k = (uint)Math.Ceiling((double)chr.Length / 0x2000);
                if (length8k <= 0xEFF)
                {
                    header[5] = (byte)(length8k & 0xFF);
                    header[9] |= (byte)((length8k >> 4) & 0xF0);
                    chrSizePadded = length8k * 0x2000;
                }
                else
                {
                    byte exponent, multiplier;
                    (exponent, multiplier, chrSizePadded) = SizeToExponent((ulong)chr.Length);
                    header[5] = (byte)((exponent << 2) | (multiplier & 3));
                    header[9] |= 0xF0;
                }
                // Hard-wired nametable mirroring type
                if (Mirroring == MirroringType.Vertical)
                    header[6] |= 1;
                // "Battery" and other non-volatile memory
                if (Battery)
                    header[6] |= (1 << 1);
                // 512-byte Trainer
                if (trainer.Length > 0)
                    header[6] |= (1 << 2);
                // Hard-wired four-screen mode
                if (Mirroring == MirroringType.FourScreenVram)
                    header[6] |= (1 << 3);
                // Mapper Number D0..D3
                header[6] |= (byte)(Mapper << 4);
                // Console type
                header[7] |= (byte)((byte)Console & 3);
                // NES 2.0 identifier
                header[7] |= 1 << 3;
                // Mapper Number D4..D7
                header[7] |= (byte)(Mapper & 0xF0);
                // Mapper number D8..D11
                header[8] |= (byte)((Mapper >> 8) & 0x0F);
                // Submapper
                header[8] |= (byte)(Submapper << 4);
                // PRG RAM (volatile) shift count
                var prgRamBitSize = PrgRamSize > 0 ? Math.Max(1, (int)Math.Ceiling(Math.Log(PrgRamSize, 2)) - 6) : 0;
                header[10] |= (byte)(prgRamBitSize & 0x0F);
                // PRG-NVRAM/EEPROM (non-volatile) shift count
                var prgNvRamBitSize = PrgNvRamSize > 0 ? Math.Max(1, (int)Math.Ceiling(Math.Log(PrgNvRamSize, 2)) - 6) : 0;
                header[10] |= (byte)((prgNvRamBitSize << 4) & 0xF0);
                // CHR-RAM size (volatile) shift count
                var chrRamBitSize = ChrRamSize > 0 ? Math.Max(1, (int)Math.Ceiling(Math.Log(ChrRamSize, 2)) - 6) : 0;
                header[11] |= (byte)(chrRamBitSize & 0x0F);
                // CHR-NVRAM size (non-volatile) shift count
                var chrNvRamBitSize = ChrNvRamSize > 0 ? Math.Max(1, (int)Math.Ceiling(Math.Log(ChrNvRamSize, 2)) - 6) : 0;
                header[11] |= (byte)((chrNvRamBitSize << 4) & 0xF0);
                // CPU/PPU timing mode
                header[12] |= (byte)((byte)Region & 3);
                switch (Console)
                {
                    // When Byte 7 AND 3 =1: Vs. System Type
                    case ConsoleType.VsSystem:
                        // Vs. PPU Type
                        header[13] |= (byte)((byte)VsPpu & 0x0F);
                        // Vs. Hardware Type
                        header[13] |= (byte)(((byte)VsHardware << 4) & 0xF0);
                        break;
                    // When Byte 7 AND 3 =3: Extended Console Type
                    case ConsoleType.Extended:
                        // Extended Console Type
                        header[13] = (byte)ExtendedConsole;
                        break;
                }
                // Miscellaneous ROMs
                header[14] |= (byte)(MiscellaneousROMsCount & 3);
                // Default Expansion Device
                header[15] |= (byte)((byte)DefaultExpansionDevice & 0x3F);

                data.AddRange(header);
                if (trainer.Length > 0)
                {
                    data.AddRange(trainer);
                    if (trainer.Length < 512) data.AddRange(Enumerable.Repeat(byte.MaxValue, (int)512 - trainer.Length));
                }
                data.AddRange(prg);
                data.AddRange(Enumerable.Repeat(byte.MaxValue, (int)prgSizePadded - prg.Length));
                data.AddRange(chr);
                data.AddRange(Enumerable.Repeat(byte.MaxValue, (int)chrSizePadded - chr.Length));
                if (MiscellaneousROMsCount > 0 || miscellaneousROM.Length > 0)
                {
                    if (MiscellaneousROMsCount == 0)
                        throw new InvalidDataException("MiscellaneousROMsCount is zero while MiscellaneousROM is not empty");
                    if (MiscellaneousROM.Length == 0)
                        throw new InvalidDataException("MiscellaneousROM is empty while MiscellaneousROMsCount is not zero");
                    data.AddRange(miscellaneousROM);
                }
            }
            return data.ToArray();
        }

        /// <summary>
        /// Save as .nes file
        /// </summary>
        /// <param name="filename">Target filename</param>
        public void Save(string filename) => File.WriteAllBytes(filename, ToBytes());

        private static ulong ExponentToSize(byte exponent, byte multiplier)
            => (1UL << exponent) * (ulong)(multiplier * 2 + 1);

        private static (byte Exponent, byte Multiplier, ulong Padded) SizeToExponent(ulong value)
        {
            if (value == 0) return (0, 0, 1);
            if (value < 8)
            {
                var r = SizeToExponent(value << 3);
                return ((byte)(r.Exponent - 3), r.Multiplier, r.Padded >> 3);
            }

            // Calculate bits required to store number
            byte bitsize = 0;
            while (value >> bitsize > 0) bitsize++;

            // Split it into two parts
            var major = value >> (bitsize - 3);
            var minor = value & (ulong)~(0b111 << (bitsize - 3));

            // Round up
            if (minor != 0) major++;

            byte e, m;
            switch (major)
            {
                case 0b100:
                    e = (byte)(bitsize - 1);
                    m = 0; // 0*2+1=1
                    break;
                case 0b101:
                    e = (byte)(bitsize - 3);
                    m = 2; // 2*2+1=5
                    break;
                case 0b110:
                    e = (byte)(bitsize - 2);
                    m = 1; // 1*2+1=3
                    break;
                case 0b111:
                    e = (byte)(bitsize - 3);
                    m = 3; // 3*2+1=7
                    break;
                case 0b1000:
                    e = (byte)bitsize;
                    m = 0; // 0*2+1=1
                    break;
                default:
                    throw new InvalidProgramException();
            }

            return (e, m, ExponentToSize(e, m));
        }

        /// <summary>
        /// Calculate MD5 checksum of ROM (CHR+PRG without header)
        /// </summary>
        /// <returns>MD5 checksum for all PRG and CHR data</returns>
        public byte[] CalculateMD5()
        {
            int prgSizeUpPow2 = 1;
            int chrSizeUpPow2 = 1;
            if (PRG.Length == 0)
                prgSizeUpPow2 = 0; // Is it possible?
            else
                while (prgSizeUpPow2 < PRG.Length) prgSizeUpPow2 <<= 1;
            if (CHR.Length == 0)
                chrSizeUpPow2 = 0;
            else
                while (chrSizeUpPow2 < CHR.Length) chrSizeUpPow2 <<= 1;
            using (var md5 = MD5.Create())
            {
                md5.TransformBlock(prg, 0, prg.Length, null, 0);
                md5.TransformBlock(Enumerable.Repeat<byte>(byte.MaxValue, prgSizeUpPow2 - prg.Length).ToArray(), 0, prgSizeUpPow2 - prg.Length, null, 0);
                md5.TransformBlock(chr, 0, chr.Length, null, 0);
                md5.TransformBlock(Enumerable.Repeat<byte>(byte.MaxValue, chrSizeUpPow2 - chr.Length).ToArray(), 0, chrSizeUpPow2 - chr.Length, null, 0);
                md5.TransformFinalBlock(new byte[0], 0, 0);
                return md5.Hash;
            }
        }

        /// <summary>
        /// Calculate CRC32 checksum of ROM (CHR+PRG without header)
        /// </summary>
        /// <returns>CRC32 checksum for all PRG and CHR data</returns>
        public uint CalculateCRC32()
        {
            int prgSizeUpPow2 = 1;
            int chrSizeUpPow2 = 1;
            if (PRG.Length == 0)
                prgSizeUpPow2 = 0; // Is it possible?
            else
                while (prgSizeUpPow2 < PRG.Length) prgSizeUpPow2 <<= 1;
            if (CHR.Length == 0)
                chrSizeUpPow2 = 0;
            else
                while (chrSizeUpPow2 < CHR.Length) chrSizeUpPow2 <<= 1;
            using (var crc32 = new Crc32())
            {
                crc32.TransformBlock(prg, 0, prg.Length, null, 0);
                crc32.TransformBlock(Enumerable.Repeat<byte>(byte.MaxValue, prgSizeUpPow2 - prg.Length).ToArray(), 0, prgSizeUpPow2 - prg.Length, null, 0);
                crc32.TransformBlock(chr, 0, chr.Length, null, 0);
                crc32.TransformBlock(Enumerable.Repeat<byte>(byte.MaxValue, chrSizeUpPow2 - chr.Length).ToArray(), 0, chrSizeUpPow2 - chr.Length, null, 0);
                crc32.TransformFinalBlock(new byte[0], 0, 0);
                return BitConverter.ToUInt32(crc32.Hash.Reverse().ToArray(), 0);
            }
        }
    }
}