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.
61 extern struct FxInit_s SuperFX;
64 extern uint8 *SFXPlotTable;
68 static uint8 bytes0x2000 [0x2000];
70 extern char *rom_filename;
71 extern bool8 LoadZip(const char* , int32 *, int32 *);
73 bool8_32 CMemory::AllASCII (uint8 *b, int size)
75 for (int i = 0; i < size; i++)
77 if (b[i] < 32 || b[i] > 126)
83 int CMemory::ScoreHiROM (bool8_32 skip_header)
86 int o = skip_header ? 0xff00 + 0x200 : 0xff00;
88 if ((Memory.ROM [o + 0xdc] + (Memory.ROM [o + 0xdd] << 8) +
89 Memory.ROM [o + 0xde] + (Memory.ROM [o + 0xdf] << 8)) == 0xffff)
92 if (Memory.ROM [o + 0xda] == 0x33)
94 if ((Memory.ROM [o + 0xd5] & 0xf) < 4)
96 if (!(Memory.ROM [o + 0xfd] & 0x80))
98 if (CalculatedSize > 1024 * 1024 * 3)
100 if ((1 << (Memory.ROM [o + 0xd7] - 7)) > 48)
102 if (!AllASCII (&Memory.ROM [o + 0xb0], 6))
104 if (!AllASCII (&Memory.ROM [o + 0xc0], ROM_NAME_LEN - 1))
110 int CMemory::ScoreLoROM (bool8_32 skip_header)
113 int o = skip_header ? 0x7f00 + 0x200 : 0x7f00;
115 if ((Memory.ROM [o + 0xdc] + (Memory.ROM [o + 0xdd] << 8) +
116 Memory.ROM [o + 0xde] + (Memory.ROM [o + 0xdf] << 8)) == 0xffff)
119 if (Memory.ROM [o + 0xda] == 0x33)
121 if ((Memory.ROM [o + 0xd5] & 0xf) < 4)
123 if (CalculatedSize <= 1024 * 1024 * 16)
125 if (!(Memory.ROM [o + 0xfd] & 0x80))
127 if ((1 << (Memory.ROM [o + 0xd7] - 7)) > 48)
129 if (!AllASCII (&Memory.ROM [o + 0xb0], 6))
131 if (!AllASCII (&Memory.ROM [o + 0xc0], ROM_NAME_LEN - 1))
137 char *CMemory::Safe (const char *s)
139 static char *safe = NULL;
140 static int safe_len = 0;
142 int len = strlen (s);
143 if (!safe || len + 1 > safe_len)
146 free ((char *) safe);
147 safe = (char *) malloc (safe_len = len + 1);
150 for (int i = 0; i < len; i++)
152 if (s [i] >= 32 && s [i] < 127)
161 /**********************************************************************************************/
163 /* This function allocates all the memory needed by the emulator */
164 /**********************************************************************************************/
165 bool8_32 CMemory::Init ()
167 RAM = (uint8 *) malloc (0x20000);
168 SRAM = (uint8 *) malloc (0x20000);
169 VRAM = (uint8 *) malloc (0x10000);
170 ROM = (uint8 *) malloc (MAX_ROM_SIZE + 0x200 + 0x8000);
173 IPPU.TileCache [TILE_2BIT] = (uint8 *) malloc (MAX_2BIT_TILES * 128);
174 IPPU.TileCache [TILE_4BIT] = (uint8 *) malloc (MAX_4BIT_TILES * 128);
175 IPPU.TileCache [TILE_8BIT] = (uint8 *) malloc (MAX_8BIT_TILES * 128);
177 IPPU.TileCached [TILE_2BIT] = (uint8 *) malloc (MAX_2BIT_TILES);
178 IPPU.TileCached [TILE_4BIT] = (uint8 *) malloc (MAX_4BIT_TILES);
179 IPPU.TileCached [TILE_8BIT] = (uint8 *) malloc (MAX_8BIT_TILES);
181 if (!RAM || !SRAM || !VRAM || !ROM ||
182 !IPPU.TileCache [TILE_2BIT] || !IPPU.TileCache [TILE_4BIT] ||
183 !IPPU.TileCache [TILE_8BIT] || !IPPU.TileCached [TILE_2BIT] ||
184 !IPPU.TileCached [TILE_4BIT] || !IPPU.TileCached [TILE_8BIT])
190 // FillRAM uses first 32K of ROM image area, otherwise space just
191 // wasted. Might be read by the SuperFX code.
195 // Add 0x8000 to ROM image pointer to stop SuperFX code accessing
196 // unallocated memory (can cause crash on some ports).
199 C4RAM = ROM + 0x400000 + 8192 * 8;
205 SFXPlotTable = ROM + 0x400000;
207 SuperFX.pvRegisters = &Memory.FillRAM [0x3000];
208 SuperFX.nRamBanks = 1;
209 SuperFX.pvRam = ::SRAM;
210 SuperFX.nRomBanks = (2 * 1024 * 1024) / (32 * 1024);
211 SuperFX.pvRom = (uint8 *) ROM;
214 ZeroMemory (IPPU.TileCached [TILE_2BIT], MAX_2BIT_TILES);
215 ZeroMemory (IPPU.TileCached [TILE_4BIT], MAX_4BIT_TILES);
216 ZeroMemory (IPPU.TileCached [TILE_8BIT], MAX_8BIT_TILES);
224 void CMemory::Deinit ()
233 free ((char *) SRAM);
238 free ((char *) VRAM);
248 if (IPPU.TileCache [TILE_2BIT])
250 free ((char *) IPPU.TileCache [TILE_2BIT]);
251 IPPU.TileCache [TILE_2BIT] = NULL;
253 if (IPPU.TileCache [TILE_4BIT])
255 free ((char *) IPPU.TileCache [TILE_4BIT]);
256 IPPU.TileCache [TILE_4BIT] = NULL;
258 if (IPPU.TileCache [TILE_8BIT])
260 free ((char *) IPPU.TileCache [TILE_8BIT]);
261 IPPU.TileCache [TILE_8BIT] = NULL;
264 if (IPPU.TileCached [TILE_2BIT])
266 free ((char *) IPPU.TileCached [TILE_2BIT]);
267 IPPU.TileCached [TILE_2BIT] = NULL;
269 if (IPPU.TileCached [TILE_4BIT])
271 free ((char *) IPPU.TileCached [TILE_4BIT]);
272 IPPU.TileCached [TILE_4BIT] = NULL;
274 if (IPPU.TileCached [TILE_8BIT])
276 free ((char *) IPPU.TileCached [TILE_8BIT]);
277 IPPU.TileCached [TILE_8BIT] = NULL;
283 void CMemory::FreeSDD1Data ()
287 free ((char *) SDD1Index);
292 free ((char *) SDD1Data);
297 /**********************************************************************************************/
299 /**********************************************************************************************/
300 int checkzip( char * fn )
302 int cnt = strlen(fn);
303 if( ( (fn[cnt-1] == 'p') || (fn[cnt-1] == 'P') ) &&
304 ( (fn[cnt-2] == 'i') || (fn[cnt-2] == 'I') ) &&
305 ( (fn[cnt-3] == 'z') || (fn[cnt-3] == 'Z') ) ){
312 /**********************************************************************************************/
314 /* This function loads a Snes-Backup image */
315 /**********************************************************************************************/
317 #pragma warning(disable : 4101)
318 #pragma warning(disable : 4700)
320 bool8_32 CMemory::LoadROM (const char *filename)
322 unsigned long FileSize = 0;
325 bool8_32 Interleaved = FALSE;
326 bool8_32 Tales = FALSE;
327 char dir [_MAX_DIR + 1];
328 char drive [_MAX_DRIVE + 1];
329 char name [_MAX_FNAME + 1];
330 char ext [_MAX_EXT + 1];
331 char fname [_MAX_PATH + 1];
334 memset (&SNESGameFixes, 0, sizeof(SNESGameFixes));
335 SNESGameFixes.SRAMInitialValue = 0x60;
337 memset (bytes0x2000, 0, 0x2000);
338 CPU.TriedInterleavedMode2 = FALSE;
342 PathSplit(filename, drive, dir, name, ext);
343 PathMake(fname, drive, dir, name, ext);
345 int32 TotalFileSize = 0;
348 if( checkzip( fname ) )
350 if (!LoadZip (fname, &TotalFileSize, &HeaderCount))
353 strcpy (ROMFilename, fname);
358 if ((ROMFile = OPEN_STREAM (fname, "rb")) == NULL)
361 strcpy (ROMFilename, fname);
365 bool8_32 more = FALSE;
369 FileSize = READ_STREAM (ptr, MAX_ROM_SIZE + 0x200 - (ptr - ROM), ROMFile);
370 CLOSE_STREAM (ROMFile);
371 int calc_size = (FileSize / 0x2000) * 0x2000;
373 if ((FileSize - calc_size == 512 && !Settings.ForceNoHeader) ||
374 Settings.ForceHeader)
376 memmove (ptr, ptr + 512, calc_size);
381 TotalFileSize += FileSize;
384 if (ptr - ROM < MAX_ROM_SIZE + 0x200 &&
385 (isdigit (ext [0]) && ext [1] == 0 && ext [0] < '9'))
389 PathMake(fname, drive, dir, name, ext);
392 if (ptr - ROM < MAX_ROM_SIZE + 0x200 &&
393 (((len = strlen (name)) == 7 || len == 8) &&
394 strncasecmp (name, "sf", 2) == 0 &&
395 isdigit (name [2]) && isdigit (name [3]) && isdigit (name [4]) &&
396 isdigit (name [5]) && isalpha (name [len - 1])))
400 PathMake(fname, drive, dir, name, ext);
404 } while (more && (ROMFile = OPEN_STREAM (fname, "rb")) != NULL);
407 if (HeaderCount == 0)
408 S9xMessage (S9X_INFO, S9X_HEADERS_INFO, "No ROM file header found.");
411 if (HeaderCount == 1)
412 S9xMessage (S9X_INFO, S9X_HEADERS_INFO,
413 "Found ROM file header (and ignored it).");
415 S9xMessage (S9X_INFO, S9X_HEADERS_INFO,
416 "Found multiple ROM file headers (and ignored them).");
419 CheckForIPSPatch (filename, HeaderCount != 0, TotalFileSize);
420 int orig_hi_score, orig_lo_score;
421 int hi_score, lo_score;
423 orig_hi_score = hi_score = ScoreHiROM (FALSE);
424 orig_lo_score = lo_score = ScoreLoROM (FALSE);
426 if (HeaderCount == 0 && !Settings.ForceNoHeader &&
427 ((hi_score > lo_score && ScoreHiROM (TRUE) > hi_score) ||
428 (hi_score <= lo_score && ScoreLoROM (TRUE) > lo_score)))
430 memmove (Memory.ROM, Memory.ROM + 512, TotalFileSize - 512);
431 TotalFileSize -= 512;
432 S9xMessage (S9X_INFO, S9X_HEADER_WARNING,
433 "Try specifying the -nhd command line option if the game doesn't work\n");
436 CalculatedSize = (TotalFileSize / 0x2000) * 0x2000;
437 ZeroMemory (ROM + CalculatedSize, MAX_ROM_SIZE - CalculatedSize);
439 // Check for cherryroms.com DAIKAIJYUMONOGATARI2
441 if (CalculatedSize == 0x500000 &&
442 strncmp ((const char *)&ROM [0x40ffc0], "DAIKAIJYUMONOGATARI2", 20) == 0 &&
443 strncmp ((const char *)&ROM [0x40ffb0], "18AE6J", 6) == 0 &&
444 memcmp (&ROM[0x40ffb0], &ROM [0xffb0], 0x30))
446 memmove (&ROM[0x100000], ROM, 0x500000);
447 memmove (ROM, &ROM[0x500000], 0x100000);
450 Interleaved = Settings.ForceInterleaved || Settings.ForceInterleaved2;
451 if (Settings.ForceLoROM || (!Settings.ForceHiROM && lo_score >= hi_score))
456 // Ignore map type byte if not 0x2x or 0x3x
457 if ((ROM [0x7fd5] & 0xf0) == 0x20 || (ROM [0x7fd5] & 0xf0) == 0x30)
459 switch (ROM [0x7fd5] & 0xf)
462 if (strncmp ((char *) &ROM [0x7fc0], "TREASURE HUNTER G", 17) != 0)
467 if (!Settings.ForceLoROM &&
468 strncmp ((char *) &ROM [0x7fc0], "SUPER FORMATION SOCCE", 21) != 0 &&
469 strncmp ((char *) &ROM [0x7fc0], "Star Ocean", 10) != 0)
485 if ((ROM [0xffd5] & 0xf0) == 0x20 || (ROM [0xffd5] & 0xf0) == 0x30)
487 switch (ROM [0xffd5] & 0xf)
500 if (!Settings.ForceHiROM && !Settings.ForceLoROM &&
501 !Settings.ForceInterleaved && !Settings.ForceInterleaved2 &&
502 !Settings.ForceNotInterleaved && !Settings.ForcePAL &&
503 !Settings.ForceSuperFX && !Settings.ForceDSP1 &&
504 !Settings.ForceSA1 && !Settings.ForceC4 &&
507 if (strncmp ((char *) &ROM [0x7fc0], "YUYU NO QUIZ DE GO!GO!", 22) == 0)
514 if (strncmp ((char *) &ROM [0x7fc0], "SP MOMOTAROU DENTETSU2", 22) == 0)
521 if (CalculatedSize == 0x100000 &&
522 strncmp ((char *) &ROM [0xffc0], "WWF SUPER WRESTLEMANIA", 22) == 0)
526 memmove (&ROM[0x100000] , ROM, 0x100000);
527 for (cvcount = 0; cvcount < 16; cvcount++)
529 memmove (&ROM[0x8000 * cvcount], &ROM[0x10000 * cvcount + 0x100000 + 0x8000], 0x8000);
530 memmove (&ROM[0x8000 * cvcount + 0x80000], &ROM[0x10000 * cvcount + 0x100000], 0x8000);
534 ZeroMemory (ROM + CalculatedSize, MAX_ROM_SIZE - CalculatedSize);
538 if (!Settings.ForceNotInterleaved && Interleaved)
540 CPU.TriedInterleavedMode2 = TRUE;
541 S9xMessage (S9X_INFO, S9X_ROM_INTERLEAVED_INFO,
542 "ROM image is in interleaved format - converting...");
544 int nblocks = CalculatedSize >> 16;
548 while (nblocks <= step)
558 for (i = 0; i < 0x40; i += 2)
560 blocks [i + 0] = (i >> 1) + 0x20;
561 blocks [i + 1] = (i >> 1) + 0x00;
563 for (i = 0; i < 0x80; i += 2)
565 blocks [i + 0x40] = (i >> 1) + 0x80;
566 blocks [i + 0x41] = (i >> 1) + 0x40;
572 if (Settings.ForceInterleaved2)
574 for (i = 0; i < nblocks * 2; i++)
576 blocks [i] = (i & ~0x1e) | ((i & 2) << 2) | ((i & 4) << 2) |
577 ((i & 8) >> 2) | ((i & 16) >> 2);
587 for (i = 0; i < nblocks; i++)
589 blocks [i * 2] = i + nblocks;
590 blocks [i * 2 + 1] = i;
594 uint8 *tmp = (uint8 *) malloc (0x8000);
597 for (i = 0; i < nblocks * 2; i++)
599 for (int j = i; j < nblocks * 2; j++)
603 memmove (tmp, &ROM [blocks [j] * 0x8000], 0x8000);
604 memmove (&ROM [blocks [j] * 0x8000],
605 &ROM [blocks [i] * 0x8000], 0x8000);
606 memmove (&ROM [blocks [i] * 0x8000], tmp, 0x8000);
607 uint8 b = blocks [j];
608 blocks [j] = blocks [i];
617 hi_score = ScoreHiROM (FALSE);
618 lo_score = ScoreLoROM (FALSE);
621 (lo_score >= hi_score || hi_score < 0)) ||
623 (hi_score > lo_score || lo_score < 0)))
625 if (retry_count == 0)
627 S9xMessage (S9X_INFO, S9X_ROM_CONFUSING_FORMAT_INFO,
628 "ROM lied about its type! Trying again.");
629 Settings.ForceNotInterleaved = TRUE;
630 Settings.ForceInterleaved = FALSE;
639 S9xLoadCheatFile (S9xGetFilename(FILE_CHT));
648 void S9xDeinterleaveMode2 ()
650 S9xMessage (S9X_INFO, S9X_ROM_INTERLEAVED_INFO,
651 "ROM image is in interleaved format - converting...");
653 int nblocks = Memory.CalculatedSize >> 15;
656 while (nblocks <= step)
663 for (i = 0; i < nblocks * 2; i++)
665 blocks [i] = (i & ~0x1e) | ((i & 2) << 2) | ((i & 4) << 2) |
666 ((i & 8) >> 2) | ((i & 16) >> 2);
669 uint8 *tmp = (uint8 *) malloc (0x8000);
673 for (i = 0; i < nblocks * 2; i++)
675 for (int j = i; j < nblocks * 2; j++)
679 memmove (tmp, &Memory.ROM [blocks [j] * 0x8000], 0x8000);
680 memmove (&Memory.ROM [blocks [j] * 0x8000],
681 &Memory.ROM [blocks [i] * 0x8000], 0x8000);
682 memmove (&Memory.ROM [blocks [i] * 0x8000], tmp, 0x8000);
683 uint8 b = blocks [j];
684 blocks [j] = blocks [i];
692 Memory.InitROM (FALSE);
696 void CMemory::InitROM (bool8_32 Interleaved)
699 SuperFX.nRomBanks = CalculatedSize >> 15;
701 Settings.MultiPlayer5Master = Settings.MultiPlayer5;
702 Settings.MouseMaster = Settings.Mouse;
703 Settings.SuperScopeMaster = Settings.SuperScope;
704 Settings.DSP1Master = Settings.ForceDSP1;
705 Settings.SuperFX = FALSE;
706 Settings.SA1 = FALSE;
708 Settings.SDD1 = FALSE;
709 Settings.SRTC = FALSE;
711 ZeroMemory (BlockIsRAM, MEMMAP_NUM_BLOCKS);
712 ZeroMemory (BlockIsROM, MEMMAP_NUM_BLOCKS);
715 memset (ROMId, 0, 5);
716 memset (CompanyId, 0, 3);
720 Memory.SRAMSize = ROM [0xffd8];
721 strncpy (ROMName, (char *) &ROM[0xffc0], ROM_NAME_LEN - 1);
722 ROMSpeed = ROM [0xffd5];
723 ROMType = ROM [0xffd6];
724 ROMSize = ROM [0xffd7];
725 ROMChecksum = ROM [0xffde] + (ROM [0xffdf] << 8);
726 ROMComplementChecksum = ROM [0xffdc] + (ROM [0xffdd] << 8);
728 memmove (ROMId, &ROM [0xffb2], 4);
729 memmove (CompanyId, &ROM [0xffb0], 2);
731 // Try to auto-detect the DSP1 chip
732 if (!Settings.ForceNoDSP1 &&
733 (ROMType & 0xf) >= 3 && (ROMType & 0xf0) == 0)
734 Settings.DSP1Master = TRUE;
736 Settings.SDD1 = Settings.ForceSDD1;
737 if ((ROMType & 0xf0) == 0x40)
738 Settings.SDD1 = !Settings.ForceNoSDD1;
743 if ((ROMSpeed & ~0x10) == 0x25)
744 TalesROMMap (Interleaved);
746 if ((ROMSpeed & ~0x10) == 0x22 &&
747 strncmp (ROMName, "Super Street Fighter", 20) != 0)
756 Memory.HiROM = FALSE;
757 Memory.SRAMSize = ROM [0x7fd8];
758 ROMSpeed = ROM [0x7fd5];
759 ROMType = ROM [0x7fd6];
760 ROMSize = ROM [0x7fd7];
761 ROMChecksum = ROM [0x7fde] + (ROM [0x7fdf] << 8);
762 ROMComplementChecksum = ROM [0x7fdc] + (ROM [0x7fdd] << 8);
763 memmove (ROMId, &ROM [0x7fb2], 4);
764 memmove (CompanyId, &ROM [0x7fb0], 2);
766 strncpy (ROMName, (char *) &ROM[0x7fc0], ROM_NAME_LEN - 1);
767 Settings.SuperFX = Settings.ForceSuperFX;
769 if ((ROMType & 0xf0) == 0x10)
770 Settings.SuperFX = !Settings.ForceNoSuperFX;
772 // Try to auto-detect the DSP1 chip
773 if (!Settings.ForceNoDSP1 &&
774 (ROMType & 0xf) >= 3 && (ROMType & 0xf0) == 0)
775 Settings.DSP1Master = TRUE;
777 Settings.SDD1 = Settings.ForceSDD1;
778 if ((ROMType & 0xf0) == 0x40)
779 Settings.SDD1 = !Settings.ForceNoSDD1;
784 Settings.C4 = Settings.ForceC4;
785 if ((ROMType & 0xf0) == 0xf0 &&
786 (strncmp (ROMName, "MEGAMAN X", 9) == 0 ||
787 strncmp (ROMName, "ROCKMAN X", 9) == 0))
789 Settings.C4 = !Settings.ForceNoC4;
792 if (Settings.SuperFX)
794 //::SRAM = ROM + 1024 * 1024 * 4;
796 Settings.MultiPlayer5Master = FALSE;
797 //Settings.MouseMaster = FALSE;
798 //Settings.SuperScopeMaster = FALSE;
799 Settings.DSP1Master = FALSE;
800 Settings.SA1 = FALSE;
802 Settings.SDD1 = FALSE;
805 if (Settings.ForceSA1 ||
806 (!Settings.ForceNoSA1 && (ROMSpeed & ~0x10) == 0x23 &&
807 (ROMType & 0xf) > 3 && (ROMType & 0xf0) == 0x30))
810 Settings.MultiPlayer5Master = FALSE;
811 //Settings.MouseMaster = FALSE;
812 //Settings.SuperScopeMaster = FALSE;
813 Settings.DSP1Master = FALSE;
815 Settings.SDD1 = FALSE;
819 if ((ROMSpeed & ~0x10) == 0x25)
820 TalesROMMap (Interleaved);
822 if (strncmp ((char *) &Memory.ROM [0x7fc0], "SOUND NOVEL-TCOOL", 17) == 0 ||
823 strncmp ((char *) &Memory.ROM [0x7fc0], "DERBY STALLION 96", 17) == 0)
826 Settings.DSP1Master = FALSE;
829 if (strncmp ((char *) &Memory.ROM [0x7fc0], "THOROUGHBRED BREEDER3", 21) == 0 ||
830 strncmp ((char *) &Memory.ROM [0x7fc0], "RPG-TCOOL 2", 11) == 0)
833 Settings.DSP1Master = FALSE;
836 if (strncmp ((char *) &Memory.ROM [0x7fc0], "DEZAEMON ", 10) == 0)
838 Settings.DSP1Master = FALSE;
839 SRAM1024KLoROMMap ();
842 if (strncmp ((char *) &Memory.ROM [0x7fc0], "ADD-ON BASE CASSETE", 19) == 0)
844 Settings.MultiPlayer5Master = FALSE;
845 Settings.MouseMaster = FALSE;
846 Settings.SuperScopeMaster = FALSE;
847 Settings.DSP1Master = FALSE;
848 SufamiTurboLoROMMap();
852 if ((ROMSpeed & ~0x10) == 0x22 &&
853 strncmp (ROMName, "Super Street Fighter", 20) != 0)
862 int size = CalculatedSize;
868 uint32 remainder = CalculatedSize - size;
875 for (i = 0; i < size; i++)
878 for (i = 0; i < (int) remainder; i++)
879 sum2 += ROM [size + i];
884 if (CalculatedSize == 0x500000 && Memory.HiROM &&
885 strncmp ((const char *)&ROM[0xffb0], "18AZ", 4) == 0 &&
886 !memcmp(&ROM[0xffd5], "\x3a\xf9\x0d\x03\x00\x33\x00", 7))
889 sum1 += sum2 * (size / remainder);
894 if (Settings.ForceNTSC)
895 Settings.PAL = FALSE;
897 if (Settings.ForcePAL)
902 Settings.PAL = ROM [0xffd9] >= 2;
904 Settings.PAL = ROM [0x7fd9] >= 2;
908 Settings.FrameTime = Settings.FrameTimePAL;
909 Memory.ROMFramesPerSecond = 50;
913 Settings.FrameTime = Settings.FrameTimeNTSC;
914 Memory.ROMFramesPerSecond = 60;
917 ROMName[ROM_NAME_LEN - 1] = 0;
918 if (strlen (ROMName))
920 char *p = ROMName + strlen (ROMName) - 1;
922 while (p > ROMName && *(p - 1) == ' ')
927 if (Settings.SuperFX)
929 CPU.Memory_SRAMMask = 0xffff;
930 Memory.SRAMSize = 16;
934 CPU.Memory_SRAMMask = Memory.SRAMSize ?
935 ((1 << (Memory.SRAMSize + 3)) * 128) - 1 : 0;
938 IAPU.OneCycle = ONE_APU_CYCLE;
939 Settings.Shutdown = Settings.ShutdownMaster;
941 SetDSP = &DSP1SetByte;
942 GetDSP = &DSP1GetByte;
945 sprintf (ROMName, "%s", Safe (ROMName));
946 sprintf (ROMId, "%s", Safe (ROMId));
947 sprintf (CompanyId, "%s", Safe (CompanyId));
949 sprintf (String, "\"%s\" [%s] %s, %s, Type: %s, Mode: %s, TV: %s, S-RAM: %s, ROMId: %s Company: %2.2s",
951 (ROMChecksum + ROMComplementChecksum != 0xffff ||
952 ROMChecksum != sum1) ? "bad checksum" : "checksum ok",
962 S9xMessage (S9X_INFO, S9X_ROM_INFO, String);
965 bool8_32 CMemory::LoadSRAM (const char *filename)
967 int size = Memory.SRAMSize ?
968 (1 << (Memory.SRAMSize + 3)) * 128 : 0;
970 memset (SRAM, SNESGameFixes.SRAMInitialValue, 0x20000);
978 if ((file = fopen(filename, "rb")))
980 int len = fread ((char*) ::SRAM, 1, 0x20000, file);
982 if (len - size == 512)
984 // S-RAM file has a header - remove it
985 memmove (::SRAM, ::SRAM + 512, size);
987 if (len == size + SRTC_SRAM_PAD)
989 S9xSRTCPostLoadState ();
992 rtc.mode = MODE_READ;
1003 S9xSDD1LoadLoggedData ();
1008 bool8_32 CMemory::SaveSRAM (const char *filename)
1010 int size = Memory.SRAMSize ?
1011 (1 << (Memory.SRAMSize + 3)) * 128 : 0;
1014 size += SRTC_SRAM_PAD;
1015 S9xSRTCPreSaveState ();
1019 S9xSDD1SaveLoggedData ();
1024 if (size && *Memory.ROMFilename)
1027 if ((file = fopen (filename, "wb")))
1029 fwrite ((char *) ::SRAM, size, 1, file);
1031 #if defined(__linux)
1032 chown (filename, getuid (), getgid ());
1040 void CMemory::FixROMSpeed ()
1044 for (c = 0x800; c < 0x1000; c++)
1047 MemorySpeed [c] = (uint8) CPU.FastROMSpeed;
1051 void CMemory::WriteProtectROM ()
1053 memmove ((void *) WriteMap, (void *) Map, sizeof (Map));
1054 for (int c = 0; c < 0x1000; c++)
1057 WriteMap [c] = (uint8 *) MAP_NONE;
1061 void CMemory::MapRAM ()
1065 // Banks 7e->7f, RAM
1066 for (c = 0; c < 16; c++)
1068 Map [c + 0x7e0] = RAM;
1069 Map [c + 0x7f0] = RAM + 0x10000;
1070 BlockIsRAM [c + 0x7e0] = TRUE;
1071 BlockIsRAM [c + 0x7f0] = TRUE;
1072 BlockIsROM [c + 0x7e0] = FALSE;
1073 BlockIsROM [c + 0x7f0] = FALSE;
1076 // Banks 70->77, S-RAM
1077 for (c = 0; c < 0x80; c++)
1079 Map [c + 0x700] = (uint8 *) MAP_LOROM_SRAM;
1080 BlockIsRAM [c + 0x700] = TRUE;
1081 BlockIsROM [c + 0x700] = FALSE;
1085 void CMemory::MapExtraRAM ()
1089 // Banks 7e->7f, RAM
1090 for (c = 0; c < 16; c++)
1092 Map [c + 0x7e0] = RAM;
1093 Map [c + 0x7f0] = RAM + 0x10000;
1094 BlockIsRAM [c + 0x7e0] = TRUE;
1095 BlockIsRAM [c + 0x7f0] = TRUE;
1096 BlockIsROM [c + 0x7e0] = FALSE;
1097 BlockIsROM [c + 0x7f0] = FALSE;
1100 // Banks 70->73, S-RAM
1101 for (c = 0; c < 16; c++)
1103 Map [c + 0x700] = ::SRAM;
1104 Map [c + 0x710] = ::SRAM + 0x8000;
1105 Map [c + 0x720] = ::SRAM + 0x10000;
1106 Map [c + 0x730] = ::SRAM + 0x18000;
1108 BlockIsRAM [c + 0x700] = TRUE;
1109 BlockIsROM [c + 0x700] = FALSE;
1110 BlockIsRAM [c + 0x710] = TRUE;
1111 BlockIsROM [c + 0x710] = FALSE;
1112 BlockIsRAM [c + 0x720] = TRUE;
1113 BlockIsROM [c + 0x720] = FALSE;
1114 BlockIsRAM [c + 0x730] = TRUE;
1115 BlockIsROM [c + 0x730] = FALSE;
1119 void CMemory::LoROMMap ()
1124 // Banks 00->3f and 80->bf
1125 for (c = 0; c < 0x400; c += 16)
1127 Map [c + 0] = Map [c + 0x800] = RAM;
1128 Map [c + 1] = Map [c + 0x801] = RAM;
1129 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1130 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1132 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1133 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1134 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1135 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1136 if (Settings.DSP1Master)
1138 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1139 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1144 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_C4;
1145 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_C4;
1149 Map [c + 6] = Map [c + 0x806] = (uint8 *) bytes0x2000 - 0x6000;
1150 Map [c + 7] = Map [c + 0x807] = (uint8 *) bytes0x2000 - 0x6000;
1153 for (i = c + 8; i < c + 16; i++)
1155 Map [i] = Map [i + 0x800] = &ROM [(c << 11) % CalculatedSize] - 0x8000;
1156 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1159 for (i = c; i < c + 16; i++)
1164 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1168 if (Settings.DSP1Master)
1170 // Banks 30->3f and b0->bf
1171 for (c = 0x300; c < 0x400; c += 16)
1173 for (i = c + 8; i < c + 16; i++)
1175 Map [i] = Map [i + 0x800] = (uint8 *) MAP_DSP;
1176 BlockIsROM [i] = BlockIsROM [i + 0x800] = FALSE;
1181 // Banks 40->7f and c0->ff
1182 for (c = 0; c < 0x400; c += 16)
1184 for (i = c; i < c + 8; i++)
1185 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) % CalculatedSize];
1187 for (i = c + 8; i < c + 16; i++)
1188 Map [i + 0x400] = Map [i + 0xc00] = &ROM [((c << 11) + 0x200000) % CalculatedSize - 0x8000];
1190 for (i = c; i < c + 16; i++)
1192 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1193 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1197 if (Settings.DSP1Master)
1199 for (c = 0; c < 0x100; c++)
1201 Map [c + 0xe00] = (uint8 *) MAP_DSP;
1202 MemorySpeed [c + 0xe00] = SLOW_ONE_CYCLE;
1203 BlockIsROM [c + 0xe00] = FALSE;
1210 void CMemory::HiROMMap ()
1215 // Banks 00->3f and 80->bf
1216 for (c = 0; c < 0x400; c += 16)
1218 Map [c + 0] = Map [c + 0x800] = RAM;
1219 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1220 Map [c + 1] = Map [c + 0x801] = RAM;
1221 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1223 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1224 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1225 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1226 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1227 if (Settings.DSP1Master)
1229 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1230 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1234 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1235 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1238 for (i = c + 8; i < c + 16; i++)
1240 Map [i] = Map [i + 0x800] = &ROM [(c << 12) % CalculatedSize];
1241 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1244 for (i = c; i < c + 16; i++)
1249 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1253 // Banks 30->3f and b0->bf, address ranges 6000->7fff is S-RAM.
1254 for (c = 0; c < 16; c++)
1256 Map [0x306 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1257 Map [0x307 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1258 Map [0xb06 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1259 Map [0xb07 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1260 BlockIsRAM [0x306 + (c << 4)] = TRUE;
1261 BlockIsRAM [0x307 + (c << 4)] = TRUE;
1262 BlockIsRAM [0xb06 + (c << 4)] = TRUE;
1263 BlockIsRAM [0xb07 + (c << 4)] = TRUE;
1266 // Banks 40->7f and c0->ff
1267 for (c = 0; c < 0x400; c += 16)
1269 for (i = c; i < c + 16; i++)
1271 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1272 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1273 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1281 void CMemory::TalesROMMap (bool8_32 Interleaved)
1286 uint32 OFFSET0 = 0x400000;
1287 uint32 OFFSET1 = 0x400000;
1288 uint32 OFFSET2 = 0x000000;
1297 // Banks 00->3f and 80->bf
1298 for (c = 0; c < 0x400; c += 16)
1300 Map [c + 0] = Map [c + 0x800] = RAM;
1301 Map [c + 1] = Map [c + 0x801] = RAM;
1302 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1303 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1305 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1306 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1307 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1308 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1309 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1310 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1311 for (i = c + 8; i < c + 16; i++)
1313 Map [i] = &ROM [((c << 12) + OFFSET0) % CalculatedSize];
1314 Map [i + 0x800] = &ROM [((c << 12) + OFFSET0) % CalculatedSize];
1315 BlockIsROM [i] = TRUE;
1316 BlockIsROM [i + 0x800] = TRUE;
1319 for (i = c; i < c + 16; i++)
1324 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1328 // Banks 30->3f and b0->bf, address ranges 6000->7ffff is S-RAM.
1329 for (c = 0; c < 16; c++)
1331 Map [0x306 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1332 Map [0x307 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1333 Map [0xb06 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1334 Map [0xb07 + (c << 4)] = (uint8 *) MAP_HIROM_SRAM;
1335 BlockIsRAM [0x306 + (c << 4)] = TRUE;
1336 BlockIsRAM [0x307 + (c << 4)] = TRUE;
1337 BlockIsRAM [0xb06 + (c << 4)] = TRUE;
1338 BlockIsRAM [0xb07 + (c << 4)] = TRUE;
1341 // Banks 40->7f and c0->ff
1342 for (c = 0; c < 0x400; c += 16)
1344 for (i = c; i < c + 8; i++)
1346 Map [i + 0x400] = &ROM [((c << 12) + OFFSET1) % CalculatedSize];
1347 Map [i + 0x408] = &ROM [((c << 12) + OFFSET1) % CalculatedSize];
1348 Map [i + 0xc00] = &ROM [((c << 12) + OFFSET2) % CalculatedSize];
1349 Map [i + 0xc08] = &ROM [((c << 12) + OFFSET2) % CalculatedSize];
1350 BlockIsROM [i + 0x400] = TRUE;
1351 BlockIsROM [i + 0x408] = TRUE;
1352 BlockIsROM [i + 0xc00] = TRUE;
1353 BlockIsROM [i + 0xc08] = TRUE;
1354 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1355 MemorySpeed [i + 0x408] = MemorySpeed [i + 0xc08] = SLOW_ONE_CYCLE;
1362 void CMemory::AlphaROMMap ()
1367 // Banks 00->3f and 80->bf
1368 for (c = 0; c < 0x400; c += 16)
1370 Map [c + 0] = Map [c + 0x800] = RAM;
1371 Map [c + 1] = Map [c + 0x801] = RAM;
1372 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1373 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1375 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1376 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1377 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1378 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1379 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1380 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1382 for (i = c + 8; i < c + 16; i++)
1384 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1385 BlockIsROM [i] = TRUE;
1388 for (i = c; i < c + 16; i++)
1393 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1397 // Banks 40->7f and c0->ff
1399 for (c = 0; c < 0x400; c += 16)
1401 for (i = c; i < c + 16; i++)
1403 Map [i + 0x400] = &ROM [(c << 12) % CalculatedSize];
1404 Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1405 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1406 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1414 void CMemory::SuperFXROMMap ()
1419 // Banks 00->3f and 80->bf
1420 for (c = 0; c < 0x400; c += 16)
1422 Map [c + 0] = Map [c + 0x800] = RAM;
1423 Map [c + 1] = Map [c + 0x801] = RAM;
1424 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1425 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1427 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1428 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1429 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1430 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1431 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1432 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1433 for (i = c + 8; i < c + 16; i++)
1435 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1436 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1439 for (i = c; i < c + 8; i++)
1444 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1448 // Banks 40->7f and c0->ff
1449 for (c = 0; c < 0x400; c += 16)
1451 for (i = c; i < c + 16; i++)
1453 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1454 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1455 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1459 // Banks 7e->7f, RAM
1460 for (c = 0; c < 16; c++)
1462 Map [c + 0x7e0] = RAM;
1463 Map [c + 0x7f0] = RAM + 0x10000;
1464 BlockIsRAM [c + 0x7e0] = TRUE;
1465 BlockIsRAM [c + 0x7f0] = TRUE;
1466 BlockIsROM [c + 0x7e0] = FALSE;
1467 BlockIsROM [c + 0x7f0] = FALSE;
1470 // Banks 70->71, S-RAM
1471 for (c = 0; c < 32; c++)
1473 Map [c + 0x700] = ::SRAM + (((c >> 4) & 1) << 16);
1474 BlockIsRAM [c + 0x700] = TRUE;
1475 BlockIsROM [c + 0x700] = FALSE;
1478 // Banks 00->3f and 80->bf address ranges 6000->7fff is RAM.
1479 for (c = 0; c < 0x40; c++)
1481 Map [0x006 + (c << 4)] = (uint8 *) ::SRAM - 0x6000;
1482 Map [0x007 + (c << 4)] = (uint8 *) ::SRAM - 0x6000;
1483 Map [0x806 + (c << 4)] = (uint8 *) ::SRAM - 0x6000;
1484 Map [0x807 + (c << 4)] = (uint8 *) ::SRAM - 0x6000;
1485 BlockIsRAM [0x006 + (c << 4)] = TRUE;
1486 BlockIsRAM [0x007 + (c << 4)] = TRUE;
1487 BlockIsRAM [0x806 + (c << 4)] = TRUE;
1488 BlockIsRAM [0x807 + (c << 4)] = TRUE;
1490 // Replicate the first 2Mb of the ROM at ROM + 2MB such that each 32K
1491 // block is repeated twice in each 64K block.
1492 for (c = 0; c < 64; c++)
1494 memmove (&ROM [0x200000 + c * 0x10000], &ROM [c * 0x8000], 0x8000);
1495 memmove (&ROM [0x208000 + c * 0x10000], &ROM [c * 0x8000], 0x8000);
1501 void CMemory::SA1ROMMap ()
1506 // Banks 00->3f and 80->bf
1507 for (c = 0; c < 0x400; c += 16)
1509 Map [c + 0] = Map [c + 0x800] = RAM;
1510 Map [c + 1] = Map [c + 0x801] = RAM;
1511 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1512 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1514 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1515 Map [c + 3] = Map [c + 0x803] = (uint8 *) &Memory.FillRAM [0x3000] - 0x3000;
1516 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1517 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1518 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_BWRAM;
1519 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_BWRAM;
1520 for (i = c + 8; i < c + 16; i++)
1522 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1523 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1526 for (i = c; i < c + 16; i++)
1531 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1536 for (c = 0; c < 0x400; c += 16)
1538 for (i = c; i < c + 16; i++)
1539 Map [i + 0x400] = (uint8 *) &SRAM [(c << 12) & 0x1ffff];
1541 for (i = c; i < c + 16; i++)
1543 MemorySpeed [i + 0x400] = SLOW_ONE_CYCLE;
1544 BlockIsROM [i + 0x400] = FALSE;
1549 for (c = 0; c < 0x400; c += 16)
1551 for (i = c; i < c + 16; i++)
1553 Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1554 MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1555 BlockIsROM [i + 0xc00] = TRUE;
1559 for (c = 0; c < 16; c++)
1561 Map [c + 0x7e0] = RAM;
1562 Map [c + 0x7f0] = RAM + 0x10000;
1563 BlockIsRAM [c + 0x7e0] = TRUE;
1564 BlockIsRAM [c + 0x7f0] = TRUE;
1565 BlockIsROM [c + 0x7e0] = FALSE;
1566 BlockIsROM [c + 0x7f0] = FALSE;
1570 // Now copy the map and correct it for the SA1 CPU.
1571 memmove ((void *) SA1.WriteMap, (void *) WriteMap, sizeof (WriteMap));
1572 memmove ((void *) SA1.Map, (void *) Map, sizeof (Map));
1574 // Banks 00->3f and 80->bf
1575 for (c = 0; c < 0x400; c += 16)
1577 SA1.Map [c + 0] = SA1.Map [c + 0x800] = &Memory.FillRAM [0x3000];
1578 SA1.Map [c + 1] = SA1.Map [c + 0x801] = (uint8 *) MAP_NONE;
1579 SA1.WriteMap [c + 0] = SA1.WriteMap [c + 0x800] = &Memory.FillRAM [0x3000];
1580 SA1.WriteMap [c + 1] = SA1.WriteMap [c + 0x801] = (uint8 *) MAP_NONE;
1584 for (c = 0; c < 0x100; c++)
1585 SA1.Map [c + 0x600] = SA1.WriteMap [c + 0x600] = (uint8 *) MAP_BWRAM_BITMAP;
1590 void CMemory::LoROM24MBSMap ()
1595 // Banks 00->3f and 80->bf
1596 for (c = 0; c < 0x400; c += 16)
1598 Map [c + 0] = Map [c + 0x800] = RAM;
1599 Map [c + 1] = Map [c + 0x801] = RAM;
1600 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1601 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1603 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1604 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1605 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1606 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1607 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1608 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1610 for (i = c + 8; i < c + 16; i++)
1612 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1613 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1616 for (i = c; i < c + 16; i++)
1621 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1625 // Banks 00->3f and 80->bf
1626 for (c = 0; c < 0x200; c += 16)
1628 Map [c + 0x800] = RAM;
1629 Map [c + 0x801] = RAM;
1630 BlockIsRAM [c + 0x800] = TRUE;
1631 BlockIsRAM [c + 0x801] = TRUE;
1633 Map [c + 0x802] = (uint8 *) MAP_PPU;
1634 Map [c + 0x803] = (uint8 *) MAP_PPU;
1635 Map [c + 0x804] = (uint8 *) MAP_CPU;
1636 Map [c + 0x805] = (uint8 *) MAP_CPU;
1637 Map [c + 0x806] = (uint8 *) MAP_NONE;
1638 Map [c + 0x807] = (uint8 *) MAP_NONE;
1640 for (i = c + 8; i < c + 16; i++)
1642 Map [i + 0x800] = &ROM [c << 11] - 0x8000 + 0x200000;
1643 BlockIsROM [i + 0x800] = TRUE;
1646 for (i = c; i < c + 16; i++)
1650 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1654 // Banks 40->7f and c0->ff
1655 for (c = 0; c < 0x400; c += 16)
1657 for (i = c; i < c + 8; i++)
1658 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000];
1660 for (i = c + 8; i < c + 16; i++)
1661 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000 - 0x8000];
1663 for (i = c; i < c + 16; i++)
1665 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1666 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1674 void CMemory::SufamiTurboLoROMMap ()
1679 // Banks 00->3f and 80->bf
1680 for (c = 0; c < 0x400; c += 16)
1682 Map [c + 0] = Map [c + 0x800] = RAM;
1683 Map [c + 1] = Map [c + 0x801] = RAM;
1684 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1685 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1687 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1688 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1689 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1690 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1691 if (Settings.DSP1Master)
1693 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_DSP;
1694 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_DSP;
1698 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1699 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1701 for (i = c + 8; i < c + 16; i++)
1703 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1704 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1707 for (i = c; i < c + 16; i++)
1712 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1716 if (Settings.DSP1Master)
1718 // Banks 30->3f and b0->bf
1719 for (c = 0x300; c < 0x400; c += 16)
1721 for (i = c + 8; i < c + 16; i++)
1723 Map [i] = Map [i + 0x800] = (uint8 *) MAP_DSP;
1724 BlockIsROM [i] = BlockIsROM [i + 0x800] = FALSE;
1729 // Banks 40->7f and c0->ff
1730 for (c = 0; c < 0x400; c += 16)
1732 for (i = c; i < c + 8; i++)
1733 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000];
1735 for (i = c + 8; i < c + 16; i++)
1736 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000 - 0x8000];
1738 for (i = c; i < c + 16; i++)
1740 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1741 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1745 if (Settings.DSP1Master)
1747 for (c = 0; c < 0x100; c++)
1749 Map [c + 0xe00] = (uint8 *) MAP_DSP;
1750 MemorySpeed [c + 0xe00] = SLOW_ONE_CYCLE;
1751 BlockIsROM [c + 0xe00] = FALSE;
1755 // Banks 7e->7f, RAM
1756 for (c = 0; c < 16; c++)
1758 Map [c + 0x7e0] = RAM;
1759 Map [c + 0x7f0] = RAM + 0x10000;
1760 BlockIsRAM [c + 0x7e0] = TRUE;
1761 BlockIsRAM [c + 0x7f0] = TRUE;
1762 BlockIsROM [c + 0x7e0] = FALSE;
1763 BlockIsROM [c + 0x7f0] = FALSE;
1766 // Banks 60->67, S-RAM
1767 for (c = 0; c < 0x80; c++)
1769 Map [c + 0x600] = (uint8 *) MAP_LOROM_SRAM;
1770 BlockIsRAM [c + 0x600] = TRUE;
1771 BlockIsROM [c + 0x600] = FALSE;
1777 void CMemory::SRAM512KLoROMMap ()
1782 // Banks 00->3f and 80->bf
1783 for (c = 0; c < 0x400; c += 16)
1785 Map [c + 0] = Map [c + 0x800] = RAM;
1786 Map [c + 1] = Map [c + 0x801] = RAM;
1787 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1788 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1790 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1791 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1792 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1793 Map [c + 5] = Map [c + 0x805] = (uint8 *) MAP_CPU;
1794 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1795 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1797 for (i = c + 8; i < c + 16; i++)
1799 Map [i] = Map [i + 0x800] = &ROM [c << 11] - 0x8000;
1800 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1803 for (i = c; i < c + 16; i++)
1808 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1812 // Banks 40->7f and c0->ff
1813 for (c = 0; c < 0x400; c += 16)
1815 for (i = c; i < c + 8; i++)
1816 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000];
1818 for (i = c + 8; i < c + 16; i++)
1819 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 11) + 0x200000 - 0x8000];
1821 for (i = c; i < c + 16; i++)
1823 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1824 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1832 void CMemory::SRAM1024KLoROMMap ()
1837 // Banks 00->3f and 80->bf
1838 for (c = 0; c < 0x400; c += 16)
1840 Map [c + 0] = Map [c + 0x800] = Map [c + 0x400] = Map [c + 0xc00] = RAM;
1841 Map [c + 1] = Map [c + 0x801] = Map [c + 0x401] = Map [c + 0xc01] = RAM;
1842 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = BlockIsRAM [c + 0x400] = BlockIsRAM [c + 0xc00] = TRUE;
1843 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = BlockIsRAM [c + 0x401] = BlockIsRAM [c + 0xc01] = TRUE;
1845 Map [c + 2] = Map [c + 0x802] = Map [c + 0x402] = Map [c + 0xc02] = (uint8 *) MAP_PPU;
1846 Map [c + 3] = Map [c + 0x803] = Map [c + 0x403] = Map [c + 0xc03] = (uint8 *) MAP_PPU;
1847 Map [c + 4] = Map [c + 0x804] = Map [c + 0x404] = Map [c + 0xc04] = (uint8 *) MAP_CPU;
1848 Map [c + 5] = Map [c + 0x805] = Map [c + 0x405] = Map [c + 0xc05] = (uint8 *) MAP_CPU;
1849 Map [c + 6] = Map [c + 0x806] = Map [c + 0x406] = Map [c + 0xc06] = (uint8 *) MAP_NONE;
1850 Map [c + 7] = Map [c + 0x807] = Map [c + 0x407] = Map [c + 0xc07] = (uint8 *) MAP_NONE;
1851 for (i = c + 8; i < c + 16; i++)
1853 Map [i] = Map [i + 0x800] = Map [i + 0x400] = Map [i + 0xc00] = &ROM [c << 11] - 0x8000;
1854 BlockIsROM [i] = BlockIsROM [i + 0x800] = BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1857 for (i = c; i < c + 16; i++)
1861 MemorySpeed [i] = MemorySpeed [i + 0x800] =
1862 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1870 void CMemory::BSHiROMMap ()
1875 // Banks 00->3f and 80->bf
1876 for (c = 0; c < 0x400; c += 16)
1878 Map [c + 0] = Map [c + 0x800] = RAM;
1879 BlockIsRAM [c + 0] = BlockIsRAM [c + 0x800] = TRUE;
1880 Map [c + 1] = Map [c + 0x801] = RAM;
1881 BlockIsRAM [c + 1] = BlockIsRAM [c + 0x801] = TRUE;
1883 Map [c + 2] = Map [c + 0x802] = (uint8 *) MAP_PPU;
1884 Map [c + 3] = Map [c + 0x803] = (uint8 *) MAP_PPU;
1885 Map [c + 4] = Map [c + 0x804] = (uint8 *) MAP_CPU;
1886 // XXX: How large is SRAM??
1887 Map [c + 5] = Map [c + 0x805] = (uint8 *) SRAM;
1888 BlockIsRAM [c + 5] = BlockIsRAM [c + 0x805] = TRUE;
1889 Map [c + 6] = Map [c + 0x806] = (uint8 *) MAP_NONE;
1890 Map [c + 7] = Map [c + 0x807] = (uint8 *) MAP_NONE;
1892 for (i = c + 8; i < c + 16; i++)
1894 Map [i] = Map [i + 0x800] = &ROM [(c << 12) % CalculatedSize];
1895 BlockIsROM [i] = BlockIsROM [i + 0x800] = TRUE;
1898 for (i = c; i < c + 16; i++)
1903 MemorySpeed [i + 0x800] = ppu >= 2 && ppu <= 3 ? ONE_CYCLE : SLOW_ONE_CYCLE;
1907 // Banks 60->7d offset 0000->7fff & 60->7f offset 8000->ffff PSRAM
1908 // XXX: How large is PSRAM?
1909 for (c = 0x600; c < 0x7e0; c += 16)
1911 for (i = c; i < c + 8; i++)
1913 Map [i] = &ROM [0x400000 + (c << 11)];
1914 BlockIsRAM [i] = TRUE;
1916 for (i = c + 8; i < c + 16; i++)
1918 Map [i] = &ROM [0x400000 + (c << 11) - 0x8000];
1919 BlockIsRAM [i] = TRUE;
1923 // Banks 40->7f and c0->ff
1924 for (c = 0; c < 0x400; c += 16)
1926 for (i = c; i < c + 16; i++)
1928 Map [i + 0x400] = Map [i + 0xc00] = &ROM [(c << 12) % CalculatedSize];
1929 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = SLOW_ONE_CYCLE;
1930 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = TRUE;
1938 const char *CMemory::TVStandard ()
1940 return (Settings.PAL ? "PAL" : "NTSC");
1943 const char *CMemory::Speed ()
1945 return (ROMSpeed & 0x10 ? "120ns" : "200ns");
1948 const char *CMemory::MapType ()
1950 return (HiROM ? "HiROM" : "LoROM");
1953 const char *CMemory::StaticRAMSize ()
1955 static char tmp [20];
1957 if (Memory.SRAMSize > 16)
1959 sprintf (tmp, "%dKb", (CPU.Memory_SRAMMask + 1) / 1024);
1963 const char *CMemory::Size ()
1965 static char tmp [20];
1967 if (ROMSize < 7 || ROMSize - 7 > 23)
1969 sprintf (tmp, "%dMbits", 1 << (ROMSize - 7));
1973 const char *CMemory::KartContents ()
1975 static char tmp [30];
1976 static const char *CoPro [16] = {
1977 "DSP1", "SuperFX", "OBC1", "SA-1", "S-DD1", "S-RTC", "CoPro#6",
1978 "CoPro#7", "CoPro#8", "CoPro#9", "CoPro#10", "CoPro#11", "CoPro#12",
1979 "CoPro#13", "CoPro#14", "CoPro-Custom"
1981 static const char *Contents [3] = {
1982 "ROM", "ROM+RAM", "ROM+RAM+BAT"
1985 return ("ROM only");
1987 sprintf (tmp, "%s", Contents [(ROMType & 0xf) % 3]);
1989 if ((ROMType & 0xf) >= 3)
1990 sprintf (tmp, "%s+%s", tmp, CoPro [(ROMType & 0xf0) >> 4]);
1995 const char *CMemory::MapMode ()
1997 static char tmp [4];
1998 sprintf (tmp, "%02x", ROMSpeed & ~0x10);
2002 const char *CMemory::ROMID ()
2007 void CMemory::ApplyROMFixes ()
2010 if (strncmp(ROMName, "DUNGEON MASTER", 14) == 0)
2013 SetDSP=&DSP2SetByte;
2014 GetDSP=&DSP2GetByte;
2017 // Enable S-RTC (Real Time Clock) emulation for Dai Kaijyu Monogatari 2
2018 Settings.SRTC = ((ROMType & 0xf0) >> 4) == 5;
2020 Settings.StrikeGunnerOffsetHack = strcmp (ROMName, "STRIKE GUNNER") == 0 ? 7 : 0;
2022 CPU.NMITriggerPoint = 4;
2023 if (strcmp (ROMName, "CACOMA KNIGHT") == 0)
2024 CPU.NMITriggerPoint = 25;
2026 // These games complain if the multi-player adaptor is 'connected'
2027 if (strcmp (ROMName, "TETRIS&Dr.MARIO") == 0 ||
2028 strcmp (ROMName, "JIGSAW PARTY") == 0 ||
2029 strcmp (ROMName, "SUPER PICROSS") == 0 ||
2030 strcmp (ROMName, "KIRBY NO KIRA KIZZU") == 0 ||
2031 strcmp (ROMName, "BLOCK") == 0 ||
2032 strncmp (ROMName, "SUPER BOMBLISS", 14) == 0 ||
2033 strcmp (ROMId, "ABOJ") == 0)
2035 Settings.MultiPlayer5Master = FALSE;
2036 Settings.MouseMaster = FALSE;
2037 Settings.SuperScopeMaster = FALSE;
2040 // Games which spool sound samples between the SNES and sound CPU using
2041 // H-DMA as the sample is playing.
2042 if (strcmp (ROMName, "EARTHWORM JIM 2") == 0 ||
2043 strcmp (ROMName, "PRIMAL RAGE") == 0 ||
2044 strcmp (ROMName, "CLAY FIGHTER") == 0 ||
2045 strcmp (ROMName, "ClayFighter 2") == 0 ||
2046 strncasecmp (ROMName, "MADDEN", 6) == 0 ||
2047 strncmp (ROMName, "NHL", 3) == 0 ||
2048 strcmp (ROMName, "WeaponLord") == 0)
2050 Settings.Shutdown = FALSE;
2055 if (strcmp (ROMId, "CQ ") == 0 ||
2057 strncmp (ROMId, "JG", 2) == 0 ||
2058 strcmp (ROMName, "GAIA GENSOUKI 1 JPN") == 0)
2061 Settings.APUEnabled |= 2;
2062 CPU.APU_APUExecuting |= 2;
2065 // RENDERING RANGER R2
2066 if (strcmp (ROMId, "AVCJ") == 0 ||
2068 strncmp (ROMId, "ARF", 3) == 0 ||
2069 // Tales of Phantasia
2070 strncmp (ROMId, "ATV", 3) == 0 ||
2072 strncasecmp (ROMName, "ActRaiser", 9) == 0 ||
2074 strcmp (ROMName, "SOULBLAZER - 1 USA") == 0 ||
2075 strcmp (ROMName, "SOULBLADER - 1") == 0 ||
2076 strncmp (ROMName, "SOULBLAZER 1",12) == 0 ||
2078 strncmp (ROMId, "AQT", 3) == 0 ||
2080 strncmp (ROMId, "E9 ", 3) == 0 ||
2081 strcmp (ROMName, "SLAP STICK 1 JPN") == 0 ||
2082 // ZENNIHON PURORESU2
2083 strncmp (ROMId, "APR", 3) == 0 ||
2085 strncmp (ROMId, "A4B", 3) == 0 ||
2086 // UFO KAMEN YAKISOBAN
2087 strncmp (ROMId, "Y7 ", 3) == 0 ||
2088 strncmp (ROMId, "Y9 ", 3) == 0 ||
2089 // Panic Bomber World
2090 strncmp (ROMId, "APB", 3) == 0 ||
2091 ((strncmp (ROMName, "Parlor", 6) == 0 ||
2092 strcmp (ROMName, "HEIWA Parlor!Mini8") == 0 ||
2093 strncmp (ROMName, "SANKYO Fever! ̨°ÊÞ°!", 21) == 0) &&
2094 strcmp (CompanyId, "A0") == 0) ||
2095 strcmp (ROMName, "DARK KINGDOM") == 0 ||
2096 strcmp (ROMName, "ZAN3 SFC") == 0 ||
2097 strcmp (ROMName, "HIOUDEN") == 0 ||
2098 strcmp (ROMName, "ÃݼɳÀ") == 0 ||
2099 strcmp (ROMName, "FORTUNE QUEST") == 0 ||
2100 strcmp (ROMName, "FISHING TO BASSING") == 0 ||
2101 strncmp (ROMName, "TokyoDome '95Battle 7", 21) == 0 ||
2102 strcmp (ROMName, "OHMONO BLACKBASS") == 0)
2105 // notaz: strangely enough, these games work properly with my hack enabled
2106 Settings.APUEnabled |= 2;
2107 CPU.APU_APUExecuting |= 2;
2110 if (strcmp (ROMName, "BATMAN--REVENGE JOKER") == 0)
2112 Memory.HiROM = FALSE;
2113 Memory.LoROM = TRUE;
2116 Settings.StarfoxHack = strcmp (ROMName, "STAR FOX") == 0 ||
2117 strcmp (ROMName, "STAR WING") == 0;
2118 Settings.WinterGold = strcmp (ROMName, "FX SKIING NINTENDO 96") == 0 ||
2119 strcmp (ROMName, "DIRT RACER") == 0 ||
2120 strcmp (ROMName, "Stunt Race FX") == 0 ||
2121 Settings.StarfoxHack;
2122 Settings.ChuckRock = strcmp (ROMName, "CHUCK ROCK") == 0;
2123 Settings.Dezaemon = strcmp (ROMName, "DEZAEMON") == 0;
2125 if (strcmp (ROMName, "RADICAL DREAMERS") == 0 ||
2126 strcmp (ROMName, "TREASURE CONFLIX") == 0)
2130 for (c = 0; c < 0x80; c++)
2132 Map [c + 0x700] = ROM + 0x200000 + 0x1000 * (c & 0xf0);
2133 BlockIsRAM [c + 0x700] = TRUE;
2134 BlockIsROM [c + 0x700] = FALSE;
2136 for (c = 0; c < 0x400; c += 16)
2138 Map [c + 5] = Map [c + 0x805] = ROM + 0x300000;
2139 BlockIsRAM [c + 5] = BlockIsRAM [c + 0x805] = TRUE;
2144 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE *
2145 Settings.CyclesPercentage) / 100;
2147 if (strcmp (ROMId, "ASRJ") == 0 && Settings.CyclesPercentage == 100)
2149 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 95) / 100;
2151 // Power Rangers Fight
2152 if (strncmp (ROMId, "A3R", 3) == 0 ||
2154 strncmp (ROMId, "AJE", 3) == 0)
2155 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 103) / 100;
2157 if (strcmp (ROMId, "AWVP") == 0 || strcmp (ROMId, "AWVE") == 0 ||
2158 strcmp (ROMId, "AWVJ") == 0)
2160 // Wrestlemania Arcade
2162 if (Settings.CyclesPercentage == 100)
2163 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 140) / 100; // Fixes sound
2165 Settings.WrestlemaniaArcade = TRUE;
2167 // Theme Park - disable offset-per-tile mode.
2168 if (strcmp (ROMId, "ATQP") == 0)
2169 Settings.WrestlemaniaArcade = TRUE;
2171 if (strncmp (ROMId, "A3M", 3) == 0 && Settings.CyclesPercentage == 100)
2172 // Mortal Kombat 3. Fixes cut off speech sample
2173 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 110) / 100;
2175 if (strcmp (ROMName, "\x0bd\x0da\x0b2\x0d4\x0b0\x0bd\x0de") == 0 &&
2176 Settings.CyclesPercentage == 100)
2177 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 101) / 100;
2179 if (strcmp (ROMName, "WILD TRAX") == 0 ||
2180 strcmp (ROMName, "YOSSY'S ISLAND") == 0 ||
2181 strcmp (ROMName, "YOSHI'S ISLAND") == 0)
2182 CPU.TriedInterleavedMode2 = TRUE;
2184 // Start Trek: Deep Sleep 9
2185 if (strncmp (ROMId, "A9D", 3) == 0 && Settings.CyclesPercentage == 100)
2186 Settings.H_Max = (SNES_CYCLES_PER_SCANLINE * 110) / 100;
2188 Settings.APURAMInitialValue = 0xff;
2190 if (strcmp (ROMName, "·³Ô¸¥Ò¶ÞÐÃݾ²") == 0 ||
2191 strcmp (ROMName, "KENTOUOU WORLDCHAMPIO") == 0 ||
2192 strcmp (ROMName, "TKO SUPERCHAMPIONSHIP") == 0 ||
2193 strcmp (ROMName, "TKO SUPER CHAMPIONSHI") == 0 ||
2194 strcmp (ROMName, "IHATOVO STORY") == 0 ||
2195 strcmp (ROMName, "WANDERERS FROM YS") == 0 ||
2196 strcmp (ROMName, "SUPER GENTYOUHISHI") == 0 ||
2197 // Panic Bomber World
2198 strncmp (ROMId, "APB", 3) == 0)
2200 Settings.APURAMInitialValue = 0;
2203 Settings.DaffyDuck = strcmp (ROMName, "DAFFY DUCK: MARV MISS") == 0;
2204 Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
2206 SA1.WaitAddress = NULL;
2207 SA1.WaitByteAddress1 = NULL;
2208 SA1.WaitByteAddress2 = NULL;
2211 if (strcmp (ROMId, "ZBPJ") == 0)
2213 SA1.WaitAddress = SA1.Map [0x0093f1 >> MEMMAP_SHIFT] + 0x93f1;
2214 SA1.WaitByteAddress1 = FillRAM + 0x304a;
2216 /* DAISENRYAKU EXPERTWW2 */
2217 if (strcmp (ROMId, "AEVJ") == 0)
2219 SA1.WaitAddress = SA1.Map [0x0ed18d >> MEMMAP_SHIFT] + 0xd18d;
2220 SA1.WaitByteAddress1 = FillRAM + 0x3000;
2223 if (strcmp (ROMId, "A2DJ") == 0)
2225 SA1.WaitAddress = SA1.Map [0x008b62 >> MEMMAP_SHIFT] + 0x8b62;
2227 /* Dragon Ballz HD */
2228 if (strcmp (ROMId, "AZIJ") == 0)
2230 SA1.WaitAddress = SA1.Map [0x008083 >> MEMMAP_SHIFT] + 0x8083;
2231 SA1.WaitByteAddress1 = FillRAM + 0x3020;
2233 /* SFC SDGUNDAMGNEXT */
2234 if (strcmp (ROMId, "ZX3J") == 0)
2236 SA1.WaitAddress = SA1.Map [0x0087f2 >> MEMMAP_SHIFT] + 0x87f2;
2237 SA1.WaitByteAddress1 = FillRAM + 0x30c4;
2239 /* ShougiNoHanamichi */
2240 if (strcmp (ROMId, "AARJ") == 0)
2242 SA1.WaitAddress = SA1.Map [0xc1f85a >> MEMMAP_SHIFT] + 0xf85a;
2243 SA1.WaitByteAddress1 = SRAM + 0x0c64;
2244 SA1.WaitByteAddress2 = SRAM + 0x0c66;
2246 /* KATO HIFUMI9DAN SYOGI */
2247 if (strcmp (ROMId, "A23J") == 0)
2249 SA1.WaitAddress = SA1.Map [0xc25037 >> MEMMAP_SHIFT] + 0x5037;
2250 SA1.WaitByteAddress1 = SRAM + 0x0c06;
2251 SA1.WaitByteAddress2 = SRAM + 0x0c08;
2254 if (strcmp (ROMId, "AIIJ") == 0)
2256 SA1.WaitAddress = SA1.Map [0xc100be >> MEMMAP_SHIFT] + 0x00be;
2257 SA1.WaitByteAddress1 = SRAM + 0x1002;
2258 SA1.WaitByteAddress2 = SRAM + 0x1004;
2261 if (strcmp (ROMId, "AITJ") == 0)
2263 SA1.WaitAddress = SA1.Map [0x0080b7 >> MEMMAP_SHIFT] + 0x80b7;
2265 /* J96 DREAM STADIUM */
2266 if (strcmp (ROMId, "AJ6J") == 0)
2268 SA1.WaitAddress = SA1.Map [0xc0f74a >> MEMMAP_SHIFT] + 0xf74a;
2271 if (strcmp (ROMId, "AJUJ") == 0)
2273 SA1.WaitAddress = SA1.Map [0x00d926 >> MEMMAP_SHIFT] + 0xd926;
2275 /* JKAKINOKI SHOUGI */
2276 if (strcmp (ROMId, "AKAJ") == 0)
2278 SA1.WaitAddress = SA1.Map [0x00f070 >> MEMMAP_SHIFT] + 0xf070;
2280 /* HOSHI NO KIRBY 3 & KIRBY'S DREAM LAND 3 JAP & US */
2281 if (strcmp (ROMId, "AFJJ") == 0 || strcmp (ROMId, "AFJE") == 0)
2283 SA1.WaitAddress = SA1.Map [0x0082d4 >> MEMMAP_SHIFT] + 0x82d4;
2284 SA1.WaitByteAddress1 = SRAM + 0x72a4;
2286 /* KIRBY SUPER DELUXE JAP */
2287 if (strcmp (ROMId, "AKFJ") == 0)
2289 SA1.WaitAddress = SA1.Map [0x008c93 >> MEMMAP_SHIFT] + 0x8c93;
2290 SA1.WaitByteAddress1 = FillRAM + 0x300a;
2291 SA1.WaitByteAddress2 = FillRAM + 0x300e;
2293 /* KIRBY SUPER DELUXE US */
2294 if (strcmp (ROMId, "AKFE") == 0)
2296 SA1.WaitAddress = SA1.Map [0x008cb8 >> MEMMAP_SHIFT] + 0x8cb8;
2297 SA1.WaitByteAddress1 = FillRAM + 0x300a;
2298 SA1.WaitByteAddress2 = FillRAM + 0x300e;
2300 /* SUPER MARIO RPG JAP & US */
2301 if (strcmp (ROMId, "ARWJ") == 0 || strcmp (ROMId, "ARWE") == 0)
2303 SA1.WaitAddress = SA1.Map [0xc0816f >> MEMMAP_SHIFT] + 0x816f;
2304 SA1.WaitByteAddress1 = FillRAM + 0x3000;
2307 if (strcmp (ROMId, "AVRJ") == 0)
2309 SA1.WaitAddress = SA1.Map [0x0085f2 >> MEMMAP_SHIFT] + 0x85f2;
2310 SA1.WaitByteAddress1 = FillRAM + 0x3024;
2312 /* AUGUSTA3 MASTERS NEW */
2313 if (strcmp (ROMId, "AO3J") == 0)
2315 SA1.WaitAddress = SA1.Map [0x00dddb >> MEMMAP_SHIFT] + 0xdddb;
2316 SA1.WaitByteAddress1 = FillRAM + 0x37b4;
2318 /* OSHABERI PARODIUS */
2319 if (strcmp (ROMId, "AJOJ") == 0)
2321 SA1.WaitAddress = SA1.Map [0x8084e5 >> MEMMAP_SHIFT] + 0x84e5;
2323 /* PANIC BOMBER WORLD */
2324 if (strcmp (ROMId, "APBJ") == 0)
2326 SA1.WaitAddress = SA1.Map [0x00857a >> MEMMAP_SHIFT] + 0x857a;
2328 /* PEBBLE BEACH NEW */
2329 if (strcmp (ROMId, "AONJ") == 0)
2331 SA1.WaitAddress = SA1.Map [0x00df33 >> MEMMAP_SHIFT] + 0xdf33;
2332 SA1.WaitByteAddress1 = FillRAM + 0x37b4;
2334 /* PGA EUROPEAN TOUR */
2335 if (strcmp (ROMId, "AEPE") == 0)
2337 SA1.WaitAddress = SA1.Map [0x003700 >> MEMMAP_SHIFT] + 0x3700;
2338 SA1.WaitByteAddress1 = FillRAM + 0x3102;
2341 if (strcmp (ROMId, "A3GE") == 0)
2343 SA1.WaitAddress = SA1.Map [0x003700 >> MEMMAP_SHIFT] + 0x3700;
2344 SA1.WaitByteAddress1 = FillRAM + 0x3102;
2346 /* POWER RANGERS 4 */
2347 if (strcmp (ROMId, "A4RE") == 0)
2349 SA1.WaitAddress = SA1.Map [0x009899 >> MEMMAP_SHIFT] + 0x9899;
2350 SA1.WaitByteAddress1 = FillRAM + 0x3000;
2352 /* PACHISURO PALUSUPE */
2353 if (strcmp (ROMId, "AGFJ") == 0)
2355 // Never seems to turn on the SA-1!
2357 /* SD F1 GRAND PRIX */
2358 if (strcmp (ROMId, "AGFJ") == 0)
2360 SA1.WaitAddress = SA1.Map [0x0181bc >> MEMMAP_SHIFT] + 0x81bc;
2362 /* SHOUGI MARJONG */
2363 if (strcmp (ROMId, "ASYJ") == 0)
2365 SA1.WaitAddress = SA1.Map [0x00f2cc >> MEMMAP_SHIFT] + 0xf2cc;
2366 SA1.WaitByteAddress1 = SRAM + 0x7ffe;
2367 SA1.WaitByteAddress2 = SRAM + 0x7ffc;
2370 if (strcmp (ROMId, "AX2J") == 0)
2372 SA1.WaitAddress = SA1.Map [0x00d675 >> MEMMAP_SHIFT] + 0xd675;
2375 /* SHINING SCORPION */
2376 if (strcmp (ROMId, "A4WJ") == 0)
2378 SA1.WaitAddress = SA1.Map [0xc048be >> MEMMAP_SHIFT] + 0x48be;
2380 /* SHIN SHOUGI CLUB */
2381 if (strcmp (ROMId, "AHJJ") == 0)
2383 SA1.WaitAddress = SA1.Map [0xc1002a >> MEMMAP_SHIFT] + 0x002a;
2384 SA1.WaitByteAddress1 = SRAM + 0x0806;
2385 SA1.WaitByteAddress2 = SRAM + 0x0808;
2388 // Additional game fixes by sanmaiwashi ...
2389 if (strcmp (ROMName, "SFX ŲĶÞÝÀÞÑÓɶÞÀØ 1") == 0)
2391 bytes0x2000 [0xb18] = 0x4c;
2392 bytes0x2000 [0xb19] = 0x4b;
2393 bytes0x2000 [0xb1a] = 0xea;
2396 if (strcmp (ROMName, "GOGO ACKMAN3") == 0 ||
2397 strcmp (ROMName, "HOME ALONE") == 0)
2399 // Banks 00->3f and 80->bf
2400 for (int c = 0; c < 0x400; c += 16)
2402 Map [c + 6] = Map [c + 0x806] = SRAM;
2403 Map [c + 7] = Map [c + 0x807] = SRAM;
2404 BlockIsROM [c + 6] = BlockIsROM [c + 0x806] = FALSE;
2405 BlockIsROM [c + 7] = BlockIsROM [c + 0x807] = FALSE;
2406 BlockIsRAM [c + 6] = BlockIsRAM [c + 0x806] = TRUE;
2407 BlockIsRAM [c + 7] = BlockIsRAM [c + 0x807] = TRUE;
2412 if (strncmp (ROMName, "SWORD WORLD SFC", 15) == 0 ||
2413 strcmp (ROMName, "SFC ¶ÒÝײÀÞ°") == 0)
2416 SNESGameFixes.NeedInit0x2137 = TRUE;
2417 Settings.APUEnabled |= 2;
2418 CPU.APU_APUExecuting |= 2;
2421 if (strncmp (ROMName, "SHIEN THE BLADE CHASE", 21) == 0)
2422 SNESGameFixes.Old_Read0x4200 = TRUE;
2424 if (strcmp (ROMName, "ºÞ¼Þ× ¶²¼Þ³ÀÞ²¹¯¾Ý") == 0)
2425 SNESGameFixes.NeedInit0x2137 = TRUE;
2427 if (strcmp (ROMName, "UMIHARAKAWASE") == 0)
2428 SNESGameFixes.umiharakawaseFix = TRUE;
2430 if (strcmp (ROMName, "ALIENS vs. PREDATOR") == 0)
2431 SNESGameFixes.alienVSpredetorFix = TRUE;
2433 if (strcmp (ROMName, "demon's blazon") == 0 ||
2434 strcmp (ROMName, "demon's crest") == 0 ||
2435 strcmp (ROMName, "ROCKMAN X") == 0 ||
2436 strcmp (ROMName, "MEGAMAN X") == 0)
2440 // Banks 0x808000, 0x408000 are mirroring.
2441 for (int c = 0; c < 8; c++)
2442 Map [0x408 + c] = ROM - 0x8000;
2445 if (strcmp (ROMName, "½°Ȩ̂߰нÀ") == 0 ||
2446 strcmp (ROMName, "½°Ȩ̂߰нÀ 2") == 0 ||
2447 strcmp (ROMName, "ZENKI TENCHIMEIDOU") == 0 ||
2448 strcmp (ROMName, "GANBA LEAGUE") == 0)
2450 SNESGameFixes.APU_OutPorts_ReturnValueFix = TRUE;
2454 if (strcmp (ROMName, "HITOMI3") == 0)
2456 Memory.SRAMSize = 1;
2457 CPU.Memory_SRAMMask = Memory.SRAMSize ?
2458 ((1 << (Memory.SRAMSize + 3)) * 128) - 1 : 0;
2461 if (strcmp (ROMName, "goemon 4") == 0)
2462 SNESGameFixes.SRAMInitialValue = 0x00;
2464 if (strcmp (ROMName, "PACHISLO ¹Ý·³") == 0)
2465 SNESGameFixes._0x213E_ReturnValue = 1;
2467 if (strcmp (ROMName, "»Þ Ï°¼Þ¬Ý ijʲÃÞÝ") == 0)
2468 SNESGameFixes.TouhaidenControllerFix = TRUE;
2470 if (strcmp (ROMName, "DRAGON KNIGHT 4") == 0)
2472 // Banks 70->7e, S-RAM
2473 for (int c = 0; c < 0xe0; c++)
2475 Map [c + 0x700] = (uint8 *) MAP_LOROM_SRAM;
2476 BlockIsRAM [c + 0x700] = TRUE;
2477 BlockIsROM [c + 0x700] = FALSE;
2482 if (strncmp (ROMName, "LETs PACHINKO(", 14) == 0)
2485 Settings.APUEnabled |= 2;
2486 CPU.APU_APUExecuting |= 2;
2487 if (!Settings.ForceNTSC && !Settings.ForcePAL)
2489 Settings.PAL = FALSE;
2490 Settings.FrameTime = Settings.FrameTimeNTSC;
2491 Memory.ROMFramesPerSecond = 60;
2495 if (strcmp (ROMName, "FURAI NO SIREN") == 0)
2496 SNESGameFixes.SoundEnvelopeHeightReading2 = TRUE;
2498 if(strcmp (ROMName, "XBAND JAPANESE MODEM") == 0)
2500 for (c = 0x200; c < 0x400; c += 16)
2502 for (int i = c; i < c + 16; i++)
2504 Map [i + 0x400] = Map [i + 0xc00] = &ROM[c * 0x1000];
2505 MemorySpeed [i + 0x400] = MemorySpeed [i + 0xc00] = 8;
2506 BlockIsRAM [i + 0x400] = BlockIsRAM [i + 0xc00] = TRUE;
2507 BlockIsROM [i + 0x400] = BlockIsROM [i + 0xc00] = FALSE;
2514 #define RomPatch(adr,ov,nv) \
2515 if (ROM [adr] == ov) \
2519 if (strcmp (ROMName, "LOVE QUEST") == 0)
2521 RomPatch (0x1385ec, 0xd0, 0xea);
2522 RomPatch (0x1385ed, 0xb2, 0xea);
2525 // Nangoku Syonen Papuwa Kun
2526 if (strcmp (ROMName, "NANGOKUSYONEN PAPUWA") == 0)
2527 RomPatch (0x1f0d1, 0xa0, 0x6b);
2530 if (strcmp (ROMName, "Tetsuwan Atom") == 0)
2532 RomPatch (0xe24c5, 0x90, 0xea);
2533 RomPatch (0xe24c6, 0xf3, 0xea);
2537 if (strcmp (ROMName, "SFC ODA NOBUNAGA") == 0)
2539 RomPatch (0x7497, 0x80, 0xea);
2540 RomPatch (0x7498, 0xd5, 0xea);
2544 if (strcmp (ROMName, "Super Batter Up") == 0)
2546 RomPatch (0x27ae0, 0xd0, 0xea);
2547 RomPatch (0x27ae1, 0xfa, 0xea);
2550 // Super Professional Baseball 2
2551 if (strcmp (ROMName, "SUPER PRO. BASE BALL2") == 0)
2553 RomPatch (0x1e4, 0x50, 0xea);
2554 RomPatch (0x1e5, 0xfb, 0xea);
2558 // Read variable size MSB int from a file
2559 static long ReadInt (FILE *f, unsigned nbytes)
2567 v = (v << 8) | (c & 0xFF);
2572 #define IPS_EOF 0x00454F46l
2574 void CMemory::CheckForIPSPatch (const char *rom_filename, bool8_32 header,
2577 char fname [_MAX_PATH + 1];
2578 FILE *patch_file = NULL;
2579 long offset = header ? 512 : 0;
2581 if (!(patch_file = fopen(S9xGetFilename(FILE_IPS), "rb"))) return;
2583 if (fread (fname, 1, 5, patch_file) != 5 || strncmp (fname, "PATCH", 5) != 0)
2585 fclose (patch_file);
2597 ofs = ReadInt (patch_file, 3);
2606 len = ReadInt (patch_file, 2);
2610 /* Apply patch block */
2613 if (ofs + len > MAX_ROM_SIZE)
2618 rchar = fgetc (patch_file);
2621 ROM [ofs++] = (uint8) rchar;
2628 rlen = ReadInt (patch_file, 2);
2632 rchar = fgetc (patch_file);
2636 if (ofs + rlen > MAX_ROM_SIZE)
2640 ROM [ofs++] = (uint8) rchar;
2647 // Check if ROM image needs to be truncated
2648 ofs = ReadInt (patch_file, 3);
2649 if (ofs != -1 && ofs - offset < rom_size)
2651 // Need to truncate ROM image
2652 rom_size = ofs - offset;
2654 fclose (patch_file);
2659 fclose (patch_file);