2 * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
4 * (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
5 * Jerremy Koot (jkoot@snes9x.com)
7 * Super FX C emulator code
8 * (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
10 * Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
12 * DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
13 * C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
14 * C4 C code (c) Copyright 2001 Gary Henderson (gary.henderson@ntlworld.com).
16 * DOS port code contains the works of other authors. See headers in
19 * Snes9x homepage: http://www.snes9x.com
21 * Permission to use, copy, modify and distribute Snes9x in both binary and
22 * source form, for non-commercial purposes, is hereby granted without fee,
23 * providing that this license information and copyright notice appear with
24 * all copies and any derived work.
26 * This software is provided 'as-is', without any express or implied
27 * warranty. In no event shall the authors be held liable for any damages
28 * arising from the use of this software.
30 * Snes9x is freeware for PERSONAL USE only. Commercial users should
31 * seek permission of the copyright holders first. Commercial use includes
32 * charging money for Snes9x or software derived from Snes9x.
34 * The copyright holders request that bug fixes and improvements to the code
35 * should be forwarded to them so everyone can benefit from the modifications
38 * Super NES and Super Nintendo Entertainment System are trademarks of
39 * Nintendo Co., Limited and its subsidiary companies.
44 /* Do not reencode this file. There are special chars on it. */
59 extern struct FxInit_s SuperFX;
62 extern uint8 *SFXPlotTable;
66 static uint8 bytes0x2000 [0x2000];
68 extern char *rom_filename;
69 extern bool8 LoadZip(const char* , int32 *, int32 *);
71 bool8_32 CMemory::AllASCII (uint8 *b, int size)
73 for (int i = 0; i < size; i++)
75 if (b[i] < 32 || b[i] > 126)
81 int CMemory::ScoreHiROM (bool8_32 skip_header)
84 int o = skip_header ? 0xff00 + 0x200 : 0xff00;
86 if ((Memory.ROM [o + 0xdc] + (Memory.ROM [o + 0xdd] << 8) +
87 Memory.ROM [o + 0xde] + (Memory.ROM [o + 0xdf] << 8)) == 0xffff)
90 if (Memory.ROM [o + 0xda] == 0x33)
92 if ((Memory.ROM [o + 0xd5] & 0xf) < 4)
94 if (!(Memory.ROM [o + 0xfd] & 0x80))
96 if (CalculatedSize > 1024 * 1024 * 3)
98 if ((1 << (Memory.ROM [o + 0xd7] - 7)) > 48)
100 if (!AllASCII (&Memory.ROM [o + 0xb0], 6))
102 if (!AllASCII (&Memory.ROM [o + 0xc0], ROM_NAME_LEN - 1))
108 int CMemory::ScoreLoROM (bool8_32 skip_header)
111 int o = skip_header ? 0x7f00 + 0x200 : 0x7f00;
113 if ((Memory.ROM [o + 0xdc] + (Memory.ROM [o + 0xdd] << 8) +
114 Memory.ROM [o + 0xde] + (Memory.ROM [o + 0xdf] << 8)) == 0xffff)
117 if (Memory.ROM [o + 0xda] == 0x33)
119 if ((Memory.ROM [o + 0xd5] & 0xf) < 4)
121 if (CalculatedSize <= 1024 * 1024 * 16)
123 if (!(Memory.ROM [o + 0xfd] & 0x80))
125 if ((1 << (Memory.ROM [o + 0xd7] - 7)) > 48)
127 if (!AllASCII (&Memory.ROM [o + 0xb0], 6))
129 if (!AllASCII (&Memory.ROM [o + 0xc0], ROM_NAME_LEN - 1))
135 char *CMemory::Safe (const char *s)
137 static char *safe = NULL;
138 static int safe_len = 0;
140 int len = strlen (s);
141 if (!safe || len + 1 > safe_len)
144 free ((char *) safe);
145 safe = (char *) malloc (safe_len = len + 1);
148 for (int i = 0; i < len; i++)
150 if (s [i] >= 32 && s [i] < 127)
159 /**********************************************************************************************/
161 /* This function allocates all the memory needed by the emulator */
162 /**********************************************************************************************/
163 bool8_32 CMemory::Init ()
165 RAM = (uint8 *) malloc (0x20000);
166 SRAM = (uint8 *) malloc (0x20000);
167 VRAM = (uint8 *) malloc (0x10000);
168 ROM = (uint8 *) malloc (MAX_ROM_SIZE + 0x200 + 0x8000);
171 IPPU.TileCache [TILE_2BIT] = (uint8 *) malloc (MAX_2BIT_TILES * 128);
172 IPPU.TileCache [TILE_4BIT] = (uint8 *) malloc (MAX_4BIT_TILES * 128);
173 IPPU.TileCache [TILE_8BIT] = (uint8 *) malloc (MAX_8BIT_TILES * 128);
175 IPPU.TileCached [TILE_2BIT] = (uint8 *) malloc (MAX_2BIT_TILES);
176 IPPU.TileCached [TILE_4BIT] = (uint8 *) malloc (MAX_4BIT_TILES);
177 IPPU.TileCached [TILE_8BIT] = (uint8 *) malloc (MAX_8BIT_TILES);
179 if (!RAM || !SRAM || !VRAM || !ROM ||
180 !IPPU.TileCache [TILE_2BIT] || !IPPU.TileCache [TILE_4BIT] ||
181 !IPPU.TileCache [TILE_8BIT] || !IPPU.TileCached [TILE_2BIT] ||
182 !IPPU.TileCached [TILE_4BIT] || !IPPU.TileCached [TILE_8BIT])
188 // FillRAM uses first 32K of ROM image area, otherwise space just
189 // wasted. Might be read by the SuperFX code.
193 // Add 0x8000 to ROM image pointer to stop SuperFX code accessing
194 // unallocated memory (can cause crash on some ports).
197 C4RAM = ROM + 0x400000 + 8192 * 8;
203 SFXPlotTable = ROM + 0x400000;
205 SuperFX.pvRegisters = &Memory.FillRAM [0x3000];
206 SuperFX.nRamBanks = 1;
207 SuperFX.pvRam = ::SRAM;
208 SuperFX.nRomBanks = (2 * 1024 * 1024) / (32 * 1024);
209 SuperFX.pvRom = (uint8 *) ROM;
212 ZeroMemory (IPPU.TileCached [TILE_2BIT], MAX_2BIT_TILES);
213 ZeroMemory (IPPU.TileCached [TILE_4BIT], MAX_4BIT_TILES);
214 ZeroMemory (IPPU.TileCached [TILE_8BIT], MAX_8BIT_TILES);
222 void CMemory::Deinit ()
231 free ((char *) SRAM);
236 free ((char *) VRAM);
246 if (IPPU.TileCache [TILE_2BIT])
248 free ((char *) IPPU.TileCache [TILE_2BIT]);
249 IPPU.TileCache [TILE_2BIT] = NULL;
251 if (IPPU.TileCache [TILE_4BIT])
253 free ((char *) IPPU.TileCache [TILE_4BIT]);
254 IPPU.TileCache [TILE_4BIT] = NULL;
256 if (IPPU.TileCache [TILE_8BIT])
258 free ((char *) IPPU.TileCache [TILE_8BIT]);
259 IPPU.TileCache [TILE_8BIT] = NULL;
262 if (IPPU.TileCached [TILE_2BIT])
264 free ((char *) IPPU.TileCached [TILE_2BIT]);
265 IPPU.TileCached [TILE_2BIT] = NULL;
267 if (IPPU.TileCached [TILE_4BIT])
269 free ((char *) IPPU.TileCached [TILE_4BIT]);
270 IPPU.TileCached [TILE_4BIT] = NULL;
272 if (IPPU.TileCached [TILE_8BIT])
274 free ((char *) IPPU.TileCached [TILE_8BIT]);
275 IPPU.TileCached [TILE_8BIT] = NULL;
281 void CMemory::FreeSDD1Data ()
285 free ((char *) SDD1Index);
290 free ((char *) SDD1Data);
295 /**********************************************************************************************/
297 /**********************************************************************************************/
298 int checkzip( char * fn )
300 int cnt = strlen(fn);
301 if( ( (fn[cnt-1] == 'p') || (fn[cnt-1] == 'P') ) &&
302 ( (fn[cnt-2] == 'i') || (fn[cnt-2] == 'I') ) &&
303 ( (fn[cnt-3] == 'z') || (fn[cnt-3] == 'Z') ) ){
310 /**********************************************************************************************/
312 /* This function loads a Snes-Backup image */
313 /**********************************************************************************************/
315 #pragma warning(disable : 4101)
316 #pragma warning(disable : 4700)
318 bool8_32 CMemory::LoadROM (const char *filename)
320 unsigned long FileSize = 0;
323 bool8_32 Interleaved = FALSE;
324 bool8_32 Tales = FALSE;
325 char dir [_MAX_DIR + 1];
326 char drive [_MAX_DRIVE + 1];
327 char name [_MAX_FNAME + 1];
328 char ext [_MAX_EXT + 1];
329 char fname [_MAX_PATH + 1];
332 memset (&SNESGameFixes, 0, sizeof(SNESGameFixes));
333 SNESGameFixes.SRAMInitialValue = 0x60;
335 memset (bytes0x2000, 0, 0x2000);
336 CPU.TriedInterleavedMode2 = FALSE;
340 PathSplit(filename, drive, dir, name, ext);
341 PathMake(fname, drive, dir, name, ext);
343 int32 TotalFileSize = 0;
346 if( checkzip( fname ) )
348 if (!LoadZip (fname, &TotalFileSize, &HeaderCount))
351 strcpy (ROMFilename, fname);
356 if ((ROMFile = OPEN_STREAM (fname, "rb")) == NULL)
359 strcpy (ROMFilename, fname);
363 bool8_32 more = FALSE;
367 FileSize = READ_STREAM (ptr, MAX_ROM_SIZE + 0x200 - (ptr - ROM), ROMFile);
368 CLOSE_STREAM (ROMFile);
369 int calc_size = (FileSize / 0x2000) * 0x2000;
371 if ((FileSize - calc_size == 512 && !Settings.ForceNoHeader) ||
372 Settings.ForceHeader)
374 memmove (ptr, ptr + 512, calc_size);
379 TotalFileSize += FileSize;
382 if (ptr - ROM < MAX_ROM_SIZE + 0x200 &&
383 (isdigit (ext [0]) && ext [1] == 0 && ext [0] < '9'))
387 PathMake(fname, drive, dir, name, ext);
390 if (ptr - ROM < MAX_ROM_SIZE + 0x200 &&
391 (((len = strlen (name)) == 7 || len == 8) &&
392 strncasecmp (name, "sf", 2) == 0 &&
393 isdigit (name [2]) && isdigit (name [3]) && isdigit (name [4]) &&
394 isdigit (name [5]) && isalpha (name [len - 1])))
398 PathMake(fname, drive, dir, name, ext);
402 } while (more && (ROMFile = OPEN_STREAM (fname, "rb")) != NULL);
405 if (HeaderCount == 0)
406 S9xMessage (S9X_INFO, S9X_HEADERS_INFO, "No ROM file header found.");
409 if (HeaderCount == 1)
410 S9xMessage (S9X_INFO, S9X_HEADERS_INFO,
411 "Found ROM file header (and ignored it).");
413 S9xMessage (S9X_INFO, S9X_HEADERS_INFO,
414 "Found multiple ROM file headers (and ignored them).");
417 CheckForIPSPatch (filename, HeaderCount != 0, TotalFileSize);
418 int orig_hi_score, orig_lo_score;
419 int hi_score, lo_score;
421 orig_hi_score = hi_score = ScoreHiROM (FALSE);
422 orig_lo_score = lo_score = ScoreLoROM (FALSE);
424 if (HeaderCount == 0 && !Settings.ForceNoHeader &&
425 ((hi_score > lo_score && ScoreHiROM (TRUE) > hi_score) ||
426 (hi_score <= lo_score && ScoreLoROM (TRUE) > lo_score)))
428 memmove (Memory.ROM, Memory.ROM + 512, TotalFileSize - 512);
429 TotalFileSize -= 512;
430 S9xMessage (S9X_INFO, S9X_HEADER_WARNING,
431 "Try specifying the -nhd command line option if the game doesn't work\n");
434 CalculatedSize = (TotalFileSize / 0x2000) * 0x2000;
435 ZeroMemory (ROM + CalculatedSize, MAX_ROM_SIZE - CalculatedSize);
437 // Check for cherryroms.com DAIKAIJYUMONOGATARI2
439 if (CalculatedSize == 0x500000 &&
440 strncmp ((const char *)&ROM [0x40ffc0], "DAIKAIJYUMONOGATARI2", 20) == 0 &&
441 strncmp ((const char *)&ROM [0x40ffb0], "18AE6J", 6) == 0 &&
442 memcmp (&ROM[0x40ffb0], &ROM [0xffb0], 0x30))
444 memmove (&ROM[0x100000], ROM, 0x500000);
445 memmove (ROM, &ROM[0x500000], 0x100000);
448 Interleaved = Settings.ForceInterleaved || Settings.ForceInterleaved2;
449 if (Settings.ForceLoROM || (!Settings.ForceHiROM && lo_score >= hi_score))
454 // Ignore map type byte if not 0x2x or 0x3x
455 if ((ROM [0x7fd5] & 0xf0) == 0x20 || (ROM [0x7fd5] & 0xf0) == 0x30)
457 switch (ROM [0x7fd5] & 0xf)
460 if (strncmp ((char *) &ROM [0x7fc0], "TREASURE HUNTER G", 17) != 0)
465 if (!Settings.ForceLoROM &&
466 strncmp ((char *) &ROM [0x7fc0], "SUPER FORMATION SOCCE", 21) != 0 &&
467 strncmp ((char *) &ROM [0x7fc0], "Star Ocean", 10) != 0)
483 if ((ROM [0xffd5] & 0xf0) == 0x20 || (ROM [0xffd5] & 0xf0) == 0x30)
485 switch (ROM [0xffd5] & 0xf)
498 if (!Settings.ForceHiROM && !Settings.ForceLoROM &&
499 !Settings.ForceInterleaved && !Settings.ForceInterleaved2 &&
500 !Settings.ForceNotInterleaved && !Settings.ForcePAL &&
501 !Settings.ForceSuperFX && !Settings.ForceDSP1 &&
502 !Settings.ForceSA1 && !Settings.ForceC4 &&
505 if (strncmp ((char *) &ROM [0x7fc0], "YUYU NO QUIZ DE GO!GO!", 22) == 0)
512 if (strncmp ((char *) &ROM [0x7fc0], "SP MOMOTAROU DENTETSU2", 22) == 0)
519 if (CalculatedSize == 0x100000 &&
520 strncmp ((char *) &ROM [0xffc0], "WWF SUPER WRESTLEMANIA", 22) == 0)
524 memmove (&ROM[0x100000] , ROM, 0x100000);
525 for (cvcount = 0; cvcount < 16; cvcount++)
527 memmove (&ROM[0x8000 * cvcount], &ROM[0x10000 * cvcount + 0x100000 + 0x8000], 0x8000);
528 memmove (&ROM[0x8000 * cvcount + 0x80000], &ROM[0x10000 * cvcount + 0x100000], 0x8000);
532 ZeroMemory (ROM + CalculatedSize, MAX_ROM_SIZE - CalculatedSize);
536 if (!Settings.ForceNotInterleaved && Interleaved)
538 CPU.TriedInterleavedMode2 = TRUE;
539 S9xMessage (S9X_INFO, S9X_ROM_INTERLEAVED_INFO,
540 "ROM image is in interleaved format - converting...");
542 int nblocks = CalculatedSize >> 16;
546 while (nblocks <= step)
556 for (i = 0; i < 0x40; i += 2)
558 blocks [i + 0] = (i >> 1) + 0x20;
559 blocks [i + 1] = (i >> 1) + 0x00;
561 for (i = 0; i < 0x80; i += 2)
563 blocks [i + 0x40] = (i >> 1) + 0x80;
564 blocks [i + 0x41] = (i >> 1) + 0x40;
570 if (Settings.ForceInterleaved2)
572 for (i = 0; i < nblocks * 2; i++)
574 blocks [i] = (i & ~0x1e) | ((i & 2) << 2) | ((i & 4) << 2) |
575 ((i & 8) >> 2) | ((i & 16) >> 2);
585 for (i = 0; i < nblocks; i++)
587 blocks [i * 2] = i + nblocks;
588 blocks [i * 2 + 1] = i;
592 uint8 *tmp = (uint8 *) malloc (0x8000);
595 for (i = 0; i < nblocks * 2; i++)
597 for (int j = i; j < nblocks * 2; j++)
601 memmove (tmp, &ROM [blocks [j] * 0x8000], 0x8000);
602 memmove (&ROM [blocks [j] * 0x8000],
603 &ROM [blocks [i] * 0x8000], 0x8000);
604 memmove (&ROM [blocks [i] * 0x8000], tmp, 0x8000);
605 uint8 b = blocks [j];
606 blocks [j] = blocks [i];
615 hi_score = ScoreHiROM (FALSE);
616 lo_score = ScoreLoROM (FALSE);
619 (lo_score >= hi_score || hi_score < 0)) ||
621 (hi_score > lo_score || lo_score < 0)))
623 if (retry_count == 0)
625 S9xMessage (S9X_INFO, S9X_ROM_CONFUSING_FORMAT_INFO,
626 "ROM lied about its type! Trying again.");
627 Settings.ForceNotInterleaved = TRUE;
628 Settings.ForceInterleaved = FALSE;
637 S9xLoadCheatFile (S9xGetFilename(FILE_CHT));
646 void S9xDeinterleaveMode2 ()
648 S9xMessage (S9X_INFO, S9X_ROM_INTERLEAVED_INFO,
649 "ROM image is in interleaved format - converting...");
651 int nblocks = Memory.CalculatedSize >> 15;
654 while (nblocks <= step)
661 for (i = 0; i < nblocks * 2; i++)
663 blocks [i] = (i & ~0x1e) | ((i & 2) << 2) | ((i & 4) << 2) |
664 ((i & 8) >> 2) | ((i & 16) >> 2);
667 uint8 *tmp = (uint8 *) malloc (0x8000);
671 for (i = 0; i < nblocks * 2; i++)
673 for (int j = i; j < nblocks * 2; j++)
677 memmove (tmp, &Memory.ROM [blocks [j] * 0x8000], 0x8000);
678 memmove (&Memory.ROM [blocks [j] * 0x8000],
679 &Memory.ROM [blocks [i] * 0x8000], 0x8000);
680 memmove (&Memory.ROM [blocks [i] * 0x8000], tmp, 0x8000);
681 uint8 b = blocks [j];
682 blocks [j] = blocks [i];
690 Memory.InitROM (FALSE);
694 void CMemory::InitROM (bool8_32 Interleaved)
697 SuperFX.nRomBanks = CalculatedSize >> 15;
699 Settings.MultiPlayer5Master = Settings.MultiPlayer5;
700 Settings.MouseMaster = Settings.Mouse;
701 Settings.SuperScopeMaster = Settings.SuperScope;
702 Settings.DSP1Master = Settings.ForceDSP1;
703 Settings.SuperFX = FALSE;
704 Settings.SA1 = FALSE;
706 Settings.SDD1 = FALSE;
707 Settings.SRTC = FALSE;
709 ZeroMemory (BlockIsRAM, MEMMAP_NUM_BLOCKS);
710 ZeroMemory (BlockIsROM, MEMMAP_NUM_BLOCKS);
713 memset (ROMId, 0, 5);
714 memset (CompanyId, 0, 3);
718 Memory.SRAMSize = ROM [0xffd8];
719 strncpy (ROMName, (char *) &ROM[0xffc0], ROM_NAME_LEN - 1);
720 ROMSpeed = ROM [0xffd5];
721 ROMType = ROM [0xffd6];
722 ROMSize = ROM [0xffd7];
723 ROMChecksum = ROM [0xffde] + (ROM [0xffdf] << 8);
724 ROMComplementChecksum = ROM [0xffdc] + (ROM [0xffdd] << 8);
726 memmove (ROMId, &ROM [0xffb2], 4);
727 memmove (CompanyId, &ROM [0xffb0], 2);
729 // Try to auto-detect the DSP1 chip
730 if (!Settings.ForceNoDSP1 &&
731 (ROMType & 0xf) >= 3 && (ROMType & 0xf0) == 0)
732 Settings.DSP1Master = TRUE;
734 Settings.SDD1 = Settings.ForceSDD1;
735 if ((ROMType & 0xf0) == 0x40)
736 Settings.SDD1 = !Settings.ForceNoSDD1;
741 if ((ROMSpeed & ~0x10) == 0x25)
742 TalesROMMap (Interleaved);
744 if ((ROMSpeed & ~0x10) == 0x22 &&
745 strncmp (ROMName, "Super Street Fighter", 20) != 0)
754 Memory.HiROM = FALSE;
755 Memory.SRAMSize = ROM [0x7fd8];
756 ROMSpeed = ROM [0x7fd5];
757 ROMType = ROM [0x7fd6];
758 ROMSize = ROM [0x7fd7];
759 ROMChecksum = ROM [0x7fde] + (ROM [0x7fdf] << 8);
760 ROMComplementChecksum = ROM [0x7fdc] + (ROM [0x7fdd] << 8);
761 memmove (ROMId, &ROM [0x7fb2], 4);
762 memmove (CompanyId, &ROM [0x7fb0], 2);
764 strncpy (ROMName, (char *) &ROM[0x7fc0], ROM_NAME_LEN - 1);
765 Settings.SuperFX = Settings.ForceSuperFX;
767 if ((ROMType & 0xf0) == 0x10)
768 Settings.SuperFX = !Settings.ForceNoSuperFX;
770 // Try to auto-detect the DSP1 chip
771 if (!Settings.ForceNoDSP1 &&
772 (ROMType & 0xf) >= 3 && (ROMType & 0xf0) == 0)
773 Settings.DSP1Master = TRUE;
775 Settings.SDD1 = Settings.ForceSDD1;
776 if ((ROMType & 0xf0) == 0x40)
777 Settings.SDD1 = !Settings.ForceNoSDD1;
782 Settings.C4 = Settings.ForceC4;
783 if ((ROMType & 0xf0) == 0xf0 &&
784 (strncmp (ROMName, "MEGAMAN X", 9) == 0 ||
785 strncmp (ROMName, "ROCKMAN X", 9) == 0))
787 Settings.C4 = !Settings.ForceNoC4;
790 if (Settings.SuperFX)
792 //::SRAM = ROM + 1024 * 1024 * 4;
794 Settings.MultiPlayer5Master = FALSE;
795 //Settings.MouseMaster = FALSE;
796 //Settings.SuperScopeMaster = FALSE;
797 Settings.DSP1Master = FALSE;
798 Settings.SA1 = FALSE;
800 Settings.SDD1 = FALSE;
803 if (Settings.ForceSA1 ||
804 (!Settings.ForceNoSA1 && (ROMSpeed & ~0x10) == 0x23 &&
805 (ROMType & 0xf) > 3 && (ROMType & 0xf0) == 0x30))
808 Settings.MultiPlayer5Master = FALSE;
809 //Settings.MouseMaster = FALSE;
810 //Settings.SuperScopeMaster = FALSE;
811 Settings.DSP1Master = FALSE;
813 Settings.SDD1 = FALSE;
817 if ((ROMSpeed & ~0x10) == 0x25)
818 TalesROMMap (Interleaved);
820 if (strncmp ((char *) &Memory.ROM [0x7fc0], "SOUND NOVEL-TCOOL", 17) == 0 ||
821 strncmp ((char *) &Memory.ROM [0x7fc0], "DERBY STALLION 96", 17) == 0)
824 Settings.DSP1Master = FALSE;
827 if (strncmp ((char *) &Memory.ROM [0x7fc0], "THOROUGHBRED BREEDER3", 21) == 0 ||
828 strncmp ((char *) &Memory.ROM [0x7fc0], "RPG-TCOOL 2", 11) == 0)
831 Settings.DSP1Master = FALSE;
834 if (strncmp ((char *) &Memory.ROM [0x7fc0], "DEZAEMON ", 10) == 0)
836 Settings.DSP1Master = FALSE;
837 SRAM1024KLoROMMap ();
840 if (strncmp ((char *) &Memory.ROM [0x7fc0], "ADD-ON BASE CASSETE", 19) == 0)
842 Settings.MultiPlayer5Master = FALSE;
843 Settings.MouseMaster = FALSE;
844 Settings.SuperScopeMaster = FALSE;
845 Settings.DSP1Master = FALSE;
846 SufamiTurboLoROMMap();
850 if ((ROMSpeed & ~0x10) == 0x22 &&
851 strncmp (ROMName, "Super Street Fighter", 20) != 0)
860 int size = CalculatedSize;
866 uint32 remainder = CalculatedSize - size;
873 for (i = 0; i < size; i++)
876 for (i = 0; i < (int) remainder; i++)
877 sum2 += ROM [size + i];
882 if (CalculatedSize == 0x500000 && Memory.HiROM &&
883 strncmp ((const char *)&ROM[0xffb0], "18AZ", 4) == 0 &&
884 !memcmp(&ROM[0xffd5], "\x3a\xf9\x0d\x03\x00\x33\x00", 7))
887 sum1 += sum2 * (size / remainder);
892 if (Settings.ForceNTSC)
893 Settings.PAL = FALSE;
895 if (Settings.ForcePAL)
900 Settings.PAL = ROM [0xffd9] >= 2;
902 Settings.PAL = ROM [0x7fd9] >= 2;
906 Settings.FrameTime = Settings.FrameTimePAL;
907 Memory.ROMFramesPerSecond = 50;
911 Settings.FrameTime = Settings.FrameTimeNTSC;
912 Memory.ROMFramesPerSecond = 60;
915 ROMName[ROM_NAME_LEN - 1] = 0;
916 if (strlen (ROMName))
918 char *p = ROMName + strlen (ROMName) - 1;
920 while (p > ROMName && *(p - 1) == ' ')
925 if (Settings.SuperFX)
927 CPU.Memory_SRAMMask = 0xffff;
928 Memory.SRAMSize = 16;
932 CPU.Memory_SRAMMask = Memory.SRAMSize ?
933 ((1 << (Memory.SRAMSize + 3)) * 128) - 1 : 0;
936 IAPU.OneCycle = ONE_APU_CYCLE;
937 Settings.Shutdown = Settings.ShutdownMaster;
939 SetDSP = &DSP1SetByte;
940 GetDSP = &DSP1GetByte;
943 sprintf (ROMName, "%s", Safe (ROMName));
944 sprintf (ROMId, "%s", Safe (ROMId));
945 sprintf (CompanyId, "%s", Safe (CompanyId));
947 sprintf (String, "\"%s\" [%s] %s, %s, Type: %s, Mode: %s, TV: %s, S-RAM: %s, ROMId: %s Company: %2.2s",
949 (ROMChecksum + ROMComplementChecksum != 0xffff ||
950 ROMChecksum != sum1) ? "bad checksum" : "checksum ok",
960 S9xMessage (S9X_INFO, S9X_ROM_INFO, String);
963 bool8_32 CMemory::LoadSRAM (const char *filename)
965 int size = Memory.SRAMSize ?
966 (1 << (Memory.SRAMSize + 3)) * 128 : 0;
968 memset (SRAM, SNESGameFixes.SRAMInitialValue, 0x20000);
976 if ((file = fopen(filename, "rb")))
978 int len = fread ((char*) ::SRAM, 1, 0x20000, file);
980 if (len - size == 512)
982 // S-RAM file has a header - remove it
983 memmove (::SRAM, ::SRAM + 512, size);
985 if (len == size + SRTC_SRAM_PAD)
987 S9xSRTCPostLoadState ();
990 rtc.mode = MODE_READ;
1001 S9xSDD1LoadLoggedData ();
1006 bool8_32 CMemory::SaveSRAM (const char *filename)
1008 size_t size = Memory.SRAMSize ?
1009 (1 << (Memory.SRAMSize + 3)) * 128 : 0;
1013 size += SRTC_SRAM_PAD;
1014 S9xSRTCPreSaveState ();
1017 if (Settings.SDD1) S9xSDD1SaveLoggedData ();
1019 if (size > 0x20000) size = 0x20000;
1021 if (size && *Memory.ROMFilename)
1024 if ((file = fopen (filename, "wb")))
1026 if (fwrite((char *) ::SRAM, size, 1, file) == size) {
1038 void CMemory::FixROMSpeed ()
1042 for (c = 0x800; c < 0x1000; c++)
1045 MemorySpeed [c] = (uint8) CPU.FastROMSpeed;
1049 void CMemory::WriteProtectROM ()
1051 memmove ((void *) WriteMap, (void *) Map, sizeof (Map));
1052 for (int c = 0; c < 0x1000; c++)
1055 WriteMap [c] = (uint8 *) MAP_NONE;
1059 void CMemory::MapRAM ()
1063 // Banks 7e->7f, RAM
1064 for (c = 0; c < 16; c++)
1066 Map [c + 0x7e0] = RAM;
1067 Map [c + 0x7f0] = RAM + 0x10000;
1068 BlockIsRAM [c + 0x7e0] = TRUE;
1069 BlockIsRAM [c + 0x7f0] = TRUE;
1070 BlockIsROM [c + 0x7e0] = FALSE;
1071 BlockIsROM [c + 0x7f0] = FALSE;
1074 // Banks 70->77, S-RAM
1075 for (c = 0; c < 0x80; c++)
1077 Map [c + 0x700] = (uint8 *) MAP_LOROM_SRAM;
1078 BlockIsRAM [c + 0x700] = TRUE;
1079 BlockIsROM [c + 0x700] = FALSE;
1083 void CMemory::MapExtraRAM ()
1087 // Banks 7e->7f, RAM
1088 for (c = 0; c < 16; c++)
1090 Map [c + 0x7e0] = RAM;
1091 Map [c + 0x7f0] = RAM + 0x10000;
1092 BlockIsRAM [c + 0x7e0] = TRUE;
1093 BlockIsRAM [c + 0x7f0] = TRUE;
1094 BlockIsROM [c + 0x7e0] = FALSE;
1095 BlockIsROM [c + 0x7f0] = FALSE;
1098 // Banks 70->73, S-RAM
1099 for (c = 0; c < 16; c++)
1101 Map [c + 0x700] = ::SRAM;
1102 Map [c + 0x710] = ::SRAM + 0x8000;
1103 Map [c + 0x720] = ::SRAM + 0x10000;
1104 Map [c + 0x730] = ::SRAM + 0x18000;
1106 BlockIsRAM [c + 0x700] = TRUE;
1107 BlockIsROM [c + 0x700] = FALSE;
1108 BlockIsRAM [c + 0x710] = TRUE;
1109 BlockIsROM [c + 0x710] = FALSE;
1110 BlockIsRAM [c + 0x720] = TRUE;
1111 BlockIsROM [c + 0x720] = FALSE;
1112 BlockIsRAM [c + 0x730] = TRUE;
1113 BlockIsROM [c + 0x730] = FALSE;
1117 void CMemory::LoROMMap ()
1122 // Banks 00->3f and 80->bf
1123 for (c = 0; c < 0x400; c += 16)
1125 Map [c + 0] = Map [c + 0x800] = RAM;
1126 Map [c + 1] = Map [c + 0x801] = RAM;
1127 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1128 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1130 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1131 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1132 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1133 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1134 if (Settings.DSP1Master)
1136 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1137 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1142 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_C4;
1143 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_C4;
1147 Map [c + 6] = Map [c + 0x806] = (uint8 *) bytes0x2000 - 0x6000;
1148 Map [c + 7] = Map [c + 0x807] = (uint8 *) bytes0x2000 - 0x6000;
1151 for (i = c + 8; i < c + 16; i++)
1153 Map [i] = Map [i + 0x800] = &ROM [(c << 11) % CalculatedSize] - 0x8000;
1154 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1157 for (i = c; i < c + 16; i++)
1162 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1166 if (Settings.DSP1Master)
1168 // Banks 30->3f and b0->bf
1169 for (c = 0x300; c < 0x400; c += 16)
1171 for (i = c + 8; i < c + 16; i++)
1173 Map [i] = Map [i + 0x800] = (uint8 *) MAP_DSP;
1174 BlockIsROM [i] = BlockIsROM [i + 0x800] = FALSE;
1179 // Banks 40->7f and c0->ff
1180 for (c = 0; c < 0x400; c += 16)
1182 for (i = c; i < c + 8; i++)
1183 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) % CalculatedSize];
1185 for (i = c + 8; i < c + 16; i++)
1186 Map [i + 0x400] = Map [i + 0xc00] = &ROM [((c << 11) + 0x200000) % CalculatedSize - 0x8000];
1188 for (i = c; i < c + 16; i++)
1190 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1191 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1195 if (Settings.DSP1Master)
1197 for (c = 0; c < 0x100; c++)
1199 Map [c + 0xe00] = (uint8 *) MAP_DSP;
1200 MemorySpeed [c + 0xe00] = SLOW_ONE_CYCLE;
1201 BlockIsROM [c + 0xe00] = FALSE;
1208 void CMemory::HiROMMap ()
1213 // Banks 00->3f and 80->bf
1214 for (c = 0; c < 0x400; c += 16)
1216 Map [c + 0] = Map [c + 0x800] = RAM;
1217 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1218 Map [c + 1] = Map [c + 0x801] = RAM;
1219 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1221 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1222 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1223 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1224 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1225 if (Settings.DSP1Master)
1227 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1228 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1232 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1233 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1236 for (i = c + 8; i < c + 16; i++)
1238 Map [i] = Map [i + 0x800] = &ROM [(c << 12) % CalculatedSize];
1239 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1242 for (i = c; i < c + 16; i++)
1247 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1251 // Banks 30->3f and b0->bf, address ranges 6000->7fff is S-RAM.
1252 for (c = 0; c < 16; c++)
1254 Map [0x306 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1255 Map [0x307 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1256 Map [0xb06 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1257 Map [0xb07 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1258 BlockIsRAM [0x306 + (c << 4)] = TRUE;
1259 BlockIsRAM [0x307 + (c << 4)] = TRUE;
1260 BlockIsRAM [0xb06 + (c << 4)] = TRUE;
1261 BlockIsRAM [0xb07 + (c << 4)] = TRUE;
1264 // Banks 40->7f and c0->ff
1265 for (c = 0; c < 0x400; c += 16)
1267 for (i = c; i < c + 16; i++)
1269 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1270 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1271 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1279 void CMemory::TalesROMMap (bool8_32 Interleaved)
1284 uint32 OFFSET0 = 0x400000;
1285 uint32 OFFSET1 = 0x400000;
1286 uint32 OFFSET2 = 0x000000;
1295 // Banks 00->3f and 80->bf
1296 for (c = 0; c < 0x400; c += 16)
1298 Map [c + 0] = Map [c + 0x800] = RAM;
1299 Map [c + 1] = Map [c + 0x801] = RAM;
1300 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1301 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1303 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1304 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1305 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1306 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1307 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1308 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1309 for (i = c + 8; i < c + 16; i++)
1311 Map [i] = &ROM [((c << 12) + OFFSET0) % CalculatedSize];
1312 Map [i + 0x800] = &ROM [((c << 12) + OFFSET0) % CalculatedSize];
1313 BlockIsROM [i] = TRUE;
1314 BlockIsROM [i + 0x800] = TRUE;
1317 for (i = c; i < c + 16; i++)
1322 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1326 // Banks 30->3f and b0->bf, address ranges 6000->7ffff is S-RAM.
1327 for (c = 0; c < 16; c++)
1329 Map [0x306 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1330 Map [0x307 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1331 Map [0xb06 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1332 Map [0xb07 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1333 BlockIsRAM [0x306 + (c << 4)] = TRUE;
1334 BlockIsRAM [0x307 + (c << 4)] = TRUE;
1335 BlockIsRAM [0xb06 + (c << 4)] = TRUE;
1336 BlockIsRAM [0xb07 + (c << 4)] = TRUE;
1339 // Banks 40->7f and c0->ff
1340 for (c = 0; c < 0x400; c += 16)
1342 for (i = c; i < c + 8; i++)
1344 Map [i + 0x400] = &ROM [((c << 12) + OFFSET1) % CalculatedSize];
1345 Map [i + 0x408] = &ROM [((c << 12) + OFFSET1) % CalculatedSize];
1346 Map [i + 0xc00] = &ROM [((c << 12) + OFFSET2) % CalculatedSize];
1347 Map [i + 0xc08] = &ROM [((c << 12) + OFFSET2) % CalculatedSize];
1348 BlockIsROM [i + 0x400] = TRUE;
1349 BlockIsROM [i + 0x408] = TRUE;
1350 BlockIsROM [i + 0xc00] = TRUE;
1351 BlockIsROM [i + 0xc08] = TRUE;
1352 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1353 MemorySpeed [i + 0x408] = MemorySpeed [i + 0xc08] = SLOW_ONE_CYCLE;
1360 void CMemory::AlphaROMMap ()
1365 // Banks 00->3f and 80->bf
1366 for (c = 0; c < 0x400; c += 16)
1368 Map [c + 0] = Map [c + 0x800] = RAM;
1369 Map [c + 1] = Map [c + 0x801] = RAM;
1370 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1371 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1373 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1374 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1375 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1376 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1377 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1378 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1380 for (i = c + 8; i < c + 16; i++)
1382 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1383 BlockIsROM [i] = TRUE;
1386 for (i = c; i < c + 16; i++)
1391 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1395 // Banks 40->7f and c0->ff
1397 for (c = 0; c < 0x400; c += 16)
1399 for (i = c; i < c + 16; i++)
1401 Map [i + 0x400] = &ROM [(c << 12) % CalculatedSize];
1402 Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1403 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1404 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1412 void CMemory::SuperFXROMMap ()
1417 // Banks 00->3f and 80->bf
1418 for (c = 0; c < 0x400; c += 16)
1420 Map [c + 0] = Map [c + 0x800] = RAM;
1421 Map [c + 1] = Map [c + 0x801] = RAM;
1422 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1423 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1425 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1426 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1427 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1428 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1429 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1430 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1431 for (i = c + 8; i < c + 16; i++)
1433 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1434 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1437 for (i = c; i < c + 8; i++)
1442 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1446 // Banks 40->7f and c0->ff
1447 for (c = 0; c < 0x400; c += 16)
1449 for (i = c; i < c + 16; i++)
1451 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1452 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1453 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1457 // Banks 7e->7f, RAM
1458 for (c = 0; c < 16; c++)
1460 Map [c + 0x7e0] = RAM;
1461 Map [c + 0x7f0] = RAM + 0x10000;
1462 BlockIsRAM [c + 0x7e0] = TRUE;
1463 BlockIsRAM [c + 0x7f0] = TRUE;
1464 BlockIsROM [c + 0x7e0] = FALSE;
1465 BlockIsROM [c + 0x7f0] = FALSE;
1468 // Banks 70->71, S-RAM
1469 for (c = 0; c < 32; c++)
1471 Map [c + 0x700] = ::SRAM + (((c >> 4) & 1) << 16);
1472 BlockIsRAM [c + 0x700] = TRUE;
1473 BlockIsROM [c + 0x700] = FALSE;
1476 // Banks 00->3f and 80->bf address ranges 6000->7fff is RAM.
1477 for (c = 0; c < 0x40; c++)
1479 Map [0x006 + (c << 4)] = (uint8 *) ::SRAM - 0x6000;
1480 Map [0x007 + (c << 4)] = (uint8 *) ::SRAM - 0x6000;
1481 Map [0x806 + (c << 4)] = (uint8 *) ::SRAM - 0x6000;
1482 Map [0x807 + (c << 4)] = (uint8 *) ::SRAM - 0x6000;
1483 BlockIsRAM [0x006 + (c << 4)] = TRUE;
1484 BlockIsRAM [0x007 + (c << 4)] = TRUE;
1485 BlockIsRAM [0x806 + (c << 4)] = TRUE;
1486 BlockIsRAM [0x807 + (c << 4)] = TRUE;
1488 // Replicate the first 2Mb of the ROM at ROM + 2MB such that each 32K
1489 // block is repeated twice in each 64K block.
1490 for (c = 0; c < 64; c++)
1492 memmove (&ROM [0x200000 + c * 0x10000], &ROM [c * 0x8000], 0x8000);
1493 memmove (&ROM [0x208000 + c * 0x10000], &ROM [c * 0x8000], 0x8000);
1499 void CMemory::SA1ROMMap ()
1504 // Banks 00->3f and 80->bf
1505 for (c = 0; c < 0x400; c += 16)
1507 Map [c + 0] = Map [c + 0x800] = RAM;
1508 Map [c + 1] = Map [c + 0x801] = RAM;
1509 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1510 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1512 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1513 Map [c + 3] = Map [c + 0x803] = (uint8 *) &Memory.FillRAM [0x3000] - 0x3000;
1514 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1515 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1516 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_BWRAM;
1517 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_BWRAM;
1518 for (i = c + 8; i < c + 16; i++)
1520 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1521 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1524 for (i = c; i < c + 16; i++)
1529 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1534 for (c = 0; c < 0x400; c += 16)
1536 for (i = c; i < c + 16; i++)
1537 Map [i + 0x400] = (uint8 *) &SRAM [(c << 12) & 0x1ffff];
1539 for (i = c; i < c + 16; i++)
1541 MemorySpeed [i + 0x400] = SLOW_ONE_CYCLE;
1542 BlockIsROM [i + 0x400] = FALSE;
1547 for (c = 0; c < 0x400; c += 16)
1549 for (i = c; i < c + 16; i++)
1551 Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1552 MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1553 BlockIsROM [i + 0xc00] = TRUE;
1557 for (c = 0; c < 16; c++)
1559 Map [c + 0x7e0] = RAM;
1560 Map [c + 0x7f0] = RAM + 0x10000;
1561 BlockIsRAM [c + 0x7e0] = TRUE;
1562 BlockIsRAM [c + 0x7f0] = TRUE;
1563 BlockIsROM [c + 0x7e0] = FALSE;
1564 BlockIsROM [c + 0x7f0] = FALSE;
1568 // Now copy the map and correct it for the SA1 CPU.
1569 memmove ((void *) SA1.WriteMap, (void *) WriteMap, sizeof (WriteMap));
1570 memmove ((void *) SA1.Map, (void *) Map, sizeof (Map));
1572 // Banks 00->3f and 80->bf
1573 for (c = 0; c < 0x400; c += 16)
1575 SA1.Map [c + 0] = SA1.Map [c + 0x800] = &Memory.FillRAM [0x3000];
1576 SA1.Map [c + 1] = SA1.Map [c + 0x801] = (uint8 *) MAP_NONE;
1577 SA1.WriteMap [c + 0] = SA1.WriteMap [c + 0x800] = &Memory.FillRAM [0x3000];
1578 SA1.WriteMap [c + 1] = SA1.WriteMap [c + 0x801] = (uint8 *) MAP_NONE;
1582 for (c = 0; c < 0x100; c++)
1583 SA1.Map [c + 0x600] = SA1.WriteMap [c + 0x600] = (uint8 *) MAP_BWRAM_BITMAP;
1588 void CMemory::LoROM24MBSMap ()
1593 // Banks 00->3f and 80->bf
1594 for (c = 0; c < 0x400; c += 16)
1596 Map [c + 0] = Map [c + 0x800] = RAM;
1597 Map [c + 1] = Map [c + 0x801] = RAM;
1598 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1599 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1601 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1602 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1603 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1604 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1605 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1606 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1608 for (i = c + 8; i < c + 16; i++)
1610 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1611 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1614 for (i = c; i < c + 16; i++)
1619 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1623 // Banks 00->3f and 80->bf
1624 for (c = 0; c < 0x200; c += 16)
1626 Map [c + 0x800] = RAM;
1627 Map [c + 0x801] = RAM;
1628 BlockIsRAM [c + 0x800] = TRUE;
1629 BlockIsRAM [c + 0x801] = TRUE;
1631 Map [c + 0x802] = (uint8 *) MAP_PPU;
1632 Map [c + 0x803] = (uint8 *) MAP_PPU;
1633 Map [c + 0x804] = (uint8 *) MAP_CPU;
1634 Map [c + 0x805] = (uint8 *) MAP_CPU;
1635 Map [c + 0x806] = (uint8 *) MAP_NONE;
1636 Map [c + 0x807] = (uint8 *) MAP_NONE;
1638 for (i = c + 8; i < c + 16; i++)
1640 Map [i + 0x800] = &ROM [c << 11] - 0x8000 + 0x200000;
1641 BlockIsROM [i + 0x800] = TRUE;
1644 for (i = c; i < c + 16; i++)
1648 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1652 // Banks 40->7f and c0->ff
1653 for (c = 0; c < 0x400; c += 16)
1655 for (i = c; i < c + 8; i++)
1656 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000];
1658 for (i = c + 8; i < c + 16; i++)
1659 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000 - 0x8000];
1661 for (i = c; i < c + 16; i++)
1663 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1664 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1672 void CMemory::SufamiTurboLoROMMap ()
1677 // Banks 00->3f and 80->bf
1678 for (c = 0; c < 0x400; c += 16)
1680 Map [c + 0] = Map [c + 0x800] = RAM;
1681 Map [c + 1] = Map [c + 0x801] = RAM;
1682 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1683 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1685 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1686 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1687 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1688 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1689 if (Settings.DSP1Master)
1691 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1692 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1696 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1697 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1699 for (i = c + 8; i < c + 16; i++)
1701 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1702 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1705 for (i = c; i < c + 16; i++)
1710 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1714 if (Settings.DSP1Master)
1716 // Banks 30->3f and b0->bf
1717 for (c = 0x300; c < 0x400; c += 16)
1719 for (i = c + 8; i < c + 16; i++)
1721 Map [i] = Map [i + 0x800] = (uint8 *) MAP_DSP;
1722 BlockIsROM [i] = BlockIsROM [i + 0x800] = FALSE;
1727 // Banks 40->7f and c0->ff
1728 for (c = 0; c < 0x400; c += 16)
1730 for (i = c; i < c + 8; i++)
1731 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000];
1733 for (i = c + 8; i < c + 16; i++)
1734 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000 - 0x8000];
1736 for (i = c; i < c + 16; i++)
1738 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1739 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1743 if (Settings.DSP1Master)
1745 for (c = 0; c < 0x100; c++)
1747 Map [c + 0xe00] = (uint8 *) MAP_DSP;
1748 MemorySpeed [c + 0xe00] = SLOW_ONE_CYCLE;
1749 BlockIsROM [c + 0xe00] = FALSE;
1753 // Banks 7e->7f, RAM
1754 for (c = 0; c < 16; c++)
1756 Map [c + 0x7e0] = RAM;
1757 Map [c + 0x7f0] = RAM + 0x10000;
1758 BlockIsRAM [c + 0x7e0] = TRUE;
1759 BlockIsRAM [c + 0x7f0] = TRUE;
1760 BlockIsROM [c + 0x7e0] = FALSE;
1761 BlockIsROM [c + 0x7f0] = FALSE;
1764 // Banks 60->67, S-RAM
1765 for (c = 0; c < 0x80; c++)
1767 Map [c + 0x600] = (uint8 *) MAP_LOROM_SRAM;
1768 BlockIsRAM [c + 0x600] = TRUE;
1769 BlockIsROM [c + 0x600] = FALSE;
1775 void CMemory::SRAM512KLoROMMap ()
1780 // Banks 00->3f and 80->bf
1781 for (c = 0; c < 0x400; c += 16)
1783 Map [c + 0] = Map [c + 0x800] = RAM;
1784 Map [c + 1] = Map [c + 0x801] = RAM;
1785 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1786 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1788 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1789 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1790 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1791 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1792 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1793 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1795 for (i = c + 8; i < c + 16; i++)
1797 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1798 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1801 for (i = c; i < c + 16; i++)
1806 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1810 // Banks 40->7f and c0->ff
1811 for (c = 0; c < 0x400; c += 16)
1813 for (i = c; i < c + 8; i++)
1814 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000];
1816 for (i = c + 8; i < c + 16; i++)
1817 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000 - 0x8000];
1819 for (i = c; i < c + 16; i++)
1821 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1822 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1830 void CMemory::SRAM1024KLoROMMap ()
1835 // Banks 00->3f and 80->bf
1836 for (c = 0; c < 0x400; c += 16)
1838 Map [c + 0] = Map [c + 0x800] = Map [c + 0x400] = Map [c + 0xc00] = RAM;
1839 Map [c + 1] = Map [c + 0x801] = Map [c + 0x401] = Map [c + 0xc01] = RAM;
1840 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = BlockIsRAM [c + 0x400] = BlockIsRAM [c + 0xc00] = TRUE;
1841 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = BlockIsRAM [c + 0x401] = BlockIsRAM [c + 0xc01] = TRUE;
1843 Map [c + 2] = Map [c + 0x802] = Map [c + 0x402] = Map [c + 0xc02] = (uint8 *) MAP_PPU;
1844 Map [c + 3] = Map [c + 0x803] = Map [c + 0x403] = Map [c + 0xc03] = (uint8 *) MAP_PPU;
1845 Map [c + 4] = Map [c + 0x804] = Map [c + 0x404] = Map [c + 0xc04] = (uint8 *) MAP_CPU;
1846 Map [c + 5] = Map [c + 0x805] = Map [c + 0x405] = Map [c + 0xc05] = (uint8 *) MAP_CPU;
1847 Map [c + 6] = Map [c + 0x806] = Map [c + 0x406] = Map [c + 0xc06] = (uint8 *) MAP_NONE;
1848 Map [c + 7] = Map [c + 0x807] = Map [c + 0x407] = Map [c + 0xc07] = (uint8 *) MAP_NONE;
1849 for (i = c + 8; i < c + 16; i++)
1851 Map [i] = Map [i + 0x800] = Map [i + 0x400] = Map [i + 0xc00] = &ROM [c << 11] - 0x8000;
1852 BlockIsROM [i] = BlockIsROM [i + 0x800] = BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1855 for (i = c; i < c + 16; i++)
1859 MemorySpeed [i] = MemorySpeed [i + 0x800] =
1860 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1868 void CMemory::BSHiROMMap ()
1873 // Banks 00->3f and 80->bf
1874 for (c = 0; c < 0x400; c += 16)
1876 Map [c + 0] = Map [c + 0x800] = RAM;
1877 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1878 Map [c + 1] = Map [c + 0x801] = RAM;
1879 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1881 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1882 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1883 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1884 // XXX: How large is SRAM??
1885 Map [c + 5] = Map [c + 0x805] = (uint8 *) SRAM;
1886 BlockIsRAM [c + 5] = BlockIsRAM [c + 0x805] = TRUE;
1887 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1888 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1890 for (i = c + 8; i < c + 16; i++)
1892 Map [i] = Map [i + 0x800] = &ROM [(c << 12) % CalculatedSize];
1893 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1896 for (i = c; i < c + 16; i++)
1901 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1905 // Banks 60->7d offset 0000->7fff & 60->7f offset 8000->ffff PSRAM
1906 // XXX: How large is PSRAM?
1907 for (c = 0x600; c < 0x7e0; c += 16)
1909 for (i = c; i < c + 8; i++)
1911 Map [i] = &ROM [0x400000 + (c << 11)];
1912 BlockIsRAM [i] = TRUE;
1914 for (i = c + 8; i < c + 16; i++)
1916 Map [i] = &ROM [0x400000 + (c << 11) - 0x8000];
1917 BlockIsRAM [i] = TRUE;
1921 // Banks 40->7f and c0->ff
1922 for (c = 0; c < 0x400; c += 16)
1924 for (i = c; i < c + 16; i++)
1926 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1927 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1928 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1936 const char *CMemory::TVStandard ()
1938 return (Settings.PAL ? "PAL" : "NTSC");
1941 const char *CMemory::Speed ()
1943 return (ROMSpeed & 0x10 ? "120ns" : "200ns");
1946 const char *CMemory::MapType ()
1948 return (HiROM ? "HiROM" : "LoROM");
1951 const char *CMemory::StaticRAMSize ()
1953 static char tmp [20];
1955 if (Memory.SRAMSize > 16)
1957 sprintf (tmp, "%dKb", (CPU.Memory_SRAMMask + 1) / 1024);
1961 const char *CMemory::Size ()
1963 static char tmp [20];
1965 if (ROMSize < 7 || ROMSize - 7 > 23)
1967 sprintf (tmp, "%dMbits", 1 << (ROMSize - 7));
1971 const char *CMemory::KartContents ()
1973 static char tmp [30];
1974 static const char *CoPro [16] = {
1975 "DSP1", "SuperFX", "OBC1", "SA-1", "S-DD1", "S-RTC", "CoPro#6",
1976 "CoPro#7", "CoPro#8", "CoPro#9", "CoPro#10", "CoPro#11", "CoPro#12",
1977 "CoPro#13", "CoPro#14", "CoPro-Custom"
1979 static const char *Contents [3] = {
1980 "ROM", "ROM+RAM", "ROM+RAM+BAT"
1983 return ("ROM only");
1985 sprintf (tmp, "%s", Contents [(ROMType & 0xf) % 3]);
1987 if ((ROMType & 0xf) >= 3)
1988 sprintf (tmp, "%s+%s", tmp, CoPro [(ROMType & 0xf0) >> 4]);
1993 const char *CMemory::MapMode ()
1995 static char tmp [4];
1996 sprintf (tmp, "%02x", ROMSpeed & ~0x10);
2000 const char *CMemory::ROMID ()
2005 void CMemory::ApplyROMFixes ()
2008 if (strncmp(ROMName, "DUNGEON MASTER", 14) == 0)
2011 SetDSP=&DSP2SetByte;
2012 GetDSP=&DSP2GetByte;
2015 // Enable S-RTC (Real Time Clock) emulation for Dai Kaijyu Monogatari 2
2016 Settings.SRTC = ((ROMType & 0xf0) >> 4) == 5;
2018 Settings.StrikeGunnerOffsetHack = strcmp (ROMName, "STRIKE GUNNER") == 0 ? 7 : 0;
2020 CPU.NMITriggerPoint = 4;
2021 if (strcmp (ROMName, "CACOMA KNIGHT") == 0)
2022 CPU.NMITriggerPoint = 25;
2024 // These games complain if the multi-player adaptor is 'connected'
2025 if (strcmp (ROMName, "TETRIS&Dr.MARIO") == 0 ||
2026 strcmp (ROMName, "JIGSAW PARTY") == 0 ||
2027 strcmp (ROMName, "SUPER PICROSS") == 0 ||
2028 strcmp (ROMName, "KIRBY NO KIRA KIZZU") == 0 ||
2029 strcmp (ROMName, "BLOCK") == 0 ||
2030 strncmp (ROMName, "SUPER BOMBLISS", 14) == 0 ||
2031 strcmp (ROMId, "ABOJ") == 0)
2033 Settings.MultiPlayer5Master = FALSE;
2034 Settings.MouseMaster = FALSE;
2035 Settings.SuperScopeMaster = FALSE;
2038 // Games which spool sound samples between the SNES and sound CPU using
2039 // H-DMA as the sample is playing.
2040 if (strcmp (ROMName, "EARTHWORM JIM 2") == 0 ||
2041 strcmp (ROMName, "PRIMAL RAGE") == 0 ||
2042 strcmp (ROMName, "CLAY FIGHTER") == 0 ||
2043 strcmp (ROMName, "ClayFighter 2") == 0 ||
2044 strncasecmp (ROMName, "MADDEN", 6) == 0 ||
2045 strncmp (ROMName, "NHL", 3) == 0 ||
2046 strcmp (ROMName, "WeaponLord") == 0)
2048 Settings.Shutdown = FALSE;
2053 if (strcmp (ROMId, "CQ ") == 0 ||
2055 strncmp (ROMId, "JG", 2) == 0 ||
2056 strcmp (ROMName, "GAIA GENSOUKI 1 JPN") == 0)
2059 Settings.APUEnabled |= 2;
2060 CPU.APU_APUExecuting |= 2;
2063 // RENDERING RANGER R2
2064 if (strcmp (ROMId, "AVCJ") == 0 ||
2066 strncmp (ROMId, "ARF", 3) == 0 ||
2067 // Tales of Phantasia
2068 strncmp (ROMId, "ATV", 3) == 0 ||
2070 strncasecmp (ROMName, "ActRaiser", 9) == 0 ||
2072 strcmp (ROMName, "SOULBLAZER - 1 USA") == 0 ||
2073 strcmp (ROMName, "SOULBLADER - 1") == 0 ||
2074 strncmp (ROMName, "SOULBLAZER 1",12) == 0 ||
2076 strncmp (ROMId, "AQT", 3) == 0 ||
2078 strncmp (ROMId, "E9 ", 3) == 0 ||
2079 strcmp (ROMName, "SLAP STICK 1 JPN") == 0 ||
2080 // ZENNIHON PURORESU2
2081 strncmp (ROMId, "APR", 3) == 0 ||
2083 strncmp (ROMId, "A4B", 3) == 0 ||
2084 // UFO KAMEN YAKISOBAN
2085 strncmp (ROMId, "Y7 ", 3) == 0 ||
2086 strncmp (ROMId, "Y9 ", 3) == 0 ||
2087 // Panic Bomber World
2088 strncmp (ROMId, "APB", 3) == 0 ||
2089 ((strncmp (ROMName, "Parlor", 6) == 0 ||
2090 strcmp (ROMName, "HEIWA Parlor!Mini8") == 0 ||
2091 strncmp (ROMName, "SANKYO Fever! ̨°ÊÞ°!", 21) == 0) &&
2092 strcmp (CompanyId, "A0") == 0) ||
2093 strcmp (ROMName, "DARK KINGDOM") == 0 ||
2094 strcmp (ROMName, "ZAN3 SFC") == 0 ||
2095 strcmp (ROMName, "HIOUDEN") == 0 ||
2096 strcmp (ROMName, "ÃݼɳÀ") == 0 ||
2097 strcmp (ROMName, "FORTUNE QUEST") == 0 ||
2098 strcmp (ROMName, "FISHING TO BASSING") == 0 ||
2099 strncmp (ROMName, "TokyoDome '95Battle 7", 21) == 0 ||
2100 strcmp (ROMName, "OHMONO BLACKBASS") == 0)
2103 // notaz: strangely enough, these games work properly with my hack enabled
2104 Settings.APUEnabled |= 2;
2105 CPU.APU_APUExecuting |= 2;
2108 if (strcmp (ROMName, "BATMAN--REVENGE JOKER") == 0)
2110 Memory.HiROM = FALSE;
2111 Memory.LoROM = TRUE;
2114 Settings.StarfoxHack = strcmp (ROMName, "STAR FOX") == 0 ||
2115 strcmp (ROMName, "STAR WING") == 0;
2116 Settings.WinterGold = strcmp (ROMName, "FX SKIING NINTENDO 96") == 0 ||
2117 strcmp (ROMName, "DIRT RACER") == 0 ||
2118 strcmp (ROMName, "Stunt Race FX") == 0 ||
2119 Settings.StarfoxHack;
2120 Settings.ChuckRock = strcmp (ROMName, "CHUCK ROCK") == 0;
2121 Settings.Dezaemon = strcmp (ROMName, "DEZAEMON") == 0;
2123 if (strcmp (ROMName, "RADICAL DREAMERS") == 0 ||
2124 strcmp (ROMName, "TREASURE CONFLIX") == 0)
2128 for (c = 0; c < 0x80; c++)
2130 Map [c + 0x700] = ROM + 0x200000 + 0x1000 * (c & 0xf0);
2131 BlockIsRAM [c + 0x700] = TRUE;
2132 BlockIsROM [c + 0x700] = FALSE;
2134 for (c = 0; c < 0x400; c += 16)
2136 Map [c + 5] = Map [c + 0x805] = ROM + 0x300000;
2137 BlockIsRAM [c + 5] = BlockIsRAM [c + 0x805] = TRUE;
2142 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE *
2143 Settings.CyclesPercentage) / 100;
2145 if (strcmp (ROMId, "ASRJ") == 0 && Settings.CyclesPercentage == 100)
2147 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 95) / 100;
2149 // Power Rangers Fight
2150 if (strncmp (ROMId, "A3R", 3) == 0 ||
2152 strncmp (ROMId, "AJE", 3) == 0)
2153 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 103) / 100;
2155 if (strcmp (ROMId, "AWVP") == 0 || strcmp (ROMId, "AWVE") == 0 ||
2156 strcmp (ROMId, "AWVJ") == 0)
2158 // Wrestlemania Arcade
2160 if (Settings.CyclesPercentage == 100)
2161 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 140) / 100; // Fixes sound
2163 Settings.WrestlemaniaArcade = TRUE;
2165 // Theme Park - disable offset-per-tile mode.
2166 if (strcmp (ROMId, "ATQP") == 0)
2167 Settings.WrestlemaniaArcade = TRUE;
2169 if (strncmp (ROMId, "A3M", 3) == 0 && Settings.CyclesPercentage == 100)
2170 // Mortal Kombat 3. Fixes cut off speech sample
2171 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 110) / 100;
2173 if (strcmp (ROMName, "\x0bd\x0da\x0b2\x0d4\x0b0\x0bd\x0de") == 0 &&
2174 Settings.CyclesPercentage == 100)
2175 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 101) / 100;
2177 if (strcmp (ROMName, "WILD TRAX") == 0 ||
2178 strcmp (ROMName, "YOSSY'S ISLAND") == 0 ||
2179 strcmp (ROMName, "YOSHI'S ISLAND") == 0)
2180 CPU.TriedInterleavedMode2 = TRUE;
2182 // Start Trek: Deep Sleep 9
2183 if (strncmp (ROMId, "A9D", 3) == 0 && Settings.CyclesPercentage == 100)
2184 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 110) / 100;
2186 Settings.APURAMInitialValue = 0xff;
2188 if (strcmp (ROMName, "·³Ô¸¥Ò¶ÞÐÃݾ²") == 0 ||
2189 strcmp (ROMName, "KENTOUOU WORLDCHAMPIO") == 0 ||
2190 strcmp (ROMName, "TKO SUPERCHAMPIONSHIP") == 0 ||
2191 strcmp (ROMName, "TKO SUPER CHAMPIONSHI") == 0 ||
2192 strcmp (ROMName, "IHATOVO STORY") == 0 ||
2193 strcmp (ROMName, "WANDERERS FROM YS") == 0 ||
2194 strcmp (ROMName, "SUPER GENTYOUHISHI") == 0 ||
2195 // Panic Bomber World
2196 strncmp (ROMId, "APB", 3) == 0)
2198 Settings.APURAMInitialValue = 0;
2201 Settings.DaffyDuck = strcmp (ROMName, "DAFFY DUCK: MARV MISS") == 0;
2202 Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
2204 SA1.WaitAddress = NULL;
2205 SA1.WaitByteAddress1 = NULL;
2206 SA1.WaitByteAddress2 = NULL;
2209 if (strcmp (ROMId, "ZBPJ") == 0)
2211 SA1.WaitAddress = SA1.Map [0x0093f1 >> MEMMAP_SHIFT] + 0x93f1;
2212 SA1.WaitByteAddress1 = FillRAM + 0x304a;
2214 /* DAISENRYAKU EXPERTWW2 */
2215 if (strcmp (ROMId, "AEVJ") == 0)
2217 SA1.WaitAddress = SA1.Map [0x0ed18d >> MEMMAP_SHIFT] + 0xd18d;
2218 SA1.WaitByteAddress1 = FillRAM + 0x3000;
2221 if (strcmp (ROMId, "A2DJ") == 0)
2223 SA1.WaitAddress = SA1.Map [0x008b62 >> MEMMAP_SHIFT] + 0x8b62;
2225 /* Dragon Ballz HD */
2226 if (strcmp (ROMId, "AZIJ") == 0)
2228 SA1.WaitAddress = SA1.Map [0x008083 >> MEMMAP_SHIFT] + 0x8083;
2229 SA1.WaitByteAddress1 = FillRAM + 0x3020;
2231 /* SFC SDGUNDAMGNEXT */
2232 if (strcmp (ROMId, "ZX3J") == 0)
2234 SA1.WaitAddress = SA1.Map [0x0087f2 >> MEMMAP_SHIFT] + 0x87f2;
2235 SA1.WaitByteAddress1 = FillRAM + 0x30c4;
2237 /* ShougiNoHanamichi */
2238 if (strcmp (ROMId, "AARJ") == 0)
2240 SA1.WaitAddress = SA1.Map [0xc1f85a >> MEMMAP_SHIFT] + 0xf85a;
2241 SA1.WaitByteAddress1 = SRAM + 0x0c64;
2242 SA1.WaitByteAddress2 = SRAM + 0x0c66;
2244 /* KATO HIFUMI9DAN SYOGI */
2245 if (strcmp (ROMId, "A23J") == 0)
2247 SA1.WaitAddress = SA1.Map [0xc25037 >> MEMMAP_SHIFT] + 0x5037;
2248 SA1.WaitByteAddress1 = SRAM + 0x0c06;
2249 SA1.WaitByteAddress2 = SRAM + 0x0c08;
2252 if (strcmp (ROMId, "AIIJ") == 0)
2254 SA1.WaitAddress = SA1.Map [0xc100be >> MEMMAP_SHIFT] + 0x00be;
2255 SA1.WaitByteAddress1 = SRAM + 0x1002;
2256 SA1.WaitByteAddress2 = SRAM + 0x1004;
2259 if (strcmp (ROMId, "AITJ") == 0)
2261 SA1.WaitAddress = SA1.Map [0x0080b7 >> MEMMAP_SHIFT] + 0x80b7;
2263 /* J96 DREAM STADIUM */
2264 if (strcmp (ROMId, "AJ6J") == 0)
2266 SA1.WaitAddress = SA1.Map [0xc0f74a >> MEMMAP_SHIFT] + 0xf74a;
2269 if (strcmp (ROMId, "AJUJ") == 0)
2271 SA1.WaitAddress = SA1.Map [0x00d926 >> MEMMAP_SHIFT] + 0xd926;
2273 /* JKAKINOKI SHOUGI */
2274 if (strcmp (ROMId, "AKAJ") == 0)
2276 SA1.WaitAddress = SA1.Map [0x00f070 >> MEMMAP_SHIFT] + 0xf070;
2278 /* HOSHI NO KIRBY 3 & KIRBY'S DREAM LAND 3 JAP & US */
2279 if (strcmp (ROMId, "AFJJ") == 0 || strcmp (ROMId, "AFJE") == 0)
2281 SA1.WaitAddress = SA1.Map [0x0082d4 >> MEMMAP_SHIFT] + 0x82d4;
2282 SA1.WaitByteAddress1 = SRAM + 0x72a4;
2284 /* KIRBY SUPER DELUXE JAP */
2285 if (strcmp (ROMId, "AKFJ") == 0)
2287 SA1.WaitAddress = SA1.Map [0x008c93 >> MEMMAP_SHIFT] + 0x8c93;
2288 SA1.WaitByteAddress1 = FillRAM + 0x300a;
2289 SA1.WaitByteAddress2 = FillRAM + 0x300e;
2291 /* KIRBY SUPER DELUXE US */
2292 if (strcmp (ROMId, "AKFE") == 0)
2294 SA1.WaitAddress = SA1.Map [0x008cb8 >> MEMMAP_SHIFT] + 0x8cb8;
2295 SA1.WaitByteAddress1 = FillRAM + 0x300a;
2296 SA1.WaitByteAddress2 = FillRAM + 0x300e;
2298 /* SUPER MARIO RPG JAP & US */
2299 if (strcmp (ROMId, "ARWJ") == 0 || strcmp (ROMId, "ARWE") == 0)
2301 SA1.WaitAddress = SA1.Map [0xc0816f >> MEMMAP_SHIFT] + 0x816f;
2302 SA1.WaitByteAddress1 = FillRAM + 0x3000;
2305 if (strcmp (ROMId, "AVRJ") == 0)
2307 SA1.WaitAddress = SA1.Map [0x0085f2 >> MEMMAP_SHIFT] + 0x85f2;
2308 SA1.WaitByteAddress1 = FillRAM + 0x3024;
2310 /* AUGUSTA3 MASTERS NEW */
2311 if (strcmp (ROMId, "AO3J") == 0)
2313 SA1.WaitAddress = SA1.Map [0x00dddb >> MEMMAP_SHIFT] + 0xdddb;
2314 SA1.WaitByteAddress1 = FillRAM + 0x37b4;
2316 /* OSHABERI PARODIUS */
2317 if (strcmp (ROMId, "AJOJ") == 0)
2319 SA1.WaitAddress = SA1.Map [0x8084e5 >> MEMMAP_SHIFT] + 0x84e5;
2321 /* PANIC BOMBER WORLD */
2322 if (strcmp (ROMId, "APBJ") == 0)
2324 SA1.WaitAddress = SA1.Map [0x00857a >> MEMMAP_SHIFT] + 0x857a;
2326 /* PEBBLE BEACH NEW */
2327 if (strcmp (ROMId, "AONJ") == 0)
2329 SA1.WaitAddress = SA1.Map [0x00df33 >> MEMMAP_SHIFT] + 0xdf33;
2330 SA1.WaitByteAddress1 = FillRAM + 0x37b4;
2332 /* PGA EUROPEAN TOUR */
2333 if (strcmp (ROMId, "AEPE") == 0)
2335 SA1.WaitAddress = SA1.Map [0x003700 >> MEMMAP_SHIFT] + 0x3700;
2336 SA1.WaitByteAddress1 = FillRAM + 0x3102;
2339 if (strcmp (ROMId, "A3GE") == 0)
2341 SA1.WaitAddress = SA1.Map [0x003700 >> MEMMAP_SHIFT] + 0x3700;
2342 SA1.WaitByteAddress1 = FillRAM + 0x3102;
2344 /* POWER RANGERS 4 */
2345 if (strcmp (ROMId, "A4RE") == 0)
2347 SA1.WaitAddress = SA1.Map [0x009899 >> MEMMAP_SHIFT] + 0x9899;
2348 SA1.WaitByteAddress1 = FillRAM + 0x3000;
2350 /* PACHISURO PALUSUPE */
2351 if (strcmp (ROMId, "AGFJ") == 0)
2353 // Never seems to turn on the SA-1!
2355 /* SD F1 GRAND PRIX */
2356 if (strcmp (ROMId, "AGFJ") == 0)
2358 SA1.WaitAddress = SA1.Map [0x0181bc >> MEMMAP_SHIFT] + 0x81bc;
2360 /* SHOUGI MARJONG */
2361 if (strcmp (ROMId, "ASYJ") == 0)
2363 SA1.WaitAddress = SA1.Map [0x00f2cc >> MEMMAP_SHIFT] + 0xf2cc;
2364 SA1.WaitByteAddress1 = SRAM + 0x7ffe;
2365 SA1.WaitByteAddress2 = SRAM + 0x7ffc;
2368 if (strcmp (ROMId, "AX2J") == 0)
2370 SA1.WaitAddress = SA1.Map [0x00d675 >> MEMMAP_SHIFT] + 0xd675;
2373 /* SHINING SCORPION */
2374 if (strcmp (ROMId, "A4WJ") == 0)
2376 SA1.WaitAddress = SA1.Map [0xc048be >> MEMMAP_SHIFT] + 0x48be;
2378 /* SHIN SHOUGI CLUB */
2379 if (strcmp (ROMId, "AHJJ") == 0)
2381 SA1.WaitAddress = SA1.Map [0xc1002a >> MEMMAP_SHIFT] + 0x002a;
2382 SA1.WaitByteAddress1 = SRAM + 0x0806;
2383 SA1.WaitByteAddress2 = SRAM + 0x0808;
2386 // Additional game fixes by sanmaiwashi ...
2387 if (strcmp (ROMName, "SFX ŲĶÞÝÀÞÑÓɶÞÀØ 1") == 0)
2389 bytes0x2000 [0xb18] = 0x4c;
2390 bytes0x2000 [0xb19] = 0x4b;
2391 bytes0x2000 [0xb1a] = 0xea;
2394 if (strcmp (ROMName, "GOGO ACKMAN3") == 0 ||
2395 strcmp (ROMName, "HOME ALONE") == 0)
2397 // Banks 00->3f and 80->bf
2398 for (int c = 0; c < 0x400; c += 16)
2400 Map [c + 6] = Map [c + 0x806] = SRAM;
2401 Map [c + 7] = Map [c + 0x807] = SRAM;
2402 BlockIsROM [c + 6] = BlockIsROM [c + 0x806] = FALSE;
2403 BlockIsROM [c + 7] = BlockIsROM [c + 0x807] = FALSE;
2404 BlockIsRAM [c + 6] = BlockIsRAM [c + 0x806] = TRUE;
2405 BlockIsRAM [c + 7] = BlockIsRAM [c + 0x807] = TRUE;
2410 if (strncmp (ROMName, "SWORD WORLD SFC", 15) == 0 ||
2411 strcmp (ROMName, "SFC ¶ÒÝײÀÞ°") == 0)
2414 SNESGameFixes.NeedInit0x2137 = TRUE;
2415 Settings.APUEnabled |= 2;
2416 CPU.APU_APUExecuting |= 2;
2419 if (strncmp (ROMName, "SHIEN THE BLADE CHASE", 21) == 0)
2420 SNESGameFixes.Old_Read0x4200 = TRUE;
2422 if (strcmp (ROMName, "ºÞ¼Þ× ¶²¼Þ³ÀÞ²¹¯¾Ý") == 0)
2423 SNESGameFixes.NeedInit0x2137 = TRUE;
2425 if (strcmp (ROMName, "UMIHARAKAWASE") == 0)
2426 SNESGameFixes.umiharakawaseFix = TRUE;
2428 if (strcmp (ROMName, "ALIENS vs. PREDATOR") == 0)
2429 SNESGameFixes.alienVSpredetorFix = TRUE;
2431 if (strcmp (ROMName, "demon's blazon") == 0 ||
2432 strcmp (ROMName, "demon's crest") == 0 ||
2433 strcmp (ROMName, "ROCKMAN X") == 0 ||
2434 strcmp (ROMName, "MEGAMAN X") == 0)
2438 // Banks 0x808000, 0x408000 are mirroring.
2439 for (int c = 0; c < 8; c++)
2440 Map [0x408 + c] = ROM - 0x8000;
2443 if (strcmp (ROMName, "½°Ȩ̂߰нÀ") == 0 ||
2444 strcmp (ROMName, "½°Ȩ̂߰нÀ 2") == 0 ||
2445 strcmp (ROMName, "ZENKI TENCHIMEIDOU") == 0 ||
2446 strcmp (ROMName, "GANBA LEAGUE") == 0)
2448 SNESGameFixes.APU_OutPorts_ReturnValueFix = TRUE;
2452 if (strcmp (ROMName, "HITOMI3") == 0)
2454 Memory.SRAMSize = 1;
2455 CPU.Memory_SRAMMask = Memory.SRAMSize ?
2456 ((1 << (Memory.SRAMSize + 3)) * 128) - 1 : 0;
2459 if (strcmp (ROMName, "goemon 4") == 0)
2460 SNESGameFixes.SRAMInitialValue = 0x00;
2462 if (strcmp (ROMName, "PACHISLO ¹Ý·³") == 0)
2463 SNESGameFixes._0x213E_ReturnValue = 1;
2465 if (strcmp (ROMName, "»Þ Ï°¼Þ¬Ý ijʲÃÞÝ") == 0)
2466 SNESGameFixes.TouhaidenControllerFix = TRUE;
2468 if (strcmp (ROMName, "DRAGON KNIGHT 4") == 0)
2470 // Banks 70->7e, S-RAM
2471 for (int c = 0; c < 0xe0; c++)
2473 Map [c + 0x700] = (uint8 *) MAP_LOROM_SRAM;
2474 BlockIsRAM [c + 0x700] = TRUE;
2475 BlockIsROM [c + 0x700] = FALSE;
2480 if (strncmp (ROMName, "LETs PACHINKO(", 14) == 0)
2483 Settings.APUEnabled |= 2;
2484 CPU.APU_APUExecuting |= 2;
2485 if (!Settings.ForceNTSC && !Settings.ForcePAL)
2487 Settings.PAL = FALSE;
2488 Settings.FrameTime = Settings.FrameTimeNTSC;
2489 Memory.ROMFramesPerSecond = 60;
2493 if (strcmp (ROMName, "FURAI NO SIREN") == 0)
2494 SNESGameFixes.SoundEnvelopeHeightReading2 = TRUE;
2496 if(strcmp (ROMName, "XBAND JAPANESE MODEM") == 0)
2498 for (c = 0x200; c < 0x400; c += 16)
2500 for (int i = c; i < c + 16; i++)
2502 Map [i + 0x400] = Map [i + 0xc00] = &ROM[c * 0x1000];
2503 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = 8;
2504 BlockIsRAM [i + 0x400] = BlockIsRAM [i + 0xc00] = TRUE;
2505 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = FALSE;
2512 #define RomPatch(adr,ov,nv) \
2513 if (ROM [adr] == ov) \
2517 if (strcmp (ROMName, "LOVE QUEST") == 0)
2519 RomPatch (0x1385ec, 0xd0, 0xea);
2520 RomPatch (0x1385ed, 0xb2, 0xea);
2523 // Nangoku Syonen Papuwa Kun
2524 if (strcmp (ROMName, "NANGOKUSYONEN PAPUWA") == 0)
2525 RomPatch (0x1f0d1, 0xa0, 0x6b);
2528 if (strcmp (ROMName, "Tetsuwan Atom") == 0)
2530 RomPatch (0xe24c5, 0x90, 0xea);
2531 RomPatch (0xe24c6, 0xf3, 0xea);
2535 if (strcmp (ROMName, "SFC ODA NOBUNAGA") == 0)
2537 RomPatch (0x7497, 0x80, 0xea);
2538 RomPatch (0x7498, 0xd5, 0xea);
2542 if (strcmp (ROMName, "Super Batter Up") == 0)
2544 RomPatch (0x27ae0, 0xd0, 0xea);
2545 RomPatch (0x27ae1, 0xfa, 0xea);
2548 // Super Professional Baseball 2
2549 if (strcmp (ROMName, "SUPER PRO. BASE BALL2") == 0)
2551 RomPatch (0x1e4, 0x50, 0xea);
2552 RomPatch (0x1e5, 0xfb, 0xea);
2556 // Read variable size MSB int from a file
2557 static long ReadInt (FILE *f, unsigned nbytes)
2565 v = (v << 8) | (c & 0xFF);
2570 #define IPS_EOF 0x00454F46l
2572 void CMemory::CheckForIPSPatch (const char *rom_filename, bool8_32 header,
2575 char fname [_MAX_PATH + 1];
2576 FILE *patch_file = NULL;
2577 long offset = header ? 512 : 0;
2579 if (!(patch_file = fopen(S9xGetFilename(FILE_IPS), "rb"))) return;
2581 if (fread (fname, 1, 5, patch_file) != 5 || strncmp (fname, "PATCH", 5) != 0)
2583 fclose (patch_file);
2595 ofs = ReadInt (patch_file, 3);
2604 len = ReadInt (patch_file, 2);
2608 /* Apply patch block */
2611 if (ofs + len > MAX_ROM_SIZE)
2616 rchar = fgetc (patch_file);
2619 ROM [ofs++] = (uint8) rchar;
2626 rlen = ReadInt (patch_file, 2);
2630 rchar = fgetc (patch_file);
2634 if (ofs + rlen > MAX_ROM_SIZE)
2638 ROM [ofs++] = (uint8) rchar;
2645 // Check if ROM image needs to be truncated
2646 ofs = ReadInt (patch_file, 3);
2647 if (ofs != -1 && ofs - offset < rom_size)
2649 // Need to truncate ROM image
2650 rom_size = ofs - offset;
2652 fclose (patch_file);
2657 fclose (patch_file);