-
Notifications
You must be signed in to change notification settings - Fork 16
/
initc.c
2681 lines (2263 loc) · 70 KB
/
initc.c
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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1997-2008 ZSNES Team ( zsKnight, _Demo_, pagefault, Nach )
http://www.zsnes.com
http://sourceforge.net/projects/zsnes
https://zsnes.bountysource.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef __UNIXSDL__
#include "gblhdr.h"
#include "linux/audio.h"
#else
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#endif
#include "asm_call.h"
#include "c_init.h"
#include "c_vcache.h"
#include "cfg.h"
#include "chips/c_dsp2proc.h"
#include "chips/c_sa1regs.h"
#include "chips/msu1emu.h"
#include "cpu/c_regs.h"
#include "cpu/c_regsw.h"
#include "cpu/execute.h"
#include "cpu/memtable.h"
#include "cpu/regs.h"
#include "cpu/spc700.h"
#include "endmem.h"
#include "gui/gui.h"
#include "gui/guimisc.h"
#include "gui/guiwindp.h"
#include "init.h"
#include "initc.h"
#include "input.h"
#include "macros.h"
#include "ui.h"
#include "video/procvid.h"
#include "zpath.h"
#ifdef QT_DEBUGGER
#include "debugger/load.h"
#endif
#define NUMCONV_FR4
#include "numconv.h"
#ifndef __GNUC__
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif
// NSRT Goodness
#define Lo 0x7FC0
#define Hi 0xFFC0
#define EHi 0x40FFC0
#define ELo 0x407FC0 // FuSoYa: add support for ExLoROM
#define MB_bytes 0x100000
#define Mbit_bytes 0x20000
// Offsets to add to infoloc start to reach particular variable
#define BankOffset 21 // Contains Speed as well
#define TypeOffset 22
#define ROMSizeOffset 23
#define SRAMSizeOffset 24
#define CountryOffset 25
#define CompanyOffset 26
#define VersionOffset 27
#define InvCSLowOffset 28
#define InvCSHiOffset 29
#define CSLowOffset 30
#define CSHiOffset 31
// Additional defines for the BS header
#define BSYearOffset 21 // Not sure how to calculate year yet
#define BSMonthOffset 22
#define BSDayOffset 23
#define BSBankOffset 24
#define BSSizeOffset 25 // Contains Type as well
// 26 - 31 is the same
#define ResetLoOffset 60
#define ResetHiOffset 61
u1 ComboHeader[23] = "Key Combination File\x1A\x01";
u1 sramsavedis;
// Some archaic code from an unfinished Dynarec
extern uint32_t curexecstate;
void procexecloop(void)
{
curexecstate &= 0xFFFFFF00;
if (spcon) {
curexecstate += 3;
} else {
curexecstate += 1;
}
}
void Debug_WriteString(char* str)
{
FILE* fp = 0;
fp = fopen_dir(ZCfgPath, "zsnes.dbg", "w");
if (!fp) {
return;
}
fputs(str, fp);
fclose(fp);
}
// I want to port over the more complicated
// functions from init.asm, or replace with
// better versions from NSRT. -Nach
// init.asm goodness
extern uint32_t NumofBanks;
extern uint32_t NumofBytes;
static uint8_t Interleaved;
uint32_t maxromspace;
uint32_t curromspace;
uint32_t infoloc;
uint32_t ramsize;
uint32_t ramsizeand;
bool SplittedROM;
uint32_t addOnStart;
uint32_t addOnSize;
// Deinterleave functions
bool validChecksum(uint8_t* ROM, int32_t BankLoc)
{
if (ROM[BankLoc + InvCSLowOffset] + (ROM[BankLoc + InvCSHiOffset] << 8) + ROM[BankLoc + CSLowOffset] + (ROM[BankLoc + CSHiOffset] << 8) == 0xFFFF) {
return (true);
}
return (false);
}
bool valid_normal_bank(uint8_t bankbyte)
{
switch (bankbyte) {
case 32:
case 33:
case 48:
case 49:
return (true);
break;
}
return (false);
}
bool EHiHeader(uint8_t* ROM, int32_t BankLoc)
{
if (validChecksum(ROM, BankLoc) && (ROM[BankLoc + BankOffset] == 53 || ROM[BankLoc + BankOffset] == 37)) {
return (true);
}
return (false);
}
// FuSoYa: add support for ExLoROM
bool ELoHeader(unsigned char* ROM, int BankLoc)
{
if (validChecksum(ROM, BankLoc) && (ROM[BankLoc + BankOffset] == 0x30 || ROM[BankLoc + BankOffset] == 0x20)) {
return (true);
}
return (false);
}
void SwapData(uint32_t* loc1, uint32_t* loc2, uint32_t amount)
{
uint32_t temp;
while (amount--) {
temp = *loc1;
*loc1++ = *loc2;
*loc2++ = temp;
}
}
void swapBlocks(uint8_t* blocks)
{
uint_fast32_t i, j;
for (i = 0; i < NumofBanks; i++) {
for (j = 0; j < NumofBanks; j++) {
if (blocks[j] == (int8_t)i) {
int8_t b;
SwapData(((uint32_t*)romdata + blocks[i] * 0x2000), ((uint32_t*)romdata + blocks[j] * 0x2000), 0x2000);
b = blocks[j];
blocks[j] = blocks[i];
blocks[i] = b;
break;
}
}
}
}
void deintlv1()
{
uint8_t blocks[256];
int_fast32_t i;
int32_t numblocks = NumofBanks / 2;
for (i = 0; i < numblocks; i++) {
blocks[i * 2] = i + numblocks;
blocks[i * 2 + 1] = i;
}
swapBlocks(blocks);
}
void CheckIntl1(uint8_t* ROM)
{
uint32_t ROMmidPoint = NumofBytes / 2;
if (validChecksum(ROM, ROMmidPoint + Lo) && !validChecksum(ROM, Lo) && ROM[ROMmidPoint + Lo + CountryOffset] < 14) // Country Code
{
deintlv1();
Interleaved = true;
} else if (validChecksum(ROM, Lo) && !validChecksum(ROM, Hi) && ROM[Lo + CountryOffset] < 14 && // Country code
// Rom make up
(ROM[Lo + BankOffset] == 33 || ROM[Lo + BankOffset] == 49 || ROM[Lo + BankOffset] == 53 || ROM[Lo + BankOffset] == 58)) {
if (ROM[Lo + 20] == 32 || // Check that Header name did not overflow
!(ROM[Lo + BankOffset] == ROM[Lo + 20] || ROM[Lo + BankOffset] == ROM[Lo + 19] || ROM[Lo + BankOffset] == ROM[Lo + 18] || ROM[Lo + BankOffset] == ROM[Lo + 17])) {
deintlv1();
Interleaved = true;
}
}
}
void CheckIntlEHi(uint8_t* ROM)
{
if (EHiHeader(ROM, Lo)) {
uint32_t oldNumBanks = NumofBanks;
// Swap 4MB ROM with the other one
SwapData((uint32_t*)romdata, ((uint32_t*)romdata + ((NumofBytes - 0x400000) / 4)), 0x100000);
// Deinterleave the 4MB ROM first
NumofBanks = 128;
deintlv1();
// Now the other one
NumofBanks = oldNumBanks - 128;
romdata += 0x100000; // Ofset pointer
deintlv1();
// Now fix the data and we're done
NumofBanks = oldNumBanks;
romdata -= 0x100000;
Interleaved = true;
}
}
// ROM loading functions, which some strangly enough were in guiload.inc
bool AllASCII(unsigned char* b, int32_t size)
{
int_fast32_t i;
for (i = 0; i < size; i++) {
if (b[i] && (b[i] < 32 || b[i] > 126)) {
return (false);
}
}
return (true);
}
// Code to detect if opcode sequence is a valid and popular one for an SNES ROM
// Code by Cowering
static bool valid_start_sequence(uint8_t opcode1, uint8_t opcode2, uint8_t opcode3)
{
switch (opcode1) {
case 0x78:
case 0x5c:
case 0x18:
case 0xad:
return (true);
break;
case 0x4b:
if (opcode2 == 0xab && (opcode3 == 0x18 || opcode3 == 0x20)) {
return (true);
}
break;
case 0x4c:
if ((opcode2 == 0x00 || opcode2 == 0xc0) && opcode3 == 0x84) {
return (true);
}
if (opcode2 == 0x6d && opcode3 == 0x86) {
return (true);
}
if (opcode2 == 0x00 && opcode3 == 0x80) {
return (true);
}
break;
case 0xc2:
if (opcode2 == 0x30 && opcode3 == 0xa9) {
return (true);
}
break;
case 0x20:
if ((opcode2 == 0x16 || opcode2 == 0x06) && opcode3 == 0x80) {
return (true);
}
break;
case 0x80:
if ((opcode2 == 0x16 && opcode3 == 0x4c) || (opcode2 == 0x07 && opcode3 == 0x82)) {
return (true);
}
break;
case 0x9c:
if (opcode2 == 0x00 && opcode3 == 0x21) {
return (true);
}
break;
case 0xa2:
if (opcode2 == 0xff && opcode3 == 0x86) {
return (true);
}
break;
case 0xa9:
if ((opcode2 == 0x00 && (opcode3 = 0x48 || opcode3 == 0x4b)) || (opcode2 == 0x8f && opcode3 == 0x8d) || (opcode2 == 0x20 && opcode3 == 0x4b) || (opcode2 == 0x1f && opcode3 == 0x4b)) {
return (true);
}
break;
}
return (false);
}
static int16_t valid_reset(uint8_t* Buffer)
{
uint8_t* ROM = romdata;
uint16_t Reset = Buffer[ResetLoOffset] | ((uint16_t)Buffer[ResetHiOffset] << 8);
if ((Reset != 0xFFFF) && (Reset & 0x8000)) {
uint8_t opcode1 = ROM[(Reset + 0) & 0x7FFF];
uint8_t opcode2 = ROM[(Reset + 1) & 0x7FFF];
uint8_t opcode3 = ROM[(Reset + 2) & 0x7FFF];
if (valid_start_sequence(opcode1, opcode2, opcode3)) {
return (10);
}
return (2);
}
return (-4);
}
int32_t InfoScore(uint8_t* Buffer)
{
int32_t score = valid_reset(Buffer);
if (validChecksum(Buffer, 0)) {
score += 5;
}
if (Buffer[CompanyOffset] == 0x33) {
score += 3;
}
if (!Buffer[ROMSizeOffset]) {
score += 2;
}
if ((1 << (Buffer[ROMSizeOffset] - 7)) > 48) {
score -= 2;
}
if ((8 << Buffer[SRAMSizeOffset]) > 1024) {
score -= 2;
}
if (Buffer[CountryOffset] < 14) {
score += 2;
}
if (!AllASCII(Buffer, 20)) {
score -= 2;
}
if (valid_normal_bank(Buffer[BSBankOffset])) {
score += 2;
}
return (score);
}
extern uint8_t ForceHiLoROM;
void BankCheck()
{
uint8_t* ROM = romdata;
infoloc = 0;
Interleaved = false;
if (NumofBytes < Lo) {
romtype = 1;
infoloc = 1; // Whatever, we just need a valid location
}
if (NumofBytes < Hi) {
romtype = 1;
infoloc = Lo;
}
if (NumofBytes >= 0x500000) {
// Deinterleave if neccesary
CheckIntlEHi(ROM);
if (EHiHeader(ROM, EHi)) {
romtype = 2;
infoloc = EHi;
} else if (ELoHeader(ROM, ELo)) { // FuSoYa:Add support for ExLoROM
romtype = 1;
infoloc = ELo;
}
}
if (!infoloc) {
static bool CommandLineForce2 = false;
int32_t loscore, hiscore;
// Deinterleave if neccesary
CheckIntl1(ROM);
loscore = InfoScore(ROM + Lo);
hiscore = InfoScore(ROM + Hi);
switch (ROM[Lo + BankOffset]) {
case 32:
case 35:
case 48:
case 50:
loscore += 3;
break;
}
switch (ROM[Hi + BankOffset]) {
case 33:
case 49:
case 53:
case 58:
hiscore += 3;
break;
}
/*
Force code.
ForceHiLoROM is from the GUI.
forceromtype is from Command line, we have a static var
to prevent forcing a secong game loaded from the GUI when
the first was loaded from the command line with forcing.
*/
if (ForceHiLoROM == 1 || (forceromtype == 1 && !CommandLineForce2)) {
CommandLineForce2 = true;
loscore += 50;
} else if (ForceHiLoROM == 2 || (forceromtype == 2 && !CommandLineForce2)) {
CommandLineForce2 = true;
hiscore += 50;
}
if (hiscore > loscore) {
romtype = 2;
infoloc = Hi;
} else {
romtype = 1;
infoloc = Lo;
}
}
}
// Chip detection functions
bool CHIPBATT, BSEnable, C4Enable, DSP1Enable, DSP2Enable, DSP3Enable;
bool DSP4Enable, OBCEnable, RTCEnable, SA1Enable, SDD1Enable, SFXEnable;
bool SETAEnable; // ST010 & 11
bool SGBEnable, SPC7110Enable, ST18Enable, MSUEnable;
void chip_detect()
{
uint8_t* ROM = romdata;
C4Enable = RTCEnable = SA1Enable = SDD1Enable = OBCEnable = CHIPBATT = false;
SGBEnable = ST18Enable = DSP1Enable = DSP2Enable = DSP3Enable = false;
DSP4Enable = SPC7110Enable = BSEnable = SFXEnable = SETAEnable = MSUEnable = false;
// DSP Family
if (ROM[infoloc + TypeOffset] == 3) {
if (ROM[infoloc + BankOffset] == 48) {
DSP4Enable = true;
} else {
DSP1Enable = true;
}
return;
}
if (ROM[infoloc + TypeOffset] == 5) {
CHIPBATT = true;
if (ROM[infoloc + BankOffset] == 32) {
DSP2Enable = true;
} else if (ROM[infoloc + BankOffset] == 48 && ROM[infoloc + CompanyOffset] == 0xB2) // Bandai
{
DSP3Enable = true;
} else {
DSP1Enable = true;
}
return;
}
// [sneed]: MSU detection
sprintf(MSU_BasePath, "%s%s", ZRomPath, ZCartName);
MSUEnable = readMSU();
// [sneed]: add support for FastROM superFX roms
switch ((uint16_t)ROM[infoloc + BankOffset] | (ROM[infoloc + TypeOffset] << 8)) {
case 0x1320: // Mario Chip 1
case 0x1330: // Mario Chip 1 + FastROM
case 0x1420: // GSU-x
case 0x1430: // GSU-x + FastROM
SFXEnable = true;
return;
break;
case 0x1520: // GSU-x + Battery
case 0x1530: // GSU-x + Battery + FastROM
case 0x1A20: // GSU-1 + Battery + Start in 21MHz
case 0x1A30: // GSU-1 + Battery + Start in 21MHz + FastROM
SFXEnable = true;
CHIPBATT = true;
return;
break;
case 0x2530:
OBCEnable = true;
CHIPBATT = true;
return;
break;
case 0x3423:
SA1Enable = true;
return;
break;
case 0x3223: // One sample game seems to use this for some reason
case 0x3523:
SA1Enable = true;
CHIPBATT = true;
return;
break;
case 0x4332:
SDD1Enable = true;
return;
break;
case 0x4532:
SDD1Enable = true;
CHIPBATT = true;
return;
break;
case 0x5535:
RTCEnable = true;
CHIPBATT = true;
return;
break;
case 0xE320:
SGBEnable = true;
return;
break;
case 0xF320:
C4Enable = true;
return;
break;
case 0xF530:
ST18Enable = true;
CHIPBATT = true; // Check later if this should be removed
return;
break;
case 0xF53A:
SPC7110Enable = true;
CHIPBATT = true;
return;
break;
case 0xF630:
SETAEnable = true;
CHIPBATT = true;
return;
break;
case 0xF93A:
SPC7110Enable = true;
RTCEnable = true;
CHIPBATT = true;
return;
break;
}
// BS Dump
if ((ROM[infoloc + CompanyOffset] == 0x33 || ROM[infoloc + CompanyOffset] == 0xFF) && (!ROM[infoloc + BSYearOffset] || (ROM[infoloc + BSYearOffset] & 131) == 128) && valid_normal_bank(ROM[infoloc + BSBankOffset])) {
uint8_t m = ROM[infoloc + BSMonthOffset];
if (!m && !ROM[infoloc + BSDayOffset]) {
// BS Add-on cart
return;
}
if ((m == 0xFF && ROM[infoloc + BSDayOffset] == 0xFF) || (!(m & 0xF) && ((m >> 4) - 1 < 12))) {
BSEnable = true;
return;
}
}
}
// Checksum functions
uint16_t sum(uint8_t* array, size_t size)
{
uint16_t theSum = 0;
uint_fast32_t i;
// Prevent crashing by reading too far (needed for messed up ROMs)
if (array + size > romdata + maxromspace) {
return (0xFFFF);
}
for (i = 0; i < size; i++) {
theSum += array[i];
}
return (theSum);
}
static uint16_t Checksumvalue;
void CalcChecksum()
{
uint8_t* ROM = romdata;
if (SplittedROM) {
Checksumvalue = sum(ROM + addOnStart, addOnSize);
Checksumvalue -= sum(ROM + infoloc + addOnStart - 16, 48);
} else if (SPC7110Enable) {
Checksumvalue = sum(ROM, curromspace);
} else {
Checksumvalue = sum(ROM, curromspace);
if (NumofBanks > 128 && maxromspace == 6 * MB_bytes) {
Checksumvalue += sum(ROM + 4 * MB_bytes, 2 * MB_bytes);
}
if (BSEnable) {
Checksumvalue -= sum(&ROM[infoloc - 16], 48); // Fix for BS Dumps
}
}
}
static void rom_memcpy(uint8_t* dest, uint8_t* src, size_t len)
{
uint8_t* endrom = romdata + maxromspace;
while (len-- && (dest < endrom) && (src < endrom)) {
*dest++ = *src++;
}
}
// This will mirror up non power of two ROMs to powers of two
static uint32_t mirror_rom(uint8_t* start, size_t length)
{
uint32_t mask = 0x800000;
while (!(length & mask)) {
mask >>= 1;
}
length -= mask;
if (length) {
start += mask;
length = mirror_rom(start, length);
while (length != mask) {
rom_memcpy(start + length, start, length);
length += length;
}
}
return (length + mask);
}
// Misc functions
void MirrorROM(uint8_t* ROM)
{
uint32_t ROMSize, StartMirror = 0;
if (!SPC7110Enable) {
curromspace = mirror_rom(romdata, curromspace);
} else if (curromspace == 0x300000) {
memcpy(romdata + curromspace, romdata, curromspace);
curromspace += curromspace;
}
if (curromspace > maxromspace) {
curromspace = maxromspace;
}
NumofBanks = curromspace >> 15;
// This will mirror (now) full sized ROMs through the ROM buffer
ROMSize = curromspace;
while (ROMSize < maxromspace) {
ROM[ROMSize++] = ROM[StartMirror++];
}
// If ROM was too small before, but now decent size with mirroring, adjust location
if (infoloc < Lo) {
infoloc = Lo;
}
}
void SetupSramSize()
{
uint8_t* ROM = romdata;
if (BSEnable) {
ramsize = 0;
} else if (SFXEnable) {
if (ROM[infoloc + CompanyOffset] == 0x33) // Extended header
{
ramsize = 8 << ((uint32_t)ROM[infoloc - 3]);
} else {
ramsize = 256;
}
} else if (SETAEnable) {
ramsize = 32;
} else if (!strncmp((char*)ROM, "BANDAI SFC-ADX", 14)) { // For the Sufami Turbo
ramsize = 8 << ((uint32_t)ROM[0x100032]);
} else {
ramsize = ((ROM[infoloc + SRAMSizeOffset]) ? (8 << ((uint32_t)ROM[infoloc + SRAMSizeOffset])) : 0);
}
// Fix if some ROM goes nuts on size
if (ramsize > 1024) {
ramsize = 1024;
}
// Convert from Kb to bytes;
ramsize *= 128;
ramsizeand = ramsize - 1;
}
// File loading code
bool Header512;
char CSStatus[41], CSStatus2[41], CSStatus3[41], CSStatus4[41];
void DumpROMLoadInfo()
{
extern char *ZVERSION, *VERSION_DATE, *VERSION_PORT;
FILE* fp = 0;
if (RomInfo) // rominfo.txt info dumping enabled?
{
fp = fopen_dir(ZCfgPath, "rominfo.txt", "w");
if (!fp) {
return;
}
fprintf(fp, "This is the info for the last game you ran.\n\nZSNES v%s - %s - %s\n",
ZVERSION, VERSION_DATE, VERSION_PORT);
fputs("File: ", fp);
fputs(ZCartName, fp);
fputs(" Header: ", fp);
fputs(Header512 ? "Yes\n" : "No\n", fp);
fputs(CSStatus, fp);
fputs("\n", fp);
fputs(CSStatus2, fp);
fputs("\n", fp);
fputs(CSStatus3, fp);
fputs("\n", fp);
fputs(CSStatus4, fp);
fputs("\n", fp);
fclose(fp);
}
}
void loadFile(char* filename)
{
bool multifile = false;
char* incrementer = 0;
uint8_t* ROM = romdata;
if (strlen(filename) >= 3) // Char + ".1"
{
char* ext = filename + strlen(filename) - 2;
if (!strcmp(ext, ".1") || !strcasecmp(ext, ".A")) {
incrementer = ext + 1;
multifile = true;
}
}
for (;;) {
struct stat stat_results;
stat_dir(ZRomPath, filename, &stat_results);
if ((uint32_t)stat_results.st_size <= maxromspace + 512 - curromspace) {
FILE* fp = 0;
fp = fopen_dir(ZRomPath, filename, "rb");
if (!fp) {
return;
}
if (curromspace && ((stat_results.st_size & 0x7FFF) == 512)) {
stat_results.st_size -= 512;
fseek(fp, 512, SEEK_SET);
}
fread(ROM + curromspace, stat_results.st_size, 1, fp);
fclose(fp);
curromspace += stat_results.st_size;
if (!multifile) {
return;
}
(*incrementer)++;
} else {
return;
}
}
}
void loadGZipFile(char* filename)
{
// Open file for size reading
FILE* fp = fopen_dir(ZRomPath, filename, "rb");
if (fp) {
uint32_t fsize, gzsize;
gzFile GZipFile;
fseek(fp, -4, SEEK_END);
gzsize = fread4(fp);
fsize = ftell(fp);
rewind(fp);
// Open GZip file for decompression, use existing file handle
if ((GZipFile = gzdopen(fileno(fp), "rb"))) {
uint32_t len = gzdirect(GZipFile) ? fsize : gzsize;
if (len && (len <= maxromspace + 512) && ((uint32_t)gzread(GZipFile, romdata, len) == len)) {
curromspace = len; // Success
}
gzclose(GZipFile);
}
fclose(fp);
}
}
void loadZipFile(char* filename)
{
int err, fileSize;
uint8_t* ROM = romdata;
bool multifile = false, NSS = false;
char* incrementer = 0;
unzFile zipfile = unzopen_dir(ZRomPath, filename); // Open zip file
int cFile = unzGoToFirstFile(zipfile); // Set cFile to first compressed file
unz_file_info cFileInfo; // Create variable to hold info for a compressed file
int LargestGoodFile = 0; // To keep track of largest file
// Variables for the file we pick
char ourFile[256];
ourFile[0] = '\n';
while (cFile == UNZ_OK) // While not at end of compressed file list
{
// Temporary char array for file name
char cFileName[256];
// Gets info on current file, and places it in cFileInfo
unzGetCurrentFileInfo(zipfile, &cFileInfo, cFileName, 256, NULL, 0, NULL, 0);
// Get the file's size
fileSize = cFileInfo.uncompressed_size;
// Find split files
if (strlen(cFileName) >= 3) // Char + ".1"
{
char* ext = cFileName + strlen(cFileName) - 2;
if (!strcmp(ext, ".1") || !strcasecmp(ext, ".A")) {
strcpy(ourFile, cFileName);
incrementer = ourFile + strlen(ourFile) - 1;
multifile = true;
break;
}
}
// Find Nintendo Super System ROMs
if (strlen(cFileName) >= 5) // Char + ".IC2"
{
char* ext = cFileName + strlen(cFileName) - 4;
if (!strncasecmp(ext, ".IC", 3)) {
strcpy(ourFile, cFileName);
incrementer = ourFile + strlen(ourFile) - 1;
*incrementer = '7';
NSS = true;
break;
}
}
// Check for valid ROM based on size
if (((intmax_t)fileSize <= maxromspace + 512) && (fileSize > LargestGoodFile)) {
strcpy(ourFile, cFileName);
LargestGoodFile = fileSize;
}
// Go to next file in zip file
cFile = unzGoToNextFile(zipfile);
}
// No files found
if (ourFile[0] == '\n') {
unzClose(zipfile);
return;
}
for (;;) {
// Sets current file to the file we liked before
if (unzLocateFile(zipfile, ourFile, 1) != UNZ_OK) {
if (NSS) {
(*incrementer)--;
continue;
}
unzClose(zipfile);
return;
}
// Gets info on current file, and places it in cFileInfo
unzGetCurrentFileInfo(zipfile, &cFileInfo, ourFile, 256, NULL, 0, NULL, 0);
// Get the file's size
fileSize = cFileInfo.uncompressed_size;
// Too big?
if (curromspace + fileSize > maxromspace + 512) {
unzClose(zipfile);
return;
}
// Open file
unzOpenCurrentFile(zipfile);
// Read file into memory
err = unzReadCurrentFile(zipfile, ROM + curromspace, fileSize);
// Close file
unzCloseCurrentFile(zipfile);
// Encountered error?
if (err != fileSize) {
unzClose(zipfile);
return;
}
if (curromspace && ((fileSize & 0x7FFF) == 512)) {
fileSize -= 512;
memmove(ROM + curromspace, ROM + curromspace + 512, fileSize);
}
curromspace += fileSize;
if (NSS) {
if (!*incrementer) {
return;
}
(*incrementer)--;
continue;
}
if (!multifile) {
unzClose(zipfile);
return;
}
(*incrementer)++;
}
}
void load_file_fs(char* path)
{
uint8_t* ROM = romdata;
if (isextension(path, "jma")) {
#ifdef NO_JMA
puts("This binary was built without JMA support.");
#else
load_jma_file_dir(ZRomPath, path);
#endif
}
if (isextension(path, "zip")) {
loadZipFile(path);
}
if (isextension(path, "gz")) {
loadGZipFile(path);
} else {
loadFile(path);
}
if ((curromspace & 0x7FFF) == 512) {