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

FlashWriter.cs « FlashWriters « FamicomDumper - github.com/ClusterM/famicom-dumper-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1611a0d95f9a2d911913c40e6893620a79a12701 (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
using com.clusterrr.Famicom.Containers;
using com.clusterrr.Famicom.DumperConnection;
using RemoteDumper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static com.clusterrr.Famicom.Dumper.FlashWriters.CFIInfo;

namespace com.clusterrr.Famicom.Dumper.FlashWriters
{
    public enum FlashEraseMode { Chip, Sector }
    public record FlashInfo
    {
        public int DeviceSize;
        public int MaximumNumberOfBytesInMultiProgram;
        public EraseBlockRegionInfo[]? Regions;
    }


    public abstract class FlashWriter
    {
        const int MAX_WRITE_ERROR_COUNT = 5;

        protected abstract IFamicomDumperConnectionExt dumper { get; }
        protected abstract int BankSize { get; }
        protected abstract FlashEraseMode EraseMode { get; }
        protected virtual bool UseSubmappers { get => false; }
        protected virtual bool CanUsePpbs { get => false; }
        protected virtual bool NeedEnlarge { get => false; }
        protected virtual void Init()
        {
        }
        protected abstract bool CheckMapper(ushort mapper, byte submapper);
        protected abstract bool CheckMapper(string mapper);
        protected abstract FlashInfo GetFlashInfo();
        protected virtual void InitBanking()
        {
        }
        protected abstract void Erase(int offset);
        protected abstract void Write(byte[] data, int offset);
        protected abstract ushort ReadCrc(int offset);
        protected virtual void PPBClear()
        {
        }
        protected virtual void PPBSet(int offset)
        {
        }
        public abstract void PrintFlashInfo();

        protected virtual byte[] LoadPrg(string filename)
        {
            byte[] PRG;
            var extension = Path.GetExtension(filename).ToLower();
            switch (extension)
            {
                case ".bin":
                    PRG = File.ReadAllBytes(filename);
                    break;
                case ".nes":
                    var nes = NesFile.FromFile(filename);
                    if (!CheckMapper(nes.Mapper, nes.Submapper))
                        Console.WriteLine($"WARNING! Invalid mapper: {nes.Mapper}{(UseSubmappers ? $".{nes.Submapper}" : "")}, most likely it will not work after writing.");
                    PRG = nes.PRG;
                    break;
                case ".unf":
                    var unif = UnifFile.FromFile(filename);
                    var mapper = unif.Mapper!;
                    if (mapper.StartsWith("NES-") || mapper.StartsWith("UNL-") || mapper.StartsWith("HVC-") || mapper.StartsWith("BTL-") || mapper.StartsWith("BMC-"))
                        mapper = mapper[4..];
                    if (!CheckMapper(mapper))
                        Console.WriteLine($"WARNING! Invalid mapper: {unif.Mapper}, most likely it will not work after writing.");
                    PRG = unif.PRG0!;
                    break;
                default:
                    throw new InvalidDataException($"Unknown file extension: {extension}, can't detect file format");
            }
            return PRG;
        }

        public void Write(string filename, IEnumerable<int>? badSectors = null, bool silent = false, bool needCheck = false, bool writePBBs = false, bool ignoreBadSectors = false)
        {
            var PRG = LoadPrg(filename);
            Program.Reset(dumper);
            Init();
            InitBanking();
            var flash = GetFlashInfo();
            if (flash.DeviceSize > 4 * 1024 * 1024)
                Console.WriteLine($"Device size: {flash.DeviceSize / 1024 / 1024} MByte / {flash.DeviceSize / 1024 / 1024 * 8} Mbit");
            else
                Console.WriteLine($"Device size: {flash.DeviceSize / 1024 } KByte / {flash.DeviceSize / 1024 * 8} Kbit");
            if (flash.MaximumNumberOfBytesInMultiProgram > 0)
            {
#if DEBUG
                Console.WriteLine($"Maximum number of bytes in multi-byte program: {flash.MaximumNumberOfBytesInMultiProgram}");
#endif
                if (dumper.ProtocolVersion >= 3)
                    dumper.SetMaximumNumberOfBytesInMultiProgram((uint)flash.MaximumNumberOfBytesInMultiProgram);
            }
            if (PRG.Length > flash.DeviceSize)
                throw new InvalidDataException("This ROM is too big for this cartridge");

            if (NeedEnlarge)
            {
                // Round up to power of 2
                var pow = (int)Math.Ceiling(Math.Log(PRG.Length, 2));
                var upSize = (int)Math.Pow(2, pow);
                if (upSize - PRG.Length > 0)
                    PRG = Enumerable.Concat(PRG, Enumerable.Repeat(byte.MaxValue, upSize - PRG.Length)).ToArray();
                // Enlarge
                while (PRG.Length < flash.DeviceSize)
                    PRG = Enumerable.Concat(PRG, PRG).ToArray();
            }

            try
            {
                if (CanUsePpbs)
                {
                    PPBClear();
                }
            }
            catch (Exception ex)
            {
                if (!silent) Program.PlayErrorSound();
                Console.WriteLine($"ERROR! {ex.Message}. Lets continue anyway.");
            }

            int banks = (int)Math.Ceiling((float)PRG.Length / (float)BankSize);
            int region = 0;
            int totalSector = 0;
            int currentRegionSector = 0;
            int totalBank = 0;
            int currentSectorBank = 0;
            int totalErrorCount = 0;
            int currentErrorCount = 0;
            var newBadSectorsList = new List<int>();
            var stopwatch = new Stopwatch();
            bool sectorContainsData = false;
            stopwatch.Start();

            while (totalBank < banks)
            {
                try
                {
                    int offset = totalBank * BankSize;

                    if (EraseMode == FlashEraseMode.Sector)
                    {
                        if (currentSectorBank * BankSize >= flash.Regions![region].SizeOfBlocks)
                        {
                            totalSector++;
                            currentRegionSector++;
                            currentSectorBank = 0;
                        }
                        if (currentRegionSector > flash.Regions[region].NumberOfBlocks)
                        {
                            region++;
                            currentRegionSector = 0;
                        }
                        if ((badSectors != null) && (badSectors.Contains(totalSector) || newBadSectorsList.Contains(totalSector)))
                        {
                            // Bad sector :( Skip it
                            Console.WriteLine($"Sector #{totalSector} is bad, let's skip it.");
                            totalBank += flash.Regions[region].SizeOfBlocks / BankSize;
                            currentSectorBank += flash.Regions[region].SizeOfBlocks / BankSize;
                            continue;
                        }
                    }

                    if (currentSectorBank == 0)
                    {
                        sectorContainsData = false;
                        // TODO: Should i add option to skip empty sectors erasing?
                        // Erase sector
                        switch (EraseMode)
                        {
                            case FlashEraseMode.Chip:
                                Console.Write($"Erasing sector chip... ");
                                break;
                            case FlashEraseMode.Sector:
                                Console.Write($"Erasing sector #{totalSector}... ");
                                break;
                        }
                        Erase(offset);
                        Console.WriteLine("OK");
                    }

                    var data = PRG.Skip(offset).Take(BankSize).ToArray();
                    sectorContainsData |= data.Where(b => b != 0xFF).Any();
                    var timePassed = stopwatch.Elapsed;
                    var timeEstimated = offset > 0 ? timePassed * PRG.Length / offset : new TimeSpan();
                    Console.Write($"Writing bank #{totalBank}/{banks} ({(offset > 0 ? 100L * offset / PRG.Length : 0)}%, {timePassed.Hours:D2}:{timePassed.Minutes:D2}:{timePassed.Seconds:D2}/{timeEstimated.Hours:D2}:{timeEstimated.Minutes:D2}:{timeEstimated.Seconds:D2})... ");
                    Write(data, offset);
                    Console.WriteLine("OK");

                    if (EraseMode == FlashEraseMode.Sector)
                    {
                        if (((currentSectorBank + 1) * BankSize >= flash.Regions![region].SizeOfBlocks) // end of sector
                            || (totalBank + 1 >= banks)) // last bank
                        {
                            if (CanUsePpbs && writePBBs && sectorContainsData)
                                PPBSet(offset);
                            currentErrorCount = 0;
                        }
                    }

                    totalBank++;
                    currentSectorBank++;
                }
                catch (Exception ex)
                {
                    switch (EraseMode)
                    {
                        case FlashEraseMode.Chip:
                            throw;
                        case FlashEraseMode.Sector:
                            totalErrorCount++;
                            currentErrorCount++;
                            Console.WriteLine($"ERROR: {ex.Message}");
                            if (!silent) Program.PlayErrorSound();
                            if (currentErrorCount >= MAX_WRITE_ERROR_COUNT)
                            {
                                if (!ignoreBadSectors)
                                    throw;
                                else
                                {
                                    newBadSectorsList.Add(totalSector);
                                    currentErrorCount = 0;
                                    Console.WriteLine($"Lets skip sector #{currentRegionSector}");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Lets try again");
                            }
                            // Back to the first bank of the sector
                            totalBank -= currentSectorBank;
                            currentSectorBank = 0;
                            Program.Reset(dumper);
                            InitBanking();
                            FlashHelper.ResetFlash(dumper);
                            continue;
                    }
                }
            }

            var wrongCrcSectorsList = new HashSet<int>();
            if (needCheck)
            {
                Console.WriteLine("Starting verification process");
                Program.Reset(dumper);
                InitBanking();

                banks = PRG.Length / BankSize;
                region = 0;
                totalSector = 0;
                currentRegionSector = 0;
                totalBank = 0;
                currentSectorBank = 0;
                stopwatch = new Stopwatch();
                stopwatch.Start();

                while (totalBank < banks)
                {
                    int offset = totalBank * BankSize;

                    if (EraseMode == FlashEraseMode.Sector)
                    {
                        if (currentSectorBank * BankSize >= flash.Regions![region].SizeOfBlocks)
                        {
                            totalSector++;
                            currentRegionSector++;
                            currentSectorBank = 0;
                        }
                        if (currentRegionSector > flash.Regions[region].NumberOfBlocks)
                        {
                            region++;
                            currentRegionSector = 0;
                        }
                        if ((badSectors != null) && badSectors.Contains(totalSector) || newBadSectorsList.Contains(totalSector))
                        {
                            // Bad sector :( Skip it
                            Console.WriteLine($"Sector #{totalSector} is bad, let's skip it.");
                            totalBank += flash.Regions[region].SizeOfBlocks / BankSize;
                            currentSectorBank += flash.Regions[region].SizeOfBlocks / BankSize;
                            continue;
                        }
                    }

                    ushort crc = Crc16Calculator.CalculateCRC16(PRG, offset, BankSize);
                    var timePassed = stopwatch.Elapsed;
                    var timeEstimated = offset > 0 ? timePassed * PRG.Length / offset : new TimeSpan();
                    Console.Write($"Reading CRC of bank #{totalBank}/{banks} ({(offset > 0 ? 100L * offset / PRG.Length : 0)}%, {timePassed.Hours:D2}:{timePassed.Minutes:D2}:{timePassed.Seconds:D2}/{timeEstimated.Hours:D2}:{timeEstimated.Minutes:D2}:{timeEstimated.Seconds:D2})... ");
                    var crcr = ReadCrc(offset);
                    if (crcr != crc)
                    {
                        Console.WriteLine($"Verification failed: {crcr:X4} != {crc:X4}");
                        if (!silent) Program.PlayErrorSound();
                        switch (EraseMode)
                        {
                            case FlashEraseMode.Chip:
                                wrongCrcSectorsList.Add(totalBank);
                                break;
                            case FlashEraseMode.Sector:
                                wrongCrcSectorsList.Add(currentRegionSector);
                                break;
                        }
                    }
                    else Console.WriteLine($"OK (CRC = {crcr:X4})");

                    totalBank++;
                    currentSectorBank++;
                }
            }

            if (totalErrorCount > 0)
                Console.WriteLine($"Write error count: {totalErrorCount}");
            if (newBadSectorsList.Any())
                Console.WriteLine($"Can't write sectors: {string.Join(", ", newBadSectorsList.OrderBy(s => s))}");
            if (wrongCrcSectorsList.Any())
                Console.WriteLine($"{(EraseMode == FlashEraseMode.Sector ? "Sectors" : "Banks")} with wrong CRC: {string.Join(", ", wrongCrcSectorsList.Distinct().OrderBy(s => s))}");

            if (newBadSectorsList.Any() || wrongCrcSectorsList.Any())
                throw new IOException("Cartridge is not writed correctly");
        }
    }
}