aboutsummaryrefslogtreecommitdiff
path: root/source/apu_blargg.c
blob: 0a1c33467e064ff921fc24ae65a01c9501bea00f (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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
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
/***********************************************************************************
  Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.

  (c) Copyright 1996 - 2002  Gary Henderson (gary.henderson@ntlworld.com),
                             Jerremy Koot (jkoot@snes9x.com)

  (c) Copyright 2002 - 2004  Matthew Kendora

  (c) Copyright 2002 - 2005  Peter Bortas (peter@bortas.org)

  (c) Copyright 2004 - 2005  Joel Yliluoma (http://iki.fi/bisqwit/)

  (c) Copyright 2001 - 2006  John Weidman (jweidman@slip.net)

  (c) Copyright 2002 - 2006  funkyass (funkyass@spam.shaw.ca),
                             Kris Bleakley (codeviolation@hotmail.com)

  (c) Copyright 2002 - 2010  Brad Jorsch (anomie@users.sourceforge.net),
                             Nach (n-a-c-h@users.sourceforge.net),
                             zones (kasumitokoduck@yahoo.com)

  (c) Copyright 2006 - 2007  nitsuja

  (c) Copyright 2009 - 2010  BearOso,
                             OV2


  BS-X C emulator code
  (c) Copyright 2005 - 2006  Dreamer Nom,
                             zones

  C4 x86 assembler and some C emulation code
  (c) Copyright 2000 - 2003  _Demo_ (_demo_@zsnes.com),
                             Nach,
                             zsKnight (zsknight@zsnes.com)

  C4 C++ code
  (c) Copyright 2003 - 2006  Brad Jorsch,
                             Nach

  DSP-1 emulator code
  (c) Copyright 1998 - 2006  _Demo_,
                             Andreas Naive (andreasnaive@gmail.com),
                             Gary Henderson,
                             Ivar (ivar@snes9x.com),
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora,
                             Nach,
                             neviksti (neviksti@hotmail.com)

  DSP-2 emulator code
  (c) Copyright 2003         John Weidman,
                             Kris Bleakley,
                             Lord Nightmare (lord_nightmare@users.sourceforge.net),
                             Matthew Kendora,
                             neviksti

  DSP-3 emulator code
  (c) Copyright 2003 - 2006  John Weidman,
                             Kris Bleakley,
                             Lancer,
                             z80 gaiden

  DSP-4 emulator code
  (c) Copyright 2004 - 2006  Dreamer Nom,
                             John Weidman,
                             Kris Bleakley,
                             Nach,
                             z80 gaiden

  OBC1 emulator code
  (c) Copyright 2001 - 2004  zsKnight,
                             pagefault (pagefault@zsnes.com),
                             Kris Bleakley
                             Ported from x86 assembler to C by sanmaiwashi

  SPC7110 and RTC C++ emulator code used in 1.39-1.51
  (c) Copyright 2002         Matthew Kendora with research by
                             zsKnight,
                             John Weidman,
                             Dark Force

  SPC7110 and RTC C++ emulator code used in 1.52+
  (c) Copyright 2009         byuu,
                             neviksti

  S-DD1 C emulator code
  (c) Copyright 2003         Brad Jorsch with research by
                             Andreas Naive,
                             John Weidman

  S-RTC C emulator code
  (c) Copyright 2001 - 2006  byuu,
                             John Weidman

  ST010 C++ emulator code
  (c) Copyright 2003         Feather,
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora

  Super FX x86 assembler emulator code
  (c) Copyright 1998 - 2003  _Demo_,
                             pagefault,
                             zsKnight

  Super FX C emulator code
  (c) Copyright 1997 - 1999  Ivar,
                             Gary Henderson,
                             John Weidman

  Sound emulator code used in 1.5-1.51
  (c) Copyright 1998 - 2003  Brad Martin
  (c) Copyright 1998 - 2006  Charles Bilyue'

  Sound emulator code used in 1.52+
  (c) Copyright 2004 - 2007  Shay Green (gblargg@gmail.com)

  SH assembler code partly based on x86 assembler code
  (c) Copyright 2002 - 2004  Marcus Comstedt (marcus@mc.pp.se)

  2xSaI filter
  (c) Copyright 1999 - 2001  Derek Liauw Kie Fa

  HQ2x, HQ3x, HQ4x filters
  (c) Copyright 2003         Maxim Stepin (maxim@hiend3d.com)

  NTSC filter
  (c) Copyright 2006 - 2007  Shay Green

  GTK+ GUI code
  (c) Copyright 2004 - 2010  BearOso

  Win32 GUI code
  (c) Copyright 2003 - 2006  blip,
                             funkyass,
                             Matthew Kendora,
                             Nach,
                             nitsuja
  (c) Copyright 2009 - 2010  OV2

  Mac OS GUI code
  (c) Copyright 1998 - 2001  John Stiles
  (c) Copyright 2001 - 2010  zones


  Specific ports contains the works of other authors. See headers in
  individual files.


  Snes9x homepage: http://www.snes9x.com/

  Permission to use, copy, modify and/or distribute Snes9x in both binary
  and source form, for non-commercial purposes, is hereby granted without
  fee, providing that this license information and copyright notice appear
  with all copies and any derived work.

  This software is provided 'as-is', without any express or implied
  warranty. In no event shall the authors be held liable for any damages
  arising from the use of this software or it's derivatives.

  Snes9x is freeware for PERSONAL USE only. Commercial users should
  seek permission of the copyright holders first. Commercial use includes,
  but is not limited to, charging money for Snes9x or software derived from
  Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
  using Snes9x as a promotion for your commercial product.

  The copyright holders request that bug fixes and improvements to the code
  should be forwarded to them so everyone can benefit from the modifications
  in future versions.

  Super NES and Super Nintendo Entertainment System are trademarks of
  Nintendo Co., Limited and its subsidiary companies.
 ***********************************************************************************/
#ifdef USE_BLARGG_APU

#include <math.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>

#ifndef INLINE
#ifdef PSP
#define INLINE __attribute((force_inline))
#else
#define INLINE inline
#endif
#endif

#include "blargg_endian.h"
#include "apu_blargg.h"

#include "snes9x.h"
//#include "snapshot.h"
#include "display.h"


/***********************************************************************************
   SPC DSP
***********************************************************************************/

static dsp_state_t dsp_m;

/* Copyright (C) 2007 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */

#if INT_MAX < 0x7FFFFFFF
   #error "Requires that int type have at least 32 bits"
#endif

/* if ( io < -32768 ) io = -32768; */
/* if ( io >  32767 ) io =  32767; */
#define CLAMP16( io )\
{\
   if ( (int16_t) io != io )\
      io = (io >> 31) ^ 0x7FFF;\
}

/* Access global DSP register */
#define REG(n)      dsp_m.regs [R_##n]

/* Access voice DSP register */
#define VREG(r,n)   r [V_##n]

#define WRITE_SAMPLES( l, r, out ) \
{\
   out [0] = l;\
   out [1] = r;\
   out += 2;\
   if ( out >= dsp_m.out_end )\
   {\
      out       = dsp_m.extra;\
      dsp_m.out_end = &dsp_m.extra [EXTRA_SIZE];\
   }\
}\


/* Volume registers and efb are signed! Easy to forget int8_t cast. */
/* Prefixes are to avoid accidental use of locals with same names. */

/* Gaussian interpolation */

static short gauss [512] =
{
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   2,   2,   2,   2,   2,
   2,   2,   3,   3,   3,   3,   3,   4,   4,   4,   4,   4,   5,   5,   5,   5,
   6,   6,   6,   6,   7,   7,   7,   8,   8,   8,   9,   9,   9,  10,  10,  10,
  11,  11,  11,  12,  12,  13,  13,  14,  14,  15,  15,  15,  16,  16,  17,  17,
  18,  19,  19,  20,  20,  21,  21,  22,  23,  23,  24,  24,  25,  26,  27,  27,
  28,  29,  29,  30,  31,  32,  32,  33,  34,  35,  36,  36,  37,  38,  39,  40,
  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,
  58,  59,  60,  61,  62,  64,  65,  66,  67,  69,  70,  71,  73,  74,  76,  77,
  78,  80,  81,  83,  84,  86,  87,  89,  90,  92,  94,  95,  97,  99, 100, 102,
 104, 106, 107, 109, 111, 113, 115, 117, 118, 120, 122, 124, 126, 128, 130, 132,
 134, 137, 139, 141, 143, 145, 147, 150, 152, 154, 156, 159, 161, 163, 166, 168,
 171, 173, 175, 178, 180, 183, 186, 188, 191, 193, 196, 199, 201, 204, 207, 210,
 212, 215, 218, 221, 224, 227, 230, 233, 236, 239, 242, 245, 248, 251, 254, 257,
 260, 263, 267, 270, 273, 276, 280, 283, 286, 290, 293, 297, 300, 304, 307, 311,
 314, 318, 321, 325, 328, 332, 336, 339, 343, 347, 351, 354, 358, 362, 366, 370,
 374, 378, 381, 385, 389, 393, 397, 401, 405, 410, 414, 418, 422, 426, 430, 434,
 439, 443, 447, 451, 456, 460, 464, 469, 473, 477, 482, 486, 491, 495, 499, 504,
 508, 513, 517, 522, 527, 531, 536, 540, 545, 550, 554, 559, 563, 568, 573, 577,
 582, 587, 592, 596, 601, 606, 611, 615, 620, 625, 630, 635, 640, 644, 649, 654,
 659, 664, 669, 674, 678, 683, 688, 693, 698, 703, 708, 713, 718, 723, 728, 732,
 737, 742, 747, 752, 757, 762, 767, 772, 777, 782, 787, 792, 797, 802, 806, 811,
 816, 821, 826, 831, 836, 841, 846, 851, 855, 860, 865, 870, 875, 880, 884, 889,
 894, 899, 904, 908, 913, 918, 923, 927, 932, 937, 941, 946, 951, 955, 960, 965,
 969, 974, 978, 983, 988, 992, 997,1001,1005,1010,1014,1019,1023,1027,1032,1036,
1040,1045,1049,1053,1057,1061,1066,1070,1074,1078,1082,1086,1090,1094,1098,1102,
1106,1109,1113,1117,1121,1125,1128,1132,1136,1139,1143,1146,1150,1153,1157,1160,
1164,1167,1170,1174,1177,1180,1183,1186,1190,1193,1196,1199,1202,1205,1207,1210,
1213,1216,1219,1221,1224,1227,1229,1232,1234,1237,1239,1241,1244,1246,1248,1251,
1253,1255,1257,1259,1261,1263,1265,1267,1269,1270,1272,1274,1275,1277,1279,1280,
1282,1283,1284,1286,1287,1288,1290,1291,1292,1293,1294,1295,1296,1297,1297,1298,
1299,1300,1300,1301,1302,1302,1303,1303,1303,1304,1304,1304,1304,1304,1305,1305,
};

/* Gaussian interpolation */

static INLINE int dsp_interpolate( dsp_voice_t *v )
{
   int offset, out, *in;
   short *fwd, *rev;

   /* Make pointers into gaussian based on fractional position between samples */
   offset = v->interp_pos >> 4 & 0xFF;
   fwd = gauss + 255 - offset;
   rev = gauss       + offset; /* mirror left half of gaussian */

   in = &v->buf [(v->interp_pos >> 12) + v->buf_pos];
   out  = (fwd [  0] * in [0]) >> 11;
   out += (fwd [256] * in [1]) >> 11;
   out += (rev [256] * in [2]) >> 11;
   out = (int16_t) out;
   out += (rev [  0] * in [3]) >> 11;

   CLAMP16( out );
   out &= ~1;
   return out;
}

/* Counters */

/* 30720 =  2048 * 5 * 3 */
#define SIMPLE_COUNTER_RANGE 30720

static unsigned const counter_rates [32] =
{
   SIMPLE_COUNTER_RANGE + 1, /* never fires */
          2048, 1536,
   1280, 1024,  768,
    640,  512,  384,
    320,  256,  192,
    160,  128,   96,
     80,   64,   48,
     40,   32,   24,
     20,   16,   12,
     10,    8,    6,
      5,    4,    3,
            2,
            1
};

static unsigned const counter_offsets [32] =
{
     1, 0, 1040,
   536, 0, 1040,
   536, 0, 1040,
   536, 0, 1040,
   536, 0, 1040,
   536, 0, 1040,
   536, 0, 1040,
   536, 0, 1040,
   536, 0, 1040,
   536, 0, 1040,
        0,
        0
};

#define RUN_COUNTERS() \
   if ( --dsp_m.counter < 0 ) \
      dsp_m.counter = SIMPLE_COUNTER_RANGE - 1;

#define READ_COUNTER(rate) (((unsigned) dsp_m.counter + counter_offsets [rate]) % counter_rates [rate])

/* Envelope */

static INLINE void dsp_run_envelope( dsp_voice_t* const v )
{
   int env, rate, env_data;

   env = v->env;
   env_data = v->regs[V_ADSR1];

   if ( dsp_m.t_adsr0 & 0x80 ) /* 99% ADSR */
   {
      if ( v->env_mode >= ENV_DECAY ) /* 99% */
      {
         env--;
         env -= env >> 8;
         rate = env_data & 0x1F;
         if ( v->env_mode == ENV_DECAY ) /* 1% */
            rate = (dsp_m.t_adsr0 >> 3 & 0x0E) + 0x10;
      }
      else /* ENV_ATTACK */
      {
         rate = (dsp_m.t_adsr0 & 0x0F) * 2 + 1;
         env += rate < 31 ? 0x20 : 0x400;
      }
   }
   else /* GAIN */
   {
      int mode;
      env_data = v->regs[V_GAIN];
      mode = env_data >> 5;
      if ( mode < 4 ) /* direct */
      {
         env = env_data * 0x10;
         rate = 31;
      }
      else
      {
         rate = env_data & 0x1F;
         if ( mode == 4 ) /* 4: linear decrease */
         {
            env -= 0x20;
         }
         else if ( mode < 6 ) /* 5: exponential decrease */
         {
            env--;
            env -= env >> 8;
         }
         else /* 6,7: linear increase */
         {
            env += 0x20;
            if ( mode > 6 && (unsigned) v->hidden_env >= 0x600 )
               env += 0x8 - 0x20; /* 7: two-slope linear increase */
         }
      }
   }

   /* Sustain level */
   if ( (env >> 8) == (env_data >> 5) && v->env_mode == ENV_DECAY )
      v->env_mode = ENV_SUSTAIN;

   v->hidden_env = env;

   /* unsigned cast because linear decrease going negative also triggers this */
   if ( (unsigned) env > 0x7FF )
   {
      env = (env < 0 ? 0 : 0x7FF);
      if ( v->env_mode == ENV_ATTACK )
         v->env_mode = ENV_DECAY;
   }

   if (!READ_COUNTER( rate ))
      v->env = env; /* nothing else is controlled by the counter */
}

/* BRR Decoding */

static INLINE void dsp_decode_brr( dsp_voice_t* v )
{
   int nybbles, *pos, *end, header;

   /* Arrange the four input nybbles in 0xABCD order for easy decoding */
   nybbles = dsp_m.t_brr_byte * 0x100 + dsp_m.ram [(v->brr_addr + v->brr_offset + 1) & 0xFFFF];

   header = dsp_m.t_brr_header;

   /* Write to next four samples in circular buffer */
   pos = &v->buf [v->buf_pos];

   if ( (v->buf_pos += 4) >= BRR_BUF_SIZE )
      v->buf_pos = 0;

   /* Decode four samples */
   for ( end = pos + 4; pos < end; pos++, nybbles <<= 4 )
   {
      int filter, p1, p2, s, shift;
      /* Extract nybble and sign-extend */
      s = (int16_t) nybbles >> 12;

      /* Shift sample based on header */
      shift = header >> 4;
      s = (s << shift) >> 1;
      if ( shift >= 0xD ) /* handle invalid range */
         s = (s >> 25) << 11; /* same as: s = (s < 0 ? -0x800 : 0) */

      /* Apply IIR filter (8 is the most commonly used) */
      filter = header & 0x0C;
      p1 = pos [BRR_BUF_SIZE - 1];
      p2 = pos [BRR_BUF_SIZE - 2] >> 1;
      if ( filter >= 8 )
      {
         s += p1;
         s -= p2;
         if ( filter == 8 ) /* s += p1 * 0.953125 - p2 * 0.46875 */
         {
            s += p2 >> 4;
            s += (p1 * -3) >> 6;
         }
         else /* s += p1 * 0.8984375 - p2 * 0.40625 */
         {
            s += (p1 * -13) >> 7;
            s += (p2 * 3) >> 4;
         }
      }
      else if ( filter ) /* s += p1 * 0.46875 */
      {
         s += p1 >> 1;
         s += (-p1) >> 5;
      }

      /* Adjust and write sample */
      CLAMP16( s );
      s = (int16_t) (s * 2);
      pos [BRR_BUF_SIZE] = pos [0] = s; /* second copy simplifies wrap-around */
   }
}

/* Misc */

/* voice 0 doesn't support PMON */

#define MISC_27() dsp_m.t_pmon = dsp_m.regs[R_PMON] & 0xFE;

#define MISC_28() \
   dsp_m.t_non = dsp_m.regs[R_NON]; \
   dsp_m.t_eon = dsp_m.regs[R_EON]; \
   dsp_m.t_dir = dsp_m.regs[R_DIR];

#define MISC_29() \
   if ( (dsp_m.every_other_sample ^= 1) != 0 ) \
      dsp_m.new_kon &= ~dsp_m.kon; /* clears KON 63 clocks after it was last read */

static INLINE void dsp_misc_30 (void)
{
   if ( dsp_m.every_other_sample )
   {
      dsp_m.kon    = dsp_m.new_kon;
      dsp_m.t_koff = dsp_m.regs[R_KOFF];
   }

   RUN_COUNTERS();

   /* Noise */
   if ( !READ_COUNTER( dsp_m.regs[R_FLG] & 0x1F ) )
   {
      int feedback = (dsp_m.noise << 13) ^ (dsp_m.noise << 14);
      dsp_m.noise = (feedback & 0x4000) ^ (dsp_m.noise >> 1);
   }
}

/* Voices */

static INLINE void dsp_voice_V1( dsp_voice_t* const v )
{
   dsp_m.t_dir_addr = dsp_m.t_dir * 0x100 + dsp_m.t_srcn * 4;
   dsp_m.t_srcn = v->regs[V_SRCN];
}

static INLINE void dsp_voice_V2( dsp_voice_t* const v )
{
   uint8_t *entry;

   entry = &dsp_m.ram [dsp_m.t_dir_addr];
   if ( !v->kon_delay )
      entry += 2;

   dsp_m.t_brr_next_addr = GET_LE16( entry );

   dsp_m.t_adsr0 = v->regs [V_ADSR0];


   dsp_m.t_pitch = v->regs [V_PITCHL];
}

static INLINE void dsp_voice_V3a( dsp_voice_t* const v )
{
   dsp_m.t_pitch += (v->regs [V_PITCHH] & 0x3F) << 8;
}

static INLINE void dsp_voice_V3b( dsp_voice_t* const v )
{
   dsp_m.t_brr_byte = dsp_m.ram [(v->brr_addr + v->brr_offset) & 0xffff];
   dsp_m.t_brr_header = dsp_m.ram [v->brr_addr];
}

static void dsp_voice_V3c( dsp_voice_t* const v )
{
   int output;

   /* Pitch modulation using previous voice's output */
   if ( dsp_m.t_pmon & v->vbit )
      dsp_m.t_pitch += ((dsp_m.t_output >> 5) * dsp_m.t_pitch) >> 10;

   if ( v->kon_delay )
   {
      /* Get ready to start BRR decoding on next sample */
      if ( v->kon_delay == 5 )
      {
         v->brr_addr    = dsp_m.t_brr_next_addr;
         v->brr_offset  = 1;
         v->buf_pos     = 0;
         dsp_m.t_brr_header = 0; /* header is ignored on this sample */
      }

      /* Envelope is never run during KON */
      v->env        = 0;
      v->hidden_env = 0;

      /* Disable BRR decoding until last three samples */
      v->interp_pos = 0;
      if ( --v->kon_delay & 3 )
         v->interp_pos = 0x4000;

      /* Pitch is never added during KON */
      dsp_m.t_pitch = 0;
   }

   output = dsp_interpolate( v );

   /* Noise */
   if ( dsp_m.t_non & v->vbit )
      output = (int16_t) (dsp_m.noise * 2);

   /* Apply envelope */
   dsp_m.t_output = (output * v->env) >> 11 & ~1;
   v->t_envx_out = (uint8_t) (v->env >> 4);

   /* Immediate silence due to end of sample or soft reset */
   if ( dsp_m.regs[R_FLG] & 0x80 || (dsp_m.t_brr_header & 3) == 1 )
   {
      v->env_mode = ENV_RELEASE;
      v->env      = 0;
   }

   if ( dsp_m.every_other_sample )
   {
      /* KOFF */
      if ( dsp_m.t_koff & v->vbit )
         v->env_mode = ENV_RELEASE;

      /* KON */
      if ( dsp_m.kon & v->vbit )
      {
         v->kon_delay = 5;
         v->env_mode  = ENV_ATTACK;
      }
   }

   /* Run envelope for next sample */
   if ( !v->kon_delay )
   {
      int env = v->env;
      if ( v->env_mode == ENV_RELEASE ) /* 60% */
      {
         if ( (env -= 0x8) < 0 )
            env = 0;
         v->env = env;
      }
      else
      {
         dsp_run_envelope( v );
      }
   }
}

static INLINE void dsp_voice_output( dsp_voice_t const* v, int ch )
{
   int amp;

   /* Apply left/right volume */
   amp = (dsp_m.t_output * (int8_t) VREG(v->regs,VOLL + ch)) >> 7;

   /* Add to output total */
   dsp_m.t_main_out [ch] += amp;
   CLAMP16( dsp_m.t_main_out [ch] );

   /* Optionally add to echo total */
   if ( dsp_m.t_eon & v->vbit )
   {
      dsp_m.t_echo_out [ch] += amp;
      CLAMP16( dsp_m.t_echo_out [ch] );
   }
}

static INLINE void dsp_voice_V4( dsp_voice_t* const v )
{
   /* Decode BRR */
   dsp_m.t_looped = 0;
   if ( v->interp_pos >= 0x4000 )
   {
      dsp_decode_brr( v );

      if ( (v->brr_offset += 2) >= BRR_BLOCK_SIZE )
      {
         /* Start decoding next BRR block */
         v->brr_addr = (v->brr_addr + BRR_BLOCK_SIZE) & 0xFFFF;
         if ( dsp_m.t_brr_header & 1 )
         {
            v->brr_addr = dsp_m.t_brr_next_addr;
            dsp_m.t_looped = v->vbit;
         }
         v->brr_offset = 1;
      }
   }

   /* Apply pitch */
   v->interp_pos = (v->interp_pos & 0x3FFF) + dsp_m.t_pitch;

   /* Keep from getting too far ahead (when using pitch modulation) */
   if ( v->interp_pos > 0x7FFF )
      v->interp_pos = 0x7FFF;

   /* Output left */
   dsp_voice_output( v, 0 );
}

static INLINE void dsp_voice_V5( dsp_voice_t* const v )
{
   int endx_buf;
   /* Output right */
   dsp_voice_output( v, 1 );

   /* ENDX, OUTX, and ENVX won't update if you wrote to them 1-2 clocks earlier */
   endx_buf = dsp_m.regs[R_ENDX] | dsp_m.t_looped;

   /* Clear bit in ENDX if KON just began */
   if ( v->kon_delay == 5 )
      endx_buf &= ~v->vbit;
   dsp_m.endx_buf = (uint8_t) endx_buf;
}

static INLINE void dsp_voice_V6( dsp_voice_t* const v )
{
   (void) v; /* avoid compiler warning about unused v */
   dsp_m.outx_buf = (uint8_t) (dsp_m.t_output >> 8);
}

static INLINE void dsp_voice_V7( dsp_voice_t* const v )
{
   /* Update ENDX */
   dsp_m.regs[R_ENDX] = dsp_m.endx_buf;

   dsp_m.envx_buf = v->t_envx_out;
}

static INLINE void dsp_voice_V8( dsp_voice_t* const v )
{
   /* Update OUTX */
   v->regs [V_OUTX] = dsp_m.outx_buf;
}

static INLINE void dsp_voice_V9( dsp_voice_t* const v )
{
   v->regs [V_ENVX] = dsp_m.envx_buf;
}

/* Most voices do all these in one clock, so make a handy composite */

static INLINE void dsp_voice_V3( dsp_voice_t* const v )
{
   dsp_voice_V3a( v );
   dsp_voice_V3b( v );
   dsp_voice_V3c( v );
}

/* Common combinations of voice steps on different voices. This greatly reduces
   code size and allows everything to be inlined in these functions. */

static void dsp_voice_V7_V4_V1( dsp_voice_t* const v )
{
   dsp_voice_V7(v);
   dsp_voice_V1(v+3);
   dsp_voice_V4(v+1);
}

static void dsp_voice_V8_V5_V2( dsp_voice_t* const v )
{
   dsp_voice_V8(v);
   dsp_voice_V5(v+1);
   dsp_voice_V2(v+2);
}

static void dsp_voice_V9_V6_V3( dsp_voice_t* const v )
{
   dsp_voice_V9(v);
   dsp_voice_V6(v+1);
   dsp_voice_V3(v+2);
}

/* Echo */

/* Current echo buffer pointer for left/right channel */
#define ECHO_PTR( ch )      (&dsp_m.ram [dsp_m.t_echo_ptr + ch * 2])

/* Sample in echo history buffer, where 0 is the oldest */
#define ECHO_FIR( i )       (dsp_m.echo_hist_pos [i])

/* Calculate FIR point for left/right channel */
#define CALC_FIR( i, ch )   ((ECHO_FIR( i + 1 ) [ch] * (int8_t) REG(FIR + i * 0x10)) >> 6)

#define ECHO_READ(ch) \
{ \
   int s; \
   if ( dsp_m.t_echo_ptr >= 0xffc0 && dsp_m.rom_enabled ) \
      s = GET_LE16SA( &dsp_m.hi_ram [dsp_m.t_echo_ptr + ch * 2 - 0xffc0] ); \
   else \
      s = GET_LE16SA( ECHO_PTR( ch ) ); \
   /* second copy simplifies wrap-around handling */ \
   ECHO_FIR( 0 ) [ch] = ECHO_FIR( 8 ) [ch] = s >> 1; \
}

static INLINE void dsp_echo_22 (void)
{
   int l, r;

   if ( ++dsp_m.echo_hist_pos >= &dsp_m.echo_hist [ECHO_HIST_SIZE] )
      dsp_m.echo_hist_pos = dsp_m.echo_hist;

   dsp_m.t_echo_ptr = (dsp_m.t_esa * 0x100 + dsp_m.echo_offset) & 0xFFFF;

   ECHO_READ(0);

   l = (((dsp_m.echo_hist_pos [0 + 1]) [0] * (int8_t) dsp_m.regs [R_FIR + 0 * 0x10]) >> 6);
   r = (((dsp_m.echo_hist_pos [0 + 1]) [1] * (int8_t) dsp_m.regs [R_FIR + 0 * 0x10]) >> 6);

   dsp_m.t_echo_in [0] = l;
   dsp_m.t_echo_in [1] = r;
}

static INLINE void dsp_echo_23 (void)
{
   int l, r;

   l = (((dsp_m.echo_hist_pos [1 + 1]) [0] * (int8_t) dsp_m.regs [R_FIR + 1 * 0x10]) >> 6) + (((dsp_m.echo_hist_pos [2 + 1]) [0] * (int8_t) dsp_m.regs [R_FIR + 2 * 0x10]) >> 6);
   r = (((dsp_m.echo_hist_pos [1 + 1]) [1] * (int8_t) dsp_m.regs [R_FIR + 1 * 0x10]) >> 6) + (((dsp_m.echo_hist_pos [2 + 1]) [1] * (int8_t) dsp_m.regs [R_FIR + 2 * 0x10]) >> 6);

   dsp_m.t_echo_in [0] += l;
   dsp_m.t_echo_in [1] += r;

   ECHO_READ(1);
}

static INLINE void dsp_echo_24 (void)
{
   int l, r;

   l = (((dsp_m.echo_hist_pos [3 + 1]) [0] * (int8_t) dsp_m.regs [R_FIR + 3 * 0x10]) >> 6) + (((dsp_m.echo_hist_pos [4 + 1]) [0] * (int8_t) dsp_m.regs [R_FIR + 4 * 0x10]) >> 6) + (((dsp_m.echo_hist_pos [5 + 1]) [0] * (int8_t) dsp_m.regs [R_FIR + 5 * 0x10]) >> 6);
   r = (((dsp_m.echo_hist_pos [3 + 1]) [1] * (int8_t) dsp_m.regs [R_FIR + 3 * 0x10]) >> 6) + (((dsp_m.echo_hist_pos [4 + 1]) [1] * (int8_t) dsp_m.regs [R_FIR + 4 * 0x10]) >> 6) + (((dsp_m.echo_hist_pos [5 + 1]) [1] * (int8_t) dsp_m.regs [R_FIR + 5 * 0x10]) >> 6);

   dsp_m.t_echo_in [0] += l;
   dsp_m.t_echo_in [1] += r;
}

static INLINE void dsp_echo_25 (void)
{
   int l = dsp_m.t_echo_in [0] + (((dsp_m.echo_hist_pos [6 + 1]) [0] * (int8_t) dsp_m.regs [R_FIR + 6 * 0x10]) >> 6);
   int r = dsp_m.t_echo_in [1] + (((dsp_m.echo_hist_pos [6 + 1]) [1] * (int8_t) dsp_m.regs [R_FIR + 6 * 0x10]) >> 6);

   l = (int16_t) l;
   r = (int16_t) r;

   l += (int16_t) (((dsp_m.echo_hist_pos [7 + 1]) [0] * (int8_t) dsp_m.regs [R_FIR + 7 * 0x10]) >> 6);
   r += (int16_t) (((dsp_m.echo_hist_pos [7 + 1]) [1] * (int8_t) dsp_m.regs [R_FIR + 7 * 0x10]) >> 6);

   if ( (int16_t) l != l )
      l = (l >> 31) ^ 0x7FFF;
   if ( (int16_t) r != r )
      r = (r >> 31) ^ 0x7FFF;

   dsp_m.t_echo_in [0] = l & ~1;
   dsp_m.t_echo_in [1] = r & ~1;
}

#define ECHO_OUTPUT(var, ch) \
{ \
   var = (int16_t) ((dsp_m.t_main_out [ch] * (int8_t) REG(MVOLL + ch * 0x10)) >> 7) + (int16_t) ((dsp_m.t_echo_in [ch] * (int8_t) REG(EVOLL + ch * 0x10)) >> 7); \
   CLAMP16( var ); \
}

static INLINE void dsp_echo_26 (void)
{
   int l, r;

   ECHO_OUTPUT(dsp_m.t_main_out[0], 0 );

   l = dsp_m.t_echo_out [0] + (int16_t) ((dsp_m.t_echo_in [0] * (int8_t) dsp_m.regs [R_EFB]) >> 7);
   r = dsp_m.t_echo_out [1] + (int16_t) ((dsp_m.t_echo_in [1] * (int8_t) dsp_m.regs [R_EFB]) >> 7);

   if ( (int16_t) l != l ) l = (l >> 31) ^ 0x7FFF;
   if ( (int16_t) r != r ) r = (r >> 31) ^ 0x7FFF;

   dsp_m.t_echo_out [0] = l & ~1;
   dsp_m.t_echo_out [1] = r & ~1;
}

static INLINE void dsp_echo_27 (void)
{
   int l, r;
   short *out;

   l = dsp_m.t_main_out [0];
   ECHO_OUTPUT(r, 1);
   dsp_m.t_main_out [0] = 0;
   dsp_m.t_main_out [1] = 0;

   if ( dsp_m.regs [R_FLG] & 0x40 )
   {
      l = 0;
      r = 0;
   }

   out = dsp_m.out;
   out [0] = l;
   out [1] = r;
   out += 2;
   if ( out >= dsp_m.out_end )
   {
      out = dsp_m.extra;
      dsp_m.out_end = &dsp_m.extra [EXTRA_SIZE];
   }
   dsp_m.out = out;
}

#define ECHO_28() dsp_m.t_echo_enabled = dsp_m.regs [R_FLG];

#define ECHO_WRITE(ch) \
   if ( !(dsp_m.t_echo_enabled & 0x20) ) \
   { \
      SET_LE16A( ECHO_PTR( ch ), dsp_m.t_echo_out [ch] ); \
      if ( dsp_m.t_echo_ptr >= 0xffc0 ) \
      { \
         SET_LE16A( &dsp_m.hi_ram [dsp_m.t_echo_ptr + ch * 2 - 0xffc0], dsp_m.t_echo_out [ch] ); \
         if ( dsp_m.rom_enabled ) \
            SET_LE16A( ECHO_PTR( ch ), GET_LE16A( &dsp_m.rom [dsp_m.t_echo_ptr + ch * 2 - 0xffc0] ) ); \
      } \
   } \
   dsp_m.t_echo_out [ch] = 0;

static INLINE void dsp_echo_29 (void)
{
   dsp_m.t_esa = dsp_m.regs [R_ESA];

   if ( !dsp_m.echo_offset )
      dsp_m.echo_length = (dsp_m.regs [R_EDL] & 0x0F) * 0x800;

   dsp_m.echo_offset += 4;
   if ( dsp_m.echo_offset >= dsp_m.echo_length )
      dsp_m.echo_offset = 0;


   ECHO_WRITE(0);

   dsp_m.t_echo_enabled = dsp_m.regs [R_FLG];
}

/* Timing */

/* Execute clock for a particular voice */

/* The most common sequence of clocks uses composite operations
for efficiency. For example, the following are equivalent to the
individual steps on the right:

V(V7_V4_V1,2) -> V(V7,2) V(V4,3) V(V1,5)
V(V8_V5_V2,2) -> V(V8,2) V(V5,3) V(V2,4)
V(V9_V6_V3,2) -> V(V9,2) V(V6,3) V(V3,4) */

/* Voice      0      1      2      3      4      5      6      7 */

/* Runs DSP for specified number of clocks (~1024000 per second). Every 32 clocks
   a pair of samples is be generated. */

static void dsp_run( int clocks_remain )
{
   int phase;

   phase = dsp_m.phase;
   dsp_m.phase = (phase + clocks_remain) & 31;

   switch ( phase )
   {
loop:
      if ( 0 && !--clocks_remain )
         break;
      case 0:
      dsp_voice_V5( &dsp_m.voices [0] );
      dsp_voice_V2( &dsp_m.voices [1] );
      if ( 1 && !--clocks_remain )
         break;
      case 1:
      dsp_voice_V6( &dsp_m.voices [0] );
      dsp_voice_V3( &dsp_m.voices [1] );
      if ( 2 && !--clocks_remain )
         break;
      case 2:
      dsp_voice_V7_V4_V1( &dsp_m.voices [0] );
      if ( 3 && !--clocks_remain )
         break;
      case 3:
      dsp_voice_V8_V5_V2( &dsp_m.voices [0] );
      if ( 4 && !--clocks_remain )
         break;
      case 4:
      dsp_voice_V9_V6_V3( &dsp_m.voices [0] );
      if ( 5 && !--clocks_remain )
         break;
      case 5:
      dsp_voice_V7_V4_V1( &dsp_m.voices [1] );
      if ( 6 && !--clocks_remain )
         break;
      case 6:
      dsp_voice_V8_V5_V2( &dsp_m.voices [1] );
      if ( 7 && !--clocks_remain )
         break;
      case 7:
      dsp_voice_V9_V6_V3( &dsp_m.voices [1] );
      if ( 8 && !--clocks_remain )
         break;
      case 8:
      dsp_voice_V7_V4_V1( &dsp_m.voices [2] );
      if ( 9 && !--clocks_remain )
         break;
      case 9:
      dsp_voice_V8_V5_V2( &dsp_m.voices [2] );
      if ( 10 && !--clocks_remain )
         break;
      case 10:
      dsp_voice_V9_V6_V3( &dsp_m.voices [2] );
      if ( 11 && !--clocks_remain )
         break;
      case 11:
      dsp_voice_V7_V4_V1( &dsp_m.voices [3] );
      if ( 12 && !--clocks_remain )
         break;
      case 12:
      dsp_voice_V8_V5_V2( &dsp_m.voices [3] );
      if ( 13 && !--clocks_remain )
         break;
      case 13:
      dsp_voice_V9_V6_V3( &dsp_m.voices [3] );
      if ( 14 && !--clocks_remain )
         break;
      case 14:
      dsp_voice_V7_V4_V1( &dsp_m.voices [4] );
      if ( 15 && !--clocks_remain )
         break;
      case 15:
      dsp_voice_V8_V5_V2( &dsp_m.voices [4] );
      if ( 16 && !--clocks_remain )
         break;
      case 16:
      dsp_voice_V9_V6_V3( &dsp_m.voices [4] );
      if ( 17 && !--clocks_remain )
         break;
      case 17:
      dsp_voice_V1( &dsp_m.voices [0] );
      dsp_voice_V7( &dsp_m.voices [5] );
      dsp_voice_V4( &dsp_m.voices [6] );
      if ( 18 && !--clocks_remain )
         break;
      case 18:
      dsp_voice_V8_V5_V2( &dsp_m.voices [5] );
      if ( 19 && !--clocks_remain )
         break;
      case 19:
      dsp_voice_V9_V6_V3( &dsp_m.voices [5] );
      if ( 20 && !--clocks_remain )
         break;
      case 20:
      dsp_voice_V1( &dsp_m.voices [1] );
      dsp_voice_V7( &dsp_m.voices [6] );
      dsp_voice_V4( &dsp_m.voices [7] );
      if ( 21 && !--clocks_remain )
         break;
      case 21:
      dsp_voice_V8( &dsp_m.voices [6] );
      dsp_voice_V5( &dsp_m.voices [7] );
      dsp_voice_V2( &dsp_m.voices [0] );
      if ( 22 && !--clocks_remain )
         break;
      case 22:
      dsp_voice_V3a( &dsp_m.voices [0] );
      dsp_voice_V9( &dsp_m.voices [6] );
      dsp_voice_V6( &dsp_m.voices [7] );
      dsp_echo_22();
      if ( 23 && !--clocks_remain )
         break;
      case 23:
      dsp_voice_V7( &dsp_m.voices [7] );
      dsp_echo_23();
      if ( 24 && !--clocks_remain )
         break;
      case 24:
      dsp_voice_V8( &dsp_m.voices [7] );
      dsp_echo_24();
      if ( 25 && !--clocks_remain )
         break;
      case 25:
      dsp_voice_V3b( &dsp_m.voices [0] );
      dsp_voice_V9( &dsp_m.voices [7] );
      dsp_echo_25();
      if ( 26 && !--clocks_remain )
         break;
      case 26:
      dsp_echo_26();
      if ( 27 && !--clocks_remain )
         break;
      case 27:
      MISC_27();
      dsp_echo_27();
      if ( 28 && !--clocks_remain )
         break;
      case 28:
      MISC_28();
      ECHO_28();
      if ( 29 && !--clocks_remain )
         break;
      case 29:
      MISC_29();
      dsp_echo_29();
      if ( 30 && !--clocks_remain )
         break;
      case 30:
      dsp_misc_30();
      dsp_voice_V3c( &dsp_m.voices [0] );
      ECHO_WRITE(1);
      if ( 31 && !--clocks_remain )
         break;
      case 31:
      dsp_voice_V4( &dsp_m.voices [0] );
      dsp_voice_V1( &dsp_m.voices [2] );

      if ( --clocks_remain )
         goto loop;
   }
}

/* Sets destination for output samples. If out is NULL or out_size is 0,
   doesn't generate any. */

static void dsp_set_output( short * out, int size )
{
   if ( !out )
   {
      out  = dsp_m.extra;
      size = EXTRA_SIZE;
   }
   dsp_m.out_begin = out;
   dsp_m.out       = out;
   dsp_m.out_end   = out + size;
}

/* Setup */

static void dsp_soft_reset_common (void)
{
   dsp_m.noise              = 0x4000;
   dsp_m.echo_hist_pos      = dsp_m.echo_hist;
   dsp_m.every_other_sample = 1;
   dsp_m.echo_offset        = 0;
   dsp_m.phase              = 0;

   dsp_m.counter = 0;
}

/* Resets DSP to power-on state */

static void dsp_reset (void)
{
   int i;

   uint8_t const initial_regs [REGISTER_COUNT] =
   {
      0x45,0x8B,0x5A,0x9A,0xE4,0x82,0x1B,0x78,0x00,0x00,0xAA,0x96,0x89,0x0E,0xE0,0x80,
      0x2A,0x49,0x3D,0xBA,0x14,0xA0,0xAC,0xC5,0x00,0x00,0x51,0xBB,0x9C,0x4E,0x7B,0xFF,
      0xF4,0xFD,0x57,0x32,0x37,0xD9,0x42,0x22,0x00,0x00,0x5B,0x3C,0x9F,0x1B,0x87,0x9A,
      0x6F,0x27,0xAF,0x7B,0xE5,0x68,0x0A,0xD9,0x00,0x00,0x9A,0xC5,0x9C,0x4E,0x7B,0xFF,
      0xEA,0x21,0x78,0x4F,0xDD,0xED,0x24,0x14,0x00,0x00,0x77,0xB1,0xD1,0x36,0xC1,0x67,
      0x52,0x57,0x46,0x3D,0x59,0xF4,0x87,0xA4,0x00,0x00,0x7E,0x44,0x00,0x4E,0x7B,0xFF,
      0x75,0xF5,0x06,0x97,0x10,0xC3,0x24,0xBB,0x00,0x00,0x7B,0x7A,0xE0,0x60,0x12,0x0F,
      0xF7,0x74,0x1C,0xE5,0x39,0x3D,0x73,0xC1,0x00,0x00,0x7A,0xB3,0xFF,0x4E,0x7B,0xFF
   };

   /* Resets DSP and uses supplied values to initialize registers */

   for (i = 0; i < REGISTER_COUNT; i++)
      dsp_m.regs[i] = initial_regs[i];

   /* Internal state */
   for ( i = VOICE_COUNT; --i >= 0; )
   {
      dsp_voice_t* v = &dsp_m.voices [i];
      v->brr_offset = 1;
      v->vbit       = 1 << i;
      v->regs       = &dsp_m.regs [i * 0x10];
   }
   dsp_m.new_kon = dsp_m.regs[R_KON];
   dsp_m.t_dir   = dsp_m.regs[R_DIR];
   dsp_m.t_esa   = dsp_m.regs[R_ESA];

   dsp_soft_reset_common();
}

/* Initializes DSP and has it use the 64K RAM provided */

static void dsp_init( void* ram_64k )
{
   dsp_m.ram = (uint8_t*) ram_64k;
   dsp_set_output( 0, 0 );
   dsp_reset();
}

/* Emulates pressing reset switch on SNES */

static void dsp_soft_reset (void)
{
   dsp_m.regs[R_FLG] = 0xE0;
   dsp_soft_reset_common();
}


/* State save/load */

#if !SPC_NO_COPY_STATE_FUNCS

static void spc_copier_copy(spc_state_copy_t * copier, void* state, size_t size )
{
   copier->func(copier->buf, state, size );
}

static int spc_copier_copy_int(spc_state_copy_t * copier, int state, int size )
{
   uint8_t s [2];
   SET_LE16( s, state );
   copier->func(copier->buf, &s, size );
   return GET_LE16( s );
}

static void spc_copier_extra(spc_state_copy_t * copier)
{
   int n = 0;
   n = (uint8_t) spc_copier_copy_int(copier, n, sizeof (uint8_t) );

   if ( n > 0 )
   {
      char temp [64];
      memset( temp, 0, sizeof(temp));
      do
      {
         int size_n = sizeof(temp);
         if ( size_n > n )
            size_n = n;
         n -= size_n;
         copier->func(copier->buf, temp, size_n );
      }
      while ( n );
   }
}

/* Saves/loads exact emulator state */

static void dsp_copy_state( unsigned char** io, dsp_copy_func_t copy )
{
   int i, j;

   spc_state_copy_t copier;
   copier.func = copy;
   copier.buf = io;

   /* DSP registers */
   spc_copier_copy(&copier, dsp_m.regs, REGISTER_COUNT );

   /* Internal state */

   /* Voices */
   for ( i = 0; i < VOICE_COUNT; i++ )
   {
      dsp_voice_t* v;

      v = &dsp_m.voices [i];

      /* BRR buffer */
      for ( j = 0; j < BRR_BUF_SIZE; j++ )
      {
         int s;

         s = v->buf [j];
         SPC_COPY(  int16_t, s );
         v->buf [j] = v->buf [j + BRR_BUF_SIZE] = s;
      }

      SPC_COPY( uint16_t, v->interp_pos );
      SPC_COPY( uint16_t, v->brr_addr );
      SPC_COPY( uint16_t, v->env );
      SPC_COPY(  int16_t, v->hidden_env );
      SPC_COPY(  uint8_t, v->buf_pos );
      SPC_COPY(  uint8_t, v->brr_offset );
      SPC_COPY(  uint8_t, v->kon_delay );
      {
         int m;

         m = v->env_mode;
         SPC_COPY(  uint8_t, m );
         v->env_mode = m;
      }
      SPC_COPY(  uint8_t, v->t_envx_out );

      spc_copier_extra(&copier);
   }

   /* Echo history */
   for ( i = 0; i < ECHO_HIST_SIZE; i++ )
   {
      int s, s2;

      s = dsp_m.echo_hist_pos [i] [0];
      s2 = dsp_m.echo_hist_pos [i] [1];

      SPC_COPY( int16_t, s );
      dsp_m.echo_hist [i] [0] = s; /* write back at offset 0 */

      SPC_COPY( int16_t, s2 );
      dsp_m.echo_hist [i] [1] = s2; /* write back at offset 0 */
   }
   dsp_m.echo_hist_pos = dsp_m.echo_hist;
   memcpy( &dsp_m.echo_hist [ECHO_HIST_SIZE], dsp_m.echo_hist, ECHO_HIST_SIZE * sizeof dsp_m.echo_hist [0] );

   /* Misc */
   SPC_COPY(  uint8_t, dsp_m.every_other_sample );
   SPC_COPY(  uint8_t, dsp_m.kon );

   SPC_COPY( uint16_t, dsp_m.noise );
   SPC_COPY( uint16_t, dsp_m.counter );
   SPC_COPY( uint16_t, dsp_m.echo_offset );
   SPC_COPY( uint16_t, dsp_m.echo_length );
   SPC_COPY(  uint8_t, dsp_m.phase );

   SPC_COPY(  uint8_t, dsp_m.new_kon );
   SPC_COPY(  uint8_t, dsp_m.endx_buf );
   SPC_COPY(  uint8_t, dsp_m.envx_buf );
   SPC_COPY(  uint8_t, dsp_m.outx_buf );

   SPC_COPY(  uint8_t, dsp_m.t_pmon );
   SPC_COPY(  uint8_t, dsp_m.t_non );
   SPC_COPY(  uint8_t, dsp_m.t_eon );
   SPC_COPY(  uint8_t, dsp_m.t_dir );
   SPC_COPY(  uint8_t, dsp_m.t_koff );

   SPC_COPY( uint16_t, dsp_m.t_brr_next_addr );
   SPC_COPY(  uint8_t, dsp_m.t_adsr0 );
   SPC_COPY(  uint8_t, dsp_m.t_brr_header );
   SPC_COPY(  uint8_t, dsp_m.t_brr_byte );
   SPC_COPY(  uint8_t, dsp_m.t_srcn );
   SPC_COPY(  uint8_t, dsp_m.t_esa );
   SPC_COPY(  uint8_t, dsp_m.t_echo_enabled );

   SPC_COPY(  int16_t, dsp_m.t_main_out [0] );
   SPC_COPY(  int16_t, dsp_m.t_main_out [1] );
   SPC_COPY(  int16_t, dsp_m.t_echo_out [0] );
   SPC_COPY(  int16_t, dsp_m.t_echo_out [1] );
   SPC_COPY(  int16_t, dsp_m.t_echo_in  [0] );
   SPC_COPY(  int16_t, dsp_m.t_echo_in  [1] );

   SPC_COPY( uint16_t, dsp_m.t_dir_addr );
   SPC_COPY( uint16_t, dsp_m.t_pitch );
   SPC_COPY(  int16_t, dsp_m.t_output );
   SPC_COPY( uint16_t, dsp_m.t_echo_ptr );
   SPC_COPY(  uint8_t, dsp_m.t_looped );

   spc_copier_extra(&copier);
}
#endif

/* Core SPC emulation: CPU, timers, SMP registers, memory */

/* snes_spc 0.9.0. http://www.slack.net/~ant/ */

/***********************************************************************************
   SNES SPC
***********************************************************************************/

static spc_state_t m;
static signed char reg_times [256];
static bool allow_time_overflow;

/* Copyright (C) 2004-2007 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */

/* (n ? n : 256) */
#define IF_0_THEN_256( n ) ((uint8_t) ((n) - 1) + 1)

/* Timers */

#define TIMER_DIV( t, n ) ((n) >> t->prescaler)
#define TIMER_MUL( t, n ) ((n) << t->prescaler)

static Timer* spc_run_timer_( Timer* t, int time )
{
   int elapsed;

   elapsed = TIMER_DIV( t, time - t->next_time ) + 1;
   t->next_time += TIMER_MUL( t, elapsed );

   if ( t->enabled )
   {
      int remain, divider, over, n;

      remain = IF_0_THEN_256( t->period - t->divider );
      divider = t->divider + elapsed;
      over = elapsed - remain;
      if ( over >= 0 )
      {
         n = over / t->period;
         t->counter = (t->counter + 1 + n) & 0x0F;
         divider = over - n * t->period;
      }
      t->divider = (uint8_t) divider;
   }
   return t;
}

/* ROM */

void spc_enable_rom( int enable )
{
   if ( m.rom_enabled != enable )
   {
      m.rom_enabled = dsp_m.rom_enabled = enable;
      if ( enable )
         memcpy( m.hi_ram, &m.ram.ram[ROM_ADDR], sizeof m.hi_ram );
      memcpy( &m.ram.ram[ROM_ADDR], (enable ? m.rom : m.hi_ram), ROM_SIZE );
      /* TODO: ROM can still get overwritten when DSP writes to echo buffer */
   }
}


/* DSP */

#define MAX_REG_TIME 29

#define RUN_DSP( time, offset ) \
   int count = (time) - (offset) - m.dsp_time; \
   if ( count >= 0 ) \
   { \
      int clock_count; \
      clock_count = (count & ~(CLOCKS_PER_SAMPLE - 1)) + CLOCKS_PER_SAMPLE; \
      m.dsp_time += clock_count; \
      dsp_run( clock_count ); \
   }

static INLINE void spc_dsp_write( int data, int time )
{
   int addr;

   /* Writes DSP registers. */
   addr = m.smp_regs[0][R_DSPADDR];
   dsp_m.regs [addr] = (uint8_t) data;
   switch ( addr & 0x0F )
   {
      case V_ENVX:
         dsp_m.envx_buf = (uint8_t) data;
         break;

      case V_OUTX:
         dsp_m.outx_buf = (uint8_t) data;
         break;
      case 0x0C:
         if ( addr == R_KON )
            dsp_m.new_kon = (uint8_t) data;

         if ( addr == R_ENDX ) /* always cleared, regardless of data written */
         {
            dsp_m.endx_buf = 0;
            dsp_m.regs [R_ENDX] = 0;
         }
         break;
   }
   /* dprintf( "SPC wrote to DSP register > $7F\n" ); */
}


/* Memory access extras */

/* CPU write */

/* divided into multiple functions to keep rarely-used functionality separate
   so often-used functionality can be optimized better by compiler */

/* If write isn't preceded by read, data has this added to it
   int const no_read_before_write = 0x2000; */

#define NO_READ_BEFORE_WRITE			8192
#define NO_READ_BEFORE_WRITE_DIVIDED_BY_TWO	4096

static void spc_cpu_write_smp_reg_( int data, int time, int addr )
{
   switch ( addr )
   {
      case R_T0TARGET:
      case R_T1TARGET:
      case R_T2TARGET:
         {
            int period;
            Timer *t;

            t = &m.timers [addr - R_T0TARGET];
            period = IF_0_THEN_256( data );

            if ( t->period != period )
            {
               if ( time >= t->next_time )
                  t = spc_run_timer_( t, time );
               t->period = period;
            }
            break;
         }
      case R_T0OUT:
      case R_T1OUT:
      case R_T2OUT:
             /* dprintf( "SPC wrote to counter %d\n", (int) addr - R_T0OUT ); */

             if ( data < NO_READ_BEFORE_WRITE_DIVIDED_BY_TWO)
             {
                if ( (time - 1) >= m.timers[addr - R_T0OUT].next_time )
                   spc_run_timer_( &m.timers [addr - R_T0OUT], time - 1 )->counter = 0;
               else
                  m.timers[addr - R_T0OUT].counter = 0;
             }
             break;

             /* Registers that act like RAM */
      case 0x8:
      case 0x9:
             m.smp_regs[1][addr] = (uint8_t) data;
             break;

      case R_TEST:
#if 0
             if ( (uint8_t) data != 0x0A )
                dprintf( "SPC wrote to test register\n" );
#endif
             break;

      case R_CONTROL:
             {
               int i;
                /* port clears */
                if ( data & 0x10 )
                {
                   m.smp_regs[1][R_CPUIO0] = 0;
                   m.smp_regs[1][R_CPUIO1] = 0;
                }
                if ( data & 0x20 )
                {
                   m.smp_regs[1][R_CPUIO2] = 0;
                   m.smp_regs[1][R_CPUIO3] = 0;
                }

                /* timers */
                {
                   for ( i = 0; i < TIMER_COUNT; i++ )
                   {
                      Timer* t = &m.timers [i];
                      int enabled = data >> i & 1;
                      if ( t->enabled != enabled )
                      {
                         if ( time >= t->next_time )
                            t = spc_run_timer_( t, time );
                         t->enabled = enabled;
                         if ( enabled )
                         {
                            t->divider = 0;
                            t->counter = 0;
                         }
                      }
                   }
                }
                spc_enable_rom( data & 0x80 );
             }
             break;
   }
}

static int const bits_in_int = CHAR_BIT * sizeof (int);

static void spc_cpu_write( int data, int addr, int time )
{
   int reg;
   /* RAM */
   m.ram.ram[addr] = (uint8_t) data;
   reg = addr - 0xF0;
   if ( reg >= 0 ) /* 64% */
   {
      /* $F0-$FF */
      if ( reg < REG_COUNT ) /* 87% */
      {
         m.smp_regs[0][reg] = (uint8_t) data;

         /* Registers other than $F2 and $F4-$F7
            if ( reg != 2 && reg != 4 && reg != 5 && reg != 6 && reg != 7 )
            TODO: this is a bit on the fragile side */

         if ( ((~0x2F00 << (bits_in_int - 16)) << reg) < 0 ) /* 36% */
         {
            if ( reg == R_DSPDATA ) /* 99% */
            {
               RUN_DSP(time, reg_times [m.smp_regs[0][R_DSPADDR]] );
               if (m.smp_regs[0][R_DSPADDR] <= 0x7F )
                  spc_dsp_write( data, time );
            }
            else
               spc_cpu_write_smp_reg_( data, time, reg);
         }
      }
      /* High mem/address wrap-around */
      else
      {
         reg -= ROM_ADDR - 0xF0;
         if ( reg >= 0 ) /* 1% in IPL ROM area or address wrapped around */
         {
            if ( reg < ROM_SIZE )
            {
               m.hi_ram [reg] = (uint8_t) data;
               if ( m.rom_enabled )
                  m.ram.ram[reg + ROM_ADDR] = m.rom [reg]; /* restore overwritten ROM */
            }
            else
            {
               *(&(m.ram.ram[0]) + reg + ROM_ADDR) = CPU_PAD_FILL; /* restore overwritten padding */
               spc_cpu_write( data, addr & 0xFFFF, time );
            }
         }
      }
   }
}

/* CPU read */

static int spc_cpu_read( int addr, int time )
{
   int result, reg;

   /* RAM */
   result = m.ram.ram[addr];
   reg = addr - 0xF0;

   if ( reg >= 0 ) /* 40% */
   {
      reg -= 0x10;
      if ( (unsigned) reg >= 0xFF00 ) /* 21% */
      {
         reg += 0x10 - R_T0OUT;

         /* Timers */
         if ( (unsigned) reg < TIMER_COUNT ) /* 90% */
         {
            Timer* t = &m.timers [reg];
            if ( time >= t->next_time )
               t = spc_run_timer_( t, time );
            result = t->counter;
            t->counter = 0;
         }
         /* Other registers */
         else if ( reg < 0 ) /* 10% */
         {
            int reg_tmp;

            reg_tmp = reg + R_T0OUT;
            result = m.smp_regs[1][reg_tmp];
            reg_tmp -= R_DSPADDR;
            /* DSP addr and data */
            if ( (unsigned) reg_tmp <= 1 ) /* 4% 0xF2 and 0xF3 */
            {
               result = m.smp_regs[0][R_DSPADDR];
               if ( (unsigned) reg_tmp == 1 )
               {
                  RUN_DSP( time, reg_times [m.smp_regs[0][R_DSPADDR] & 0x7F] );

                  result = dsp_m.regs[m.smp_regs[0][R_DSPADDR] & 0x7F]; /* 0xF3 */
               }
            }
         }
         else /* 1% */
            result = spc_cpu_read( reg + (R_T0OUT + 0xF0 - 0x10000), time );
      }
   }

   return result;
}

/***********************************************************************************
 SPC CPU
***********************************************************************************/

/* Inclusion here allows static memory access functions and better optimization */

/* Timers are by far the most common thing read from dp */

#define CPU_READ_TIMER( time, offset, addr_, out )\
{\
   int adj_time, dp_addr, ti; \
   adj_time = time + offset;\
   dp_addr = addr_;\
   ti = dp_addr - (R_T0OUT + 0xF0);\
   if ( (unsigned) ti < TIMER_COUNT )\
   {\
      Timer* t = &m.timers [ti];\
      if ( adj_time >= t->next_time )\
      t = spc_run_timer_( t, adj_time );\
      out = t->counter;\
      t->counter = 0;\
   }\
   else\
   {\
      int i, reg; \
      out = ram [dp_addr];\
      i = dp_addr - 0xF0;\
      if ( (unsigned) i < 0x10 )\
      { \
         reg = i;  \
         out = m.smp_regs[1][reg]; \
         reg -= R_DSPADDR; \
         /* DSP addr and data */ \
         if ( (unsigned) reg <= 1 ) /* 4% 0xF2 and 0xF3 */ \
         { \
            out = m.smp_regs[0][R_DSPADDR]; \
            if ( (unsigned) reg == 1 ) \
            { \
               RUN_DSP( adj_time, reg_times [m.smp_regs[0][R_DSPADDR] & 0x7F] ); \
               out = dsp_m.regs[m.smp_regs[0][R_DSPADDR] & 0x7F ]; /* 0xF3 */ \
            } \
         } \
      } \
   }\
}

#define READ_TIMER( time, addr, out )	CPU_READ_TIMER( rel_time, time, (addr), out )
#define SPC_CPU_READ( time, addr )		spc_cpu_read((addr), rel_time + time )
#define SPC_CPU_WRITE( time, addr, data )	spc_cpu_write((data), (addr), rel_time + time )

static unsigned spc_CPU_mem_bit( uint8_t const* pc, int rel_time )
{
   unsigned addr, t;

   addr = GET_LE16( pc );
   t = SPC_CPU_READ( 0, addr & 0x1FFF ) >> (addr >> 13);
   return t << 8 & 0x100;
}

#define DP_ADDR( addr )                     (dp + (addr))

#define READ_DP_TIMER(  time, addr, out )   CPU_READ_TIMER( rel_time, time, DP_ADDR( addr ), out )
#define READ_DP(  time, addr )              SPC_CPU_READ( time, DP_ADDR( addr ) )
#define WRITE_DP( time, addr, data )        SPC_CPU_WRITE( time, DP_ADDR( addr ), data )

#define READ_PROG16( addr )                 GET_LE16( ram + (addr) )

#define SET_PC( n )     (pc = ram + (n))
#define GET_PC()        (pc - ram)
#define READ_PC( pc )   (*(pc))

#define SET_SP( v )     (sp = ram + 0x101 + (v))
#define GET_SP()        (sp - 0x101 - ram)

#define PUSH16( v )     (sp -= 2, SET_LE16( sp, v ))
#define PUSH( v )       (void) (*--sp = (uint8_t) (v))
#define POP( out )      (void) ((out) = *sp++)

#define MEM_BIT( rel ) spc_CPU_mem_bit( pc, rel_time + rel )

#define GET_PSW( out )\
{\
   out = psw & ~(N80 | P20 | Z02 | C01);\
   out |= c  >> 8 & C01;\
   out |= dp >> 3 & P20;\
   out |= ((nz >> 4) | nz) & N80;\
   if ( !(uint8_t) nz ) out |= Z02;\
}

#define SET_PSW( in )\
{\
   psw = in;\
   c   = in << 8;\
   dp  = in << 3 & 0x100;\
   nz  = (in << 4 & 0x800) | (~in & Z02);\
}

static uint8_t* spc_run_until_( int end_time )
{
   int dp, nz, c, psw, a, x, y;
   uint8_t *ram, *pc, *sp;
   int rel_time = m.spc_time - end_time;
   m.spc_time = end_time;
   m.dsp_time += rel_time;
   m.timers [0].next_time += rel_time;
   m.timers [1].next_time += rel_time;
   m.timers [2].next_time += rel_time;
   ram = m.ram.ram;
   a = m.cpu_regs.a;
   x = m.cpu_regs.x;
   y = m.cpu_regs.y;

   SET_PC( m.cpu_regs.pc );
   SET_SP( m.cpu_regs.sp );
   SET_PSW( m.cpu_regs.psw );

   goto loop;


   /* Main loop */

cbranch_taken_loop:
   pc += *(int8_t const*) pc;
inc_pc_loop:
   pc++;
loop:
   {
      unsigned opcode, data;

      opcode = *pc;

      if (allow_time_overflow && rel_time >= 0 )
         goto stop;
      if ( (rel_time += m.cycle_table [opcode]) > 0 && !allow_time_overflow)
         goto out_of_time;

      /* TODO: if PC is at end of memory, this will get wrong operand (very obscure) */
      data = *++pc;
      switch ( opcode )
      {

         /* Common instructions */

#define BRANCH( cond )\
         {\
            pc++;\
            pc += (int8_t) data;\
            if ( cond )\
            goto loop;\
            pc -= (int8_t) data;\
            rel_time -= 2;\
            goto loop;\
         }

         case 0xF0: /* BEQ */
            BRANCH( !(uint8_t) nz ) /* 89% taken */

         case 0xD0: /* BNE */
               BRANCH( (uint8_t) nz )

         case 0x3F:
               { /* CALL */
                  int old_addr;
                  old_addr = GET_PC() + 2;
                  SET_PC( GET_LE16( pc ) );
                  PUSH16( old_addr );
                  goto loop;
               }
         case 0x6F: /* RET */
               SET_PC( GET_LE16( sp ) );
               sp += 2;
               goto loop;

         case 0xE4: /* MOV a,dp */
               ++pc;
               /* 80% from timer */
               READ_DP_TIMER( 0, data, a = nz );
               goto loop;

         case 0xFA:{ /* MOV dp,dp */
                 int temp;
                 READ_DP_TIMER( -2, data, temp );
                 data = temp + NO_READ_BEFORE_WRITE ;
              }
              /* fall through */
         case 0x8F:
              { /* MOV dp,#imm */
                 int i, temp;
                 temp = READ_PC( pc + 1 );
                 pc += 2;

                 i = dp + temp;
                 ram [i] = (uint8_t) data;
                 i -= 0xF0;
                 if ( (unsigned) i < 0x10 ) /* 76% */
                 {
                    m.smp_regs[0][i] = (uint8_t) data;

                    /* Registers other than $F2 and $F4-$F7 */
                    /* if ( i != 2 && i != 4 && i != 5 && i != 6 && i != 7 ) */
                    if ( ((~0x2F00 << (bits_in_int - 16)) << i) < 0 ) /* 12% */
                    {
                       if ( i == R_DSPDATA ) /* 99% */
                       {
                          RUN_DSP(rel_time, reg_times [m.smp_regs[0][R_DSPADDR]] );
                          if (m.smp_regs[0][R_DSPADDR] <= 0x7F )
                             spc_dsp_write( data, rel_time );
                       }
                       else
                          spc_cpu_write_smp_reg_( data, rel_time, i);
                    }
                 }
                 goto loop;
              }

         case 0xC4: /* MOV dp,a */
              ++pc;
              {
                 int i;
                 i = dp + data;
                 ram [i] = (uint8_t) a;
                 i -= 0xF0;
                 if ( (unsigned) i < 0x10 ) /* 39% */
                 {
                    unsigned sel;
                    sel = i - 2;
                    m.smp_regs[0][i] = (uint8_t) a;

                    if ( sel == 1 ) /* 51% $F3 */
                    {
                       RUN_DSP(rel_time, reg_times [m.smp_regs[0][R_DSPADDR]] );
                       if (m.smp_regs[0][R_DSPADDR] <= 0x7F )
                          spc_dsp_write( a, rel_time );
                    }
                    else if ( sel > 1 ) /* 1% not $F2 or $F3 */
                       spc_cpu_write_smp_reg_( a, rel_time, i );
                 }
              }
              goto loop;

#define CASE( n )   case n:

              /* Define common address modes based on opcode for immediate mode. Execution
                 ends with data set to the address of the operand. */
#define ADDR_MODES_( op )\
              CASE( op - 0x02 ) /* (X) */\
              data = x + dp;\
              pc--;\
              goto end_##op;\
              CASE( op + 0x0F ) /* (dp)+Y */\
              data = READ_PROG16( data + dp ) + y;\
              goto end_##op;\
              CASE( op - 0x01 ) /* (dp+X) */\
              data = READ_PROG16( ((uint8_t) (data + x)) + dp );\
              goto end_##op;\
              CASE( op + 0x0E ) /* abs+Y */\
              data += y;\
              goto abs_##op;\
              CASE( op + 0x0D ) /* abs+X */\
              data += x;\
              CASE( op - 0x03 ) /* abs */\
              abs_##op:\
              data += 0x100 * READ_PC( ++pc );\
              goto end_##op;\
              CASE( op + 0x0C ) /* dp+X */\
              data = (uint8_t) (data + x);

#define ADDR_MODES_NO_DP( op )\
              ADDR_MODES_( op )\
              data += dp;\
              end_##op:

#define ADDR_MODES( op )\
              ADDR_MODES_( op )\
              CASE( op - 0x04 ) /* dp */\
              data += dp;\
              end_##op:

              /* 1. 8-bit Data Transmission Commands. Group I */

              ADDR_MODES_NO_DP( 0xE8 ) /* MOV A,addr */
                 a = nz = SPC_CPU_READ( 0, data );
              goto inc_pc_loop;

         case 0xBF:
              {
                 /* MOV A,(X)+ */
                 int temp;
                 temp = x + dp;
                 x = (uint8_t) (x + 1);
                 a = nz = SPC_CPU_READ( -1, temp );
                 goto loop;
              }

         case 0xE8: /* MOV A,imm */
              a  = data;
              nz = data;
              goto inc_pc_loop;

         case 0xF9: /* MOV X,dp+Y */
              data = (uint8_t) (data + y);
         case 0xF8: /* MOV X,dp */
              READ_DP_TIMER( 0, data, x = nz );
              goto inc_pc_loop;

         case 0xE9: /* MOV X,abs */
              data = GET_LE16( pc );
              ++pc;
              data = SPC_CPU_READ( 0, data );
         case 0xCD: /* MOV X,imm */
              x  = data;
              nz = data;
              goto inc_pc_loop;

         case 0xFB: /* MOV Y,dp+X */
              data = (uint8_t) (data + x);
         case 0xEB: /* MOV Y,dp */
              /* 70% from timer */
              pc++;
              READ_DP_TIMER( 0, data, y = nz );
              goto loop;

         case 0xEC:
              { /* MOV Y,abs */
                 int temp;
                 temp = GET_LE16( pc );
                 pc += 2;
                 READ_TIMER( 0, temp, y = nz );
                 /* y = nz = SPC_CPU_READ( 0, temp ); */
                 goto loop;
              }

         case 0x8D: /* MOV Y,imm */
              y  = data;
              nz = data;
              goto inc_pc_loop;

              /* 2. 8-BIT DATA TRANSMISSION COMMANDS, GROUP 2 */

              ADDR_MODES_NO_DP( 0xC8 ) /* MOV addr,A */
                 SPC_CPU_WRITE( 0, data, a );
              goto inc_pc_loop;

              {
                 int temp;
                 case 0xCC: /* MOV abs,Y */
                 temp = y;
                 goto mov_abs_temp;
                 case 0xC9: /* MOV abs,X */
                 temp = x;
mov_abs_temp:
                 SPC_CPU_WRITE( 0, GET_LE16( pc ), temp );
                 pc += 2;
                 goto loop;
              }

         case 0xD9: /* MOV dp+Y,X */
              data = (uint8_t) (data + y);
         case 0xD8: /* MOV dp,X */
              SPC_CPU_WRITE( 0, data + dp, x );
              goto inc_pc_loop;

         case 0xDB: /* MOV dp+X,Y */
              data = (uint8_t) (data + x);
         case 0xCB: /* MOV dp,Y */
              SPC_CPU_WRITE( 0, data + dp, y );
              goto inc_pc_loop;

              /* 3. 8-BIT DATA TRANSMISSION COMMANDS, GROUP 3. */

         case 0x7D: /* MOV A,X */
              a  = x;
              nz = x;
              goto loop;

         case 0xDD: /* MOV A,Y */
              a  = y;
              nz = y;
              goto loop;

         case 0x5D: /* MOV X,A */
              x  = a;
              nz = a;
              goto loop;

         case 0xFD: /* MOV Y,A */
              y  = a;
              nz = a;
              goto loop;

         case 0x9D: /* MOV X,SP */
              x = nz = GET_SP();
              goto loop;

         case 0xBD: /* MOV SP,X */
              SET_SP( x );
              goto loop;

              /* case 0xC6: // MOV (X),A (handled by MOV addr,A in group 2) */

         case 0xAF: /* MOV (X)+,A */
              WRITE_DP( 0, x, a + NO_READ_BEFORE_WRITE  );
              x++;
              goto loop;

              /* 5. 8-BIT LOGIC OPERATION COMMANDS */

#define LOGICAL_OP( op, func )\
              ADDR_MODES( op ) /* addr */\
              data = SPC_CPU_READ( 0, data );\
         case op: /* imm */\
                 nz = a func##= data;\
              goto inc_pc_loop;\
              {   unsigned addr;\
                 case op + 0x11: /* X,Y */\
                           data = READ_DP( -2, y );\
                 addr = x + dp;\
                 goto addr_##op;\
                 case op + 0x01: /* dp,dp */\
                             data = READ_DP( -3, data );\
                 case op + 0x10:{/*dp,imm*/\
                         uint8_t const* addr2 = pc + 1;\
                         pc += 2;\
                         addr = READ_PC( addr2 ) + dp;\
                      }\
                 addr_##op:\
                 nz = data func SPC_CPU_READ( -1, addr );\
                 SPC_CPU_WRITE( 0, addr, nz );\
                 goto loop;\
              }

              LOGICAL_OP( 0x28, & ); /* AND */

              LOGICAL_OP( 0x08, | ); /* OR */

              LOGICAL_OP( 0x48, ^ ); /* EOR */

              /* 4. 8-BIT ARITHMETIC OPERATION COMMANDS */

              ADDR_MODES( 0x68 ) /* CMP addr */
                 data = SPC_CPU_READ( 0, data );
         case 0x68: /* CMP imm */
              nz = a - data;
              c = ~nz;
              nz &= 0xFF;
              goto inc_pc_loop;

         case 0x79: /* CMP (X),(Y) */
              data = READ_DP( -2, y );
              nz = READ_DP( -1, x ) - data;
              c = ~nz;
              nz &= 0xFF;
              goto loop;

         case 0x69: /* CMP dp,dp */
              data = READ_DP( -3, data );
         case 0x78: /* CMP dp,imm */
              nz = READ_DP( -1, READ_PC( ++pc ) ) - data;
              c = ~nz;
              nz &= 0xFF;
              goto inc_pc_loop;

         case 0x3E: /* CMP X,dp */
              data += dp;
              goto cmp_x_addr;
         case 0x1E: /* CMP X,abs */
              data = GET_LE16( pc );
              pc++;
cmp_x_addr:
              data = SPC_CPU_READ( 0, data );
         case 0xC8: /* CMP X,imm */
              nz = x - data;
              c = ~nz;
              nz &= 0xFF;
              goto inc_pc_loop;

         case 0x7E: /* CMP Y,dp */
              data += dp;
              goto cmp_y_addr;
         case 0x5E: /* CMP Y,abs */
              data = GET_LE16( pc );
              pc++;
cmp_y_addr:
              data = SPC_CPU_READ( 0, data );
         case 0xAD: /* CMP Y,imm */
              nz = y - data;
              c = ~nz;
              nz &= 0xFF;
              goto inc_pc_loop;

              {
                 int addr;
                 case 0xB9: /* SBC (x),(y) */
                 case 0x99: /* ADC (x),(y) */
                 pc--; /* compensate for inc later */
                 data = READ_DP( -2, y );
                 addr = x + dp;
                 goto adc_addr;
                 case 0xA9: /* SBC dp,dp */
                 case 0x89: /* ADC dp,dp */
                 data = READ_DP( -3, data );
                 case 0xB8: /* SBC dp,imm */
                 case 0x98: /* ADC dp,imm */
                 addr = READ_PC( ++pc ) + dp;
adc_addr:
                 nz = SPC_CPU_READ( -1, addr );
                 goto adc_data;

                 /* catch ADC and SBC together, then decode later based on operand */
#undef CASE
#define CASE( n ) case n: case (n) + 0x20:
                 ADDR_MODES( 0x88 ) /* ADC/SBC addr */
                    data = SPC_CPU_READ( 0, data );
                 case 0xA8: /* SBC imm */
                 case 0x88: /* ADC imm */
                 addr = -1; /* A */
                 nz = a;
adc_data: {
        int flags;
        if ( opcode >= 0xA0 ) /* SBC */
           data ^= 0xFF;

        flags = data ^ nz;
        nz += data + (c >> 8 & 1);
        flags ^= nz;

        psw = (psw & ~(V40 | H08)) |
           (flags >> 1 & H08) |
           ((flags + 0x80) >> 2 & V40);
        c = nz;
        if ( addr < 0 )
        {
           a = (uint8_t) nz;
           goto inc_pc_loop;
        }
        SPC_CPU_WRITE( 0, addr, /*(uint8_t)*/ nz );
        goto inc_pc_loop;
     }

              }

              /* 6. ADDITION & SUBTRACTION COMMANDS */

#define INC_DEC_REG( reg, op )\
              nz  = reg op;\
              reg = (uint8_t) nz;\
              goto loop;

         case 0xBC: INC_DEC_REG( a, + 1 ) /* INC A */
         case 0x3D: INC_DEC_REG( x, + 1 ) /* INC X */
         case 0xFC: INC_DEC_REG( y, + 1 ) /* INC Y */

         case 0x9C: INC_DEC_REG( a, - 1 ) /* DEC A */
         case 0x1D: INC_DEC_REG( x, - 1 ) /* DEC X */
         case 0xDC: INC_DEC_REG( y, - 1 ) /* DEC Y */

         case 0x9B: /* DEC dp+X */
         case 0xBB: /* INC dp+X */
               data = (uint8_t) (data + x);
         case 0x8B: /* DEC dp */
         case 0xAB: /* INC dp */
               data += dp;
               goto inc_abs;
         case 0x8C: /* DEC abs */
         case 0xAC: /* INC abs */
               data = GET_LE16( pc );
               pc++;
inc_abs:
               nz = (opcode >> 4 & 2) - 1;
               nz += SPC_CPU_READ( -1, data );
               SPC_CPU_WRITE( 0, data, /*(uint8_t)*/ nz );
               goto inc_pc_loop;

               /* 7. SHIFT, ROTATION COMMANDS */

         case 0x5C: /* LSR A */
               c = 0;
         case 0x7C:{ /* ROR A */
                 nz = (c >> 1 & 0x80) | (a >> 1);
                 c = a << 8;
                 a = nz;
                 goto loop;
              }

         case 0x1C: /* ASL A */
              c = 0;
         case 0x3C:
              {/* ROL A */
                 int temp;
                 temp = c >> 8 & 1;
                 c = a << 1;
                 nz = c | temp;
                 a = (uint8_t) nz;
                 goto loop;
              }

         case 0x0B: /* ASL dp */
              c = 0;
              data += dp;
              goto rol_mem;
         case 0x1B: /* ASL dp+X */
              c = 0;
         case 0x3B: /* ROL dp+X */
              data = (uint8_t) (data + x);
         case 0x2B: /* ROL dp */
              data += dp;
              goto rol_mem;
         case 0x0C: /* ASL abs */
              c = 0;
         case 0x2C: /* ROL abs */
              data = GET_LE16( pc );
              pc++;
rol_mem:
              nz = c >> 8 & 1;
              nz |= (c = SPC_CPU_READ( -1, data ) << 1);
              SPC_CPU_WRITE( 0, data, /*(uint8_t)*/ nz );
              goto inc_pc_loop;

         case 0x4B: /* LSR dp */
              c = 0;
              data += dp;
              goto ror_mem;
         case 0x5B: /* LSR dp+X */
              c = 0;
         case 0x7B: /* ROR dp+X */
              data = (uint8_t) (data + x);
         case 0x6B: /* ROR dp */
              data += dp;
              goto ror_mem;
         case 0x4C: /* LSR abs */
              c = 0;
         case 0x6C: /* ROR abs */
              data = GET_LE16( pc );
              pc++;
ror_mem: {
       int temp = SPC_CPU_READ( -1, data );
       nz = (c >> 1 & 0x80) | (temp >> 1);
       c = temp << 8;
       SPC_CPU_WRITE( 0, data, nz );
       goto inc_pc_loop;
    }

         case 0x9F: /* XCN */
    nz = a = (a >> 4) | (uint8_t) (a << 4);
    goto loop;

    /* 8. 16-BIT TRANSMISION COMMANDS */

         case 0xBA: /* MOVW YA,dp */
    a = READ_DP( -2, data );
    nz = (a & 0x7F) | (a >> 1);
    y = READ_DP( 0, (uint8_t) (data + 1) );
    nz |= y;
    goto inc_pc_loop;

         case 0xDA: /* MOVW dp,YA */
    WRITE_DP( -1, data, a );
    WRITE_DP( 0, (uint8_t) (data + 1), y + NO_READ_BEFORE_WRITE  );
    goto inc_pc_loop;

    /* 9. 16-BIT OPERATION COMMANDS */

         case 0x3A: /* INCW dp */
         case 0x1A:{/* DECW dp */
                 int temp;
                 /* low byte */
                 data += dp;
                 temp = SPC_CPU_READ( -3, data );
                 temp += (opcode >> 4 & 2) - 1; /* +1 for INCW, -1 for DECW */
                 nz = ((temp >> 1) | temp) & 0x7F;
                 SPC_CPU_WRITE( -2, data, /*(uint8_t)*/ temp );

                 /* high byte */
                 data = (uint8_t) (data + 1) + dp;
                 temp = (uint8_t) ((temp >> 8) + SPC_CPU_READ( -1, data ));
                 nz |= temp;
                 SPC_CPU_WRITE( 0, data, temp );

                 goto inc_pc_loop;
              }

         case 0x7A: /* ADDW YA,dp */
         case 0x9A:
              {/* SUBW YA,dp */
                 int lo, hi, result, flags;
                 lo = READ_DP( -2, data );
                 hi = READ_DP( 0, (uint8_t) (data + 1) );

                 if ( opcode == 0x9A ) /* SUBW */
                 {
                    lo = (lo ^ 0xFF) + 1;
                    hi ^= 0xFF;
                 }

                 lo += a;
                 result = y + hi + (lo >> 8);
                 flags = hi ^ y ^ result;

                 psw = (psw & ~(V40 | H08)) |
                    (flags >> 1 & H08) |
                    ((flags + 0x80) >> 2 & V40);
                 c = result;
                 a = (uint8_t) lo;
                 result = (uint8_t) result;
                 y = result;
                 nz = (((lo >> 1) | lo) & 0x7F) | result;

                 goto inc_pc_loop;
              }

         case 0x5A:
              { /* CMPW YA,dp */
                 int temp;
                 temp = a - READ_DP( -1, data );
                 nz = ((temp >> 1) | temp) & 0x7F;
                 temp = y + (temp >> 8);
                 temp -= READ_DP( 0, (uint8_t) (data + 1) );
                 nz |= temp;
                 c  = ~temp;
                 nz &= 0xFF;
                 goto inc_pc_loop;
              }

               /* 10. MULTIPLICATION & DIVISON COMMANDS */

         case 0xCF: { /* MUL YA */
                  unsigned temp = y * a;
                  a = (uint8_t) temp;
                  nz = ((temp >> 1) | temp) & 0x7F;
                  y = temp >> 8;
                  nz |= y;
                  goto loop;
               }

         case 0x9E: /* DIV YA,X */
               {
                  unsigned ya = y * 0x100 + a;

                  psw &= ~(H08 | V40);

                  if ( y >= x )
                     psw |= V40;

                  if ( (y & 15) >= (x & 15) )
                     psw |= H08;

                  if ( y < x * 2 )
                  {
                     a = ya / x;
                     y = ya - a * x;
                  }
                  else
                  {
                     a = 255 - (ya - x * 0x200) / (256 - x);
                     y = x   + (ya - x * 0x200) % (256 - x);
                  }

                  nz = (uint8_t) a;
                  a = (uint8_t) a;

                  goto loop;
               }

               /* 11. DECIMAL COMPENSATION COMMANDS */

         case 0xDF: /* DAA */
               if ( a > 0x99 || c & 0x100 )
               {
                  a += 0x60;
                  c = 0x100;
               }

               if ( (a & 0x0F) > 9 || psw & H08 )
                  a += 0x06;

               nz = a;
               a = (uint8_t) a;
               goto loop;

         case 0xBE: /* DAS */
               if ( a > 0x99 || !(c & 0x100) )
               {
                  a -= 0x60;
                  c = 0;
               }

               if ( (a & 0x0F) > 9 || !(psw & H08) )
                  a -= 0x06;

               nz = a;
               a = (uint8_t) a;
               goto loop;

               /* 12. BRANCHING COMMANDS */

         case 0x2F: /* BRA rel */
               pc += (int8_t) data;
               goto inc_pc_loop;

         case 0x30: /* BMI */
               BRANCH( (nz & NZ_NEG_MASK) )

         case 0x10: /* BPL */
                  BRANCH( !(nz & NZ_NEG_MASK) )

         case 0xB0: /* BCS */
                  BRANCH( c & 0x100 )

         case 0x90: /* BCC */
                  BRANCH( !(c & 0x100) )

         case 0x70: /* BVS */
                  BRANCH( psw & V40 )

         case 0x50: /* BVC */
                  BRANCH( !(psw & V40) )

#define CBRANCH( cond )\
                  {\
                     pc++;\
                     if ( cond )\
                        goto cbranch_taken_loop;\
                     rel_time -= 2;\
                     goto inc_pc_loop;\
                  }

         case 0x03: /* BBS dp.bit,rel */
         case 0x23:
         case 0x43:
         case 0x63:
         case 0x83:
         case 0xA3:
         case 0xC3:
         case 0xE3:
                  CBRANCH( READ_DP( -4, data ) >> (opcode >> 5) & 1 )

         case 0x13: /* BBC dp.bit,rel */
         case 0x33:
         case 0x53:
         case 0x73:
         case 0x93:
         case 0xB3:
         case 0xD3:
         case 0xF3:
               CBRANCH( !(READ_DP( -4, data ) >> (opcode >> 5) & 1) )

         case 0xDE: /* CBNE dp+X,rel */
                                       data = (uint8_t) (data + x);
                                       /* fall through */
         case 0x2E:{ /* CBNE dp,rel */
                 int temp;
                 /* 61% from timer */
                 READ_DP_TIMER( -4, data, temp );
                 CBRANCH( temp != a )
              }

         case 0x6E: { /* DBNZ dp,rel */
                  unsigned temp = READ_DP( -4, data ) - 1;
                  WRITE_DP( -3, (uint8_t) data, /*(uint8_t)*/ temp + NO_READ_BEFORE_WRITE  );
                  CBRANCH( temp )
               }

         case 0xFE: /* DBNZ Y,rel */
               y = (uint8_t) (y - 1);
               BRANCH( y )

         case 0x1F: /* JMP [abs+X] */
                  SET_PC( GET_LE16( pc ) + x );
                  /* fall through */
         case 0x5F: /* JMP abs */
                  SET_PC( GET_LE16( pc ) );
                  goto loop;

                  /* 13. SUB-ROUTINE CALL RETURN COMMANDS */

         case 0x0F:{/* BRK */
                 int temp;
                 int ret_addr = GET_PC();
                 SET_PC( READ_PROG16( 0xFFDE ) ); /* vector address verified */
                 PUSH16( ret_addr );
                 GET_PSW( temp );
                 psw = (psw | B10) & ~I04;
                 PUSH( temp );
                 goto loop;
              }

         case 0x4F:{/* PCALL offset */
                 int ret_addr = GET_PC() + 1;
                 SET_PC( 0xFF00 | data );
                 PUSH16( ret_addr );
                 goto loop;
              }

         case 0x01: /* TCALL n */
         case 0x11:
         case 0x21:
         case 0x31:
         case 0x41:
         case 0x51:
         case 0x61:
         case 0x71:
         case 0x81:
         case 0x91:
         case 0xA1:
         case 0xB1:
         case 0xC1:
         case 0xD1:
         case 0xE1:
         case 0xF1: {
                  int ret_addr = GET_PC();
                  SET_PC( READ_PROG16( 0xFFDE - (opcode >> 3) ) );
                  PUSH16( ret_addr );
                  goto loop;
               }

               /* 14. STACK OPERATION COMMANDS */

               {
                  int temp;
                  case 0x7F: /* RET1 */
                  temp = *sp;
                  SET_PC( GET_LE16( sp + 1 ) );
                  sp += 3;
                  goto set_psw;
                  case 0x8E: /* POP PSW */
                  POP( temp );
set_psw:
                  SET_PSW( temp );
                  goto loop;
               }

         case 0x0D: { /* PUSH PSW */
                  int temp;
                  GET_PSW( temp );
                  PUSH( temp );
                  goto loop;
               }

         case 0x2D: /* PUSH A */
               PUSH( a );
               goto loop;

         case 0x4D: /* PUSH X */
               PUSH( x );
               goto loop;

         case 0x6D: /* PUSH Y */
               PUSH( y );
               goto loop;

         case 0xAE: /* POP A */
               POP( a );
               goto loop;

         case 0xCE: /* POP X */
               POP( x );
               goto loop;

         case 0xEE: /* POP Y */
               POP( y );
               goto loop;

               /* 15. BIT OPERATION COMMANDS */

         case 0x02: /* SET1 */
         case 0x22:
         case 0x42:
         case 0x62:
         case 0x82:
         case 0xA2:
         case 0xC2:
         case 0xE2:
         case 0x12: /* CLR1 */
         case 0x32:
         case 0x52:
         case 0x72:
         case 0x92:
         case 0xB2:
         case 0xD2:
         case 0xF2:
               {
                  int bit, mask;
                  bit = 1 << (opcode >> 5);
                  mask = ~bit;
                  if ( opcode & 0x10 )
                     bit = 0;
                  data += dp;
                  SPC_CPU_WRITE( 0, data, (SPC_CPU_READ( -1, data ) & mask) | bit );
                  goto inc_pc_loop;
               }

         case 0x0E: /* TSET1 abs */
         case 0x4E: /* TCLR1 abs */
               data = GET_LE16( pc );
               pc += 2;
               {
                  unsigned temp = SPC_CPU_READ( -2, data );
                  nz = (uint8_t) (a - temp);
                  temp &= ~a;
                  if ( opcode == 0x0E )
                     temp |= a;
                  SPC_CPU_WRITE( 0, data, temp );
               }
               goto loop;

         case 0x4A: /* AND1 C,mem.bit */
               c &= MEM_BIT( 0 );
               pc += 2;
               goto loop;

         case 0x6A: /* AND1 C,/mem.bit */
               c &= ~MEM_BIT( 0 );
               pc += 2;
               goto loop;

         case 0x0A: /* OR1 C,mem.bit */
               c |= MEM_BIT( -1 );
               pc += 2;
               goto loop;

         case 0x2A: /* OR1 C,/mem.bit */
               c |= ~MEM_BIT( -1 );
               pc += 2;
               goto loop;

         case 0x8A: /* EOR1 C,mem.bit */
               c ^= MEM_BIT( -1 );
               pc += 2;
               goto loop;

         case 0xEA: /* NOT1 mem.bit */
               data = GET_LE16( pc );
               pc += 2;
               {
                  unsigned temp = SPC_CPU_READ( -1, data & 0x1FFF );
                  temp ^= 1 << (data >> 13);
                  SPC_CPU_WRITE( 0, data & 0x1FFF, temp );
               }
               goto loop;

         case 0xCA: /* MOV1 mem.bit,C */
               data = GET_LE16( pc );
               pc += 2;
               {
                  unsigned temp, bit;
                  temp = SPC_CPU_READ( -2, data & 0x1FFF );
                  bit = data >> 13;
                  temp = (temp & ~(1 << bit)) | ((c >> 8 & 1) << bit);
                  SPC_CPU_WRITE( 0, data & 0x1FFF, temp + NO_READ_BEFORE_WRITE  );
               }
               goto loop;

         case 0xAA: /* MOV1 C,mem.bit */
               c = MEM_BIT( 0 );
               pc += 2;
               goto loop;

               /* 16. PROGRAM PSW FLAG OPERATION COMMANDS */

         case 0x60: /* CLRC */
               c = 0;
               goto loop;

         case 0x80: /* SETC */
               c = ~0;
               goto loop;

         case 0xED: /* NOTC */
               c ^= 0x100;
               goto loop;

         case 0xE0: /* CLRV */
               psw &= ~(V40 | H08);
               goto loop;

         case 0x20: /* CLRP */
               dp = 0;
               goto loop;

         case 0x40: /* SETP */
               dp = 0x100;
               goto loop;

         case 0xA0: /* EI */
               psw |= I04;
               goto loop;

         case 0xC0: /* DI */
               psw &= ~I04;
               goto loop;

               /* 17. OTHER COMMANDS */

         case 0x00: /* NOP */
               goto loop;

         case 0xFF:
               { /* STOP */
                  /* handle PC wrap-around */
                  unsigned addr = GET_PC() - 1;
                  if ( addr >= 0x10000 )
                  {
                     addr &= 0xFFFF;
                     SET_PC( addr );
                     /* dprintf( "SPC: PC wrapped around\n" ); */
                     goto loop;
                  }
               }
              /* fall through */
         case 0xEF: /* SLEEP */
              --pc;
              rel_time = 0;
              goto stop;
      } /* switch */
   }
out_of_time:
   rel_time -= m.cycle_table [*pc]; /* undo partial execution of opcode */
stop:

   /* Uncache registers */
#if 0
   if ( GET_PC() >= 0x10000 )
      dprintf( "SPC: PC wrapped around\n" );
#endif
   m.cpu_regs.pc = (uint16_t) GET_PC();
   m.cpu_regs.sp = ( uint8_t) GET_SP();
   m.cpu_regs.a  = ( uint8_t) a;
   m.cpu_regs.x  = ( uint8_t) x;
   m.cpu_regs.y  = ( uint8_t) y;
   {
      int temp;
      GET_PSW( temp );
      m.cpu_regs.psw = (uint8_t) temp;
   }
   m.spc_time += rel_time;
   m.dsp_time -= rel_time;
   m.timers [0].next_time -= rel_time;
   m.timers [1].next_time -= rel_time;
   m.timers [2].next_time -= rel_time;
   return &m.smp_regs[0][R_CPUIO0];
}

/* Runs SPC to end_time and starts a new time frame at 0 */

static void spc_end_frame( int end_time )
{
   int i;
   /* Catch CPU up to as close to end as possible. If final instruction
      would exceed end, does NOT execute it and leaves m.spc_time < end. */

   if ( end_time > m.spc_time )
      spc_run_until_( end_time );

   m.spc_time     -= end_time;
   m.extra_clocks += end_time;

   /* Catch timers up to CPU */
   for ( i = 0; i < TIMER_COUNT; i++ )
   {
      if ( 0 >= m.timers[i].next_time )
         spc_run_timer_( &m.timers [i], 0 );
   }

   /* Catch DSP up to CPU */
   if ( m.dsp_time < 0 )
   {
      RUN_DSP( 0, MAX_REG_TIME );
   }

   /* Save any extra samples beyond what should be generated */
   if ( m.buf_begin )
   {
      short *main_end, *dsp_end, *out, *in;
      /* Get end pointers */
      main_end = m.buf_end;	/* end of data written to buf */
      dsp_end  = dsp_m.out;	/* end of data written to dsp.extra() */
      if ( m.buf_begin <= dsp_end && dsp_end <= main_end )
      {
         main_end = dsp_end;
         dsp_end  = dsp_m.extra; /* nothing in DSP's extra */
      }

      /* Copy any extra samples at these ends into extra_buf */
      out = m.extra_buf;
      for ( in = m.buf_begin + SPC_SAMPLE_COUNT(); in < main_end; in++ )
         *out++ = *in;
      for ( in = dsp_m.extra; in < dsp_end ; in++ )
         *out++ = *in;

      m.extra_pos = out;
   }
}

/* Support SNES_MEMORY_APURAM */

uint8_t * spc_apuram()
{
   return m.ram.ram;
}

/* Init */

static void spc_reset_buffer(void)
{
   short *out;
   /* Start with half extra buffer of silence */
   out = m.extra_buf;
   while ( out < &m.extra_buf [EXTRA_SIZE_DIV_2] )
      *out++ = 0;
   m.extra_pos = out;
   m.buf_begin = 0;
   dsp_set_output( 0, 0 );
}

/* Sets tempo, where tempo_unit = normal, tempo_unit / 2 = half speed, etc. */

static void spc_set_tempo( int t )
{
   int timer2_shift, other_shift;
   m.tempo = t;
   timer2_shift = 4; /* 64 kHz */
   other_shift  = 3; /*  8 kHz */

   m.timers [2].prescaler = timer2_shift;
   m.timers [1].prescaler = timer2_shift + other_shift;
   m.timers [0].prescaler = timer2_shift + other_shift;
}

static void spc_reset_common( int timer_counter_init )
{
   int i;
   for ( i = 0; i < TIMER_COUNT; i++ )
      m.smp_regs[1][R_T0OUT + i] = timer_counter_init;

   /* Run IPL ROM */
   memset( &m.cpu_regs, 0, sizeof(m.cpu_regs));
   m.cpu_regs.pc = ROM_ADDR;

   m.smp_regs[0][R_TEST   ] = 0x0A;
   m.smp_regs[0][R_CONTROL] = 0xB0; /* ROM enabled, clear ports */
   for ( i = 0; i < PORT_COUNT; i++ )
      m.smp_regs[1][R_CPUIO0 + i] = 0;

   /* reset time registers */
   m.spc_time      = 0;
   m.dsp_time      = 0;
   m.dsp_time = CLOCKS_PER_SAMPLE + 1;

   for ( i = 0; i < TIMER_COUNT; i++ )
   {
      Timer* t = &m.timers [i];
      t->next_time = 1;
      t->divider   = 0;
   }

   /* Registers were just loaded. Applies these new values. */
   spc_enable_rom( m.smp_regs[0][R_CONTROL] & 0x80 );

   /*	Timer registers have been loaded. Applies these to the timers. Does not
      reset timer prescalers or dividers. */
   for ( i = 0; i < TIMER_COUNT; i++ )
   {
      Timer* t = &m.timers [i];
      t->period  = IF_0_THEN_256( m.smp_regs[0][R_T0TARGET + i] );
      t->enabled = m.smp_regs[0][R_CONTROL] >> i & 1;
      t->counter = m.smp_regs[1][R_T0OUT + i] & 0x0F;
   }

   spc_set_tempo( m.tempo );

   m.extra_clocks = 0;
   spc_reset_buffer();
}

/*	Resets SPC to power-on state. This resets your output buffer, so you must
   call set_output() after this. */

static void spc_reset (void)
{
   m.cpu_regs.pc  = 0xFFC0;
   m.cpu_regs.a   = 0x00;
   m.cpu_regs.x   = 0x00;
   m.cpu_regs.y   = 0x00;
   m.cpu_regs.psw = 0x02;
   m.cpu_regs.sp  = 0xEF;
   memset( m.ram.ram, 0x00, 0x10000 );

   /*	RAM was just loaded from SPC, with $F0-$FF containing SMP registers
      and timer counts. Copies these to proper registers. */
   m.rom_enabled = dsp_m.rom_enabled = 0;

   /* Loads registers from unified 16-byte format */
   memcpy( m.smp_regs[0], &m.ram.ram[0xF0], REG_COUNT );
   memcpy( m.smp_regs[1], m.smp_regs[0], REG_COUNT );

   /* These always read back as 0 */
   m.smp_regs[1][R_TEST    ] = 0;
   m.smp_regs[1][R_CONTROL ] = 0;
   m.smp_regs[1][R_T0TARGET] = 0;
   m.smp_regs[1][R_T1TARGET] = 0;
   m.smp_regs[1][R_T2TARGET] = 0;

   /* Put STOP instruction around memory to catch PC underflow/overflow */
   memset( m.ram.padding1, CPU_PAD_FILL, sizeof m.ram.padding1 );
   memset( m.ram.padding2, CPU_PAD_FILL, sizeof m.ram.padding2 );

   spc_reset_common( 0x0F );
   dsp_reset();
}


/*	Emulates pressing reset switch on SNES. This resets your output buffer, so
   you must call set_output() after this. */

static void spc_soft_reset (void)
{
   spc_reset_common( 0 );
   dsp_soft_reset();
}

#if !SPC_NO_COPY_STATE_FUNCS
void spc_copy_state( unsigned char** io, dsp_copy_func_t copy )
{
   int i;
   spc_state_copy_t copier;
   copier.func = copy;
   copier.buf = io;

   /*	Make state data more readable by putting 64K RAM, 16 SMP registers,
      then DSP (with its 128 registers) first */

   /* RAM */
   spc_enable_rom( 0 ); /* will get re-enabled if necessary in regs_loaded() below */
   spc_copier_copy(&copier, m.ram.ram, 0x10000 );

   {
      /* SMP registers */
      uint8_t regs [REG_COUNT], regs_in [REG_COUNT];

      memcpy( regs, m.smp_regs[0], REG_COUNT );
      memcpy( regs_in, m.smp_regs[1], REG_COUNT );

      spc_copier_copy(&copier, regs, sizeof(regs));
      spc_copier_copy(&copier, regs_in, sizeof(regs_in));

      memcpy( m.smp_regs[0], regs, REG_COUNT);
      memcpy( m.smp_regs[1], regs_in, REG_COUNT );

      spc_enable_rom( m.smp_regs[0][R_CONTROL] & 0x80 );
   }

   /* CPU registers */
   SPC_COPY( uint16_t, m.cpu_regs.pc );
   SPC_COPY(  uint8_t, m.cpu_regs.a );
   SPC_COPY(  uint8_t, m.cpu_regs.x );
   SPC_COPY(  uint8_t, m.cpu_regs.y );
   SPC_COPY(  uint8_t, m.cpu_regs.psw );
   SPC_COPY(  uint8_t, m.cpu_regs.sp );
   spc_copier_extra(&copier);

   SPC_COPY( int16_t, m.spc_time );
   SPC_COPY( int16_t, m.dsp_time );

   /* DSP */
   dsp_copy_state( io, copy );

   /* Timers */
   for ( i = 0; i < TIMER_COUNT; i++ )
   {
      Timer *t;

      t = &m.timers [i];
      t->period  = IF_0_THEN_256( m.smp_regs[0][R_T0TARGET + i] );
      t->enabled = m.smp_regs[0][R_CONTROL] >> i & 1;
      SPC_COPY( int16_t, t->next_time );
      SPC_COPY( uint8_t, t->divider );
      SPC_COPY( uint8_t, t->counter );
      spc_copier_extra(&copier);
   }

   spc_set_tempo( m.tempo );

   spc_copier_extra(&copier);
}
#endif


/***********************************************************************************
 APU
***********************************************************************************/

#define APU_DEFAULT_INPUT_RATE		32000
#define APU_MINIMUM_SAMPLE_COUNT	(512*8)
#define APU_MINIMUM_SAMPLE_BLOCK	(128*8)
#define APU_NUMERATOR_NTSC		15664
#define APU_DENOMINATOR_NTSC		328125
#define APU_NUMERATOR_PAL		34176
#define APU_DENOMINATOR_PAL		709379

static apu_callback	sa_callback     = NULL;

static bool		sound_in_sync   = true;

static int		buffer_size;
static int		lag_master      = 0;
static int		lag             = 0;

static short		*landing_buffer = NULL;

static bool		resampler      = false;

static int32_t		reference_time;
static uint32_t		spc_remainder;

static int		timing_hack_denominator = TEMPO_UNIT;
/* Set these to NTSC for now. Will change to PAL in S9xAPUTimingSetSpeedup
   if necessary on game load. */
static uint32_t		ratio_numerator = APU_NUMERATOR_NTSC;
static uint32_t		ratio_denominator = APU_DENOMINATOR_NTSC;

/***********************************************************************************
   RESAMPLER
************************************************************************************/
static int rb_size;
static int rb_buffer_size;
static int rb_start;
static unsigned char *rb_buffer;
static uint32_t r_step;
static uint32_t r_frac;
static int    r_left[4], r_right[4];

#define SPACE_EMPTY() (rb_buffer_size - rb_size)
#define SPACE_FILLED() (rb_size)
#define MAX_WRITE() (SPACE_EMPTY() >> 1)
#define AVAIL() (((((uint32_t) rb_size) << 14) - r_frac) / r_step * 2)

#define RESAMPLER_MIN(a, b) ((a) < (b) ? (a) : (b))
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define SHORT_CLAMP(n) ((short) CLAMP((n), -32768, 32767))

static INLINE int32_t hermite (int32_t mu1, int32_t a, int32_t b, int32_t c, int32_t d)
{
   int32_t mu2, mu3, m0, m1, a0, a1, a2, a3;

   mu2 = ((mu1 * mu1) >> 15);
   mu3 = ((mu2 * mu1) >> 15);

   m0 = (c - a) << 14;
   m1 = (d - b) << 14;

   a0 = (((mu3 << 1) - (3 * mu2) + 32768) * b);
   a1 = ((mu3 - (mu2 << 1) + mu1) * m0) >> 15;
   a2 = ((mu3 -     mu2) * m1) >> 15;
   a3 = ((3 * mu2 - (mu3 << 1)) * c);

   return ((a0) + (a1) + (a2) + (a3)) >> 15;
}

static void resampler_clear(void)
{
   rb_start = 0;
   rb_size = 0;
   memset (rb_buffer,  0, rb_buffer_size);

   r_frac = 65536;
   r_left [0] = r_left [1] = r_left [2] = r_left [3] = 0;
   r_right[0] = r_right[1] = r_right[2] = r_right[3] = 0;
}

static void resampler_time_ratio(double ratio)
{
   r_step = 65536 * ratio;
   resampler_clear();
}

static void resampler_read(short *data, int num_samples)
{
   int i_position, o_position, consumed;
   short *internal_buffer;

   i_position = rb_start >> 1;
   internal_buffer = (short *)rb_buffer;
   o_position = 0;
   consumed = 0;

   while (o_position < num_samples && consumed < rb_buffer_size)
   {
      int s_left, s_right, max_samples;
      int hermite_val;

      s_left = internal_buffer[i_position];
      s_right = internal_buffer[i_position + 1];
      max_samples = rb_buffer_size >> 1;

      while (r_frac <= 65536 && o_position < num_samples)
      {
         hermite_val	= hermite(r_frac >> 1, r_left [0], r_left [1], r_left [2], r_left [3]);
         data[o_position]     = SHORT_CLAMP (hermite_val);
         hermite_val = hermite(r_frac >> 1, r_right[0], r_right[1], r_right[2], r_right[3]);
         data[o_position + 1] = SHORT_CLAMP (hermite_val);

         o_position += 2;

         r_frac += r_step;
      }

      if (r_frac > 65536)
      {
         r_left [0] = r_left [1];
         r_left [1] = r_left [2];
         r_left [2] = r_left [3];
         r_left [3] = s_left;

         r_right[0] = r_right[1];
         r_right[1] = r_right[2];
         r_right[2] = r_right[3];
         r_right[3] = s_right;

         r_frac -= 65536;

         i_position += 2;
         if (i_position >= max_samples)
            i_position -= max_samples;
         consumed += 2;
      }
   }

   rb_size -= consumed << 1;
   rb_start += consumed << 1;
   if (rb_start >= rb_buffer_size)
      rb_start -= rb_buffer_size;
}

static void resampler_new(int num_samples)
{
   int new_size = num_samples << 1;

   rb_buffer_size = new_size;
   rb_buffer = (unsigned char*)malloc(rb_buffer_size);
   memset (rb_buffer, 0, rb_buffer_size);

   rb_size = 0;
   rb_start = 0;
   resampler_clear();
}

static INLINE bool resampler_push(short *src, int num_samples)
{
   int bytes, end, first_write_size;
   unsigned char *src_ring;

   bytes = num_samples << 1;
   if (MAX_WRITE() < num_samples || SPACE_EMPTY() < bytes)
      return false;

   /* Ring buffer push */
   src_ring = (unsigned char*)src;
   end = (rb_start + rb_size) % rb_buffer_size;
   first_write_size = RESAMPLER_MIN(bytes, rb_buffer_size - end);

   memcpy (rb_buffer + end, src_ring, first_write_size);

   if (bytes > first_write_size)
      memcpy (rb_buffer, src_ring + first_write_size, bytes - first_write_size);

   rb_size += bytes;

   return true;
}

static INLINE void resampler_resize (int num_samples)
{
   /* int size; */
   /* size = num_samples << 1; */
   free(rb_buffer);
   rb_buffer_size = rb_size;
   rb_buffer = (unsigned char*)malloc(rb_buffer_size);
   memset (rb_buffer, 0, rb_buffer_size);

   rb_size = 0;
   rb_start = 0;
}

/***********************************************************************************
   APU
 ***********************************************************************************/

bool S9xMixSamples (short *buffer, unsigned sample_count)
{
   if (AVAIL() >= (sample_count + lag))
   {
      resampler_read(buffer, sample_count);
      if (lag == lag_master)
         lag = 0;
   }
   else
   {
      memset(buffer, 0, sample_count << 1);
      if (lag == 0)
         lag = lag_master;

      return (false);
   }

   return (true);
}

int S9xGetSampleCount (void)
{
   return AVAIL();
}

/* Sets destination for output samples */

static void spc_set_output( short* out, int size )
{
   short *out_end, *in;

   out_end = out + size;
   m.buf_begin = out;
   m.buf_end   = out_end;

   /* Copy extra to output */
   in = m.extra_buf;
   while ( in < m.extra_pos && out < out_end )
      *out++ = *in++;

   /* Handle output being full already */
   if ( out >= out_end )
   {
      /* Have DSP write to remaining extra space */
      out     = dsp_m.extra;
      out_end = &dsp_m.extra[EXTRA_SIZE];

      /* Copy any remaining extra samples as if DSP wrote them */
      while ( in < m.extra_pos )
         *out++ = *in++;
   }

   dsp_set_output( out, out_end - out );
}

void S9xFinalizeSamples (void)
{
   bool ret;

   ret = resampler_push(landing_buffer, SPC_SAMPLE_COUNT());
   sound_in_sync = false;

   /* We weren't able to process the entire buffer. Potential overrun. */
   if (!ret && Settings.SoundSync)
      return;

   if (!Settings.SoundSync || (SPACE_EMPTY() >= SPACE_FILLED()))
      sound_in_sync = true;

   m.extra_clocks &= CLOCKS_PER_SAMPLE - 1;
   spc_set_output(landing_buffer, buffer_size);
}

void S9xClearSamples (void)
{
   resampler_clear();
   lag = lag_master;
}

bool S9xSyncSound (void)
{
   if (!Settings.SoundSync || sound_in_sync)
      return true;

   sa_callback();

   return sound_in_sync;
}

void S9xSetSamplesAvailableCallback (apu_callback callback)
{
   sa_callback = callback;
}

static void UpdatePlaybackRate (void)
{
   double time_ratio;
   if (Settings.SoundInputRate == 0)
      Settings.SoundInputRate = APU_DEFAULT_INPUT_RATE;

   time_ratio = (double) Settings.SoundInputRate * TEMPO_UNIT / (Settings.SoundPlaybackRate * timing_hack_denominator);
   resampler_time_ratio(time_ratio);
}

bool S9xInitSound (int buffer_ms, int lag_ms)
{
   /*	buffer_ms : buffer size given in millisecond
      lag_ms    : allowable time-lag given in millisecond */
   int sample_count, lag_sample_count;

   sample_count     = buffer_ms * 32000 / 1000;
   lag_sample_count = lag_ms    * 32000 / 1000;

   lag_master = lag_sample_count;

   lag_master <<= 1;

   lag = lag_master;

   if (sample_count < APU_MINIMUM_SAMPLE_COUNT)
      sample_count = APU_MINIMUM_SAMPLE_COUNT;

   buffer_size = sample_count;
   buffer_size <<= 1;
   buffer_size <<= 1;

   printf("Sound buffer size: %d (%d samples)\n", buffer_size, sample_count);

   if (landing_buffer)
      free(landing_buffer);
   landing_buffer = (short*)malloc(buffer_size * 2);
   if (!landing_buffer)
      return (false);

   /* The resampler and spc unit use samples (16-bit short) as
      arguments. Use 2x in the resampler for buffer leveling with SoundSync */

   if (!resampler)
   {
      resampler_new(buffer_size >> (Settings.SoundSync ? 0 : 1));
      resampler = true;
   }
   else
      resampler_resize(buffer_size >> (Settings.SoundSync ? 0 : 1));

   m.extra_clocks &= CLOCKS_PER_SAMPLE - 1;
   spc_set_output(landing_buffer, buffer_size >> 1);

   UpdatePlaybackRate();

   return true;
}

/* Must be called once before using */
static unsigned char cycle_table [128] =
{/*   01   23   45   67   89   AB   CD   EF */
   0x28,0x47,0x34,0x36,0x26,0x54,0x54,0x68, /* 0 */
   0x48,0x47,0x45,0x56,0x55,0x65,0x22,0x46, /* 1 */
   0x28,0x47,0x34,0x36,0x26,0x54,0x54,0x74, /* 2 */
   0x48,0x47,0x45,0x56,0x55,0x65,0x22,0x38, /* 3 */
   0x28,0x47,0x34,0x36,0x26,0x44,0x54,0x66, /* 4 */
   0x48,0x47,0x45,0x56,0x55,0x45,0x22,0x43, /* 5 */
   0x28,0x47,0x34,0x36,0x26,0x44,0x54,0x75, /* 6 */
   0x48,0x47,0x45,0x56,0x55,0x55,0x22,0x36, /* 7 */
   0x28,0x47,0x34,0x36,0x26,0x54,0x52,0x45, /* 8 */
   0x48,0x47,0x45,0x56,0x55,0x55,0x22,0xC5, /* 9 */
   0x38,0x47,0x34,0x36,0x26,0x44,0x52,0x44, /* A */
   0x48,0x47,0x45,0x56,0x55,0x55,0x22,0x34, /* B */
   0x38,0x47,0x45,0x47,0x25,0x64,0x52,0x49, /* C */
   0x48,0x47,0x56,0x67,0x45,0x55,0x22,0x83, /* D */
   0x28,0x47,0x34,0x36,0x24,0x53,0x43,0x40, /* E */
   0x48,0x47,0x45,0x56,0x34,0x54,0x22,0x60, /* F */
};

static signed char const reg_times_ [256] =
{
   -1,  0,-11,-10,-15,-11, -2, -2,  4,  3, 14, 14, 26, 26, 14, 22,
   2,  3,  0,  1,-12,  0,  1,  1,  7,  6, 14, 14, 27, 14, 14, 23,
   5,  6,  3,  4, -1,  3,  4,  4, 10,  9, 14, 14, 26, -5, 14, 23,
   8,  9,  6,  7,  2,  6,  7,  7, 13, 12, 14, 14, 27, -4, 14, 24,
   11, 12,  9, 10,  5,  9, 10, 10, 16, 15, 14, 14, -2, -4, 14, 24,
   14, 15, 12, 13,  8, 12, 13, 13, 19, 18, 14, 14, -2,-36, 14, 24,
   17, 18, 15, 16, 11, 15, 16, 16, 22, 21, 14, 14, 28, -3, 14, 25,
   20, 21, 18, 19, 14, 18, 19, 19, 25, 24, 14, 14, 14, 29, 14, 25,

   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
};

bool S9xInitAPU (void)
{
   int i;

   uint8_t APUROM[64] =
   {
      0xCD, 0xEF, 0xBD, 0xE8, 0x00, 0xC6, 0x1D, 0xD0,
      0xFC, 0x8F, 0xAA, 0xF4, 0x8F, 0xBB, 0xF5, 0x78,
      0xCC, 0xF4, 0xD0, 0xFB, 0x2F, 0x19, 0xEB, 0xF4,
      0xD0, 0xFC, 0x7E, 0xF4, 0xD0, 0x0B, 0xE4, 0xF5,
      0xCB, 0xF4, 0xD7, 0x00, 0xFC, 0xD0, 0xF3, 0xAB,
      0x01, 0x10, 0xEF, 0x7E, 0xF4, 0x10, 0xEB, 0xBA,
      0xF6, 0xDA, 0x00, 0xBA, 0xF4, 0xC4, 0xF4, 0xDD,
      0x5D, 0xD0, 0xDB, 0x1F, 0x00, 0x00, 0xC0, 0xFF
   };

   memset( &m, 0, sizeof m );
   dsp_init( m.ram.ram );

   m.tempo = TEMPO_UNIT;

   /*	Most SPC music doesn't need ROM, and almost all the rest only
      rely on these two bytes */

   m.rom [0x3E] = 0xFF;
   m.rom [0x3F] = 0xC0;


   /* unpack cycle table */
   for ( i = 0; i < 128; i++ )
   {
      int n;
      n = cycle_table [i];
      m.cycle_table [i * 2 + 0] = n >> 4;
      m.cycle_table [i * 2 + 1] = n & 0x0F;
   }

   allow_time_overflow = false;

   dsp_m.rom = m.rom;
   dsp_m.hi_ram = m.hi_ram;


   memcpy( reg_times, reg_times_, sizeof reg_times );

   spc_reset();


   memcpy( m.rom, APUROM, sizeof m.rom );

   landing_buffer = NULL;

   return true;
}

void S9xDeinitAPU (void)
{
   if (resampler)
   {
      free(rb_buffer);
      resampler = false;
   }

   if (landing_buffer)
   {
      free(landing_buffer);
      landing_buffer = NULL;
   }
}

#define S9X_APU_GET_CLOCK(cpucycles)		((ratio_numerator * (cpucycles - reference_time) + spc_remainder) / ratio_denominator)
#define S9X_APU_GET_CLOCK_REMAINDER(cpucycles)	((ratio_numerator * (cpucycles - reference_time) + spc_remainder) % ratio_denominator)

/* Emulated port read at specified time */

uint8_t S9xAPUReadPort (int port)	{ return ((uint8_t) spc_run_until_(S9X_APU_GET_CLOCK(CPU.Cycles))[port]); }

/* Emulated port write at specified time */

void S9xAPUWritePort (int port, uint8_t byte)
{
   spc_run_until_( S9X_APU_GET_CLOCK(CPU.Cycles) ) [0x10 + port] = byte;
   m.ram.ram [0xF4 + port] = byte;
}

void S9xAPUSetReferenceTime (int32_t cpucycles)
{
   reference_time = cpucycles;
}

void S9xAPUExecute (void)
{
   /* Accumulate partial APU cycles */
   spc_end_frame(S9X_APU_GET_CLOCK(CPU.Cycles));

   spc_remainder = S9X_APU_GET_CLOCK_REMAINDER(CPU.Cycles);
   reference_time = CPU.Cycles;

   if (SPC_SAMPLE_COUNT() >= APU_MINIMUM_SAMPLE_BLOCK || !sound_in_sync)
      sa_callback();
}

void S9xAPUTimingSetSpeedup (int ticks)
{
   if (ticks != 0)
      printf("APU speedup hack: %d\n", ticks);

   timing_hack_denominator = TEMPO_UNIT - ticks;
   spc_set_tempo(timing_hack_denominator);

   ratio_numerator = Settings.PAL ? APU_NUMERATOR_PAL : APU_NUMERATOR_NTSC;
   ratio_denominator = Settings.PAL ? APU_DENOMINATOR_PAL : APU_DENOMINATOR_NTSC;
   ratio_denominator = ratio_denominator * timing_hack_denominator / TEMPO_UNIT;

   UpdatePlaybackRate();
}

void S9xAPUAllowTimeOverflow (bool allow)
{
   allow_time_overflow = allow;
}

void S9xResetAPU (void)
{
   reference_time = 0;
   spc_remainder = 0;
   spc_reset();

   m.extra_clocks &= CLOCKS_PER_SAMPLE - 1;

   spc_set_output(landing_buffer, buffer_size >> 1);

   resampler_clear();
}

void S9xSoftResetAPU (void)
{
   reference_time = 0;
   spc_remainder = 0;
   spc_soft_reset();

   m.extra_clocks &= CLOCKS_PER_SAMPLE - 1;
   spc_set_output(landing_buffer, buffer_size >> 1);

   resampler_clear();
}

static void from_apu_to_state (uint8_t **buf, void *var, size_t size)
{
   memcpy(*buf, var, size);
   *buf += size;
}

static void to_apu_from_state (uint8_t **buf, void *var, size_t size)
{
   memcpy(var, *buf, size);
   *buf += size;
}

// work around optimization bug in android GCC
// similar to this: http://jeffq.com/blog/over-aggressive-gcc-optimization-can-cause-sigbus-crash-when-using-memcpy-with-the-android-ndk/
#if defined(ANDROID) || defined(__QNX__)
void __attribute__((optimize(0))) S9xAPUSaveState (uint8_t *block)
#else
void S9xAPUSaveState (uint8_t *block)
#endif
{
   uint8_t *ptr;

   ptr = block;

   spc_copy_state(&ptr, from_apu_to_state);

   SET_LE32(ptr, reference_time);
   ptr += sizeof(int32_t);
   SET_LE32(ptr, spc_remainder);
}

#if defined(ANDROID) || defined(__QNX__)
void __attribute__((optimize(0))) S9xAPULoadState (uint8_t *block)
#else
void S9xAPULoadState (uint8_t *block)
#endif
{
   uint8_t *ptr;

   ptr = block;

   S9xResetAPU();

   spc_copy_state(&ptr, to_apu_from_state);

   reference_time = GET_LE32(ptr);
   ptr += sizeof(int32_t);
   spc_remainder = GET_LE32(ptr);
}

#undef  INLINE
#define INLINE static inline

#endif