summaryrefslogtreecommitdiff
path: root/old/spc700/spcgen.c
blob: 3ac06d64f60fd59bd722c739549f7d2cd559df14 (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
// notaz's SPC700 Emulator
// (c) Copyright 2006 notaz, All rights reserved.
//
// Added some modifications by Bitrider 2010-2011.
//
// this is a rewrite of spc700.cpp in ARM asm, inspired by other asm CPU cores like
// Cyclone and DrZ80. It is meant to be used in Snes9x emulator ports for ARM platforms.
//
// notes:
// "Shutdown" mechanism is not supported, so undefine SPC700_SHUTDOWN in your port.h
// code branches backwards over start of memory are not supported
// (never seen any game doing that)
//
// license:
// the code is released under Snes9x license. It would be nice if the word "notaz"
// would appear somewhere in your documentation or your program's "about" screen
// if you use this :)

/*
 * Permission to use, copy, modify and 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.
 *
 * Snes9x is freeware for PERSONAL USE only. Commercial users should
 * seek permission of the copyright holders first. Commercial use includes
 * charging money for Snes9x or software derived from Snes9x.
 *
 * 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.
 */

int one_apu_cycle[] = {13, 14, 15, 21};
int current_cycles;


// settings
#define VERSION "0.12"
#define APU_EXECUTING_OFF	124
//#define SPC_DEBUG


// includes
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>


// timings
int S9xAPUCycles [256] =
{
    /*        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, */
    /* 00 */  2, 8, 4, 5, 3, 4, 3, 6, 2, 6, 5, 4, 5, 4, 6, 8, 
    /* 10 */  2, 8, 4, 5, 4, 5, 5, 6, 5, 5, 6, 5, 2, 2, 4, 6, 
    /* 20 */  2, 8, 4, 5, 3, 4, 3, 6, 2, 6, 5, 4, 5, 4, 5, 4, 
    /* 30 */  2, 8, 4, 5, 4, 5, 5, 6, 5, 5, 6, 5, 2, 2, 3, 8, 
    /* 40 */  2, 8, 4, 5, 3, 4, 3, 6, 2, 6, 4, 4, 5, 4, 6, 6, 
    /* 50 */  2, 8, 4, 5, 4, 5, 5, 6, 5, 5, 4, 5, 2, 2, 4, 3, 
    /* 60 */  2, 8, 4, 5, 3, 4, 3, 6, 2, 6, 4, 4, 5, 4, 5, 5, 
    /* 70 */  2, 8, 4, 5, 4, 5, 5, 6, 5, 5, 5, 5, 2, 2, 3, 6, 
    /* 80 */  2, 8, 4, 5, 3, 4, 3, 6, 2, 6, 5, 4, 5, 2, 4, 5, 
    /* 90 */  2, 8, 4, 5, 4, 5, 5, 6, 5, 5, 5, 5, 2, 2,12, 5, 
    /* a0 */  3, 8, 4, 5, 3, 4, 3, 6, 2, 6, 4, 4, 5, 2, 4, 4, 
    /* b0 */  2, 8, 4, 5, 4, 5, 5, 6, 5, 5, 5, 5, 2, 2, 3, 4, 
    /* c0 */  3, 8, 4, 5, 4, 5, 4, 7, 2, 5, 6, 4, 5, 2, 4, 9, 
    /* d0 */  2, 8, 4, 5, 5, 6, 6, 7, 4, 5, 4, 5, 2, 2, 6, 3, 
    /* e0 */  2, 8, 4, 5, 3, 4, 3, 6, 2, 4, 5, 3, 4, 3, 4, 3, 
    /* f0 */  2, 8, 4, 5, 4, 5, 5, 6, 3, 4, 5, 4, 2, 2, 4, 3
};


// stuff
static FILE *AsmFile=NULL;
static int opcode=0; // 0-0xff
static int ibuffer = 0; 
static char buff[1024];


void ot(char *format, ...)
{
  va_list valist;
  int i, len;

  // notaz: stop me from leaving newlines in the middle of format string
  // and generating bad code
  for(i=0, len=strlen(format); i < len && format[i] != '\n'; i++);
  if(i < len-1 && format[len-1] != '\n') printf("\nWARNING: possible improper newline placement:\n%s\n", format);

  va_start(valist,format);
  if (AsmFile) vfprintf(AsmFile,format,valist);
  va_end(valist);
}

// ot buffered
void otb(char *format, ...)
{
  va_list valist;
  int i, len;

  // notaz: stop me from leaving newlines in the middle of format string
  // and generating bad code
  for(i=0, len=strlen(format); i < len && format[i] != '\n'; i++);
  if(i < len-1 && format[len-1] != '\n') printf(buff, "\nWARNING: possible improper newline placement:\n%s\n", format);

  va_start(valist,format);
  if (AsmFile) vsprintf(buff,format,valist);
  ibuffer = 1;
  va_end(valist);
}

void flush_buffer() {
	if (ibuffer != 0) {
		ot(buff);
		ibuffer = 0; 
	}		
}

// trashes: r0, r1, r2
// return: r0
// exit at label "2"
static void GetAPUDSP() {
	ot("GetAPUDSP:		\n");
	ot("	ldrb	r1, [spc_ram, #0xf2]		\n");	
	// r1 = IAPU.RAM [0xf2]		

	ot("	mov	r0, #0\n");
	ot("	and	r2, r1, #0X0f	\n");		
	// switch (reg & 0xf) {
	ot("	cmp	r2, #0x08\n");
	ot("	bxeq	lr\n");	// APU_ENVX = 8	
	// r1 = IAPU.RAM [0xf2] & 0x7f;		

	ot("	cmp	r2, #0x09\n");		
	// return APU.DSP [reg];
	ot("	ldrne	r2, .APU_DSP\n");
	ot("	and	r1, r1, #0X7f	\n");	// r1 = IAPU.RAM[0xf2] & 0x7f	
	ot("	ldrneb	r0, [r2, r1]		\n");	
	ot("	bxne	lr		\n");
	
	// APU_OUTX = 9
	// if (SoundData.channels [reg >> 4].state == SOUND_SILENT) return 0;
	// return ((SoundData.channels [reg >> 4].sample >> 8) | (SoundData.channels [reg >> 4].sample & 0x
	ot("	ldr	r2, .SOUNDDATA_CHANNELS\n");	
	ot("	mov	r1, r1, lsr #4		\n");
	//ot("	add	r1, r2, r1, asl #8\n");	
	//ot("	ldr	r0, [r1, #0x0]	\n");	// r0 = SoundData.channels[reg >> 4].state
	ot("	ldr	r0, [r2, r1, asl #8]	\n");
	ot("	add	r1, r2, r1, asl #8\n");
	ot("	cmp	r0, #0		\n");	// SOUND_SILENT = 0
	ot("	ldrneh	r1, [r1, #0x48]		\n"); // r1 = SoundData.channels[reg >> 4].sample
	ot("	bxeq	lr		\n");

	ot("	and	r0, r1, #0xff\n");
	ot("	orr	r0, r0, r1, lsr #8\n");
 	ot("	bx	lr		\n");
	
	ot(".APU_DSP:\n");
	ot("	.long	APU + 0x0b\n");	// &APU.DSP
	ot(".SOUNDDATA_CHANNELS:\n");
	ot("	.long	SoundData + 0x30\n"); // &SoundData.channels[0] 	
}


// bitrider
// macros
static void GetByte() {
	ot("	mov	r1, r0\n");

	ot("	ldrb	r0, [spc_ram, r1]		\n");
	
	ot("	cmp	r1, #0x0ff\n");
	ot("	bhi	1f	\n");
		
	ot("	cmp	r1, #0xf3			\n");
	ot("	addeq	lr, pc, #12	\n");	// lr = &ExitPoint
	ot("	beq	GetAPUDSP			\n");

	ot("	cmp	r1, #0xfd			\n");
	ot("	movhs	r2, #0				\n");
	ot("	strhsb	r2, [spc_ram, r1]		\n");
	ot("1:\n");


}

// trashes: r0, r1, r14
static void GetByteZ() {
	ot("	mov	r1, r0\n");

	ot("	cmp	r1, #0xf3			\n");
	ot("	addeq	lr, pc, #20	\n");	// lr = &ExitPoint
	ot("	beq	GetAPUDSP			\n");

	ot("	ldr	r14, [context, #iapu_directpage]\n");
	ot("	cmp	r1, #0xfd			\n");
	ot("	ldrb	r0, [r14, r1]			\n");
	ot("	movhs	r2, #0				\n");
	ot("	strhsb	r2, [r14, r1]			\n");
}

static void SetByte(int restore) {
	// Still should check for ShowRom
	ot("	add	r2, r1, #40			\n");
	ot("	tst	r2, #0x10000			\n");
	ot("	bne	1f				\n");

	ot("	bic	r2, r1, #0x0f\n");
	ot("	cmp	r2, #0xf0\n");
	ot("	strneb	r0, [spc_ram, r1]		\n");
	ot("	bne	3f\n");

	ot("	add	lr, pc, #20\n"); 
	
	ot("	cmp	r1, #0xf1			\n");
	ot("	beq	S9xSetAPUControl		\n");	

	ot("	cmp	r1, #0xf3			\n");   // pc + 4
	ot("	beq	S9xSetAPUDSP			\n");	// pc + 8
	ot("	b	S9xAPUSetByteFFtoF0		\n");	// pc + 12

	ot("1:						\n");
	ot("	bl	S9xAPUSetByteFFC0		\n");	
	ot("	ldr   	spc_ram, [context, #iapu_ram]	\n");
	ot("3:						\n");

}

static void SetByteZ(int restore) {
	ot("	ldr	r2, [context, #iapu_directpage]	\n");
	ot("	cmp	r2, spc_ram			\n");
	ot("	bne	2f				\n");

	ot("	cmp	r1, #0xf0			\n");
	ot("	blo  	2f				\n");

	ot("	cmp	r1, #0xfe			\n");
	ot("	bhs	1f				\n");

	if (restore) ot("	add	lr, pc, #16\n");
	else ot("	add	lr, pc, #20\n");

	ot("	cmp	r1, #0xf1			\n");
	ot("	beq	S9xSetAPUControl		\n");

	ot("	cmp	r1, #0xf3			\n");	// pc + 4
	ot("	beq	S9xSetAPUDSP			\n");	// pc + 8
	ot("	b	S9xAPUSetByteFFtoF0		\n");   // pc + 12
	//ot("6:						\n");   // pc + 16
	if (restore) {		
		ot("   	ldr   	spc_ram, [context, #iapu_ram]	\n");
		ot("	b	1f				\n");
		}
	ot("2:						\n");
	ot("	strb	r0, [r2, r1]			\n");
	ot("1:						\n");
}


//  r0-2: Temporary registers
//  r3  : current opcode or temp
//  r4  : Cycles remaining
//  r5  : Pointer to IAPU structure
//  r6  : Pointer to Opcode Jump table
//  r7  : Current PC
//  r8  : YA
//  r9  : P (nzzzzzzz ........ ........ NODBHIZC; nzzzzzzz - NZ flag in use (for a little speedup)

//  r10 : X
//  r11 : S
//  r14 : temp
//  lr  : RAM pointer

static void PrintFramework()
{


#ifndef SPC_DEBUG
	ot("  .extern IAPU\n");
#else
	ot("  .extern IAPU2\n");
#endif
	ot("  .extern CPU @ for STOP and SLEEP\n");
	ot("  .extern S9xAPUGetByte\n");
	ot("  .extern S9xAPUSetByte\n");
	ot("  .extern S9xAPUGetByteZ\n");
	ot("  .extern S9xAPUSetByteZ\n\n");

	// bitrider
	ot("   .extern S9xGetAPUDSP\n");
  	ot("   .extern S9xSetAPUDSP\n");
  	ot("   .extern S9xSetAPUControl\n");	
  	ot("   .extern S9xAPUSetByteFFC0\n");
  	ot("   .extern S9xAPUSetByteFFtoF0\n");

	ot("  .global spc700_execute @ int cycles\n");
	//ot("  .global Spc700JumpTab\n\n");
	for (current_cycles=0; current_cycles < (sizeof(one_apu_cycle) / sizeof(int)); current_cycles++) 
		ot("  .global Spc700JumpTab_%i\n", one_apu_cycle[current_cycles]);
	ot("\n");	


	ot("  opcode  .req r3\n");
	ot("  cycles  .req r4\n");
	ot("  context .req r5\n");
	ot("  opcodes .req r6\n");
	ot("  spc_pc  .req r7\n");
	ot("  spc_ya  .req r8\n");
	ot("  spc_p   .req r9\n");
	ot("  spc_x   .req r10\n");
	ot("  spc_s   .req r11\n");
	ot("  spc_ram .req r12\n\n");

	ot("  .equ iapu_directpage,    0x00\n");
	ot("  .equ iapu_ram,           0x44\n");
	ot("  .equ iapu_extraram,      0x48\n");
	ot("  .equ iapu_allregs_load,  0x30\n");
	ot("  .equ iapu_allregs_save,  0x34\n\n");

	ot("  .equ flag_c,             0x01\n");
	ot("  .equ flag_z,             0x02\n");
	ot("  .equ flag_i,             0x04\n");
	ot("  .equ flag_h,             0x08\n");
	ot("  .equ flag_b,             0x10\n");
	ot("  .equ flag_d,             0x20\n");
	ot("  .equ flag_o,             0x40\n");
	ot("  .equ flag_n,             0x80\n\n");

	ot("  .equ cpu_apu_executing,		%i \n\n", APU_EXECUTING_OFF);
	// tmp
//	ot("  .equ iapu_carry,         0x24\n");
//	ot("  .equ iapu_overflow,      0x26\n\n");

	ot("@ --------------------------- Framework --------------------------\n");
	ot("	.align 	4\n");	
	ot("spc700_execute: @ int cycles\n");

	ot("  stmfd sp!,{r4-r11,lr}\n");

#ifndef SPC_DEBUG
	ot("  ldr   context,=IAPU               @ Pointer to SIAPU struct\n");
#else
	ot("  ldr   context,=IAPU2              @ Pointer to SIAPU struct\n");
#endif
	ot("  mov   cycles,r0                   @ Cycles\n");
	ot("  add   r0,context,#iapu_allregs_load\n");
	ot("  ldmia r0,{opcodes,spc_pc,spc_ya,spc_p,spc_x,spc_ram}\n");

	ot("  ldrb  opcode,[spc_pc],#1          @ Fetch first opcode\n");
	ot("  mov   spc_s,spc_x,lsr #8\n");
	ot("  and   spc_x,spc_x,#0xff\n");
	ot("\n");

	ot("  ldr   pc,[opcodes,opcode,lsl #2]  @ Jump to opcode handler\n");
	ot("\n\n");


	ot("@ We come back here after execution\n");
	ot("spc700End:\n");
	ot("  orr   spc_x,spc_x,spc_s,lsl #8\n");
	ot("  add   r0,context,#iapu_allregs_save\n");
	ot("  stmia r0,{spc_pc,spc_ya,spc_p,spc_x}\n");
	ot("  mov   r0,cycles\n");
	ot("  ldmfd sp!,{r4-r11,pc}\n");
	ot("\n");

	ot("  .ltorg\n");
	ot("\n");

	GetAPUDSP();
}


// ---------------------------------------------------------------------------

// Trashes r0-r3
static void MemHandler(int set, int z, int save)
{
	//if(set) ot("  bl    S9xAPUSetByte%s\n", z ? "Z" : "");
	//else    ot("  bl    S9xAPUGetByte%s\n", z ? "Z" : "");

	//if(set) ot("  asm_S9xAPUSetByte%s\n", z ? "Z" : "");
	//else    ot("  asm_S9xAPUGetByte%s\n", z ? "Z" : "");
	if(set) {
		if (z) SetByteZ(save);
		else SetByte(save);
	} else {
		if (z) GetByteZ();
		else GetByte();
	}

	//if(save) ot("  ldr   spc_ram,[context,#iapu_ram]\n");
}

// pushes reg, trashes r1
static void Push(char *reg)
{
	ot("  add   r1,spc_ram,spc_s\n");
	ot("  strb  %s,[r1,#0x100]\n", reg);
	ot("  sub   spc_s,spc_s,#1\n");
}

// pushes r0, trashes r0,r1
static void PushW()
{
	ot("  add   r1,spc_ram,spc_s\n");
	ot("  sub   spc_s,spc_s,#2\n");
	ot("  strb  r0,[r1,#0xff]\n");
	ot("  mov   r0,r0,lsr #8\n");
	ot("  strb  r0,[r1,#0x100]\n");
}

// pops to reg
static void Pop(char *reg)
{
	ot("  add   %s,spc_ram,spc_s\n", reg);
	ot("  ldrb  %s,[%s,#(0x100 + 1)]\n", reg, reg);
	ot("  add   spc_s,spc_s,#1\n");
}

// pops to r0, trashes r1
static void PopW()
{
	ot("  add   r1,spc_ram,spc_s\n");
	ot("  ldrb  r0,[r1,#(0xff + 2)]\n");
	ot("  ldrb  r1,[r1,#(0x100 + 2)]\n");
	ot("  add   spc_s,spc_s,#2\n");
	ot("  orr   r0,r0,r1,lsl #8\n");
}

// // rr <- absolute, trashes r14
// rr <- absolute

static void AbsoluteAdd(int r, char *rAdd)
{	
	ot("  ldrb  r%i,[spc_pc],#1\n", r);
	ot("  ldrb  r14,[spc_pc],#1\n");
	if (rAdd) ot("  add   r%i,r%i,%s\n", r, r, rAdd);
	ot("  add   r%i,r%i,r14,lsl #8\n", r, r);
}

// // rr <- absolute, trashes r14
// rr <- absolute
static void Absolute(int r)
{	
	//ot("  ldrb  r%i,[spc_pc],#1\n", r);
	//ot("  ldrb  r14,[spc_pc],#1\n");
	//ot("  orr   r%i,r%i,r14,lsl #8\n", r, r);
	AbsoluteAdd(r, NULL);
}


// rr <- absoluteX, trashes r14
static void AbsoluteX(int r)
{
	//Absolute(r);
	//ot("  ldrb  r%i,[spc_pc],#1\n", r);
	//ot("  ldrb  r14,[spc_pc],#1\n");
	//ot("  add   r%i,r%i,spc_x\n", r, r);
	//ot("  add   r%i,r%i,r14,lsl #8\n", r, r);
	AbsoluteAdd(r, "spc_x");
}

// r0 <- absoluteY, trashes r1
static void AbsoluteY(int r)
{
	//Absolute(r);
	//ot("  ldrb  r%i,[spc_pc],#1\n", r);
	//ot("  ldrb  r14,[spc_pc],#1\n");
	//ot("  add   r%i,r%i,spc_ya,lsr #8\n", r, r);
	//ot("  add   r%i,r%i,r14,lsl #8\n", r, r);
	AbsoluteAdd(r, "spc_ya, lsr #8");
}

// rr <- IndirectIndexedY, trashes r14
static void IndirectIndexedY(int r)
{
	ot("  ldrb  r%i,[spc_pc],#1\n", r);
	ot("  ldr   r14,[context,#iapu_directpage]\n");
	ot("  ldrb  r%i,[r14,r%i]!\n", r, r);
	ot("  ldrb  r14,[r14,#1]\n");
	//ot("  orr   r%i,r%i,r14,lsl #8\n", r, r);
	ot("  add   r%i,r%i,spc_ya,lsr #8\n", r, r);
	ot("  add   r%i,r%i,r14,lsl #8\n", r, r);
}

// rr <- address, trashes r14
static void IndexedXIndirect(int r)
{
	ot("  ldrb  r%i,[spc_pc],#1\n", r);
	ot("  ldr   r14,[context,#iapu_directpage]\n");		// again, interlocks are bad
	ot("  add   r%i,r%i,spc_x\n", r, r);
	ot("  and   r%i,r%i,#0xff\n", r, r);
	ot("  ldrb  r%i,[r14,r%i]!\n", r, r);
	ot("  ldrb  r14,[r14,#1]\n");
	ot("  orr   r%i,r%i,r14,lsl #8\n", r, r);
}

// sets ZN for reg in *reg, not suitable for Y
static void SetZN8(char *reg)
{
	ot("  and   spc_p,spc_p,#0xff\n");
	ot("  orr   spc_p,spc_p,%s,lsl #24\n", reg);
}

// sets ZN for reg in *reg
static void SetZN16(char *reg)
{
	ot("  and   spc_p,spc_p,#0xff\n");
	ot("  orr   spc_p,spc_p,%s,lsl #16\n", reg);
	ot("  tst   %s,#0xff\n", reg);
	ot("  orrne spc_p,spc_p,#0x01000000\n");
}

// does ROL on r0, sets flags
static void Rol()
{
	ot("  mov   r0,r0,lsl #1\n");
	ot("  tst   spc_p,#flag_c\n");
	ot("  orrne r0,r0,#1\n");
	ot("  tst   r0,#0x100\n");
	ot("  orrne spc_p,spc_p,#flag_c\n");
	ot("  biceq spc_p,spc_p,#flag_c\n");
	SetZN8("r0");
}

// does ROR on r0, sets flags
static void Ror()
{
	ot("  tst   spc_p,#flag_c\n");
	ot("  orrne r0,r0,#0x100\n");
	ot("  movs  r0,r0,lsr #1\n");
	ot("  orrcs spc_p,spc_p,#flag_c\n");
	ot("  biccc spc_p,spc_p,#flag_c\n");
	SetZN8("r0");
}

// does ASL on r0, sets flags but doesn't cut the shifted bits
static void Asl()
{
	ot("  tst   r0,#0x80\n");
	ot("  orrne spc_p,spc_p,#flag_c\n");
	ot("  biceq spc_p,spc_p,#flag_c\n");
	ot("  mov   r0,r0,lsl #1\n");
	SetZN8("r0");
}

// does LSR on r0, sets flags
static void Lsr()
{
	ot("  tst   r0,#0x01\n");
	ot("  orrne spc_p,spc_p,#flag_c\n");
	ot("  biceq spc_p,spc_p,#flag_c\n");
	ot("  mov   r0,r0,lsr #1\n");
	SetZN8("r0");
}

// CMP rr0,rr1; trashes r14
static void Cmp(char *r0, char *r1, int and_r0)
{
	char *lop = r0;

	if(and_r0) { ot("  and   r14,%s,#0xff\n", r0); lop = "r14"; }
	ot("  subs  r14,%s,%s\n", lop, r1);
	//ot("  orrge spc_p,spc_p,#flag_c\n");
	//ot("  biclt spc_p,spc_p,#flag_c\n");
	ot("  orrcs spc_p,spc_p,#flag_c\n");
	ot("  biccc spc_p,spc_p,#flag_c\n");
	SetZN8("r14");
}

// ADC rr0,rr1 -> rr0, trashes r2,r14, does not mask to byte
static void Adc(char *r0, char *r1)
{
	ot("  eor   r2,%s,%s\n", r0, r1); // r3=(a) ^ (b)
	ot("  movs  r14, spc_p, lsr #1\n");
	ot("  adc   %s, %s, %s\n", r0, r0, r1);	
	//ot("  add   %s,%s,%s\n", r0, r0, r1);
	//ot("  tst   spc_p,#flag_c\n");
	//ot("  addne %s,%s,#1\n", r0, r0);
	ot("  movs  r14,%s,lsr #8\n", r0);
	ot("  orrne spc_p,spc_p,#flag_c\n");
	ot("  biceq spc_p,spc_p,#flag_c\n");
	ot("  eor   r14,%s,%s\n", r0, r1); // r14=(b) ^ Work16
	ot("  bic   r14,r14,r2\n"); // ((b) ^ Work16) & ~((a) ^ (b))
	ot("  tst   r14,#0x80\n");
	ot("  orrne spc_p,spc_p,#flag_o\n");
	ot("  biceq spc_p,spc_p,#flag_o\n");
	ot("  eor   r14,r2,%s\n", r0);
	ot("  tst   r14,#0x10\n");
	ot("  orrne spc_p,spc_p,#flag_h\n");
	ot("  biceq spc_p,spc_p,#flag_h\n");
}

// SBC rr0,rr1 -> rr0, trashes r2,r3,r14, does not mask to byte
static void Sbc(char *r0, char *r1)
{
	ot("  movs  r14,spc_p,lsr #1\n");
	ot("  sbcs  r2,%s,%s\n", r0, r1);
	ot("  orrge spc_p,spc_p,#flag_c\n");
	ot("  biclt spc_p,spc_p,#flag_c\n");
	ot("  eor   r14,%s,r2\n", r0); // r14=(a) ^ Int16
	ot("  eor   r3,%s,%s\n", r0, r1); // r3=(a) ^ (b)
	ot("  and   r14,r14,r3\n"); // ((a) ^ Work16) & ((a) ^ (b))
	ot("  tst   r14,#0x80\n");
	ot("  orrne spc_p,spc_p,#flag_o\n");
	ot("  biceq spc_p,spc_p,#flag_o\n");
	ot("  eor   r14,r3,r2\n");
	ot("  tst   r14,#0x10\n");
	ot("  orreq spc_p,spc_p,#flag_h\n");
	ot("  bicne spc_p,spc_p,#flag_h\n");
	ot("  mov   %s,r2\n", r0);
}


// 
static void TCall()
{
	ot("  sub   r0,spc_pc,spc_ram\n");
	PushW();
	ot("  ldr   r0,[context,#iapu_extraram]\n");
	ot("  ldrh  r0,[r0,#0x%x]\n", (15-(opcode>>4))<<1);
	ot("  add   spc_pc,spc_ram,r0\n");
}

// 
static void SetClr1()
{
	ot("  ldrb  r0,[spc_pc]\n");
	MemHandler(0, 1, 0);
	ot("  %s   r0,r0,#0x%02x\n", opcode & 0x10 ? "bic" : "orr", 1<<(opcode>>5));
	ot("  ldrb  r1,[spc_pc],#1\n");
	MemHandler(1, 1, 1);
}

// 
static void BssBbc()
{
	ot("  ldrb  r0,[spc_pc],#1\n");
	MemHandler(0, 1, 1);
	ot("  tst   r0,#0x%02x\n", 1<<(opcode>>5));
	ot("  add%s spc_pc,spc_pc,#1\n", opcode & 0x10 ? "ne" : "eq");
	ot("  ldr%ssb r0,[spc_pc],#1\n", opcode & 0x10 ? "eq" : "ne");
	ot("  sub%s cycles,cycles,#%i\n",opcode & 0x10 ? "eq" : "ne", one_apu_cycle[current_cycles]*2);
	ot("  add%s spc_pc,spc_pc,r0\n", opcode & 0x10 ? "eq" : "ne");
}

//
static void Membit()
{
	ot("  ldrb  r0,[spc_pc], #1\n");	
	ot("  ldrb  r3,[spc_pc], #1\n");
	//ot("  orr   spc_x,spc_x,r1,lsl #(29-5) 
	ot("  add   r3,r0,r3,lsl #8\n");	//@ store membit where it can survive memhandler call\n"); // saving bit 12 ?	
	//ot("  mov   r1,r1,lsr #5\n");
	//ot("  mov   r0,r0,lsl #19\n");
	//ot("  mov   r0,r0,lsr #19\n");
	//if((opcode >> 4) >= 0xC) ot("  mov r3, r0\n"); //ot("  stmfd sp!,{r0}\n"); // membit
	ot("  bic   r0, r3, #0xe000\n");	// Clear bits 15, 14 & 13 => r0 = r0 & 0x1fff
	MemHandler(0, 0, 0);
	//ot("  mov   r1,spc_x,lsr #29\n");
	//ot("  and   spc_x,spc_x,#0xff\n");
	ot("  mov   r1, r3, lsr #13\n");  // membit = bits[15:13] of memory address
	if((opcode >> 4) < 0xC) {
		ot("  mov   r0,r0,lsr r1\n");
		ot("  tst   r0,#1\n");
		switch(opcode >> 4) {
			case 0x0: ot("  orrne spc_p,spc_p,#flag_c\n"); break; // OR1 C,membit
			case 0x2: ot("  orreq spc_p,spc_p,#flag_c\n"); break; // OR1 C,not membit
			case 0x4: ot("  biceq spc_p,spc_p,#flag_c\n"); break; // AND1 C,membit
			case 0x6: ot("  bicne spc_p,spc_p,#flag_c\n"); break; // AND1 C, not membit
			case 0x8: ot("  eorne spc_p,spc_p,#flag_c\n"); break; // EOR1 C, membit
			case 0xA: ot("  orrne spc_p,spc_p,#flag_c\n");        // MOV1 C,membit
				  ot("  biceq spc_p,spc_p,#flag_c\n"); break;
		}
	} else {
		ot("  mov   r2,#1\n");
		ot("  mov   r2,r2,lsl r1\n");
		if((opcode >> 4) == 0xC) { // MOV1 membit,C
			ot("  tst   spc_p,#flag_c\n");
			ot("  orrne r0,r0,r2\n");
			ot("  biceq r0,r0,r2\n");
		} else { // NOT1 membit
			ot("  eor   r0,r0,r2\n");
		}
		//ot("  ldmfd sp!,{r1}\n");
		ot("  bic   r1, r3, #0xe000\n");	// Clear bits 15, 14 & 13 => r0 = r0 & 0x1fff
		MemHandler(1, 0, 0);
	}
	//ot("  ldr   spc_ram,[context,#iapu_ram] @ restore what memhandler(s) messed up\n");
}

//
static void CBranch()
{
	int tests[]  = { 0x80000000, 0x40, 0x01, 0xff000000 }; // NOCZ
	char *eq = "eq";
	char *ne = "ne";

	if((opcode>>6) == 3) { // zero test inverts everything
		eq = "ne";
		ne = "eq";
	}

	ot("  tst   spc_p,#0x%08X\n", tests[opcode>>6]);
	ot("  add%s spc_pc,spc_pc,#1\n",  opcode & 0x20 ? eq : ne);
/*
	ot("  b%s   Apu%02X\n",  opcode & 0x20 ? eq : ne, opcode);
	ot("  sub   r0,spc_pc,spc_ram\n");
	ot("  ldrsb r1,[spc_pc],#1\n");
	ot("  add   r0,r0,r1\n");
	ot("  mov   r0,r0,lsl #16\n");
	ot("  add   spc_pc,spc_ram,r0,lsr #16\n");
*/
	ot("  ldr%ssb r0,[spc_pc],#1\n",  opcode & 0x20 ? ne : eq);

	ot("  sub%s cycles,cycles,#%i\n", opcode & 0x20 ? ne : eq, one_apu_cycle[current_cycles]*2);
	ot("  add%s spc_pc,spc_pc,r0\n",  opcode & 0x20 ? ne : eq);
//	ot("Apu%02X:\n", opcode);
}

// NeededOperation spc_ya,r0 -> spc_ya
static void ArithOpToA()
{
	// special A pre-processing
	if((opcode>>5) == 4 || (opcode>>5) == 5) {
		ot("  and   r1,spc_ya,#0xff00\n");
		ot("  and   spc_ya,spc_ya,#0xff\n");
	}

	switch(opcode>>5) {
		case 0: ot("  orr   spc_ya,spc_ya,r0\n"); break; // OR
		case 1: ot("  orr   r0,r0,#0xff00\n");
			ot("  and   spc_ya,spc_ya,r0\n"); break; // AND
		case 2: ot("  eor   spc_ya,spc_ya,r0\n"); break; // EOR
		case 3: Cmp("spc_ya", "r0", 1); break; // CMP
		case 4: Adc("spc_ya", "r0");    break; // ADC
		case 5: Sbc("spc_ya", "r0");    break; // SBC
		case 6: printf("MOV (reversed)!?\n");     break; // MOV (reversed)
		case 7: ot("  and   spc_ya,spc_ya,#0xff00\n");
			ot("  orr   spc_ya,spc_ya,r0\n"); break; // MOV
	}

	if((opcode>>5) != 3) SetZN8("spc_ya"); // only if not Cmp

	// special A post-processing
	if((opcode>>5) == 4 || (opcode>>5) == 5) {
		ot("  and   spc_ya,spc_ya,#0xff\n");
		ot("  orr   spc_ya,spc_ya,r1\n");
	}
}

//
static void ArithmeticToA()
{
	switch(opcode&0x1f) {
		case 0x04: // OP A,dp
			ot("  ldrb  r0,[spc_pc],#1\n");
			MemHandler(0, 1, 1);
			ArithOpToA();
			break;

		case 0x05: // OP A,abs
			Absolute(0);
			MemHandler(0, 0, 1);
			ArithOpToA();
			break;

		case 0x06: // OP A,(X)
			ot("  mov   r0,spc_x\n");
			MemHandler(0, 1, 1);
			ArithOpToA();
			break;

		case 0x07: // OP A,(dp+X)
			IndexedXIndirect(0);
			MemHandler(0, 0, 1);
			ArithOpToA();
			break;

		case 0x08: // OP A,#00
			ot("  ldrb  r0,[spc_pc],#1\n");
			ArithOpToA();
			break;

		case 0x14: // OP A,dp+X
			ot("  ldrb  r0,[spc_pc],#1\n");
			ot("  add   r0,r0,spc_x\n");
			MemHandler(0, 1, 1);
			ArithOpToA();
			break;

		case 0x15: // OP A,abs+X
			AbsoluteX(0);
			MemHandler(0, 0, 1);
			ArithOpToA();
			break;

		case 0x16: // OP A,abs+Y
			AbsoluteY(0);
			MemHandler(0, 0, 1);
			ArithOpToA();
			break;

		case 0x17: // OP A,(dp)+Y
			IndirectIndexedY(0);
			MemHandler(0, 0, 1);
			ArithOpToA();
			break;

		default:
			printf("Op %02X - arithmetic??\n", opcode);
	}
}

void printOpcodes(int apu_cycles) {
	for(opcode = 0; opcode < 0x100; opcode++) {
		printf("%02X", opcode);

		ot("\n\n");
		//tmp_prologue();
		ot("Apu%02X_%i:\n", opcode, apu_cycles);

		if((opcode & 0x1f) == 0x10) CBranch();  // BXX
		if((opcode & 0x0f) == 0x01) TCall();    // TCALL X
		if((opcode & 0x0f) == 0x02) SetClr1();  // SET1/CLR1 direct page bit X
		if((opcode & 0x0f) == 0x03) BssBbc();   // BBS/BBC direct page bit X
		if((opcode & 0x1f) == 0x0A) Membit();   // membit ops
		if((opcode & 0x0f) >= 0x04 && (opcode & 0x0f) <= 0x08 && (opcode & 0x1f) != 0x18 && (opcode >> 5) != 6)
									ArithmeticToA();


		switch(opcode) {
			case 0x00: // NOP
				break;

			case 0x3F: // CALL absolute
				Absolute(2);
				ot("  sub   r0,spc_pc,spc_ram\n");
				PushW();
				ot("  add   spc_pc,spc_ram,r2\n");
				break;

			case 0x4F: // PCALL $XX
				ot("  ldrb  r2,[spc_pc],#1\n");
				ot("  sub   r0,spc_pc,spc_ram\n");
				PushW();
				ot("  add   spc_pc,spc_ram,r2\n");
				ot("  add   spc_pc,spc_pc,#0xff00\n");
				break;

			case 0x09: // OR dp(dest),dp(src)
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 0);
				//ot("  orr   spc_x,spc_x,r0,lsl #24 @ save from harm\n");
				ot("  mov   r3, r0\n");
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				ot("   orr  r0, r0, r3\n");				
				//ot("  orr   r0,r0,spc_x,lsr #24\n");
				//ot("  and   spc_x,spc_x,#0xff\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x18: // OR dp,#00
				ot("  ldrb  r0,[spc_pc,#1]\n");
				MemHandler(0, 1, 0);
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  orr   r0,r0,r1\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x19: // OR (X),(Y)
				ot("  mov   r0,spc_x\n");
				MemHandler(0, 1, 0);
				//ot("  orr   spc_x,spc_x,r0,lsl #24\n");
				ot("  mov   r3, r0\n");				
				ot("  mov   r0,spc_ya,lsr #8\n");
				MemHandler(0, 1, 0);
				ot("  orr   r0, r3, r0\n");
				//ot("  orr   r0,r0,spc_x,lsr #24\n");
				//ot("  and   spc_x,spc_x,#0xff\n");
				SetZN8("r0");
				ot("  mov   r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0x0B: // ASL dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				Asl();
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x0C: // ASL abs
				Absolute(0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov r3, r0\n");				
				MemHandler(0, 0, 0);
				Asl();
				//ot("  ldmfd sp!,{r1}\n");
				ot("  mov r1, r3\n");				
				MemHandler(1, 0, 1);
				break;

			case 0x1B: // ASL dp+X
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  add   r0,r0,spc_x\n");
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov r3, r0\n");				
				MemHandler(0, 1, 0);
				Asl();
				//ot("  ldmfd sp!,{r1}\n");
				ot("  mov r1, r3\n");				
				MemHandler(1, 1, 1);
				break;

			case 0x1C: // ASL A
				ot("  tst   spc_ya,#0x80\n");
				ot("  orrne spc_p,spc_p,#flag_c\n");
				ot("  biceq spc_p,spc_p,#flag_c\n");
				ot("  and   r0,spc_ya,#0x7f\n");
				ot("  and   spc_ya,spc_ya,#0xff00\n");
				ot("  orr   spc_ya,spc_ya,r0,lsl #1\n");
				SetZN8("spc_ya");
				break;

			case 0x0D: // PUSH PSW
				ot("  mov   r0,spc_p,lsr #24\n");
				ot("  and   r1,r0,#0x80\n");
				ot("  tst   r0,r0\n");
				ot("  orreq r1,r1,#flag_z\n");
				ot("  and   spc_p,spc_p,#0x7d @ clear N & Z\n");
				ot("  orr   spc_p,spc_p,r1\n");
				Push("spc_p");
				otb("  orr   spc_p,spc_p,r0,lsl #24\n");
				break;

			case 0x2D: // PUSH A
				Push("spc_ya");
				break;

			case 0x4D: // PUSH X
				Push("spc_x");
				break;

			case 0x6D: // PUSH Y
				ot("  mov   r0,spc_ya,lsr #8\n");
				Push("r0");
				break;

			case 0x8E: // POP PSW
				Pop("spc_p");
				ot("  and   r0,spc_p,#(flag_z|flag_n)\n");
				ot("  eor   r0,r0,#flag_z\n");
				ot("  orr   spc_p,spc_p,r0,lsl #24\n");
				ot("  tst   spc_p,#flag_d\n");
				ot("  addne r0,spc_ram,#0x100\n");
				ot("  moveq r0,spc_ram\n");
				otb("  str   r0,[context,#iapu_directpage]\n");
				break;

			case 0xAE: // POP A
				Pop("r0");
				ot("  and   spc_ya,spc_ya,#0xff00\n");
				otb("  orr   spc_ya,spc_ya,r0\n");
				break;

			case 0xCE: // POP X
				Pop("spc_x");
				break;

			case 0xEE: // POP X
				Pop("r0");
				ot("  and   spc_ya,spc_ya,#0xff\n");
				otb("  orr   spc_ya,spc_ya,r0,lsl #8\n");
				break;

			case 0x0E: // TSET1 abs
				Absolute(0);
				ot("  mov   r3, r0\n");
				//ot("  orr   spc_x,spc_x,r0,lsl #16 @ save from memhandler\n");
				MemHandler(0, 0, 0);
				ot("  and   r2,r0,spc_ya\n");
				SetZN8("r2");
				ot("  orr   r0,r0,spc_ya\n");
				ot("  mov   r1, r3\n");
				//ot("  mov   r1,spc_x,lsr #16\n");
				//ot("  and   spc_x,spc_x,#0xff\n");
				MemHandler(1, 0, 1);
				break;

			case 0x4E: // TCLR1 abs
				Absolute(0);
				ot("  mov   r3, r0\n");
				//ot("  orr   spc_x,spc_x,r0,lsl #16 @ save from memhandler\n");
				MemHandler(0, 0, 0);
				ot("  and   r2,r0,spc_ya\n");
				SetZN8("r2");
				ot("  bic   r0,r0,spc_ya\n");
				ot("  mov   r1, r3\n");
				//ot("  mov   r1,spc_x,lsr #16\n");
				//ot("  and   spc_x,spc_x,#0xff\n");
				MemHandler(1, 0, 1);
				break;

			case 0x0F: // BRK
				ot("  sub   r0,spc_pc,spc_ram\n");
				PushW();
				ot("  mov   r0,spc_p,lsr #24\n");
				ot("  and   r1,r0,#0x80\n");
				ot("  tst   r0,r0\n");
				ot("  orrne r1,r1,#flag_z\n");
				ot("  and   spc_p,spc_p,#0x7d @ clear N & Z\n");
				ot("  orr   spc_p,spc_p,r1\n");
				Push("spc_p");
				ot("  orr   spc_p,spc_p,#flag_b\n");
				ot("  bic   spc_p,spc_p,#flag_i\n");
				ot("  ldr   r0,[context,#iapu_extraram]\n");
				ot("  ldrh  r0,[r0,#0x20]\n");
				ot("  add   spc_pc,spc_ram,r0\n");
				break;

			case 0xEF: // SLEEP
			case 0xFF: // STOP: this is to be compatible with yoyofr's code
				//ot("  ldr   r0,=CPU\n");
				ot("	ldr	r0, 5001f\n", apu_cycles);
				ot("  mov   r1,#0\n");
				//otb("  strb  r1,[r0,#122]\n");				
				otb("  str  r1,[r0,#cpu_apu_executing]\n");
				//tmp_epilogue();
				ot("  subs   cycles,cycles,#%i\n", S9xAPUCycles[opcode] * one_apu_cycle[current_cycles]);
				ot("  ldrgeb opcode,[spc_pc],#1\n");
				flush_buffer();		
				ot("  ldrge  pc,[opcodes,opcode,lsl #2]\n");
				ot("  b      spc700End\n");
				// don't let code flow until here
				ot("5001:\n", apu_cycles);
				ot("	.long	CPU	\n");
				break;

			case 0x2F: // BRA
				ot("  ldrsb r0,[spc_pc],#1\n");
				ot("  add   spc_pc,spc_pc,r0\n");
				break;

			case 0x80: // SETC
				otb("  orr   spc_p,spc_p,#flag_c\n");
				break;

			case 0xED: // NOTC
				otb("  eor   spc_p,spc_p,#flag_c\n");
				break;

			case 0x40: // SETP
				ot("  orr   spc_p,spc_p,#flag_d\n");
				ot("  add   r0,spc_ram,#0x100\n");
				otb("  str   r0,[context,#iapu_directpage]\n");
				break;

			case 0x1A: // DECW dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				ot("  ldrb  r0,[spc_pc]\n");
				ot("  add   r0,r0,#1\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				ot("  orr   r1,r3,r0,lsl #8\n");
				ot("  sub   r0,r1,#1\n");
				SetZN16("r0");
				ot("  stmfd sp!,{r0}\n");
				ot("  ldrb  r1,[spc_pc]\n");
				MemHandler(1, 1, 0);
				ot("  ldmfd sp!,{r0}\n");
				ot("  mov   r0,r0,lsr #8\n");
				ot("  ldrb  r1,[spc_pc],#1\n");

				ot("  add   r1,r1,#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x5A: // CMPW YA,dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  add   r0,r0,#1\n");
				MemHandler(0, 1, 1);
				//ot("  ldmfd sp!,{r1}\n");
				ot("  orr   r1,r3,r0,lsl #8\n");
				ot("  subs  r0,spc_ya,r1\n");
				ot("  orrge spc_p,spc_p,#flag_c\n");
				ot("  biclt spc_p,spc_p,#flag_c\n");
				SetZN16("r0");
				break;

			case 0x3A: // INCW dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				ot("  ldrb  r0,[spc_pc]\n");
				ot("  add   r0,r0,#1\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				ot("  orr   r1,r3,r0,lsl #8\n");
				ot("  add   r0,r1,#1\n");
				SetZN16("r0");
				ot("  stmfd sp!,{r0}\n");
				ot("  ldrb  r1,[spc_pc]\n");
				MemHandler(1, 1, 0);
				ot("  ldmfd sp!,{r0}\n");
				ot("  mov   r0,r0,lsr #8\n");
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  add   r1,r1,#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x7A: // ADDW YA,dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  add   r0,r0,#1\n");
				MemHandler(0, 1, 1);
				//ot("  ldmfd sp!,{r1}\n");
				ot("  orr   r1,r3,r0,lsl #8\n");
				ot("  add   r0,spc_ya,r1\n");
				ot("  movs  r2,r0,lsr #16\n");
				ot("  orrne spc_p,spc_p,#flag_c\n");
				ot("  biceq spc_p,spc_p,#flag_c\n");
				ot("  bic   r2,r0,#0x00ff0000\n");
				ot("  eor   r3,r1,r2\n"); // Work16 ^ (uint16) Work32
				ot("  eor   r14,spc_ya,r1\n");
				ot("  mvn   r14,r14\n");  // ~(pIAPU->YA.W ^ Work16)
				ot("  and   r14,r14,r3\n");
				ot("  tst   r14,#0x8000\n");
				ot("  orrne spc_p,spc_p,#flag_o\n");
				ot("  biceq spc_p,spc_p,#flag_o\n");
				ot("  eor   r14,r3,spc_ya\n");
				ot("  tst   r14,#0x10\n");
				ot("  orrne spc_p,spc_p,#flag_h\n");
				ot("  biceq spc_p,spc_p,#flag_h\n");
				ot("  mov   spc_ya,r2\n");
				SetZN16("spc_ya");

				break;

			case 0x9A: // SUBW YA,dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  add   r0,r0,#1\n");
				MemHandler(0, 1, 1);
				//ot("  ldmfd sp!,{r1}\n");
				ot("  orr   r1,r3,r0,lsl #8\n");
				ot("  subs  r0,spc_ya,r1\n");
				ot("  orrge spc_p,spc_p,#flag_c\n");
				ot("  biclt spc_p,spc_p,#flag_c\n");
				ot("  mov   r2,r0,lsl #16\n");
				ot("  mov   r2,r2,lsr #16\n"); // r2=(uint16) Int32
				ot("  eor   r3,spc_ya,r2\n");  // r3=pIAPU->YA.W ^ (uint16) Int32
				ot("  eor   r14,spc_ya,r1\n");
				ot("  and   r14,r14,r3\n");
				ot("  tst   r14,#0x8000\n");
				ot("  orrne spc_p,spc_p,#flag_o\n");
				ot("  biceq spc_p,spc_p,#flag_o\n");
				ot("  eor   r14,r3,r1\n");
				ot("  tst   r14,#0x10\n");
				ot("  bicne spc_p,spc_p,#flag_h\n");
				ot("  orreq spc_p,spc_p,#flag_h\n");
				ot("  mov   spc_ya,r2\n");
				SetZN16("spc_ya");
				break;

			case 0xBA: // MOVW YA,dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				ot("  ldrb  r1, [spc_pc],#1\n");
				ot("  mov   spc_ya, r0\n");	// avoiding inter-locks
				ot("  add   r0, r1, #1\n");
				MemHandler(0, 1, 1);
				ot("  orr   spc_ya,spc_ya,r0,lsl #8\n");
				SetZN16("spc_ya");
				break;

			case 0xDA: // MOVW dp,YA
				ot("  ldrb  r1,[spc_pc]\n");
				ot("  mov   r0,spc_ya\n");
				MemHandler(1, 1, 0);
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  mov   r0,spc_ya,lsr #8\n");  // avoiding inter-locks
				ot("  add   r1,r1,#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x69: // CMP dp(dest), dp(src)
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 0);
				ot("  orr   spc_x,spc_x,r0,lsl #24\n");
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 1);
				ot("  mov   r1,spc_x,lsr #24\n");
				Cmp("r0", "r1", 0);
				otb("  and   spc_x,spc_x,#0xff\n");
				break;

			case 0x78: // CMP dp,#00
				ot("  ldrb  r0,[spc_pc,#1]\n");
				MemHandler(0, 1, 1);
				ot("  ldrb  r1,[spc_pc],#2\n");
				Cmp("r0", "r1", 0);
				break;

			case 0x79: // CMP (X),(Y)
				ot("  mov   r0,spc_x\n");
				MemHandler(0, 1, 0);
				ot("  orr   spc_x,spc_x,r0,lsl #24\n");
				ot("  mov   r0,spc_ya,lsr #8\n");
				MemHandler(0, 1, 1);
				ot("  mov   r1,spc_x,lsr #24\n");
				Cmp("r1", "r0", 0);
				otb("  and   spc_x,spc_x,#0xff\n");
				break;

			case 0x1E: // CMP X,abs
				Absolute(0);
				MemHandler(0, 0, 1);
				Cmp("spc_x", "r0", 0);
				break;

			case 0x3E: // CMP X,dp
				ot("  ldrb  r0,[spc_pc],#1\n");

				MemHandler(0, 1, 1);
				Cmp("spc_x", "r0", 0);
				break;

			case 0xC8: // CMP X,#00
				ot("  ldrb  r0,[spc_pc],#1\n");
				Cmp("spc_x", "r0", 0);
				break;

			case 0x5E: // CMP Y,abs
				Absolute(0);
				MemHandler(0, 0, 1);
				ot("  mov   r1,spc_ya,lsr #8\n");
				Cmp("r1", "r0", 0);
				break;

			case 0x7E: // CMP Y,dp
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 1);
				ot("  mov   r1,spc_ya,lsr #8\n");
				Cmp("r1", "r0", 0);
				break;

			case 0xAD: // CMP Y,#00
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  mov   r1,spc_ya,lsr #8\n");
				Cmp("r1", "r0", 0);
				break;

			case 0x1F: // JMP (abs+X)
				AbsoluteX(0);
				ot("  sub   sp,sp,#8\n");
				ot("  str   r0,[sp,#4]\n");
				MemHandler(0, 0, 0);
				ot("  str   r0,[sp]\n");
				ot("  ldr   r0,[sp,#4]\n");
				ot("  add   r0,r0,#1\n");
				MemHandler(0, 0, 1);
				ot("  ldr   r1,[sp],#8\n");
				ot("  orr   r0,r1,r0,lsl #8\n");
				ot("  add   spc_pc,spc_ram,r0\n");
				break;

			case 0x5F: // JMP abs
				//Absolute(0);
				//ot("  add   spc_pc,spc_ram,r0\n");
				ot("  ldrb  r0, [spc_pc], #1\n");
				ot("  ldrb  r14, [spc_pc], #1\n");
				ot("  add   spc_pc, r0, spc_ram\n");
				ot("  add   spc_pc, spc_pc, r14, lsl #8\n");
				break;

			case 0x20: // CLRP
				ot("  bic   spc_p,spc_p,#flag_d\n");
				otb("  str   spc_ram,[context,#iapu_directpage]\n");
				break;

			case 0x60: // CLRC
				otb("  bic   spc_p,spc_p,#flag_c\n");
				break;

			case 0xE0: // CLRV
				otb("  bic   spc_p,spc_p,#(flag_o|flag_h)\n");
				break;

			case 0x29: // AND dp(dest), dp(src)
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				ot("  and   r0,r0,r3\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x38: // AND dp,#00
				ot("  ldrb  r0,[spc_pc,#1]\n");
				MemHandler(0, 1, 0);
				ot("  ldrb  r1,[spc_pc],#2\n");
				ot("  and   r0,r0,r1\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc,#-1]\n");
				MemHandler(1, 1, 1);
				break;

			case 0x39: // AND (X),(Y)
				ot("  mov   r0,spc_x\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				ot("  mov   r0,spc_ya,lsr #8\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				ot("  and   r0,r0,r3\n");
				SetZN8("r0");
				ot("  mov   r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0x2B: // ROL dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				Rol();
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x2C: // ROL abs
				Absolute(0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				MemHandler(0, 0, 0);
				Rol();
				//ot("  ldmfd sp!,{r1}\n");
				ot("  mov   r1, r3\n");				
				MemHandler(1, 0, 1);
				break;

			case 0x3B: // ROL dp+X
				ot("  ldrb  r0,[spc_pc]\n");
				ot("  add   r0,r0,spc_x\n");
				MemHandler(0, 1, 0);
				Rol();
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  add   r1,r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0x3C: // ROL A
				ot("  and   r0,spc_ya,#0xff\n");
				Rol();
				ot("  and   r0,r0,#0xff\n");
				ot("  mov   spc_ya,spc_ya,lsr #8\n");
				otb("  orr   spc_ya,r0,spc_ya,lsl #8\n");
				break;

			case 0x2E: // CBNE dp,rel
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 1);
				ot("  and   r1,spc_ya,#0xff\n");
				ot("  cmp   r0,r1\n");
				ot("  addeq spc_pc,spc_pc,#1\n");
				ot("  ldrnesb r0,[spc_pc],#1\n");
				ot("  subne cycles,cycles,#%i\n", one_apu_cycle[current_cycles]*2);
				ot("  addne spc_pc,spc_pc,r0\n");
				break;

			case 0xDE: // CBNE dp+X,rel
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  add   r0,r0,spc_x\n");
				MemHandler(0, 1, 1);
				ot("  and   r1,spc_ya,#0xff\n");
				ot("  cmp   r0,r1\n");
				ot("  addeq spc_pc,spc_pc,#1\n");
				ot("  ldrnesb r0,[spc_pc],#1\n");
				ot("  addne spc_pc,spc_pc,r0\n");
				ot("  subne cycles,cycles,#%i\n", one_apu_cycle[current_cycles]*2);
				break;

			case 0x3D: // INC X
				ot("  add   spc_x,spc_x,#1\n");
				ot("  and   spc_x,spc_x,#0xff\n");
				SetZN8("spc_x");
				break;

			case 0xFC: // INC Y
				ot("  mov   r0,spc_ya,lsr #8\n");
				ot("  add   r0,r0,#1\n");
				ot("  and   r0,r0,#0xff\n");
				SetZN8("r0");
				ot("  and   spc_ya,spc_ya,#0xff\n");
				otb("  orr   spc_ya,spc_ya,r0,lsl #8\n");
				break;

			case 0x1D: // DEC X
				ot("  sub   spc_x,spc_x,#1\n");
				ot("  and   spc_x,spc_x,#0xff\n");
				SetZN8("spc_x");
				break;

			case 0xDC: // DEC Y
				ot("  mov   r0,spc_ya,lsr #8\n");
				ot("  sub   r0,r0,#1\n");
				ot("  and   r0,r0,#0xff\n");
				SetZN8("r0");
				ot("  and   spc_ya,spc_ya,#0xff\n");
				otb("  orr   spc_ya,spc_ya,r0,lsl #8\n");
				break;

			case 0xAB: // INC dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				ot("  add   r0,r0,#1\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0xAC: // INC abs
				Absolute(0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				MemHandler(0, 0, 0);
				ot("  add   r0,r0,#1\n");
				SetZN8("r0");
				//ot("  ldmfd sp!,{r1}\n");
				ot("  mov   r1, r3\n");
				MemHandler(1, 0, 1);
				break;

			case 0xBB: // INC dp+X
				ot("  ldrb  r0,[spc_pc]\n");
				ot("  add   r0,r0,spc_x\n");
				MemHandler(0, 1, 0);
				ot("  add   r0,r0,#1\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  add   r1,r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0xBC: // INC A
				ot("  and   r0,spc_ya,#0xff\n");
				ot("  add   r0,r0,#1\n");
				SetZN8("r0");
				ot("  and   r0,r0,#0xff\n");
				ot("  mov   spc_ya,spc_ya,lsr #8\n");
				otb("  orr   spc_ya,r0,spc_ya,lsl #8\n");
				break;

			case 0x8B: // DEC dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				ot("  sub   r0,r0,#1\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x8C: // DEC abs
				Absolute(0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				MemHandler(0, 0, 0);
				ot("  sub   r0,r0,#1\n");
				SetZN8("r0");
				//ot("  ldmfd sp!,{r1}\n");
				ot("  mov   r1, r3\n");
				MemHandler(1, 0, 1);
				break;

			case 0x9B: // DEC dp+X
				ot("  ldrb  r0,[spc_pc]\n");
				ot("  add   r0,r0,spc_x\n");
				MemHandler(0, 1, 0);
				ot("  sub   r0,r0,#1\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");

				ot("  add   r1,r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0x9C: // DEC A
				ot("  and   r0,spc_ya,#0xff\n");
				ot("  sub   r0,r0,#1\n");
				SetZN8("r0");
				ot("  and   r0,r0,#0xff\n");
				ot("  mov   spc_ya,spc_ya,lsr #8\n");
				otb("  orr   spc_ya,r0,spc_ya,lsl #8\n");
				break;

			case 0x49: // EOR dp(dest), dp(src)
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				ot("  eor   r0,r0,r3\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x58: // EOR dp,#00
				ot("  ldrb  r0,[spc_pc,#1]\n");
				MemHandler(0, 1, 0);
				ot("  ldrb  r1,[spc_pc],#2\n");
				ot("  eor   r0,r0,r1\n");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc,#-1]\n");
				MemHandler(1, 1, 1);
				break;

			case 0x59: // EOR (X),(Y)
				ot("  mov   r0,spc_x\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				ot("  mov   r0,spc_ya,lsr #8\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				ot("  eor   r0,r0,r3\n");
				SetZN8("r0");
				ot("  mov   r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0x4B: // LSR dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				Lsr();
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x4C: // LSR abs
				Absolute(0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov   r3, r0\n");
				MemHandler(0, 0, 0);
				Lsr();
				//ot("  ldmfd sp!,{r1}\n");
				ot("  mov r1, r3\n");				
				MemHandler(1, 0, 1);
				break;

			case 0x5B: // LSR dp+X
				ot("  ldrb  r0,[spc_pc]\n");
				ot("  add   r0,r0,spc_x\n");
				MemHandler(0, 1, 0);
				Lsr();
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  add   r1,r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0x5C: // LSR A
				ot("  and   r0,spc_ya,#0xff\n");
				Lsr();
				ot("  mov   spc_ya,spc_ya,lsr #8\n");
				otb("  orr   spc_ya,r0,spc_ya,lsl #8\n");
				break;

			case 0x7D: // MOV A,X
				ot("  and   spc_ya,spc_ya,#0xff00\n");
				ot("  orr   spc_ya,spc_ya,spc_x\n");
				SetZN8("spc_ya");
				break;

			case 0xDD: // MOV A,Y
				ot("  and   spc_ya,spc_ya,#0xff00\n");
				ot("  orr   spc_ya,spc_ya,spc_ya,lsr #8\n");
				SetZN8("spc_ya");
				break;

			case 0x5D: // MOV X,A
				ot("  and   spc_x,spc_ya,#0xff\n");
				SetZN8("spc_x");
				break;

			case 0xFD: // MOV Y,A
				ot("  and   spc_ya,spc_ya,#0xff\n");
				ot("  orr   spc_ya,spc_ya,spc_ya,lsl #8\n");
				SetZN8("spc_ya");
				break;

			case 0x9D: // MOV X,SP
				ot("  mov   spc_x,spc_s\n");
				SetZN8("spc_x");
				break;

			case 0xBD: // SP,X
				otb("  mov   spc_s,spc_x\n");
				break;

			case 0x6B: // ROR dp
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				Ror();
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x6C: // ROR abs
				Absolute(0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov r3, r0\n");				
				MemHandler(0, 0, 0);
				Ror();
				ot("  mov r1, r3\n");				
				//ot("  ldmfd sp!,{r1}\n");
				MemHandler(1, 0, 1);
				break;

			case 0x7B: // ROR dp+X
				ot("  ldrb  r0,[spc_pc]\n");
				ot("  add   r0,r0,spc_x\n");
				MemHandler(0, 1, 0);
				Ror();
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  add   r1,r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0x7C: // ROR A
				ot("  and   r0,spc_ya,#0xff\n");
				Ror();
				ot("  mov   spc_ya,spc_ya,lsr #8\n");
				otb("  orr   spc_ya,r0,spc_ya,lsl #8\n");
				break;

			case 0x6E: // DBNZ dp,rel
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  sub   r0,r0,#1\n");
				ot("  tst   r0,r0\n");
				ot("  addeq spc_pc,spc_pc,#1\n");
				ot("  ldrnesb r2,[spc_pc],#1\n");
				ot("  addne spc_pc,spc_pc,r2\n");
				ot("  subne cycles,cycles,#%i\n", one_apu_cycle[current_cycles]*2);
				MemHandler(1, 1, 1);
				break;

			case 0xFE: // DBNZ Y,rel
				ot("  sub   spc_ya,spc_ya,#0x100\n");
				ot("  mov   spc_ya,spc_ya,lsl #16\n");
				ot("  mov   spc_ya,spc_ya,lsr #16\n");
				ot("  movs  r0,spc_ya,lsr #8\n");
				ot("  addeq spc_pc,spc_pc,#1\n");
				ot("  ldrnesb r0,[spc_pc],#1\n");
				ot("  addne spc_pc,spc_pc,r0\n");
				ot("  subne cycles,cycles,#%i\n", one_apu_cycle[current_cycles]*2);
				break;

			case 0x6F: // RET
				PopW();
				ot("  add   spc_pc,spc_ram,r0\n");
				break;

			case 0x7F: // RETI
				Pop("spc_p");
				ot("  and   r0,spc_p,#(flag_z|flag_n)\n");
				ot("  eor   r0,r0,#flag_z\n");
				ot("  orr   spc_p,spc_p,r0,lsl #24\n");
				ot("  tst   spc_p,#flag_d\n");
				ot("  addne r0,spc_ram,#0x100\n");
				ot("  moveq r0,spc_ram\n");
				ot("  str   r0,[context,#iapu_directpage]\n");
				PopW();
				ot("  add   spc_pc,spc_ram,r0\n");
				break;

			case 0x89: // ADC dp(dest), dp(src)
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov r3, r0\n");				
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				//ot("  mov r1, r3\n");
				Adc("r0", "r3");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x98: // ADC dp,#00
				ot("  ldrb  r0,[spc_pc,#1]\n");
				MemHandler(0, 1, 0);
				ot("  ldrb  r1,[spc_pc],#2\n");
				Adc("r0", "r1");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc,#-1]\n");
				MemHandler(1, 1, 1);
				break;

			case 0x99: // ADC (X),(Y)
				ot("  mov   r0,spc_x\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov r3, r0\n");				
				ot("  mov   r0,spc_ya,lsr #8\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				//ot("  mov r1, r3\n");				
				Adc("r0", "r3");
				SetZN8("r0");
				ot("  mov   r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0x8D: // MOV Y,#00		//-REVISAR
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  and   spc_ya,spc_ya,#0xff\n");
				ot("  orr   spc_ya,spc_ya,r0,lsl #8\n");
				SetZN8("r0");
				break;

			case 0x8F: // MOV dp,#00   //-REVISAR
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0x9E: // DIV YA,X
				ot("  tst   spc_x,spc_x @ div by 0?\n");
				ot("  orreq spc_ya,spc_ya,#0xff00\n");
				ot("  orreq spc_ya,spc_ya,#0x00ff\n");
				ot("  orreq spc_p,spc_p,#flag_o\n");
				ot("  beq   1002f\n");
				ot("  bic   spc_p,spc_p,#flag_o\n");

				// division algo from Cyclone (result in r3, remainder instead of divident)
				ot("@ Divide spc_ya by spc_x\n");
				ot("  mov r3,#0\n");
				ot("  mov r1,spc_x\n");
				ot("\n");

				//
				/*ot("@ Shift up divisor till it's just less than numerator\n");
				ot("divshift:\n");
				ot("  cmp r1,spc_ya,lsr #1\n");
				ot("  movls r1,r1,lsl #1\n");
				ot("  bcc divshift\n");
				ot("\n");*/

				//optimised version of code provided by William Blair
				ot("@ Shift up divisor till it's just less than numerator\n");
			    ot("cmp   spc_ya,r1,lsl #8\n");
			    ot("movge r1,r1,lsl #8\n");
			    ot("cmp   spc_ya,r1,lsl #4\n");
			    ot("movge r1,r1,lsl #4\n");
			    ot("cmp   spc_ya,r1,lsl #2\n");
			    ot("movge r1,r1,lsl #2\n");
			    ot("cmp   spc_ya,r1,lsl #1\n");
			    ot("movge r1,r1,lsl #1\n");

				ot("1001:\n");
				ot("  cmp spc_ya,r1\n");
				ot("  adc r3,r3,r3 ;@ Double r3 and add 1 if carry set\n");
				ot("  subcs spc_ya,spc_ya,r1\n");
				ot("  teq r1,spc_x\n");
				ot("  movne r1,r1,lsr #1\n");
				ot("  bne 1001b\n");
				ot("\n");

				ot("  and   spc_ya,spc_ya,#0xff\n");
				ot("  and   r3,r3,#0xff\n");
				ot("  orr   spc_ya,r3,spc_ya,lsl #8\n");

				ot("1002:\n");
				SetZN8("spc_ya");
				break;

			case 0x9F: // XCN A
				ot("  and   r0,spc_ya,#0xff\n");
				ot("  mov   r1,r0,lsl #28\n");
				ot("  orr   r0,r1,r0,lsl #20\n");
				ot("  and   spc_ya,spc_ya,#0xff00\n");
				ot("  orr   spc_ya,spc_ya,r0,lsr #24\n");
				SetZN8("spc_ya");
				break;

			case 0xA9: // SBC dp(dest), dp(src)
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov r3, r0\n");				
				ot("  ldrb  r0,[spc_pc]\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				//ot("  mov r1, r3\n");
				Sbc("r0", "r3");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0xB8: // SBC dp,#00
				ot("  ldrb  r0,[spc_pc,#1]\n");
				MemHandler(0, 1, 0);
				ot("  ldrb  r1,[spc_pc],#2\n");
				Sbc("r0", "r1");
				SetZN8("r0");
				ot("  ldrb  r1,[spc_pc,#-1]\n");
				MemHandler(1, 1, 1);
				break;

			case 0xB9: // SBC (X),(Y)
				ot("  mov   r0,spc_x\n");
				MemHandler(0, 1, 0);
				//ot("  stmfd sp!,{r0}\n");
				ot("  mov r3, r0\n");				
				ot("  mov   r0,spc_ya,lsr #8\n");
				MemHandler(0, 1, 0);
				//ot("  ldmfd sp!,{r1}\n");
				//ot("  mov r1, r3\n");
				Sbc("r0", "r3");
				SetZN8("r0");
				ot("  mov   r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0xAF: // MOV (X)+, A
				ot("  mov   r0,spc_ya\n");
				ot("  mov   r1,spc_x\n");
				MemHandler(1, 1, 1);
				ot("  add   spc_x,spc_x,#1\n");
				otb("  and   spc_x,spc_x,#0xff\n");
				break;

			case 0xBE: // DAS
				ot("  and   r0,spc_ya,#0xff\n");
				ot("  and   r1,spc_ya,#0x0f\n");
				ot("  cmp   r1,#9\n");
				ot("  subhi r0,r0,#6\n");
				ot("  tstls spc_p,#flag_h\n");
				ot("  subeq r0,r0,#6\n");
				ot("  cmp   r0,#0x9f\n");
				ot("  bhi   2001f\n");
				ot("  tst   spc_p,#flag_c\n");
				ot("  beq   2001f\n");
				ot("  orr   spc_p,spc_p,#flag_c\n");
				ot("  b     2002f\n");
				ot("2001:\n"); // tens
				ot("  sub   r0,r0,#0x60\n");
				ot("  bic   spc_p,spc_p,#flag_c\n");

				ot("2002:\n"); // end
				ot("  and   spc_ya,spc_ya,#0xff00\n");
				ot("  orr   spc_ya,spc_ya,r0\n");
				SetZN8("spc_ya");
				break;

			case 0xBF: // MOV A,(X)+
				ot("  mov   r0,spc_x\n");
				MemHandler(0, 1, 1);
				ot("  and   spc_ya,spc_ya,#0xff00\n");
				ot("  orr   spc_ya,spc_ya,r0\n");
				ot("  add   spc_x,spc_x,#1\n");
				ot("  and   spc_x,spc_x,#0xff\n");
				SetZN8("spc_ya");
				break;

			case 0xC0: // DI
				otb("  bic   spc_p,spc_p,#flag_i\n");
				break;

			case 0xA0: // EI
				otb("  orr   spc_p,spc_p,#flag_i\n");
				break;

			case 0xC4: // MOV dp,A
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  mov   r0,spc_ya\n");
				MemHandler(1, 1, 1);
				break;

			case 0xC5: // MOV abs,A
				Absolute(1);
				ot("  mov   r0,spc_ya\n");
				MemHandler(1, 0, 1);
				break;

			case 0xC6: // MOV (X),A
				ot("  mov   r0,spc_ya\n");
				ot("  mov   r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0xC7: // MOV (dp+X),A
				IndexedXIndirect(1);
				ot("  mov   r0,spc_ya\n");
				MemHandler(1, 0, 1);
				break;

			case 0xC9: // MOV abs,X
				Absolute(1);
				ot("  mov   r0,spc_x\n");
				MemHandler(1, 0, 1);
				break;

			case 0xCB: // MOV dp,Y
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  mov   r0,spc_ya,lsr #8\n");
				MemHandler(1, 1, 1);
				break;

			case 0xCC: // MOV abs,Y

				Absolute(1);
				ot("  mov   r0,spc_ya,lsr #8\n");
				MemHandler(1, 0, 1);
				break;

			case 0xCD: // MOV X,#00
				ot("  ldrb  spc_x,[spc_pc],#1\n");
				SetZN8("spc_x");
				break;

			case 0xCF: // MUL YA
				ot("  mov   r0,spc_ya,lsr #8\n");
				ot("  and   spc_ya,spc_ya,#0xff\n");
				ot("  mul   spc_ya,r0,spc_ya\n");
				SetZN16("spc_ya");
				break;

			case 0xD4: // MOV dp+X, A
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  mov   r0,spc_ya\n");
				ot("  add   r1,r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0xD5: // MOV abs+X,A
				AbsoluteX(1);
				ot("  mov   r0,spc_ya\n");
				MemHandler(1, 0, 1);
				break;

			case 0xD6: // MOV abs+Y,A
				AbsoluteY(1);
				ot("  mov   r0,spc_ya\n");
				MemHandler(1, 0, 1);
				break;

			case 0xD7: // MOV (dp)+Y,A
				IndirectIndexedY(1);
				ot("  mov   r0,spc_ya\n");
				MemHandler(1, 0, 1);
				break;

			case 0xD8: // MOV dp,X
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  mov   r0,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0xD9: // MOV dp+Y,X
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  mov   r0,spc_x\n");
				ot("  add   r1,r1,spc_ya,lsr #8\n");
				MemHandler(1, 1, 1);
				break;

			case 0xDB: // MOV dp+X,Y
				ot("  ldrb  r1,[spc_pc],#1\n");
				ot("  mov   r0,spc_ya,lsr #8\n");
				ot("  add   r1,r1,spc_x\n");
				MemHandler(1, 1, 1);
				break;

			case 0xDF: // DAA
				ot("  and   r0,spc_ya,#0xff\n");
				ot("  and   r1,spc_ya,#0x0f\n");
				ot("  cmp   r1,#9\n");
				ot("  addhi r0,r0,#6\n");
				ot("  bls   3001f\n");
				ot("  cmphi r0,#0xf0\n");
				ot("  orrhi spc_p,spc_p,#flag_c\n");
				ot("  b     3002f\n");
				ot("3001:\n"); // testHc
				ot("  tst   spc_p,#flag_h\n");
				ot("  addne r0,r0,#6\n");
				ot("  beq   3002f\n");
				ot("  cmp   r0,#0xf0\n");
				ot("  orrhi spc_p,spc_p,#flag_c\n");
				ot("3002:\n"); // test2
				ot("  tst   spc_p,#flag_c\n");
				ot("  addne r0,r0,#0x60\n");
				ot("  bne   3003f\n");
				ot("  cmp   r0,#0x9f\n");
				ot("  addhi r0,r0,#0x60\n");
				ot("  orrhi spc_p,spc_p,#flag_c\n");
				ot("  bicls spc_p,spc_p,#flag_c\n");
				ot("3003:\n"); // end
				ot("  and   spc_ya,spc_ya,#0xff00\n");
				ot("  orr   spc_ya,spc_ya,r0\n");
				SetZN8("spc_ya");
				break;

			case 0xE9: // MOV X, abs
				Absolute(0);
				MemHandler(0, 0, 1);
				ot("  mov   spc_x,r0\n");
				SetZN8("spc_x");
				break;

			case 0xEB: // MOV Y,dp
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 1);
				ot("  and   spc_ya,spc_ya,#0xff\n");
				ot("  orr   spc_ya,spc_ya,r0,lsl #8\n");
				SetZN8("r0");
				break;

			case 0xEC: // MOV Y,abs
				Absolute(0);
				MemHandler(0, 0, 1);
				ot("  and   spc_ya,spc_ya,#0xff\n");
				ot("  orr   spc_ya,spc_ya,r0,lsl #8\n");
				SetZN8("r0");
				break;

			case 0xF8: // MOV X,dp
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 1);
				ot("  mov   spc_x,r0\n");
				SetZN8("spc_x");
				break;

			case 0xF9: // MOV X,dp+Y
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  add   r0,r0,spc_ya,lsr #8\n");
				MemHandler(0, 1, 1);
				ot("  mov   spc_x,r0\n");
				SetZN8("spc_x");
				break;

			case 0xFA: // MOV dp(dest),dp(src)
				ot("  ldrb  r0,[spc_pc],#1\n");
				MemHandler(0, 1, 0);
				ot("  ldrb  r1,[spc_pc],#1\n");
				MemHandler(1, 1, 1);
				break;

			case 0xFB: // MOV Y,dp+X
				ot("  ldrb  r0,[spc_pc],#1\n");
				ot("  add   r0,r0,spc_x\n");
				MemHandler(0, 1, 1);
				ot("  and   spc_ya,spc_ya,#0xff\n");
				ot("  orr   spc_ya,spc_ya,r0,lsl #8\n");
				SetZN8("r0");
				break;
		}

		//tmp_epilogue();
		ot("  subs   cycles,cycles,#%i\n", S9xAPUCycles[opcode] * one_apu_cycle[current_cycles]);
		ot("  ldrgeb opcode,[spc_pc],#1\n");
		flush_buffer();		
		ot("  ldrge  pc,[opcodes,opcode,lsl #2]\n");
		ot("  b      spc700End\n");

		printf("\b\b");
	}


	ot("\n\n");

}


void printJumpTable(int apu_cycles) {
	int i;
	ot("@ -------------------------- Jump Table %i --------------------------\n", apu_cycles);
	ot("Spc700JumpTab_%i:\n", apu_cycles);

	for (i=0; i < 0x100; i++)
	{
		if ((i&7)==0) ot("  .long ");

		ot("Apu%02X_%i", i, apu_cycles);

		if ((i&7)==7) ot(" @ %02x\n",i-7);
		else if (i+1 < 0x100) ot(", ");
	}

}

int main()
{
	printf("\n  notaz's SPC700 Emulator v%s - Core Creator\n\n", VERSION);

	// Open the assembly file
	AsmFile=fopen("spc700a.s", "wt"); if (AsmFile==NULL) return 1;

	ot("@  notaz's SPC700 Emulator v%s - Assembler Output\n\n", VERSION);
	ot("@ (c) Copyright 2006 notaz, All rights reserved.\n\n");
	ot("@ Modified by bitrider 2010 - 2011\n\n");
	ot("@ this is a rewrite of spc700.cpp in ARM asm, inspired by other asm CPU cores like\n");
	ot("@ Cyclone and DrZ80. It is meant to be used in Snes9x emulator ports for ARM platforms.\n\n");
	ot("@ the code is released under Snes9x license. See spcgen.c or any other source file\n@ from Snes9x source tree.\n\n\n");

	PrintFramework();

	ot("	.align 	4\n");	

	for (current_cycles=0; current_cycles < (sizeof(one_apu_cycle) / sizeof(int)); current_cycles++) {
		printOpcodes(one_apu_cycle[current_cycles]);
		printJumpTable(one_apu_cycle[current_cycles]);
		}


	fclose(AsmFile); AsmFile=NULL;

	printf("Assembling...\n");
	// Assemble the file
	//system("as -marmv4t -mthumb-interwork -o spc700a.o spc700a.S");
	printf("Done!\n\n");

	return 0;
}