From f8da1524414209e4e090c3ec45aee59999c9aaf3 Mon Sep 17 00:00:00 2001 From: qwertymodo Date: Wed, 16 Aug 2017 18:56:57 -0700 Subject: [PATCH 1/7] Add support for .msu1 pack files --- memmap.cpp | 22 ++++++++++++++++++++++ msu1.cpp | 52 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/memmap.cpp b/memmap.cpp index 4fc88dd7..01777ac7 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -4257,6 +4257,28 @@ void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &r } #ifdef UNZIP_SUPPORT + // Mercurial Magic (MSU-1 distribution pack) + _makepath(fname, drive, dir, name, "msu1"); + unzFile msu1file = unzOpen(fname); + + if (msu1file) + { + int port = unzFindExtension(msu1file, "bps"); + if (port == UNZ_OK) + { + printf(" in %s", fname); + + Stream *s = new unzStream(msu1file); + ret = ReadBPSPatch(s, offset, rom_size); + s->closeStream(); + + if (ret) + printf("!\n"); + else + printf(" failed!\n"); + } + } + if (!strcasecmp(ext, "zip") || !strcasecmp(ext, ".zip")) { unzFile file = unzOpen(rom_filename); diff --git a/msu1.cpp b/msu1.cpp index 1c253244..a13e45bb 100644 --- a/msu1.cpp +++ b/msu1.cpp @@ -224,7 +224,7 @@ static int unzFindExtension(unzFile &file, const char *ext, bool restart = TRUE, unzGetCurrentFileInfo(file, &info, name, 128, NULL, 0, NULL, 0); len = strlen(name); - if (len >= l + 1 && strcasecmp(name + len - l, ext) == 0 && unzOpenCurrentFile(file) == UNZ_OK) + if (len >= l && strcasecmp(name + len - l, ext) == 0 && unzOpenCurrentFile(file) == UNZ_OK) { if (print) printf("Using msu file %s", name); @@ -247,24 +247,28 @@ STREAM S9xMSU1OpenFile(char *msu_ext) printf("Using msu file %s.\n", filename); #ifdef UNZIP_SUPPORT - // look for msu file in .msu.zip if not found in rom dir + // look for msu file in .msu1 (Mercurial Magic pack) or .msu.zip if not found in rom dir if (!file) { const char *zip_filename = S9xGetFilename(".msu.zip", ROMFILENAME_DIR); - if (zip_filename) + unzFile unzFile = unzOpen(zip_filename); + + if (!unzFile) + { + zip_filename = S9xGetFilename(".msu1", ROMFILENAME_DIR); + unzFile = unzOpen(zip_filename); + } + + if (unzFile) { - unzFile unzFile = unzOpen(zip_filename); - if (unzFile) + int port = unzFindExtension(unzFile, msu_ext); + if (port == UNZ_OK) { - int port = unzFindExtension(unzFile, msu_ext); - if (port == UNZ_OK) - { - printf(" in %s.\n", zip_filename); - file = new unzStream(unzFile); - } - else - unzCloseCurrentFile(unzFile); + printf(" in %s.\n", zip_filename); + file = new unzStream(unzFile); } + else + unzCloseCurrentFile(unzFile); } } #endif @@ -321,6 +325,10 @@ bool DataOpen() } dataStream = S9xMSU1OpenFile(".msu"); + + if(!dataStream) + dataStream = S9xMSU1OpenFile("msu1.rom"); + return dataStream != NULL; } @@ -368,10 +376,20 @@ bool S9xMSU1ROMExists(void) { struct stat buf; STREAM s = S9xMSU1OpenFile(".msu"); - bool8 exists = (s != NULL); - if(s) - CLOSE_STREAM(s); - return exists; + if (s) + { + CLOSE_STREAM(s); + return true; + } + + s = S9xMSU1OpenFile("msu1.rom"); + if (s) + { + CLOSE_STREAM(s); + return true; + } + + return false; } void S9xMSU1Generate(size_t sample_count) From ab2da6d9136ce9da559bcff47e47ef71363af11c Mon Sep 17 00:00:00 2001 From: qwertymodo Date: Thu, 17 Aug 2017 10:34:13 -0700 Subject: [PATCH 2/7] Add IPS_DIR to the search path for .msu1 pack files --- memmap.cpp | 7 +++++++ msu1.cpp | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/memmap.cpp b/memmap.cpp index 01777ac7..259c416c 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -4261,6 +4261,13 @@ void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &r _makepath(fname, drive, dir, name, "msu1"); unzFile msu1file = unzOpen(fname); + if (!msu1file) + { + _snprintf(fname, sizeof(fname), "%s" SLASH_STR "%s%s", + S9xGetDirectory(IPS_DIR), name, ".msu1"); + msu1file = unzOpen(fname); + } + if (msu1file) { int port = unzFindExtension(msu1file, "bps"); diff --git a/msu1.cpp b/msu1.cpp index a13e45bb..1f042110 100644 --- a/msu1.cpp +++ b/msu1.cpp @@ -259,6 +259,12 @@ STREAM S9xMSU1OpenFile(char *msu_ext) unzFile = unzOpen(zip_filename); } + if (!unzFile) + { + zip_filename = S9xGetFilename(".msu1", IPS_DIR); + unzFile = unzOpen(zip_filename); + } + if (unzFile) { int port = unzFindExtension(unzFile, msu_ext); From 96fd5c41210be38fc2db9281917ac510645f8f9d Mon Sep 17 00:00:00 2001 From: qwertymodo Date: Thu, 17 Aug 2017 10:37:28 -0700 Subject: [PATCH 3/7] Allow unzFindExtension to optionally match the exact filename --- memmap.cpp | 8 ++++---- msu1.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/memmap.cpp b/memmap.cpp index 259c416c..230ea285 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -967,7 +967,7 @@ static bool8 ReadUPSPatch (Stream *, long, int32 &); static long ReadInt (Stream *, unsigned); static bool8 ReadIPSPatch (Stream *, long, int32 &); #ifdef UNZIP_SUPPORT -static int unzFindExtension (unzFile &, const char *, bool restart = TRUE, bool print = TRUE); +static int unzFindExtension (unzFile &, const char *, bool restart = TRUE, bool print = TRUE, bool allowExact = FALSE); #endif // deinterleave @@ -4188,10 +4188,10 @@ static bool8 ReadIPSPatch (Stream *r, long offset, int32 &rom_size) } #ifdef UNZIP_SUPPORT -static int unzFindExtension (unzFile &file, const char *ext, bool restart, bool print) +static int unzFindExtension (unzFile &file, const char *ext, bool restart, bool print, bool allowExact) { unz_file_info info; - int port, l = strlen(ext); + int port, l = strlen(ext), e = allowExact ? 0 : 1; if (restart) port = unzGoToFirstFile(file); @@ -4206,7 +4206,7 @@ static int unzFindExtension (unzFile &file, const char *ext, bool restart, bool unzGetCurrentFileInfo(file, &info, name, 128, NULL, 0, NULL, 0); len = strlen(name); - if (len >= l + 1 && name[len - l - 1] == '.' && strcasecmp(name + len - l, ext) == 0 && unzOpenCurrentFile(file) == UNZ_OK) + if (len >= l + e && name[len - l - 1] == '.' && strcasecmp(name + len - l, ext) == 0 && unzOpenCurrentFile(file) == UNZ_OK) { if (print) printf("Using patch %s", name); diff --git a/msu1.cpp b/msu1.cpp index 1f042110..51e15dd3 100644 --- a/msu1.cpp +++ b/msu1.cpp @@ -206,10 +206,10 @@ size_t partial_samples; int16 *bufPos, *bufBegin, *bufEnd; #ifdef UNZIP_SUPPORT -static int unzFindExtension(unzFile &file, const char *ext, bool restart = TRUE, bool print = TRUE) +static int unzFindExtension(unzFile &file, const char *ext, bool restart = TRUE, bool print = TRUE, bool allowExact = FALSE) { unz_file_info info; - int port, l = strlen(ext); + int port, l = strlen(ext), e = allowExact ? 0 : 1; if (restart) port = unzGoToFirstFile(file); @@ -224,7 +224,7 @@ static int unzFindExtension(unzFile &file, const char *ext, bool restart = TRUE, unzGetCurrentFileInfo(file, &info, name, 128, NULL, 0, NULL, 0); len = strlen(name); - if (len >= l && strcasecmp(name + len - l, ext) == 0 && unzOpenCurrentFile(file) == UNZ_OK) + if (len >= l + e && strcasecmp(name + len - l, ext) == 0 && unzOpenCurrentFile(file) == UNZ_OK) { if (print) printf("Using msu file %s", name); @@ -267,7 +267,7 @@ STREAM S9xMSU1OpenFile(char *msu_ext) if (unzFile) { - int port = unzFindExtension(unzFile, msu_ext); + int port = unzFindExtension(unzFile, msu_ext, true, true, true); if (port == UNZ_OK) { printf(" in %s.\n", zip_filename); From 9e804b4515520f7251866a07ade9c544d4ebb2d1 Mon Sep 17 00:00:00 2001 From: qwertymodo Date: Thu, 17 Aug 2017 11:30:44 -0700 Subject: [PATCH 4/7] Add support for loading .msu1 packs as ROM files, if they actually contain a ROM file --- loadzip.cpp | 13 +++++++++++-- memmap.cpp | 45 ++++++++++++++++++++++++--------------------- win32/wsnes9x.cpp | 11 +++++++++-- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/loadzip.cpp b/loadzip.cpp index d3168939..840da42e 100644 --- a/loadzip.cpp +++ b/loadzip.cpp @@ -209,7 +209,7 @@ bool8 LoadZip (const char *zipname, uint32 *TotalFileSize, uint8 *buffer) if (file == NULL) return (FALSE); - // find largest file in zip file (under MAX_ROM_SIZE) or a file with extension .1 + // find largest file in zip file (under MAX_ROM_SIZE) or a file with extension .1, or a file named program.rom char filename[132]; uint32 filesize = 0; int port = unzGoToFirstFile(file); @@ -241,10 +241,19 @@ bool8 LoadZip (const char *zipname, uint32 *TotalFileSize, uint8 *buffer) break; } + if (strncasecmp(name, "program.rom", 11) == 0) + { + strcpy(filename, name); + filesize = info.uncompressed_size; + break; + } + port = unzGoToNextFile(file); } - if (!(port == UNZ_END_OF_LIST_OF_FILE || port == UNZ_OK) || filesize == 0) + int len = strlen(zipname); + if (!(port == UNZ_END_OF_LIST_OF_FILE || port == UNZ_OK) || filesize == 0 || + (len > 5 && strcasecmp(zipname + len - 5, ".msu1") == 0 && strcasecmp(filename, "program.rom") != 0)) { assert(unzClose(file) == UNZ_OK); return (FALSE); diff --git a/memmap.cpp b/memmap.cpp index 230ea285..6e2534ee 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -1439,7 +1439,7 @@ uint32 CMemory::FileLoader (uint8 *buffer, const char *filename, uint32 maxsize) _makepath(fname, drive, dir, name, exts); int nFormat = FILE_DEFAULT; - if (strcasecmp(ext, "zip") == 0) + if (strcasecmp(ext, "zip") == 0 || strcasecmp(ext, "msu1") == 0) nFormat = FILE_ZIP; else if (strcasecmp(ext, "jma") == 0) @@ -4258,31 +4258,34 @@ void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &r #ifdef UNZIP_SUPPORT // Mercurial Magic (MSU-1 distribution pack) - _makepath(fname, drive, dir, name, "msu1"); - unzFile msu1file = unzOpen(fname); - - if (!msu1file) + if (strcasecmp(ext, "msu1") && strcasecmp(ext, ".msu1")) { - _snprintf(fname, sizeof(fname), "%s" SLASH_STR "%s%s", - S9xGetDirectory(IPS_DIR), name, ".msu1"); - msu1file = unzOpen(fname); - } + _makepath(fname, drive, dir, name, "msu1"); + unzFile msu1file = unzOpen(fname); - if (msu1file) - { - int port = unzFindExtension(msu1file, "bps"); - if (port == UNZ_OK) + if (!msu1file) { - printf(" in %s", fname); + _snprintf(fname, sizeof(fname), "%s" SLASH_STR "%s%s", + S9xGetDirectory(IPS_DIR), name, ".msu1"); + msu1file = unzOpen(fname); + } - Stream *s = new unzStream(msu1file); - ret = ReadBPSPatch(s, offset, rom_size); - s->closeStream(); + if (msu1file) + { + int port = unzFindExtension(msu1file, "bps"); + if (port == UNZ_OK) + { + printf(" in %s", fname); - if (ret) - printf("!\n"); - else - printf(" failed!\n"); + Stream *s = new unzStream(msu1file); + ret = ReadBPSPatch(s, offset, rom_size); + s->closeStream(); + + if (ret) + printf("!\n"); + else + printf(" failed!\n"); + } } } diff --git a/win32/wsnes9x.cpp b/win32/wsnes9x.cpp index 2758f4c1..015a1498 100644 --- a/win32/wsnes9x.cpp +++ b/win32/wsnes9x.cpp @@ -7020,10 +7020,17 @@ void MakeExtFile(void) ofstream out; out.open("Valid.Ext"); - out<<"smcN"< Date: Thu, 17 Aug 2017 14:37:59 -0700 Subject: [PATCH 5/7] Adjust MSU-1 data ROM detection inside .msu1 packs --- msu1.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/msu1.cpp b/msu1.cpp index 51e15dd3..802d9697 100644 --- a/msu1.cpp +++ b/msu1.cpp @@ -191,6 +191,7 @@ ***********************************************************************************/ #include "snes9x.h" +#include "memmap.h" #include "display.h" #include "msu1.h" #include "apu/bapu/dsp/blargg_endian.h" @@ -380,21 +381,29 @@ void S9xMSU1Init(void) bool S9xMSU1ROMExists(void) { - struct stat buf; STREAM s = S9xMSU1OpenFile(".msu"); if (s) { CLOSE_STREAM(s); return true; } +#ifdef UNZIP_SUPPORT + char ext[_MAX_EXT + 1]; + _splitpath(Memory.ROMFilename, nullptr, nullptr, nullptr, ext); + if (!strcasecmp(ext, ".msu1")) + return true; - s = S9xMSU1OpenFile("msu1.rom"); - if (s) + unzFile unzFile = unzOpen(S9xGetFilename(".msu1", ROMFILENAME_DIR)); + + if(!unzFile) + unzFile = unzOpen(S9xGetFilename(".msu1", IPS_DIR)); + + if (unzFile) { - CLOSE_STREAM(s); + unzCloseCurrentFile(unzFile); return true; } - +#endif return false; } From b230e50ec51719f872ce6821d94b003d6dbca383 Mon Sep 17 00:00:00 2001 From: qwertymodo Date: Wed, 30 Aug 2017 17:59:39 -0700 Subject: [PATCH 6/7] Fix MSU-1 audio memory leak --- apu/apu.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/apu/apu.cpp b/apu/apu.cpp index 8cbacca0..707ba6e3 100644 --- a/apu/apu.cpp +++ b/apu/apu.cpp @@ -245,6 +245,8 @@ namespace msu static int buffer_size; static uint8 *landing_buffer = NULL; static Resampler *resampler = NULL; + static int resample_buffer_size = -1; + static uint8 *resample_buffer = NULL; } static void EightBitize (uint8 *, int); @@ -314,6 +316,13 @@ bool8 S9xMixSamples (uint8 *buffer, int sample_count) else dest = buffer; + if (Settings.MSU1 && msu::resample_buffer_size < (sample_count << 1)) + { + delete[] msu::resample_buffer; + msu::resample_buffer = new uint8[sample_count << 1]; + msu::resample_buffer_size = sample_count << 1; + } + if (Settings.Mute) { memset(dest, 0, sample_count << 1); @@ -336,10 +345,9 @@ bool8 S9xMixSamples (uint8 *buffer, int sample_count) { if (msu::resampler->avail() >= sample_count) { - uint8 *msu_sample = new uint8[sample_count * 2]; - msu::resampler->read((short *)msu_sample, sample_count); + msu::resampler->read((short *)msu::resample_buffer, sample_count); for (uint32 i = 0; i < sample_count; ++i) - *((int16*)(dest+(i * 2))) += *((int16*)(msu_sample+(i * 2))); + *((int16*)(dest+(i * 2))) += *((int16*)(msu::resample_buffer +(i * 2))); } } } @@ -603,6 +611,12 @@ void S9xDeinitAPU (void) delete[] msu::landing_buffer; msu::landing_buffer = NULL; } + + if (msu::resample_buffer) + { + delete[] msu::resample_buffer; + msu::resample_buffer = NULL; + } } static inline int S9xAPUGetClock (int32 cpucycles) From 698bf1a4b2470a2aa0e98bd4fc282542b982d711 Mon Sep 17 00:00:00 2001 From: qwertymodo Date: Sun, 3 Sep 2017 14:54:01 -0700 Subject: [PATCH 7/7] Remove .msu.zip extension support in favor of .msu1 --- memmap.cpp | 66 ++++++++++++++++++++++++++++-------------------------- msu1.cpp | 10 ++------- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/memmap.cpp b/memmap.cpp index 6e2534ee..c9927c43 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -4257,38 +4257,6 @@ void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &r } #ifdef UNZIP_SUPPORT - // Mercurial Magic (MSU-1 distribution pack) - if (strcasecmp(ext, "msu1") && strcasecmp(ext, ".msu1")) - { - _makepath(fname, drive, dir, name, "msu1"); - unzFile msu1file = unzOpen(fname); - - if (!msu1file) - { - _snprintf(fname, sizeof(fname), "%s" SLASH_STR "%s%s", - S9xGetDirectory(IPS_DIR), name, ".msu1"); - msu1file = unzOpen(fname); - } - - if (msu1file) - { - int port = unzFindExtension(msu1file, "bps"); - if (port == UNZ_OK) - { - printf(" in %s", fname); - - Stream *s = new unzStream(msu1file); - ret = ReadBPSPatch(s, offset, rom_size); - s->closeStream(); - - if (ret) - printf("!\n"); - else - printf(" failed!\n"); - } - } - } - if (!strcasecmp(ext, "zip") || !strcasecmp(ext, ".zip")) { unzFile file = unzOpen(rom_filename); @@ -4783,4 +4751,38 @@ void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &r if (flag) return; } + +#ifdef UNZIP_SUPPORT + // Mercurial Magic (MSU-1 distribution pack) + if (strcasecmp(ext, "msu1") && strcasecmp(ext, ".msu1")) + { + _makepath(fname, drive, dir, name, "msu1"); + unzFile msu1file = unzOpen(fname); + + if (!msu1file) + { + _snprintf(fname, sizeof(fname), "%s" SLASH_STR "%s%s", + S9xGetDirectory(IPS_DIR), name, ".msu1"); + msu1file = unzOpen(fname); + } + + if (msu1file) + { + int port = unzFindExtension(msu1file, "bps"); + if (port == UNZ_OK) + { + printf(" in %s", fname); + + Stream *s = new unzStream(msu1file); + ret = ReadBPSPatch(s, offset, rom_size); + s->closeStream(); + + if (ret) + printf("!\n"); + else + printf(" failed!\n"); + } + } + } +#endif } diff --git a/msu1.cpp b/msu1.cpp index 802d9697..9e8c8901 100644 --- a/msu1.cpp +++ b/msu1.cpp @@ -248,18 +248,12 @@ STREAM S9xMSU1OpenFile(char *msu_ext) printf("Using msu file %s.\n", filename); #ifdef UNZIP_SUPPORT - // look for msu file in .msu1 (Mercurial Magic pack) or .msu.zip if not found in rom dir + // look for msu1 pack file in the rom or patch dir if msu data file not found in rom dir if (!file) { - const char *zip_filename = S9xGetFilename(".msu.zip", ROMFILENAME_DIR); + const char *zip_filename = S9xGetFilename(".msu1", ROMFILENAME_DIR); unzFile unzFile = unzOpen(zip_filename); - if (!unzFile) - { - zip_filename = S9xGetFilename(".msu1", ROMFILENAME_DIR); - unzFile = unzOpen(zip_filename); - } - if (!unzFile) { zip_filename = S9xGetFilename(".msu1", IPS_DIR);