Path: blob/main/misc/emulator/xnes/snes9x/apu/apu.cpp
28798 views
/***********************************************************************************1Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.23(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),4Jerremy Koot ([email protected])56(c) Copyright 2002 - 2004 Matthew Kendora78(c) Copyright 2002 - 2005 Peter Bortas ([email protected])910(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)1112(c) Copyright 2001 - 2006 John Weidman ([email protected])1314(c) Copyright 2002 - 2006 funkyass ([email protected]),15Kris Bleakley ([email protected])1617(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),18Nach ([email protected]),1920(c) Copyright 2002 - 2011 zones ([email protected])2122(c) Copyright 2006 - 2007 nitsuja2324(c) Copyright 2009 - 2011 BearOso,25OV2262728BS-X C emulator code29(c) Copyright 2005 - 2006 Dreamer Nom,30zones3132C4 x86 assembler and some C emulation code33(c) Copyright 2000 - 2003 _Demo_ ([email protected]),34Nach,35zsKnight ([email protected])3637C4 C++ code38(c) Copyright 2003 - 2006 Brad Jorsch,39Nach4041DSP-1 emulator code42(c) Copyright 1998 - 2006 _Demo_,43Andreas Naive ([email protected]),44Gary Henderson,45Ivar ([email protected]),46John Weidman,47Kris Bleakley,48Matthew Kendora,49Nach,50neviksti ([email protected])5152DSP-2 emulator code53(c) Copyright 2003 John Weidman,54Kris Bleakley,55Lord Nightmare ([email protected]),56Matthew Kendora,57neviksti5859DSP-3 emulator code60(c) Copyright 2003 - 2006 John Weidman,61Kris Bleakley,62Lancer,63z80 gaiden6465DSP-4 emulator code66(c) Copyright 2004 - 2006 Dreamer Nom,67John Weidman,68Kris Bleakley,69Nach,70z80 gaiden7172OBC1 emulator code73(c) Copyright 2001 - 2004 zsKnight,74pagefault ([email protected]),75Kris Bleakley76Ported from x86 assembler to C by sanmaiwashi7778SPC7110 and RTC C++ emulator code used in 1.39-1.5179(c) Copyright 2002 Matthew Kendora with research by80zsKnight,81John Weidman,82Dark Force8384SPC7110 and RTC C++ emulator code used in 1.52+85(c) Copyright 2009 byuu,86neviksti8788S-DD1 C emulator code89(c) Copyright 2003 Brad Jorsch with research by90Andreas Naive,91John Weidman9293S-RTC C emulator code94(c) Copyright 2001 - 2006 byuu,95John Weidman9697ST010 C++ emulator code98(c) Copyright 2003 Feather,99John Weidman,100Kris Bleakley,101Matthew Kendora102103Super FX x86 assembler emulator code104(c) Copyright 1998 - 2003 _Demo_,105pagefault,106zsKnight107108Super FX C emulator code109(c) Copyright 1997 - 1999 Ivar,110Gary Henderson,111John Weidman112113Sound emulator code used in 1.5-1.51114(c) Copyright 1998 - 2003 Brad Martin115(c) Copyright 1998 - 2006 Charles Bilyue'116117Sound emulator code used in 1.52+118(c) Copyright 2004 - 2007 Shay Green ([email protected])119120SH assembler code partly based on x86 assembler code121(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])1221232xSaI filter124(c) Copyright 1999 - 2001 Derek Liauw Kie Fa125126HQ2x, HQ3x, HQ4x filters127(c) Copyright 2003 Maxim Stepin ([email protected])128129NTSC filter130(c) Copyright 2006 - 2007 Shay Green131132GTK+ GUI code133(c) Copyright 2004 - 2011 BearOso134135Win32 GUI code136(c) Copyright 2003 - 2006 blip,137funkyass,138Matthew Kendora,139Nach,140nitsuja141(c) Copyright 2009 - 2011 OV2142143Mac OS GUI code144(c) Copyright 1998 - 2001 John Stiles145(c) Copyright 2001 - 2011 zones146147148Specific ports contains the works of other authors. See headers in149individual files.150151152Snes9x homepage: http://www.snes9x.com/153154Permission to use, copy, modify and/or distribute Snes9x in both binary155and source form, for non-commercial purposes, is hereby granted without156fee, providing that this license information and copyright notice appear157with all copies and any derived work.158159This software is provided 'as-is', without any express or implied160warranty. In no event shall the authors be held liable for any damages161arising from the use of this software or it's derivatives.162163Snes9x is freeware for PERSONAL USE only. Commercial users should164seek permission of the copyright holders first. Commercial use includes,165but is not limited to, charging money for Snes9x or software derived from166Snes9x, including Snes9x or derivatives in commercial game bundles, and/or167using Snes9x as a promotion for your commercial product.168169The copyright holders request that bug fixes and improvements to the code170should be forwarded to them so everyone can benefit from the modifications171in future versions.172173Super NES and Super Nintendo Entertainment System are trademarks of174Nintendo Co., Limited and its subsidiary companies.175***********************************************************************************/176177178#include <math.h>179#include "snes9x.h"180#include "apu.h"181#ifdef FANCY182#include "snapshot.h"183#endif184#include "display.h"185#include "linear_resampler.h"186#include "hermite_resampler.h"187188#define APU_DEFAULT_INPUT_RATE 32000189#define APU_MINIMUM_SAMPLE_COUNT 512190#define APU_MINIMUM_SAMPLE_BLOCK 128191#define APU_NUMERATOR_NTSC 15664192#define APU_DENOMINATOR_NTSC 328125193#define APU_NUMERATOR_PAL 34176194#define APU_DENOMINATOR_PAL 709379195#define APU_DEFAULT_RESAMPLER HermiteResampler196197SNES_SPC *spc_core = NULL;198199static uint8 APUROM[64] =200{2010xCD, 0xEF, 0xBD, 0xE8, 0x00, 0xC6, 0x1D, 0xD0,2020xFC, 0x8F, 0xAA, 0xF4, 0x8F, 0xBB, 0xF5, 0x78,2030xCC, 0xF4, 0xD0, 0xFB, 0x2F, 0x19, 0xEB, 0xF4,2040xD0, 0xFC, 0x7E, 0xF4, 0xD0, 0x0B, 0xE4, 0xF5,2050xCB, 0xF4, 0xD7, 0x00, 0xFC, 0xD0, 0xF3, 0xAB,2060x01, 0x10, 0xEF, 0x7E, 0xF4, 0x10, 0xEB, 0xBA,2070xF6, 0xDA, 0x00, 0xBA, 0xF4, 0xC4, 0xF4, 0xDD,2080x5D, 0xD0, 0xDB, 0x1F, 0x00, 0x00, 0xC0, 0xFF209};210211namespace spc212{213static apu_callback sa_callback = NULL;214static void *extra_data = NULL;215216static bool8 sound_in_sync = TRUE;217static bool8 sound_enabled = FALSE;218219static int buffer_size;220static int lag_master = 0;221static int lag = 0;222223static uint8 *landing_buffer = NULL;224static uint8 *shrink_buffer = NULL;225226static Resampler *resampler = NULL;227228static int32 reference_time;229static uint32 remainder;230231static const int timing_hack_numerator = SNES_SPC::tempo_unit;232static int timing_hack_denominator = SNES_SPC::tempo_unit;233/* Set these to NTSC for now. Will change to PAL in S9xAPUTimingSetSpeedup234if necessary on game load. */235static uint32 ratio_numerator = APU_NUMERATOR_NTSC;236static uint32 ratio_denominator = APU_DENOMINATOR_NTSC;237}238239static void EightBitize (uint8 *, int);240static void DeStereo (uint8 *, int);241static void ReverseStereo (uint8 *, int);242static void UpdatePlaybackRate (void);243static void from_apu_to_state (uint8 **, void *, size_t);244static void to_apu_from_state (uint8 **, void *, size_t);245#ifdef FANCY246static void SPCSnapshotCallback (void);247#endif248static inline int S9xAPUGetClock (int32);249static inline int S9xAPUGetClockRemainder (int32);250251252static void EightBitize (uint8 *buffer, int sample_count)253{254uint8 *buf8 = (uint8 *) buffer;255int16 *buf16 = (int16 *) buffer;256257for (int i = 0; i < sample_count; i++)258buf8[i] = (uint8) ((buf16[i] / 256) + 128);259}260261static void DeStereo (uint8 *buffer, int sample_count)262{263int16 *buf = (int16 *) buffer;264int32 s1, s2;265266for (int i = 0; i < sample_count >> 1; i++)267{268s1 = (int32) buf[2 * i];269s2 = (int32) buf[2 * i + 1];270buf[i] = (int16) ((s1 + s2) >> 1);271}272}273274static void ReverseStereo (uint8 *src_buffer, int sample_count)275{276int16 *buffer = (int16 *) src_buffer;277278for (int i = 0; i < sample_count; i += 2)279{280buffer[i + 1] ^= buffer[i];281buffer[i] ^= buffer[i + 1];282buffer[i + 1] ^= buffer[i];283}284}285286bool8 S9xMixSamples (uint8 *buffer, int sample_count)287{288static int shrink_buffer_size = -1;289uint8 *dest;290291if (!Settings.SixteenBitSound || !Settings.Stereo)292{293/* We still need both stereo samples for generating the mono sample */294if (!Settings.Stereo)295sample_count <<= 1;296297/* We still have to generate 16-bit samples for bit-dropping, too */298if (shrink_buffer_size < (sample_count << 1))299{300delete[] spc::shrink_buffer;301spc::shrink_buffer = new uint8[sample_count << 1];302shrink_buffer_size = sample_count << 1;303}304305dest = spc::shrink_buffer;306}307else308dest = buffer;309310if (Settings.Mute)311{312memset(dest, 0, sample_count << 1);313spc::resampler->clear();314315return (FALSE);316}317else318{319if (spc::resampler->avail() >= (sample_count + spc::lag))320{321spc::resampler->read((short *) dest, sample_count);322if (spc::lag == spc::lag_master)323spc::lag = 0;324}325else326{327memset(buffer, (Settings.SixteenBitSound ? 0 : 128), (sample_count << (Settings.SixteenBitSound ? 1 : 0)) >> (Settings.Stereo ? 0 : 1));328if (spc::lag == 0)329spc::lag = spc::lag_master;330331return (FALSE);332}333}334335if (Settings.ReverseStereo && Settings.Stereo)336ReverseStereo(dest, sample_count);337338if (!Settings.Stereo || !Settings.SixteenBitSound)339{340if (!Settings.Stereo)341{342DeStereo(dest, sample_count);343sample_count >>= 1;344}345346if (!Settings.SixteenBitSound)347EightBitize(dest, sample_count);348349memcpy(buffer, dest, (sample_count << (Settings.SixteenBitSound ? 1 : 0)));350}351352return (TRUE);353}354355int S9xGetSampleCount (void)356{357return (spc::resampler->avail() >> (Settings.Stereo ? 0 : 1));358}359360void S9xFinalizeSamples (void)361{362if (!Settings.Mute)363{364if (!spc::resampler->push((short *) spc::landing_buffer, spc_core->sample_count()))365{366/* We weren't able to process the entire buffer. Potential overrun. */367spc::sound_in_sync = FALSE;368369if (Settings.SoundSync && !Settings.TurboMode)370return;371}372}373374if (!Settings.SoundSync || Settings.TurboMode || Settings.Mute)375spc::sound_in_sync = TRUE;376else377if (spc::resampler->space_empty() >= spc::resampler->space_filled())378spc::sound_in_sync = TRUE;379else380spc::sound_in_sync = FALSE;381382spc_core->set_output((SNES_SPC::sample_t *) spc::landing_buffer, spc::buffer_size >> 1);383}384385void S9xLandSamples (void)386{387if (spc::sa_callback != NULL)388spc::sa_callback(spc::extra_data);389else390S9xFinalizeSamples();391}392393void S9xClearSamples (void)394{395spc::resampler->clear();396spc::lag = spc::lag_master;397}398399bool8 S9xSyncSound (void)400{401if (!Settings.SoundSync || spc::sound_in_sync)402return (TRUE);403404S9xLandSamples();405406return (spc::sound_in_sync);407}408409void S9xSetSamplesAvailableCallback (apu_callback callback, void *data)410{411spc::sa_callback = callback;412spc::extra_data = data;413}414415static void UpdatePlaybackRate (void)416{417if (Settings.SoundInputRate == 0)418Settings.SoundInputRate = APU_DEFAULT_INPUT_RATE;419420double time_ratio = (double) Settings.SoundInputRate * spc::timing_hack_numerator / (Settings.SoundPlaybackRate * spc::timing_hack_denominator);421spc::resampler->time_ratio(time_ratio);422}423424bool8 S9xInitSound (int buffer_ms, int lag_ms)425{426// buffer_ms : buffer size given in millisecond427// lag_ms : allowable time-lag given in millisecond428429int sample_count = buffer_ms * 32000 / 1000;430int lag_sample_count = lag_ms * 32000 / 1000;431432spc::lag_master = lag_sample_count;433if (Settings.Stereo)434spc::lag_master <<= 1;435spc::lag = spc::lag_master;436437if (sample_count < APU_MINIMUM_SAMPLE_COUNT)438sample_count = APU_MINIMUM_SAMPLE_COUNT;439440spc::buffer_size = sample_count;441if (Settings.Stereo)442spc::buffer_size <<= 1;443if (Settings.SixteenBitSound)444spc::buffer_size <<= 1;445446printf("Sound buffer size: %d (%d samples)\n", spc::buffer_size, sample_count);447448if (spc::landing_buffer)449delete[] spc::landing_buffer;450spc::landing_buffer = new uint8[spc::buffer_size * 2];451if (!spc::landing_buffer)452return (FALSE);453454/* The resampler and spc unit use samples (16-bit short) as455arguments. Use 2x in the resampler for buffer leveling with SoundSync */456if (!spc::resampler)457{458spc::resampler = new APU_DEFAULT_RESAMPLER(spc::buffer_size >> (Settings.SoundSync ? 0 : 1));459if (!spc::resampler)460{461delete[] spc::landing_buffer;462return (FALSE);463}464}465else466spc::resampler->resize(spc::buffer_size >> (Settings.SoundSync ? 0 : 1));467468spc_core->set_output((SNES_SPC::sample_t *) spc::landing_buffer, spc::buffer_size >> 1);469470UpdatePlaybackRate();471472spc::sound_enabled = S9xOpenSoundDevice();473474return (spc::sound_enabled);475}476477void S9xSetSoundControl (uint8 voice_switch)478{479spc_core->dsp_set_stereo_switch(voice_switch << 8 | voice_switch);480}481482void S9xSetSoundMute (bool8 mute)483{484Settings.Mute = mute;485if (!spc::sound_enabled)486Settings.Mute = TRUE;487}488489void S9xDumpSPCSnapshot (void)490{491spc_core->dsp_dump_spc_snapshot();492}493#ifdef FANCY494static void SPCSnapshotCallback (void)495{496S9xSPCDump(S9xGetFilenameInc((".spc"), SPC_DIR));497printf("Dumped key-on triggered spc snapshot.\n");498}499#endif500bool8 S9xInitAPU (void)501{502spc_core = new SNES_SPC;503if (!spc_core)504return (FALSE);505506spc_core->init();507spc_core->init_rom(APUROM);508#ifdef FANCY509spc_core->dsp_set_spc_snapshot_callback(SPCSnapshotCallback);510#endif511spc::landing_buffer = NULL;512spc::shrink_buffer = NULL;513spc::resampler = NULL;514515return (TRUE);516}517518void S9xDeinitAPU (void)519{520if (spc_core)521{522delete spc_core;523spc_core = NULL;524}525526if (spc::resampler)527{528delete spc::resampler;529spc::resampler = NULL;530}531532if (spc::landing_buffer)533{534delete[] spc::landing_buffer;535spc::landing_buffer = NULL;536}537538if (spc::shrink_buffer)539{540delete[] spc::shrink_buffer;541spc::shrink_buffer = NULL;542}543}544545static inline int S9xAPUGetClock (int32 cpucycles)546{547return (spc::ratio_numerator * (cpucycles - spc::reference_time) + spc::remainder) /548spc::ratio_denominator;549}550551static inline int S9xAPUGetClockRemainder (int32 cpucycles)552{553return (spc::ratio_numerator * (cpucycles - spc::reference_time) + spc::remainder) %554spc::ratio_denominator;555}556557uint8 S9xAPUReadPort (int port)558{559return ((uint8) spc_core->read_port(S9xAPUGetClock(CPU.Cycles), port));560}561562void S9xAPUWritePort (int port, uint8 byte)563{564spc_core->write_port(S9xAPUGetClock(CPU.Cycles), port, byte);565}566567void S9xAPUSetReferenceTime (int32 cpucycles)568{569spc::reference_time = cpucycles;570}571572void S9xAPUExecute (void)573{574/* Accumulate partial APU cycles */575spc_core->end_frame(S9xAPUGetClock(CPU.Cycles));576577spc::remainder = S9xAPUGetClockRemainder(CPU.Cycles);578579S9xAPUSetReferenceTime(CPU.Cycles);580}581582void S9xAPUEndScanline (void)583{584S9xAPUExecute();585586if (spc_core->sample_count() >= APU_MINIMUM_SAMPLE_BLOCK || !spc::sound_in_sync)587S9xLandSamples();588}589590void S9xAPUTimingSetSpeedup (int ticks)591{592if (ticks != 0)593printf("APU speedup hack: %d\n", ticks);594595spc::timing_hack_denominator = SNES_SPC::tempo_unit - ticks;596spc_core->set_tempo(spc::timing_hack_denominator);597598spc::ratio_numerator = Settings.PAL ? APU_NUMERATOR_PAL : APU_NUMERATOR_NTSC;599spc::ratio_denominator = Settings.PAL ? APU_DENOMINATOR_PAL : APU_DENOMINATOR_NTSC;600spc::ratio_denominator = spc::ratio_denominator * spc::timing_hack_denominator / spc::timing_hack_numerator;601602UpdatePlaybackRate();603}604605void S9xAPUAllowTimeOverflow (bool allow)606{607if (allow)608printf("APU time overflow allowed\n");609610spc_core->spc_allow_time_overflow(allow);611}612613void S9xResetAPU (void)614{615spc::reference_time = 0;616spc::remainder = 0;617spc_core->reset();618spc_core->set_output((SNES_SPC::sample_t *) spc::landing_buffer, spc::buffer_size >> 1);619620spc::resampler->clear();621}622623void S9xSoftResetAPU (void)624{625spc::reference_time = 0;626spc::remainder = 0;627spc_core->soft_reset();628spc_core->set_output((SNES_SPC::sample_t *) spc::landing_buffer, spc::buffer_size >> 1);629630spc::resampler->clear();631}632633static void from_apu_to_state (uint8 **buf, void *var, size_t size)634{635memcpy(*buf, var, size);636*buf += size;637}638639static void to_apu_from_state (uint8 **buf, void *var, size_t size)640{641memcpy(var, *buf, size);642*buf += size;643}644645void S9xAPUSaveState (uint8 *block)646{647uint8 *ptr = block;648649spc_core->copy_state(&ptr, from_apu_to_state);650651SET_LE32(ptr, spc::reference_time);652ptr += sizeof(int32);653SET_LE32(ptr, spc::remainder);654}655656void S9xAPULoadState (uint8 *block)657{658uint8 *ptr = block;659660S9xResetAPU();661662spc_core->copy_state(&ptr, to_apu_from_state);663664spc::reference_time = GET_LE32(ptr);665ptr += sizeof(int32);666spc::remainder = GET_LE32(ptr);667}668669670