-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcartridge.cpp
872 lines (751 loc) · 28.3 KB
/
cartridge.cpp
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
/*
Copyright 2019-2020 Hydr8gon
This file is part of NooDS.
NooDS 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 3 of the License, or
(at your option) any later version.
NooDS 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 NooDS. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstring>
#include "cartridge.h"
#include "core.h"
#include "settings.h"
FILE *ndsRomFile;
Cartridge::~Cartridge()
{
// Write the save before exiting
writeSave();
// Free the ROM and save memory
if (ndsRomFile) fclose(ndsRomFile);
if (ndsSave) delete[] ndsSave;
if (gbaRom) delete[] gbaRom;
if (gbaSave) delete[] gbaSave;
}
void Cartridge::loadNdsRom(std::string path)
{
// Attempt to load an NDS ROM
ndsRomName = path;
ndsRomFile = fopen(ndsRomName.c_str(), "rb");
if (!ndsRomFile) return;
fseek(ndsRomFile, 0, SEEK_END);
ndsRomSize = ftell(ndsRomFile);
fseek(ndsRomFile, 0, SEEK_SET);
fread(RomHeader, sizeof(uint8_t), 0x1000, ndsRomFile);
fseek(ndsRomFile, 0x4000, SEEK_SET);
fread(SecureArea, sizeof(uint8_t), 0x800, ndsRomFile);
fseek(ndsRomFile, 0, SEEK_SET);
//WriteLog("Done");
// Attempt to load the ROM's save file
ndsSaveName = path.substr(0, path.rfind(".")) + ".sav";
FILE *ndsSaveFile = fopen(ndsSaveName.c_str(), "rb");
if (ndsSaveFile)
{
fseek(ndsSaveFile, 0, SEEK_END);
ndsSaveSize = ftell(ndsSaveFile);
fseek(ndsSaveFile, 0, SEEK_SET);
ndsSave = new uint8_t[ndsSaveSize];
fread(ndsSave, sizeof(uint8_t), ndsSaveSize, ndsSaveFile);
fclose(ndsSaveFile);
}
else
{
// The save size is unknown, so just assume FLASH 512KB and hope the user will change it if it doesn't work
ndsSaveSize = 0x80000;
ndsSave = new uint8_t[ndsSaveSize];
memset(ndsSave, 0xFF, ndsSaveSize * sizeof(uint8_t));
}
}
void Cartridge::loadGbaRom(std::string path)
{
// Attempt to load a GBA ROM
gbaRomName = path;
FILE *gbaRomFile = fopen(gbaRomName.c_str(), "rb");
if (!gbaRomFile) return;
fseek(gbaRomFile, 0, SEEK_END);
gbaRomSize = ftell(gbaRomFile);
fseek(gbaRomFile, 0, SEEK_SET);
gbaRom = new uint8_t[gbaRomSize];
fread(gbaRom, sizeof(uint8_t), gbaRomSize, gbaRomFile);
fclose(gbaRomFile);
// Attempt to load the ROM's save file
gbaSaveName = path.substr(0, path.rfind(".")) + ".sav";
FILE *gbaSaveFile = fopen(gbaSaveName.c_str(), "rb");
if (gbaSaveFile)
{
fseek(gbaSaveFile, 0, SEEK_END);
gbaSaveSize = ftell(gbaSaveFile);
fseek(gbaSaveFile, 0, SEEK_SET);
gbaSave = new uint8_t[gbaSaveSize];
fread(gbaSave, sizeof(uint8_t), gbaSaveSize, gbaSaveFile);
fclose(gbaSaveFile);
}
else
{
const std::string saveStrs[] = { "EEPROM_V", "SRAM_V", "FLASH_V", "FLASH512_V", "FLASH1M_V" };
int match = -1;
// Unlike the DS, a GBA cart's save type can be reliably detected by searching for strings in the ROM
// Search the ROM for a save string so a new save of that type can be created
for (unsigned int i = 0; i < gbaRomSize; i += 4)
{
for (unsigned int j = 0; j < 5; j++)
{
match = j;
for (unsigned int k = 0; k < saveStrs[j].length(); k++)
{
if (i + k >= gbaRomSize || gbaRom[i + k] != saveStrs[j][k])
{
match = -1;
break;
}
}
if (match != -1) break;
}
if (match != -1) break;
}
// Create a new GBA save of the detected type
if (match != -1)
{
switch (match)
{
case 0: // EEPROM
{
// EEPROM can be either 0.5KB or 8KB, so it must be guessed based on how the game uses it
gbaSaveSize = -1;
return;
}
case 1: // SRAM 32KB
{
gbaSaveSize = 0x8000;
printf("Detected SRAM 32KB save type\n");
break;
}
case 2: case 3: // FLASH 64KB
{
gbaSaveSize = 0x10000;
printf("Detected FLASH 64KB save type\n");
break;
}
case 4: // FLASH 128KB
{
gbaSaveSize = 0x20000;
printf("Detected FLASH 128KB save type\n");
break;
}
}
gbaSave = new uint8_t[gbaSaveSize];
memset(gbaSave, 0xFF, gbaSaveSize * sizeof(uint8_t));
}
}
}
//uint8_t _arm[1<<20];
void Cartridge::directBoot()
{
// Extract some information about the initial ARM9 code from the header
uint32_t offset9 = U8TO32(RomHeader, 0x20);
uint32_t entryAddr9 = U8TO32(RomHeader, 0x24);
uint32_t ramAddr9 = U8TO32(RomHeader, 0x28);
uint32_t size9 = U8TO32(RomHeader, 0x2C);
printf("ARM9 code ROM offset: 0x%X\n", offset9);
printf("ARM9 code entry address: 0x%X\n", entryAddr9);
printf("ARM9 RAM address: 0x%X\n", ramAddr9);
printf("ARM9 code size: 0x%X\n", size9);
// Extract some information about the initial ARM7 code from the header
uint32_t offset7 = U8TO32(RomHeader, 0x30);
uint32_t entryAddr7 = U8TO32(RomHeader, 0x34);
uint32_t ramAddr7 = U8TO32(RomHeader, 0x38);
uint32_t size7 = U8TO32(RomHeader, 0x3C);
printf("ARM7 code ROM offset: 0x%X\n", offset7);
printf("ARM7 code entry address: 0x%X\n", entryAddr7);
printf("ARM7 RAM address: 0x%X\n", ramAddr7);
printf("ARM7 code size: 0x%X\n", size7);
/*char buff[256];
sprintf(buff,"0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X",offset9, entryAddr9, ramAddr9, size9, offset7, entryAddr7, ramAddr7,size7);
WriteLog(buff);*/
uint8_t data;
// Load the ROM header into memory
for (uint32_t i = 0; i < 0x170; i++)
core->memory.write<uint8_t>(0, 0x27FFE00 + i, RomHeader[i]);
fseek(ndsRomFile, offset9, SEEK_SET);
// Load the initial ARM9 code into memory
for (uint32_t i = 0; i < size9; i++){
fread(&data, sizeof(uint8_t), 1, ndsRomFile);
core->memory.write<uint8_t>(0, ramAddr9 + i, data);
}
fseek(ndsRomFile, 0, SEEK_SET);
fseek(ndsRomFile, offset7, SEEK_SET);
// Load the initial ARM7 code into memory
for (uint32_t i = 0; i < size7; i++){
fread(&data, sizeof(uint8_t), 1, ndsRomFile);
core->memory.write<uint8_t>(1, ramAddr7 + i, data);
}
fseek(ndsRomFile, 0, SEEK_SET);
}
void Cartridge::writeSave()
{
// Update the NDS save file if necessary
if (ndsSaveDirty)
{
FILE *ndsSaveFile = fopen(ndsSaveName.c_str(), "wb");
if (ndsSaveFile)
{
if (ndsSaveSize > 0)
fwrite(ndsSave, sizeof(uint8_t), ndsSaveSize, ndsSaveFile);
fclose(ndsSaveFile);
ndsSaveDirty = false;
}
}
// Update the GBA save file if necessary
if (gbaSaveDirty)
{
FILE *gbaSaveFile = fopen(gbaSaveName.c_str(), "wb");
if (gbaSaveFile)
{
if (gbaSaveSize > 0)
fwrite(gbaSave, sizeof(uint8_t), gbaSaveSize, gbaSaveFile);
fclose(gbaSaveFile);
gbaSaveDirty = false;
}
}
}
void Cartridge::trimRom(uint8_t **rom, int *romSize, std::string *romName)
{
// Starting from the end, reduce the ROM size until a non-filler word is found
int newSize;
for (newSize = *romSize & ~3; newSize > 0; newSize -= 4)
{
if (U8TO32(*rom, newSize - 4) != 0xFFFFFFFF)
break;
}
if (newSize < *romSize)
{
// Update the ROM in memory
*romSize = newSize;
uint8_t *newRom = new uint8_t[newSize];
memcpy(newRom, *rom, newSize * sizeof(uint8_t));
delete[] *rom;
*rom = newRom;
// Update the ROM file
FILE *romFile = fopen(romName->c_str(), "wb");
if (romFile)
{
if (newSize > 0)
fwrite(*rom, sizeof(uint8_t), newSize, romFile);
fclose(romFile);
}
}
}
void Cartridge::resizeSave(int newSize, uint8_t **save, int *saveSize, bool *saveDirty)
{
}
uint8_t Cartridge::gbaEepromRead()
{
return 0;
}
void Cartridge::gbaEepromWrite(uint8_t value)
{
}
uint8_t Cartridge::gbaSramRead(uint32_t address)
{
return 0xFF;
}
void Cartridge::gbaSramWrite(uint32_t address, uint8_t value)
{
}
void Cartridge::encrypt(uint32_t* data)
{
// Encrypt a 64-bit value using the Blowfish algorithm
// This is a translation of the pseudocode from GBATEK to C++
uint32_t y = data[0];
uint32_t x = data[1];
for (int i = 0x00; i <= 0x0F; i++)
{
uint32_t z = encTable[i] ^ x;
x = encTable[0x012 + ((z >> 24) & 0xFF)];
x = encTable[0x112 + ((z >> 16) & 0xFF)] + x;
x = encTable[0x212 + ((z >> 8) & 0xFF)] ^ x;
x = encTable[0x312 + ((z >> 0) & 0xFF)] + x;
x ^= y;
y = z;
}
data[0] = x ^ encTable[0x10];
data[1] = y ^ encTable[0x11];
}
void Cartridge::decrypt(uint32_t* data)
{
// Decrypt a 64-bit value using the Blowfish algorithm
// This is a translation of the pseudocode from GBATEK to C++
uint32_t y = data[0];
uint32_t x = data[1];
for (int i = 0x11; i >= 0x02; i--)
{
uint32_t z = encTable[i] ^ x;
x = encTable[0x012 + ((z >> 24) & 0xFF)];
x = encTable[0x112 + ((z >> 16) & 0xFF)] + x;
x = encTable[0x212 + ((z >> 8) & 0xFF)] ^ x;
x = encTable[0x312 + ((z >> 0) & 0xFF)] + x;
x ^= y;
y = z;
}
data[0] = x ^ encTable[0x1];
data[1] = y ^ encTable[0x0];
}
void Cartridge::initKeycode(int level)
{
// Initialize the Blowfish encryption table
// This is a translation of the pseudocode from GBATEK to C++
for (int i = 0; i < 0x412; i++)
encTable[i] = core->memory.read<uint32_t>(1, 0x30 + i * 4);
uint32_t code = U8TO32(RomHeader, 0x0C);
encCode[0] = code;
encCode[1] = code / 2;
encCode[2] = code * 2;
if (level >= 1) applyKeycode();
if (level >= 2) applyKeycode();
encCode[1] *= 2;
encCode[2] /= 2;
if (level >= 3) applyKeycode();
}
void Cartridge::applyKeycode()
{
// Apply a keycode to the Blowfish encryption table
// This is a translation of the pseudocode from GBATEK to C++
encrypt(&encCode[1]);
encrypt(&encCode[0]);
for (int i = 0; i <= 0x11; i++)
{
uint32_t byteReverse = 0;
for (int j = 0; j < 4; j++)
byteReverse |= ((encCode[i % 2] >> (j * 8)) & 0xFF) << ((3 - j) * 8);
encTable[i] ^= byteReverse;
}
uint64_t scratch = 0;
uint32_t temp[2] = {0,0};
for (int i = 0; i <= 0x410; i += 2)
{
encrypt(temp);
encTable[i + 0] = temp[1];
encTable[i + 1] = temp[0];
}
}
void Cartridge::writeAuxSpiCnt(bool cpu, uint16_t mask, uint16_t value)
{
// Write to one of the AUXSPICNT registers
mask &= 0xE043;
auxSpiCnt[cpu] = (auxSpiCnt[cpu] & ~mask) | (value & mask);
}
void Cartridge::writeRomCmdOutL(bool cpu, uint32_t mask, uint32_t value)
{
// Write to one of the ROMCMDOUT registers (low)
romCmdOut[cpu] = (romCmdOut[cpu] & ~((uint64_t)mask)) | (value & mask);
}
void Cartridge::writeRomCmdOutH(bool cpu, uint32_t mask, uint32_t value)
{
// Write to one of the ROMCMDOUT registers (high)
romCmdOut[cpu] = (romCmdOut[cpu] & ~((uint64_t)mask << 32)) | ((uint64_t)(value & mask) << 32);
}
void Cartridge::writeAuxSpiData(bool cpu, uint8_t value)
{
// Do nothing if there is no save
if (ndsSaveSize == 0) return;
if (auxWriteCount[cpu] == 0)
{
// On the first write, set the command byte
if (value == 0) return;
auxCommand[cpu] = value;
auxAddress[cpu] = 0;
auxSpiData[cpu] = 0;
}
else
{
switch (ndsSaveSize)
{
case 0x200: // EEPROM 0.5KB
{
switch (auxCommand[cpu])
{
case 0x03: // Read from lower memory
{
if (auxWriteCount[cpu] < 2)
{
// On the second write, set the 1 byte address to read from
auxAddress[cpu] = value;
auxSpiData[cpu] = 0;
}
else
{
// On writes 3+, read data from the save and send it back
auxSpiData[cpu] = (auxAddress[cpu] < 0x200) ? ndsSave[auxAddress[cpu]] : 0;
auxAddress[cpu]++;
}
break;
}
case 0x0B: // Read from upper memory
{
if (auxWriteCount[cpu] < 2)
{
// On the second write, set the 1 byte address to read from
auxAddress[cpu] = 0x100 + value;
auxSpiData[cpu] = 0;
}
else
{
// On writes 3+, read data from the save and send it back
auxSpiData[cpu] = (auxAddress[cpu] < 0x200) ? ndsSave[auxAddress[cpu]] : 0;
auxAddress[cpu]++;
}
break;
}
case 0x02: // Write to lower memory
{
if (auxWriteCount[cpu] < 2)
{
// On the second write, set the 1 byte address to write to
auxAddress[cpu] = value;
auxSpiData[cpu] = 0;
}
else
{
// On writes 3+, write data to the save
if (auxAddress[cpu] < 0x200)
{
ndsSave[auxAddress[cpu]] = value;
ndsSaveDirty = true;
}
auxAddress[cpu]++;
auxSpiData[cpu] = 0;
}
break;
}
case 0x0A: // Write to upper memory
{
if (auxWriteCount[cpu] < 2)
{
// On the second write, set the 1 byte address to write to
auxAddress[cpu] = 0x100 + value;
auxSpiData[cpu] = 0;
}
else
{
// On writes 3+, write data to the save
if (auxAddress[cpu] < 0x200)
{
ndsSave[auxAddress[cpu]] = value;
ndsSaveDirty = true;
}
auxAddress[cpu]++;
auxSpiData[cpu] = 0;
}
break;
}
default:
{
printf("Write to AUX SPI with unknown EEPROM 0.5KB command: 0x%X\n", auxCommand[cpu]);
auxSpiData[cpu] = 0;
break;
}
}
break;
}
case 0x2000: case 0x8000: case 0x10000: case 0x20000: // EEPROM 8KB, 64KB, 128KB; FRAM 32KB
{
switch (auxCommand[cpu])
{
case 0x03: // Read from memory
{
if (auxWriteCount[cpu] < ((ndsSaveSize == 0x20000) ? 4 : 3))
{
// On writes 2-3, set the 2 byte address to read from (not EEPROM 128KB)
// EEPROM 128KB uses a 3 byte address, so it's set on writes 2-4
auxAddress[cpu] |= value << ((((ndsSaveSize == 0x20000) ? 3 : 2) - auxWriteCount[cpu]) * 8);
auxSpiData[cpu] = 0;
}
else
{
// On writes 4+, read data from the save and send it back
auxSpiData[cpu] = (auxAddress[cpu] < ndsSaveSize) ? ndsSave[auxAddress[cpu]] : 0;
auxAddress[cpu]++;
}
break;
}
case 0x02: // Write to memory
{
if (auxWriteCount[cpu] < ((ndsSaveSize == 0x20000) ? 4 : 3))
{
// On writes 2-3, set the 2 byte address to write to (not EEPROM 128KB)
// EEPROM 128KB uses a 3 byte address, so it's set on writes 2-4
auxAddress[cpu] |= value << ((((ndsSaveSize == 0x20000) ? 3 : 2) - auxWriteCount[cpu]) * 8);
auxSpiData[cpu] = 0;
}
else
{
// On writes 4+, write data to the save
if (auxAddress[cpu] < ndsSaveSize)
{
ndsSave[auxAddress[cpu]] = value;
ndsSaveDirty = true;
}
auxAddress[cpu]++;
auxSpiData[cpu] = 0;
}
break;
}
default:
{
printf("Write to AUX SPI with unknown EEPROM/FRAM command: 0x%X\n", auxCommand[cpu]);
auxSpiData[cpu] = 0;
break;
}
}
break;
}
case 0x40000: case 0x80000: case 0x100000: case 0x800000: // FLASH 256KB, 512KB, 1024KB, 8192KB
{
switch (auxCommand[cpu])
{
case 0x03: // Read data bytes
{
if (auxWriteCount[cpu] < 4)
{
// On writes 2-4, set the 3 byte address to read from
auxAddress[cpu] |= value << ((3 - auxWriteCount[cpu]) * 8);
auxSpiData[cpu] = 0;
}
else
{
// On writes 5+, read data from the save and send it back
auxSpiData[cpu] = (auxAddress[cpu] < ndsSaveSize) ? ndsSave[auxAddress[cpu]] : 0;
auxAddress[cpu]++;
}
break;
}
case 0x0A: // Page write
{
if (auxWriteCount[cpu] < 4)
{
// On writes 2-4, set the 3 byte address to write to
auxAddress[cpu] |= value << ((3 - auxWriteCount[cpu]) * 8);
auxSpiData[cpu] = 0;
}
else
{
// On writes 5+, write data to the save
if (auxAddress[cpu] < ndsSaveSize)
{
ndsSave[auxAddress[cpu]] = value;
ndsSaveDirty = true;
}
auxAddress[cpu]++;
auxSpiData[cpu] = 0;
}
break;
}
case 0x08: // IR-related
{
// If a gamecode starts with 'I', the game has an infrared port in its cartridge
// This shares the same SPI as FLASH memory
// Some games check this command as an anti-piracy measure
auxSpiData[cpu] = (RomHeader[0xC] == 'I') ? 0xAA : 0;
break;
}
default:
{
printf("Write to AUX SPI with unknown FLASH command: 0x%X\n", auxCommand[cpu]);
auxSpiData[cpu] = 0;
break;
}
}
break;
}
default:
{
printf("Write to AUX SPI with unknown save size: 0x%X\n", ndsSaveSize);
break;
}
}
}
// Keep track of the write count
if (auxSpiCnt[cpu] & BIT(6)) // Keep chip selected
auxWriteCount[cpu]++;
else // Deselect chip
auxWriteCount[cpu] = 0;
}
void Cartridge::writeRomCtrl(bool cpu, uint32_t mask, uint32_t value)
{
bool transfer = false;
// Set the release reset bit, but never clear it
romCtrl[cpu] |= (value & BIT(29));
// Start a transfer if the start bit changes from 0 to 1
if (!(romCtrl[cpu] & BIT(31)) && (value & BIT(31)))
transfer = true;
// Write to one of the ROMCTRL registers
mask &= 0xDF7F7FFF;
romCtrl[cpu] = (romCtrl[cpu] & ~mask) | (value & mask);
if (!transfer) return;
// Determine the size of the block to transfer
uint8_t size = (romCtrl[cpu] & 0x07000000) >> 24;
switch (size)
{
case 0: blockSize[cpu] = 0; break;
case 7: blockSize[cpu] = 4; break;
default: blockSize[cpu] = 0x100 << size; break;
}
// Reverse the byte order of the ROM command
command[cpu] = 0;
for (int i = 0; i < 8; i++)
command[cpu] |= ((romCmdOut[cpu] >> (i * 8)) & 0xFF) << ((7 - i) * 8);
// Decrypt the ROM command if encryption is enabled
if (encrypted[cpu])
{
initKeycode(2);
uint32_t data[2];
data[0] = command[cpu];
data[1] = command[cpu]>>32;
decrypt(data);
command[cpu] = (data[1]<<32) | data[0];
}
// Handle encryption commands
if (ndsRomFile)
{
if ((command[cpu] >> 56) == 0x3C) // Activate KEY1 encryption mode
{
// Initialize KEY1 encryption
encrypted[cpu] = true;
}
else if (((command[cpu] >> 56) & 0xF0) == 0xA0) // Enter main data mode
{
// Disable KEY1 encryption
// On hardware, this is where KEY2 encryption would start
encrypted[cpu] = false;
}
}
if (blockSize[cpu] == 0)
{
// End the transfer right away if the block size is 0
romCtrl[cpu] &= ~BIT(23); // Word not ready
romCtrl[cpu] &= ~BIT(31); // Block ready
// Disable DS cartridge DMA transfers
core->dma[cpu].disable((cpu == 0) ? 5 : 2);
// Trigger a block ready IRQ if enabled
if (auxSpiCnt[cpu] & BIT(14))
core->interpreter[cpu].sendInterrupt(19);
}
else
{
// Indicate that a word is ready
romCtrl[cpu] |= BIT(23);
// Trigger DS cartridge DMA transfers
core->dma[cpu].trigger((cpu == 0) ? 5 : 2);
readCount[cpu] = 0;
}
}
uint32_t Cartridge::readRomDataIn(bool cpu)
{
// Don't transfer if the word ready bit isn't set
if (!(romCtrl[cpu] & BIT(23)))
return 0;
// Endless 0xFFs are returned on a dummy command or when no cart is inserted
uint32_t value = 0xFFFFFFFF;
if (ndsRomFile)
{
// Interpret the current ROM command
if (command[cpu] == 0x0000000000000000) // Get header
{
// Return the ROM header, repeated every 0x1000 bytes
value = U8TO32(RomHeader, readCount[cpu] % 0x1000);
}
else if (command[cpu] == 0x9000000000000000 || ((command[cpu] >> 56) & 0xF0) == 0x10 ||
command[cpu] == 0xB800000000000000) // Get chip ID
{
// Return the chip ID, repeated every 4 bytes
// ROM dumps don't provide a chip ID, so use a fake one
value = 0x00001FC2;
}
else if (((command[cpu] >> 56) & 0xF0) == 0x20) // Get secure area
{
uint32_t address = ((command[cpu] & 0x0FFFF00000000000) >> 44) * 0x1000;
// Return data from the selected secure area block
if (address == 0x4000 && readCount[cpu] < 0x800)
{
// Encrypt the first 2KB of the first block
// The first 8 bytes of this block should contain the double-encrypted string 'encryObj'
// This string isn't included in ROM dumps, so manually supply it
uint32_t data[2];
if (readCount[cpu] < 8)
{
data[0] = 0x6A624F79;
data[1] = 0x72636E65; // encryObj
}
else
{
data[1] = U8TO32(SecureArea, ((address + readCount[cpu]) & ~7) + 4) << 32;
data[0] = U8TO32(SecureArea, ((address + readCount[cpu]) & ~7));
}
// Encrypt the data
initKeycode(3);
encrypt(data);
// Double-encrypt the 'encryObj' string
if (readCount[cpu] < 8)
{
initKeycode(2);
encrypt(data);
}
value = (((address + readCount[cpu]) & 4) ? data[0] : data[1]);
}
else
{
value = U8TO32(RomHeader, address + readCount[cpu]);
}
}
else if ((command[cpu] & 0xFF00000000FFFFFF) == 0xB700000000000000) // Get data
{
// Return ROM data from the given address
// This command can't read the first 32KB of a ROM, so it redirects the address
// Some games verify that the first 32KB are unreadable as an anti-piracy measure
uint32_t address = (command[cpu] & 0x00FFFFFFFF000000) >> 24;
if (address < 0x8000) address = 0x8000 + (address & 0x1FF);
int addr_tot = address + readCount[cpu];
const uint8_t dataBlockSZ = 4;
uint32_t ROMdata;
/*static int last_addrRead = addr_tot + (dataBlockSZ);
static int read_status_index = -1;*/
//if (addr_tot >= last_addrRead || read_status_index < 0)
{
// last_addrRead = addr_tot + (dataBlockSZ);
fseek(ndsRomFile,addr_tot,SEEK_SET);
fread(&ROMdata,sizeof(uint32_t),1,ndsRomFile);
// read_status_index = 0;
}
if (address + readCount[cpu] < ndsRomSize) value = ROMdata;
//if (address + readCount[cpu] < ndsRomSize) value = U8TO32(ndsRom, address + readCount[cpu]);
}
else if (command[cpu] != 0x9F00000000000000) // Unknown (not dummy)
{
printf("ROM transfer with unknown command: 0x%llX\n", command[cpu]);
value = 0;
}
}
readCount[cpu] += 4;
if (readCount[cpu] == blockSize[cpu])
{
// End the transfer when the block size has been reached
romCtrl[cpu] &= ~BIT(23); // Word not ready
romCtrl[cpu] &= ~BIT(31); // Block ready
// Disable DS cartridge DMA transfers
core->dma[cpu].disable((cpu == 0) ? 5 : 2);
// Trigger a block ready IRQ if enabled
if (auxSpiCnt[cpu] & BIT(14))
core->interpreter[cpu].sendInterrupt(19);
}
else
{
// Trigger DS cartridge DMA transfers until the block size is reached
core->dma[cpu].trigger((cpu == 0) ? 5 : 2);
}
return value;
}