summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/luaconf.h
blob: 109e680a174744878b141a7dc19b4775c31c62d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
** $Id$
** Configuration file for Lua
** See Copyright Notice in lua.h
*/


#ifndef lconfig_h
#define lconfig_h

#include <limits.h>
#include <stddef.h>


/*
** ==================================================================
** Search for "@@" to find all configurable definitions.
** ===================================================================
*/


/*
@@ LUA_ANSI controls the use of non-ansi features.
** CHANGE it (define it) if you want Lua to avoid the use of any
** non-ansi feature or library.
*/
#if defined(__STRICT_ANSI__)
#define LUA_ANSI
#endif


#if !defined(LUA_ANSI) && defined(_WIN32)
#define LUA_WIN
#endif

#if defined(LUA_USE_LINUX)
#define LUA_USE_POSIX
#define LUA_USE_DLOPEN		/* needs an extra library: -ldl */
#define LUA_USE_READLINE	/* needs some extra libraries */
#endif

#if defined(LUA_USE_MACOSX)
#define LUA_USE_POSIX
#define LUA_DL_DYLD		/* does not need extra library */
#endif



/*
@@ LUA_USE_POSIX includes all functionallity listed as X/Open System
@* Interfaces Extension (XSI).
** CHANGE it (define it) if your system is XSI compatible.
*/
#if defined(LUA_USE_POSIX)
#define LUA_USE_MKSTEMP
#define LUA_USE_ISATTY
#define LUA_USE_POPEN
#define LUA_USE_ULONGJMP
#endif


/*
@@ LUA_PATH and LUA_CPATH are the names of the environment variables that
@* Lua check to set its paths.
@@ LUA_INIT is the name of the environment variable that Lua
@* checks for initialization code.
** CHANGE them if you want different names.
*/
#define LUA_PATH        "LUA_PATH"
#define LUA_CPATH       "LUA_CPATH"
#define LUA_INIT	"LUA_INIT"


/*
@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
@* Lua libraries.
@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
@* C libraries.
** CHANGE them if your machine has a non-conventional directory
** hierarchy or if you want to install your libraries in
** non-conventional directories.
*/
#if defined(_WIN32)
/*
** In Windows, any exclamation mark ('!') in the path is replaced by the
** path of the directory of the executable file of the current process.
*/
#define LUA_LDIR	"!\\lua\\"
#define LUA_CDIR	"!\\"
#define LUA_PATH_DEFAULT  \
		".\\?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?\\init.lua;" \
		             LUA_CDIR"?.lua;"  LUA_CDIR"?\\init.lua"
#define LUA_CPATH_DEFAULT \
	".\\?.dll;"  LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"

#else
#define LUA_ROOT	"/usr/local/"
#define LUA_LDIR	LUA_ROOT "share/lua/5.1/"
#define LUA_CDIR	LUA_ROOT "lib/lua/5.1/"
#define LUA_PATH_DEFAULT  \
		"./?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?/init.lua;" \
		            LUA_CDIR"?.lua;"  LUA_CDIR"?/init.lua"
#define LUA_CPATH_DEFAULT \
	"./?.so;"  LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
#endif


/*
@@ LUA_DIRSEP is the directory separator (for submodules).
** CHANGE it if your machine does not use "/" as the directory separator
** and is not Windows. (On Windows Lua automatically uses "\".)
*/
#if defined(_WIN32)
#define LUA_DIRSEP	"\\"
#else
#define LUA_DIRSEP	"/"
#endif


/*
@@ LUA_PATHSEP is the character that separates templates in a path.
@@ LUA_PATH_MARK is the string that marks the substitution points in a
@* template.
@@ LUA_EXECDIR in a Windows path is replaced by the executable's
@* directory.
@@ LUA_IGMARK is a mark to ignore all before it when bulding the
@* luaopen_ function name.
** CHANGE them if for some reason your system cannot use those
** characters. (E.g., if one of those characters is a common character
** in file/directory names.) Probably you do not need to change them.
*/
#define LUA_PATHSEP	";"
#define LUA_PATH_MARK	"?"
#define LUA_EXECDIR	"!"
#define LUA_IGMARK	"-"


/*
@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
** machines, ptrdiff_t gives a good choice between int or long.)
*/
#define LUA_INTEGER	ptrdiff_t


/*
@@ LUA_API is a mark for all core API functions.
@@ LUALIB_API is a mark for all standard library functions.
** CHANGE them if you need to define those functions in some special way.
** For instance, if you want to create one Windows DLL with the core and
** the libraries, you may want to use the following definition (define
** LUA_BUILD_AS_DLL to get it).
*/
#if defined(LUA_BUILD_AS_DLL)

#if defined(LUA_CORE) || defined(LUA_LIB)
#define LUA_API __declspec(dllexport)
#else
#define LUA_API __declspec(dllimport)
#endif

#else

#define LUA_API		extern

#endif

/* more often than not the libs go together with the core */
#define LUALIB_API	LUA_API


/*
@@ LUAI_FUNC is a mark for all extern functions that are not to be
@* exported to outside modules.
@@ LUAI_DATA is a mark for all extern (const) variables that are not to
@* be exported to outside modules.
** CHANGE them if you need to mark them in some special way. Elf/gcc
** (versions 3.2 and later) mark them as "hidden" to optimize access
** when Lua is compiled as a shared library.
*/
#if defined(luaall_c)
#define LUAI_FUNC	static
#define LUAI_DATA	/* empty */

#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
      defined(__ELF__)
#define LUAI_FUNC	__attribute__((visibility("hidden"))) extern
#define LUAI_DATA	LUAI_FUNC

#else
#define LUAI_FUNC	extern
#define LUAI_DATA	extern
#endif



/*
@@ LUA_QL describes how error messages quote program elements.
** CHANGE it if you want a different appearance.
*/
#define LUA_QL(x)	"'" x "'"
#define LUA_QS		LUA_QL("%s")


/*
@@ LUA_IDSIZE gives the maximum size for the description of the source
@* of a function in debug information.
** CHANGE it if you want a different size.
*/
#define LUA_IDSIZE	60


/*
** {==================================================================
** Stand-alone configuration
** ===================================================================
*/

#if defined(lua_c) || defined(luaall_c)

/*
@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that
@* is, whether we're running lua interactively).
** CHANGE it if you have a better definition for non-POSIX/non-Windows
** systems.
*/
#if defined(LUA_USE_ISATTY)
#include <unistd.h>
#define lua_stdin_is_tty()	isatty(0)
#elif defined(LUA_WIN)
#include <io.h>
#include <stdio.h>
#define lua_stdin_is_tty()	_isatty(_fileno(stdin))
#else
#define lua_stdin_is_tty()	1  /* assume stdin is a tty */
#endif


/*
@@ LUA_PROMPT is the default prompt used by stand-alone Lua.
@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.
** CHANGE them if you want different prompts. (You can also change the
** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)
*/
#define LUA_PROMPT		"> "
#define LUA_PROMPT2		">> "


/*
@@ LUA_PROGNAME is the default name for the stand-alone Lua program.
** CHANGE it if your stand-alone interpreter has a different name and
** your system is not able to detect that name automatically.
*/
#define LUA_PROGNAME		"lua"


/*
@@ LUA_MAXINPUT is the maximum length for an input line in the
@* stand-alone interpreter.
** CHANGE it if you need longer lines.
*/
#define LUA_MAXINPUT	512


/*
@@ lua_readline defines how to show a prompt and then read a line from
@* the standard input.
@@ lua_saveline defines how to "save" a read line in a "history".
@@ lua_freeline defines how to free a line read by lua_readline.
** CHANGE them if you want to improve this functionality (e.g., by using
** GNU readline and history facilities).
*/
#if defined(LUA_USE_READLINE)
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#define lua_readline(L,b,p)	((void)L, ((b)=readline(p)) != NULL)
#define lua_saveline(L,idx) \
	if (lua_strlen(L,idx) > 0)  /* non-empty line? */ \
	  add_history(lua_tostring(L, idx));  /* add it to history */
#define lua_freeline(L,b)	((void)L, free(b))
#else
#define lua_readline(L,b,p)	\
	((void)L, fputs(p, stdout), fflush(stdout),  /* show prompt */ \
	fgets(b, LUA_MAXINPUT, stdin) != NULL)  /* get line */
#define lua_saveline(L,idx)	{ (void)L; (void)idx; }
#define lua_freeline(L,b)	{ (void)L; (void)b; }
#endif

#endif

/* }================================================================== */


/*
@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles
@* as a percentage.
** CHANGE it if you want the GC to run faster or slower (higher values
** mean larger pauses which mean slower collection.) You can also change
** this value dynamically.
*/
#define LUAI_GCPAUSE	200  /* 200% (wait memory to double before next GC) */


/*
@@ LUAI_GCMUL defines the default speed of garbage collection relative to
@* memory allocation as a percentage.
** CHANGE it if you want to change the granularity of the garbage
** collection. (Higher values mean coarser collections. 0 represents
** infinity, where each step performs a full collection.) You can also
** change this value dynamically.
*/
#define LUAI_GCMUL	200 /* GC runs 'twice the speed' of memory allocation */



/*
@@ LUA_COMPAT_GETN controls compatibility with old getn behavior.
** CHANGE it (define it) if you want exact compatibility with the
** behavior of setn/getn in Lua 5.0.
*/
#undef LUA_COMPAT_GETN

/*
@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib.
** CHANGE it to undefined as soon as you do not need a global 'loadlib'
** function (the function is still available as 'package.loadlib').
*/
#undef LUA_COMPAT_LOADLIB

/*
@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature.
** CHANGE it to undefined as soon as your programs use only '...' to
** access vararg parameters (instead of the old 'arg' table).
*/
#undef LUA_COMPAT_VARARG

/*
@@ LUA_COMPAT_MOD controls compatibility with old math.mod function.
** CHANGE it to undefined as soon as your programs use 'math.fmod' or
** the new '%' operator instead of 'math.mod'.
*/
#undef LUA_COMPAT_MOD

/*
@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting
@* facility.
** CHANGE it to 2 if you want the old behaviour, or undefine it to turn
** off the advisory error when nesting [[...]].
*/
#undef LUA_COMPAT_LSTR

/*
@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name.
** CHANGE it to undefined as soon as you rename 'string.gfind' to
** 'string.gmatch'.
*/
#undef LUA_COMPAT_GFIND

/*
@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib'
@* behavior.
** CHANGE it to undefined as soon as you replace to 'luaL_registry'
** your uses of 'luaL_openlib'
*/
#undef LUA_COMPAT_OPENLIB



/*
@@ luai_apicheck is the assert macro used by the Lua-C API.
** CHANGE luai_apicheck if you want Lua to perform some checks in the
** parameters it gets from API calls. This may slow down the interpreter
** a bit, but may be quite useful when debugging C code that interfaces
** with Lua. A useful redefinition is to use assert.h.
*/
#if defined(LUA_USE_APICHECK)
#include <assert.h>
#define luai_apicheck(L,o)	{ (void)L; assert(o); }
#else
#define luai_apicheck(L,o)	{ (void)L; }
#endif


/*
@@ LUAI_BITSINT defines the number of bits in an int.
** CHANGE here if Lua cannot automatically detect the number of bits of
** your machine. Probably you do not need to change this.
*/
/* avoid overflows in comparison */
#if INT_MAX-20 < 32760
#define LUAI_BITSINT	16
#elif INT_MAX > 2147483640L
/* int has at least 32 bits */
#define LUAI_BITSINT	32
#else
#error "you must define LUA_BITSINT with number of bits in an integer"
#endif


/*
@@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
@@ LUAI_INT32 is an signed integer with at least 32 bits.
@@ LUAI_UMEM is an unsigned integer big enough to count the total
@* memory used by Lua.
@@ LUAI_MEM is a signed integer big enough to count the total memory
@* used by Lua.
** CHANGE here if for some weird reason the default definitions are not
** good enough for your machine. (The definitions in the 'else'
** part always works, but may waste space on machines with 64-bit
** longs.) Probably you do not need to change this.
*/
#if LUAI_BITSINT >= 32
#define LUAI_UINT32	unsigned int
#define LUAI_INT32	int
#define LUAI_MAXINT32	INT_MAX
#define LUAI_UMEM	size_t
#define LUAI_MEM	ptrdiff_t
#else
/* 16-bit ints */
#define LUAI_UINT32	unsigned long
#define LUAI_INT32	long
#define LUAI_MAXINT32	LONG_MAX
#define LUAI_UMEM	unsigned long
#define LUAI_MEM	long
#endif


/*
@@ LUAI_MAXCALLS limits the number of nested calls.
** CHANGE it if you need really deep recursive calls. This limit is
** arbitrary; its only purpose is to stop infinite recursion before
** exhausting memory.
*/
#define LUAI_MAXCALLS	20000


/*
@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function
@* can use.
** CHANGE it if you need lots of (Lua) stack space for your C
** functions. This limit is arbitrary; its only purpose is to stop C
** functions to consume unlimited stack space. (must be smaller than
** -LUA_REGISTRYINDEX)
*/
#define LUAI_MAXCSTACK 8000



/*
** {==================================================================
** CHANGE (to smaller values) the following definitions if your system
** has a small C stack. (Or you may want to change them to larger
** values if your system has a large C stack and these limits are
** too rigid for you.) Some of these constants control the size of
** stack-allocated arrays used by the compiler or the interpreter, while
** others limit the maximum number of recursive calls that the compiler
** or the interpreter can perform. Values too large may cause a C stack
** overflow for some forms of deep constructs.
** ===================================================================
*/


/*
@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and
@* syntactical nested non-terminals in a program.
*/
#define LUAI_MAXCCALLS		200


/*
@@ LUAI_MAXVARS is the maximum number of local variables per function
@* (must be smaller than 250).
*/
#define LUAI_MAXVARS		200


/*
@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function
@* (must be smaller than 250).
*/
#define LUAI_MAXUPVALUES	60


/*
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
*/
#define LUAL_BUFFERSIZE		1024

/* }================================================================== */




/*
** {==================================================================
@@ LUA_NUMBER is the type of numbers in Lua.
** CHANGE the following definitions only if you want to build Lua
** with a number type different from double. You may also need to
** change lua_number2int & lua_number2integer.
** ===================================================================
*/

#undef LUA_NUMBER_DOUBLE
#define LUA_NUMBER	long

/*
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
@* over a number.
*/
#define LUAI_UACNUMBER	long


/*
@@ LUA_NUMBER_SCAN is the format for reading numbers.
@@ LUA_NUMBER_FMT is the format for writing numbers.
@@ lua_number2str converts a number to a string.
@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
@@ lua_str2number converts a string to a number.
*/
#define LUA_NUMBER_SCAN		"%ld"
#define LUA_NUMBER_FMT		"%ld"
#define LUAI_MAXNUMBER2STR	32 /* 16 digits, sign, point, and \0 */
#define lua_number2str(s,n)	snprintf((s), 32, LUA_NUMBER_FMT, (n))
#define lua_str2number(s,p)	strtol((s), (p), 10)


/*
@@ The luai_num* macros define the primitive operations over numbers.
*/
#if defined(LUA_CORE)
extern long rb_pow(long, long);
#define luai_numadd(a,b)	((a)+(b))
#define luai_numsub(a,b)	((a)-(b))
#define luai_nummul(a,b)	((a)*(b))
#define luai_numdiv(a,b)	((a)/(b))
#define luai_nummod(a,b)	((a)%(b))
#define luai_numpow(a,b)	(rb_pow(a,b))
#define luai_numunm(a)		(-(a))
#define luai_numeq(a,b)		((a)==(b))
#define luai_numlt(a,b)		((a)<(b))
#define luai_numle(a,b)		((a)<=(b))
#define luai_numisnan(a)	(!luai_numeq((a), (a)))
#endif


/*
@@ lua_number2int is a macro to convert lua_Number to int.
@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
** CHANGE them if you know a faster way to convert a lua_Number to
** int (with any rounding method and without throwing errors) in your
** system. In Pentium machines, a naive typecast from double to int
** in C is extremely slow, so any alternative is worth trying.
*/

/* On a Pentium, resort to a trick */
#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
    (defined(__i386) || defined (_M_IX86) || defined(__i386__))

/* On a Microsoft compiler, use assembler */
#if defined(_MSC_VER)

#define lua_number2int(i,d)   __asm fld d   __asm fistp i
#define lua_number2integer(i,n)		lua_number2int(i, n)

/* the next trick should work on any Pentium, but sometimes clashes
   with a DirectX idiosyncrasy */
#else

union luai_Cast { double l_d; long l_l; };
#define lua_number2int(i,d) \
  { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
#define lua_number2integer(i,n)		lua_number2int(i, n)

#endif


/* this option always works, but may be slow */
#else
#define lua_number2int(i,d)	((i)=(int)(d))
#define lua_number2integer(i,d)	((i)=(lua_Integer)(d))

#endif

/* }================================================================== */


/*
@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
** CHANGE it if your system requires alignments larger than double. (For
** instance, if your system supports long doubles and they must be
** aligned in 16-byte boundaries, then you should add long double in the
** union.) Probably you do not need to change this.
*/
#define LUAI_USER_ALIGNMENT_T	union { void *s; long l; }


/*
@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling.
** CHANGE them if you prefer to use longjmp/setjmp even with C++
** or if want/don't to use _longjmp/_setjmp instead of regular
** longjmp/setjmp. By default, Lua handles errors with exceptions when
** compiling as C++ code, with _longjmp/_setjmp when asked to use them,
** and with longjmp/setjmp otherwise.
*/
#if defined(__cplusplus)
/* C++ exceptions */
#define LUAI_THROW(L,c)	throw(c)
#define LUAI_TRY(L,c,a)	try { a } catch(...) \
	{ if ((c)->status == 0) (c)->status = -1; }
#define luai_jmpbuf	int  /* dummy variable */

#elif defined(LUA_USE_ULONGJMP)
/* in Unix, try _longjmp/_setjmp (more efficient) */
#define LUAI_THROW(L,c)	_longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a)	if (_setjmp((c)->b) == 0) { a }
#define luai_jmpbuf	jmp_buf

#else
/* default handling with long jumps */
#define LUAI_THROW(L,c)	longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a)	if (setjmp((c)->b) == 0) { a }
#define luai_jmpbuf	jmp_buf

#endif


/*
@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern
@* can do during pattern-matching.
** CHANGE it if you need more captures. This limit is arbitrary.
*/
#define LUA_MAXCAPTURES		32


/*
@@ lua_tmpnam is the function that the OS library uses to create a
@* temporary name.
@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam.
** CHANGE them if you have an alternative to tmpnam (which is considered
** insecure) or if you want the original tmpnam anyway.  By default, Lua
** uses tmpnam except when POSIX is available, where it uses mkstemp.
*/
#if defined(loslib_c) || defined(luaall_c)

#if defined(LUA_USE_MKSTEMP)
#include <unistd.h>
#define LUA_TMPNAMBUFSIZE	32
#define lua_tmpnam(b,e)	{ \
	strcpy(b, "/tmp/lua_XXXXXX"); \
	e = mkstemp(b); \
	if (e != -1) close(e); \
	e = (e == -1); }

#else
#define LUA_TMPNAMBUFSIZE	L_tmpnam
#define lua_tmpnam(b,e)		{ e = (tmpnam(b) == NULL); }
#endif

#endif


/*
@@ lua_popen spawns a new process connected to the current one through
@* the file streams.
** CHANGE it if you have a way to implement it in your system.
*/
#if defined(LUA_USE_POPEN)

#define lua_popen(L,c,m)	((void)L, fflush(NULL), popen(c,m))
#define lua_pclose(L,file)	((void)L, (pclose(file) != -1))

#elif defined(LUA_WIN)

#define lua_popen(L,c,m)	((void)L, _popen(c,m))
#define lua_pclose(L,file)	((void)L, (_pclose(file) != -1))

#else

#define lua_popen(L,c,m)	((void)((void)c, m),  \
		luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
#define lua_pclose(L,file)		((void)((void)L, file), 0)

#endif

/*
@@ LUA_DL_* define which dynamic-library system Lua should use.
** CHANGE here if Lua has problems choosing the appropriate
** dynamic-library system for your platform (either Windows' DLL, Mac's
** dyld, or Unix's dlopen). If your system is some kind of Unix, there
** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for
** it.  To use dlopen you also need to adapt the src/Makefile (probably
** adding -ldl to the linker options), so Lua does not select it
** automatically.  (When you change the makefile to add -ldl, you must
** also add -DLUA_USE_DLOPEN.)
** If you do not want any kind of dynamic library, undefine all these
** options.
** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD.
*/
#if defined(LUA_USE_DLOPEN)
#define LUA_DL_DLOPEN
#endif

#if defined(LUA_WIN)
#define LUA_DL_DLL
#endif


/*
@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State
@* (the data goes just *before* the lua_State pointer).
** CHANGE (define) this if you really need that. This value must be
** a multiple of the maximum alignment required for your machine.
*/
#define LUAI_EXTRASPACE		0


/*
@@ luai_userstate* allow user-specific actions on threads.
** CHANGE them if you defined LUAI_EXTRASPACE and need to do something
** extra when a thread is created/deleted/resumed/yielded.
*/
#define luai_userstateopen(L)		((void)L)
#define luai_userstateclose(L)		((void)L)
#define luai_userstatethread(L,L1)	((void)L)
#define luai_userstatefree(L)		((void)L)
#define luai_userstateresume(L,n)	((void)L)
#define luai_userstateyield(L,n)	((void)L)


/*
@@ LUA_INTFRMLEN is the length modifier for integer conversions
@* in 'string.format'.
@@ LUA_INTFRM_T is the integer type correspoding to the previous length
@* modifier.
** CHANGE them if your system supports long long or does not support long.
*/

#if defined(LUA_USELONGLONG)

#define LUA_INTFRMLEN		"ll"
#define LUA_INTFRM_T		long long

#else

#define LUA_INTFRMLEN		"l"
#define LUA_INTFRM_T		long

#endif



/* =================================================================== */

/*
** Local configuration. You can use this space to add your redefinitions
** without modifying the main part of the file.
*/

#include "rockconf.h"

#endif

='n2001' href='#n2001'>2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2006 Adam Gashlin (hcs)
 * Copyright (C) 2004 Disch
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/*
 * This is a perversion of Disch's excellent NotSoFatso.
 */

#include "codeclib.h"
#include "inttypes.h"
#include "system.h"

CODEC_HEADER

/* arm doesn't benefit from IRAM? */
#ifdef CPU_ARM
#undef ICODE_ATTR
#define ICODE_ATTR
#undef IDATA_ATTR
#define IDATA_ATTR
#else
#define ICODE_INSTEAD_OF_INLINE
#endif

/* Maximum number of bytes to process in one iteration */
#define WAV_CHUNK_SIZE (1024*2)

static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR;

#define ZEROMEMORY(addr,size) memset(addr,0,size)

/* simple profiling with USEC_TIMER

#define NSF_PROFILE

*/

#ifdef NSF_PROFILE

#define CREATE_TIMER(name) uint32_t nsf_timer_##name##_start,\
    nsf_timer_##name##_total
#define ENTER_TIMER(name) nsf_timer_##name##_start=USEC_TIMER
#define EXIT_TIMER(name) nsf_timer_##name##_total+=\
    (USEC_TIMER-nsf_timer_##name##_start)
#define READ_TIMER(name) (nsf_timer_##name##_total)
#define RESET_TIMER(name) nsf_timer_##name##_total=0

#define PRINT_TIMER_PCT(bname,tname,nstr) ci->fdprintf(
    logfd,"%10ld ",READ_TIMER(bname));\
    ci->fdprintf(logfd,"(%3d%%) " nstr "\t",\
    ((uint64_t)READ_TIMER(bname))*100/READ_TIMER(tname))

CREATE_TIMER(total);
CREATE_TIMER(cpu);
CREATE_TIMER(apu);
CREATE_TIMER(squares);
CREATE_TIMER(tnd);
CREATE_TIMER(tnd_enter);
CREATE_TIMER(tnd_tri);
CREATE_TIMER(tnd_noise);
CREATE_TIMER(tnd_dmc);
CREATE_TIMER(fds);
CREATE_TIMER(frame);
CREATE_TIMER(mix);

void reset_profile_timers(void) {
    RESET_TIMER(total);
    RESET_TIMER(cpu);
    RESET_TIMER(apu);
    RESET_TIMER(squares);
    RESET_TIMER(tnd);
    RESET_TIMER(tnd_enter);
    RESET_TIMER(tnd_tri);
    RESET_TIMER(tnd_noise);
    RESET_TIMER(tnd_dmc);
    RESET_TIMER(fds);
    RESET_TIMER(frame);
    RESET_TIMER(mix);
}

int logfd=-1;

void print_timers(char * path, int track) {
    logfd = ci->open("/nsflog.txt",O_WRONLY|O_CREAT|O_APPEND, 0666);
    ci->fdprintf(logfd,"%s[%d]:\t",path,track);
    ci->fdprintf(logfd,"%10ld total\t",READ_TIMER(total));
    PRINT_TIMER_PCT(cpu,total,"CPU");
    PRINT_TIMER_PCT(apu,total,"APU");
    ci->fdprintf(logfd,"\n\t");
    PRINT_TIMER_PCT(squares,apu,"squares");
    PRINT_TIMER_PCT(frame,apu,"frame");
    PRINT_TIMER_PCT(mix,apu,"mix");
    PRINT_TIMER_PCT(fds,apu,"FDS");
    PRINT_TIMER_PCT(tnd,apu,"tnd");
    ci->fdprintf(logfd,"\n\t\t");
    PRINT_TIMER_PCT(tnd_enter,tnd,"enter");
    PRINT_TIMER_PCT(tnd_tri,tnd,"triangle");
    PRINT_TIMER_PCT(tnd_noise,tnd,"noise");
    PRINT_TIMER_PCT(tnd_dmc,tnd,"DMC");
    ci->fdprintf(logfd,"\n");

    ci->close(logfd);
    logfd=-1;
}

#else

#define CREATE_TIMER(name)
#define ENTER_TIMER(name)
#define EXIT_TIMER(name)
#define READ_TIMER(name)
#define RESET_TIMER(name)
#define print_timers(path,track)
#define reset_profile_timers()

#endif

/* proper handling of multibyte values */
#ifdef ROCKBOX_LITTLE_ENDIAN
union TWIN
{
    uint16_t                        W;
    struct{ uint8_t l; uint8_t h; } B;
};

union QUAD
{
    uint32_t                                    D;
    struct{ uint8_t l; uint8_t h; uint16_t w; } B;
};
#else

union TWIN
{
    uint16_t                        W;
    struct{ uint8_t h; uint8_t l; } B;
};

union QUAD
{
    uint32_t                                    D;
    struct{uint16_t w; uint8_t h; uint8_t 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] ICONST_ATTR = {0,1,2,4,0,-4,-2,-1};
const uint16_t  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 uint8_t DUTY_CYCLE_TABLE[4] = {2,4,8,12};

const uint8_t 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 uint16_t 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
{
    uint32_t        nHeader;
    uint8_t         nHeaderExtra;
    uint8_t         nVersion;
    uint8_t         nTrackCount;
    uint8_t         nInitialTrack;
    uint16_t        nLoadAddress;
    uint16_t        nInitAddress;
    uint16_t        nPlayAddress;
    uint8_t         szGameTitle[32];
    uint8_t         szArtist[32];
    uint8_t         szCopyright[32];
    uint16_t        nSpeedNTSC;
    uint8_t         nBankSwitch[8];
    uint16_t        nSpeedPAL;
    uint8_t         nNTSC_PAL;
    uint8_t         nExtraChip;
    uint8_t         nExpansion[4];
};

struct NSFE_INFOCHUNK
{
    uint16_t        nLoadAddress;
    uint16_t        nInitAddress;
    uint16_t        nPlayAddress;
    uint8_t         nIsPal;
    uint8_t         nExt;
    uint8_t         nTrackCount;
    uint8_t         nStartingTrack;
};

int32_t     LoadFile(uint8_t *,size_t);

int32_t     LoadFile_NESM(uint8_t *,size_t);
int32_t     LoadFile_NSFE(uint8_t *,size_t);

/* NSF file info */

/* basic NSF info */
int32_t     bIsExtended=0;      /* 0 = NSF, 1 = NSFE */
uint8_t     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 */
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) */
uint8_t     nChipExtensions=0;  /* Bitwise representation of the external chips
                                   used by this NSF.  */
    
/* old NESM speed stuff (blarg) */
int32_t     nNTSC_PlaySpeed=0;
int32_t     nPAL_PlaySpeed=0;

/* track info */
/* The number of tracks in the NSF (1 = 1 track, 5 = 5 tracks, etc) */
int32_t     nTrackCount=0;
/* The initial track (ZERO BASED:  0 = 1st track, 4 = 5th track, etc) */
int32_t     nInitialTrack=0;

/* nsf data */
uint8_t*    pDataBuffer=0;      /* the buffer containing NSF code. */
int32_t     nDataBufferSize=0;  /* the size of the above buffer. */

/* playlist */
uint8_t     nPlaylist[256];     /* Each entry is the zero based index of the
                                   song to play */
int32_t     nPlaylistSize=0;    /* the number of tracks in the playlist */

/* track time / fade */
int32_t     nTrackTime[256];    /* track times -1 if no track times specified */
int32_t     nTrackFade[256];    /* track fade times -1 if none are specified */

/* string info */
uint8_t     szGameTitle[0x101];
uint8_t     szArtist[0x101];
uint8_t     szCopyright[0x101];
uint8_t     szRipper[0x101];

/* bankswitching info */
uint8_t     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(uint8_t * 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(uint8_t* inbuffer, size_t size)
{
    uint8_t ignoreversion=1;
    uint8_t 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;
    /* stupid NSFs claim to be above version 1  >_> */
    if((!ignoreversion) && (hdr.nVersion != 1)) return -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);
    nNTSC_PlaySpeed =           letoh16(hdr.nSpeedNTSC);
    nfileLoadAddress =          letoh16(hdr.nLoadAddress);
    nfileInitAddress =          letoh16(hdr.nInitAddress);
    nfilePlayAddress =          letoh16(hdr.nPlayAddress);
    nChipExtensions =           hdr.nExtraChip;


    nTrackCount =               hdr.nTrackCount;
    nInitialTrack =             hdr.nInitialTrack - 1;

    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(uint8_t* inbuffer, size_t size)
{
    /* the vars we'll be using */
    uint32_t nChunkType;
    int32_t  nChunkSize;
    int32_t  nChunkUsed;
    int32_t i;
    uint8_t *  nDataPos = 0;
    uint8_t bInfoFound = 0;
    uint8_t bEndFound = 0;
    uint8_t 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)) {
            /* only one info chunk permitted */
            if(bInfoFound)                      return -1;
            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 =        (uint16_t)(1000000 / PAL_NMIRATE);
            nNTSC_PlaySpeed =       (uint16_t)(1000000 / NTSC_NMIRATE);
        } 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]=
                    ((uint32_t)inbuffer[0])|
                    ((uint32_t)inbuffer[1]<<8)|
                    ((uint32_t)inbuffer[2]<<16)|
                    ((uint32_t)inbuffer[3]<<24);
            }

            inbuffer+=nChunkSize-(nChunkUsed*4);

            /* negative signals to use default time */
            for(; nChunkUsed < nTrackCount; nChunkUsed++)
                nTrackTime[nChunkUsed] = -1;
        } else if (!memcmp(&nChunkType,"fade",4)) {
            if(!bInfoFound)                     return -1;
            for (nChunkUsed=0; nChunkUsed < MIN(nChunkSize / 4,nTrackCount);
                 nChunkUsed++,inbuffer+=4) {
                nTrackFade[nChunkUsed]=
                    ((uint32_t)inbuffer[0])|
                    ((uint32_t)inbuffer[1]<<8)|
                    ((uint32_t)inbuffer[2]<<16)|
                    ((uint32_t)inbuffer[3]<<24);
            }

            inbuffer+=nChunkSize-(nChunkUsed*4);

            /* negative signals to use default time */
            for(; nChunkUsed < nTrackCount; nChunkUsed++)
                nTrackFade[nChunkUsed] = -1;
        } 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)) {
            uint8_t*        ptr;

            ptr = inbuffer;

            uint8_t*    ar[4] = {szGameTitle,szArtist,szCopyright,szRipper};
            int32_t     i;
            for(i = 0; (ptr-inbuffer)<nChunkSize && i < 4; i++)
            {
                nChunkUsed = strlen(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 */
            /* chunk is vital... don't continue */
            if((nChunkType >= 'A') && (nChunkType <= 'Z'))
                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   */
    uint8_t     bEnvelopeEnable;
    uint8_t     nEnvelopeSpeed;

    /*  Volume Envelope */
    uint8_t     nVolEnv_Mode;
    uint8_t     nVolEnv_Decay;
    uint8_t     nVolEnv_Gain;
    int32_t     nVolEnv_Timer;
    int32_t     nVolEnv_Count;
    uint8_t     nVolume;
    uint8_t     bVolEnv_On;

    /*  Sweep Envenlope */
    uint8_t     nSweep_Mode;
    uint8_t     nSweep_Decay;
    int32_t     nSweep_Timer;
    int32_t     nSweep_Count;
    uint8_t     nSweep_Gain;
    uint8_t     bSweepEnv_On;

    /*  Effector / LFO / Modulation Unit    */
    int32_t     nSweepBias;
    uint8_t     bLFO_Enabled;
    union TWIN  nLFO_Freq;
    /*float       fLFO_Timer;*/
    /*float       fLFO_Count;*/
    int32_t    nLFO_Timer; /* -17.14*/
    int32_t    nLFO_Count; /* -17.14*/
    uint8_t     nLFO_Addr;
    uint8_t     nLFO_Table[0x40];
    uint8_t     bLFO_On;

    /*  Main Output     */
    uint8_t     nMainVolume;
    uint8_t     bEnabled;
    union TWIN  nFreq;
    /*float       fFreqCount;*/
    int32_t    nFreqCount; /* -17.14 */
    uint8_t     nMainAddr;
    uint8_t     nWaveTable[0x40];
    uint8_t     bWaveWrite;
    uint8_t     bMain_On;

    /*  Output and Downsampling */
    int32_t     nMixL;
    
    /*  Pop Reducer             */
    uint8_t     bPopReducer;
    uint8_t     nPopOutput;
    int32_t     nPopCount;
    
};
int16_t     FDS_nOutputTable_L[4][0x21][0x40];

struct FME07Wave
{
    /* Frequency Control */
    union TWIN  nFreqTimer;
    int32_t     nFreqCount;

    /* Channel Disabling */
    uint8_t     bChannelEnabled;

    /* Volume */
    uint8_t     nVolume;

    /* Duty Cycle */
    uint8_t     nDutyCount;

    /* Output and Downsampling */
    int32_t     nMixL;
};

int16_t     FME07_nOutputTable_L[0x10] IDATA_ATTR;

struct N106Wave
{
    /*  All Channel Stuff */

    uint8_t     nActiveChannels;
    uint8_t     bAutoIncrement;
    uint8_t     nCurrentAddress;
    uint8_t     nRAM[0x100];      /* internal memory for registers/wave data */
    int32_t     nFrequencyLookupTable[8]; /* lookup tbl for freq conversions */

    /*
     *  Individual channel stuff
     */
    /*  Wavelength / Frequency */
    union QUAD  nFreqReg[8];
    int32_t     nFreqTimer[8];
    int32_t     nFreqCount[8];

    /*  Wave data length / remaining */
    uint8_t     nWaveSize[8];
    uint8_t     nWaveRemaining[8];

    /*  Wave data position */
    uint8_t     nWavePosStart[8];
    uint8_t     nWavePos[8];
    uint8_t     nOutput[8];

    /*  Volume */
    uint8_t     nVolume[8];

    /*  Pop Reducer */
    uint8_t     nPreVolume[8];
    uint8_t     nPopCheck[8];

    /* Mixing */
    int32_t     nMixL[8];
};

int16_t     N106_nOutputTable_L[0x10][0x10];

struct VRC6PulseWave
{

    /* Frequency Control */
    union TWIN  nFreqTimer;
    int32_t     nFreqCount;

    /* Flags */
    uint8_t     bChannelEnabled;
    uint8_t     bDigitized;

    /* Volume */
    uint8_t     nVolume;

    /* Duty Cycle */
    uint8_t     nDutyCycle;
    uint8_t     nDutyCount;

    /* Output and Downsampling */
    int32_t     nMixL;
    
};

int16_t     VRC6Pulse_nOutputTable_L[0x10] IDATA_ATTR;

struct VRC6SawWave
{

    /* Frequency Control */
    union TWIN  nFreqTimer;
    int32_t     nFreqCount;

    /* Flags */
    uint8_t     bChannelEnabled;

    /* Phase Accumulator */
    uint8_t     nAccumRate;
    uint8_t     nAccum;
    uint8_t     nAccumStep;

    /* Output and Downsampling */
    int32_t     nMixL;
    
};

int16_t     VRC6Saw_nOutputTable_L[0x20] IDATA_ATTR;

struct Wave_Squares
{

    /* Programmable Timer */
    union TWIN  nFreqTimer[2];
    int32_t     nFreqCount[2];

    /* Length Counter */
    uint8_t     nLengthCount[2];
    uint8_t     bLengthEnabled[2];
    uint8_t     bChannelEnabled[2];

    /* Volume / Decay */
    uint8_t     nVolume[2];
    uint8_t     nDecayVolume[2];
    uint8_t     bDecayEnable[2];
    uint8_t     bDecayLoop[2];
    uint8_t     nDecayTimer[2];
    uint8_t     nDecayCount[2];

    /* Sweep Unit */
    uint8_t     bSweepEnable[2];
    uint8_t     bSweepMode[2];
    uint8_t     bSweepForceSilence[2];
    uint8_t     nSweepTimer[2];
    uint8_t     nSweepCount[2];
    uint8_t     nSweepShift[2];

    /* Duty Cycle */
    uint8_t     nDutyCount[2];
    uint8_t     nDutyCycle[2];

    /* Output and Downsampling */
    int32_t         nMixL;
};

int16_t     Squares_nOutputTable_L[0x10][0x10] IDATA_ATTR;

struct Wave_TND
{

    /*
     * Triangle
     */

    /* Programmable Timer */
    union TWIN  nTriFreqTimer;
    int32_t     nTriFreqCount;

    /* Length Counter */
    uint8_t     nTriLengthCount;
    uint8_t     bTriLengthEnabled;
    uint8_t     bTriChannelEnabled;

    /* Linear Counter */
    uint8_t     nTriLinearCount;
    uint8_t     nTriLinearLoad;
    uint8_t     bTriLinearHalt;
    uint8_t     bTriLinearControl;

    /* Tri-Step Generator / Output */
    uint8_t     nTriStep;
    uint8_t     nTriOutput;

    /*
     * Noise
     */

    /* Programmable Timer */
    uint16_t    nNoiseFreqTimer;
    int32_t     nNoiseFreqCount;

    /* Length Counter */
    uint8_t     nNoiseLengthCount;
    uint8_t     bNoiseLengthEnabled;
    uint8_t     bNoiseChannelEnabled;

    /* Volume / Decay */
    uint8_t     nNoiseVolume;
    uint8_t     nNoiseDecayVolume;
    uint8_t     bNoiseDecayEnable;
    uint8_t     bNoiseDecayLoop;
    uint8_t     nNoiseDecayTimer;
    uint8_t     nNoiseDecayCount;

    /* Random Number Generator */
    uint16_t    nNoiseRandomShift;
    uint8_t     bNoiseRandomMode;           /* 1 = 32k, 6 = 93-bit */
    uint8_t     bNoiseRandomOut;

    /*
     * DMC
     */

    /* Play Mode */
    uint8_t     bDMCLoop;
    uint8_t     bDMCIRQEnabled;
    uint8_t     bDMCIRQPending;

    /* Address / DMA */
    uint8_t     nDMCDMABank_Load;
    uint16_t    nDMCDMAAddr_Load;
    uint8_t     nDMCDMABank;
    uint16_t    nDMCDMAAddr;
    uint8_t*    pDMCDMAPtr[8];

    /* Length / Input */
    uint16_t    nDMCLength;
    uint16_t    nDMCBytesRemaining;
    uint8_t     nDMCDelta;
    uint8_t     nDMCDeltaBit;
    uint8_t     bDMCDeltaSilent;
    uint8_t     nDMCSampleBuffer;
    uint8_t     bDMCSampleBufferEmpty;

    /* Frequency */
    uint16_t    nDMCFreqTimer;
    int32_t     nDMCFreqCount;

    /* Output */
    uint8_t     bDMCActive;
    uint8_t     nDMCOutput;

    int32_t     nMixL;
};

/* channels */
struct Wave_Squares mWave_Squares IDATA_ATTR; /* Square channels 1 and 2 */
struct Wave_TND     mWave_TND IDATA_ATTR;     /* Triangle/Noise/DMC channels */
struct VRC6PulseWave    mWave_VRC6Pulse[2] IDATA_ATTR;
struct VRC6SawWave  mWave_VRC6Saw IDATA_ATTR;
struct N106Wave     mWave_N106 IDATA_ATTR;
struct FDSWave      mWave_FDS IDATA_ATTR;
struct FME07Wave    mWave_FME07[3] IDATA_ATTR; /* FME-07's 3 pulse channels */


/****************** MMC5 ******************/
/* will include MMC5 sound channels some day,
   currently only multiply is supported */

/****************** N106 (Disch loves this chip) ******************/

#ifdef ICODE_INSTEAD_OF_INLINE
void Wave_N106_DoTicks(const int32_t ticks) ICODE_ATTR;
void Wave_N106_DoTicks(const int32_t ticks)
#else
inline void Wave_N106_DoTicks(const int32_t ticks);
inline void Wave_N106_DoTicks(const int32_t ticks)
#endif
{
    register int32_t i;

    for(i = (7 - mWave_N106.nActiveChannels); i < 8; i++)
    {
        if(!mWave_N106.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 */
            mWave_N106.nVolume[i] = mWave_N106.nPreVolume[i];
            continue;
        }

        {
            mWave_N106.nMixL[i] = 
                N106_nOutputTable_L[mWave_N106.nVolume[i]]
                                      [mWave_N106.nOutput[i]];
            
            if(mWave_N106.nFreqTimer[i] < 0)
                mWave_N106.nFreqTimer[i] =
                 (mWave_N106.nFrequencyLookupTable[mWave_N106.nActiveChannels] /
                 mWave_N106.nFreqReg[i].D);
            if(mWave_N106.nFreqCount[i] > mWave_N106.nFreqTimer[i])
                mWave_N106.nFreqCount[i] = mWave_N106.nFreqTimer[i];

            mWave_N106.nFreqCount[i] -= ticks << 8;
            while(mWave_N106.nFreqCount[i] <= 0)
            {
                mWave_N106.nFreqCount[i] += mWave_N106.nFreqTimer[i];
                if(mWave_N106.nWaveRemaining[i])
                {
                    mWave_N106.nWaveRemaining[i]--;
                    mWave_N106.nWavePos[i]++;
                }
                if(!mWave_N106.nWaveRemaining[i])
                {
                    mWave_N106.nWaveRemaining[i] = mWave_N106.nWaveSize[i];
                    mWave_N106.nWavePos[i] = mWave_N106.nWavePosStart[i];
                    if(mWave_N106.nVolume[i] != mWave_N106.nPreVolume[i])
                    {
                        if(++mWave_N106.nPopCheck[i] >= 2)
                        {
                            mWave_N106.nPopCheck[i] = 0;
                            mWave_N106.nVolume[i] = mWave_N106.nPreVolume[i];
                        }
                    }
                }

                mWave_N106.nOutput[i] =
                    mWave_N106.nRAM[mWave_N106.nWavePos[i]];
                    
                if(!mWave_N106.nOutput[i])
                {
                    mWave_N106.nPopCheck[i] = 0;
                    mWave_N106.nVolume[i] = mWave_N106.nPreVolume[i];
                }
                    
            }
        }
    }
}
/****************** VRC6 ******************/

#ifdef ICODE_INSTEAD_OF_INLINE
void Wave_VRC6_DoTicks(const int32_t ticks) ICODE_ATTR;
void Wave_VRC6_DoTicks(const int32_t ticks)
#else
inline void Wave_VRC6_DoTicks(const int32_t ticks);
inline void Wave_VRC6_DoTicks(const int32_t ticks)
#endif
{
    register int32_t i;

    for(i = 0; i < 2; i++) {

        if(mWave_VRC6Pulse[i].bChannelEnabled) {

            mWave_VRC6Pulse[i].nFreqCount -= ticks;

            if(mWave_VRC6Pulse[i].nDutyCount <=
               mWave_VRC6Pulse[i].nDutyCycle)
            {
                mWave_VRC6Pulse[i].nMixL =
                    VRC6Pulse_nOutputTable_L[mWave_VRC6Pulse[i].nVolume];
            }
            else
                mWave_VRC6Pulse[i].nMixL = 0;

            while(mWave_VRC6Pulse[i].nFreqCount <= 0) {
                mWave_VRC6Pulse[i].nFreqCount +=
                    mWave_VRC6Pulse[i].nFreqTimer.W + 1;

                if(!mWave_VRC6Pulse[i].bDigitized)
                    mWave_VRC6Pulse[i].nDutyCount =
                        (mWave_VRC6Pulse[i].nDutyCount + 1) & 0x0F;
            }
        }
    }

    if(mWave_VRC6Saw.bChannelEnabled) {

        mWave_VRC6Saw.nFreqCount -= ticks;

        mWave_VRC6Saw.nMixL =
            VRC6Saw_nOutputTable_L[mWave_VRC6Saw.nAccum >> 3];

        while(mWave_VRC6Saw.nFreqCount <= 0) {

            mWave_VRC6Saw.nFreqCount += mWave_VRC6Saw.nFreqTimer.W + 1;

            mWave_VRC6Saw.nAccumStep++;
            if(mWave_VRC6Saw.nAccumStep == 14)
            {
                mWave_VRC6Saw.nAccumStep = 0;
                mWave_VRC6Saw.nAccum = 0;
            }
            else if(!(mWave_VRC6Saw.nAccumStep & 1))
                mWave_VRC6Saw.nAccum += mWave_VRC6Saw.nAccumRate;
        }
    }
}

/****************** Square waves ******************/

/* decay */
#ifdef ICODE_INSTEAD_OF_INLINE
void Wave_Squares_ClockMajor(void) ICODE_ATTR;
void Wave_Squares_ClockMajor()
#else
inline void Wave_Squares_ClockMajor(void);
inline void Wave_Squares_ClockMajor()
#endif
{
    if(mWave_Squares.nDecayCount[0])
        mWave_Squares.nDecayCount[0]--;
    else
    {
        mWave_Squares.nDecayCount[0] = mWave_Squares.nDecayTimer[0];
        if(mWave_Squares.nDecayVolume[0])
            mWave_Squares.nDecayVolume[0]--;
        else
        {
            if(mWave_Squares.bDecayLoop[0])
                mWave_Squares.nDecayVolume[0] = 0x0F;
        }

        if(mWave_Squares.bDecayEnable[0])
            mWave_Squares.nVolume[0] = mWave_Squares.nDecayVolume[0];
    }
        
    if(mWave_Squares.nDecayCount[1])
        mWave_Squares.nDecayCount[1]--;
    else
        {
        mWave_Squares.nDecayCount[1] = mWave_Squares.nDecayTimer[1];
        if(mWave_Squares.nDecayVolume[1])
            mWave_Squares.nDecayVolume[1]--;
        else
        {
            if(mWave_Squares.bDecayLoop[1])
                mWave_Squares.nDecayVolume[1] = 0x0F;
        }

        if(mWave_Squares.bDecayEnable[1])
            mWave_Squares.nVolume[1] = mWave_Squares.nDecayVolume[1];
    }
        
}


#ifdef ICODE_INSTEAD_OF_INLINE
void Wave_Squares_CheckSweepForcedSilence(const int32_t i) ICODE_ATTR;
void Wave_Squares_CheckSweepForcedSilence(const int32_t i)
#else
inline void Wave_Squares_CheckSweepForcedSilence(const int32_t i);
inline void Wave_Squares_CheckSweepForcedSilence(const int32_t i)
#endif
{
    if(mWave_Squares.nFreqTimer[i].W < 8) {
        mWave_Squares.bSweepForceSilence[i] = 1; return;
    }
    if(!mWave_Squares.bSweepMode[i] &&
       (( mWave_Squares.nFreqTimer[i].W +
          (mWave_Squares.nFreqTimer[i].W >> mWave_Squares.nSweepShift[i]))
       >= 0x0800)) { mWave_Squares.bSweepForceSilence[i] = 1; return; }

    mWave_Squares.bSweepForceSilence[i] = 0;
}

/* sweep / length */
#ifdef ICODE_INSTEAD_OF_INLINE
void Wave_Squares_ClockMinor(void) ICODE_ATTR;
void Wave_Squares_ClockMinor()
#else
inline void Wave_Squares_ClockMinor(void);
inline void Wave_Squares_ClockMinor()
#endif
{
/* unrolled a little loop
   static int i = 0;
  for(i = 0; i < 2; i++)
  {
*/
    if(mWave_Squares.bLengthEnabled[0] && mWave_Squares.nLengthCount[0])
            mWave_Squares.nLengthCount[0]--;

    if(!mWave_Squares.bSweepEnable[0] || !mWave_Squares.nLengthCount[0] ||
        mWave_Squares.bSweepForceSilence[0] || !mWave_Squares.nSweepShift[0])
        goto other_square;

    if(mWave_Squares.nSweepCount[0])
        mWave_Squares.nSweepCount[0]--;
    else
    {
        mWave_Squares.nSweepCount[0] = mWave_Squares.nSweepTimer[0];
        if(mWave_Squares.bSweepMode[0])  mWave_Squares.nFreqTimer[0].W -=
            (mWave_Squares.nFreqTimer[0].W >> mWave_Squares.nSweepShift[0])+1;
        else mWave_Squares.nFreqTimer[0].W +=
            (mWave_Squares.nFreqTimer[0].W >> mWave_Squares.nSweepShift[0]);

        Wave_Squares_CheckSweepForcedSilence(0);
    }
        
    /* */
other_square:
    if(mWave_Squares.bLengthEnabled[1] && mWave_Squares.nLengthCount[1])
        mWave_Squares.nLengthCount[1]--;

    if(!mWave_Squares.bSweepEnable[1] || !mWave_Squares.nLengthCount[1] ||
        mWave_Squares.bSweepForceSilence[1] || !mWave_Squares.nSweepShift[1])
        return;

    if(mWave_Squares.nSweepCount[1])
        mWave_Squares.nSweepCount[1]--;
    else
    {
        mWave_Squares.nSweepCount[1] = mWave_Squares.nSweepTimer[1];
        if(mWave_Squares.bSweepMode[1])  mWave_Squares.nFreqTimer[1].W -=
            (mWave_Squares.nFreqTimer[1].W >> mWave_Squares.nSweepShift[1]);
        else mWave_Squares.nFreqTimer[1].W +=
            (mWave_Squares.nFreqTimer[1].W >> mWave_Squares.nSweepShift[1]);

        Wave_Squares_CheckSweepForcedSilence(1);
    }
}

/****************** Triangle/noise/DMC ******************/

/* decay (noise), linear (tri) */

#ifdef ICODE_INSTEAD_OF_INLINE
void Wave_TND_ClockMajor(void) ICODE_ATTR;
void Wave_TND_ClockMajor()
#else
inline void Wave_TND_ClockMajor(void);
inline void Wave_TND_ClockMajor()
#endif
{
    /* noise's decay */
    if(mWave_TND.nNoiseDecayCount)
        mWave_TND.nNoiseDecayCount--;
    else
    {
        mWave_TND.nNoiseDecayCount = mWave_TND.nNoiseDecayTimer;
        if(mWave_TND.nNoiseDecayVolume)
            mWave_TND.nNoiseDecayVolume--;
        else
        {
            if(mWave_TND.bNoiseDecayLoop)
                mWave_TND.nNoiseDecayVolume = 0x0F;
        }

        if(mWave_TND.bNoiseDecayEnable)
            mWave_TND.nNoiseVolume = mWave_TND.nNoiseDecayVolume;
    }

    /* triangle's linear */
    if(mWave_TND.bTriLinearHalt)
        mWave_TND.nTriLinearCount = mWave_TND.nTriLinearLoad;
    else if(mWave_TND.nTriLinearCount)
        mWave_TND.nTriLinearCount--;

    if(!mWave_TND.bTriLinearControl)
        mWave_TND.bTriLinearHalt = 0;
}

/* length */

#ifdef ICODE_INSTEAD_OF_INLINE
void Wave_TND_ClockMinor(void) ICODE_ATTR;
void Wave_TND_ClockMinor()
#else
inline void Wave_TND_ClockMinor(void);
inline void Wave_TND_ClockMinor()
#endif
{
    if(mWave_TND.bNoiseLengthEnabled && mWave_TND.nNoiseLengthCount)
        mWave_TND.nNoiseLengthCount--;
        
    if(mWave_TND.bTriLengthEnabled && mWave_TND.nTriLengthCount)
        mWave_TND.nTriLengthCount--;
}

/*#undef this*/

/****************** NSF Core ******************/

/* start globals */

/*
 *  Memory
 */
/* RAM:      0x0000 - 0x07FF */
uint8_t     pRAM[0x800] IDATA_ATTR;
/* SRAM:     0x6000 - 0x7FFF (non-FDS only) */
uint8_t     pSRAM[0x2000];
/* ExRAM:    0x5C00 - 0x5FF5 (MMC5 only)
 * Also holds NSF player code (at 0x5000 - 0x500F) */
uint8_t     pExRAM[0x1000];
/* Full ROM buffer */
uint8_t*    pROM_Full IDATA_ATTR;

uint16_t    main_nOutputTable_L[0x8000];

uint8_t*    pROM[10] IDATA_ATTR;/* ROM banks (point to areas in pROM_Full) */
                                /* 0x8000 - 0xFFFF */
                                /* also includes 0x6000 - 0x7FFF (FDS only) */
uint8_t*        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 uint8_t ( *ReadProc)(uint16_t);
typedef void ( *WriteProc)(uint16_t,uint8_t);
ReadProc    ReadMemory[0x10] IDATA_ATTR;
WriteProc   WriteMemory[0x10] IDATA_ATTR;

/*
 *  6502 Registers / Mode
 */

uint8_t     regA IDATA_ATTR;        /* Accumulator */
uint8_t     regX IDATA_ATTR;        /* X-Index */
uint8_t     regY IDATA_ATTR;        /* Y-Index */
uint8_t     regP IDATA_ATTR;        /* Processor Status */
uint8_t     regSP IDATA_ATTR;       /* Stack Pointer */
uint16_t    regPC IDATA_ATTR;       /* Program Counter */

uint8_t     bPALMode IDATA_ATTR;/* 1 if in PAL emulation mode, 0 if in NTSC */
uint8_t     bCPUJammed IDATA_ATTR;  /* 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 */

/* Multiplication Register, for MMC5 chip only (5205+5206) */
uint8_t     nMultIn_Low;
uint8_t     nMultIn_High;

/*
 *  NSF Preparation Information
 */

uint8_t     nBankswitchInitValues[10];  /* banks to swap to on tune init */
uint16_t    nPlayAddress;               /* Play routine address */
uint16_t    nInitAddress;               /* Init routine address */

uint8_t     nExternalSound;             /* external sound chips */
uint8_t     nCurTrack;

float       fNSFPlaybackSpeed;

/*
 *  pAPU
 */

uint8_t     nFrameCounter;      /* Frame Sequence Counter */
uint8_t     nFrameCounterMax;   /* Frame Sequence Counter Size
                                   (3 or 4 depending on $4017.7) */
uint8_t     bFrameIRQEnabled;   /* TRUE if frame IRQs are enabled */
uint8_t     bFrameIRQPending;   /* TRUE if the frame sequencer is holding down
                                   an IRQ */

uint8_t         nFME07_Address;

/*
 *  Timing and Counters
 */
/* fixed point -15.16 */

int32_t     nTicksUntilNextFrame;
int32_t     nTicksPerPlay;
int32_t     nTicksUntilNextPlay;
int32_t     nTicksPerSample;
int32_t     nTicksUntilNextSample;

uint32_t    nCPUCycle IDATA_ATTR;
uint32_t    nAPUCycle IDATA_ATTR;

   
uint32_t    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;
uint8_t     bNoSilenceIfTime;
uint8_t     bTimeNotDefault;

/*
 *  Sound output options
 */
const int32_t       nSampleRate=44100;

/*
 *  Volume/fading/filter tracking
 */

uint32_t        nStartFade; /* play call to start fading out */
uint32_t        nEndFade;   /* play call to stop fading out (song is over) */
uint8_t         bFade;      /* are we fading? */
float           fFadeVolume;
float           fFadeChange;

/*
 *  Designated Output Buffer
 */
uint8_t*        pOutput IDATA_ATTR;

const uint8_t   bDMCPopReducer=1;
uint8_t         nDMCPop_Prev IDATA_ATTR = 0;
uint8_t         bDMCPop_Skip IDATA_ATTR = 0;
uint8_t         bDMCPop_SamePlay IDATA_ATTR = 0;

const uint8_t   nForce4017Write=0;
const uint8_t   bN106PopReducer=0;
const uint8_t   bIgnore4011Writes=0;
    
const uint8_t   bIgnoreBRK=0;
const uint8_t   bIgnoreIllegalOps=0;
const uint8_t   bNoWaitForReturn=0;
const uint8_t   bPALPreference=0;
const uint8_t   bCleanAXY=0;
const uint8_t   bResetDuty=0;

/*
 *  Sound Filter
 */

int64_t     nFilterAccL IDATA_ATTR;
int64_t     nHighPass IDATA_ATTR;

int32_t     nHighPassBase IDATA_ATTR;

uint8_t     bHighPassEnabled IDATA_ATTR;

/* end globals */

#define CLOCK_MAJOR() { Wave_Squares_ClockMajor(); Wave_TND_ClockMajor(); }
#define CLOCK_MINOR() { Wave_Squares_ClockMinor(); Wave_TND_ClockMinor(); }

#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
 */
 
uint32_t Emulate6502(uint32_t runto) ICODE_ATTR;
void EmulateAPU(uint8_t bBurnCPUCycles) ICODE_ATTR;

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(uint8_t track);  /* Change tracks */

/*
 *  Getting Samples
 */
/* fill a buffer with samples */
int32_t     GetSamples(uint8_t* buffer, int32_t buffersize);

/*
 *  Playback options
 */
/* Set desired playback options (0 = bad options couldn't be set) */
int     SetPlaybackOptions(int32_t samplerate);
/* Speed throttling (0 = uses NSF specified speed) */
void    SetPlaybackSpeed(float playspersec);

float   GetPlaybackSpeed(void);
float   GetMasterVolume(void);

/*
 *  Seeking
 */
/* gets the number of 'play' routine calls executed */
float   GetPlayCalls(void);

/* gets the output time (based on the given play rate,
   if basedplayspersec is zero, current playback speed is used */
uint32_t    GetWrittenTime(float basedplayspersec);
/* sets the number of 'plays' routines executed (for precise seeking) */
void    SetPlayCalls(float plays);
/* sets the written time (approx. seeking) */
void    SetWrittenTime(uint32_t ms,float basedplays);

/*
 *  Fading
 */

void    StopFade(void);         /* stops all fading (plays indefinitely) */
uint8_t SongCompleted(void);    /* song has faded out (samples have stopped
                                   being generated) */
/* parameters are play calls */
void    SetFade(int32_t fadestart,int32_t fadestop,uint8_t bNotDefault);
void    SetFadeTime(uint32_t fadestart,uint32_t fadestop,float basedplays,
            uint8_t bNotDefault); /* parameters are in milliseconds */

/*
 *  Internal Functions
 */
void    RebuildOutputTables(void);
void    RecalculateFade(void);  /* called when fade status is changed. */
void    RecalcFilter(void);
void    RecalcSilenceTracker(void);

void    WriteMemory_VRC6(uint16_t a,uint8_t v) ICODE_ATTR;
void    WriteMemory_MMC5(uint16_t a,uint8_t v) ICODE_ATTR;
void    WriteMemory_N106(uint16_t a,uint8_t v) ICODE_ATTR;
void    WriteMemory_FME07(uint16_t a,uint8_t v) ICODE_ATTR;

/*
 *  Memory Read/Write routines
 */

uint8_t     ReadMemory_RAM(uint16_t a) ICODE_ATTR;
uint8_t     ReadMemory_ExRAM(uint16_t a) ICODE_ATTR;
uint8_t     ReadMemory_SRAM(uint16_t a) ICODE_ATTR;
uint8_t     ReadMemory_pAPU(uint16_t a) ICODE_ATTR;
uint8_t     ReadMemory_ROM(uint16_t a) ICODE_ATTR;
uint8_t     ReadMemory_Default(uint16_t a) ICODE_ATTR;

uint8_t     ReadMemory_N106(uint16_t a) ICODE_ATTR;

void        WriteMemory_RAM(uint16_t a,uint8_t v) ICODE_ATTR;
void        WriteMemory_ExRAM(uint16_t a,uint8_t v) ICODE_ATTR;
void        WriteMemory_SRAM(uint16_t a,uint8_t v) ICODE_ATTR;
void        WriteMemory_pAPU(uint16_t a,uint8_t v) ICODE_ATTR;
void        WriteMemory_FDSRAM(uint16_t a,uint8_t v) ICODE_ATTR;
void        WriteMemory_Default(uint16_t a,uint8_t v) ICODE_ATTR;

uint8_t         ReadMemory_RAM(uint16_t a)      { return pRAM[a & 0x07FF]; }
uint8_t         ReadMemory_ExRAM(uint16_t a)    { return pExRAM[a & 0x0FFF]; }
uint8_t         ReadMemory_SRAM(uint16_t a)     { return pSRAM[a & 0x1FFF]; }
uint8_t         ReadMemory_ROM(uint16_t a)
    { return pROM[(a >> 12) - 6][a & 0x0FFF]; }
uint8_t         ReadMemory_Default(uint16_t a)  { return (a >> 8); }

void        WriteMemory_RAM(uint16_t a,uint8_t v)
    { pRAM[a & 0x07FF] = v; }
void        WriteMemory_ExRAM(uint16_t a,uint8_t v);
void        WriteMemory_SRAM(uint16_t a,uint8_t v)
    { pSRAM[a & 0x1FFF] = v; }
void        WriteMemory_FDSRAM(uint16_t a,uint8_t v)
    { pROM[(a >> 12) - 6][a & 0x0FFF] = v; }
void        WriteMemory_Default(uint16_t a,uint8_t v)   { (void)a; (void)v; }


/* Read Memory Procs */

uint8_t  ReadMemory_pAPU(uint16_t a)
{
    EmulateAPU(1);

    if(a == 0x4015)
    {
        uint8_t 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;

    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;
}

uint8_t  ReadMemory_N106(uint16_t a)
{
    if(a != 0x4800)
        return ReadMemory_pAPU(a);

    uint8_t 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(uint16_t a,uint8_t v)
{
    if(a < 0x5FF6)              /* Invalid */
        return;

    a -= 0x5FF6;

    /* Swap out banks */

    EmulateAPU(1);
    /* stop it from swapping to a bank that doesn't exist */
    if(v >= nROMBankCount)
        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(uint16_t a,uint8_t 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;
        /* IRQ can't be pending if disabled */
        if(!mWave_TND.bDMCIRQEnabled)
            mWave_TND.bDMCIRQPending = 0;

        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;

    /* 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.nLFO_Timer = (0x10000<<14) / 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.nLFO_Timer = (0x10000<<14) / 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(uint16_t a,uint8_t 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(uint16_t a,uint8_t v)
{
    if((a <= 0x5015) && !bPALMode)
    {
        /* no audio emulation */
        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(uint16_t a,uint8_t 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.nFreqTimer[ch] = -1;              \
                break;                                          \
    case r1:    if(mWave_N106.nFreqReg[ch].B.h == v) break;     \
                mWave_N106.nFreqReg[ch].B.h = v;                \
                mWave_N106.nFreqTimer[ch] = -1;              \
                break;                                          \
    case r2:    if(mWave_N106.nFreqReg[ch].B.w != (v & 3)){     \
                    mWave_N106.nFreqReg[ch].B.w = v & 0x03;     \
                    mWave_N106.nFreqTimer[ch] = -1;}         \
                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.nFreqTimer[0] = -1;
                mWave_N106.nFreqTimer[1] = -1;
                mWave_N106.nFreqTimer[2] = -1;
                mWave_N106.nFreqTimer[3] = -1;
                mWave_N106.nFreqTimer[4] = -1;
                mWave_N106.nFreqTimer[5] = -1;
                mWave_N106.nFreqTimer[6] = -1;
                mWave_N106.nFreqTimer[7] = -1;
                break;
        }
#undef N106REGWRITE
    }
}

void WriteMemory_FME07(uint16_t a,uint8_t 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(uint8_t bBurnCPUCycles)
{
    int32_t tick;
    int64_t diff;
    
    int32_t tnd_out;
    int square_out1;
    int square_out2;
    
    ENTER_TIMER(apu);
    
    fulltick += (signed)(nCPUCycle - nAPUCycle);

    int32_t burned;
    int32_t mixL;

    if(bFade && nSilentSampleMax && (nSilentSamples >= nSilentSampleMax))
        fulltick = 0;

    while(fulltick>0)
    {
        tick = (nTicksUntilNextSample+0xffff)>>16;

        fulltick -= tick;

        /*
         * Sample Generation
         */

        ENTER_TIMER(squares);
        /* Square generation */

        mWave_Squares.nFreqCount[0] -= tick;
        mWave_Squares.nFreqCount[1] -= tick;

        if((mWave_Squares.nDutyCount[0] < mWave_Squares.nDutyCycle[0]) &&
            mWave_Squares.nLengthCount[0] &&
            !mWave_Squares.bSweepForceSilence[0])
            square_out1 = mWave_Squares.nVolume[0];
        else
            square_out1 = 0;

        if((mWave_Squares.nDutyCount[1] < mWave_Squares.nDutyCycle[1]) &&
            mWave_Squares.nLengthCount[1] &&
            !mWave_Squares.bSweepForceSilence[1])
            square_out2 = mWave_Squares.nVolume[1];
        else
            square_out2 = 0;

        mWave_Squares.nMixL = Squares_nOutputTable_L[square_out1][square_out2];

        if(mWave_Squares.nFreqCount[0]<=0)
        {
            int cycles =
                (-mWave_Squares.nFreqCount[0])/
                (mWave_Squares.nFreqTimer[0].W + 1) + 1;
            mWave_Squares.nFreqCount[0] =
                (mWave_Squares.nFreqTimer[0].W + 1)-
                (-mWave_Squares.nFreqCount[0])%
                (mWave_Squares.nFreqTimer[0].W + 1);
            mWave_Squares.nDutyCount[0] =
                (mWave_Squares.nDutyCount[0]+cycles)%0x10;
        }
        if(mWave_Squares.nFreqCount[1]<=0)
        {
            int cycles =
                (-mWave_Squares.nFreqCount[1])/
                (mWave_Squares.nFreqTimer[1].W + 1) + 1;
            mWave_Squares.nFreqCount[1] = 
                (mWave_Squares.nFreqTimer[1].W + 1)-
                (-mWave_Squares.nFreqCount[1])%
                (mWave_Squares.nFreqTimer[1].W + 1);
            mWave_Squares.nDutyCount[1] = (mWave_Squares.nDutyCount[1]+cycles)%
                0x10;
        }
        /* end of Square generation */
        EXIT_TIMER(squares);
        ENTER_TIMER(tnd);
        
        ENTER_TIMER(tnd_enter);
    
        burned=0;
    
        /* TND generation */
    
        if(mWave_TND.nNoiseFreqTimer) mWave_TND.nNoiseFreqCount -= tick;
            
        if(mWave_TND.nTriFreqTimer.W > 8)
            mWave_TND.nTriFreqCount -= tick;

        tnd_out = mWave_TND.nTriOutput << 11;

        if(mWave_TND.bNoiseRandomOut && mWave_TND.nNoiseLengthCount)
            tnd_out |= mWave_TND.nNoiseVolume << 7;

        tnd_out |= mWave_TND.nDMCOutput;

        mWave_TND.nMixL = main_nOutputTable_L[tnd_out];

        EXIT_TIMER(tnd_enter);
    
        ENTER_TIMER(tnd_tri);
    
        /* Tri */

        if(mWave_TND.nTriFreqCount<=0)
        {
            if(mWave_TND.nTriLengthCount && mWave_TND.nTriLinearCount)
            {
                do mWave_TND.nTriStep++;
                while ((mWave_TND.nTriFreqCount +=
                    mWave_TND.nTriFreqTimer.W + 1) <= 0);
                mWave_TND.nTriStep &= 0x1F;

                if(mWave_TND.nTriStep & 0x10)
                    mWave_TND.nTriOutput = mWave_TND.nTriStep ^ 0x1F;
                else mWave_TND.nTriOutput = mWave_TND.nTriStep;
            } else mWave_TND.nTriFreqCount=mWave_TND.nTriFreqTimer.W+1;
        }

        EXIT_TIMER(tnd_tri);
    
        ENTER_TIMER(tnd_noise);
    
        /* Noise */

        if(mWave_TND.nNoiseFreqTimer &&
           mWave_TND.nNoiseVolume && mWave_TND.nNoiseFreqCount<=0)
        {
            mWave_TND.nNoiseFreqCount = mWave_TND.nNoiseFreqTimer;
            mWave_TND.nNoiseRandomShift <<= 1;
            mWave_TND.bNoiseRandomOut = (((mWave_TND.nNoiseRandomShift <<
                mWave_TND.bNoiseRandomMode) ^
                mWave_TND.nNoiseRandomShift) & 0x8000 ) ? 1 : 0;
            if(mWave_TND.bNoiseRandomOut)
                mWave_TND.nNoiseRandomShift |= 0x01;
        }
    
        EXIT_TIMER(tnd_noise);
    
        ENTER_TIMER(tnd_dmc);

        /* DMC */
        if(mWave_TND.bDMCActive)
        {
            mWave_TND.nDMCFreqCount -= tick;
            while (mWave_TND.nDMCFreqCount <= 0) {
                if (!mWave_TND.bDMCActive) {
                    mWave_TND.nDMCFreqCount = mWave_TND.nDMCFreqTimer;
                    break;
                }

                mWave_TND.nDMCFreqCount += mWave_TND.nDMCFreqTimer;

                if(mWave_TND.bDMCSampleBufferEmpty &&
                   mWave_TND.nDMCBytesRemaining)
                {
                    burned += 4;        /* 4 cycle burn! */
                    mWave_TND.nDMCSampleBuffer =
                        mWave_TND.pDMCDMAPtr[mWave_TND.nDMCDMABank]
                                            [mWave_TND.nDMCDMAAddr];
                    mWave_TND.nDMCDMAAddr++;
                    if(mWave_TND.nDMCDMAAddr & 0x1000)
                    {
                        mWave_TND.nDMCDMAAddr &= 0x0FFF;
                        mWave_TND.nDMCDMABank =
                            (mWave_TND.nDMCDMABank + 1) & 0x07;
                    }

                    mWave_TND.bDMCSampleBufferEmpty = 0;
                    mWave_TND.nDMCBytesRemaining--;
                    if(!mWave_TND.nDMCBytesRemaining)
                    {
                        if(mWave_TND.bDMCLoop)
                        {
                            mWave_TND.nDMCDMABank = mWave_TND.nDMCDMABank_Load;
                            mWave_TND.nDMCDMAAddr = mWave_TND.nDMCDMAAddr_Load;
                            mWave_TND.nDMCBytesRemaining =mWave_TND.nDMCLength;
                        }
                        else if(mWave_TND.bDMCIRQEnabled)
                            mWave_TND.bDMCIRQPending = 1;
                    }
                }

                if(!mWave_TND.nDMCDeltaBit)
                {
                    mWave_TND.nDMCDeltaBit = 8;
                    mWave_TND.bDMCDeltaSilent =mWave_TND.bDMCSampleBufferEmpty;
                    mWave_TND.nDMCDelta = mWave_TND.nDMCSampleBuffer;
                    mWave_TND.bDMCSampleBufferEmpty = 1;
                }
                
                if(mWave_TND.nDMCDeltaBit) {
                    mWave_TND.nDMCDeltaBit--;
                    if(!mWave_TND.bDMCDeltaSilent)
                    {
                        if(mWave_TND.nDMCDelta & 0x01)
                        {
                            if(mWave_TND.nDMCOutput < 0x7E)
                                mWave_TND.nDMCOutput += 2;
                        }
                        else if(mWave_TND.nDMCOutput > 1)
                            mWave_TND.nDMCOutput -= 2;
                    }
                    mWave_TND.nDMCDelta >>= 1;
                }

                if(!mWave_TND.nDMCBytesRemaining &&
                    mWave_TND.bDMCSampleBufferEmpty &&
                    mWave_TND.bDMCDeltaSilent)
                    mWave_TND.bDMCActive = mWave_TND.nDMCDeltaBit = 0;
            }
        }
    
        EXIT_TIMER(tnd_dmc);
   
        /* end of TND generation */
        EXIT_TIMER(tnd);

        if(nExternalSound && !bPALMode)
        {
            if(nExternalSound & EXTSOUND_VRC6)
                Wave_VRC6_DoTicks(tick);
            if(nExternalSound & EXTSOUND_N106)
                Wave_N106_DoTicks(tick);
            if(nExternalSound & EXTSOUND_FME07)
            {
                if (mWave_FME07[0].bChannelEnabled &&
                    mWave_FME07[0].nFreqTimer.W) {
                    mWave_FME07[0].nFreqCount -= tick;

                    if(mWave_FME07[0].nDutyCount < 16)
                    {
                        mWave_FME07[0].nMixL =
                            FME07_nOutputTable_L[mWave_FME07[0].nVolume];
                    } else mWave_FME07[0].nMixL = 0;
                    while(mWave_FME07[0].nFreqCount <= 0) {
                        mWave_FME07[0].nFreqCount +=
                            mWave_FME07[0].nFreqTimer.W;

                        mWave_FME07[0].nDutyCount=
                            (mWave_FME07[0].nDutyCount+1)&0x1f;
                    }
                }

                if (mWave_FME07[1].bChannelEnabled &&
                    mWave_FME07[1].nFreqTimer.W) {
                    mWave_FME07[1].nFreqCount -= tick;

                    if(mWave_FME07[1].nDutyCount < 16)
                    {
                        mWave_FME07[1].nMixL =
                            FME07_nOutputTable_L[mWave_FME07[1].nVolume];
                    } else mWave_FME07[1].nMixL = 0;
                    while(mWave_FME07[1].nFreqCount <= 0) {
                        mWave_FME07[1].nFreqCount +=
                            mWave_FME07[1].nFreqTimer.W;

                        mWave_FME07[1].nDutyCount=
                            (mWave_FME07[1].nDutyCount+1)&0x1f;
                    }
                }

                if (mWave_FME07[2].bChannelEnabled &&
                    mWave_FME07[2].nFreqTimer.W) {
                    mWave_FME07[2].nFreqCount -= tick;

                    if(mWave_FME07[2].nDutyCount < 16)
                    {
                        mWave_FME07[2].nMixL =
                            FME07_nOutputTable_L[mWave_FME07[2].nVolume];
                    } else mWave_FME07[2].nMixL = 0;
                    while(mWave_FME07[2].nFreqCount <= 0) {
                        mWave_FME07[2].nFreqCount +=
                            mWave_FME07[2].nFreqTimer.W;

                        mWave_FME07[2].nDutyCount=
                            (mWave_FME07[2].nDutyCount+1)&0x1f;
                    }
                }

            } /* end FME07 */
            ENTER_TIMER(fds);
            if(nExternalSound & EXTSOUND_FDS) {

                /*  Volume Envelope Unit    */
                if(mWave_FDS.bVolEnv_On)
                {
                    mWave_FDS.nVolEnv_Count -= tick;
                    while(mWave_FDS.nVolEnv_Count <= 0)
                    {
                        mWave_FDS.nVolEnv_Count += mWave_FDS.nVolEnv_Timer;
                        if(mWave_FDS.nVolEnv_Mode) {
                            if(mWave_FDS.nVolEnv_Gain < 0x20)
                                mWave_FDS.nVolEnv_Gain++;
                            }
                        else {
                            if(mWave_FDS.nVolEnv_Gain)
                                mWave_FDS.nVolEnv_Gain--;
                        }
                    }
                }
    
                /*  Sweep Envelope Unit */
                if(mWave_FDS.bSweepEnv_On)
                {
                    mWave_FDS.nSweep_Count -= tick;
                    while(mWave_FDS.nSweep_Count <= 0)
                    {
                        mWave_FDS.nSweep_Count += mWave_FDS.nSweep_Timer;
                        if(mWave_FDS.nSweep_Mode)    {
                            if(mWave_FDS.nSweep_Gain < 0x20)
                                mWave_FDS.nSweep_Gain++;
                        } else {
                            if(mWave_FDS.nSweep_Gain) mWave_FDS.nSweep_Gain--;
                        }
                    }
                }
            
                /*  Effector / LFO      */
                int32_t     subfreq = 0;
                if(mWave_FDS.bLFO_On)
                {
                    mWave_FDS.nLFO_Count -= tick<<14;
                    while(mWave_FDS.nLFO_Count <= 0)
                    {
                        mWave_FDS.nLFO_Count += mWave_FDS.nLFO_Timer;
                        if(mWave_FDS.nLFO_Table[mWave_FDS.nLFO_Addr] == 4)
                            mWave_FDS.nSweepBias = 0;
                        else 
                            mWave_FDS.nSweepBias +=
                                ModulationTable[ 
                                    mWave_FDS.nLFO_Table[mWave_FDS.nLFO_Addr]
                                ];
                        mWave_FDS.nLFO_Addr = (mWave_FDS.nLFO_Addr + 1) & 0x3F;
                    }
            
                    while(mWave_FDS.nSweepBias >  63)
                        mWave_FDS.nSweepBias -= 128;
                    while(mWave_FDS.nSweepBias < -64)
                        mWave_FDS.nSweepBias += 128;
            
                    register int32_t temp =
                        mWave_FDS.nSweepBias * mWave_FDS.nSweep_Gain;
                    if(temp & 0x0F)
                    {
                        temp /= 16;
                        if(mWave_FDS.nSweepBias < 0) temp--;
                        else                temp += 2;
                    }
                    else
                        temp /= 16;
            
                    if(temp > 193)  temp -= 258;
                    if(temp < -64)  temp += 256;
            
                    subfreq = mWave_FDS.nFreq.W * temp / 64;
                }
            
                /*  Main Unit       */
                if(mWave_FDS.bMain_On)
                {
                    mWave_FDS.nMixL =
                        FDS_nOutputTable_L[mWave_FDS.nMainVolume]
                                          [mWave_FDS.nVolume]
                                 [mWave_FDS.nWaveTable[mWave_FDS.nMainAddr] ];
            
                    if((subfreq + mWave_FDS.nFreq.W) > 0)
                    {
                        int32_t freq = (0x10000<<14) / (subfreq + mWave_FDS.nFreq.W);
            
                        mWave_FDS.nFreqCount -= tick<<14;
                        while(mWave_FDS.nFreqCount <= 0)
                        {
                            mWave_FDS.nFreqCount += freq;
            
                            mWave_FDS.nMainAddr =
                                (mWave_FDS.nMainAddr + 1) & 0x3F;
                            mWave_FDS.nPopOutput =
                                mWave_FDS.nWaveTable[mWave_FDS.nMainAddr];
                            if(!mWave_FDS.nMainAddr)
                            {
                                if(mWave_FDS.nVolEnv_Gain < 0x20)
                                    mWave_FDS.nVolume = mWave_FDS.nVolEnv_Gain;
                                else mWave_FDS.nVolume = 0x20;
                            }
                        }
                    }
                    else
                        mWave_FDS.nFreqCount = mWave_FDS.nLFO_Count;
                }
                else if(mWave_FDS.bPopReducer && mWave_FDS.nPopOutput)
                {
                    mWave_FDS.nMixL = FDS_nOutputTable_L[mWave_FDS.nMainVolume]
                                                        [mWave_FDS.nVolume]
                                                        [mWave_FDS.nPopOutput];
            
                    mWave_FDS.nPopCount -= tick;
                    while(mWave_FDS.nPopCount <= 0)
                    {
                        mWave_FDS.nPopCount += 500;
                        mWave_FDS.nPopOutput--;
                        if(!mWave_FDS.nPopOutput)
                            mWave_FDS.nMainAddr = 0;
                    }
                } /* end FDS */
            }
            EXIT_TIMER(fds);
        } /* end while fulltick */

        if(bBurnCPUCycles)
        {
            nCPUCycle += burned;
            fulltick += burned;
        }
        
        /* Frame Sequencer */

        ENTER_TIMER(frame);
        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;
            }
        }
        EXIT_TIMER(frame);

        ENTER_TIMER(mix);
        nTicksUntilNextSample -= tick<<16;
        if(nTicksUntilNextSample <= 0)
        {
            nTicksUntilNextSample += nTicksPerSample;
            
            mixL = mWave_Squares.nMixL;
            mixL += mWave_TND.nMixL;

            if(nExternalSound && !bPALMode)
            {
                if(nExternalSound & EXTSOUND_VRC6)
                {
                    mixL += (mWave_VRC6Pulse[0].nMixL);
                    mixL += (mWave_VRC6Pulse[1].nMixL);
                    mixL += (mWave_VRC6Saw.nMixL);
                }
                if(nExternalSound & EXTSOUND_N106) {
                    mixL += (mWave_N106.nMixL[0]);
                    mixL += (mWave_N106.nMixL[1]);
                    mixL += (mWave_N106.nMixL[2]);
                    mixL += (mWave_N106.nMixL[3]);
                    mixL += (mWave_N106.nMixL[4]);
                    mixL += (mWave_N106.nMixL[5]);
                    mixL += (mWave_N106.nMixL[6]);
                    mixL += (mWave_N106.nMixL[7]);
                }
                if(nExternalSound & EXTSOUND_FME07)
                {
                    mixL += (mWave_FME07[0].nMixL);
                    mixL += (mWave_FME07[1].nMixL);
                    mixL += (mWave_FME07[2].nMixL);
                }
                if(nExternalSound & EXTSOUND_FDS)
                    mixL += mWave_FDS.nMixL;
            }

            /*  Filter  */
            diff = ((int64_t)mixL << 25) - nFilterAccL;
            nFilterAccL += (diff * nHighPass) >> 16;
            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;

            *((uint16_t*)pOutput) = (uint16_t)mixL;
            pOutput += 2;
        }
        
    }
    EXIT_TIMER(mix);

    nAPUCycle = nCPUCycle;
    
    EXIT_TIMER(apu);
}


/*
 *  Initialize
 *
 *      Initializes Memory
 */

int NSFCore_Initialize()
{
    int32_t i;
    /* clear globals */
    /* why, yes, this was easier when they were in a struct */

    /*
     *  Memory
     */

    ZEROMEMORY(pRAM,0x800);
    ZEROMEMORY(pSRAM,0x2000);
    ZEROMEMORY(pExRAM,0x1000);
    pROM_Full=0;

    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
     */

    ZEROMEMORY(nBankswitchInitValues,10);
    nPlayAddress=0;
    nInitAddress=0;

    nExternalSound=0;
    nCurTrack=0;

    fNSFPlaybackSpeed=0;

    /*
     * pAPU
     */

    nFrameCounter=0;
    nFrameCounterMax=0;
    bFrameIRQEnabled=0;
    bFrameIRQPending=0;

    /*
     *  Timing and Counters
     */
    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;

    /*
     * Volume/fading/filter tracking
     */

    nStartFade=0;
    nEndFade=0;
    bFade=0;
    fFadeVolume=0;
    fFadeChange=0;

    pOutput=0;

    nDMCPop_Prev=0;
    bDMCPop_Skip=0;
    bDMCPop_SamePlay=0;

    /*
     * Sound Filter
     */

    nFilterAccL=0;
    nHighPass=0;

    nHighPassBase=0;

    bHighPassEnabled=0;

    /* channels */
    
    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;

    bHighPassEnabled = 1;

    mWave_TND.nNoiseRandomShift =   1;
    for(i = 0; i < 8; i++)
        mWave_TND.pDMCDMAPtr[i] = pROM[i + 2];


    SetPlaybackOptions(nSampleRate);

    for(i = 0; i < 8; i++)
        mWave_N106.nFrequencyLookupTable[i] =
            ((((i + 1) * 45 * 0x40000) / (float)NES_FREQUENCY) *
            (float)NTSC_FREQUENCY) * 256.0;

    ZEROMEMORY(pRAM,0x800);
    ZEROMEMORY(pSRAM,0x2000);
    ZEROMEMORY(pExRAM,0x1000);
    pStack = pRAM + 0x100;
    return 1;
}

/*
 *  LoadNSF
 */

int LoadNSF(int32_t datasize)
{
    if(!pDataBuffer)                return 0;

    int32_t i;

    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;

    uint8_t 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] = (uint8_t)(neededsize >> 12);
                nBankswitchInitValues[1] = (uint8_t)(neededsize >> 12) + 1;
                neededsize += 0x2000;
            }
        }
    }
    else            /* doesn't use bankswitching */
    {
        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] = (uint8_t)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] = (uint8_t)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);
    
    SetPlaybackSpeed(0);

    nPlayAddress = nfilePlayAddress;
    nInitAddress = nfileInitAddress;

    pExRAM[0x00] = 0x20;                        /* JSR */
    pExRAM[0x01] = nInitAddress&0xff;           /* Init Address */
    pExRAM[0x02] = (nInitAddress>>8)&0xff;
    pExRAM[0x03] = 0xF2;                        /* JAM */
    pExRAM[0x04] = 0x20;                        /* JSR */
    pExRAM[0x05] = nPlayAddress&0xff;           /* Play Address */
    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 = 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)
        {
            /* if both VRC6+VRC7... it MUST go to WriteMemory_VRC6
             * or register writes will be lost (WriteMemory_VRC6 calls
             * WriteMemory_VRC7 if needed) */
            WriteMemory[0x09] = WriteMemory_VRC6;   
            WriteMemory[0x0A] = WriteMemory_VRC6;   
            WriteMemory[0x0B] = WriteMemory_VRC6;   
        }
        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;
        }
    }
    
    /* MMC5 still has a multiplication reg that needs to be available on
       PAL tunes */
    if(nExternalSound & EXTSOUND_MMC5)
        WriteMemory[0x05] = WriteMemory_MMC5;

    return 1;
}

/*
 *  SetTrack
 */

void SetTrack(uint8_t track)
{
    int32_t i;
    
    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;

    /*  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 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_Timer = 0;
    mWave_FDS.nLFO_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.nFreqCount = 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;

    nSilentSamples = 0;

    fulltick=0;
}

/*
 *  SetPlaybackOptions
 */

int SetPlaybackOptions(int32_t samplerate)
{
    if(samplerate < 2000)                   return 0;
    if(samplerate > 96000)                  return 0;

    nTicksPerSample =
        (bPALMode ? PAL_FREQUENCY : NTSC_FREQUENCY) / samplerate * 0x10000;
    nTicksUntilNextSample = nTicksPerSample;

    RecalcFilter();
    RecalcSilenceTracker();

    return 1;
}

/*
 *  SetPlaybackSpeed
 */

void SetPlaybackSpeed(float playspersec)
{
    if(playspersec < 1)
    {
        playspersec = fNSFPlaybackSpeed;
    }

    nTicksPerPlay = nTicksUntilNextPlay =
        (bPALMode ? PAL_FREQUENCY : NTSC_FREQUENCY) / playspersec * 0x10000;
}

/*
*   GetPlaybackSpeed
*/

float GetPlaybackSpeed()
{
    if(nTicksPerPlay <= 0)  return 0;
    return ((bPALMode ? PAL_FREQUENCY : NTSC_FREQUENCY) / (nTicksPerPlay>>16));
}

/*
 *  RecalcFilter
 */

void RecalcFilter()
{
    if(!nSampleRate) return;

    nHighPass = ((int64_t)nHighPassBase << 16) / nSampleRate;

    if(nHighPass > (1<<16)) nHighPass = 1<<16;
}

/*
 *  RecalcSilenceTracker
 */

void RecalcSilenceTracker()
{
    if(nSilenceTrackMS <= 0 || !nSampleRate ||
       (bNoSilenceIfTime && bTimeNotDefault))
    {
        nSilentSampleMax = 0;
        return;
    }

    nSilentSampleMax = nSilenceTrackMS * nSampleRate / 500;
    nSilentSampleMax /= 2;
}

void RebuildOutputTables(void) {
    int32_t i,j;
    float l[3];
    int32_t temp;
    float ftemp;
    
    /* tnd */
    for(i = 0; i < 3; i++)
    {
        l[i] = 255;
    }

    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)
            main_nOutputTable_L[i] = 0;
        else
            main_nOutputTable_L[i] =
                (int16_t)(2396850 / ((1.0f / ftemp) + 100));
    }
    
    /* squares */
    for(i = 0; i < 2; i++)
    {
        l[i] = 255;
    }

    for(j = 0; j < 0x10; j++)
    {
        for(i = 0; i < 0x10; i++)
        {
            temp = (int32_t)(l[0] * j);
            temp += (int32_t)(l[1] * i);

            if(!temp)
                Squares_nOutputTable_L[j][i] = 0;
            else
                Squares_nOutputTable_L[j][i] = 1438200 / ((2072640 / temp) + 100);
        }
    }

    /* VRC6 Pulse 1,2 */
    for(i = 0; i < 0x10; i++)
    {
        VRC6Pulse_nOutputTable_L[i] =
            1875 * i / 0x0F;
    }
    /* VRC6 Saw */
    for(i = 0; i < 0x20; i++)
    {
        VRC6Saw_nOutputTable_L[i] = 3750 * i / 0x1F;
    }

    /* N106 channels */
    /* this amplitude is just a guess */

    for(i = 0; i < 0x10; i++)
    {
        for(j = 0; j < 0x10; j++)
        {
            N106_nOutputTable_L[i][j] = (3000 * i * j) / 0xE1;
        }
    }
    
    /* FME-07 Square A,B,C */
    FME07_nOutputTable_L[15] = 3000;
    FME07_nOutputTable_L[0] = 0;
    for(i = 14; i > 0; i--)
    {
        FME07_nOutputTable_L[i] = FME07_nOutputTable_L[i + 1] * 80 / 100;
    }

    /*
     *  FDS
     */
    /*  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.
     */
    for(i = 0; i < 0x21; i++)
    {
        for(j = 0; j < 0x40; j++)
        {
            FDS_nOutputTable_L[0][i][j] =
                (4000 * i * j * 30) / (0x21 * 0x40 * 30);
            FDS_nOutputTable_L[1][i][j] =
                (4000 * i * j * 20) / (0x21 * 0x40 * 30);
            FDS_nOutputTable_L[2][i][j] =
                (4000 * i * j * 15) / (0x21 * 0x40 * 30);
            FDS_nOutputTable_L[3][i][j] =
                (4000 * i * j * 12) / (0x21 * 0x40 * 30);
        }
    }
}

/*
 *  GetPlayCalls
 */

float GetPlayCalls()
{
    if(!nTicksPerPlay)  return 0;

    return ((float)nTotalPlays) +
        (1.0f - (nTicksUntilNextPlay*1.0f / nTicksPerPlay));
}

/*
 *  GetWrittenTime
 */
uint32_t GetWrittenTime(float basedplayspersec /* = 0 */)
{
    if(basedplayspersec <= 0)
        basedplayspersec = GetPlaybackSpeed();

    if(basedplayspersec <= 0)
        return 0;

    return (uint32_t)((GetPlayCalls() * 1000) / basedplayspersec);
}

/*
 *  StopFade
 */
void StopFade()
{
    bFade = 0;
    fFadeVolume = 1;
}

/*
 *  SongCompleted
 */

uint8_t 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,
             uint8_t 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(uint32_t fadestart,uint32_t fadestop,float basedplays,
                 uint8_t 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;

    /* make it hit silence a little before the song ends...
       otherwise we're not really fading OUT, we're just fading umm...
       quieter =P */
    int32_t temp = (int32_t)(GetPlaybackSpeed() / 4);

    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(uint8_t* buffer,int32_t buffersize)
{
    if(!buffer)                             return 0;
    if(buffersize < 16)                     return 0;
    if(bFade && (nTotalPlays >= nEndFade))  return 0;
    
    pOutput = buffer;
    uint32_t runtocycle =
        (uint32_t)((buffersize / 2) * nTicksPerSample / 0x10000);
    nCPUCycle = nAPUCycle = 0;
    uint32_t tick;

    while(1)
    {
        /*tick = (uint32_t)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;

    if(nSilentSampleMax && bFade)
    {
        int16_t* tempbuf = (int16_t*)buffer;
        while( ((uint8_t*)tempbuf) < pOutput)
        {
            if( (*tempbuf < -SILENCE_THRESHOLD) ||
                (*tempbuf > SILENCE_THRESHOLD) )
                nSilentSamples = 0;
            else
            {
                if(++nSilentSamples >= nSilentSampleMax)
                    return (int32_t)( ((uint8_t*)tempbuf) - buffer);
            }
            tempbuf++;
        }
    }

    return (int32_t)(pOutput - buffer);
}

/****************** 6502 emulation ******************/

/*  Memory reading/writing and other defines */

/* reads zero page memory */
#define     Zp(a)           pRAM[a]
/* reads zero page memory in word form */
#define     ZpWord(a)       (Zp(a) | (Zp((uint8_t)(a + 1)) << 8))
/* reads memory */
#define     Rd(a)           ((ReadMemory[((uint16_t)(a)) >> 12])(a))
/* reads memory in word form */
#define     RdWord(a)       (Rd(a) | (Rd(a + 1) << 8))
/* writes memory */
#define     Wr(a,v)         (WriteMemory[((uint16_t)(a)) >> 12])(a,v)
/* writes zero paged memory */
#define     WrZ(a,v)        pRAM[a] = v
/* pushes a value onto the stack */
#define     PUSH(v)         pStack[SP--] = v
/* pulls a value from the stack */
#define     PULL(v)         v = pStack[++SP]

/*  Addressing Modes */

/* first set - gets the value that's being addressed */
/*Immediate*/
#define Ad_VlIm()   val = Rd(PC.W); PC.W++
/*Zero Page*/
#define Ad_VlZp()   final.W = Rd(PC.W); val = Zp(final.W); PC.W++
/*Zero Page, X*/
#define Ad_VlZx()   front.W = final.W = Rd(PC.W); final.B.l += X;           \
                    val = Zp(final.B.l); PC.W++
/*Zero Page, Y*/
#define Ad_VlZy()   front.W = final.W = Rd(PC.W); final.B.l += Y;           \
                    val = Zp(final.B.l); PC.W++
/*Absolute*/
#define Ad_VlAb()   final.W = RdWord(PC.W); val = Rd(final.W); PC.W += 2
/*Absolute, X [uses extra cycle if crossed page]*/
#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)
/*(Indirect, X)*/
#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), Y [uses extra cycle if crossed page]*/
#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)

/* 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)
 */
/*Zero Page*/
#define Ad_AdZp()   final.W = Rd(PC.W); PC.W++
/*Zero Page, X*/
#define Ad_AdZx()   final.W = front.W = Rd(PC.W); final.B.l += X; PC.W++
/*Zero Page, Y*/
#define Ad_AdZy()   final.W = front.W = Rd(PC.W); final.B.l += Y; PC.W++
/*Absolute*/
#define Ad_AdAb()   final.W = RdWord(PC.W); PC.W += 2
/*Absolute, X*/
#define Ad_AdAx()   front.W = final.W = RdWord(PC.W); PC.W += 2;            \
                    final.W += X
/*Absolute, Y*/
#define Ad_AdAy()   front.W = final.W = RdWord(PC.W); PC.W += 2;            \
                    final.W += Y
/*(Indirect, X)*/
#define Ad_AdIx()   front.W = final.W = Rd(PC.W); PC.W++; final.B.l += X;   \
                    final.W = ZpWord(final.B.l)
/*(Indirect), Y*/
#define Ad_AdIy()   front.W = Rd(PC.W); final.W = ZpWord(front.W) + Y;      \
                    PC.W++

/* 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)
 */
/*Zero Page*/
#define MRW_Zp(cmd) Ad_AdZp(); val = Zp(final.W); cmd(val); WrZ(final.W,val)
/*Zero Page, X*/
#define MRW_Zx(cmd) Ad_AdZx(); val = Zp(final.W); cmd(val); WrZ(final.W,val)
/*Zero Page, Y*/
#define MRW_Zy(cmd) Ad_AdZy(); val = Zp(final.W); cmd(val); WrZ(final.W,val)
/*Absolute*/
#define MRW_Ab(cmd) Ad_AdAb(); val = Rd(final.W); cmd(val); Wr(final.W,val)
/*Absolute, X*/
#define MRW_Ax(cmd) Ad_AdAx(); val = Rd(final.W); cmd(val); Wr(final.W,val)
/*Absolute, Y*/
#define MRW_Ay(cmd) Ad_AdAy(); val = Rd(final.W); cmd(val); Wr(final.W,val)
/*(Indirect, X)*/
#define MRW_Ix(cmd) Ad_AdIx(); val = Rd(final.W); cmd(val); Wr(final.W,val)
/*(Indirect), Y*/
#define MRW_Iy(cmd) Ad_AdIy(); val = Rd(final.W); cmd(val); Wr(final.W,val)

/* 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
 *  incremented
 */

#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 */

/* the number of CPU cycles used for each instruction */
static const uint8_t CPU_Cycles[0x100] = {
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     };

/* the status of the NZ flags for the given value */
static const uint8_t NZTable[0x100] = {
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;
uint8_t val;
uint8_t op;

uint32_t Emulate6502(uint32_t runto)
{
    /* If the CPU is jammed... don't bother */
    if(bCPUJammed == 1)
        return 0;

    register union TWIN tw;     /* used in calculations */
    register uint8_t    ST = regP;
    register union TWIN PC;
    uint8_t         SP = regSP;
    register uint8_t    A = regA;
    register uint8_t    X = regX;
    register uint8_t    Y = regY;
    union TWIN          front;
    union TWIN          final;
    PC.W = regPC;

    uint32_t ret = nCPUCycle;
    
    ENTER_TIMER(cpu);
    
    /*  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 */
            {
                /* the CPU will endlessly loop...
                   just jam it to ease processing power */
                bCPUJammed = 1;
                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  */