diff --git a/apu/apu.cpp b/apu/apu.cpp index 8cbacca0..8d12cd1a 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,11 +345,12 @@ 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))); } + else // should never occur + assert(0); } } else @@ -381,20 +391,11 @@ int S9xGetSampleCount (void) /* TODO: Attach */ void S9xFinalizeSamples (void) { + bool drop_current_msu1_samples = true; + if (!Settings.Mute) { - if (Settings.MSU1) - { - S9xMSU1SetOutput((int16 *)msu::landing_buffer, msu::buffer_size); - S9xMSU1Generate(SNES::dsp.spc_dsp.sample_count()); - if (!msu::resampler->push((short *)msu::landing_buffer, S9xMSU1Samples())) - { - //spc::sound_in_sync = FALSE; - - //if (Settings.SoundSync && !Settings.TurboMode) - //return; - } - } + drop_current_msu1_samples = false; if (!spc::resampler->push((short *)spc::landing_buffer, SNES::dsp.spc_dsp.sample_count())) { @@ -403,6 +404,24 @@ void S9xFinalizeSamples (void) if (Settings.SoundSync && !Settings.TurboMode) return; + + // since we drop the current dsp samples we also want to drop generated msu1 samples + drop_current_msu1_samples = true; + } + } + + // only generate msu1 if we really consumed the dsp samples (sample_count() resets at end of function), + // otherwise we will generate multiple times for the same samples - so this needs to be after all early + // function returns + if (Settings.MSU1) + { + // generate the same number of msu1 samples as dsp samples were generated + S9xMSU1SetOutput((int16 *)msu::landing_buffer, msu::buffer_size); + S9xMSU1Generate(SNES::dsp.spc_dsp.sample_count()); + if (!drop_current_msu1_samples && !msu::resampler->push((short *)msu::landing_buffer, S9xMSU1Samples())) + { + // should not occur, msu buffer is larger and we drop msu samples if spc buffer overruns + assert(0); } } @@ -486,7 +505,7 @@ bool8 S9xInitSound (int buffer_ms, int lag_ms) spc::buffer_size <<= 1; if (Settings.SixteenBitSound) spc::buffer_size <<= 1; - msu::buffer_size = sample_count << 2; // Always 16-bit, Stereo + msu::buffer_size = (int)((sample_count << 2) * 1.5); // Always 16-bit, Stereo; 1.5 to never overflow before dsp buffer printf("Sound buffer size: %d (%d samples)\n", spc::buffer_size, sample_count); @@ -603,6 +622,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) 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 4fc88dd7..c9927c43 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 @@ -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) @@ -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); @@ -4751,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 1c253244..9e8c8901 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" @@ -206,10 +207,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 +225,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 + e && strcasecmp(name + len - l, ext) == 0 && unzOpenCurrentFile(file) == UNZ_OK) { if (print) printf("Using msu file %s", name); @@ -247,24 +248,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 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); - if (zip_filename) + const char *zip_filename = S9xGetFilename(".msu1", ROMFILENAME_DIR); + unzFile unzFile = unzOpen(zip_filename); + + if (!unzFile) + { + zip_filename = S9xGetFilename(".msu1", IPS_DIR); + unzFile = unzOpen(zip_filename); + } + + if (unzFile) { - unzFile unzFile = unzOpen(zip_filename); - if (unzFile) + int port = unzFindExtension(unzFile, msu_ext, true, true, true); + 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 +326,10 @@ bool DataOpen() } dataStream = S9xMSU1OpenFile(".msu"); + + if(!dataStream) + dataStream = S9xMSU1OpenFile("msu1.rom"); + return dataStream != NULL; } @@ -366,12 +375,30 @@ void S9xMSU1Init(void) 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; + } +#ifdef UNZIP_SUPPORT + char ext[_MAX_EXT + 1]; + _splitpath(Memory.ROMFilename, nullptr, nullptr, nullptr, ext); + if (!strcasecmp(ext, ".msu1")) + return true; + + unzFile unzFile = unzOpen(S9xGetFilename(".msu1", ROMFILENAME_DIR)); + + if(!unzFile) + unzFile = unzOpen(S9xGetFilename(".msu1", IPS_DIR)); + + if (unzFile) + { + unzCloseCurrentFile(unzFile); + return true; + } +#endif + return false; } void S9xMSU1Generate(size_t sample_count) diff --git a/win32/wsnes9x.cpp b/win32/wsnes9x.cpp index bbcf34f8..4b6272a8 100644 --- a/win32/wsnes9x.cpp +++ b/win32/wsnes9x.cpp @@ -7063,10 +7063,17 @@ void MakeExtFile(void) ofstream out; out.open("Valid.Ext"); - out<<"smcN"<