Index: apps/metadata.c
===================================================================
RCS file: /cvsroot/rockbox/apps/metadata.c,v
retrieving revision 1.52
diff -u -r1.52 metadata.c
--- apps/metadata.c	11 Oct 2006 17:42:33 -0000	1.52
+++ apps/metadata.c	23 Oct 2006 06:42:49 -0000
@@ -115,6 +115,8 @@
     { AFMT_AIFF,          "aiff" },
     { AFMT_SID,           "sid"  },
     { AFMT_ADX,           "adx"  },
+    { AFMT_NSF,           "nsf"  },
+    { AFMT_NSF,           "nsfe" },
 #endif
 };
 
@@ -1881,6 +1883,34 @@
         }
 
         break;
+
+    case AFMT_NSF:
+        buf = track->id3.path;
+	if ((lseek(fd, 0, SEEK_SET) < 0)
+	    || ((read(fd, buf, 0x10)) < 0x10))
+	{
+	    DEBUGF("lseek or read failed\n");
+    	    return false;
+	}
+	track->id3.vbr = false;
+	track->id3.filesize = filesize(fd);
+	if (!memcmp(buf,"NESM",4))
+        track->id3.length = buf[6]*1000;
+	else if (!memcmp(buf,"NSFE",4)) {
+	    lseek(fd, 4, SEEK_SET);
+
+	    do {
+		if (read(fd,buf,8)<8) return false;
+		if (memcmp(buf+4,"INFO",4))
+		    lseek(fd, ((int32_t)buf[0])|((int32_t)buf[1]<<8)|
+		    ((int32_t)buf[2]<<16)|((int32_t)buf[3]<<24), SEEK_CUR);
+		else break;
+	    }
+	    while (1);
+	    read(fd,buf,0x10);
+	    track->id3.length = buf[8]*1000;
+	} else return false;
+	break;
 #endif /* CONFIG_CODEC == SWCODEC */
 
     case AFMT_AIFF:
Index: apps/tree.c
===================================================================
RCS file: /cvsroot/rockbox/apps/tree.c,v
retrieving revision 1.450
diff -u -r1.450 tree.c
--- apps/tree.c	19 Oct 2006 09:42:19 -0000	1.450
+++ apps/tree.c	23 Oct 2006 06:42:49 -0000
@@ -106,6 +106,8 @@
     { "aiff",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
     { "sid", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
     { "adx", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
+    { "nsf", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
+    { "nsfe", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
 #endif
     { "m3u", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
     { "m3u8", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
Index: apps/codecs/Makefile
===================================================================
RCS file: /cvsroot/rockbox/apps/codecs/Makefile,v
retrieving revision 1.38
diff -u -r1.38 Makefile
--- apps/codecs/Makefile	29 Sep 2006 20:04:35 -0000	1.38
+++ apps/codecs/Makefile	23 Oct 2006 06:42:49 -0000
@@ -51,6 +51,7 @@
 $(OBJDIR)/wav.elf : $(OBJDIR)/wav.o
 $(OBJDIR)/sid.elf : $(OBJDIR)/sid.o
 $(OBJDIR)/adx.elf : $(OBJDIR)/adx.o
+$(OBJDIR)/nsf.elf : $(OBJDIR)/nsf.o
 $(OBJDIR)/aiff.elf : $(OBJDIR)/aiff.o
 $(OBJDIR)/mpa.elf : $(OBJDIR)/mpa.o $(BUILDDIR)/libmad.a
 $(OBJDIR)/a52.elf : $(OBJDIR)/a52.o $(BUILDDIR)/liba52.a
Index: apps/codecs/SOURCES
===================================================================
RCS file: /cvsroot/rockbox/apps/codecs/SOURCES,v
retrieving revision 1.11
diff -u -r1.11 SOURCES
--- apps/codecs/SOURCES	25 Sep 2006 16:13:05 -0000	1.11
+++ apps/codecs/SOURCES	23 Oct 2006 06:42:49 -0000
@@ -15,6 +15,7 @@
 aiff.c
 sid.c
 adx.c
+nsf.c
 #if defined(HAVE_RECORDING) && !defined(SIMULATOR)
 /* encoders */
 mp3_enc.c
Index: apps/codecs/nsf.c
===================================================================
RCS file: apps/codecs/nsf.c
diff -N apps/codecs/nsf.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ apps/codecs/nsf.c	23 Oct 2006 06:42:49 -0000
@@ -0,0 +1,4730 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ *
+ * Copyright (C) 2006 Adam Gashlin (hcs)
+ * Copyright (C) 2004 Disch
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#include "codeclib.h"
+#include "inttypes.h"
+/* for letoh */
+#include "system.h"
+
+CODEC_HEADER
+
+#ifdef USE_IRAM
+extern char iramcopy[];
+extern char iramstart[];
+extern char iramend[];
+extern char iedata[];
+extern char iend[];
+#endif
+
+struct codec_api *rb;
+
+/* Maximum number of bytes to process in one iteration */
+#define WAV_CHUNK_SIZE (1024*2)
+
+static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR;
+
+typedef uint8_t UINT8;
+typedef uint8_t BYTE;
+typedef int16_t INT16;
+typedef uint16_t UINT16;
+typedef uint16_t WORD;
+typedef uint32_t UINT;
+typedef int32_t INT32;
+typedef uint32_t UINT32;
+typedef int64_t __int64;
+typedef int8_t * LPCSTR;
+
+#define min(x, y) ((x)<(y)?(x):(y))
+#define ZeroMemory(addr,size) memset(addr,0,size)
+#define lstrlen(x) strlen(x)
+
+#if (CONFIG_CPU == MCF5249) || (CONFIG_CPU == MCF5250)
+/* things which seem to improve performance on coldfire */
+// disable use of const in APU
+#define USE_CONST
+// enable use of division and modulus instead of loops where possible
+#define USE_MODACCEL
+#else
+/* things which seem to improve performance on portalplayer */
+// use const
+#define USE_CONST const
+#endif
+
+/* proper handling of multibyte values */
+#ifdef ROCKBOX_LITTLE_ENDIAN
+union TWIN
+{
+	WORD						W;
+	struct{ BYTE l; BYTE h; }	B;
+};
+
+union QUAD
+{
+	UINT								D;
+	struct{ BYTE l; BYTE h; WORD w; }	B;
+};
+#else
+
+union TWIN
+{
+	WORD						W;
+	struct{ BYTE h; BYTE l; }	B;
+};
+
+union QUAD
+{
+	UINT								D;
+	struct{WORD w; BYTE h; BYTE l; }	B;
+};
+
+#endif
+
+#define NTSC_FREQUENCY			 1789772.727273f
+#define PAL_FREQUENCY			 1652097.692308f
+#define NTSC_NMIRATE			      60.098814f
+#define PAL_NMIRATE				      50.006982f
+
+#define NES_FREQUENCY				21477270
+#define NTSC_FRAME_COUNTER_FREQ		(NTSC_FREQUENCY / (NES_FREQUENCY / 89490.0f))
+#define PAL_FRAME_COUNTER_FREQ		(PAL_FREQUENCY / (NES_FREQUENCY / 89490.0f))
+
+/****************** tables */
+static const int32_t ModulationTable[8] = {0,1,2,4,0,-4,-2,-1};
+const WORD	DMC_FREQ_TABLE[2][0x10] = {
+//NTSC
+{0x1AC,0x17C,0x154,0x140,0x11E,0x0FE,0x0E2,0x0D6,0x0BE,0x0A0,0x08E,0x080,0x06A,0x054,0x048,0x036},
+//PAL
+{0x18C,0x160,0x13A,0x128,0x108,0x0EA,0x0D0,0x0C6,0x0B0,0x094,0x082,0x076,0x062,0x04E,0x042,0x032}
+};
+
+const BYTE DUTY_CYCLE_TABLE[4] = {2,4,8,12};
+
+const BYTE LENGTH_COUNTER_TABLE[0x20] = {
+0x0A,0xFE,0x14,0x02,0x28,0x04,0x50,0x06,0xA0,0x08,0x3C,0x0A,0x0E,0x0C,0x1A,0x0E,
+0x0C,0x10,0x18,0x12,0x30,0x14,0x60,0x16,0xC0,0x18,0x48,0x1A,0x10,0x1C,0x20,0x1E	};
+
+const WORD NOISE_FREQ_TABLE[0x10] = {
+0x004,0x008,0x010,0x020,0x040,0x060,0x080,0x0A0,0x0CA,0x0FE,0x17C,0x1FC,0x2FA,0x3F8,0x7F2,0xFE4	};
+/****************** NSF loading ******************/
+
+/// file format structs (both are little endian)
+
+struct NESM_HEADER
+{
+	UINT			nHeader;
+	BYTE			nHeaderExtra;
+	BYTE			nVersion;
+	BYTE			nTrackCount;
+	BYTE			nInitialTrack;
+	WORD			nLoadAddress;
+	WORD			nInitAddress;
+	WORD			nPlayAddress;
+	BYTE			szGameTitle[32];
+	BYTE			szArtist[32];
+	BYTE			szCopyright[32];
+	WORD			nSpeedNTSC;
+	BYTE			nBankSwitch[8];
+	WORD			nSpeedPAL;
+	BYTE			nNTSC_PAL;
+	BYTE			nExtraChip;
+	BYTE			nExpansion[4];
+};
+
+struct NSFE_INFOCHUNK
+{
+	WORD			nLoadAddress;
+	WORD			nInitAddress;
+	WORD			nPlayAddress;
+	BYTE			nIsPal;
+	BYTE			nExt;
+	BYTE			nTrackCount;
+	BYTE			nStartingTrack;
+};
+
+/// prototyped
+
+int32_t				LoadFile(BYTE *,size_t);
+
+int32_t		LoadFile_NESM(BYTE *,size_t);	//these functions are used internally and should not be called
+int32_t		LoadFile_NSFE(BYTE *,size_t);
+
+///////////////////////////////////
+// (global) data members
+
+//basic NSF info
+int32_t				bIsExtended=0;		//0 = NSF, 1 = NSFE
+BYTE				nIsPal=0;				//0 = NTSC, 1 = PAL, 2,3 = mixed NTSC/PAL (interpretted as NTSC)
+int32_t					nfileLoadAddress=0;		//The address to which the NSF code is loaded to
+int32_t					nfileInitAddress=0;		//The address of the Init routine (called at track change)
+int32_t					nfilePlayAddress=0;		//The address of the Play routine (called several times a second)
+BYTE				nChipExtensions=0;	//Bitwise representation of the external chips used by this NSF.  Read NSFSpec.txt for details.
+	
+//old NESM speed stuff (blarg)
+int32_t					nNTSC_PlaySpeed=0;
+int32_t					nPAL_PlaySpeed=0;
+
+//track info
+int32_t					nTrackCount=0;		//The number of tracks in the NSF (1 = 1 track, 5 = 5 tracks, etc)
+int32_t					nInitialTrack=0;		//The initial track (ZERO BASED:  0 = 1st track, 4 = 5th track, etc)
+
+//nsf data
+BYTE*				pDataBuffer=0;		//the buffer containing NSF code.  If needdata was false when loading the NSF, this is NULL
+int32_t					nDataBufferSize=0;	//the size of the above buffer.  0 if needdata was false
+
+//playlist
+BYTE				nPlaylist[256];			//the buffer containing the playlist (NULL if none exists).  Each entry is the zero based index of the song to play
+int32_t					nPlaylistSize=0;		//the size of the above buffer (and the number of tracks in the playlist)
+
+//track time / fade
+int32_t				nTrackTime[256];			//the buffer containing the track times.  NULL if no track times specified.  Otherwise this buffer MUST BE (nTrackCount * sizeof(int)) in size
+int32_t				nTrackFade[256];			//the buffer containing the track fade times.  NULL if none are specified.  Same conditions as pTrackTime
+
+//string info
+BYTE				szGameTitle[0x101];
+BYTE				szArtist[0x101];
+BYTE				szCopyright[0x101];
+BYTE				szRipper[0x101];
+
+//bankswitching info
+BYTE				nBankswitch[8]={0};		//The initial bankswitching registers needed for some NSFs.  If the NSF does not use bankswitching, these values will all be zero
+
+int32_t		LoadFile(BYTE * inbuffer, size_t size)
+{
+	if(!inbuffer) return -1;
+
+	int32_t ret = -1;
+
+	if(!memcmp(inbuffer,"NESM",4)) ret = LoadFile_NESM(inbuffer,size);
+	if(!memcmp(inbuffer,"NSFE",4)) ret = LoadFile_NSFE(inbuffer,size);
+
+	// Snake's revenge puts '00' for the initial track, which (after subtracting 1) makes it 256 or -1 (bad!)
+	// This prevents that crap
+	if(nInitialTrack >= nTrackCount)
+		nInitialTrack = 0;
+	if(nInitialTrack < 0)
+		nInitialTrack = 0;
+
+	// if there's no tracks... this is a crap NSF
+	if(nTrackCount < 1)
+	{
+		return -1;
+	}
+
+	return ret;
+}
+
+int32_t LoadFile_NESM(BYTE* inbuffer, size_t size)
+{
+    BYTE ignoreversion=1;
+    BYTE needdata=1;
+
+	//read the info
+	struct NESM_HEADER hdr;
+	
+	memcpy(&hdr,inbuffer,sizeof(hdr));
+
+	//confirm the header
+	if(memcmp("NESM",&(hdr.nHeader),4))			return -1;
+	if(hdr.nHeaderExtra != 0x1A)				return -1;
+	if((!ignoreversion) && (hdr.nVersion != 1))	return -1; //stupid NSFs claim to be above version 1  >_>
+
+	//NESM is generally easier to work with (but limited!)
+	//  just move the data over from NESM_HEADER over to our member data
+
+	bIsExtended =				0;
+	nIsPal =					hdr.nNTSC_PAL & 0x03;
+	nPAL_PlaySpeed =			letoh16(hdr.nSpeedPAL);			//blarg
+	nNTSC_PlaySpeed =			letoh16(hdr.nSpeedNTSC);			//blarg
+	nfileLoadAddress =				letoh16(hdr.nLoadAddress);
+	nfileInitAddress =				letoh16(hdr.nInitAddress);
+	nfilePlayAddress =				letoh16(hdr.nPlayAddress);
+	nChipExtensions =			hdr.nExtraChip;
+
+
+	nTrackCount =				hdr.nTrackCount;
+	nInitialTrack =				hdr.nInitialTrack - 1;	//stupid 1-based number =P
+
+	memcpy(nBankswitch,hdr.nBankSwitch,8);
+
+	memcpy(szGameTitle,hdr.szGameTitle,32);
+	memcpy(szArtist   ,hdr.szArtist   ,32);
+	memcpy(szCopyright,hdr.szCopyright,32);
+
+	//read the NSF data
+	if(needdata)
+	{
+		pDataBuffer=inbuffer+0x80;
+		nDataBufferSize=size-0x80;
+	}
+
+	//if we got this far... it was a successful read
+	return 0;
+}
+
+int32_t LoadFile_NSFE(BYTE* inbuffer, size_t size)
+{
+	//the vars we'll be using
+	UINT nChunkType;
+	int32_t  nChunkSize;
+	int32_t  nChunkUsed;
+	int32_t i;
+	BYTE *  nDataPos = 0;
+	BYTE	bInfoFound = 0;
+	BYTE	bEndFound = 0;
+	BYTE	bBankFound = 0;
+	nPlaylistSize=-1;
+
+	struct NSFE_INFOCHUNK	info;
+	ZeroMemory(&info,sizeof(struct NSFE_INFOCHUNK));
+	ZeroMemory(nBankswitch,8);
+	info.nTrackCount = 1;		//default values
+	
+	if (size < 8) return -1; // must have at least NSFE,NEND
+
+	//confirm the header!
+	memcpy(&nChunkType,inbuffer,4);
+	inbuffer+=4;
+	if(memcmp(&nChunkType,"NSFE",4))			return -1;
+
+	for (i=0;i<256;i++) {
+	    nTrackTime[i]=-1;
+	    nTrackFade[i]=-1;
+	}
+
+	//begin reading chunks
+	while(!bEndFound)
+	{
+		memcpy(&nChunkSize,inbuffer,4);
+		nChunkSize=letoh32(nChunkSize);
+		inbuffer+=4;
+		memcpy(&nChunkType,inbuffer,4);
+		inbuffer+=4;
+
+		if(!memcmp(&nChunkType,"INFO",4)) {
+			if(bInfoFound)						return -1;	//only one info chunk permitted
+			if(nChunkSize < 8)					return -1;	//minimum size
+
+			bInfoFound = 1;
+			nChunkUsed = min((int32_t)sizeof(struct NSFE_INFOCHUNK),nChunkSize);
+
+			memcpy(&info,inbuffer,nChunkUsed);
+			inbuffer+=nChunkSize;
+
+			bIsExtended =			1;
+			nIsPal =				info.nIsPal & 3;
+			nfileLoadAddress =			letoh16(info.nLoadAddress);
+			nfileInitAddress =			letoh16(info.nInitAddress);
+			nfilePlayAddress =			letoh16(info.nPlayAddress);
+			nChipExtensions =		info.nExt;
+			nTrackCount =			info.nTrackCount;
+			nInitialTrack =			info.nStartingTrack;
+
+			nPAL_PlaySpeed =		(WORD)(1000000 / PAL_NMIRATE);		//blarg
+			nNTSC_PlaySpeed =		(WORD)(1000000 / NTSC_NMIRATE);		//blarg
+		} else if (!memcmp(&nChunkType,"DATA",4)) {
+			if(!bInfoFound)						return -1;
+			if(nDataPos)						return -1;
+			if(nChunkSize < 1)					return -1;
+
+			nDataBufferSize = nChunkSize;
+			nDataPos = inbuffer;
+
+			inbuffer+=nChunkSize;
+		} else if (!memcmp(&nChunkType,"NEND",4)) {
+			bEndFound = 1;
+		} else if (!memcmp(&nChunkType,"time",4)) {
+			if(!bInfoFound)						return -1;
+			for (nChunkUsed=0; nChunkUsed < min(nChunkSize / 4,nTrackCount); nChunkUsed++,inbuffer+=4) {
+			    nTrackTime[nChunkUsed]=((UINT)inbuffer[0])|((UINT)inbuffer[1]<<8)|((UINT)inbuffer[2]<<16)|((UINT)inbuffer[3]<<24);
+			}
+
+			inbuffer+=nChunkSize-(nChunkUsed*4);
+
+			for(; nChunkUsed < nTrackCount; nChunkUsed++)
+				nTrackTime[nChunkUsed] = -1;	//negative signals to use default time
+		} else if (!memcmp(&nChunkType,"fade",4)) {
+			if(!bInfoFound)						return -1;
+			for (nChunkUsed=0; nChunkUsed < min(nChunkSize / 4,nTrackCount); nChunkUsed++,inbuffer+=4) {
+			    nTrackFade[nChunkUsed]=((UINT)inbuffer[0])|((UINT)inbuffer[1]<<8)|((UINT)inbuffer[2]<<16)|((UINT)inbuffer[3]<<24);
+			}
+
+			inbuffer+=nChunkSize-(nChunkUsed*4);
+
+			for(; nChunkUsed < nTrackCount; nChunkUsed++)
+				nTrackFade[nChunkUsed] = -1;	//negative signals to use default time
+		} else if (!memcmp(&nChunkType,"BANK",4)) {
+			if(bBankFound)						return -1;
+
+			bBankFound = 1;
+			nChunkUsed = min(8,nChunkSize);
+			memcpy(nBankswitch,inbuffer,nChunkUsed);
+
+			inbuffer+=nChunkSize;
+		} else if (!memcmp(&nChunkType,"plst",4)) {
+
+			nPlaylistSize = nChunkSize;
+			if(nPlaylistSize >= 1) {
+
+				memcpy(nPlaylist,inbuffer,nChunkSize);
+				inbuffer+=nChunkSize;
+			}
+		} else if (!memcmp(&nChunkType,"auth",4)) {
+			BYTE*		ptr;
+
+			ptr = inbuffer;
+
+			BYTE*		ar[4] = {szGameTitle,szArtist,szCopyright,szRipper};
+			int32_t			i;
+			for(i = 0; (ptr-inbuffer)<nChunkSize && i < 4; i++)
+			{
+				nChunkUsed = lstrlen(ptr) + 1;
+				memcpy(ar[i],ptr,nChunkUsed);
+				ptr += nChunkUsed;
+			}
+			inbuffer+=nChunkSize;
+		} else if (!memcmp(&nChunkType,"tlbl",4)) {
+		    // we unfortunately can't use these anyway
+			inbuffer+=nChunkSize;
+		} else { // unknown chunk
+			nChunkType = letoh32(nChunkType)>>24;  //check the first byte
+			if((nChunkType >= 'A') && (nChunkType <= 'Z'))	//chunk is vital... don't continue
+				return -1;
+			//otherwise, just skip it
+			inbuffer+=nChunkSize;
+		}		//end if series
+	}			//end while
+
+	//if we exited the while loop without a 'return', we must have hit an NEND chunk
+	//  if this is the case, the file was layed out as it was expected.
+	//  now.. make sure we found both an info chunk, AND a data chunk... since these are
+	//  minimum requirements for a valid NSFE file
+
+	if(!bInfoFound)			return -1;
+	if(!nDataPos)			return -1;
+
+	//if both those chunks existed, this file is valid.  Load the data if it's needed
+
+	pDataBuffer=nDataPos;
+
+	//return success!
+	return 0;
+}
+
+
+/****************** Audio Device Structures ******************/
+
+struct FDSWave
+{
+	/*	Envelope Unit	*/
+	BYTE		bEnvelopeEnable;
+	BYTE		nEnvelopeSpeed;
+
+	/*	Volume Envelope	*/
+	BYTE		nVolEnv_Mode;
+	BYTE		nVolEnv_Decay;
+	BYTE		nVolEnv_Gain;
+	int32_t			nVolEnv_Timer;
+	int32_t			nVolEnv_Count;
+	BYTE		nVolume;
+	BYTE		bVolEnv_On;
+
+	/*	Sweep Envenlope	*/
+	BYTE		nSweep_Mode;
+	BYTE		nSweep_Decay;
+	int32_t			nSweep_Timer;
+	int32_t			nSweep_Count;
+	BYTE		nSweep_Gain;
+	BYTE		bSweepEnv_On;
+
+	/*	Effector / LFO / Modulation	Unit	*/
+	int32_t			nSweepBias;
+	BYTE		bLFO_Enabled;
+	union TWIN		nLFO_Freq;
+	float		fLFO_Timer;
+	float		fLFO_Count;
+	BYTE		nLFO_Addr;
+	BYTE		nLFO_Table[0x40];
+	BYTE		bLFO_On;
+
+	/*	Main Output		*/
+	BYTE		nMainVolume;
+	BYTE		bEnabled;
+	union TWIN		nFreq;
+	float		fFreqCount;
+	BYTE		nMainAddr;
+	BYTE		nWaveTable[0x40];
+	BYTE		bWaveWrite;
+	BYTE		bMain_On;
+
+	/*	Output and Downsampling	*/
+	int16_t		nOutputTable_L[4][0x21][0x40];
+	int32_t			nMixL;
+	
+	/*	Pop Reducer				*/
+	BYTE		bPopReducer;
+	BYTE		nPopOutput;
+	int32_t			nPopCount;
+	
+};
+
+struct FME07Wave
+{
+	/////////////////////////////
+	// Frequency Control
+	union TWIN		nFreqTimer;
+	int32_t			nFreqCount;
+
+	/////////////////////////////
+	// Channel Disabling
+	BYTE		bChannelEnabled;
+
+	/////////////////////////////
+	// Volume
+	BYTE		nVolume;
+
+	/////////////////////////////
+	// Duty Cycle
+	BYTE		nDutyCount;
+
+	///////////////////////////////////
+	// Output and Downsampling
+	int16_t		nOutputTable_L[0x10];
+	int32_t			nMixL;
+};
+
+struct N106Wave
+{
+	//////////////////////////////
+	//  All Channel Stuff
+	BYTE		nActiveChannels;
+	BYTE		bAutoIncrement;
+	BYTE		nCurrentAddress;
+	BYTE		nRAM[0x100];				//internal memory for registers/wave data
+	float		fFrequencyLookupTable[8];	//lookup table for frequency conversions
+
+
+	//////////////////////////////
+	//  Individual channel stuff
+	//////////////////////////////
+	//  Wavelength / Frequency
+	union QUAD		nFreqReg[8];
+	float		fFreqTimer[8];
+	float		fFreqCount[8];
+
+	//////////////////////////////
+	//  Wave data length / remaining
+	BYTE		nWaveSize[8];
+	BYTE		nWaveRemaining[8];
+
+	//////////////////////////////
+	//  Wave data position
+	BYTE		nWavePosStart[8];
+	BYTE		nWavePos[8];
+	BYTE		nOutput[8];
+
+	//////////////////////////////
+	//  Volume
+	BYTE		nVolume[8];
+
+	//////////////////////////////
+	//  Pop Reducer
+	BYTE		nPreVolume[8];
+	BYTE		nPopCheck[8];
+
+	//////////////////////////////
+	// Mixing
+	int16_t		nOutputTable_L[8][0x10][0x10];
+	int32_t			nMixL[8];
+};
+
+struct VRC6PulseWave
+{
+
+	///////////////////////////////////
+	// Frequency Control
+	union TWIN		nFreqTimer;
+	int32_t			nFreqCount;
+
+	///////////////////////////////////
+	// Flags
+	BYTE		bChannelEnabled;
+	BYTE		bDigitized;
+
+	///////////////////////////////////
+	// Volume
+	BYTE		nVolume;
+
+	///////////////////////////////////
+	// Duty Cycle
+	BYTE		nDutyCycle;
+	BYTE		nDutyCount;
+
+	///////////////////////////////////
+	// Output and Downsampling
+	int16_t		nOutputTable_L[0x10];
+	int32_t			nMixL;
+	
+};
+
+struct VRC6SawWave
+{
+
+	///////////////////////////////////
+	// Frequency Control
+	union TWIN		nFreqTimer;
+	int32_t			nFreqCount;
+
+	///////////////////////////////////
+	// Flags
+	BYTE		bChannelEnabled;
+
+	///////////////////////////////////
+	// Phase Accumulator
+	BYTE		nAccumRate;
+	BYTE		nAccum;
+	BYTE		nAccumStep;
+
+	///////////////////////////////////
+	// Output and Downsampling
+	int16_t		nOutputTable_L[0x20];
+	int32_t			nMixL;
+	
+};
+
+struct Wave_Squares
+{
+
+	///////////////////////////////////
+	// Programmable Timer
+	union TWIN	nFreqTimer[2];
+	int32_t			nFreqCount[2];
+
+	///////////////////////////////////
+	// Length Counter
+	BYTE		nLengthCount[2];
+	BYTE		bLengthEnabled[2];
+	BYTE		bChannelEnabled[2];
+
+	///////////////////////////////////
+	// Volume / Decay
+	BYTE		nVolume[2];
+	BYTE		nDecayVolume[2];
+	BYTE		bDecayEnable[2];
+	BYTE		bDecayLoop[2];
+	BYTE		nDecayTimer[2];
+	BYTE		nDecayCount[2];
+
+	///////////////////////////////////
+	// Sweep Unit
+	BYTE		bSweepEnable[2];
+	BYTE		bSweepMode[2];
+	BYTE		bSweepForceSilence[2];
+	BYTE		nSweepTimer[2];
+	BYTE		nSweepCount[2];
+	BYTE		nSweepShift[2];
+
+	///////////////////////////////////
+	// Duty Cycle
+	BYTE		nDutyCount[2];
+	BYTE		nDutyCycle[2];
+
+	///////////////////////////////////
+	// Output and Downsampling
+	BYTE		bChannelMix[2];
+	int16_t		nOutputTable_L[0x100];
+	int32_t			nMixL;
+};
+
+struct Wave_TND
+{
+
+	/*	Triangle			*/
+
+	///////////////////////////////////
+	// Programmable Timer
+	union TWIN		nTriFreqTimer;
+	int32_t			nTriFreqCount;
+
+	///////////////////////////////////
+	// Length Counter
+	BYTE		nTriLengthCount;
+	BYTE		bTriLengthEnabled;
+	BYTE		bTriChannelEnabled;
+
+	///////////////////////////////////
+	// Linear Counter
+	BYTE		nTriLinearCount;
+	BYTE		nTriLinearLoad;
+	BYTE		bTriLinearHalt;
+	BYTE		bTriLinearControl;
+
+	///////////////////////////////////
+	// Tri-Step Generator / Output
+	BYTE		nTriStep;
+	BYTE		nTriOutput;
+	BYTE		bTriChannelMix;
+
+	/*	Noise				*/
+
+	///////////////////////////////////
+	// Programmable Timer
+	WORD		nNoiseFreqTimer;
+	int32_t			nNoiseFreqCount;
+
+	///////////////////////////////////
+	// Length Counter
+	BYTE		nNoiseLengthCount;
+	BYTE		bNoiseLengthEnabled;
+	BYTE		bNoiseChannelEnabled;
+
+	///////////////////////////////////
+	// Volume / Decay
+	BYTE		nNoiseVolume;
+	BYTE		nNoiseDecayVolume;
+	BYTE		bNoiseDecayEnable;
+	BYTE		bNoiseDecayLoop;
+	BYTE		nNoiseDecayTimer;
+	BYTE		nNoiseDecayCount;
+
+	///////////////////////////////////
+	// Random Number Generator
+	WORD		nNoiseRandomShift;
+	BYTE		bNoiseRandomMode;			//1 = 32k, 6 = 93-bit
+	BYTE		bNoiseRandomOut;
+	BYTE		bNoiseChannelMix;
+
+
+	/*	DMC					*/
+
+	///////////////////////////////////
+	// Play Mode
+	BYTE		bDMCLoop;
+	BYTE		bDMCIRQEnabled;
+	BYTE		bDMCIRQPending;
+
+	///////////////////////////////////
+	// Address / DMA
+	BYTE		nDMCDMABank_Load;
+	WORD		nDMCDMAAddr_Load;
+	BYTE		nDMCDMABank;
+	WORD		nDMCDMAAddr;
+	BYTE*		pDMCDMAPtr[8];
+
+	//////////////////////////////////
+	// Length / Input
+	WORD		nDMCLength;
+	WORD		nDMCBytesRemaining;
+	BYTE		nDMCDelta;
+	BYTE		nDMCDeltaBit;
+	BYTE		bDMCDeltaSilent;
+	BYTE		nDMCSampleBuffer;
+	BYTE		bDMCSampleBufferEmpty;
+
+	//////////////////////////////////
+	// Frequency
+	WORD		nDMCFreqTimer;
+	//WORD		nDMCFreqCount;
+	int32_t		nDMCFreqCount;
+
+	//////////////////////////////////
+	// Output
+	BYTE		bDMCActive;
+	BYTE		nDMCOutput;
+	BYTE		bDMCChannelMix;
+
+	/*	All's Output table			*/
+	int16_t*		nOutputTable_L;
+	int32_t			nMixL;
+
+};
+
+/* channels */
+struct Wave_Squares	mWave_Squares;			//Square channels 1 and 2
+struct Wave_TND		mWave_TND;				//Triangle/Noise/DMC channels
+struct VRC6PulseWave	mWave_VRC6Pulse[2];
+struct VRC6SawWave	mWave_VRC6Saw;
+//struct MMC5SquareWave	mWave_MMC5Square[2];
+//struct MMC5VoiceWave	mWave_MMC5Voice;
+struct N106Wave		mWave_N106;
+struct FDSWave		mWave_FDS;
+	
+struct FME07Wave		mWave_FME07[3];			//FME-07's 3 pulse channels
+
+/****************** FDS ******************/
+
+
+
+#define this mWave_FDS
+	
+inline void Wave_FDS_DoTicks(USE_CONST int32_t ticks,USE_CONST BYTE mix);
+inline void Wave_FDS_DoTicks(USE_CONST int32_t ticks,USE_CONST BYTE mix)
+{
+    float freq;
+
+	/*	Volume Envelope Unit	*/
+	if(this.bVolEnv_On)
+	{
+		this.nVolEnv_Count -= ticks;
+		while(this.nVolEnv_Count <= 0)
+		{
+			this.nVolEnv_Count += this.nVolEnv_Timer;
+			if(this.nVolEnv_Mode){	if(this.nVolEnv_Gain < 0x20)	this.nVolEnv_Gain++; }
+			else			{	if(this.nVolEnv_Gain)		this.nVolEnv_Gain--; }
+		}
+	}
+
+	/*	Sweep Envelope Unit	*/
+	if(this.bSweepEnv_On)
+	{
+		this.nSweep_Count -= ticks;
+		while(this.nSweep_Count <= 0)
+		{
+			this.nSweep_Count += this.nSweep_Timer;
+			if(this.nSweep_Mode)	{	if(this.nSweep_Gain < 0x20)	this.nSweep_Gain++; }
+			else			{	if(this.nSweep_Gain)			this.nSweep_Gain--; }
+		}
+	}
+
+	/*	Effector / LFO		*/
+	int32_t		subfreq = 0;
+	if(this.bLFO_On)
+	{
+		this.fLFO_Count -= ticks;
+		while(this.fLFO_Count <= 0)
+		{
+			this.fLFO_Count += this.fLFO_Timer;
+			if(this.nLFO_Table[this.nLFO_Addr] == 4)	this.nSweepBias = 0;
+			else							this.nSweepBias += ModulationTable[ this.nLFO_Table[this.nLFO_Addr] ];
+			this.nLFO_Addr = (this.nLFO_Addr + 1) & 0x3F;
+		}
+
+		while(this.nSweepBias >  63)	this.nSweepBias -= 128;
+		while(this.nSweepBias < -64)	this.nSweepBias += 128;
+
+		register int32_t temp = this.nSweepBias * this.nSweep_Gain;
+		if(temp & 0x0F)
+		{
+			temp /= 16;
+			if(this.nSweepBias < 0)	temp--;
+			else				temp += 2;
+		}
+		else
+			temp /= 16;
+
+		if(temp > 193)	temp -= 258;
+		if(temp < -64)	temp += 256;
+
+		subfreq = this.nFreq.W * temp / 64;
+	}
+
+	/*	Main Unit		*/
+	if(this.bMain_On)
+	{
+		if(mix)
+		{
+			this.nMixL += this.nOutputTable_L[this.nMainVolume][this.nVolume][ this.nWaveTable[this.nMainAddr] ]; // * mn;
+		}
+
+		if((subfreq + this.nFreq.W) > 0)
+		{
+			freq = 65536.0f / (subfreq + this.nFreq.W);
+
+			this.fFreqCount -= ticks;
+			while(this.fFreqCount <= 0)
+			{
+				this.fFreqCount += freq;
+
+				this.nMainAddr = (this.nMainAddr + 1) & 0x3F;
+				this.nPopOutput = this.nWaveTable[this.nMainAddr];
+				if(!this.nMainAddr)
+				{
+					if(this.nVolEnv_Gain < 0x20)		this.nVolume = this.nVolEnv_Gain;
+					else						this.nVolume = 0x20;
+				}
+			}
+		}
+		else
+			this.fFreqCount = this.fLFO_Count;
+	}
+	else if(this.bPopReducer && this.nPopOutput)
+	{
+		if(mix)
+		{
+			this.nMixL += this.nOutputTable_L[this.nMainVolume][this.nVolume][ this.nPopOutput ]; // * mn;
+		}
+
+		this.nPopCount -= ticks;
+		while(this.nPopCount <= 0)
+		{
+			this.nPopCount += 500;
+			this.nPopOutput--;
+			if(!this.nPopOutput)
+				this.nMainAddr = 0;
+		}
+	}
+}
+	
+inline void Wave_FDS_Mix_Mono(int32_t * USE_CONST mix);
+inline void Wave_FDS_Mix_Mono(int32_t * USE_CONST mix)
+{
+	*mix += (this.nMixL);
+	this.nMixL = 0;
+}
+	
+#undef this
+
+/****************** FME-07 ******************/
+
+#define this mWave_FME07[chan]
+
+inline void Wave_FME07_DoTicks(USE_CONST BYTE chan, USE_CONST int32_t ticks, USE_CONST BYTE mix);
+inline void Wave_FME07_DoTicks(USE_CONST BYTE chan, USE_CONST int32_t ticks, USE_CONST BYTE mix)
+{
+
+	if(!this.bChannelEnabled)		return;
+	if(!this.nFreqTimer.W)			return;
+
+	this.nFreqCount -= ticks;
+
+	if(mix && (this.nDutyCount < 16))
+	{
+		this.nMixL += this.nOutputTable_L[this.nVolume]; // * mn;
+	}
+#ifdef USE_MODACCEL
+	if(this.nFreqCount <= 0) {
+    	int32_t cycles = (-this.nFreqCount)/this.nFreqTimer.W+1;
+    	this.nFreqCount = this.nFreqTimer.W - (-this.nFreqCount)%this.nFreqTimer.W;
+
+        this.nDutyCount=(this.nDutyCount+cycles)%32;
+    }
+#else
+	while(this.nFreqCount <= 0) {
+	   this.nFreqCount += this.nFreqTimer.W;
+
+        this.nDutyCount=(this.nDutyCount+1)&0x1f;
+    }
+#endif
+}
+
+inline void Wave_FME07_Mix_Mono(USE_CONST BYTE chan, int32_t * USE_CONST mix);
+inline void Wave_FME07_Mix_Mono(USE_CONST BYTE chan, int32_t * USE_CONST mix)
+{
+	*mix += (this.nMixL);
+	this.nMixL = 0;
+}
+	
+#undef this
+
+/****************** MMC5 ******************/
+/* will include MMC5 sound channels some day, currently only multiply is supported */
+
+/****************** N106 (Disch loves this chip) ******************/
+
+#define this mWave_N106
+	
+inline void Wave_N106_DoTicks(USE_CONST int32_t ticks,USE_CONST BYTE* USE_CONST mix);
+inline void Wave_N106_DoTicks(USE_CONST int32_t ticks,USE_CONST BYTE* USE_CONST mix)
+{
+	register int32_t i;
+
+	for(i = (7 - this.nActiveChannels); i < 8; i++)
+	{
+		if(!this.nFreqReg[i].D)
+		{
+			// written frequency of zero will cause divide by zero error
+			// makes me wonder if the formula was supposed to be Reg+1
+			this.nVolume[i] = this.nPreVolume[i];
+			continue;
+		}
+
+		{
+			if(mix[i])
+			{
+				this.nMixL[i] += this.nOutputTable_L[i][this.nVolume[i]][this.nOutput[i]]; // * mn;
+			}
+
+			if(this.fFreqTimer[i] < 0)
+				this.fFreqTimer[i] = (this.fFrequencyLookupTable[this.nActiveChannels] / this.nFreqReg[i].D);
+			if(this.fFreqCount[i] > this.fFreqTimer[i])
+				this.fFreqCount[i] = this.fFreqTimer[i];
+
+			this.fFreqCount[i] -= ticks;
+			while(this.fFreqCount[i] <= 0)
+			{
+				this.fFreqCount[i] += this.fFreqTimer[i];
+				if(this.nWaveRemaining[i])
+				{
+					this.nWaveRemaining[i]--;
+					this.nWavePos[i]++;
+				}
+				if(!this.nWaveRemaining[i])
+				{
+					this.nWaveRemaining[i] = this.nWaveSize[i];
+					this.nWavePos[i] = this.nWavePosStart[i];
+					if(this.nVolume[i] != this.nPreVolume[i])
+					{
+						if(++this.nPopCheck[i] >= 2)
+						{
+							this.nPopCheck[i] = 0;
+							this.nVolume[i] = this.nPreVolume[i];
+						}
+					}
+				}
+
+				this.nOutput[i] = this.nRAM[this.nWavePos[i]];
+					
+				if(!this.nOutput[i])
+				{
+					this.nPopCheck[i] = 0;
+					this.nVolume[i] = this.nPreVolume[i];
+				}
+					
+			}
+		}
+	}
+}
+
+inline void Wave_N106_Mix_Mono(int32_t * const mix);
+inline void Wave_N106_Mix_Mono(int32_t * const mix)
+{
+	register int32_t i;
+	for(i = 0; i < 8; i++)
+	{
+		*mix += (this.nMixL[i]);
+		this.nMixL[i] = 0;
+	}
+}
+	
+#undef this
+
+/****************** VRC-6 ******************/
+
+#define this mWave_VRC6Pulse[chan]
+
+inline void VRC6Pulse_DoTicks(USE_CONST BYTE chan, USE_CONST int32_t ticks,USE_CONST BYTE mix);
+inline void VRC6Pulse_DoTicks(USE_CONST BYTE chan, USE_CONST int32_t ticks,USE_CONST BYTE mix)
+{
+
+	if(!this.bChannelEnabled)
+		return;
+
+	this.nFreqCount -= ticks;
+
+	if(mix && (this.nDutyCount <= this.nDutyCycle))
+	{
+		this.nMixL += this.nOutputTable_L[this.nVolume];
+	}
+
+#ifdef USE_MODACCEL
+	if(this.nFreqCount <= 0) {
+    	int32_t cycles = (-this.nFreqCount)/(this.nFreqTimer.W+1)+1;
+    	this.nFreqCount = this.nFreqTimer.W + 1 - (-this.nFreqCount)%(this.nFreqTimer.W+1);
+
+    	if(!this.bDigitized)
+    	{
+    		this.nDutyCount = (this.nDutyCount + cycles) & 0x0F;
+    	}
+	}
+#else
+	while(this.nFreqCount <= 0) {
+		this.nFreqCount += this.nFreqTimer.W + 1;
+
+    	if(!this.bDigitized)
+		this.nDutyCount = (this.nDutyCount + 1) & 0x0F;
+	}
+#endif
+}
+
+inline void Wave_VRC6Pulse_Mix_Mono(USE_CONST BYTE chan, int32_t * USE_CONST mix);
+inline void Wave_VRC6Pulse_Mix_Mono(USE_CONST BYTE chan, int32_t * USE_CONST mix)
+{
+	*mix += (this.nMixL);
+	this.nMixL = 0;
+}
+	
+#undef this
+
+#define this mWave_VRC6Saw
+
+inline void VRC6Saw_DoTicks(USE_CONST int32_t ticks, USE_CONST BYTE mix)
+{
+	register int32_t mn;
+
+	mn = min(this.nFreqCount,ticks);
+	mn = ticks;
+
+	this.nFreqCount -= mn;
+
+	if(mix)
+	{
+		this.nMixL += this.nOutputTable_L[this.nAccum >> 3]; // * mn;
+	}
+
+#ifdef USE_MODACCEL
+	if(this.nFreqCount <= 0) {
+		int32_t cycles = (-this.nFreqCount)/(this.nFreqTimer.W+1)+1;
+		this.nFreqCount = this.nFreqTimer.W + 1 - (-this.nFreqCount)%(this.nFreqTimer.W+1);
+
+		this.nAccumStep=(this.nAccumStep+cycles)%14;
+						
+		this.nAccum = this.nAccumStep/2*this.nAccumRate;
+	}
+#else
+	while(this.nFreqCount <= 0) {
+		
+		this.nFreqCount += this.nFreqTimer.W + 1;
+
+		this.nAccumStep++;
+		if(this.nAccumStep == 14)
+		{
+			this.nAccumStep = 0;
+			this.nAccum = 0;
+		}
+		else if(!(this.nAccumStep & 1))
+			this.nAccum += this.nAccumRate;
+	}
+#endif
+
+}
+
+inline void Wave_VRC6Saw_Mix_Mono(int32_t * USE_CONST mix);
+inline void Wave_VRC6Saw_Mix_Mono(int32_t * USE_CONST mix)
+{
+	*mix += (this.nMixL);
+	this.nMixL = 0;
+}
+	
+#undef this
+
+/****************** Square waves ******************/
+
+#define this mWave_Squares
+
+inline void Wave_Squares_ClockMajor(void);
+inline void Wave_Squares_ClockMajor()		//decay
+{
+	if(this.nDecayCount[0])
+		this.nDecayCount[0]--;
+	else
+	{
+		this.nDecayCount[0] = this.nDecayTimer[0];
+		if(this.nDecayVolume[0])
+			this.nDecayVolume[0]--;
+		else
+		{
+			if(this.bDecayLoop[0])
+				this.nDecayVolume[0] = 0x0F;
+		}
+
+		if(this.bDecayEnable[0])
+			this.nVolume[0] = this.nDecayVolume[0];
+	}
+		
+	if(this.nDecayCount[1])
+		this.nDecayCount[1]--;
+	else
+		{
+		this.nDecayCount[1] = this.nDecayTimer[1];
+		if(this.nDecayVolume[1])
+			this.nDecayVolume[1]--;
+		else
+		{
+			if(this.bDecayLoop[1])
+				this.nDecayVolume[1] = 0x0F;
+		}
+
+		if(this.bDecayEnable[1])
+			this.nVolume[1] = this.nDecayVolume[1];
+	}
+		
+}
+
+inline void Wave_Squares_CheckSweepForcedSilence(USE_CONST int32_t i);
+inline void Wave_Squares_CheckSweepForcedSilence(USE_CONST int32_t i)
+{
+	if(this.nFreqTimer[i].W < 8){	this.bSweepForceSilence[i] = 1; return; }
+	if(!this.bSweepMode[i] && (( this.nFreqTimer[i].W + (this.nFreqTimer[i].W >> this.nSweepShift[i])) >= 0x0800))
+		{ this.bSweepForceSilence[i] = 1; return; }
+
+	this.bSweepForceSilence[i] = 0;
+}
+
+inline void Wave_Squares_ClockMinor(void);
+inline void Wave_Squares_ClockMinor()		//sweep / length
+{
+// unrolled a little loop
+//	static int i = 0;
+//	for(i = 0; i < 2; i++)
+//	{
+	if(this.bLengthEnabled[0] && this.nLengthCount[0])
+			this.nLengthCount[0]--;
+
+	if(!this.bSweepEnable[0] || !this.nLengthCount[0] || this.bSweepForceSilence[0] || !this.nSweepShift[0])
+		goto other_square;
+
+	if(this.nSweepCount[0])
+		this.nSweepCount[0]--;
+	else
+	{
+		this.nSweepCount[0] = this.nSweepTimer[0];
+		if(this.bSweepMode[0])	this.nFreqTimer[0].W -= (this.nFreqTimer[0].W >> this.nSweepShift[0]) + 1;
+		else				this.nFreqTimer[0].W += (this.nFreqTimer[0].W >> this.nSweepShift[0]);
+
+		Wave_Squares_CheckSweepForcedSilence(0);
+	}
+		
+	/* */
+other_square:
+	if(this.bLengthEnabled[1] && this.nLengthCount[1])
+		this.nLengthCount[1]--;
+
+	if(!this.bSweepEnable[1] || !this.nLengthCount[1] || this.bSweepForceSilence[1] || !this.nSweepShift[1])
+		return;
+
+	if(this.nSweepCount[1])
+		this.nSweepCount[1]--;
+	else
+	{
+		this.nSweepCount[1] = this.nSweepTimer[1];
+		if(this.bSweepMode[1])	this.nFreqTimer[1].W -= (this.nFreqTimer[1].W >> this.nSweepShift[1]);
+		else				this.nFreqTimer[1].W += (this.nFreqTimer[1].W >> this.nSweepShift[1]);
+
+		Wave_Squares_CheckSweepForcedSilence(1);
+	}
+
+}
+
+inline void Wave_Squares_DoTicks(USE_CONST int32_t ticks);
+inline void Wave_Squares_DoTicks(USE_CONST int32_t ticks)
+{
+	BYTE out;
+
+	this.nFreqCount[0] -= ticks;
+	this.nFreqCount[1] -= ticks;
+
+	if((this.nDutyCount[0] < this.nDutyCycle[0]) && this.nLengthCount[0] && !this.bSweepForceSilence[0] && this.bChannelMix[0])
+		out = (this.nVolume[0] << 4);
+	else
+		out = 0;
+
+	if((this.nDutyCount[1] < this.nDutyCycle[1]) && this.nLengthCount[1] && !this.bSweepForceSilence[1] && this.bChannelMix[1])
+		out |= this.nVolume[1];
+
+	this.nMixL += this.nOutputTable_L[out];// * mn;
+
+#ifdef USE_MODACCEL
+    /* 30% boost on SMB1 track 1 */
+	if(this.nFreqCount[0]<=0)
+	{
+		int32_t cycles = (-this.nFreqCount[0])/(this.nFreqTimer[0].W + 1) + 1;
+		this.nFreqCount[0]=(this.nFreqTimer[0].W + 1)-(-this.nFreqCount[0])%(this.nFreqTimer[0].W + 1);
+		this.nDutyCount[0] = (this.nDutyCount[0]+cycles)%0x10;
+	}
+   	if(this.nFreqCount[1]<=0)
+   	{
+		int32_t cycles = (-this.nFreqCount[1])/(this.nFreqTimer[1].W + 1) + 1;
+		this.nFreqCount[1]=(this.nFreqTimer[1].W + 1)-(-this.nFreqCount[1])%(this.nFreqTimer[1].W + 1);
+		this.nDutyCount[1] = (this.nDutyCount[1]+cycles)%0x10;
+	}
+#else
+    /* 27% boost on SMB1 track 1 */
+	while(this.nFreqCount[0]<=0)
+	{
+    	this.nFreqCount[0] += this.nFreqTimer[0].W + 1;
+		this.nDutyCount[0] = (this.nDutyCount[0]+1)&0xf;
+	}
+   	while(this.nFreqCount[1]<=0)
+   	{
+		this.nFreqCount[1] += this.nFreqTimer[1].W + 1;
+		this.nDutyCount[1] = (this.nDutyCount[1]+1)&0xf;
+	}
+#endif
+}
+
+inline void Wave_Squares_Mix_Mono(int32_t * USE_CONST mix);
+inline void Wave_Squares_Mix_Mono(int32_t * USE_CONST mix)
+{
+	*mix += (this.nMixL);
+	this.nMixL = 0;
+}
+	
+#undef this
+
+/****************** Triangle/noise/DMC ******************/
+
+#define this mWave_TND
+
+inline void Wave_TND_ClockMajor(void);
+inline void Wave_TND_ClockMajor()		// decay (noise), linear (tri)
+{
+	//noise's decay
+	if(this.nNoiseDecayCount)
+		this.nNoiseDecayCount--;
+	else
+	{
+		this.nNoiseDecayCount = this.nNoiseDecayTimer;
+		if(this.nNoiseDecayVolume)
+			this.nNoiseDecayVolume--;
+		else
+		{
+			if(this.bNoiseDecayLoop)
+				this.nNoiseDecayVolume = 0x0F;
+		}
+
+		if(this.bNoiseDecayEnable)
+    		this.nNoiseVolume = this.nNoiseDecayVolume;
+	}
+
+	//triangle's linear
+	if(this.bTriLinearHalt)
+		this.nTriLinearCount = this.nTriLinearLoad;
+	else if(this.nTriLinearCount)
+		this.nTriLinearCount--;
+
+	if(!this.bTriLinearControl)
+		this.bTriLinearHalt = 0;
+}
+
+inline void Wave_TND_ClockMinor(void);
+inline void Wave_TND_ClockMinor()		//length
+{
+	if(this.bNoiseLengthEnabled && this.nNoiseLengthCount)
+		this.nNoiseLengthCount--;
+		
+	if(this.bTriLengthEnabled && this.nTriLengthCount)
+		this.nTriLengthCount--;
+}
+
+inline int32_t Wave_TND_DoTicks(USE_CONST int32_t ticks);
+inline int32_t Wave_TND_DoTicks(USE_CONST int32_t ticks)		//returns number of burned cycles (burned by DMC's DMA)
+{
+    register int32_t out;
+	int32_t burnedcycles = 0;
+	
+	this.nNoiseFreqCount -= ticks;
+			
+	if(this.nTriFreqTimer.W > 8)
+		this.nTriFreqCount -= ticks;
+
+	if(!this.bTriChannelMix)												out = 0;
+	else															out = this.nTriOutput << 11;
+
+	if(this.bNoiseRandomOut && this.nNoiseLengthCount && this.bNoiseChannelMix)	out |= this.nNoiseVolume << 7;
+
+	if(this.bDMCChannelMix)												out |= this.nDMCOutput;
+
+
+	this.nMixL += this.nOutputTable_L[out]; // * mn;
+
+	/*	Tri				*/
+//#ifdef USE_MODACCEL
+#if 0
+    /* 80% boost on Metroid track 5 */
+	if(this.nTriFreqCount<=0)
+	{
+	    int32_t cycles = (-this.nTriFreqCount)/(this.nTriFreqTimer.W + 1) + 1;
+		this.nTriFreqCount = this.nTriFreqTimer.W + 1 - (-this.nTriFreqCount)%(this.nTriFreqTimer.W+1);
+		if(this.nTriLengthCount && this.nTriLinearCount)
+		{
+			this.nTriStep = (this.nTriStep + cycles) & 0x1F;
+
+			if(this.nTriStep & 0x10)		this.nTriOutput = this.nTriStep ^ 0x1F;
+			else					this.nTriOutput = this.nTriStep;
+		}
+	}
+#else
+    /* 75% boost on Metroid track 5 */
+	while(this.nTriFreqCount<=0)
+	{
+		this.nTriFreqCount += this.nTriFreqTimer.W + 1;
+    	if(this.nTriLengthCount && this.nTriLinearCount)
+    	{
+    		this.nTriStep = (this.nTriStep + 1) & 0x1F;
+
+    		if(this.nTriStep & 0x10)		this.nTriOutput = this.nTriStep ^ 0x1F;
+    		else					this.nTriOutput = this.nTriStep;
+    	}
+	}
+#endif
+	/*	Noise			*/
+
+	while(this.nNoiseFreqTimer && this.nNoiseFreqCount<=0)
+	{
+		this.nNoiseFreqCount += this.nNoiseFreqTimer;
+		this.nNoiseRandomShift <<= 1;
+		this.bNoiseRandomOut = (((this.nNoiseRandomShift << this.bNoiseRandomMode) ^ this.nNoiseRandomShift) & 0x8000 ) ? 1 : 0;
+    	if(this.bNoiseRandomOut)
+			this.nNoiseRandomShift |= 0x01;
+
+	}
+
+	/*	DMC				*/
+	if(this.bDMCActive)
+	{
+		this.nDMCFreqCount -= ticks;
+		while (this.nDMCFreqCount <= 0) {
+		    if (!this.bDMCActive) {
+			this.nDMCFreqCount = this.nDMCFreqTimer;
+			break;
+		    }
+
+		this.nDMCFreqCount += this.nDMCFreqTimer;
+
+		if(this.bDMCSampleBufferEmpty && this.nDMCBytesRemaining)
+		{
+			burnedcycles += 4;		//4 cycle burn!
+			this.nDMCSampleBuffer = this.pDMCDMAPtr[this.nDMCDMABank][this.nDMCDMAAddr];
+			this.nDMCDMAAddr++;
+			if(this.nDMCDMAAddr & 0x1000)
+			{
+				this.nDMCDMAAddr &= 0x0FFF;
+				this.nDMCDMABank = (this.nDMCDMABank + 1) & 0x07;
+			}
+
+			this.bDMCSampleBufferEmpty = 0;
+			this.nDMCBytesRemaining--;
+			if(!this.nDMCBytesRemaining)
+			{
+				if(this.bDMCLoop)
+				{
+					this.nDMCDMABank = this.nDMCDMABank_Load;
+					this.nDMCDMAAddr = this.nDMCDMAAddr_Load;
+					this.nDMCBytesRemaining = this.nDMCLength;
+				}
+				else if(this.bDMCIRQEnabled)
+					this.bDMCIRQPending = 1;
+			}
+		}
+
+		if(!this.nDMCDeltaBit)
+		{
+			this.nDMCDeltaBit = 8;
+			this.bDMCDeltaSilent = this.bDMCSampleBufferEmpty;
+			this.nDMCDelta = this.nDMCSampleBuffer;
+			this.bDMCSampleBufferEmpty = 1;
+		}
+				
+		if(this.nDMCDeltaBit)
+		{
+			this.nDMCDeltaBit--;
+			if(!this.bDMCDeltaSilent)
+			{
+				if(this.nDMCDelta & 0x01)
+				{
+					if(this.nDMCOutput < 0x7E) this.nDMCOutput += 2;
+				}
+				else if(this.nDMCOutput > 1)	this.nDMCOutput -= 2;
+			}
+			this.nDMCDelta >>= 1;
+		}
+
+		if(!this.nDMCBytesRemaining && this.bDMCSampleBufferEmpty && this.bDMCDeltaSilent)
+			this.bDMCActive = this.nDMCDeltaBit = 0;
+		}
+	}
+   
+	return burnedcycles;
+}
+	
+inline void Wave_TND_Mix_Mono(int32_t * USE_CONST mix);
+inline void Wave_TND_Mix_Mono(int32_t * USE_CONST mix)
+{
+	*mix += (this.nMixL);
+	this.nMixL = 0;
+}
+
+#undef this
+
+/****************** NSF Core ******************/
+
+/* start globals */
+//
+//	Initialization flags (TODO: make extinct)
+//
+BYTE			bMemoryOK;					//did memory get allocated ok?
+BYTE			bFileLoaded;				//is a file loaded?
+BYTE			bTrackSelected;				//did they select a track?
+BYTE			bIsGeneratingSamples;		//currently generating samples...
+
+/*
+ *	Memory
+ */
+BYTE		pRAM[0x800];			//RAM:		0x0000 - 0x07FF
+BYTE		pSRAM[0x2000];			//SRAM:		0x6000 - 0x7FFF (non-FDS only)
+BYTE		pExRAM[0x1000];			//ExRAM:	0x5C00 - 0x5FF5 (MMC5 only)
+							// Also holds NSF player info (at 0x5000 - 0x500F)
+BYTE*		pROM_Full;		//Full ROM buffer
+
+WORD main_nOutputTable_L[0x8000];
+
+BYTE*		pROM[10];		//ROM banks (point to areas in pROM_Full)
+							//0x8000 - 0xFFFF
+							//also includes 0x6000 - 0x7FFF (FDS only)
+BYTE*		pStack;			//the stack (points to areas in pRAM)
+							//0x0100 - 0x01FF
+
+int32_t			nROMSize;		//size of this ROM file in bytes
+int32_t			nROMBankCount;	//max number of 4k banks
+int32_t			nROMMaxSize;	//size of allocated pROM_Full buffer
+
+/*
+ *	Memory Proc Pointers
+ */
+ 
+typedef BYTE ( *ReadProc)(WORD);
+typedef void ( *WriteProc)(WORD,BYTE);
+ReadProc	ReadMemory[0x10];
+WriteProc	WriteMemory[0x10];
+
+/*
+ *	6502 Registers / Mode
+ */
+
+BYTE		regA;			// Accumulator
+BYTE		regX;			// X-Index
+BYTE		regY;			// Y-Index
+BYTE		regP;			// Processor Status
+BYTE		regSP;			// Stack Pointer
+WORD		regPC;			// Program Counter
+
+BYTE		bPALMode;		// 1 if in PAL emulation mode, 0 if in NTSC
+BYTE		bCPUJammed;		// 0 = not jammed.  1 = really jammed.  2 = 'fake' jammed
+							//  fake jam caused by the NSF code to signal the end of the play/init routine
+
+
+BYTE		nMultIn_Low;	//Multiplication Register, for MMC5 chip only (5205+5206)
+BYTE		nMultIn_High;
+
+
+/*
+ *	NSF Preparation Information
+ */
+
+BYTE		nBankswitchInitValues[10];	//banks to swap to on tune init
+WORD		nPlayAddress;				// Play routine address
+WORD		nInitAddress;				// Init routine address
+
+BYTE		nExternalSound;				// external sound chips
+BYTE		nCurTrack;
+
+float		fNSFPlaybackSpeed;
+
+/*
+ *	pAPU
+ */
+
+BYTE		nFrameCounter;		//Frame Sequence Counter
+BYTE		nFrameCounterMax;	//Frame Sequence Counter Size (3 or 4 depending on $4017.7)
+BYTE		bFrameIRQEnabled;	//TRUE if frame IRQs are enabled
+BYTE		bFrameIRQPending;	//TRUE if the frame sequencer is holding down an IRQ
+
+BYTE		bChannelMix[24];	//Mixing flags for each channel (except the main 5!)
+BYTE		nChannelVol[29];	//Volume settings for each channel
+	
+BYTE			nFME07_Address;
+
+/*
+ *	Timing and Counters
+ */
+/* fixed point -15.16 */
+
+int32_t     nTicksUntilNextFrame;
+int32_t     nTicksPerPlay;
+int32_t     nTicksUntilNextPlay;
+int32_t     nTicksPerSample;
+int32_t     nTicksUntilNextSample;
+
+UINT		nCPUCycle;
+UINT		nAPUCycle;
+UINT		nTotalPlays;			//number of times the play subroutine has been called (for tracking output time)
+
+/*
+ *	Silence Tracker
+ */
+int32_t			nSilentSamples;
+int32_t			nSilentSampleMax;
+int32_t			nSilenceTrackMS;
+BYTE		bNoSilenceIfTime;
+BYTE		bTimeNotDefault;
+
+//
+//	Sound output options
+//
+int32_t				nSampleRate;
+
+//
+//	Volume/fading/filter tracking
+//
+
+float			fMasterVolume;
+
+UINT			nStartFade;					//play call to start fading out
+UINT			nEndFade;					//play call to stop fading out (song is over)
+BYTE			bFade;						//are we fading?
+float			fFadeVolume;
+float			fFadeChange;
+
+/*
+ *	Designated Output Buffer
+ */
+BYTE*		pOutput;
+
+/*
+ *	Misc Options
+ */
+ /*
+BYTE		bDMCPopReducer;					//1 = enabled, 0 = disabled
+BYTE		nDMCPop_Prev;
+BYTE		bDMCPop_Skip;
+BYTE		bDMCPop_SamePlay;
+
+BYTE		nForce4017Write;
+BYTE		bN106PopReducer;
+BYTE		bIgnore4011Writes;
+	
+BYTE		bIgnoreBRK;
+BYTE		bIgnoreIllegalOps;
+BYTE		bNoWaitForReturn;
+BYTE		bPALPreference;
+BYTE		bCleanAXY;
+BYTE		bResetDuty;
+*/
+
+const BYTE		bDMCPopReducer=1;
+BYTE		nDMCPop_Prev=0;
+BYTE		bDMCPop_Skip=0;
+BYTE		bDMCPop_SamePlay=0;
+
+const BYTE		nForce4017Write=0;
+const BYTE		bN106PopReducer=0;
+const BYTE		bIgnore4011Writes=0;
+	
+const BYTE      bIgnoreBRK=0;
+const BYTE      bIgnoreIllegalOps=0;
+const BYTE		bNoWaitForReturn=0;
+const BYTE		bPALPreference=0;
+const BYTE		bCleanAXY=0;
+const BYTE		bResetDuty=0;
+
+/*
+ *	Sound Filter
+ */
+
+__int64		nFilterAccL;
+__int64		nFilterAccR;
+__int64		nFilterAcc2L;
+__int64		nFilterAcc2R;
+__int64		nHighPass;
+__int64		nLowPass;
+
+int32_t			nHighPassBase;
+int32_t			nLowPassBase;
+
+BYTE		bHighPassEnabled;
+BYTE		bLowPassEnabled;
+BYTE		bPrePassEnabled;
+
+int32_t			nSmPrevL;
+int32_t			nSmAccL;
+int32_t			nSmPrevR;
+int32_t			nSmAccR;
+int32_t			nPrePassBase;
+float		fSmDiv;
+
+/* end globals */
+
+#define CLOCK_MAJOR() {	Wave_Squares_ClockMajor(); Wave_TND_ClockMajor(); }
+#define CLOCK_MINOR() {	Wave_Squares_ClockMinor(); Wave_TND_ClockMinor(); }
+/*
+struct NSF_ADVANCEDOPTIONS
+{
+	int			nSilenceTrackMS;
+	int			nInvertCutoffHz;
+	BYTE		bDMCPopReducer;
+	BYTE		nForce4017Write;
+	BYTE		bN106PopReducer;
+	BYTE		bFDSPopReducer;
+
+	BYTE		bIgnore4011Writes;
+	BYTE		bIgnoreBRK;
+	BYTE		bIgnoreIllegalOps;
+	BYTE		bNoWaitForReturn;
+
+	BYTE		bPALPreference;
+	BYTE		bCleanAXY;
+	BYTE		bResetDuty;
+	BYTE		bNoSilenceIfTime;
+
+	BYTE		bHighPassEnabled;
+	BYTE		bLowPassEnabled;
+	BYTE		bPrePassEnabled;
+	int			nHighPassBase;
+	int			nLowPassBase;
+	int			nPrePassBase;
+};
+*/
+#define EXTSOUND_VRC6			0x01
+#define EXTSOUND_VRC7			0x02
+#define EXTSOUND_FDS			0x04
+#define EXTSOUND_MMC5			0x08
+#define EXTSOUND_N106			0x10
+#define EXTSOUND_FME07			0x20
+
+#define SILENCE_THRESHOLD		3
+
+/*
+ *  prototypes
+ */
+ 
+UINT Emulate6502(UINT runto);
+void EmulateAPU(BYTE bBurnCPUCycles);
+
+int		NSFCore_Initialize(void);				//1 = initialized ok, 0 = couldn't initialize (memory allocation error)
+
+//
+//	Song Loading
+//
+int		LoadNSF(int32_t);	//grab data from an existing file  1 = loaded ok, 0 = error loading
+
+//
+//	Track Control
+//
+void	SetTrack(BYTE track);			//Change tracks
+
+//
+//	Getting Samples
+//
+int32_t		GetSamples(BYTE* buffer, int32_t buffersize);	//fill a buffer with samples
+
+//
+//	Playback options
+//
+int		SetPlaybackOptions(int32_t samplerate);				//Set desired playback options (0 = bad options couldn't be set)
+void	SetPlaybackSpeed(float playspersec);							//Speed throttling (0 = uses NSF specified speed)
+void	SetMasterVolume(float vol);										//1 = full volume, 2 = double volume, .5 = half volume, etc
+void	SetChannelOptions(UINT chan,int32_t mix,int32_t vol);	//chan is the zero based index of the channel to change.
+//void	SetAdvancedOptions(const struct NSF_ADVANCEDOPTIONS* opt);				//misc options
+
+float	GetPlaybackSpeed(void);
+float	GetMasterVolume(void);
+//void	GetAdvancedOptions(struct NSF_ADVANCEDOPTIONS* opt);
+
+//
+//	Seeking
+//
+float	GetPlayCalls(void);								//gets the number of 'play' routine calls executed
+UINT	GetWrittenTime(float basedplayspersec);	//gets the output time (based on the given play rate, if basedplayspersec is zero, current playback speed is used
+void	SetPlayCalls(float plays);					//sets the number os 'plays' routines executed (for precise seeking)
+void	SetWrittenTime(UINT ms,float basedplays);	//sets the written time (approx. seeking)
+
+//
+//	Fading
+//
+
+void	StopFade(void);									//stops all fading (plays indefinitely)
+BYTE	SongCompleted(void);							//song has faded out (samples have stopped being generated)
+void	SetFade(int32_t fadestart,int32_t fadestop,BYTE bNotDefault);						//parameters are play calls
+void	SetFadeTime(UINT fadestart,UINT fadestop,float basedplays,BYTE bNotDefault);//parameters are in milliseconds
+
+//
+//	Internal Functions
+//
+void	RebuildOutputTables(UINT chans);			//Rebuilds the output tables (when output options are changed.. like volume, pan, etc)
+void	CalculateChannelVolume(int32_t maxvol,int32_t * left, BYTE vol);	//used in above function
+void	RecalculateFade(void);							//called when fade status is changed.
+void	RecalcFilter(void);
+void	RecalcSilenceTracker(void);
+
+void 		WriteMemory_VRC6(WORD a,BYTE v);
+void 		WriteMemory_MMC5(WORD a,BYTE v);
+void 		WriteMemory_N106(WORD a,BYTE v);
+void 		WriteMemory_FME07(WORD a,BYTE v);
+
+/*
+ *	Memory Read/Write routines
+ */
+BYTE 		ReadMemory_RAM(WORD a)				{ return pRAM[a & 0x07FF]; }
+BYTE 		ReadMemory_ExRAM(WORD a)			{ return pExRAM[a & 0x0FFF]; }
+BYTE 		ReadMemory_SRAM(WORD a)				{ return pSRAM[a & 0x1FFF]; }
+BYTE 		ReadMemory_pAPU(WORD a);
+BYTE 		ReadMemory_ROM(WORD a)				{ return pROM[(a >> 12) - 6][a & 0x0FFF]; }
+BYTE 		ReadMemory_Default(WORD a)			{ return (a >> 8); }
+
+BYTE 		ReadMemory_N106(WORD a);
+
+void 		WriteMemory_RAM(WORD a,BYTE v)		{ pRAM[a & 0x07FF] = v; }
+void 		WriteMemory_ExRAM(WORD a,BYTE v);
+void 		WriteMemory_SRAM(WORD a,BYTE v)		{ pSRAM[a & 0x1FFF] = v; }
+void 		WriteMemory_pAPU(WORD a,BYTE v);
+void 		WriteMemory_FDSRAM(WORD a,BYTE v)	{ pROM[(a >> 12) - 6][a & 0x0FFF] = v; }
+void 		WriteMemory_Default(WORD a,BYTE v)	{ (void)a; (void)v; }
+
+
+//////////////////////////////////////////////////////////////////////////
+//
+//		Read Memory Procs
+//
+
+BYTE  ReadMemory_pAPU(WORD a)
+{
+	EmulateAPU(1);
+
+	if(a == 0x4015)
+	{
+		BYTE ret = 0;
+		if(mWave_Squares.nLengthCount[0])		ret |= 0x01;
+		if(mWave_Squares.nLengthCount[1])		ret |= 0x02;
+		if(mWave_TND.nTriLengthCount)			ret |= 0x04;
+		if(mWave_TND.nNoiseLengthCount)			ret |= 0x08;
+		if(mWave_TND.nDMCBytesRemaining)		ret |= 0x10;
+
+		if(bFrameIRQPending)			ret |= 0x40;
+		if(mWave_TND.bDMCIRQPending)			ret |= 0x80;
+
+		bFrameIRQPending = 0;
+		return ret;
+	}
+
+	if(!(nExternalSound & EXTSOUND_FDS))		return 0x40;
+	if(bPALMode)								return 0x40; //no expansion sound on PAL
+
+	if((a >= 0x4040) && (a <= 0x407F))
+		return mWave_FDS.nWaveTable[a & 0x3F] | 0x40;
+	if(a == 0x4090)
+		return (mWave_FDS.nVolEnv_Gain & 0x3F) | 0x40;
+	if(a == 0x4092)
+		return (mWave_FDS.nSweep_Gain & 0x3F) | 0x40;
+
+	return 0x40;
+}
+
+BYTE  ReadMemory_N106(WORD a)
+{
+	if(a != 0x4800)
+		return ReadMemory_pAPU(a);
+
+    BYTE ret = mWave_N106.nRAM[(mWave_N106.nCurrentAddress << 1)] | (mWave_N106.nRAM[(mWave_N106.nCurrentAddress << 1) + 1] << 4);
+	if(mWave_N106.bAutoIncrement)
+		mWave_N106.nCurrentAddress = (mWave_N106.nCurrentAddress + 1) & 0x7F;
+
+	return ret;
+}
+
+
+//////////////////////////////////////////////////////////////////////////
+//
+//		Write Memory Procs
+//
+
+void  WriteMemory_ExRAM(WORD a,BYTE v)
+{
+	if(a < 0x5FF6)				//Invalid
+		return;
+
+	a -= 0x5FF6;
+
+	// Swap out banks
+
+	EmulateAPU(1);
+	if(v >= nROMBankCount)		//stop it from swapping to a bank that doesn't exist
+		v = 0;
+
+	pROM[a] = pROM_Full + (v << 12);
+
+	// Update the DMC's DMA pointer, as well
+	if(a >= 2)
+		mWave_TND.pDMCDMAPtr[a - 2] = pROM[a];
+}
+
+void  WriteMemory_pAPU(WORD a,BYTE v)
+{
+	EmulateAPU(1);
+	switch(a)
+	{
+		//////////////////////////
+		// Square 1
+	case 0x4000:
+		mWave_Squares.nDutyCycle[0] = DUTY_CYCLE_TABLE[v >> 6];
+		mWave_Squares.bLengthEnabled[0] = !(mWave_Squares.bDecayLoop[0] = (v & 0x20));
+		mWave_Squares.bDecayEnable[0] = !(v & 0x10);
+		mWave_Squares.nDecayTimer[0] = (v & 0x0F);
+
+		if(!mWave_Squares.bDecayEnable[0])
+            mWave_Squares.nVolume[0] = mWave_Squares.nDecayTimer[0];
+		break;
+
+	case 0x4001:
+		mWave_Squares.bSweepEnable[0] = (v & 0x80);
+		mWave_Squares.nSweepTimer[0] = (v & 0x70) >> 4;
+		mWave_Squares.bSweepMode[0] = v & 0x08;
+		mWave_Squares.nSweepShift[0] = v & 0x07;
+		Wave_Squares_CheckSweepForcedSilence(0);
+		break;
+		
+	case 0x4002:
+		mWave_Squares.nFreqTimer[0].B.l = v;
+		Wave_Squares_CheckSweepForcedSilence(0);
+		break;
+		
+	case 0x4003:
+		mWave_Squares.nFreqTimer[0].B.h = v & 0x07;
+		Wave_Squares_CheckSweepForcedSilence(0);
+
+		mWave_Squares.nDecayVolume[0] = 0x0F;
+
+		if(mWave_Squares.bChannelEnabled[0])
+			mWave_Squares.nLengthCount[0] = LENGTH_COUNTER_TABLE[v >> 3];
+
+		if(bResetDuty)
+			mWave_Squares.nDutyCount[0] = 0;
+		break;
+		
+
+		//////////////////////////
+		// Square 2
+	case 0x4004:
+		mWave_Squares.nDutyCycle[1] = DUTY_CYCLE_TABLE[v >> 6];
+		mWave_Squares.bLengthEnabled[1] = !(mWave_Squares.bDecayLoop[1] = (v & 0x20));
+		mWave_Squares.bDecayEnable[1] = !(v & 0x10);
+		mWave_Squares.nDecayTimer[1] = (v & 0x0F);
+
+		if(!mWave_Squares.bDecayEnable[1])
+			mWave_Squares.nVolume[1] = mWave_Squares.nDecayTimer[1];
+		break;
+
+	case 0x4005:
+		mWave_Squares.bSweepEnable[1] = (v & 0x80);
+		mWave_Squares.nSweepTimer[1] = (v & 0x70) >> 4;
+		mWave_Squares.bSweepMode[1] = v & 0x08;
+		mWave_Squares.nSweepShift[1] = v & 0x07;
+		Wave_Squares_CheckSweepForcedSilence(1);
+		break;
+		
+	case 0x4006:
+		mWave_Squares.nFreqTimer[1].B.l = v;
+		Wave_Squares_CheckSweepForcedSilence(1);
+		break;
+		
+	case 0x4007:
+		mWave_Squares.nFreqTimer[1].B.h = v & 0x07;
+		Wave_Squares_CheckSweepForcedSilence(1);
+
+		mWave_Squares.nDecayVolume[1] = 0x0F;
+
+		if(mWave_Squares.bChannelEnabled[1])
+			mWave_Squares.nLengthCount[1] = LENGTH_COUNTER_TABLE[v >> 3];
+
+		if(bResetDuty)
+			mWave_Squares.nDutyCount[1] = 0;
+		break;
+
+		
+		//////////////////////////
+		// Triangle
+	case 0x4008:
+		mWave_TND.nTriLinearLoad = v & 0x7F;
+		mWave_TND.bTriLinearControl = v & 0x80;
+		mWave_TND.bTriLengthEnabled = !(v & 0x80);
+		break;
+
+	case 0x400A:
+		mWave_TND.nTriFreqTimer.B.l = v;
+		break;
+
+	case 0x400B:
+		mWave_TND.nTriFreqTimer.B.h = v & 0x07;
+		mWave_TND.bTriLinearHalt = 1;
+		
+		if(mWave_TND.bTriChannelEnabled)
+			mWave_TND.nTriLengthCount = LENGTH_COUNTER_TABLE[v >> 3];
+		break;
+
+		//////////////////////////
+		// Noise
+	case 0x400C:
+		mWave_TND.bNoiseLengthEnabled = !(mWave_TND.bNoiseDecayLoop = (v & 0x20));
+		mWave_TND.bNoiseDecayEnable = !(v & 0x10);
+		mWave_TND.nNoiseDecayTimer = (v & 0x0F);
+
+		if(mWave_TND.bNoiseDecayEnable)
+			mWave_TND.nNoiseVolume = mWave_TND.nNoiseDecayVolume;
+		else
+			mWave_TND.nNoiseVolume = mWave_TND.nNoiseDecayTimer;
+		break;
+
+	case 0x400E:
+		mWave_TND.nNoiseFreqTimer = NOISE_FREQ_TABLE[v & 0x0F];
+		mWave_TND.bNoiseRandomMode = (v & 0x80) ? 6 : 1;
+		break;
+
+	case 0x400F:
+		if(mWave_TND.bNoiseChannelEnabled)
+			mWave_TND.nNoiseLengthCount = LENGTH_COUNTER_TABLE[v >> 3];
+
+		mWave_TND.nNoiseDecayVolume = 0x0F;
+		if(mWave_TND.bNoiseDecayEnable)
+			mWave_TND.nNoiseVolume = 0x0F;
+		break;
+
+		//////////////////////////
+		// DMC
+	case 0x4010:
+		mWave_TND.bDMCLoop = v & 0x40;
+		mWave_TND.bDMCIRQEnabled = v & 0x80;
+		if(!mWave_TND.bDMCIRQEnabled)
+			mWave_TND.bDMCIRQPending = 0;		//IRQ can't be pending if it's disabled
+
+		mWave_TND.nDMCFreqTimer = DMC_FREQ_TABLE[bPALMode][v & 0x0F];
+		break;
+
+	case 0x4011:
+		if(bIgnore4011Writes)
+			break;
+		v &= 0x7F;
+		if(bDMCPopReducer)
+		{
+			if(bDMCPop_SamePlay)
+				mWave_TND.nDMCOutput = v;
+			else
+			{
+				if(bDMCPop_Skip)
+				{
+					bDMCPop_Skip = 0;
+					break;
+				}
+				if(nDMCPop_Prev == v) break;
+				if(mWave_TND.nDMCOutput == v) break;
+				mWave_TND.nDMCOutput = nDMCPop_Prev;
+				nDMCPop_Prev = v;
+				bDMCPop_SamePlay = 1;
+			}
+		}
+		else
+			mWave_TND.nDMCOutput = v;
+		break;
+
+	case 0x4012:
+		mWave_TND.nDMCDMABank_Load = (v >> 6) | 0x04;
+		mWave_TND.nDMCDMAAddr_Load = (v << 6) & 0x0FFF;
+		break;
+
+	case 0x4013:
+		mWave_TND.nDMCLength = (v << 4) + 1;
+		break;
+
+		//////////////////////////
+		// All / General Purpose
+	case 0x4015:
+		mWave_TND.bDMCIRQPending = 0;
+
+		if(v & 0x01){	mWave_Squares.bChannelEnabled[0] =									1;	}
+		else		{	mWave_Squares.bChannelEnabled[0] = mWave_Squares.nLengthCount[0] =	0;	}
+		if(v & 0x02){	mWave_Squares.bChannelEnabled[1] =									1;	}
+		else		{	mWave_Squares.bChannelEnabled[1] = mWave_Squares.nLengthCount[1] =	0;	}
+		if(v & 0x04){	mWave_TND.bTriChannelEnabled =										1;	}
+		else		{	mWave_TND.bTriChannelEnabled = mWave_TND.nTriLengthCount =			0;	}
+		if(v & 0x08){	mWave_TND.bNoiseChannelEnabled =									1;	}
+		else		{	mWave_TND.bNoiseChannelEnabled = mWave_TND.nNoiseLengthCount =		0;	}
+
+		if(v & 0x10)
+		{
+			if(!mWave_TND.nDMCBytesRemaining)
+			{
+				bDMCPop_Skip = 1;
+				mWave_TND.nDMCDMAAddr = mWave_TND.nDMCDMAAddr_Load;
+				mWave_TND.nDMCDMABank = mWave_TND.nDMCDMABank_Load;
+				mWave_TND.nDMCBytesRemaining = mWave_TND.nDMCLength;
+				mWave_TND.bDMCActive = 1;
+			}
+		}
+		else
+			mWave_TND.nDMCBytesRemaining = 0;
+		break;
+
+	case 0x4017:
+		bFrameIRQEnabled = !(v & 0x40);
+		bFrameIRQPending = 0;
+		nFrameCounter = 0;
+		nFrameCounterMax = (v & 0x80) ? 4 : 3;
+		nTicksUntilNextFrame = (bPALMode ? PAL_FRAME_COUNTER_FREQ : NTSC_FRAME_COUNTER_FREQ) * 0x10000;
+
+		CLOCK_MAJOR();
+		if(v & 0x80) CLOCK_MINOR();
+		break;
+	}
+
+	if(!(nExternalSound & EXTSOUND_FDS))		return;
+	if(bPALMode)								return;	//no expansion sound on PAL
+
+	//////////////////////////////////////////////////////////////////////////
+	//   FDS Sound registers
+
+	if(a < 0x4040)		return;
+
+	// wave table
+	if(a <= 0x407F)
+	{
+		if(mWave_FDS.bWaveWrite)
+			mWave_FDS.nWaveTable[a - 0x4040] = v;
+	}
+	else
+	{
+		switch(a)
+		{
+		case 0x4080:
+			mWave_FDS.nVolEnv_Mode = (v >> 6);
+			if(v & 0x80)
+			{
+				mWave_FDS.nVolEnv_Gain = v & 0x3F;
+				if(!mWave_FDS.nMainAddr)
+				{
+					if(mWave_FDS.nVolEnv_Gain < 0x20)	mWave_FDS.nVolume = mWave_FDS.nVolEnv_Gain;
+					else								mWave_FDS.nVolume = 0x20;
+				}
+			}
+			mWave_FDS.nVolEnv_Decay = v & 0x3F;
+			mWave_FDS.nVolEnv_Timer = ((mWave_FDS.nVolEnv_Decay + 1) * mWave_FDS.nEnvelopeSpeed * 8);
+
+			mWave_FDS.bVolEnv_On = mWave_FDS.bEnvelopeEnable && mWave_FDS.nEnvelopeSpeed && !(v & 0x80);
+			break;
+
+		case 0x4082:
+			mWave_FDS.nFreq.B.l = v;
+			mWave_FDS.bMain_On = mWave_FDS.nFreq.W && mWave_FDS.bEnabled && !mWave_FDS.bWaveWrite;
+			break;
+
+		case 0x4083:
+			mWave_FDS.bEnabled =		!(v & 0x80);
+			mWave_FDS.bEnvelopeEnable = !(v & 0x40);
+			if(v & 0x80)
+			{
+				if(mWave_FDS.nVolEnv_Gain < 0x20)	mWave_FDS.nVolume = mWave_FDS.nVolEnv_Gain;
+				else								mWave_FDS.nVolume = 0x20;
+			}
+			mWave_FDS.nFreq.B.h = v & 0x0F;
+			mWave_FDS.bMain_On = mWave_FDS.nFreq.W && mWave_FDS.bEnabled && !mWave_FDS.bWaveWrite;
+
+			mWave_FDS.bVolEnv_On = mWave_FDS.bEnvelopeEnable && mWave_FDS.nEnvelopeSpeed && !(mWave_FDS.nVolEnv_Mode & 2);
+			mWave_FDS.bSweepEnv_On = mWave_FDS.bEnvelopeEnable && mWave_FDS.nEnvelopeSpeed && !(mWave_FDS.nSweep_Mode & 2);
+			break;
+
+
+		case 0x4084:
+			mWave_FDS.nSweep_Mode = v >> 6;
+			if(v & 0x80)
+				mWave_FDS.nSweep_Gain = v & 0x3F;
+			mWave_FDS.nSweep_Decay = v & 0x3F;
+			mWave_FDS.nSweep_Timer = ((mWave_FDS.nSweep_Decay + 1) * mWave_FDS.nEnvelopeSpeed * 8);
+			mWave_FDS.bSweepEnv_On = mWave_FDS.bEnvelopeEnable && mWave_FDS.nEnvelopeSpeed && !(v & 0x80);
+			break;
+
+
+		case 0x4085:
+			if(v & 0x40)	mWave_FDS.nSweepBias = (v & 0x3F) - 0x40;
+			else			mWave_FDS.nSweepBias = v & 0x3F;
+			mWave_FDS.nLFO_Addr = 0;
+			break;
+
+
+		case 0x4086:
+			mWave_FDS.nLFO_Freq.B.l = v;
+			mWave_FDS.bLFO_On = mWave_FDS.bLFO_Enabled && mWave_FDS.nLFO_Freq.W;
+			if(mWave_FDS.nLFO_Freq.W)
+				mWave_FDS.fLFO_Timer = 65536.0f / mWave_FDS.nLFO_Freq.W;
+			break;
+
+		case 0x4087:
+			mWave_FDS.bLFO_Enabled = !(v & 0x80);
+			mWave_FDS.nLFO_Freq.B.h = v & 0x0F;
+			mWave_FDS.bLFO_On = mWave_FDS.bLFO_Enabled && mWave_FDS.nLFO_Freq.W;
+			if(mWave_FDS.nLFO_Freq.W)
+				mWave_FDS.fLFO_Timer = 65536.0f / mWave_FDS.nLFO_Freq.W;
+			break;
+
+		case 0x4088:
+			if(mWave_FDS.bLFO_Enabled)	break;
+			register int32_t i;
+			for(i = 0; i < 62; i++)
+				mWave_FDS.nLFO_Table[i] = mWave_FDS.nLFO_Table[i + 2];
+			mWave_FDS.nLFO_Table[62] = mWave_FDS.nLFO_Table[63] = v & 7;
+			break;
+
+		case 0x4089:
+			mWave_FDS.nMainVolume = v & 3;
+			mWave_FDS.bWaveWrite = v & 0x80;
+			mWave_FDS.bMain_On = mWave_FDS.nFreq.W && mWave_FDS.bEnabled && !mWave_FDS.bWaveWrite;
+			break;
+
+		case 0x408A:
+			mWave_FDS.nEnvelopeSpeed = v;
+			mWave_FDS.bVolEnv_On = mWave_FDS.bEnvelopeEnable && mWave_FDS.nEnvelopeSpeed && !(mWave_FDS.nVolEnv_Mode & 2);
+			mWave_FDS.bSweepEnv_On = mWave_FDS.bEnvelopeEnable && mWave_FDS.nEnvelopeSpeed && !(mWave_FDS.nSweep_Mode & 2);
+			break;
+		}
+	}
+}
+
+void  WriteMemory_VRC6(WORD a,BYTE v)
+{
+	EmulateAPU(1);
+
+	if((a < 0xA000) && (nExternalSound & EXTSOUND_VRC7)) return;
+	else if(nExternalSound & EXTSOUND_FDS)
+		WriteMemory_FDSRAM(a,v);
+
+	switch(a)
+	{
+		//////////////////////////
+		// Pulse 1
+	case 0x9000:
+		mWave_VRC6Pulse[0].nVolume = v & 0x0F;
+		mWave_VRC6Pulse[0].nDutyCycle = (v >> 4) & 0x07;
+		mWave_VRC6Pulse[0].bDigitized = v & 0x80;
+		if(mWave_VRC6Pulse[0].bDigitized)
+			mWave_VRC6Pulse[0].nDutyCount = 0;
+		break;
+
+	case 0x9001:
+		mWave_VRC6Pulse[0].nFreqTimer.B.l = v;
+		break;
+
+	case 0x9002:
+		mWave_VRC6Pulse[0].nFreqTimer.B.h = v & 0x0F;
+		mWave_VRC6Pulse[0].bChannelEnabled = v & 0x80;
+		break;
+		
+
+		//////////////////////////
+		// Pulse 2
+	case 0xA000:
+		mWave_VRC6Pulse[1].nVolume = v & 0x0F;
+		mWave_VRC6Pulse[1].nDutyCycle = (v >> 4) & 0x07;
+		mWave_VRC6Pulse[1].bDigitized = v & 0x80;
+		if(mWave_VRC6Pulse[1].bDigitized)
+			mWave_VRC6Pulse[1].nDutyCount = 0;
+		break;
+
+	case 0xA001:
+		mWave_VRC6Pulse[1].nFreqTimer.B.l = v;
+		break;
+
+	case 0xA002:
+		mWave_VRC6Pulse[1].nFreqTimer.B.h = v & 0x0F;
+		mWave_VRC6Pulse[1].bChannelEnabled = v & 0x80;
+		break;
+		
+		//////////////////////////
+		// Sawtooth
+	case 0xB000:
+		mWave_VRC6Saw.nAccumRate = (v & 0x3F);
+		break;
+
+	case 0xB001:
+		mWave_VRC6Saw.nFreqTimer.B.l = v;
+		break;
+
+	case 0xB002:
+		mWave_VRC6Saw.nFreqTimer.B.h = v & 0x0F;
+		mWave_VRC6Saw.bChannelEnabled = v & 0x80;
+		break;
+	}
+}
+
+void  WriteMemory_MMC5(WORD a,BYTE v)
+{
+	if((a <= 0x5015) && !bPALMode)
+	{
+		/*EmulateAPU(1);
+		switch(a)
+		{
+			//////////////////////////
+			// Square 1
+		case 0x5000:
+			mWave_MMC5Square[0].nDutyCycle = DUTY_CYCLE_TABLE[v >> 6];
+			mWave_MMC5Square[0].bLengthEnabled = !(mWave_MMC5Square[0].bDecayLoop = (v & 0x20));
+			mWave_MMC5Square[0].bDecayEnable = !(v & 0x10);
+			mWave_MMC5Square[0].nDecayTimer = (v & 0x0F);
+
+			if(!mWave_MMC5Square[0].bDecayEnable)
+				mWave_MMC5Square[0].nVolume = mWave_MMC5Square[0].nDecayTimer;
+			break;
+		
+		case 0x5002:
+			mWave_MMC5Square[0].nFreqTimer.B.l = v;
+			break;
+		
+		case 0x5003:
+			mWave_MMC5Square[0].nFreqTimer.B.h = v & 0x07;
+			mWave_MMC5Square[0].nDecayVolume = 0x0F;
+
+			if(mWave_MMC5Square[0].bChannelEnabled)
+				mWave_MMC5Square[0].nLengthCount = LENGTH_COUNTER_TABLE[v >> 3];
+			break;
+			
+			//////////////////////////
+			// Square 2
+		case 0x5004:
+			mWave_MMC5Square[1].nDutyCycle = DUTY_CYCLE_TABLE[v >> 6];
+			mWave_MMC5Square[1].bLengthEnabled = !(mWave_MMC5Square[1].bDecayLoop = (v & 0x20));
+			mWave_MMC5Square[1].bDecayEnable = !(v & 0x10);
+			mWave_MMC5Square[1].nDecayTimer = (v & 0x0F);
+
+			if(!mWave_MMC5Square[1].bDecayEnable)
+				mWave_MMC5Square[1].nVolume = mWave_MMC5Square[1].nDecayTimer;
+			break;
+		
+		case 0x5006:
+			mWave_MMC5Square[1].nFreqTimer.B.l = v;
+			break;
+		
+		case 0x5007:
+			mWave_MMC5Square[1].nFreqTimer.B.h = v & 0x07;
+			mWave_MMC5Square[1].nDecayVolume = 0x0F;
+
+			if(mWave_MMC5Square[1].bChannelEnabled)
+				mWave_MMC5Square[1].nLengthCount = LENGTH_COUNTER_TABLE[v >> 3];
+			break;
+
+		case 0x5011:
+			mWave_MMC5Voice.nOutput = v & 0x7F;
+			break;
+		
+
+		case 0x5015:
+			if(v & 0x01){	mWave_MMC5Square[0].bChannelEnabled =										1;	}
+			else		{	mWave_MMC5Square[0].bChannelEnabled = mWave_MMC5Square[0].nLengthCount =	0;	}
+			if(v & 0x02){	mWave_MMC5Square[1].bChannelEnabled =										1;	}
+			else		{	mWave_MMC5Square[1].bChannelEnabled = mWave_MMC5Square[1].nLengthCount =	0;	}
+			break;
+		}
+		*/
+		return;
+	}
+
+	if(a == 0x5205)
+	{
+		nMultIn_Low = v;
+		goto multiply;
+	}
+	if(a == 0x5206)
+	{
+		nMultIn_High = v;
+multiply:
+		a = nMultIn_Low * nMultIn_High;
+		pExRAM[0x205] = a & 0xFF;
+		pExRAM[0x206] = a >> 8;
+		return;
+	}
+
+	if(a < 0x5C00) return;
+
+	pExRAM[a & 0x0FFF] = v;
+	if(a >= 0x5FF6)
+		WriteMemory_ExRAM(a,v);
+}
+
+void  WriteMemory_N106(WORD a,BYTE v)
+{
+	if(a < 0x4800)
+	{
+		WriteMemory_pAPU(a,v);
+		return;
+	}
+
+	if(a == 0xF800)
+	{
+		mWave_N106.nCurrentAddress = v & 0x7F;
+		mWave_N106.bAutoIncrement = (v & 0x80);
+		return;
+	}
+
+	if(a == 0x4800)
+	{
+		EmulateAPU(1);
+		mWave_N106.nRAM[mWave_N106.nCurrentAddress << 1] = v & 0x0F;
+		mWave_N106.nRAM[(mWave_N106.nCurrentAddress << 1) + 1] = v >> 4;
+		a = mWave_N106.nCurrentAddress;
+		if(mWave_N106.bAutoIncrement)
+			mWave_N106.nCurrentAddress = (mWave_N106.nCurrentAddress + 1) & 0x7F;
+
+#define N106REGWRITE(ch,r0,r1,r2,r3,r4)							\
+	case r0:	if(mWave_N106.nFreqReg[ch].B.l == v) break;		\
+				mWave_N106.nFreqReg[ch].B.l = v;				\
+				mWave_N106.fFreqTimer[ch] = -1.0f;				\
+				break;											\
+	case r1:	if(mWave_N106.nFreqReg[ch].B.h == v) break;		\
+				mWave_N106.nFreqReg[ch].B.h = v;				\
+				mWave_N106.fFreqTimer[ch] = -1.0f;				\
+				break;											\
+	case r2:	if(mWave_N106.nFreqReg[ch].B.w != (v & 3)){		\
+					mWave_N106.nFreqReg[ch].B.w = v & 0x03;		\
+					mWave_N106.fFreqTimer[ch] = -1.0f;}			\
+				mWave_N106.nWaveSize[ch] = 0x20 - (v & 0x1C);	\
+				break;											\
+	case r3:	mWave_N106.nWavePosStart[ch] = v;				\
+				break;											\
+	case r4:	mWave_N106.nPreVolume[ch] = v & 0x0F;			\
+				if(!bN106PopReducer)							\
+					mWave_N106.nVolume[ch] = v & 0x0F
+
+		switch(a)
+		{
+			N106REGWRITE(0,0x40,0x42,0x44,0x46,0x47); break;
+			N106REGWRITE(1,0x48,0x4A,0x4C,0x4E,0x4F); break;
+			N106REGWRITE(2,0x50,0x52,0x54,0x56,0x57); break;
+			N106REGWRITE(3,0x58,0x5A,0x5C,0x5E,0x5F); break;
+			N106REGWRITE(4,0x60,0x62,0x64,0x66,0x67); break;
+			N106REGWRITE(5,0x68,0x6A,0x6C,0x6E,0x6F); break;
+			N106REGWRITE(6,0x70,0x72,0x74,0x76,0x77); break;
+			N106REGWRITE(7,0x78,0x7A,0x7C,0x7E,0x7F);
+				v = (v >> 4) & 7;
+				if(mWave_N106.nActiveChannels == v) break;
+				mWave_N106.nActiveChannels = v;
+				mWave_N106.fFreqTimer[0] = -1.0f;
+				mWave_N106.fFreqTimer[1] = -1.0f;
+				mWave_N106.fFreqTimer[2] = -1.0f;
+				mWave_N106.fFreqTimer[3] = -1.0f;
+				mWave_N106.fFreqTimer[4] = -1.0f;
+				mWave_N106.fFreqTimer[5] = -1.0f;
+				mWave_N106.fFreqTimer[6] = -1.0f;
+				mWave_N106.fFreqTimer[7] = -1.0f;
+				break;
+		}
+#undef N106REGWRITE
+	}
+}
+
+void WriteMemory_FME07(WORD a,BYTE v)
+{
+	if((a < 0xD000) && (nExternalSound & EXTSOUND_FDS))
+		WriteMemory_FDSRAM(a,v);
+
+	if(a == 0xC000)
+		nFME07_Address = v;
+	if(a == 0xE000)
+	{
+		switch(nFME07_Address)
+		{
+		case 0x00:	mWave_FME07[0].nFreqTimer.B.l = v;			break;
+		case 0x01:	mWave_FME07[0].nFreqTimer.B.h = v & 0x0F;	break;
+		case 0x02:	mWave_FME07[1].nFreqTimer.B.l = v;			break;
+		case 0x03:	mWave_FME07[1].nFreqTimer.B.h = v & 0x0F;	break;
+		case 0x04:	mWave_FME07[2].nFreqTimer.B.l = v;			break;
+		case 0x05:	mWave_FME07[2].nFreqTimer.B.h = v & 0x0F;	break;
+		case 0x07:
+			mWave_FME07[0].bChannelEnabled = !(v & 0x01);
+			mWave_FME07[1].bChannelEnabled = !(v & 0x02);
+			mWave_FME07[2].bChannelEnabled = !(v & 0x03);
+			break;
+		case 0x08:	mWave_FME07[0].nVolume = v & 0x0F; break;
+		case 0x09:	mWave_FME07[1].nVolume = v & 0x0F; break;
+		case 0x0A:	mWave_FME07[2].nVolume = v & 0x0F; break;
+		}
+	}
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+//		Emulate APU
+//
+
+int32_t fulltick;
+void EmulateAPU(BYTE bBurnCPUCycles)
+{
+	int32_t tick;
+   	__int64 diff;
+   	int32_t dif;
+   	
+	fulltick += (signed)(nCPUCycle - nAPUCycle);
+
+	int32_t mixL;
+
+	if(bFade && nSilentSampleMax && (nSilentSamples >= nSilentSampleMax))
+		fulltick = 0;
+
+	while(fulltick>0)
+	{
+		tick = (nTicksUntilNextSample+0xffff)>>16;
+
+		//tick = min(tick,fulltick);
+
+		fulltick -= tick;
+
+		// Sample Generation
+
+		Wave_Squares_DoTicks(tick);
+		mixL = Wave_TND_DoTicks(tick);
+
+		if(nExternalSound && !bPALMode)
+		{
+			if(nExternalSound & EXTSOUND_VRC6)
+			{
+				VRC6Pulse_DoTicks(0,tick,bChannelMix[0]);
+				VRC6Pulse_DoTicks(1,tick,bChannelMix[1]);
+				VRC6Saw_DoTicks(tick,bChannelMix[2]);
+			}
+			/*
+			if(nExternalSound & EXTSOUND_MMC5)
+			{
+				Wave_MMC5Square_DoTicks(0,tick,bChannelMix[3]);
+				Wave_MMC5Square_DoTicks(1,tick,bChannelMix[4]);
+				if(bChannelMix[5]) Wave_MMC5Voice_DoTicks(tick);
+			}
+			*/
+			if(nExternalSound & EXTSOUND_N106)
+				Wave_N106_DoTicks(tick,&bChannelMix[6]);
+			if(nExternalSound & EXTSOUND_FME07)
+			{
+				Wave_FME07_DoTicks(0,tick,bChannelMix[20]);
+				Wave_FME07_DoTicks(1,tick,bChannelMix[21]);
+				Wave_FME07_DoTicks(2,tick,bChannelMix[22]);
+			}
+			if(nExternalSound & EXTSOUND_FDS)
+				Wave_FDS_DoTicks(tick,bChannelMix[23]);
+		}
+
+
+		if(bBurnCPUCycles)
+		{
+			nCPUCycle += mixL;
+			fulltick += mixL;
+		}
+
+		
+		// Frame Sequencer
+
+		nTicksUntilNextFrame -= tick<<16;
+		while(nTicksUntilNextFrame <= 0)
+		{
+    		nTicksUntilNextFrame += (bPALMode ? PAL_FRAME_COUNTER_FREQ : NTSC_FRAME_COUNTER_FREQ) * 0x10000;
+			nFrameCounter++;
+			if(nFrameCounter > nFrameCounterMax)
+				nFrameCounter = 0;
+
+			if(nFrameCounterMax == 4)
+			{
+				if(nFrameCounter < 4)
+				{
+					CLOCK_MAJOR();
+					if(!(nFrameCounter & 1))
+						CLOCK_MINOR();
+				}
+			}
+			else
+			{
+				CLOCK_MAJOR();
+				if(nFrameCounter & 1)
+					CLOCK_MINOR();
+
+				if((nFrameCounter == 3) && bFrameIRQEnabled)
+					bFrameIRQPending = 1;
+			}
+		}
+
+		//if(!pOutput)
+		//	continue;
+
+		nTicksUntilNextSample -= tick<<16;
+		if(nTicksUntilNextSample <= 0)
+		{
+			nTicksUntilNextSample += nTicksPerSample;
+			//if(!nDownsample) continue;
+			//if(nDownsample)
+			//{
+				mixL = 0;
+				Wave_Squares_Mix_Mono(&mixL);
+				Wave_TND_Mix_Mono(&mixL);
+
+				if(nExternalSound && !bPALMode)
+				{
+					if(nExternalSound & EXTSOUND_VRC6)
+					{
+						Wave_VRC6Pulse_Mix_Mono(0,&mixL);
+						Wave_VRC6Pulse_Mix_Mono(1,&mixL);
+						Wave_VRC6Saw_Mix_Mono(&mixL);
+					}
+					/*
+					if(nExternalSound & EXTSOUND_MMC5)
+					{
+						Wave_MMC5Square_Mix_Mono(0,&mixL,nDownsample);
+						Wave_MMC5Square_Mix_Mono(1,&mixL,nDownsample);
+						Wave_MMC5Voice_Mix_Mono(&mixL,nDownsample);
+					}
+					*/
+					if(nExternalSound & EXTSOUND_N106)
+						Wave_N106_Mix_Mono(&mixL);
+					if(nExternalSound & EXTSOUND_FME07)
+					{
+						Wave_FME07_Mix_Mono(0,&mixL);
+						Wave_FME07_Mix_Mono(1,&mixL);
+						Wave_FME07_Mix_Mono(2,&mixL);
+					}
+					if(nExternalSound & EXTSOUND_FDS)
+						Wave_FDS_Mix_Mono(&mixL);
+				}
+
+				/*	Filter	*/
+                //{
+
+				    if(bPrePassEnabled)
+				    {
+					   dif = mixL - nSmPrevL;
+					   nSmAccL -= dif;
+					   nSmPrevL = mixL;
+					   nSmAccL -= (int32_t)(nSmAccL * fSmDiv);
+					   mixL += nSmAccL;
+				    }
+
+				    diff = ((__int64)mixL << 25) - nFilterAccL;
+				    if(bHighPassEnabled)
+				    	nFilterAccL += (diff * nHighPass) >> 16;
+			     	if(bLowPassEnabled)
+				    {
+					   nFilterAcc2L += ((diff - nFilterAcc2L) * nLowPass) >> 16;
+					   mixL = (int32_t)((nFilterAcc2L) >> (23));
+				    }
+	   			     else
+    					mixL = (int32_t)(diff >> 23);
+                //}
+				/*	End Filter	*/
+				
+				if(bFade && (fFadeVolume < 1))
+					mixL = (int32_t)(mixL * fFadeVolume);
+
+				if(mixL < -32768)	mixL = -32768;
+				if(mixL >  32767)	mixL =  32767;
+
+				*((WORD*)pOutput) = (WORD)mixL;
+				pOutput += 2;
+			//}
+
+		}
+	}
+
+	nAPUCycle = nCPUCycle;
+}
+
+
+/*
+ *	Initialize
+ *
+ *		Initializes Memory
+ */
+
+int NSFCore_Initialize()
+{
+	int32_t i;
+	/* clear globals */
+	/* why, yes, this was easier when they were in a struct */
+	
+	//
+    //	Initialization flags
+    bMemoryOK=0;
+    bFileLoaded=0;
+    bTrackSelected=0;
+    bIsGeneratingSamples=0;
+
+    /*
+     *	Memory
+     */
+
+	 ZeroMemory(pRAM,0x800);
+	 ZeroMemory(pSRAM,0x2000);
+	 ZeroMemory(pExRAM,0x1000);
+     pROM_Full=0;
+
+	 ZeroMemory(main_nOutputTable_L,0x8000);
+
+    ZeroMemory(pROM,10);
+    pStack=0;
+
+    nROMSize=0;
+    nROMBankCount=0;
+    nROMMaxSize=0;
+
+    /*
+     *	Memory Proc Pointers
+     */
+
+	 ZeroMemory(ReadMemory,sizeof(ReadProc)*0x10);
+	 ZeroMemory(WriteMemory,sizeof(WriteProc)*0x10);
+	
+    /*
+     *	6502 Registers / Mode
+     */
+
+    regA=0;
+    regX=0;
+    regY=0;
+    regP=0;
+    regSP=0;
+    regPC=0;
+
+    bPALMode=0;
+    bCPUJammed=0;
+
+    nMultIn_Low=0;
+    nMultIn_High=0;
+
+    /*
+     *	NSF Preparation Information
+     */
+
+    //ZeroVar(nBankswitchInitValues);
+	 ZeroMemory(nBankswitchInitValues,10);
+    nPlayAddress=0;
+    nInitAddress=0;
+
+    nExternalSound=0;
+    nCurTrack=0;
+
+    fNSFPlaybackSpeed=0;
+
+    /*
+    *	pAPU
+     */
+
+    nFrameCounter=0;
+    nFrameCounterMax=0;
+    bFrameIRQEnabled=0;
+    bFrameIRQPending=0;
+
+    ZeroMemory(bChannelMix,24);
+    ZeroMemory(nChannelVol,29);
+	
+    /*
+     *	Timing and Counters
+     */
+    /*
+    fTicksUntilNextFrame=0;
+
+    fTicksPerPlay=0;
+    fTicksUntilNextPlay=0;
+
+    fTicksPerSample=0;
+    fTicksUntilNextSample=0;
+    */
+    nTicksUntilNextFrame=0;
+
+    nTicksPerPlay=0;
+    nTicksUntilNextPlay=0;
+
+    nTicksPerSample=0;
+    nTicksUntilNextSample=0;
+
+    nCPUCycle=0;
+    nAPUCycle=0;
+    nTotalPlays=0;
+
+    /*
+     *	Silence Tracker
+     */
+    nSilentSamples=0;
+    nSilentSampleMax=0;
+    nSilenceTrackMS=0;
+    bNoSilenceIfTime=0;
+    bTimeNotDefault=0;
+
+    //
+    //	Sound output options
+    //
+    nSampleRate=0;
+
+    //
+    //	Volume/fading/filter tracking
+    //
+
+    fMasterVolume=0;
+
+    nStartFade=0;
+    nEndFade=0;
+    bFade=0;
+    fFadeVolume=0;
+    fFadeChange=0;
+
+    pOutput=0;
+    //nDownsample=0;
+
+    //bDMCPopReducer=0;
+    nDMCPop_Prev=0;
+    bDMCPop_Skip=0;
+    bDMCPop_SamePlay=0;
+    /*
+    nForce4017Write=0;
+    bN106PopReducer=0;
+    bIgnore4011Writes=0;
+	
+    bIgnoreBRK=0;
+    bIgnoreIllegalOps=0;
+    bNoWaitForReturn=0;
+    bPALPreference=0;
+    bCleanAXY=0;
+    bResetDuty=0;
+    */
+
+    /*
+     *	Sound Filter
+     */
+
+    nFilterAccL=0;
+    nFilterAccR=0;
+    nFilterAcc2L=0;
+    nFilterAcc2R=0;
+    nHighPass=0;
+    nLowPass=0;
+
+    nHighPassBase=0;
+    nLowPassBase=0;
+
+    bHighPassEnabled=0;
+    bLowPassEnabled=0;
+    bPrePassEnabled=0;
+
+    nSmPrevL=0;
+    nSmAccL=0;
+    nSmPrevR=0;
+    nSmAccR=0;
+    nPrePassBase=0;
+    fSmDiv=0;
+
+    /* channels */
+    //ZeroVar(mWave_MMC5Square);
+    //ZeroVar(mWave_MMC5Voice);
+	
+    ZeroMemory(&mWave_Squares,sizeof(struct Wave_Squares));
+    ZeroMemory(&mWave_TND,sizeof(struct Wave_TND));
+    ZeroMemory(mWave_VRC6Pulse,sizeof(struct VRC6PulseWave)*2);
+	 ZeroMemory(&mWave_VRC6Saw,sizeof(struct VRC6SawWave));
+    ZeroMemory(&mWave_N106,sizeof(struct N106Wave));
+    ZeroMemory(mWave_FME07,sizeof(struct FME07Wave)*3);
+	 ZeroMemory(&mWave_FDS,sizeof(struct FDSWave));
+    
+    /* end clear globals */
+
+	// Default filter bases
+	nHighPassBase = 150;
+	nLowPassBase = 27000;
+	fSmDiv = 0.75f;
+
+	bLowPassEnabled = 0;
+	bPrePassEnabled = 0;
+	bHighPassEnabled = 1;
+
+	mWave_TND.nNoiseRandomShift =	1;
+	for(i = 0; i < 8; i++)
+		mWave_TND.pDMCDMAPtr[i] = pROM[i + 2];
+
+
+	nSampleRate = 44100;		//default
+	fMasterVolume = 1.0f;
+
+	for(i = 0; i < 29; i++)
+		SetChannelOptions(i,1,255);
+
+	SetPlaybackOptions(nSampleRate);
+
+	//bDMCPopReducer = 1;
+
+	for(i = 0; i < 8; i++)
+		mWave_N106.fFrequencyLookupTable[i] = (((i + 1) * 45 * 0x40000) / (float)NES_FREQUENCY) * (float)NTSC_FREQUENCY;
+
+	if(bMemoryOK)		return 1;
+	
+	mWave_TND.nOutputTable_L = main_nOutputTable_L;
+
+		ZeroMemory(pRAM,0x800);
+		ZeroMemory(pSRAM,0x2000);
+		ZeroMemory(pExRAM,0x1000);
+		ZeroMemory(mWave_TND.nOutputTable_L,0x10000);
+		pStack = pRAM + 0x100;
+		bMemoryOK = 1;
+		return 1;
+}
+
+/*
+ *	LoadNSF
+ */
+
+int LoadNSF(int32_t datasize)
+{
+	if(!bMemoryOK)	return 0;
+
+	if(!pDataBuffer)				return 0;
+
+	int32_t i;
+
+	bFileLoaded = 0;
+	bTrackSelected = 0;
+	nExternalSound = nChipExtensions;
+	if(nIsPal & 2)
+		bPALMode = bPALPreference;
+	else
+		bPALMode = nIsPal & 1;
+
+	SetPlaybackOptions(nSampleRate);
+	
+	int32_t neededsize = datasize + (nfileLoadAddress & 0x0FFF);
+	if(neededsize & 0x0FFF)		neededsize += 0x1000 - (neededsize & 0x0FFF);
+	if(neededsize < 0x1000)		neededsize = 0x1000;
+
+	BYTE specialload = 0;
+	
+	for(i = 0; (i < 8) && (!nBankswitch[i]); i++);
+	if(i < 8)		//uses bankswitching
+	{
+		memcpy(&nBankswitchInitValues[2],nBankswitch,8);
+		nBankswitchInitValues[0] = nBankswitch[6];
+		nBankswitchInitValues[1] = nBankswitch[7];
+		if(nExternalSound & EXTSOUND_FDS)
+		{
+			if(!(nBankswitchInitValues[0] || nBankswitchInitValues[1]))
+			{
+				//FDS sound with '00' specified for both $6000 and $7000 banks.
+				// point this to an area of fresh RAM (sort of hackish solution
+				// for those FDS tunes that don't quite follow the nsf specs.
+				nBankswitchInitValues[0] = (BYTE)(neededsize >> 12);
+				nBankswitchInitValues[1] = (BYTE)(neededsize >> 12) + 1;
+				neededsize += 0x2000;
+			}
+		}
+	}
+	else			//doesn't
+	{
+		if(nExternalSound & EXTSOUND_FDS)
+		{
+			// bad load address
+			if(nfileLoadAddress < 0x6000)		return 0;
+
+			if(neededsize < 0xA000)
+				neededsize = 0xA000;
+			specialload = 1;
+			for(i = 0; i < 10; i++)
+				nBankswitchInitValues[i] = (BYTE)i;
+		}
+		else
+		{
+			// bad load address
+			if(nfileLoadAddress < 0x8000)		return 0;
+
+			int32_t j = (nfileLoadAddress >> 12) - 6;
+			for(i = 0; i < j; i++)
+				nBankswitchInitValues[i] = 0;
+			for(j = 0; i < 10; i++, j++)
+				nBankswitchInitValues[i] = (BYTE)j;
+		}
+	}
+
+	nROMSize = neededsize;
+	nROMBankCount = neededsize >> 12;
+
+	if(specialload)
+		pROM_Full = pDataBuffer-(nfileLoadAddress-0x6000);
+	else
+   	    pROM_Full = pDataBuffer-(nfileLoadAddress&0x0FFF);
+
+	ZeroMemory(pRAM,0x0800);
+	ZeroMemory(pExRAM,0x1000);
+	ZeroMemory(pSRAM,0x2000);
+
+	nExternalSound = nChipExtensions;
+	fNSFPlaybackSpeed = (bPALMode ? PAL_NMIRATE : NTSC_NMIRATE);
+	
+	bFileLoaded = 1;
+
+	SetPlaybackSpeed(0);
+
+	nPlayAddress = nfilePlayAddress;
+	nInitAddress = nfileInitAddress;
+
+	pExRAM[0x00] = 0x20;						//JSR
+	//memcpy(&pExRAM[0x01],&nInitAddress,2);		//Init Address (won't somebody think of the endians!)
+	pExRAM[0x01] = nInitAddress&0xff;
+	pExRAM[0x02] = (nInitAddress>>8)&0xff;
+	pExRAM[0x03] = 0xF2;						//JAM
+	pExRAM[0x04] = 0x20;						//JSR
+	//memcpy(&pExRAM[0x05],&nPlayAddress,2);		//Play Address
+	pExRAM[0x05] = nPlayAddress&0xff;
+	pExRAM[0x06] = (nPlayAddress>>8)&0xff;
+	pExRAM[0x07] = 0x4C;						//JMP
+	pExRAM[0x08] = 0x03;						//$5003  (JAM right before the JSR to play address)
+	pExRAM[0x09] = 0x50;
+
+	regA = regX = regY = 0;
+	regP = 0x04;			//I_FLAG
+	regSP = 0xFF;
+
+	nFilterAccL = nFilterAccR = nFilterAcc2L = nFilterAcc2R = 0;
+
+
+	/*	Reset Read/Write Procs			*/
+	
+	ReadMemory[0] = ReadMemory[1] = ReadMemory_RAM;
+	ReadMemory[2] = ReadMemory[3] = ReadMemory_Default;
+	ReadMemory[4] =					ReadMemory_pAPU;
+	ReadMemory[5] =					ReadMemory_ExRAM;
+	ReadMemory[6] = ReadMemory[7] = ReadMemory_SRAM;
+
+	WriteMemory[0] = WriteMemory[1] =	WriteMemory_RAM;
+	WriteMemory[2] = WriteMemory[3] =	WriteMemory_Default;
+	WriteMemory[4] =					WriteMemory_pAPU;
+	WriteMemory[5] =					WriteMemory_ExRAM;
+	WriteMemory[6] = WriteMemory[7] =	WriteMemory_SRAM;
+
+	for(i = 8; i < 16; i++)
+	{
+		ReadMemory[i] = ReadMemory_ROM;
+		WriteMemory[i] = WriteMemory_Default;
+	}
+
+	if(nExternalSound & EXTSOUND_FDS)
+	{
+		WriteMemory[0x06] = WriteMemory_FDSRAM;
+		WriteMemory[0x07] = WriteMemory_FDSRAM;
+		WriteMemory[0x08] = WriteMemory_FDSRAM;
+		WriteMemory[0x09] = WriteMemory_FDSRAM;
+		WriteMemory[0x0A] = WriteMemory_FDSRAM;
+		WriteMemory[0x0B] = WriteMemory_FDSRAM;
+		WriteMemory[0x0C] = WriteMemory_FDSRAM;
+		WriteMemory[0x0D] = WriteMemory_FDSRAM;
+		ReadMemory[0x06] = ReadMemory_ROM;
+		ReadMemory[0x07] = ReadMemory_ROM;
+	}
+
+	if(!bPALMode)	//no expansion sound available on a PAL system
+	{
+		if(nExternalSound & EXTSOUND_VRC6)
+		{
+			WriteMemory[0x09] = WriteMemory_VRC6;	//if both VRC6+VRC7... it MUST go to WriteMemory_VRC6 
+			WriteMemory[0x0A] = WriteMemory_VRC6;	// or register writes will be lost (WriteMemory_VRC6 calls
+			WriteMemory[0x0B] = WriteMemory_VRC6;	// WriteMemory_VRC7 if needed)
+		}
+		if(nExternalSound & EXTSOUND_N106)
+		{
+			WriteMemory[0x04] = WriteMemory_N106;
+			ReadMemory[0x04] = ReadMemory_N106;
+			WriteMemory[0x0F] = WriteMemory_N106;
+		}
+		if(nExternalSound & EXTSOUND_FME07)
+		{
+			WriteMemory[0x0C] = WriteMemory_FME07;
+			WriteMemory[0x0E] = WriteMemory_FME07;
+		}
+	}
+	
+	if(nExternalSound & EXTSOUND_MMC5)			//MMC5 still has a multiplication reg that needs to be available on PAL tunes
+		WriteMemory[0x05] = WriteMemory_MMC5;
+
+	return 1;
+}
+
+/*
+ *	SetTrack
+ */
+
+void SetTrack(BYTE track)
+{
+	int32_t i;
+	if(!bFileLoaded)		return;
+	
+	bTrackSelected = 1;
+	nCurTrack = track;
+
+	regPC = 0x5000;
+	regA = track;
+	regX = bPALMode;
+	regY = bCleanAXY ? 0 : 0xCD;
+	regSP = 0xFF;
+	if(bCleanAXY)
+		regP = 0x04;
+	bCPUJammed = 0;
+
+	nCPUCycle = nAPUCycle = 0;
+	nDMCPop_Prev = 0;
+	bDMCPop_Skip = 0;
+
+	for(i = 0x4000; i < 0x400F; i++)
+		WriteMemory_pAPU(i,0);
+	WriteMemory_pAPU(0x4010,0);
+	WriteMemory_pAPU(0x4012,0);
+	WriteMemory_pAPU(0x4013,0);
+	WriteMemory_pAPU(0x4014,0);
+	WriteMemory_pAPU(0x4015,0);
+	WriteMemory_pAPU(0x4015,0x0F);
+	WriteMemory_pAPU(0x4017,0);
+
+	for(i = 0; i < 10; i++)
+		WriteMemory_ExRAM(0x5FF6 + i,nBankswitchInitValues[i]);
+
+	ZeroMemory(pRAM,0x0800);
+	ZeroMemory(pSRAM,0x2000);
+	ZeroMemory(&pExRAM[0x10],0x0FF0);
+	bFade = 0;
+
+
+	nTicksUntilNextSample = nTicksPerSample;
+	nTicksUntilNextFrame = (bPALMode ? PAL_FRAME_COUNTER_FREQ : NTSC_FRAME_COUNTER_FREQ)*0x10000;
+	nTicksUntilNextPlay = nTicksPerPlay;
+	nTotalPlays = 0;
+	
+	/*	Clear mixing vals	*/
+	mWave_Squares.nMixL = 0;
+	mWave_TND.nMixL = 0;
+	mWave_VRC6Pulse[0].nMixL = 0;
+	mWave_VRC6Pulse[1].nMixL = 0;
+	mWave_VRC6Saw.nMixL = 0;
+	/*
+	mWave_MMC5Square[0].nMixL = mWave_MMC5Square[0].nMixR = 0;
+	mWave_MMC5Square[1].nMixL = mWave_MMC5Square[1].nMixR = 0;
+	mWave_MMC5Voice.nMixL = mWave_MMC5Voice.nMixR = 0;
+	*/
+
+
+	/*	Reset Tri/Noise/DMC	*/
+	mWave_TND.nTriStep = mWave_TND.nTriOutput = 0;
+	mWave_TND.nDMCOutput = 0;
+	mWave_TND.bNoiseRandomOut = 0;
+	mWave_Squares.nDutyCount[0] = mWave_Squares.nDutyCount[1] = 0;
+	mWave_TND.bDMCActive = 0;
+	mWave_TND.nDMCBytesRemaining = 0;
+	mWave_TND.bDMCSampleBufferEmpty = 1;
+	mWave_TND.bDMCDeltaSilent = 1;
+
+	/*	Reset VRC6	*/
+	mWave_VRC6Pulse[0].nVolume = 0;
+	mWave_VRC6Pulse[1].nVolume = 0;
+	mWave_VRC6Saw.nAccumRate = 0;
+
+	/*	Reset MMC5	*/
+	/*
+	mWave_MMC5Square[0].nVolume = 0;
+	mWave_MMC5Square[1].nVolume = 0;
+	mWave_MMC5Voice.nOutput = 0;
+	*/
+
+	/*	Reset N106	*/
+	ZeroMemory(mWave_N106.nRAM,0x100);
+	ZeroMemory(mWave_N106.nVolume,8);
+	ZeroMemory(mWave_N106.nOutput,8);
+	ZeroMemory(mWave_N106.nMixL,32);
+
+	/*	Reset FME-07	*/
+	mWave_FME07[0].nVolume = 0;
+	mWave_FME07[1].nVolume = 0;
+	mWave_FME07[2].nVolume = 0;
+
+	/*	Clear FDS crap		*/
+
+	mWave_FDS.bEnvelopeEnable = 0;
+	mWave_FDS.nEnvelopeSpeed = 0xFF;
+	mWave_FDS.nVolEnv_Mode = 2;
+	mWave_FDS.nVolEnv_Decay = 0;
+	mWave_FDS.nVolEnv_Gain = 0;
+	mWave_FDS.nVolume = 0;
+	mWave_FDS.bVolEnv_On = 0;
+	mWave_FDS.nSweep_Mode = 2;
+	mWave_FDS.nSweep_Decay = 0;
+	mWave_FDS.nSweep_Gain = 0;
+	mWave_FDS.bSweepEnv_On = 0;
+	mWave_FDS.nSweepBias = 0;
+	mWave_FDS.bLFO_Enabled = 0;
+	mWave_FDS.nLFO_Freq.W = 0;
+	mWave_FDS.fLFO_Timer = 0;
+	mWave_FDS.fLFO_Count = 0;
+	mWave_FDS.nLFO_Addr = 0;
+	mWave_FDS.bLFO_On = 0;
+	mWave_FDS.nMainVolume = 0;
+	mWave_FDS.bEnabled = 0;
+	mWave_FDS.nFreq.W = 0;
+	mWave_FDS.fFreqCount = 0;
+	mWave_FDS.nMainAddr = 0;
+	mWave_FDS.bWaveWrite = 0;
+	mWave_FDS.bMain_On = 0;
+	mWave_FDS.nMixL = 0;
+	ZeroMemory(mWave_FDS.nWaveTable,0x40);
+	ZeroMemory(mWave_FDS.nLFO_Table,0x40);
+
+	mWave_FDS.nSweep_Count = mWave_FDS.nSweep_Timer = ((mWave_FDS.nSweep_Decay + 1) * mWave_FDS.nEnvelopeSpeed * 8);
+	mWave_FDS.nVolEnv_Count = mWave_FDS.nVolEnv_Timer = ((mWave_FDS.nVolEnv_Decay + 1) * mWave_FDS.nEnvelopeSpeed * 8);
+
+	nSilentSamples = 0;
+
+	nFilterAccL = 0;
+	nFilterAccR = 0;
+	nFilterAcc2L = 0;
+	nFilterAcc2R = 0;
+	nSmPrevL = 0;
+	nSmAccL = 0;
+	nSmPrevR = 0;
+	nSmAccR = 0;
+
+	nSilentSamples = 0;
+
+	fulltick=0;
+}
+
+/*
+ *	SetPlaybackOptions
+ */
+
+int SetPlaybackOptions(int32_t samplerate)
+{
+	if(samplerate < 2000)					return 0;
+	if(samplerate > 96000)					return 0;
+
+	nSampleRate = samplerate;
+    
+	nTicksPerSample = (bPALMode ? PAL_FREQUENCY : NTSC_FREQUENCY) / samplerate * 0x10000;
+	nTicksUntilNextSample = nTicksPerSample;
+	RebuildOutputTables((UINT)(-1));
+
+	RecalcFilter();
+	RecalcSilenceTracker();
+
+    return 1;
+}
+
+/*
+ *	SetPlaybackSpeed
+ */
+
+void SetPlaybackSpeed(float playspersec)
+{
+	if(playspersec < 1)
+	{
+		if(!bFileLoaded)	return;
+		playspersec = fNSFPlaybackSpeed;
+	}
+
+	nTicksPerPlay = nTicksUntilNextPlay = (bPALMode ? PAL_FREQUENCY : NTSC_FREQUENCY) / playspersec * 0x10000;
+}
+
+/*
+ *	SetMasterVolume
+ */
+
+void SetMasterVolume(float vol)
+{
+	fMasterVolume = vol;
+	RebuildOutputTables((UINT)(-1));
+}
+
+/*
+ *	SetChannelOptions
+ */
+
+void SetChannelOptions(UINT chan,int32_t mix,int32_t vol)
+{
+	BYTE bRebuild = 0;
+
+	if(chan >= 29)			return;
+
+	if((mix == 0) || (mix == 1))
+	{
+		switch(chan)
+		{
+		case 0:	mWave_Squares.bChannelMix[0] = mix; break;
+		case 1:	mWave_Squares.bChannelMix[1] = mix; break;
+		case 2:	mWave_TND.bTriChannelMix = mix; break;
+		case 3:	mWave_TND.bNoiseChannelMix = mix; break;
+		case 4:	mWave_TND.bDMCChannelMix = mix; break;
+		default: bChannelMix[chan - 5] = mix; break;
+		}
+	}
+
+	if((vol >= 0) && (vol <= 255)){		nChannelVol[chan] = vol; bRebuild = 1; }
+
+	if(bRebuild)
+		RebuildOutputTables(1 << chan);
+}
+
+/*
+ *	SetAdvancedOptions
+ */
+/*
+void SetAdvancedOptions(const struct NSF_ADVANCEDOPTIONS* opt)
+{
+	if(!opt)	return;
+
+	mWave_FDS.bPopReducer = opt->bFDSPopReducer;
+	bDMCPopReducer = opt->bDMCPopReducer;
+	nForce4017Write = opt->nForce4017Write;
+	bN106PopReducer = opt->bN106PopReducer;
+	nSilenceTrackMS = opt->nSilenceTrackMS;
+	bNoSilenceIfTime = opt->bNoSilenceIfTime;
+	RecalcSilenceTracker();
+	
+	bIgnore4011Writes = opt->bIgnore4011Writes;
+	bIgnoreBRK = opt->bIgnoreBRK;
+	bIgnoreIllegalOps = opt->bIgnoreIllegalOps;
+	bNoWaitForReturn = opt->bNoWaitForReturn;
+	bPALPreference = opt->bPALPreference;
+	bCleanAXY = opt->bCleanAXY;
+	bResetDuty = opt->bResetDuty;
+
+	bHighPassEnabled = opt->bHighPassEnabled;
+	bLowPassEnabled = opt->bLowPassEnabled;
+	bPrePassEnabled = opt->bPrePassEnabled;
+
+	nHighPassBase = opt->nHighPassBase;
+	nLowPassBase = opt->nLowPassBase;
+	nPrePassBase = opt->nPrePassBase;
+	if(nHighPassBase < 50)				nHighPassBase = 50;
+	if(nHighPassBase > 5000)			nHighPassBase = 5000;
+	if(nLowPassBase < 8000)				nLowPassBase = 8000;
+	if(nLowPassBase > 60000)			nLowPassBase = 60000;
+	if(nPrePassBase < 0)				nPrePassBase = 0;
+	if(nPrePassBase > 1000)				nPrePassBase = 1000;
+	RecalcFilter();
+}
+*/
+
+/*
+*	GetPlaybackSpeed
+*/
+
+float GetPlaybackSpeed()
+{
+	if(nTicksPerPlay <= 0)	return 0;
+	return ((bPALMode ? PAL_FREQUENCY : NTSC_FREQUENCY) / (nTicksPerPlay>>16));
+}
+
+/*
+*	GetMasterVolume
+*/
+
+float GetMasterVolume()
+{
+	return fMasterVolume;
+}
+
+/*
+ *	GetAdvancedOptions
+ */
+
+/*
+void GetAdvancedOptions(struct NSF_ADVANCEDOPTIONS* opt)
+{
+	if(!opt)	return;
+	opt->bDMCPopReducer = bDMCPopReducer;
+	opt->nForce4017Write = nForce4017Write;
+	opt->bN106PopReducer = bN106PopReducer;
+	opt->bFDSPopReducer = mWave_FDS.bPopReducer;
+	opt->nSilenceTrackMS = nSilenceTrackMS;
+	opt->bNoSilenceIfTime = bNoSilenceIfTime;
+	opt->bIgnore4011Writes = bIgnore4011Writes;
+	opt->bIgnoreBRK = bIgnoreBRK;
+	opt->bIgnoreIllegalOps = bIgnoreIllegalOps;
+	opt->bNoWaitForReturn = bNoWaitForReturn;
+	opt->bPALPreference = bPALPreference;
+	opt->bCleanAXY = bCleanAXY;
+	opt->nHighPassBase = nHighPassBase;
+	opt->nLowPassBase = nLowPassBase;
+	opt->nPrePassBase = nPrePassBase;
+	opt->bHighPassEnabled = bHighPassEnabled;
+	opt->bLowPassEnabled = bLowPassEnabled;
+	opt->bPrePassEnabled = bPrePassEnabled;
+	opt->bResetDuty = bResetDuty;
+}
+*/
+
+/*
+ *	RecalcFilter
+ */
+
+void RecalcFilter()
+{
+	if(!nSampleRate) return;
+
+	nHighPass = ((__int64)nHighPassBase << 16) / nSampleRate;
+	nLowPass = ((__int64)nLowPassBase << 16) / nSampleRate;
+
+	if(nHighPass > (1<<16)) nHighPass = 1<<16;
+	if(nLowPass > (1<<16)) nLowPass = 1<<16;
+
+	fSmDiv = (100 - nPrePassBase) / 100.0f;
+}
+
+/*
+ *	RecalcSilenceTracker
+ */
+
+void RecalcSilenceTracker()
+{
+	if(nSilenceTrackMS <= 0 || !nSampleRate || (bNoSilenceIfTime && bTimeNotDefault))
+	{
+		nSilentSampleMax = 0;
+		return;
+	}
+
+	nSilentSampleMax = nSilenceTrackMS * nSampleRate / 500;
+	//if(nMonoStereo == 1)
+		nSilentSampleMax /= 2;
+}
+
+/*
+ *	CalculateChannelVolume
+ */
+
+void CalculateChannelVolume(int32_t maxvol,int32_t * left, BYTE vol)
+{
+	*left = (int32_t)(maxvol * vol * fMasterVolume);
+
+
+	*left /= 255;
+
+	if(*left > 32767)		*left = 32767;
+	if(*left < -32768)		*left = -32768;
+}
+
+
+/*
+ *	RebuildOutputTables
+ */
+
+void RebuildOutputTables(UINT chans)
+{
+	int32_t i,j;
+	float l[3];
+	int32_t v;
+	int32_t temp;
+	int32_t tl;
+	float ftemp;
+
+	if(chans & 0x00000003)		//squares
+	{
+		for(i = 0; i < 2; i++)
+		{
+			l[i] = nChannelVol[i];
+		}
+
+		v = (int32_t)(1438200 * fMasterVolume);
+
+		for(i = 0; i < 0x100; i++)
+		{
+			temp = (int32_t)(l[0] * (i >> 4));
+			temp += (int32_t)(l[1] * (i & 0x0F));
+
+			if(!temp)
+				mWave_Squares.nOutputTable_L[i] = 0;
+			else
+				mWave_Squares.nOutputTable_L[i] = v / ((2072640 / temp) + 100);
+			
+		}
+	}
+	if(chans & 0x0000001C)		//Tri/Noise/DMC
+	{
+		if(mWave_TND.nOutputTable_L)
+		{
+			for(i = 0; i < 3; i++)
+			{
+				l[i] = nChannelVol[i + 2];
+			}
+
+			v = (int32_t)(2396850 * fMasterVolume);
+			for(i = 0; i < 0x8000; i++)
+			{
+				ftemp = (l[0] * (i >> 11)) / 2097885;
+				ftemp += (l[1] * ((i >> 7) & 0x0F)) / 3121455;
+				ftemp += (l[2] * (i & 0x7F)) / 5772690;
+
+				if(!ftemp)
+					mWave_TND.nOutputTable_L[i] = 0;
+				else
+					mWave_TND.nOutputTable_L[i] = (int16_t)(v / ((1.0f / ftemp) + 100));
+			}
+		}
+	}
+	
+	if(chans & 0x00000020)		//VRC6 Pulse 1
+	{
+		CalculateChannelVolume(1875,&tl,nChannelVol[5]);
+		for(i = 0; i < 0x10; i++)
+		{
+			mWave_VRC6Pulse[0].nOutputTable_L[i] = tl * i / 0x0F;
+		}
+	}
+	if(chans & 0x00000040)		//VRC6 Pulse 2
+	{
+		CalculateChannelVolume(1875,&tl,nChannelVol[6]);
+		for(i = 0; i < 0x10; i++)
+		{
+			mWave_VRC6Pulse[1].nOutputTable_L[i] = tl * i / 0x0F;
+		}
+	}
+	if(chans & 0x00000080)		//VRC6 Saw
+	{
+		CalculateChannelVolume(3750,&tl,nChannelVol[7]);
+		for(i = 0; i < 0x20; i++)
+		{
+			mWave_VRC6Saw.nOutputTable_L[i] = tl * i / 0x1F;
+		}
+	}
+	/*
+
+	//the 2 MMC5 squares are probably too loud (1875 seems like it -should- be the proper base), but
+	// they seemed way to quiet in contrast to the triangle in Just Breed.  Therefore their base vol
+	// has been bumped up a bit
+	if(chans & 0x00000100)		//MMC5 Square 1
+	{
+		CalculateChannelVolume(2500,&tl,&tr,nChannelVol[8],nChannelPan[8]);
+		for(i = 0; i < 0x10; i++)
+		{
+			mWave_MMC5Square[0].nOutputTable_L[i] = tl * i / 0x0F;
+		}
+	}
+	if(chans & 0x00000200)		//MMC5 Square 2
+	{
+		CalculateChannelVolume(2500,&tl,&tr,nChannelVol[9],nChannelPan[9]);
+		for(i = 0; i < 0x10; i++)
+		{
+			mWave_MMC5Square[1].nOutputTable_L[i] = tl * i / 0x0F;
+		}
+	}
+	if(chans & 0x00000400)		//MMC5 Voice
+	{
+		CalculateChannelVolume(15000,&tl,&tr,nChannelVol[10],nChannelPan[10]);
+		for(i = 0; i < 0x10; i++)
+		{
+			mWave_MMC5Voice.nOutputTable_L[i] = tl * i / 0x7F;
+		}
+	}
+	*/
+	if(chans & 0x0007F800)		//N106 channels
+	{
+		for(v = 0; v < 8; v++)
+		{
+			if(!(chans & (0x800 << v))) continue;
+
+			CalculateChannelVolume(3000,&tl,nChannelVol[11 + v]);
+			//this amplitude is just a guess =
+
+			for(i = 0; i < 0x10; i++)
+			{
+				for(j = 0; j < 0x10; j++)
+				{
+					mWave_N106.nOutputTable_L[v][i][j] = (tl * i * j) / 0xE1;
+				}
+			}
+		}
+	}
+	
+	if(chans & 0x02000000)		//FME-07 Square A
+	{
+		CalculateChannelVolume(3000,&tl,nChannelVol[25]);
+		mWave_FME07[0].nOutputTable_L[15] = tl;
+		mWave_FME07[0].nOutputTable_L[0] = 0;
+		for(i = 14; i > 0; i--)
+		{
+			mWave_FME07[0].nOutputTable_L[i] = mWave_FME07[0].nOutputTable_L[i + 1] * 80 / 100;
+		}
+	}
+	if(chans & 0x04000000)		//FME-07 Square B
+	{
+		CalculateChannelVolume(3000,&tl,nChannelVol[26]);
+		mWave_FME07[1].nOutputTable_L[15] = tl;
+		mWave_FME07[1].nOutputTable_L[0] = 0;
+		for(i = 14; i > 0; i--)
+		{
+			mWave_FME07[1].nOutputTable_L[i] = mWave_FME07[1].nOutputTable_L[i + 1] * 80 / 100;
+		}
+	}
+	if(chans & 0x08000000)		//FME-07 Square C
+	{
+		CalculateChannelVolume(3000,&tl,nChannelVol[27]);
+		mWave_FME07[2].nOutputTable_L[15] = tl;
+		mWave_FME07[2].nOutputTable_L[0] = 0;
+		for(i = 14; i > 0; i--)
+		{
+			mWave_FME07[2].nOutputTable_L[i] = mWave_FME07[2].nOutputTable_L[i + 1] * 80 / 100;
+		}
+	}
+
+	/*
+	 *	FDS
+	 */
+	if(chans & 0x10000000)
+	{
+		//  this base volume (4000) is just a guess to what sounds right.  Given the number of steps available in an FDS
+		//	wave... it seems like it should be much much more... but then it's TOO loud.
+		CalculateChannelVolume(4000,&tl,nChannelVol[28]);
+		for(i = 0; i < 0x21; i++)
+		{
+			for(j = 0; j < 0x40; j++)
+			{
+				mWave_FDS.nOutputTable_L[0][i][j] = (tl * i * j * 30) / (0x21 * 0x40 * 30);
+
+				mWave_FDS.nOutputTable_L[1][i][j] = (tl * i * j * 20) / (0x21 * 0x40 * 30);
+
+				mWave_FDS.nOutputTable_L[2][i][j] = (tl * i * j * 15) / (0x21 * 0x40 * 30);
+
+				mWave_FDS.nOutputTable_L[3][i][j] = (tl * i * j * 12) / (0x21 * 0x40 * 30);
+			}
+		}
+	}
+}
+
+/*
+ *	GetPlayCalls
+ */
+
+float GetPlayCalls()
+{
+	if(!nTicksPerPlay)	return 0;
+
+	return ((float)nTotalPlays) + (1.0f - (nTicksUntilNextPlay*1.0f / nTicksPerPlay));
+}
+
+/*
+ *	GetWrittenTime
+ */
+UINT GetWrittenTime(float basedplayspersec /* = 0 */)
+{
+	if(basedplayspersec <= 0)
+		basedplayspersec = GetPlaybackSpeed();
+
+	if(basedplayspersec <= 0)
+		return 0;
+
+	return (UINT)((GetPlayCalls() * 1000) / basedplayspersec);
+}
+
+/*
+ *	StopFade
+ */
+void StopFade()
+{
+	bFade = 0;
+	fFadeVolume = 1;
+}
+
+/*
+ *	SongCompleted
+ */
+
+BYTE SongCompleted()
+{
+	if(!bFade)						return 0;
+	if(nTotalPlays >= nEndFade)		return 1;
+	if(nSilentSampleMax)			return (nSilentSamples >= nSilentSampleMax);
+
+	return 0;
+}
+
+/*
+ *	SetFade
+ */
+
+void SetFade(int32_t fadestart,int32_t fadestop,BYTE bNotDefault)	//play routine calls
+{
+	if(fadestart < 0)	fadestart = 0;
+	if(fadestop < fadestart) fadestop = fadestart;
+
+	nStartFade = (uint32_t)fadestart;
+	nEndFade = (uint32_t)fadestop;
+	bFade = 1;
+	bTimeNotDefault = bNotDefault;
+
+	RecalcSilenceTracker();
+	RecalculateFade();
+}
+
+/*
+ *	SetFadeTime
+ */
+
+void SetFadeTime(UINT fadestart,UINT fadestop,float basedplays,BYTE bNotDefault)	//time in MS
+{
+	if(basedplays <= 0)
+		basedplays = GetPlaybackSpeed();
+	if(basedplays <= 0)
+		return;
+
+	SetFade((int32_t)(fadestart * basedplays / 1000),(int32_t)(fadestop * basedplays / 1000),bNotDefault);
+}
+
+/*
+ *	RecalculateFade
+ */
+
+void RecalculateFade()
+{
+	if(!bFade)	return;
+
+	int32_t temp = (int32_t)(GetPlaybackSpeed() / 4);		//make it hit silence a little before the song ends... otherwise we're not really fading OUT, we're just fading umm... quieter =P
+
+	if(nEndFade <= nStartFade)
+	{
+		nEndFade = nStartFade;
+		fFadeChange = 1.0f;
+	}
+	else if((nEndFade - temp) <= nStartFade)
+		fFadeChange = 1.0f;
+	else
+		fFadeChange = 1.0f / (nEndFade - nStartFade - temp);
+
+	if(nTotalPlays < nStartFade)
+		fFadeVolume = 1.0f;
+	else if(nTotalPlays >= nEndFade)
+		fFadeVolume = 0.0f;
+	else
+	{
+		fFadeVolume = 1.0f - ( (nTotalPlays - nStartFade + 1) * fFadeChange );
+		if(fFadeVolume < 0)
+			fFadeVolume = 0;
+	}
+
+}
+
+int32_t GetSamples(BYTE* buffer,int32_t buffersize)
+{
+	if(!buffer)								return 0;
+	if(buffersize < 16)						return 0;
+	if(!bTrackSelected)						return 0;
+	if(bFade && (nTotalPlays >= nEndFade))	return 0;
+	if(bIsGeneratingSamples)				return 0;
+	
+	bIsGeneratingSamples = 1;
+
+	
+	pOutput = buffer;
+	UINT runtocycle = (UINT)((buffersize / 2) * nTicksPerSample / 0x10000);
+	nCPUCycle = nAPUCycle = 0;
+	UINT tick;
+
+	while(1)
+	{
+		//tick = (UINT)ceil(fTicksUntilNextPlay);
+		tick = (nTicksUntilNextPlay+0xffff)>>16;
+		if((tick + nCPUCycle) > runtocycle)
+			tick = runtocycle - nCPUCycle;
+
+		if(bCPUJammed)
+		{
+			nCPUCycle += tick;
+			EmulateAPU(0);
+		}
+		else
+		{
+			tick = Emulate6502(tick + nCPUCycle);
+			EmulateAPU(1);
+		}
+
+		nTicksUntilNextPlay -= tick<<16;
+		if(nTicksUntilNextPlay <= 0)
+		{
+			nTicksUntilNextPlay += nTicksPerPlay;
+			if((bCPUJammed == 2) || bNoWaitForReturn)
+			{
+				regX = regY = regA = (bCleanAXY ? 0 : 0xCD);
+				regPC = 0x5004;
+				nTotalPlays++;
+				bDMCPop_SamePlay = 0;
+				bCPUJammed = 0;
+				if(nForce4017Write == 1)	WriteMemory_pAPU(0x4017,0x00);
+				if(nForce4017Write == 2)	WriteMemory_pAPU(0x4017,0x80);
+			}
+			
+			if(bFade && (nTotalPlays >= nStartFade))
+			{
+				fFadeVolume -= fFadeChange;
+				if(fFadeVolume < 0)
+					fFadeVolume = 0;
+				if(nTotalPlays >= nEndFade)
+					break;
+			}
+		}
+
+		if(nCPUCycle >= runtocycle)
+			break;
+	}
+
+	nCPUCycle = nAPUCycle = 0;
+	bIsGeneratingSamples = 0;
+
+	if(nSilentSampleMax && bFade)
+	{
+		int16_t* tempbuf = (int16_t*)buffer;
+		while( ((BYTE*)tempbuf) < pOutput)
+		{
+			if( (*tempbuf < -SILENCE_THRESHOLD) || (*tempbuf > SILENCE_THRESHOLD) )
+				nSilentSamples = 0;
+			else
+			{
+				if(++nSilentSamples >= nSilentSampleMax)
+					return (int32_t)( ((BYTE*)tempbuf) - buffer);
+			}
+			tempbuf++;
+		}
+	}
+
+	return (int32_t)(pOutput - buffer);
+}
+
+/****************** 6502 emulation ******************/
+
+//////////////////////////////////////////////////////////////////////////
+//  Memory reading/writing and other defines
+
+#define		Zp(a)			pRAM[a]											//reads zero page memory
+#define		ZpWord(a)		(Zp(a) | (Zp((BYTE)(a + 1)) << 8))				//reads zero page memory in word form
+
+#define		Rd(a)			((ReadMemory[((WORD)(a)) >> 12])(a))		//reads memory
+
+#define		RdWord(a)		(Rd(a) | (Rd(a + 1) << 8))						//reads memory in word form
+
+#define		Wr(a,v)			(WriteMemory[((WORD)(a)) >> 12])(a,v) //writes memory
+#define		WrZ(a,v)		pRAM[a] = v										//writes zero paged memory
+
+#define		PUSH(v)			pStack[SP--] = v								//pushes a value onto the stack
+#define		PULL(v)			v = pStack[++SP]								//pulls a value from the stack
+
+//////////////////////////////////////////////////////////////////////////
+//  Addressing Modes
+
+// first set - gets the value that's being addressed
+#define	Ad_VlIm()	val = Rd(PC.W); PC.W++									//Immediate
+#define Ad_VlZp()	final.W = Rd(PC.W); val = Zp(final.W); PC.W++			//Zero Page
+#define Ad_VlZx()	front.W = final.W = Rd(PC.W); final.B.l += X;			\
+					val = Zp(final.B.l); PC.W++								//Zero Page, X
+#define Ad_VlZy()	front.W = final.W = Rd(PC.W); final.B.l += Y;			\
+					val = Zp(final.B.l); PC.W++								//Zero Page, Y
+#define Ad_VlAb()	final.W = RdWord(PC.W); val = Rd(final.W); PC.W += 2	//Absolute
+#define Ad_VlAx()	front.W = final.W = RdWord(PC.W); final.W += X; PC.W += 2;	\
+					if(front.B.h != final.B.h) nCPUCycle++; val = Rd(final.W)	//Absolute, X [uses extra cycle if crossed page]
+#define Ad_VlAy()	front.W = final.W = RdWord(PC.W); final.W += Y; PC.W += 2;	\
+					if(front.B.h != final.B.h) nCPUCycle++; val = Rd(final.W)	//Absolute, X [uses extra cycle if crossed page]
+#define Ad_VlIx()	front.W = final.W = Rd(PC.W); final.B.l += X; PC.W++;	\
+					final.W = ZpWord(final.B.l); val = Rd(final.W)			//(Indirect, X)
+#define Ad_VlIy()	val = Rd(PC.W); front.W = final.W = ZpWord(val); PC.W++;\
+					final.W += Y; if(final.B.h != front.B.h) nCPUCycle++;		\
+					front.W = val; val = Rd(final.W)						//(Indirect), Y [uses extra cycle if crossed page]
+
+// second set - gets the ADDRESS that the mode is referring to (for operators that write to memory)
+//              note that AbsoluteX, AbsoluteY, and IndirectY modes do NOT check for page boundary crossing here
+//				since that extra cycle isn't added for operators that write to memory (it only applies to ones that
+//				only read from memory.. in which case the 1st set should be used)
+#define Ad_AdZp()	final.W = Rd(PC.W); PC.W++								//Zero Page
+#define Ad_AdZx()	final.W = front.W = Rd(PC.W); final.B.l += X; PC.W++	//Zero Page, X
+#define Ad_AdZy()	final.W = front.W = Rd(PC.W); final.B.l += Y; PC.W++	//Zero Page, Y
+#define Ad_AdAb()	final.W = RdWord(PC.W); PC.W += 2						//Absolute
+#define Ad_AdAx()	front.W = final.W = RdWord(PC.W); PC.W += 2;			\
+					final.W += X											//Absolute, X
+#define Ad_AdAy()	front.W = final.W = RdWord(PC.W); PC.W += 2;			\
+					final.W += Y											//Absolute, Y
+#define Ad_AdIx()	front.W = final.W = Rd(PC.W); PC.W++; final.B.l += X;	\
+					final.W = ZpWord(final.B.l)								//(Indirect, X)
+#define Ad_AdIy()	front.W = Rd(PC.W); final.W = ZpWord(front.W) + Y;		\
+					PC.W++													//(Indirect), Y
+
+// third set - reads memory, performs the desired operation on the value, then writes back to memory
+//				used for operators that directly change memory (ASL, INC, DEC, etc)
+#define MRW_Zp(cmd)	Ad_AdZp(); val = Zp(final.W); cmd(val); WrZ(final.W,val)	//Zero Page
+#define MRW_Zx(cmd) Ad_AdZx(); val = Zp(final.W); cmd(val); WrZ(final.W,val)	//Zero Page, X
+#define MRW_Zy(cmd) Ad_AdZy(); val = Zp(final.W); cmd(val); WrZ(final.W,val)	//Zero Page, Y
+#define MRW_Ab(cmd)	Ad_AdAb(); val = Rd(final.W); cmd(val); Wr(final.W,val)		//Absolute
+#define MRW_Ax(cmd)	Ad_AdAx(); val = Rd(final.W); cmd(val); Wr(final.W,val)		//Absolute, X
+#define MRW_Ay(cmd)	Ad_AdAy(); val = Rd(final.W); cmd(val); Wr(final.W,val)		//Absolute, Y
+#define MRW_Ix(cmd)	Ad_AdIx(); val = Rd(final.W); cmd(val); Wr(final.W,val)		//(Indirect, X)
+#define MRW_Iy(cmd)	Ad_AdIy(); val = Rd(final.W); cmd(val); Wr(final.W,val)		//(Indirect), Y
+
+// Relative modes are special in that they're only used by branch commands
+//  this macro handles the jump, and should only be called if the branch condition was true
+//  if the branch condition was false, the PC must be incrimented
+
+#define RelJmp(cond)	val = Rd(PC.W); PC.W++; final.W = PC.W + (int8_t)(val);						\
+						if(cond) { nCPUCycle += ((final.B.h != PC.B.h) ? 2 : 1); PC.W = final.W; }
+
+
+
+//////////////////////////////////////////////////////////////////////////
+//  Status Flags
+
+#define		C_FLAG		0x01			//carry flag
+#define		Z_FLAG		0x02			//zero flag
+#define		I_FLAG		0x04			//mask interrupt flag
+#define		D_FLAG		0x08			//decimal flag (decimal mode is unsupported on NES)
+#define		B_FLAG		0x10			//break flag (not really in the status register!  It's value in ST is never used.  When ST is put in memory (by an interrupt or PHP), this flag is set only if BRK was called) ** also when PHP is called due to a bug
+#define		R_FLAG		0x20			//reserved flag (not really in the register.  It's value is never used.  Whenever ST is put in memory, this flag is always set)
+#define		V_FLAG		0x40			//overflow flag
+#define		N_FLAG		0x80			//sign flag
+
+
+//////////////////////////////////////////////////////////////////////////
+//  Lookup Tables
+
+static const BYTE CPU_Cycles[0x100] = {		//the number of CPU cycles used for each instruction
+7,6,0,8,3,3,5,5,3,2,2,2,4,4,6,6,
+2,5,0,8,4,4,6,6,2,4,2,7,4,4,7,7,
+6,6,0,8,3,3,5,5,4,2,2,2,4,4,6,6,
+2,5,0,8,4,4,6,6,2,4,2,7,4,4,7,7,
+6,6,0,8,3,3,5,5,3,2,2,2,3,4,6,6,
+2,5,0,8,4,4,6,6,2,4,2,7,4,4,7,7,
+6,6,0,8,3,3,5,5,4,2,2,2,5,4,6,6,
+2,5,0,8,4,4,6,6,2,4,2,7,4,4,7,7,
+2,6,2,6,3,3,3,3,2,2,2,2,4,4,4,4,
+2,6,0,6,4,4,4,4,2,5,2,5,5,5,5,5,
+2,6,2,6,3,3,3,3,2,2,2,2,4,4,4,4,
+2,5,0,5,4,4,4,4,2,4,2,4,4,4,4,4,
+2,6,2,8,3,3,5,5,2,2,2,2,4,4,6,6,
+2,5,0,8,4,4,6,6,2,4,2,7,4,4,7,7,
+2,6,2,8,3,3,5,5,2,2,2,2,4,4,6,6,
+2,5,0,8,4,4,6,6,2,4,2,7,4,4,7,7		};
+
+
+static const BYTE NZTable[0x100] = {		//the status of the NZ flags for the given value
+Z_FLAG,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,
+N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,
+N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,
+N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,
+N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,
+N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,
+N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,
+N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG,N_FLAG	};
+
+//  A quick macro for working with the above table
+#define UpdateNZ(v)	ST = (ST & ~(N_FLAG|Z_FLAG)) | NZTable[v]
+
+
+//////////////////////////////////////////////////////////////////////////
+//  Opcodes
+//
+//		These opcodes perform the action with the given value (changing that value
+//	if necessary).  Registers and flags associated with the operation are
+//	changed accordingly.  There are a few exceptions which will be noted when they arise
+
+
+/*  ADC
+		Adds the value to the accumulator with carry
+		Changes:  A, NVZC
+		- Decimal mode not supported on the NES
+		- Due to a bug, NVZ flags are not altered if the Decimal flag is on	--(taken out)-- */
+#define ADC()															\
+	tw.W = A + val + (ST & C_FLAG);										\
+	ST = (ST & (I_FLAG|D_FLAG)) | tw.B.h | NZTable[tw.B.l] |			\
+		( (0x80 & ~(A ^ val) & (A ^ tw.B.l)) ? V_FLAG : 0 );			\
+	A = tw.B.l
+
+/*	AND
+		Combines the value with the accumulator using a bitwise AND operation
+		Changes:  A, NZ		*/
+#define AND()															\
+	A &= val;															\
+	UpdateNZ(A)
+
+/*	ASL
+		Left shifts the value 1 bit.  The bit that gets shifted out goes to
+		the carry flag.
+		Changes:  value, NZC		*/
+#define ASL(value)														\
+	tw.W = value << 1;													\
+	ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | tw.B.h | NZTable[tw.B.l];		\
+	value = tw.B.l
+
+/*	BIT
+		Compares memory with the accumulator with an AND operation, but changes neither.
+		The two high bits of memory get transferred to the status reg
+		Z is set if the AND operation yielded zero, otherwise it's cleared
+		Changes:  NVZ				*/
+#define BIT()															\
+	ST = (ST & ~(N_FLAG|V_FLAG|Z_FLAG)) | (val & (N_FLAG|V_FLAG)) |		\
+			((A & val) ? 0 : Z_FLAG)
+
+/*	CMP, CPX, CPY
+		Compares memory with the given register with a subtraction operation.
+		Flags are set accordingly depending on the result:
+		Reg < Memory:  Z=0, C=0
+		Reg = Memory:  Z=1, C=1
+		Reg > Memory:  Z=0, C=1
+		N is set according to the result of the subtraction operation
+		Changes:  NZC
+
+		NOTE -- CMP, CPX, CPY all share this same routine, so the desired register
+				(A, X, or Y respectively) must be given when calling this macro... as well
+				as the memory to compare it with.		*/
+#define CMP(reg)														\
+	tw.W = reg - val;													\
+	ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | (tw.B.h ? 0 : C_FLAG) |		\
+			NZTable[tw.B.l]
+
+/*	DEC, DEX, DEY
+		Decriments a value by one.
+		Changes:  value, NZ				*/
+#define DEC(value)														\
+	value--;															\
+	UpdateNZ(value)
+
+/*	EOR
+		Combines a value with the accumulator using a bitwise exclusive-OR operation
+		Changes:  A, NZ					*/
+#define EOR()															\
+	A ^= val;															\
+	UpdateNZ(A)
+
+/*	INC, INX, INY
+		Incriments a value by one.
+		Changes:  value, NZ				*/
+#define INC(value)														\
+	value++;															\
+	UpdateNZ(value)
+
+/*	LSR
+		Shifts value one bit to the right.  Bit that gets shifted out goes to the
+		Carry flag.
+		Changes:  value, NZC			*/
+#define LSR(value)														\
+	tw.W = value >> 1;													\
+	ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | NZTable[tw.B.l] |				\
+		(value & 0x01);													\
+	value = tw.B.l
+
+/*	ORA
+		Combines a value with the accumulator using a bitwise inclusive-OR operation
+		Changes:  A, NZ					*/
+#define ORA()															\
+	A |= val;															\
+	UpdateNZ(A)
+
+/*	ROL
+		Rotates a value one bit to the left:
+		C <-   7<-6<-5<-4<-3<-2<-1<-0    <- C
+		Changes:  value, NZC			*/
+#define ROL(value)														\
+	tw.W = (value << 1) | (ST & 0x01);									\
+	ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | NZTable[tw.B.l] | tw.B.h;		\
+	value = tw.B.l
+
+/*	ROR
+		Rotates a value one bit to the right:
+		C ->   7->6->5->4->3->2->1->0   -> C
+		Changes:  value, NZC			*/
+#define ROR(value)														\
+	tw.W = (value >> 1) | (ST << 7);									\
+	ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | NZTable[tw.B.l] |				\
+		(value & 0x01);													\
+	value = tw.B.l
+
+/*	SBC
+		Subtracts a value from the accumulator with borrow (inverted carry)
+		Changes:  A, NVZC
+		- Decimal mode not supported on the NES
+		- Due to a bug, NVZ flags are not altered if the Decimal flag is on	--(taken out)-- */
+#define SBC()																\
+	tw.W = A - val - ((ST & C_FLAG) ? 0 : 1);								\
+	ST = (ST & (I_FLAG|D_FLAG)) | (tw.B.h ? 0 : C_FLAG) | NZTable[tw.B.l] |	\
+					(((A ^ val) & (A ^ tw.B.l) & 0x80) ? V_FLAG : 0);		\
+	A = tw.B.l
+
+//////////////////////////////////////////////////////////////////////////
+//  Undocumented Opcodes
+//
+//		These opcodes are not included in the official specifications.  However,
+//	some of the unused opcode values perform operations which have since been
+//	documented.
+
+
+/*	ASO
+		Left shifts a value, then ORs the result with the accumulator
+		Changes:  value, A, NZC											*/
+#define ASO(value)														\
+	tw.W = value << 1;													\
+	A |= tw.B.l;														\
+	ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | NZTable[A] | tw.B.h;			\
+	value = tw.B.l
+
+/*	RLA
+		Roll memory left 1 bit, then AND the result with the accumulator
+		Changes:  value, A, NZC											*/
+#define RLA(value)														\
+	tw.W = (value << 1) | (ST & 0x01);									\
+	A &= tw.B.l;														\
+	ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | NZTable[A] | tw.B.h;			\
+	value = tw.B.l
+
+/*	LSE
+		Right shifts a value one bit, then EORs the result with the accumulator
+		Changes:  value, A, NZC											*/
+#define LSE(value)														\
+	tw.W = value >> 1;													\
+	A ^= tw.B.l;														\
+	ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | NZTable[A] | (value & 0x01);	\
+	value = tw.B.l
+
+/*	RRA
+		Roll memory right one bit, then ADC the result
+		Changes:  value, A, NVZC										*/
+#define RRA(value)														\
+	tw.W = (value >> 1) | (ST << 7);									\
+	ST = (ST & ~C_FLAG) | (value & 0x01);								\
+	value = tw.B.l;														\
+	ADC()
+
+/*	AXS
+		ANDs the contents of the X and A registers and stores the result
+		int memory.
+		Changes:  value  [DOES NOT CHANGE X, A, or any flags]			*/
+#define AXS(value)														\
+	value = A & X
+
+/*	DCM
+		Decriments a value and compares it with the A register.
+		Changes:  value, NZC											*/
+#define DCM(value)															\
+	value--;																\
+	CMP(A)
+
+/*	INS
+		Incriments a value then SBCs it
+		Changes:  value, A, NVZC										*/
+#define INS(value)														\
+	value++;															\
+	SBC()
+
+/*	AXA		*/
+#define AXA(value)														\
+	value = A & X & (Rd(PC.W - 1) + 1)
+
+
+//////////////////////////////////////////////////////////////////////////
+//
+//		The 6502 emulation function!
+//
+//
+
+union TWIN front;
+union TWIN final;
+BYTE val;
+BYTE op;
+
+UINT Emulate6502(UINT runto)
+{
+	/////////////////////////////////////////
+	//  If the CPU is jammed... don't bother
+	if(bCPUJammed == 1)
+		return 0;
+
+	register union TWIN	tw;		//used in calculations
+	register BYTE	ST = regP;
+	register union TWIN	PC;
+	BYTE			SP = regSP;
+	register BYTE	A = regA;
+	register BYTE	X = regX;
+	register BYTE	Y = regY;
+	union TWIN			front;
+	union TWIN			final;
+	PC.W = regPC;
+
+	UINT ret = nCPUCycle;
+	
+	////////////////////
+	//  Start the loop
+	
+	while(nCPUCycle < runto)
+	{
+		op = Rd(PC.W);
+		PC.W++;
+
+		nCPUCycle += CPU_Cycles[op];
+		switch(op)
+		{
+			//////////////////////////////////////////////////////////////////////////
+			//  Documented Opcodes first
+			
+			//////////////////////////////////////////////////////////////////////////
+			//  Flag setting/clearing
+		case 0x18:	ST &= ~C_FLAG;	break;		/* CLC	*/
+		case 0x38:	ST |=  C_FLAG;	break;		/* SEC	*/
+		case 0x58:	ST &= ~I_FLAG;	break;		/* CLI	*/
+		case 0x78:	ST |=  I_FLAG;	break;		/* SEI	*/
+		case 0xB8:	ST &= ~V_FLAG;	break;		/* CLV	*/
+		case 0xD8:	ST &= ~D_FLAG;	break;		/* CLD	*/
+		case 0xF8:	ST |=  D_FLAG;	break;		/* SED	*/
+
+			//////////////////////////////////////////////////////////////////////////
+			//  Branch commands
+		case 0x10:	RelJmp(!(ST & N_FLAG)); break;							/* BPL	*/
+		case 0x30:	RelJmp( (ST & N_FLAG)); break;							/* BMI	*/
+		case 0x50:	RelJmp(!(ST & V_FLAG)); break;							/* BVC	*/
+		case 0x70:	RelJmp( (ST & V_FLAG)); break;							/* BVS	*/
+		case 0x90:	RelJmp(!(ST & C_FLAG)); break;							/* BCC	*/
+		case 0xB0:	RelJmp( (ST & C_FLAG)); break;							/* BCS	*/
+		case 0xD0:	RelJmp(!(ST & Z_FLAG)); break;							/* BNE	*/
+		case 0xF0:	RelJmp( (ST & Z_FLAG)); break;							/* BEQ	*/
+
+			//////////////////////////////////////////////////////////////////////////
+			//  Direct stack alteration commands (push/pull commands)
+
+		case 0x08:	PUSH(ST | R_FLAG | B_FLAG);						break;	/* PHP	*/
+		case 0x28:	PULL(ST);										break;	/* PLP	*/
+		case 0x48:	PUSH(A);										break;	/* PHA	*/
+		case 0x68:	PULL(A); UpdateNZ(A);							break;	/* PLA	*/
+
+			//////////////////////////////////////////////////////////////////////////
+			//  Register Transfers
+
+		case 0x8A:	A = X;	UpdateNZ(A);							break;	/* TXA	*/
+		case 0x98:	A = Y;	UpdateNZ(A);							break;	/* TYA	*/
+		case 0x9A:	SP = X;											break;	/* TXS	*/
+		case 0xA8:	Y = A;	UpdateNZ(A);							break;	/* TAY	*/
+		case 0xAA:	X = A;	UpdateNZ(A);							break;	/* TAX	*/
+		case 0xBA:	X = SP;	UpdateNZ(X);							break;	/* TSX	*/
+
+
+			//////////////////////////////////////////////////////////////////////////
+			//  Other commands
+
+			/* ADC	*/
+		case 0x61:	Ad_VlIx();	ADC();	break;
+		case 0x65:	Ad_VlZp();	ADC();	break;
+		case 0x69:	Ad_VlIm();	ADC();	break;
+		case 0x6D:	Ad_VlAb();	ADC();	break;
+		case 0x71:	Ad_VlIy();	ADC();	break;
+		case 0x75:	Ad_VlZx();	ADC();	break;
+		case 0x79:	Ad_VlAy();	ADC();	break;
+		case 0x7D:	Ad_VlAx();	ADC();	break;
+
+			/* AND	*/
+		case 0x21:	Ad_VlIx();	AND();	break;
+		case 0x25:	Ad_VlZp();	AND();	break;
+		case 0x29:	Ad_VlIm();	AND();	break;
+		case 0x2D:	Ad_VlAb();	AND();	break;
+		case 0x31:	Ad_VlIy();	AND();	break;
+		case 0x35:	Ad_VlZx();	AND();	break;
+		case 0x39:	Ad_VlAy();	AND();	break;
+		case 0x3D:	Ad_VlAx();	AND();	break;
+
+			/* ASL	*/
+		case 0x0A:	ASL(A);						break;
+		case 0x06:	MRW_Zp(ASL);				break;
+		case 0x0E:	MRW_Ab(ASL);				break;
+		case 0x16:	MRW_Zx(ASL);				break;
+		case 0x1E:	MRW_Ax(ASL);				break;
+
+			/* BIT	*/
+		case 0x24:	Ad_VlZp();	BIT();	break;
+		case 0x2C:	Ad_VlAb();	BIT();	break;
+
+			/* BRK	*/
+		case 0x00:
+			if(bIgnoreBRK)
+				break;
+			PC.W++;							//BRK has a padding byte
+			PUSH(PC.B.h);					//push high byte of the return address
+			PUSH(PC.B.l);					//push low byte of return address
+			PUSH(ST | R_FLAG | B_FLAG);		//push processor status with R|B flags
+			ST |= I_FLAG;					//mask interrupts
+			PC.W = RdWord(0xFFFE);			//read the IRQ vector and jump to it
+
+			//extra check to make sure we didn't hit an infinite BRK loop
+			if(!Rd(PC.W))					//next command will be BRK
+			{
+				bCPUJammed = 1;				//the CPU will endlessly loop... just just jam it to ease processing power
+				goto jammed;
+			}
+			break;
+
+			/* CMP	*/
+		case 0xC1:	Ad_VlIx();	CMP(A);	break;
+		case 0xC5:	Ad_VlZp();	CMP(A);	break;
+		case 0xC9:	Ad_VlIm();	CMP(A); break;
+		case 0xCD:	Ad_VlAb();	CMP(A);	break;
+		case 0xD1:	Ad_VlIy();	CMP(A);	break;
+		case 0xD5:	Ad_VlZx();	CMP(A);	break;
+		case 0xD9:	Ad_VlAy();	CMP(A);	break;
+		case 0xDD:	Ad_VlAx();	CMP(A);	break;
+
+			/* CPX	*/
+		case 0xE0:	Ad_VlIm();	CMP(X);	break;
+		case 0xE4:	Ad_VlZp();	CMP(X);	break;
+		case 0xEC:	Ad_VlAb();	CMP(X);	break;
+
+			/* CPY	*/
+		case 0xC0:	Ad_VlIm();	CMP(Y);	break;
+		case 0xC4:	Ad_VlZp();	CMP(Y);	break;
+		case 0xCC:	Ad_VlAb();	CMP(Y);	break;
+
+			/* DEC	*/
+		case 0xCA:	DEC(X);						break;		/* DEX	*/
+		case 0x88:	DEC(Y);						break;		/* DEY	*/
+		case 0xC6:	MRW_Zp(DEC);				break;
+		case 0xCE:	MRW_Ab(DEC);				break;
+		case 0xD6:	MRW_Zx(DEC);				break;
+		case 0xDE:	MRW_Ax(DEC);				break;
+
+			/* EOR	*/
+		case 0x41:	Ad_VlIx();	EOR();	break;
+		case 0x45:	Ad_VlZp();	EOR();	break;
+		case 0x49:	Ad_VlIm();	EOR();	break;
+		case 0x4D:	Ad_VlAb();	EOR();	break;
+		case 0x51:	Ad_VlIy();	EOR();	break;
+		case 0x55:	Ad_VlZx();	EOR();	break;
+		case 0x59:	Ad_VlAy();	EOR();	break;
+		case 0x5D:	Ad_VlAx();	EOR();	break;
+
+			/* INC	*/
+		case 0xE8:	INC(X);						break;		/* INX	*/
+		case 0xC8:	INC(Y);						break;		/* INY	*/
+		case 0xE6:	MRW_Zp(INC);				break;
+		case 0xEE:	MRW_Ab(INC);				break;
+		case 0xF6:	MRW_Zx(INC);				break;
+		case 0xFE:	MRW_Ax(INC);				break;
+
+			/* JMP	*/
+		case 0x4C:	final.W = RdWord(PC.W);  PC.W = final.W; val = 0;	break;		/* Absolute JMP	*/
+		case 0x6C:	front.W = final.W = RdWord(PC.W);
+					PC.B.l = Rd(final.W); final.B.l++;
+					PC.B.h = Rd(final.W); final.W = PC.W;
+					break;		/* Indirect JMP -- must take caution:
+										Indirection at 01FF will read from 01FF and 0100 (not 0200) */
+			/* JSR	*/
+		case 0x20:
+			val = 0;
+			final.W = RdWord(PC.W);
+			PC.W++;				//JSR only incriments the return address by one.  It's incrimented again upon RTS
+			PUSH(PC.B.h);		//push high byte of return address
+			PUSH(PC.B.l);		//push low byte of return address
+			PC.W = final.W;
+			break;
+
+			/* LDA	*/
+		case 0xA1:	Ad_VlIx(); A = val; UpdateNZ(A);	break;
+		case 0xA5:	Ad_VlZp(); A = val; UpdateNZ(A);	break;
+		case 0xA9:	Ad_VlIm(); A = val; UpdateNZ(A);	break;
+		case 0xAD:	Ad_VlAb(); A = val; UpdateNZ(A);	break;
+		case 0xB1:	Ad_VlIy(); A = val; UpdateNZ(A);	break;
+		case 0xB5:	Ad_VlZx(); A = val; UpdateNZ(A);	break;
+		case 0xB9:	Ad_VlAy(); A = val; UpdateNZ(A);	break;
+		case 0xBD:	Ad_VlAx(); A = val; UpdateNZ(A);	break;
+
+			/* LDX	*/
+		case 0xA2:	Ad_VlIm(); X = val; UpdateNZ(X);	break;
+		case 0xA6:	Ad_VlZp(); X = val; UpdateNZ(X);	break;
+		case 0xAE:	Ad_VlAb(); X = val; UpdateNZ(X);	break;
+		case 0xB6:	Ad_VlZy(); X = val; UpdateNZ(X);	break;
+		case 0xBE:	Ad_VlAy(); X = val; UpdateNZ(X);	break;
+
+			/* LDY	*/
+		case 0xA0:	Ad_VlIm(); Y = val; UpdateNZ(Y);	break;
+		case 0xA4:	Ad_VlZp(); Y = val; UpdateNZ(Y);	break;
+		case 0xAC:	Ad_VlAb(); Y = val; UpdateNZ(Y);	break;
+		case 0xB4:	Ad_VlZx(); Y = val; UpdateNZ(Y);	break;
+		case 0xBC:	Ad_VlAx(); Y = val; UpdateNZ(Y);	break;
+
+			/* LSR	*/
+		case 0x4A:	LSR(A);						break;
+		case 0x46:	MRW_Zp(LSR);				break;
+		case 0x4E:	MRW_Ab(LSR);				break;
+		case 0x56:	MRW_Zx(LSR);				break;
+		case 0x5E:	MRW_Ax(LSR);				break;
+
+			/* NOP	*/
+		case 0xEA:
+
+			/* --- Undocumented ---
+				These opcodes perform the same action as NOP	*/
+		case 0x1A:	case 0x3A:	case 0x5A:
+		case 0x7A:	case 0xDA:	case 0xFA:		break;
+
+			/* ORA	*/
+		case 0x01:	Ad_VlIx();	ORA();	break;
+		case 0x05:	Ad_VlZp();	ORA();	break;
+		case 0x09:	Ad_VlIm();	ORA();	break;
+		case 0x0D:	Ad_VlAb();	ORA();	break;
+		case 0x11:	Ad_VlIy();	ORA();	break;
+		case 0x15:	Ad_VlZx();	ORA();	break;
+		case 0x19:	Ad_VlAy();	ORA();	break;
+		case 0x1D:	Ad_VlAx();	ORA();	break;
+
+			/* ROL	*/
+		case 0x2A:	ROL(A);						break;
+		case 0x26:	MRW_Zp(ROL);				break;
+		case 0x2E:	MRW_Ab(ROL);				break;
+		case 0x36:	MRW_Zx(ROL);				break;
+		case 0x3E:	MRW_Ax(ROL);				break;
+
+			/* ROR	*/
+		case 0x6A:	ROR(A);						break;
+		case 0x66:	MRW_Zp(ROR);				break;
+		case 0x6E:	MRW_Ab(ROR);				break;
+		case 0x76:	MRW_Zx(ROR);				break;
+		case 0x7E:	MRW_Ax(ROR);				break;
+
+			/* RTI	*/
+		case 0x40:
+			PULL(ST);						//pull processor status
+			PULL(PC.B.l);					//pull low byte of return address
+			PULL(PC.B.h);					//pull high byte of return address
+			break;
+
+			/* RTS	*/
+		case 0x60:
+			PULL(PC.B.l);
+			PULL(PC.B.h);
+			PC.W++;				//the return address is one less of what it needs
+			break;
+
+			/* SBC	*/
+		case 0xE1:	Ad_VlIx();	SBC();	break;
+		case 0xE5:	Ad_VlZp();	SBC();	break;
+		case 0xEB:										/* -- Undocumented --  EB performs the same operation as SBC immediate */
+		case 0xE9:	Ad_VlIm();	SBC();	break;
+		case 0xED:	Ad_VlAb();	SBC();	break;
+		case 0xF1:	Ad_VlIy();	SBC();	break;
+		case 0xF5:	Ad_VlZx();	SBC();	break;
+		case 0xF9:	Ad_VlAy();	SBC();	break;
+		case 0xFD:	Ad_VlAx();	SBC();	break;
+
+			/* STA	*/
+		case 0x81:	Ad_AdIx(); val = A; Wr(final.W,A);	break;
+		case 0x85:	Ad_AdZp(); val = A; WrZ(final.W,A);	break;
+		case 0x8D:	Ad_AdAb(); val = A; Wr(final.W,A);	break;
+		case 0x91:	Ad_AdIy(); val = A; Wr(final.W,A);	break;
+		case 0x95:	Ad_AdZx(); val = A; WrZ(final.W,A);	break;
+		case 0x99:	Ad_AdAy(); val = A; Wr(final.W,A);	break;
+		case 0x9D:	Ad_AdAx(); val = A; Wr(final.W,A);	break;
+
+			/* STX	*/
+		case 0x86:	Ad_AdZp(); val = X; WrZ(final.W,X);	break;
+		case 0x8E:	Ad_AdAb(); val = X; Wr(final.W,X);	break;
+		case 0x96:	Ad_AdZy(); val = X; WrZ(final.W,X);	break;
+
+			/* STY	*/
+		case 0x84:	Ad_AdZp(); val = Y; WrZ(final.W,Y);	break;
+		case 0x8C:	Ad_AdAb(); val = Y; Wr(final.W,Y);	break;
+		case 0x94:	Ad_AdZx(); val = Y; WrZ(final.W,Y);	break;
+
+			//////////////////////////////////////////////////////////////////////////
+			//  Undocumented Opcodes
+			/* ASO	*/
+		case 0x03:	if(bIgnoreIllegalOps) break;	MRW_Ix(ASO);				break;
+		case 0x07:	if(bIgnoreIllegalOps) break;	MRW_Zp(ASO);				break;
+		case 0x0F:	if(bIgnoreIllegalOps) break;	MRW_Ab(ASO);				break;
+		case 0x13:	if(bIgnoreIllegalOps) break;	MRW_Iy(ASO);				break;
+		case 0x17:	if(bIgnoreIllegalOps) break;	MRW_Zx(ASO);				break;
+		case 0x1B:	if(bIgnoreIllegalOps) break;	MRW_Ay(ASO);				break;
+		case 0x1F:	if(bIgnoreIllegalOps) break;	MRW_Ax(ASO);				break;
+
+			/* RLA	*/
+		case 0x23:	if(bIgnoreIllegalOps) break;	MRW_Ix(RLA);				break;
+		case 0x27:	if(bIgnoreIllegalOps) break;	MRW_Zp(RLA);				break;
+		case 0x2F:	if(bIgnoreIllegalOps) break;	MRW_Ab(RLA);				break;
+		case 0x33:	if(bIgnoreIllegalOps) break;	MRW_Iy(RLA);				break;
+		case 0x37:	if(bIgnoreIllegalOps) break;	MRW_Zx(RLA);				break;
+		case 0x3B:	if(bIgnoreIllegalOps) break;	MRW_Ay(RLA);				break;
+		case 0x3F:	if(bIgnoreIllegalOps) break;	MRW_Ax(RLA);				break;
+
+			/* LSE	*/
+		case 0x43:	if(bIgnoreIllegalOps) break;	MRW_Ix(LSE);				break;
+		case 0x47:	if(bIgnoreIllegalOps) break;	MRW_Zp(LSE);				break;
+		case 0x4F:	if(bIgnoreIllegalOps) break;	MRW_Ab(LSE);				break;
+		case 0x53:	if(bIgnoreIllegalOps) break;	MRW_Iy(LSE);				break;
+		case 0x57:	if(bIgnoreIllegalOps) break;	MRW_Zx(LSE);				break;
+		case 0x5B:	if(bIgnoreIllegalOps) break;	MRW_Ay(LSE);				break;
+		case 0x5F:	if(bIgnoreIllegalOps) break;	MRW_Ax(LSE);				break;
+
+			/* RRA	*/
+		case 0x63:	if(bIgnoreIllegalOps) break;	MRW_Ix(RRA);				break;
+		case 0x67:	if(bIgnoreIllegalOps) break;	MRW_Zp(RRA);				break;
+		case 0x6F:	if(bIgnoreIllegalOps) break;	MRW_Ab(RRA);				break;
+		case 0x73:	if(bIgnoreIllegalOps) break;	MRW_Iy(RRA);				break;
+		case 0x77:	if(bIgnoreIllegalOps) break;	MRW_Zx(RRA);				break;
+		case 0x7B:	if(bIgnoreIllegalOps) break;	MRW_Ay(RRA);				break;
+		case 0x7F:	if(bIgnoreIllegalOps) break;	MRW_Ax(RRA);				break;
+
+			/* AXS	*/
+		case 0x83:	if(bIgnoreIllegalOps) break;	MRW_Ix(AXS);				break;
+		case 0x87:	if(bIgnoreIllegalOps) break;	MRW_Zp(AXS);				break;
+		case 0x8F:	if(bIgnoreIllegalOps) break;	MRW_Ab(AXS);				break;
+		case 0x97:	if(bIgnoreIllegalOps) break;	MRW_Zy(AXS);				break;
+
+			/* LAX	*/
+		case 0xA3:	if(bIgnoreIllegalOps) break;	Ad_VlIx();	X = A = val; UpdateNZ(A);	break;
+		case 0xA7:	if(bIgnoreIllegalOps) break;	Ad_VlZp();	X = A = val; UpdateNZ(A);	break;
+		case 0xAF:	if(bIgnoreIllegalOps) break;	Ad_VlAb();	X = A = val; UpdateNZ(A);	break;
+		case 0xB3:	if(bIgnoreIllegalOps) break;	Ad_VlIy();	X = A = val; UpdateNZ(A);	break;
+		case 0xB7:	if(bIgnoreIllegalOps) break;	Ad_VlZy();	X = A = val; UpdateNZ(A);	break;
+		case 0xBF:	if(bIgnoreIllegalOps) break;	Ad_VlAy();	X = A = val; UpdateNZ(A);	break;
+
+			/* DCM	*/
+		case 0xC3:	if(bIgnoreIllegalOps) break;	MRW_Ix(DCM);				break;
+		case 0xC7:	if(bIgnoreIllegalOps) break;	MRW_Zp(DCM);				break;
+		case 0xCF:	if(bIgnoreIllegalOps) break;	MRW_Ab(DCM);				break;
+		case 0xD3:	if(bIgnoreIllegalOps) break;	MRW_Iy(DCM);				break;
+		case 0xD7:	if(bIgnoreIllegalOps) break;	MRW_Zx(DCM);				break;
+		case 0xDB:	if(bIgnoreIllegalOps) break;	MRW_Ay(DCM);				break;
+		case 0xDF:	if(bIgnoreIllegalOps) break;	MRW_Ax(DCM);				break;
+
+			/* INS	*/
+		case 0xE3:	if(bIgnoreIllegalOps) break;	MRW_Ix(INS);				break;
+		case 0xE7:	if(bIgnoreIllegalOps) break;	MRW_Zp(INS);				break;
+		case 0xEF:	if(bIgnoreIllegalOps) break;	MRW_Ab(INS);				break;
+		case 0xF3:	if(bIgnoreIllegalOps) break;	MRW_Iy(INS);				break;
+		case 0xF7:	if(bIgnoreIllegalOps) break;	MRW_Zx(INS);				break;
+		case 0xFB:	if(bIgnoreIllegalOps) break;	MRW_Ay(INS);				break;
+		case 0xFF:	if(bIgnoreIllegalOps) break;	MRW_Ax(INS);				break;
+
+			/* ALR
+					AND Accumulator with memory and LSR the result	*/
+		case 0x4B:	if(bIgnoreIllegalOps) break;	Ad_VlIm();	A &= val;	LSR(A);	break;
+
+			/* ARR
+					ANDs memory with the Accumulator and RORs the result	*/
+		case 0x6B:	if(bIgnoreIllegalOps) break;	Ad_VlIm();	A &= val;	ROR(A);	break;
+
+			/* XAA
+					Transfers X -> A, then ANDs A with memory				*/
+		case 0x8B:	if(bIgnoreIllegalOps) break;	Ad_VlIm();	A = X & val; UpdateNZ(A);	break;
+
+			/* OAL
+					OR the Accumulator with #EE, AND Accumulator with Memory, Transfer A -> X	*/
+		case 0xAB:	if(bIgnoreIllegalOps) break;	Ad_VlIm();	X = (A &= (val | 0xEE));
+													UpdateNZ(A);	break;
+
+			/* SAX
+					ANDs A and X registers (does not change A), subtracts memory from result (CMP style, not SBC style)
+					result is stored in X								*/
+		case 0xCB:	if(bIgnoreIllegalOps) break;
+				Ad_VlIm();	tw.W = (X & A) - val; X = tw.B.l;
+					ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | NZTable[X] | (tw.B.h ? C_FLAG : 0);	break;
+			/* SKB
+					Skip Byte... or DOP - Double No-Op
+					These bytes do nothing, but take a parameter (which can be ignored)	*/
+		case 0x04:	case 0x14:	case 0x34:	case 0x44:	case 0x54:	case 0x64:
+		case 0x80:	case 0x82:	case 0x89:	case 0xC2:	case 0xD4:	case 0xE2:	case 0xF4:
+			if(bIgnoreIllegalOps) break;
+			PC.W++;		//skip unused byte
+			break;
+
+			/* SKW
+					Swip Word... or TOP - Tripple No-Op
+					These bytes are the same as SKB, only they take a 2 byte parameter.
+					This can be ignored in some cases, but the read needs to be performed in a some cases
+					because an extra clock cycle may be used in the process		*/
+		case 0x0C:		//Absolute address... no need for operator
+			if(bIgnoreIllegalOps) break;
+			PC.W += 2;	break;
+		case 0x1C:	case 0x3C:	case 0x5C:	case 0x7C:	case 0xDC:	case 0xFC:	//Absolute X address... may cross page, have to perform the read
+			if(bIgnoreIllegalOps) break;
+			Ad_VlAx(); break;
+
+			/* HLT / JAM
+					Jams up CPU operation			*/
+		case 0x02:	case 0x12:	case 0x22:	case 0x32:	case 0x42:	case 0x52:
+		case 0x62:	case 0x72:	case 0x92:	case 0xB2:	case 0xD2:	case 0xF2:
+			if(PC.W == 0x5004)	bCPUJammed = 2;		//it's not -really- jammed... only the NSF code has ended
+			else
+			{
+				if(bIgnoreIllegalOps) break;
+				bCPUJammed = 1;
+			}
+			goto jammed;
+
+			/* TAS	*/
+		case 0x9B:
+			if(bIgnoreIllegalOps) break;
+			Ad_AdAy();
+			SP = A & X & (Rd(PC.W - 1) + 1);
+			Wr(final.W,SP);
+			break;
+
+			/* SAY	*/
+		case 0x9C:
+			if(bIgnoreIllegalOps) break;
+			Ad_AdAx();
+			Y &= (Rd(PC.W - 1) + 1);
+			Wr(final.W,Y);
+			break;
+
+			/* XAS	*/
+		case 0x9E:
+			if(bIgnoreIllegalOps) break;
+			Ad_AdAy();
+			X &= (Rd(PC.W - 1) + 1);
+			Wr(final.W,X);
+			break;
+
+			/* AXA	*/
+		case 0x93:	if(bIgnoreIllegalOps) break;	MRW_Iy(AXA);					break;
+		case 0x9F:	if(bIgnoreIllegalOps) break;	MRW_Ay(AXA);					break;
+
+			/* ANC	*/
+		case 0x0B:	case 0x2B:
+			if(bIgnoreIllegalOps) break;
+			Ad_VlIm();
+			A &= val;
+			ST = (ST & ~(N_FLAG|Z_FLAG|C_FLAG)) | NZTable[A] | ((A & 0x80) ? C_FLAG : 0);
+			break;
+
+			/* LAS	*/
+		case 0xBB:
+			if(bIgnoreIllegalOps) break;
+			Ad_VlAy();
+			X = A = (SP &= val);
+			UpdateNZ(A);
+			break;
+		}
+	}
+
+jammed:
+	regPC = PC.W;
+	regA = A;
+	regX = X;
+	regY = Y;
+	regSP = SP;
+	regP = ST;
+	
+	return (nCPUCycle - ret);
+}
+
+/****************** rockbox interface ******************/
+
+void set_codec_track(int t) {
+    int track,fade,def=0;
+    SetTrack(t);
+
+    // for REPEAT_ONE we disable track limits
+    if (rb->global_settings->repeat_mode!=REPEAT_ONE) {
+	if (!bIsExtended || nTrackTime[t]==-1) {track=60*2*1000; def=1;}
+	else track=nTrackTime[t];
+	if (!bIsExtended || nTrackFade[t]==-1) fade=5*1000;
+	else fade=nTrackFade[t];
+	nSilenceTrackMS=5000;
+	SetFadeTime(track,track+fade, fNSFPlaybackSpeed,def);
+    }
+    rb->id3->elapsed=t*1000;
+}
+
+/* this is the codec entry point */
+enum codec_status codec_start(struct codec_api *api)
+{
+    struct codec_api *ci;
+    int written;
+    uint8_t *buf;
+    size_t n;
+    int endofstream; /* end of stream flag */
+    int track;
+    int dontreinit;
+    char last_path[MAX_PATH];
+
+    /* Generic codec initialisation */
+    rb = api;
+    ci = api;
+    
+    #ifdef USE_IRAM
+    rb->memcpy(iramstart, iramcopy, iramend - iramstart);
+    rb->memset(iedata, 0, iend - iedata);
+    #endif
+    
+    /* we only render 16 bits */
+    ci->configure(DSP_SET_SAMPLE_DEPTH, (long *)16);
+    ci->configure(DSP_DITHER, (bool *)false);
+
+    ci->configure(DSP_SET_FREQUENCY, (long*)44100);
+    ci->configure(DSP_SET_STEREO_MODE, (long *)STEREO_MONO);
+
+    dontreinit=0;
+    last_path[0]='\0';
+    track=0;
+    
+next_track:
+    DEBUGF("NSF: next_track\n");
+    if (codec_init(api)) {
+        return CODEC_ERROR;
+    }
+    DEBUGF("NSF: after init\n");
+    
+
+    /* wait for track info to load */
+    while (!*ci->taginfo_ready && !ci->stop_codec)
+        ci->sleep(1);
+        
+    if(!NSFCore_Initialize()) {DEBUGF("NSF: NSFCore_Initialize failed\n"); return CODEC_ERROR;}
+
+    /* Read the entire file */
+    DEBUGF("NSF: request file\n");
+    ci->seek_buffer(0);
+    buf = ci->request_buffer(&n, ci->filesize);
+    if (!buf || n < (size_t)ci->filesize) {
+        DEBUGF("NSF: file load failed\n");
+        return CODEC_ERROR;
+    }
+    
+    if(LoadFile(buf,ci->filesize)) {DEBUGF("NSF: LoadFile failed\n"); return CODEC_ERROR;}
+    if(!SetPlaybackOptions(44100)) {DEBUGF("NSF: SetPlaybackOptions failed\n"); return CODEC_ERROR;}
+    if(!LoadNSF(nDataBufferSize)) {DEBUGF("NSF: LoadNSF failed\n"); return CODEC_ERROR;}
+
+
+    ci->id3->title=szGameTitle;
+    ci->id3->artist=szArtist;
+    ci->id3->album=szCopyright;
+
+    // TODO: playlists
+
+    if (!dontreinit || strcmp(ci->id3->path,last_path)) set_codec_track(track=nInitialTrack);
+    else set_codec_track(track);
+    strcpy(last_path,ci->id3->path);
+
+    /* The main decoder loop */
+    
+    endofstream = 0;
+    
+    while (!endofstream) {
+
+        ci->yield();
+        if (ci->stop_codec || ci->new_track) {
+            break;
+        }
+
+	    if (ci->seek_time >0) {
+	        track=ci->seek_time/1000;
+	        set_codec_track(track);
+	        ci->seek_complete();
+	    }
+
+	    written=GetSamples((BYTE*)samples,WAV_CHUNK_SIZE/2);
+	    
+	    if (!written || SongCompleted()) {
+	        track++;
+	        if (track>=nTrackCount) break;
+	        set_codec_track(track);
+	        continue;
+	    }
+
+        while (!ci->pcmbuf_insert((char *)samples, written))
+            ci->yield();
+
+    }
+
+    if (ci->request_next_track()) {
+	if (rb->global_settings->repeat_mode==REPEAT_ONE) {
+	    track++;
+	    if (track>=nTrackCount) track=0;
+	    dontreinit=1;
+	} else {
+	    dontreinit=0;
+	    last_path[0]='\0';
+	}
+	goto next_track;
+    }
+
+    return CODEC_OK;
+}
+
Index: firmware/id3.c
===================================================================
RCS file: /cvsroot/rockbox/firmware/id3.c,v
retrieving revision 1.134
diff -u -r1.134 id3.c
--- firmware/id3.c	25 Sep 2006 16:13:05 -0000	1.134
+++ firmware/id3.c	23 Oct 2006 06:42:49 -0000
@@ -121,6 +121,8 @@
     AFMT_ENTRY("SID",  "sid.codec",     NULL,                NULL  ),
     /* ADX File Format */
     AFMT_ENTRY("ADX", "adx.codec",	NULL,		     NULL  ),
+    /* NESM (NES Sound Format) */
+    AFMT_ENTRY("NSF", "nsf.codec",      NULL,                NULL  ),
 #endif
 };
 
Index: firmware/export/id3.h
===================================================================
RCS file: /cvsroot/rockbox/firmware/export/id3.h,v
retrieving revision 1.31
diff -u -r1.31 id3.h
--- firmware/export/id3.h	25 Sep 2006 16:13:05 -0000	1.31
+++ firmware/export/id3.h	23 Oct 2006 06:42:50 -0000
@@ -43,6 +43,7 @@
     AFMT_AIFF,         /* Audio Interchange File Format */
     AFMT_SID,          /* SID File Format */
     AFMT_ADX,          /* ADX */
+    AFMT_NSF,          /* NESM (NES Sound Format) */
 
     /* New formats must be added to the end of this list */
 
