-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cart.cxx
561 lines (483 loc) · 16.6 KB
/
Cart.cxx
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
//============================================================================
//
// K K RRRR OOO K K CCCC OOO M M
// K K R R O O K K C O O MM MM
// KKK RRRR O O KKK C O O M M M "Krokodile Cart software"
// K K R R O O K K C O O M M
// K K R R OOO K K CCCC OOO M M
//
// Copyright (c) 2009-2020 by Stephen Anthony <sa666666@gmail.com>
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#include <cstring>
#include <fstream>
#include <sstream>
#include "BSType.hxx"
#include "Cart.hxx"
#include "MultiCart.hxx"
#include "CartDetector.hxx"
#include "SerialPort.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cart::create(const string& filename, const string& type)
{
// Get the cart image
memset(myCart, 0, MAXCARTSIZE);
myCartSize = readFile(filename, myCart, MAXCARTSIZE);
// Auto-detect the bankswitch type
if(myType == BS_AUTO || type == "")
{
myType = CartDetector::autodetectType(filename, myCart, myCartSize);
cout << "Bankswitch type: " << Bankswitch::typeToName(myType).c_str()
<< " (auto-detected)" << endl;
}
else
{
myType = Bankswitch::nameToType(type);
cout << "Bankswitch type: " << Bankswitch::typeToName(myType).c_str()
<< " (WARNING: overriding auto-detection)" << endl;
}
switch(myType)
{
case BS_F0:
case BS_E0:
case BS_FE:
case BS_AR:
case BS_NONE:
case BS_DPC:
case BS_DPCP:
case BS_4A50:
case BS_X07:
case BS_SB:
case BS_MC:
{
ostringstream out;
out << "Bankswitch mode \'" << Bankswitch::typeToName(myType).c_str() << "\' is not supported.";
myLogMessage = out.str();
cout << myLogMessage.c_str() << endl;
return myIsValid = false;
}
default:
break;
}
// Pad sub-4K images to minimum size
if(myType == BS_4K && myCartSize < 4096)
{
padImage(myCart, myCartSize, 4096);
myCartSize = 4096;
}
// 3F and 3E carts need the upper bank in uppermost part of the ROM
if(myType == BS_3F || myType == BS_3E)
for(int i = 0; i < 2048; i++)
myCart[MAXCARTSIZE - 2048 + i] = myCart[myCartSize - 2048 + i];
for(uInt32 i = 0; i < MAXCARTSIZE/256; ++i)
myModifiedSectors[i] = true;
myIsValid = myCartSize > 0;
if(myIsValid)
myLogMessage = "Cartridge is valid.";
else
myLogMessage = "Invalid cartridge.";
cout << myLogMessage.c_str() << endl;
return myIsValid;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cart::createMultiFile(const StringList& menuNames, const StringList& fileNames,
BSType type, bool ntsc, const string& romfile)
{
myIsValid = false;
myLogMessage = "Invalid cartridge.";
myCartSize = 0;
myType = BS_NONE;
memset(myCart, 0, MAXCARTSIZE);
uInt8 *cart = myCart, menubuffer[13];
// Rudimentary consistency check of lists
if(menuNames.size() != fileNames.size())
{
myLogMessage = "Menu and file names don't match.";
return false;
}
// Determine the bankswitch scheme each ROM should have
BSType romType = BS_NONE;
int size = 0;
const uInt8* menuPtr = NULL;
switch(type)
{
case BS_MC4K: romType = BS_4K; menuPtr = MC_4KMenu; size = 0; break;
case BS_MCF8: romType = BS_F8; menuPtr = MC_F8Menu; size = 1; break;
case BS_MCF6: romType = BS_F6; menuPtr = MC_F6Menu; size = 2; break;
case BS_MCF4: romType = BS_F4; menuPtr = MC_F4Menu; size = 3; break;
default:
myLogMessage = "Invalid multicart bankswitch scheme.";
return false;
}
uInt8* imgbuf = new uInt8[MC_ByteSizes[size]];
// Add the menu image
memcpy(myCart, menuPtr, MC_ByteSizes[size]);
// Set menu title and clear all menu entries
menuEntry(menubuffer, "- KROKOCART -");
for (int charpos = 0; charpos < 13; ++charpos)
cart[MC_MenuOffset[size] + charpos] = menubuffer[charpos];
for (int charpos = 13; charpos < (13 * 127); ++charpos)
cart[MC_MenuOffset[size] + charpos] = 0;
cart += MC_ByteSizes[size];
myCartSize += MC_ByteSizes[size];
// Scan through each item in the list(s)
int numEntries = std::min(int(menuNames.size()), MC_MaxEntries[size]);
int validEntries = 0;
for(int i = 0; i < numEntries; ++i)
{
int imgsize = readFile(fileNames[i], imgbuf, MC_ByteSizes[size]);
BSType imgtype = CartDetector::autodetectType(fileNames[i], imgbuf, imgsize);
if(imgtype == romType || imgtype == BS_4K)
{
if(imgsize < MC_ByteSizes[size])
padImage(imgbuf, imgsize, MC_ByteSizes[size]);
// Add the image
memcpy(cart, imgbuf, MC_ByteSizes[size]);
cart += MC_ByteSizes[size]; // Point to position for next cart
myCartSize += MC_ByteSizes[size]; // Cart size increases
++validEntries;
// Add menu entry
menuEntry(menubuffer, menuNames[i]);
for (int charpos = 0; charpos < 13; ++charpos)
myCart[MC_MenuOffset[size] + ((validEntries) * 13) + charpos] =
menubuffer[charpos];
}
else
cout << "Multicart image " << i << " skipped; invalid bankswitch type \'"
<< Bankswitch::typeToName(imgtype).c_str() << "\'" << endl;
}
delete[] imgbuf;
// Set PAL/NTSC
cout << "Setting " << (ntsc ? "NTSC" : "PAL") << " multicart menu type." << endl;
myCart[MC_MenuOffset[size] + 2046] = ntsc ? 128 : 0;
// Set number of menu entries
cout << "Multicart has " << validEntries << " menu entries." << endl;
myCart[MC_MenuOffset[size] + 2047] = (uInt8)validEntries;
myIsValid = validEntries > 0;
// Save the image to an external file
ostringstream buf;
if(myIsValid)
{
if(romfile != "")
{
if(writeFile(romfile, myCart, myCartSize) == 0)
{
myLogMessage = "Couldn't open multicart output file.";
return false;
}
// Add info for this ROM to the database, since autodetection won't know what it is
CartDetector::addRomInfo(romfile, type, myCart, myCartSize);
}
buf << (ntsc ? "NTSC" : "PAL") << " multicart created with " << validEntries << " entries";
if((int)menuNames.size() > validEntries)
buf << " (skipped " << (menuNames.size() - validEntries) << ")";
buf << ", size = " << myCartSize << ".";
myLogMessage = buf.str();
}
else
{
buf << "Skipped " << (numEntries - validEntries) << " invalid entries, multicart not created.";
myLogMessage = buf.str();
}
myType = type;
return myIsValid;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 Cart::initSectors(bool downloadMode)
{
myCurrentSector = 0;
if(myIsValid)
{
myNumSectors = myCartSize / 256; // the number of 256 byte sectors
if(myType == BS_3F || myType == BS_3E) // 3F and 3E add 8 more (2040 - 2047)
myNumSectors += 8;
for(uInt32 i = 0; i < MAXCARTSIZE/256; ++i)
myModifiedSectors[i] = true;
// Determine which sectors should be written to the KrokCart
if(downloadMode)
{
ostringstream out;
if(myIncremental)
{
// Read the last rom written
uInt8 buffer[MAXCARTSIZE];
memset(buffer, 0, MAXCARTSIZE);
readFile(ourLastCart, buffer, MAXCARTSIZE, false);
// Determine which 256 byte blocks differ
int count = 0;
uInt8 *cart = myCart, *buf = buffer;
for(uInt32 i = 0; i < myCartSize/256; ++i, cart += 256, buf += 256)
{
myModifiedSectors[i] = memcmp(cart, buf, 256) != 0;
if(myModifiedSectors[i]) ++count;
}
out << "Incremental download mode, " << count << " / "
<< (myCartSize/256) << " sectors are changed.";
myLogMessage = out.str();
}
else
{
out << "Normal download mode, " << myNumSectors << " / "
<< (myCartSize/256) << " sectors are changed.";
myLogMessage = out.str();
}
cout << myLogMessage.c_str() << endl;
}
}
else
myNumSectors = 0;
return myNumSectors;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 Cart::writeNextSector(SerialPort& port)
{
if(!myIsValid)
throw "write: Invalid cart";
else if(myCurrentSector == myNumSectors)
throw "write: All sectors already written";
uInt16 sector = myCurrentSector;
uInt32 retry = 0;
// Only write the sector if it has changed
if(!myIncremental || myModifiedSectors[sector])
{
bool status;
while(!(status = downloadSector(sector, port)) && retry++ < myRetry)
cout << "Write transmission of sector " << sector << " failed, retry " << retry << endl;
if(!status)
throw "write: failed max retries";
}
// Handle 3F and 3E carts, which are a little different from the rest
// There are two ranges of sectors; the second starts once we past the
// cart size
myCurrentSector++;
if((myType == BS_3F || myType == BS_3E) &&
myCurrentSector == myCartSize / 256)
myCurrentSector = 2040;
return sector;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 Cart::verifyNextSector(SerialPort& port)
{
if(!myIsValid)
throw "verify: Invalid cart";
else if(myCurrentSector == myNumSectors)
throw "verify: All sectors already verified";
uInt16 sector = myCurrentSector;
uInt32 retry = 0;
bool status;
while(!(status = verifySector(sector, port)) && retry++ < myRetry)
cout << "Read transmission of sector " << sector << " failed, retry " << retry << endl;
if(!status)
throw "verify: failed max retries";
// Handle 3F and 3E carts, which are a little different from the rest
// There are two ranges of sectors; the second starts once we past the
// cart size
myCurrentSector++;
if((myType == BS_3F || myType == BS_3E) &&
myCurrentSector == myCartSize / 256)
myCurrentSector = 2040;
return sector;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cart::finalizeSectors()
{
bool status = false;
ostringstream out;
if(myCurrentSector == myNumSectors)
{
if(myIncremental)
{
int count = 0;
for(uInt32 i = 0; i < myCartSize/256; ++i)
if(myModifiedSectors[i]) ++count;
out << "Incremental download complete, wrote " << count << " / " << myNumSectors << " sectors.";
}
else
out << "Download complete, wrote " << myNumSectors << " sectors.";
// Write out the current ROM to use for comparison next time
writeFile(ourLastCart, myCart, MAXCARTSIZE, false);
status = true;
}
else
out << "Download failure on sector " << myCurrentSector << ".";
myLogMessage = out.str();
return status;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt32 Cart::readFile(const string& filename, uInt8* buffer, uInt32 maxSize,
bool showmessage) const
{
if(showmessage) cout << "Reading from file: \'" << filename.c_str() << "\' ... ";
// Read file into buffer
ifstream in(filename, std::ios::binary);
if(!in)
return 0;
// Figure out how much data we should read
in.seekg(0, std::ios::end);
std::streampos length = in.tellg();
in.seekg(0, std::ios::beg);
uInt32 size = length > maxSize ? maxSize : uInt32(length);
in.read((char*)buffer, size);
if(showmessage) cout << "read in " << size << " bytes" << endl;
in.close();
return size;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt32 Cart::writeFile(const string& filename, uInt8* buffer, uInt32 size,
bool showmessage) const
{
if(showmessage) cout << "Writing to file: \'" << filename.c_str() << "\' ... ";
// Write to file from buffer
ofstream out(filename, std::ios::binary);
if(!out)
return 0;
out.write((char*)buffer, size);
if(showmessage) cout << "wrote out " << size << " bytes" << endl;
out.close();
return size;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Cart::padImage(uInt8* buffer, uInt32 bufsize, uInt32 requiredsize) const
{
// Pad buffer to minimum size, aligning to power-of-2 boundary
if(bufsize < requiredsize)
{
cout << " Converting to " << (requiredsize/1024) << "K." << endl;
// Determine power-of-2 boundary
uInt32 power2 = 1;
while(power2 < bufsize)
power2 <<= 1;
// Erase all garbage after the valid data
memset(buffer+bufsize, 0, requiredsize-bufsize);
// Lay down a copy of the valid buffer data at power-of-2 intervals
uInt8* tmp_ptr = buffer + power2;
for(uInt32 i = 1; i < requiredsize/power2; ++i, tmp_ptr += power2)
memcpy(tmp_ptr, buffer, bufsize);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cart::downloadSector(uInt32 sector, SerialPort& port) const
{
uInt8 buffer[262];
buffer[0] = 1; // Mark start of command
buffer[1] = 0; // Command # for 'Download Sector'
buffer[2] = (uInt8)((sector >> 8) & 0xff); // Sector # Hi-Byte
buffer[3] = (uInt8)sector; // Sector # Lo-Byte
buffer[4] = (uInt8)myType; // Bankswitching mode
uInt8 chksum = 0;
for(int i = 0; i < 256; i++)
buffer[5+i] = myCart[(sector*256) + i];
for(int i = 2; i < 261; i++)
chksum ^= buffer[i];
buffer[261] = chksum;
// Write sector to serial port
if(port.send(buffer, 262) != 262)
{
cout << "Transmission error in downloadSector" << endl;
return false;
}
// Check return code of sector write
uInt8 result = 0;
port.receive(&result, 1);
// Check return code
if(result == 0x7c)
{
cout << "Checksum Error for sector " << sector << endl;
return false;
}
else if(result == 0xff)
{
return true;
}
else
{
cout << "Undefined response " << (int)result << " for sector " << sector << endl;
return false;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Cart::verifySector(uInt32 sector, SerialPort& port) const
{
uInt8 buffer[257];
uInt8 chksum = 0;
buffer[0] = 1; // Mark start of command
buffer[1] = 1; // Command # for 'Read Sector'
buffer[2] = (uInt8)((sector >> 8) & 0xff); // Sector # Hi-Byte
buffer[3] = (uInt8)sector; // Sector # Lo-Byte
chksum ^= buffer[2];
chksum ^= buffer[3];
buffer[4] = chksum; // Chksum
// Write command to serial port
if(port.send(buffer, 5) != 5)
{
cout << "Write transmission error of command in verifySector" << endl;
return false;
}
// Check return code of command write
uInt8 result = 0;
port.receive(&result, 1);
// Check return code
if(result == 0x00)
{
cout << "Checksum Error for verify sector " << sector << endl;
return false;
}
else if(result != 0xfe)
{
cout << "Undefined response " << (int)result << " for sector " << sector << endl;
return false;
}
// Now it's safe to read the sector (256 data bytes + 1 chksum)
int BytesRead = 0;
do
{
uInt8 data = 0;
if(port.receive(&data, 1) == 1)
buffer[BytesRead++] = data;
}
while(BytesRead < 257);
port.send(buffer, 1); // Send an Ack
// Make sure the data chksum matches
chksum = 0;
for(int i = 0; i < 256; ++i)
chksum ^= buffer[i];
if(chksum != buffer[256])
return false;
// Now that we have a valid sector read back from the device,
// compare to the actual data to make sure they match
return memcmp(myCart + sector*256, &buffer, 256) == 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Cart::menuEntry(uInt8* menuentry, const string& menuname) const
{
// Iterate through characters in name
int charpos;
char next;
for (uInt32 stringpos = 0; stringpos < 13; stringpos ++)
{
// Pad with spaces once we have reached the end of the name
if (stringpos >= menuname.length())
charpos = 0;
else
{
// Retrieve next character from name
next = menuname[stringpos];
for (charpos = (sizeof(MC_NameChars) - 1); charpos > 0; charpos --)
{
// Check for character in MCNameChars
if (next == MC_NameChars[charpos])
break;
}
}
menuentry[stringpos] = (uInt8)(charpos * 5); // Chars are 5 lines high
}
// Copy first character to last place (oddity of krok menu software)
uInt8 first = menuentry[0];
for (charpos = 1 ; charpos < 13; charpos ++)
menuentry[charpos - 1] = menuentry[charpos];
menuentry[12] = first;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string Cart::ourLastCart = "";