-
Notifications
You must be signed in to change notification settings - Fork 0
/
memmap.cpp
2760 lines (2433 loc) · 69.3 KB
/
memmap.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
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
/*
* Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
*
* (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
* Jerremy Koot (jkoot@snes9x.com)
*
* Super FX C emulator code
* (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
* Gary Henderson.
* Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
*
* DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
* C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
* C4 C code (c) Copyright 2001 Gary Henderson (gary.henderson@ntlworld.com).
*
* DOS port code contains the works of other authors. See headers in
* individual files.
*
* Snes9x homepage: http://www.snes9x.com
*
* Permission to use, copy, modify and distribute Snes9x in both binary and
* source form, for non-commercial purposes, is hereby granted without fee,
* providing that this license information and copyright notice appear with
* all copies and any derived work.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event shall the authors be held liable for any damages
* arising from the use of this software.
*
* Snes9x is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for Snes9x or software derived from Snes9x.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so everyone can benefit from the modifications
* in future versions.
*
* Super NES and Super Nintendo Entertainment System are trademarks of
* Nintendo Co., Limited and its subsidiary companies.
*/
#include <string.h>
#include <ctype.h>
#ifdef __linux
#include <unistd.h>
#endif
#include "snes9x.h"
#include "memmap.h"
#include "cpuexec.h"
#include "ppu.h"
#include "display.h"
#include "cheats.h"
#include "apu.h"
#include "sa1.h"
#include "srtc.h"
#include "sdd1.h"
#ifndef ZSNES_FX
#include "fxemu.h"
extern struct FxInit_s SuperFX;
#else
START_EXTERN_C
extern uint8 *SFXPlotTable;
END_EXTERN_C
#endif
static uint8 bytes0x2000 [0x2000];
extern char *rom_filename;
extern bool8 LoadZip(const char* , int32 *, int32 *);
bool8_32 CMemory::AllASCII (uint8 *b, int size)
{
for (int i = 0; i < size; i++)
{
if (b[i] < 32 || b[i] > 126)
return (FALSE);
}
return (TRUE);
}
int CMemory::ScoreHiROM (bool8_32 skip_header)
{
int score = 0;
int o = skip_header ? 0xff00 + 0x200 : 0xff00;
if ((Memory.ROM [o + 0xdc] + (Memory.ROM [o + 0xdd] << 8) +
Memory.ROM [o + 0xde] + (Memory.ROM [o + 0xdf] << 8)) == 0xffff)
score += 2;
if (Memory.ROM [o + 0xda] == 0x33)
score += 2;
if ((Memory.ROM [o + 0xd5] & 0xf) < 4)
score += 2;
if (!(Memory.ROM [o + 0xfd] & 0x80))
score -= 4;
if (CalculatedSize > 1024 * 1024 * 3)
score += 4;
if ((1 << (Memory.ROM [o + 0xd7] - 7)) > 48)
score -= 1;
if (!AllASCII (&Memory.ROM [o + 0xb0], 6))
score -= 1;
if (!AllASCII (&Memory.ROM [o + 0xc0], ROM_NAME_LEN - 1))
score -= 1;
return (score);
}
int CMemory::ScoreLoROM (bool8_32 skip_header)
{
int score = 0;
int o = skip_header ? 0x7f00 + 0x200 : 0x7f00;
if ((Memory.ROM [o + 0xdc] + (Memory.ROM [o + 0xdd] << 8) +
Memory.ROM [o + 0xde] + (Memory.ROM [o + 0xdf] << 8)) == 0xffff)
score += 2;
if (Memory.ROM [o + 0xda] == 0x33)
score += 2;
if ((Memory.ROM [o + 0xd5] & 0xf) < 4)
score += 2;
if (CalculatedSize <= 1024 * 1024 * 16)
score += 2;
if (!(Memory.ROM [o + 0xfd] & 0x80))
score -= 4;
if ((1 << (Memory.ROM [o + 0xd7] - 7)) > 48)
score -= 1;
if (!AllASCII (&Memory.ROM [o + 0xb0], 6))
score -= 1;
if (!AllASCII (&Memory.ROM [o + 0xc0], ROM_NAME_LEN - 1))
score -= 1;
return (score);
}
char *CMemory::Safe (const char *s)
{
static char *safe = NULL;
static int safe_len = 0;
int len = strlen (s);
if (!safe || len + 1 > safe_len)
{
if (safe)
free ((char *) safe);
safe = (char *) malloc (safe_len = len + 1);
}
for (int i = 0; i < len; i++)
{
if (s [i] >= 32 && s [i] < 127)
safe [i] = s[i];
else
safe [i] = '?';
}
safe [len] = 0;
return (safe);
}
/**********************************************************************************************/
/* Init() */
/* This function allocates all the memory needed by the emulator */
/**********************************************************************************************/
bool8_32 CMemory::Init ()
{
RAM = (uint8 *) malloc (0x20000);
SRAM = (uint8 *) malloc (0x20000);
VRAM = (uint8 *) malloc (0x10000);
ROM = (uint8 *) malloc (MAX_ROM_SIZE + 0x200 + 0x8000);
FillRAM = NULL;
IPPU.TileCache [TILE_2BIT] = (uint8 *) malloc (MAX_2BIT_TILES * 128);
IPPU.TileCache [TILE_4BIT] = (uint8 *) malloc (MAX_4BIT_TILES * 128);
IPPU.TileCache [TILE_8BIT] = (uint8 *) malloc (MAX_8BIT_TILES * 128);
IPPU.TileCached [TILE_2BIT] = (uint8 *) malloc (MAX_2BIT_TILES);
IPPU.TileCached [TILE_4BIT] = (uint8 *) malloc (MAX_4BIT_TILES);
IPPU.TileCached [TILE_8BIT] = (uint8 *) malloc (MAX_8BIT_TILES);
if (!RAM || !SRAM || !VRAM || !ROM ||
!IPPU.TileCache [TILE_2BIT] || !IPPU.TileCache [TILE_4BIT] ||
!IPPU.TileCache [TILE_8BIT] || !IPPU.TileCached [TILE_2BIT] ||
!IPPU.TileCached [TILE_4BIT] || !IPPU.TileCached [TILE_8BIT])
{
Deinit ();
return (FALSE);
}
// FillRAM uses first 32K of ROM image area, otherwise space just
// wasted. Might be read by the SuperFX code.
FillRAM = ROM;
// Add 0x8000 to ROM image pointer to stop SuperFX code accessing
// unallocated memory (can cause crash on some ports).
ROM += 0x8000;
C4RAM = ROM + 0x400000 + 8192 * 8;
::ROM = ROM;
::SRAM = SRAM;
::RegRAM = FillRAM;
#ifdef ZSNES_FX
SFXPlotTable = ROM + 0x400000;
#else
SuperFX.pvRegisters = &Memory.FillRAM [0x3000];
SuperFX.nRamBanks = 1;
SuperFX.pvRam = ::SRAM;
SuperFX.nRomBanks = (2 * 1024 * 1024) / (32 * 1024);
SuperFX.pvRom = (uint8 *) ROM;
#endif
ZeroMemory (IPPU.TileCached [TILE_2BIT], MAX_2BIT_TILES);
ZeroMemory (IPPU.TileCached [TILE_4BIT], MAX_4BIT_TILES);
ZeroMemory (IPPU.TileCached [TILE_8BIT], MAX_8BIT_TILES);
SDD1Data = NULL;
SDD1Index = NULL;
return (TRUE);
}
void CMemory::Deinit ()
{
if (RAM)
{
free ((char *) RAM);
RAM = NULL;
}
if (SRAM)
{
free ((char *) SRAM);
SRAM = NULL;
}
if (VRAM)
{
free ((char *) VRAM);
VRAM = NULL;
}
if (ROM)
{
ROM -= 0x8000;
free ((char *) ROM);
ROM = NULL;
}
if (IPPU.TileCache [TILE_2BIT])
{
free ((char *) IPPU.TileCache [TILE_2BIT]);
IPPU.TileCache [TILE_2BIT] = NULL;
}
if (IPPU.TileCache [TILE_4BIT])
{
free ((char *) IPPU.TileCache [TILE_4BIT]);
IPPU.TileCache [TILE_4BIT] = NULL;
}
if (IPPU.TileCache [TILE_8BIT])
{
free ((char *) IPPU.TileCache [TILE_8BIT]);
IPPU.TileCache [TILE_8BIT] = NULL;
}
if (IPPU.TileCached [TILE_2BIT])
{
free ((char *) IPPU.TileCached [TILE_2BIT]);
IPPU.TileCached [TILE_2BIT] = NULL;
}
if (IPPU.TileCached [TILE_4BIT])
{
free ((char *) IPPU.TileCached [TILE_4BIT]);
IPPU.TileCached [TILE_4BIT] = NULL;
}
if (IPPU.TileCached [TILE_8BIT])
{
free ((char *) IPPU.TileCached [TILE_8BIT]);
IPPU.TileCached [TILE_8BIT] = NULL;
}
FreeSDD1Data ();
}
void CMemory::FreeSDD1Data ()
{
if (SDD1Index)
{
free ((char *) SDD1Index);
SDD1Index = NULL;
}
if (SDD1Data)
{
free ((char *) SDD1Data);
SDD1Data = NULL;
}
}
/**********************************************************************************************/
/* LoadROM() */
/* This function loads a Snes-Backup image */
/**********************************************************************************************/
bool8_32 CMemory::LoadROM (const char *filename)
{
unsigned long FileSize = 0;
int retry_count = 0;
STREAM ROMFile;
bool8_32 Interleaved = FALSE;
bool8_32 Tales = FALSE;
char dir [_MAX_DIR + 1];
char drive [_MAX_DRIVE + 1];
char name [_MAX_FNAME + 1];
char ext [_MAX_EXT + 1];
char fname [_MAX_PATH + 1];
int i;
memset (&SNESGameFixes, 0, sizeof(SNESGameFixes));
SNESGameFixes.SRAMInitialValue = 0x60;
memset (bytes0x2000, 0, 0x2000);
CPU.TriedInterleavedMode2 = FALSE;
CalculatedSize = 0;
again:
_splitpath (filename, drive, dir, name, ext);
_makepath (fname, drive, dir, name, ext);
#ifdef __WIN32__
memmove (&ext [0], &ext[1], 4);
#endif
int32 TotalFileSize = 0;
#ifdef UNZIP_SUPPORT
if (strcasecmp (ext, "zip") == 0)
{
bool8 LoadZip (const char *, int32 *, int32 *);
if (!LoadZip (fname, &TotalFileSize, &HeaderCount))
return (FALSE);
strcpy (ROMFilename, fname);
}
else
#endif
{
if ((ROMFile = OPEN_STREAM (fname, "rb")) == NULL)
return (FALSE);
strcpy (ROMFilename, fname);
HeaderCount = 0;
uint8 *ptr = ROM;
bool8_32 more = FALSE;
do
{
FileSize = READ_STREAM (ptr, MAX_ROM_SIZE + 0x200 - (ptr - ROM), ROMFile);
CLOSE_STREAM (ROMFile);
int calc_size = (FileSize / 0x2000) * 0x2000;
if ((FileSize - calc_size == 512 && !Settings.ForceNoHeader) ||
Settings.ForceHeader)
{
memmove (ptr, ptr + 512, calc_size);
HeaderCount++;
FileSize -= 512;
}
ptr += FileSize;
TotalFileSize += FileSize;
int len;
if (ptr - ROM < MAX_ROM_SIZE + 0x200 &&
(isdigit (ext [0]) && ext [1] == 0 && ext [0] < '9'))
{
more = TRUE;
ext [0]++;
#ifdef __WIN32__
memmove (&ext [1], &ext [0], 4);
ext [0] = '.';
#endif
_makepath (fname, drive, dir, name, ext);
}
else
if (ptr - ROM < MAX_ROM_SIZE + 0x200 &&
(((len = strlen (name)) == 7 || len == 8) &&
strncasecmp (name, "sf", 2) == 0 &&
isdigit (name [2]) && isdigit (name [3]) && isdigit (name [4]) &&
isdigit (name [5]) && isalpha (name [len - 1])))
{
more = TRUE;
name [len - 1]++;
#ifdef __WIN32__
memmove (&ext [1], &ext [0], 4);
ext [0] = '.';
#endif
_makepath (fname, drive, dir, name, ext);
}
else
more = FALSE;
} while (more && (ROMFile = OPEN_STREAM (fname, "rb")) != NULL);
}
if (HeaderCount == 0)
S9xMessage (S9X_INFO, S9X_HEADERS_INFO, "No ROM file header found.");
else
{
if (HeaderCount == 1)
S9xMessage (S9X_INFO, S9X_HEADERS_INFO,
"Found ROM file header (and ignored it).");
else
S9xMessage (S9X_INFO, S9X_HEADERS_INFO,
"Found multiple ROM file headers (and ignored them).");
}
CheckForIPSPatch (filename, HeaderCount != 0, TotalFileSize);
int orig_hi_score, orig_lo_score;
int hi_score, lo_score;
orig_hi_score = hi_score = ScoreHiROM (FALSE);
orig_lo_score = lo_score = ScoreLoROM (FALSE);
if (HeaderCount == 0 && !Settings.ForceNoHeader &&
((hi_score > lo_score && ScoreHiROM (TRUE) > hi_score) ||
(hi_score <= lo_score && ScoreLoROM (TRUE) > lo_score)))
{
memmove (Memory.ROM, Memory.ROM + 512, TotalFileSize - 512);
TotalFileSize -= 512;
S9xMessage (S9X_INFO, S9X_HEADER_WARNING,
"Try specifying the -nhd command line option if the game doesn't work\n");
}
CalculatedSize = (TotalFileSize / 0x2000) * 0x2000;
ZeroMemory (ROM + CalculatedSize, MAX_ROM_SIZE - CalculatedSize);
// Check for cherryroms.com DAIKAIJYUMONOGATARI2
if (CalculatedSize == 0x500000 &&
strncmp ((const char *)&ROM [0x40ffc0], "DAIKAIJYUMONOGATARI2", 20) == 0 &&
strncmp ((const char *)&ROM [0x40ffb0], "18AE6J", 6) == 0 &&
memcmp (&ROM[0x40ffb0], &ROM [0xffb0], 0x30))
{
memmove (&ROM[0x100000], ROM, 0x500000);
memmove (ROM, &ROM[0x500000], 0x100000);
}
Interleaved = Settings.ForceInterleaved || Settings.ForceInterleaved2;
if (Settings.ForceLoROM || (!Settings.ForceHiROM && lo_score >= hi_score))
{
LoROM = TRUE;
HiROM = FALSE;
// Ignore map type byte if not 0x2x or 0x3x
if ((ROM [0x7fd5] & 0xf0) == 0x20 || (ROM [0x7fd5] & 0xf0) == 0x30)
{
switch (ROM [0x7fd5] & 0xf)
{
case 1:
if (strncmp ((char *) &ROM [0x7fc0], "TREASURE HUNTER G", 17) != 0)
Interleaved = TRUE;
break;
case 2:
#if 0
if (!Settings.ForceLoROM &&
strncmp ((char *) &ROM [0x7fc0], "SUPER FORMATION SOCCE", 21) != 0 &&
strncmp ((char *) &ROM [0x7fc0], "Star Ocean", 10) != 0)
{
LoROM = FALSE;
HiROM = TRUE;
}
#endif
break;
case 5:
Interleaved = TRUE;
Tales = TRUE;
break;
}
}
}
else
{
if ((ROM [0xffd5] & 0xf0) == 0x20 || (ROM [0xffd5] & 0xf0) == 0x30)
{
switch (ROM [0xffd5] & 0xf)
{
case 0:
case 3:
Interleaved = TRUE;
break;
}
}
LoROM = FALSE;
HiROM = TRUE;
}
// More
if (!Settings.ForceHiROM && !Settings.ForceLoROM &&
!Settings.ForceInterleaved && !Settings.ForceInterleaved2 &&
!Settings.ForceNotInterleaved && !Settings.ForcePAL &&
!Settings.ForceSuperFX && !Settings.ForceDSP1 &&
!Settings.ForceSA1 && !Settings.ForceC4 &&
!Settings.ForceSDD1)
{
if (strncmp ((char *) &ROM [0x7fc0], "YUYU NO QUIZ DE GO!GO!", 22) == 0)
{
LoROM = TRUE;
HiROM = FALSE;
Interleaved = FALSE;
}
else
if (strncmp ((char *) &ROM [0x7fc0], "SP MOMOTAROU DENTETSU2", 22) == 0)
{
LoROM = TRUE;
HiROM = FALSE;
Interleaved = FALSE;
}
else
if (CalculatedSize == 0x100000 &&
strncmp ((char *) &ROM [0xffc0], "WWF SUPER WRESTLEMANIA", 22) == 0)
{
int cvcount;
memmove (&ROM[0x100000] , ROM, 0x100000);
for (cvcount = 0; cvcount < 16; cvcount++)
{
memmove (&ROM[0x8000 * cvcount], &ROM[0x10000 * cvcount + 0x100000 + 0x8000], 0x8000);
memmove (&ROM[0x8000 * cvcount + 0x80000], &ROM[0x10000 * cvcount + 0x100000], 0x8000);
}
LoROM = TRUE;
HiROM = FALSE;
ZeroMemory (ROM + CalculatedSize, MAX_ROM_SIZE - CalculatedSize);
}
}
if (!Settings.ForceNotInterleaved && Interleaved)
{
CPU.TriedInterleavedMode2 = TRUE;
S9xMessage (S9X_INFO, S9X_ROM_INTERLEAVED_INFO,
"ROM image is in interleaved format - converting...");
int nblocks = CalculatedSize >> 16;
#if 0
int step = 64;
while (nblocks <= step)
step >>= 1;
nblocks = step;
#endif
uint8 blocks [256];
if (Tales)
{
nblocks = 0x60;
for (i = 0; i < 0x40; i += 2)
{
blocks [i + 0] = (i >> 1) + 0x20;
blocks [i + 1] = (i >> 1) + 0x00;
}
for (i = 0; i < 0x80; i += 2)
{
blocks [i + 0x40] = (i >> 1) + 0x80;
blocks [i + 0x41] = (i >> 1) + 0x40;
}
LoROM = FALSE;
HiROM = TRUE;
}
else
if (Settings.ForceInterleaved2)
{
for (i = 0; i < nblocks * 2; i++)
{
blocks [i] = (i & ~0x1e) | ((i & 2) << 2) | ((i & 4) << 2) |
((i & 8) >> 2) | ((i & 16) >> 2);
}
}
else
{
bool8_32 t = LoROM;
LoROM = HiROM;
HiROM = t;
for (i = 0; i < nblocks; i++)
{
blocks [i * 2] = i + nblocks;
blocks [i * 2 + 1] = i;
}
}
uint8 *tmp = (uint8 *) malloc (0x8000);
if (tmp)
{
for (i = 0; i < nblocks * 2; i++)
{
for (int j = i; j < nblocks * 2; j++)
{
if (blocks [j] == i)
{
memmove (tmp, &ROM [blocks [j] * 0x8000], 0x8000);
memmove (&ROM [blocks [j] * 0x8000],
&ROM [blocks [i] * 0x8000], 0x8000);
memmove (&ROM [blocks [i] * 0x8000], tmp, 0x8000);
uint8 b = blocks [j];
blocks [j] = blocks [i];
blocks [i] = b;
break;
}
}
}
free ((char *) tmp);
}
hi_score = ScoreHiROM (FALSE);
lo_score = ScoreLoROM (FALSE);
if ((HiROM &&
(lo_score >= hi_score || hi_score < 0)) ||
(LoROM &&
(hi_score > lo_score || lo_score < 0)))
{
if (retry_count == 0)
{
S9xMessage (S9X_INFO, S9X_ROM_CONFUSING_FORMAT_INFO,
"ROM lied about its type! Trying again.");
Settings.ForceNotInterleaved = TRUE;
Settings.ForceInterleaved = FALSE;
retry_count++;
goto again;
}
}
}
#ifndef _ZAURUS
FreeSDD1Data ();
#endif
InitROM (Tales);
#ifndef _ZAURUS
S9xLoadCheatFile (S9xGetFilename(".cht"));
S9xInitCheatData ();
S9xApplyCheats ();
#endif
S9xReset ();
return (TRUE);
}
void S9xDeinterleaveMode2 ()
{
S9xMessage (S9X_INFO, S9X_ROM_INTERLEAVED_INFO,
"ROM image is in interleaved format - converting...");
int nblocks = Memory.CalculatedSize >> 15;
int step = 64;
while (nblocks <= step)
step >>= 1;
nblocks = step;
uint8 blocks [256];
int i;
for (i = 0; i < nblocks * 2; i++)
{
blocks [i] = (i & ~0x1e) | ((i & 2) << 2) | ((i & 4) << 2) |
((i & 8) >> 2) | ((i & 16) >> 2);
}
uint8 *tmp = (uint8 *) malloc (0x8000);
if (tmp)
{
for (i = 0; i < nblocks * 2; i++)
{
for (int j = i; j < nblocks * 2; j++)
{
if (blocks [j] == i)
{
memmove (tmp, &Memory.ROM [blocks [j] * 0x8000], 0x8000);
memmove (&Memory.ROM [blocks [j] * 0x8000],
&Memory.ROM [blocks [i] * 0x8000], 0x8000);
memmove (&Memory.ROM [blocks [i] * 0x8000], tmp, 0x8000);
uint8 b = blocks [j];
blocks [j] = blocks [i];
blocks [i] = b;
break;
}
}
}
free ((char *) tmp);
}
Memory.InitROM (FALSE);
S9xReset ();
}
void CMemory::InitROM (bool8_32 Interleaved)
{
#ifndef ZSNES_FX
SuperFX.nRomBanks = CalculatedSize >> 15;
#endif
Settings.MultiPlayer5Master = Settings.MultiPlayer5;
Settings.MouseMaster = Settings.Mouse;
Settings.SuperScopeMaster = Settings.SuperScope;
Settings.DSP1Master = Settings.ForceDSP1;
Settings.SuperFX = FALSE;
Settings.SA1 = FALSE;
Settings.C4 = FALSE;
Settings.SDD1 = FALSE;
Settings.SRTC = FALSE;
ZeroMemory (BlockIsRAM, MEMMAP_NUM_BLOCKS);
ZeroMemory (BlockIsROM, MEMMAP_NUM_BLOCKS);
::SRAM = SRAM;
memset (ROMId, 0, 5);
memset (CompanyId, 0, 3);
if (Memory.HiROM)
{
Memory.SRAMSize = ROM [0xffd8];
strncpy (ROMName, (char *) &ROM[0xffc0], ROM_NAME_LEN - 1);
ROMSpeed = ROM [0xffd5];
ROMType = ROM [0xffd6];
ROMSize = ROM [0xffd7];
ROMChecksum = ROM [0xffde] + (ROM [0xffdf] << 8);
ROMComplementChecksum = ROM [0xffdc] + (ROM [0xffdd] << 8);
memmove (ROMId, &ROM [0xffb2], 4);
memmove (CompanyId, &ROM [0xffb0], 2);
// Try to auto-detect the DSP1 chip
if (!Settings.ForceNoDSP1 &&
(ROMType & 0xf) >= 3 && (ROMType & 0xf0) == 0)
Settings.DSP1Master = TRUE;
Settings.SDD1 = Settings.ForceSDD1;
if ((ROMType & 0xf0) == 0x40)
Settings.SDD1 = !Settings.ForceNoSDD1;
if (Settings.BS)
BSHiROMMap ();
else
if ((ROMSpeed & ~0x10) == 0x25)
TalesROMMap (Interleaved);
#ifndef _ZAURUS
else
if ((ROMSpeed & ~0x10) == 0x22 &&
strncmp (ROMName, "Super Street Fighter", 20) != 0)
{
AlphaROMMap ();
}
#endif
else
HiROMMap ();
}
else
{
Memory.HiROM = FALSE;
Memory.SRAMSize = ROM [0x7fd8];
ROMSpeed = ROM [0x7fd5];
ROMType = ROM [0x7fd6];
ROMSize = ROM [0x7fd7];
ROMChecksum = ROM [0x7fde] + (ROM [0x7fdf] << 8);
ROMComplementChecksum = ROM [0x7fdc] + (ROM [0x7fdd] << 8);
memmove (ROMId, &ROM [0x7fb2], 4);
memmove (CompanyId, &ROM [0x7fb0], 2);
strncpy (ROMName, (char *) &ROM[0x7fc0], ROM_NAME_LEN - 1);
Settings.SuperFX = Settings.ForceSuperFX;
if ((ROMType & 0xf0) == 0x10)
Settings.SuperFX = !Settings.ForceNoSuperFX;
// Try to auto-detect the DSP1 chip
if (!Settings.ForceNoDSP1 &&
(ROMType & 0xf) >= 3 && (ROMType & 0xf0) == 0)
Settings.DSP1Master = TRUE;
#ifndef _ZAURUS
Settings.SDD1 = Settings.ForceSDD1;
if ((ROMType & 0xf0) == 0x40)
Settings.SDD1 = !Settings.ForceNoSDD1;
if (Settings.SDD1)
S9xLoadSDD1Data ();
Settings.C4 = Settings.ForceC4;
if ((ROMType & 0xf0) == 0xf0 &&
(strncmp (ROMName, "MEGAMAN X", 9) == 0 ||
strncmp (ROMName, "ROCKMAN X", 9) == 0))
{
Settings.C4 = !Settings.ForceNoC4;
}
if (Settings.SuperFX)
{
//::SRAM = ROM + 1024 * 1024 * 4;
SuperFXROMMap ();
Settings.MultiPlayer5Master = FALSE;
//Settings.MouseMaster = FALSE;
//Settings.SuperScopeMaster = FALSE;
Settings.DSP1Master = FALSE;
Settings.SA1 = FALSE;
Settings.C4 = FALSE;
Settings.SDD1 = FALSE;
}
else
if (Settings.ForceSA1 ||
(!Settings.ForceNoSA1 && (ROMSpeed & ~0x10) == 0x23 &&
(ROMType & 0xf) > 3 && (ROMType & 0xf0) == 0x30))
{
Settings.SA1 = TRUE;
Settings.MultiPlayer5Master = FALSE;
//Settings.MouseMaster = FALSE;
//Settings.SuperScopeMaster = FALSE;
Settings.DSP1Master = FALSE;
Settings.C4 = FALSE;
Settings.SDD1 = FALSE;
SA1ROMMap ();
}
else
#endif
if ((ROMSpeed & ~0x10) == 0x25)
TalesROMMap (Interleaved);
else
if (strncmp ((char *) &Memory.ROM [0x7fc0], "SOUND NOVEL-TCOOL", 17) == 0 ||
strncmp ((char *) &Memory.ROM [0x7fc0], "DERBY STALLION 96", 17) == 0)
{
LoROM24MBSMap ();
Settings.DSP1Master = FALSE;
}
else
if (strncmp ((char *) &Memory.ROM [0x7fc0], "THOROUGHBRED BREEDER3", 21) == 0 ||
strncmp ((char *) &Memory.ROM [0x7fc0], "RPG-TCOOL 2", 11) == 0)
{
SRAM512KLoROMMap ();
Settings.DSP1Master = FALSE;
}
else
if (strncmp ((char *) &Memory.ROM [0x7fc0], "DEZAEMON ", 10) == 0)
{
Settings.DSP1Master = FALSE;
SRAM1024KLoROMMap ();
}
else
if (strncmp ((char *) &Memory.ROM [0x7fc0], "ADD-ON BASE CASSETE", 19) == 0)
{
Settings.MultiPlayer5Master = FALSE;
Settings.MouseMaster = FALSE;
Settings.SuperScopeMaster = FALSE;
Settings.DSP1Master = FALSE;
SufamiTurboLoROMMap();
Memory.SRAMSize = 3;
}
#ifndef _ZAURUS
else
if ((ROMSpeed & ~0x10) == 0x22 &&
strncmp (ROMName, "Super Street Fighter", 20) != 0)
{
AlphaROMMap ();
}
#endif
else
LoROMMap ();
}
int power2 = 0;
int size = CalculatedSize;
while (size >>= 1)
power2++;
size = 1 << power2;
uint32 remainder = CalculatedSize - size;
uint32 sum1 = 0;
uint32 sum2 = 0;
int i;
for (i = 0; i < size; i++)
sum1 += ROM [i];
for (i = 0; i < (int) remainder; i++)
sum2 += ROM [size + i];
if (remainder)
{
#ifndef _ZAURUS
//for Tengai makyou
if (CalculatedSize == 0x500000 && Memory.HiROM &&
strncmp ((const char *)&ROM[0xffb0], "18AZ", 4) == 0 &&
!memcmp(&ROM[0xffd5], "\x3a\xf9\x0d\x03\x00\x33\x00", 7))
sum1 += sum2;
else
#endif
sum1 += sum2 * (size / remainder);
}
sum1 &= 0xffff;
if (Settings.ForceNTSC)
Settings.PAL = FALSE;
else
if (Settings.ForcePAL)
Settings.PAL = TRUE;
else
if (Memory.HiROM)
// Country code
Settings.PAL = ROM [0xffd9] >= 2;
else
Settings.PAL = ROM [0x7fd9] >= 2;
if (Settings.PAL)
{
Settings.FrameTime = Settings.FrameTimePAL;
Memory.ROMFramesPerSecond = 50;
}
else
{
Settings.FrameTime = Settings.FrameTimeNTSC;
Memory.ROMFramesPerSecond = 60;
}
ROMName[ROM_NAME_LEN - 1] = 0;
if (strlen (ROMName))
{
char *p = ROMName + strlen (ROMName) - 1;
while (p > ROMName && *(p - 1) == ' ')
p--;
*p = 0;
}
#ifndef _ZAURUS
if (Settings.SuperFX)
{
SRAMMask = 0xffff;
Memory.SRAMSize = 16;
}
else
#endif
{
SRAMMask = Memory.SRAMSize ?
((1 << (Memory.SRAMSize + 3)) * 128) - 1 : 0;
}
IAPU.OneCycle = ONE_APU_CYCLE;
Settings.Shutdown = Settings.ShutdownMaster;
ApplyROMFixes ();
sprintf (ROMName, "%s", Safe (ROMName));
sprintf (ROMId, "%s", Safe (ROMId));
sprintf (CompanyId, "%s", Safe (CompanyId));
sprintf (String, "\"%s\" [%s] %s, %s, Type: %s, Mode: %s, TV: %s, S-RAM: %s, ROMId: %s Company: %2.2s\n",
ROMName,
(ROMChecksum + ROMComplementChecksum != 0xffff ||
ROMChecksum != sum1) ? "bad checksum" : "checksum ok",
MapType (),
Size (),
KartContents (),
MapMode (),
TVStandard (),
StaticRAMSize (),
ROMId,
CompanyId);
S9xMessage (S9X_INFO, S9X_ROM_INFO, String);
}
bool8_32 CMemory::LoadSRAM (const char *filename)
{
int size = Memory.SRAMSize ?
(1 << (Memory.SRAMSize + 3)) * 128 : 0;
memset (SRAM, SNESGameFixes.SRAMInitialValue, 0x20000);
if (size > 0x20000)
size = 0x20000;
if (size)
{
FILE *file;
if ((file = fopen (filename, "rb")))
{
int len = fread ((char*) ::SRAM, 1, 0x20000, file);
fclose (file);
if (len - size == 512)
{
// S-RAM file has a header - remove it
memmove (::SRAM, ::SRAM + 512, size);
}
#ifndef _ZAURUS
if (len == size + SRTC_SRAM_PAD)
{
S9xSRTCPostLoadState ();
S9xResetSRTC ();
rtc.index = -1;
rtc.mode = MODE_READ;
}
else
S9xHardResetSRTC ();