summaryrefslogtreecommitdiff
path: root/plugins/sid/sidplay-libs/libsidplay/src/mos6510/cycle_based/mos6510c.i
blob: 78d57ba6792806847616b526e93236031808d54a (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
/***************************************************************************
                          mos6510.i  -  Cycle Accurate 6510 emulation
                             -------------------
    begin                : Thu May 11 06:22:40 BST 2000
    copyright            : (C) 2000 by Simon White
    email                : s_a_white@email.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/***************************************************************************
 *  $Log: mos6510c.i,v $
 *  Revision 1.30  2002/12/16 08:42:58  s_a_white
 *  Fixed use of nothrow to be namespaced with std::.
 *
 *  Revision 1.29  2002/11/28 20:35:06  s_a_white
 *  Reduced number of thrown exceptions when dma occurs.
 *
 *  Revision 1.28  2002/11/25 20:10:55  s_a_white
 *  A bus access failure should stop the CPU dead like the cycle never started.
 *  This is currently simulated using throw (execption handling) for now.
 *
 *  Revision 1.27  2002/11/21 19:52:48  s_a_white
 *  CPU upgraded to be like other components.  Theres nolonger a clock call,
 *  instead events are registered to occur at a specific time.
 *
 *  Revision 1.26  2002/11/19 22:57:33  s_a_white
 *  Initial support for external DMA to steal cycles away from the CPU.
 *
 *  Revision 1.25  2002/11/01 19:22:36  s_a_white
 *  Removed debug printf.
 *
 *  Revision 1.24  2002/11/01 17:35:27  s_a_white
 *  Frame based support for old sidplay1 modes.
 *
 *  Revision 1.23  2002/03/12 18:48:03  s_a_white
 *  Tidied illegal instruction debug print out.
 *
 *  Revision 1.22  2001/12/11 19:24:15  s_a_white
 *  More GCC3 Fixes.
 *
 *  Revision 1.21  2001/11/16 19:21:03  s_a_white
 *  Sign fixes.
 *
 *  Revision 1.20  2001/10/28 21:31:26  s_a_white
 *  Removed kernel debuging code.
 *
 *  Revision 1.19  2001/09/03 22:21:52  s_a_white
 *  When initialising the status register and therefore unmasking the irqs,
 *  check the irq line to see if any are pending.
 *
 *  Revision 1.18  2001/08/10 20:05:50  s_a_white
 *  Fixed RMW instructions which broke due to the optimisation.
 *
 *  Revision 1.17  2001/08/05 15:46:02  s_a_white
 *  No longer need to check on which cycle an instruction ends or when to print
 *  debug information.
 *
 *  Revision 1.16  2001/07/14 13:15:30  s_a_white
 *  Accumulator is now unsigned, which improves code readability.  Emulation
 *  tested with testsuite 2.15.  Various instructions required modification.
 *
 *  Revision 1.15  2001/04/20 22:23:11  s_a_white
 *  Handling of page boundary crossing now correct for branch instructions.
 *
 *  Revision 1.14  2001/03/28 22:59:59  s_a_white
 *  Converted some bad envReadMemByte's to
 *  envReadMemDataByte
 *
 *  Revision 1.13  2001/03/28 21:17:34  s_a_white
 *  Added support for proper RMW instructions.
 *
 *  Revision 1.12  2001/03/24 18:09:17  s_a_white
 *  On entry to interrupt routine the first instruction in the handler is now always
 *  executed before pending interrupts are re-checked.
 *
 *  Revision 1.11  2001/03/22 22:40:43  s_a_white
 *  Added new header for definition of nothrow.
 *
 *  Revision 1.10  2001/03/21 22:27:18  s_a_white
 *  Change to IRQ error message.
 *
 *  Revision 1.9  2001/03/19 23:46:35  s_a_white
 *  NMI no longer sets I flag.  RTI and store instructions are no longer
 *  overlapped.
 *
 *  Revision 1.8  2001/03/09 22:28:51  s_a_white
 *  Speed optimisation update and fix for interrupt flag in PushSR call.
 *
 *  Revision 1.7  2001/02/22 08:28:57  s_a_white
 *  Interrupt masking fixed.
 *
 *  Revision 1.6  2001/02/13 23:01:44  s_a_white
 *  envReadMemDataByte now used for some memory accesses.
 *
 *  Revision 1.5  2000/12/24 00:45:38  s_a_white
 *  HAVE_EXCEPTIONS update
 *
 *  Revision 1.4  2000/12/14 23:55:07  s_a_white
 *  PushSR optimisation and PopSR code cleanup.
 *
 ***************************************************************************/
/*
const char _sidtune_CHRtab[256] =  // CHR$ conversion table (0x01 = no output)
{
   0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0xd, 0x1, 0x1,
   0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
  0x20,0x21, 0x1,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
  0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
  0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
  0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x24,0x5d,0x20,0x20,
  // alternative: CHR$(92=0x5c) => ISO Latin-1(0xa3)
  0x2d,0x23,0x7c,0x2d,0x2d,0x2d,0x2d,0x7c,0x7c,0x5c,0x5c,0x2f,0x5c,0x5c,0x2f,0x2f,
  0x5c,0x23,0x5f,0x23,0x7c,0x2f,0x58,0x4f,0x23,0x7c,0x23,0x2b,0x7c,0x7c,0x26,0x5c,
  // 0x80-0xFF
   0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
   0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
  0x20,0x7c,0x23,0x2d,0x2d,0x7c,0x23,0x7c,0x23,0x2f,0x7c,0x7c,0x2f,0x5c,0x5c,0x2d,
  0x2f,0x2d,0x2d,0x7c,0x7c,0x7c,0x7c,0x2d,0x2d,0x2d,0x2f,0x5c,0x5c,0x2f,0x2f,0x23,
  0x2d,0x23,0x7c,0x2d,0x2d,0x2d,0x2d,0x7c,0x7c,0x5c,0x5c,0x2f,0x5c,0x5c,0x2f,0x2f,
  0x5c,0x23,0x5f,0x23,0x7c,0x2f,0x58,0x4f,0x23,0x7c,0x23,0x2b,0x7c,0x7c,0x26,0x5c,
  0x20,0x7c,0x23,0x2d,0x2d,0x7c,0x23,0x7c,0x23,0x2f,0x7c,0x7c,0x2f,0x5c,0x5c,0x2d,
  0x2f,0x2d,0x2d,0x7c,0x7c,0x7c,0x7c,0x2d,0x2d,0x2d,0x2f,0x5c,0x5c,0x2f,0x2f,0x23
};
*/

#include "config.h"

#ifdef HAVE_EXCEPTIONS
#   include <new>
#endif

// Microsoft Visual C++ Version Number to work around compiler bug
// Currently both Visual C++ Versions 5, 6 are broken.
#define _MSC_VER_BAD_NEW 1200 /* Defines VC6 and below */
//char filetmp[0x100];
//int  filepos = 0;

//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Status Register Routines                                                //
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Use macros to access flags.  Allows compatiblity with other versions
// of this emulation
// Set N and Z flags according to byte
#define setFlagsNZ(x) (Register_z_Flag = (Register_n_Flag = (uint_least8_t) (x)))
#define setFlagN(x)   (Register_n_Flag = (uint_least8_t) (x))
#define setFlagV(x)   (Register_v_Flag = (uint_least8_t) (x))
#define setFlagD(x)   (Register_Status = (Register_Status & ~(1 << SR_DECIMAL)) \
                                       | (((x) != 0) << SR_DECIMAL))
#define setFlagI(x)   (Register_Status = (Register_Status & ~(1 << SR_INTERRUPT)) \
                                       | (((x) != 0) << SR_INTERRUPT))
#define setFlagZ(x)   (Register_z_Flag = (uint_least8_t) (x))
#define setFlagC(x)   (Register_c_Flag = (uint_least8_t) (x))


#define getFlagN()    ((Register_n_Flag &  (1 << SR_NEGATIVE))  != 0)
#define getFlagV()    (Register_v_Flag != 0)
#define getFlagD()    ((Register_Status  & (1 << SR_DECIMAL))   != 0)
#define getFlagI()    ((Register_Status  & (1 << SR_INTERRUPT)) != 0)
#define getFlagZ()    (Register_z_Flag == 0)
#define getFlagC()    (Register_c_Flag != 0)

#define stealCycle() \
    interrupts.delay++; \
    throw((int_least8_t) -1);


// Handle bus access signals
void MOS6510::aecSignal (bool state)
{   // If the cpu blocked waiting for the bus
    // the schedule a retry.
    aec = state;
    if (state && m_blocked)
    {
        m_blocked = false;
        eventContext.schedule (this, 1);
    }
}

void MOS6510::rdySignal (bool state)
{   // If the cpu blocked waiting for the bus
    // the schedule a retry.
    rdy = state;
    if (state && m_blocked)
    {
        m_blocked = false;
        eventContext.schedule (this, 1);
    }
}

// Push P on stack, decrement S
void MOS6510::PushSR (bool b_flag)
{
    if (aec)
    {
        uint_least16_t addr = Register_StackPointer;
        endian_16hi8 (addr, SP_PAGE);
        /* Rev 1.04 - Corrected flag mask */
        Register_Status &= ((1 << SR_NOTUSED) | (1 << SR_INTERRUPT) |
                            (1 << SR_DECIMAL) | (1 << SR_BREAK));
        Register_Status |= (getFlagN () << SR_NEGATIVE);
        Register_Status |= (getFlagV () << SR_OVERFLOW);
        Register_Status |= (getFlagZ () << SR_ZERO);
        Register_Status |= (getFlagC () << SR_CARRY);
        envWriteMemByte (addr, Register_Status & ~((!b_flag) << SR_BREAK));
        Register_StackPointer--;
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

void MOS6510::PushSR (void)
{
    PushSR (true);
}

// increment S, Pop P off stack
void MOS6510::PopSR (void)
{
    if (rdy && aec)
    {
        bool newFlagI, oldFlagI;
        oldFlagI = getFlagI ();

        // Get status register off stack
        Register_StackPointer++;
        {
            uint_least16_t addr = Register_StackPointer;
            endian_16hi8 (addr, SP_PAGE);
            Register_Status = envReadMemByte (addr);
        }
        Register_Status |= ((1 << SR_NOTUSED) | (1 << SR_BREAK));
        setFlagN (Register_Status);
        setFlagV (Register_Status   & (1 << SR_OVERFLOW));
        setFlagZ (!(Register_Status & (1 << SR_ZERO)));
        setFlagC (Register_Status   & (1 << SR_CARRY));

        // I flag change is delayed by 1 instruction
        newFlagI = getFlagI ();
        interrupts.irqLatch = oldFlagI ^ newFlagI;
        // Check to see if interrupts got re-enabled
        if (!newFlagI && interrupts.irqs)
            interrupts.irqRequest = true;
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}


//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Interrupt Routines                                                      //
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
#define iIRQSMAX 3
enum
{
    oNONE = -1,
    oRST,
    oNMI,
    oIRQ
};

enum
{
    iNONE = 0,
    iRST  = 1 << oRST,
    iNMI  = 1 << oNMI,
    iIRQ  = 1 << oIRQ
};

void MOS6510::triggerRST (void)
{
    interrupts.pending |= iRST;
}

void MOS6510::triggerNMI (void)
{
    interrupts.pending |= iNMI;
    interrupts.nmiClock = eventContext.getTime ();
}

// Level triggered interrupt
void MOS6510::triggerIRQ (void)
{   // IRQ Suppressed
    if (!getFlagI ())
        interrupts.irqRequest = true;
    if (!interrupts.irqs++)
        interrupts.irqClock = eventContext.getTime ();

    if (interrupts.irqs > iIRQSMAX)
    {
        printf ("\nMOS6510 ERROR: An external component is not clearing down it's IRQs.\n\n");
        exit (-1);
    }
}

void MOS6510::clearIRQ (void)
{
    if (interrupts.irqs > 0)
    {   
        if (!(--interrupts.irqs))
        {   // Clear off the interrupts
            interrupts.irqRequest = false;
        }
    }
}

bool MOS6510::interruptPending (void)
{
    int_least8_t offset, pending;
    static const int_least8_t offTable[] = {oNONE, oRST, oNMI, oRST,
                                            oIRQ,  oRST, oNMI, oRST};
    // Update IRQ pending
    if (!interrupts.irqLatch)
    {
        interrupts.pending &= ~iIRQ;
        if (interrupts.irqRequest)
            interrupts.pending |= iIRQ;
    }

    pending = interrupts.pending;
MOS6510_interruptPending_check:
    // Service the highest priority interrupt
    offset = offTable[pending];
    switch (offset)
    {
    case oNONE:
        return false;

    case oNMI:
    {
        // Try to determine if we should be processing the NMI yet
        event_clock_t cycles = eventContext.getTime (interrupts.nmiClock);
        if (cycles >= interrupts.delay)
        {
            interrupts.pending &= ~iNMI;
            break;
        }

        // NMI delayed so check for other interrupts
        pending &= ~iNMI;
        goto MOS6510_interruptPending_check;
    }

    case oIRQ:
    {
        // Try to determine if we should be processing the IRQ yet
        event_clock_t cycles = eventContext.getTime (interrupts.irqClock);
        if (cycles >= interrupts.delay)
            break;

        // NMI delayed so check for other interrupts
        pending &= ~iIRQ;
        goto MOS6510_interruptPending_check;
    }

    case oRST:
        break;
    }

#ifdef MOS6510_DEBUG
    if (dodump)
    {
    printf ("****************************************************\n");
    switch (offset)
    {
    case oIRQ:
        printf (" IRQ Routine\n");
    break;
    case oNMI:
        printf (" NMI Routine\n");
    break;
    case oRST:
        printf (" RST Routine\n");
    break;
    }
    printf ("****************************************************\n");
    }
#endif

    // Start the interrupt
    instrCurrent = &interruptTable[offset];
    procCycle    = instrCurrent->cycle;
    cycleCount   = 0;
    return true;
}

void MOS6510::RSTRequest (void)
{
    envReset ();
}

void MOS6510::NMIRequest (void)
{
    if (rdy && aec)
        endian_16lo8 (Cycle_EffectiveAddress, envReadMemDataByte (0xFFFA));
    else
    {   // Address bus not ready
        stealCycle();
    }
}

void MOS6510::NMI1Request (void)
{
    if (rdy && aec)
    {
        endian_16hi8  (Cycle_EffectiveAddress, envReadMemDataByte (0xFFFB));
        endian_32lo16 (Register_ProgramCounter, Cycle_EffectiveAddress);
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

void MOS6510::IRQRequest (void)
{
    PushSR   (false);
    setFlagI (true);
    interrupts.irqRequest = false;
}

void MOS6510::IRQ1Request (void)
{
    if (rdy && aec)
        endian_16lo8 (Cycle_EffectiveAddress, envReadMemDataByte (0xFFFE));
    else
    {   // Address bus not ready
        stealCycle();
    }
}

void MOS6510::IRQ2Request (void)
{
    if (rdy && aec)
    {
        endian_16hi8  (Cycle_EffectiveAddress, envReadMemDataByte (0xFFFF));
        endian_32lo16 (Register_ProgramCounter, Cycle_EffectiveAddress);
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

void MOS6510::NextInstr (void)
{
    if (!interruptPending ())
        FetchOpcode ();
}


//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Common Instruction Addressing Routines                                  //
// Addressing operations as described in 64doc by John West and            //
// Marko Makela                                                            //
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//

// Fetch opcode, increment PC
// Addressing Modes: All
void MOS6510::FetchOpcode (void)
{
    if (rdy && aec)
    {   // On new instruction all interrupt delays are reset
        interrupts.delay    = MOS6510_INTERRUPT_DELAY;
        interrupts.irqLatch = false;

        instrStartPC  = endian_32lo16 (Register_ProgramCounter++);
        instrOpcode   = envReadMemByte (instrStartPC);
        // Convert opcode to pointer in instruction table
        instrCurrent  = &instrTable[instrOpcode];
        Instr_Operand = 0;
        procCycle     = instrCurrent->cycle;
        cycleCount    = 0;
        clock ();
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Fetch value, increment PC
/* Addressing Modes:    Immediate
                        Relative
*/
void MOS6510::FetchDataByte (void)
{   // Get data byte from memory
    Cycle_Data = envReadMemByte (endian_32lo16 (Register_ProgramCounter));
    Register_ProgramCounter++;

    // Nextline used for Debug
    Instr_Operand = (uint_least16_t) Cycle_Data;
}

// Fetch low address byte, increment PC
/* Addressing Modes:    Stack Manipulation
                        Absolute
                        Zero Page
                        Zerp Page Indexed
                        Absolute Indexed
                        Absolute Indirect
*/                      
void MOS6510::FetchLowAddr (void)
{
    if (rdy && aec)
    {
        Cycle_EffectiveAddress = envReadMemByte (endian_32lo16 (Register_ProgramCounter));
        Register_ProgramCounter++;

        // Nextline used for Debug
        Instr_Operand = Cycle_EffectiveAddress;
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Read from address, add index register X to it
// Addressing Modes:    Zero Page Indexed
void MOS6510::FetchLowAddrX (void)
{
    FetchLowAddr ();
    Cycle_EffectiveAddress = (Cycle_EffectiveAddress + Register_X) & 0xFF;
}

// Read from address, add index register Y to it
// Addressing Modes:    Zero Page Indexed
void MOS6510::FetchLowAddrY (void)
{
    FetchLowAddr ();
    Cycle_EffectiveAddress = (Cycle_EffectiveAddress + Register_Y) & 0xFF;
}

// Fetch high address byte, increment PC (Absoulte Addressing)
// Low byte must have been obtained first!
// Addressing Modes:    Absolute
void MOS6510::FetchHighAddr (void)
{
    if (rdy && aec)
    {   // Get the high byte of an address from memory
        endian_16hi8 (Cycle_EffectiveAddress, envReadMemByte (endian_32lo16 (Register_ProgramCounter)));
        Register_ProgramCounter++;

        // Nextline used for Debug
        endian_16hi8 (Instr_Operand, endian_16hi8 (Cycle_EffectiveAddress));
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Fetch high byte of address, add index register X to low address byte,
// increment PC
// Addressing Modes:    Absolute Indexed
void MOS6510::FetchHighAddrX (void)
{
    uint8_t page;
    // Rev 1.05 (saw) - Call base Function
    FetchHighAddr ();
    page = endian_16hi8 (Cycle_EffectiveAddress);
    Cycle_EffectiveAddress += Register_X;

#ifdef MOS6510_ACCURATE_CYCLES
    // Handle page boundary crossing
    if (endian_16hi8 (Cycle_EffectiveAddress) == page)
        cycleCount++;
#endif
}

// Same as above except dosen't worry about page crossing
void MOS6510::FetchHighAddrX2 (void)
{
    FetchHighAddr ();
    Cycle_EffectiveAddress += Register_X;
}

// Fetch high byte of address, add index register Y to low address byte,
// increment PC
// Addressing Modes:    Absolute Indexed
void MOS6510::FetchHighAddrY (void)
{
    uint8_t page;
    // Rev 1.05 (saw) - Call base Function
    FetchHighAddr ();
    page = endian_16hi8 (Cycle_EffectiveAddress);
    Cycle_EffectiveAddress += Register_Y;

#ifdef MOS6510_ACCURATE_CYCLES
    // Handle page boundary crossing
    if (endian_16hi8 (Cycle_EffectiveAddress) == page)
        cycleCount++;
#endif
}

// Same as above except dosen't worry about page crossing
void MOS6510::FetchHighAddrY2 (void)
{
    FetchHighAddr ();
    Cycle_EffectiveAddress += Register_Y;
}

// Fetch pointer address low, increment PC
/* Addressing Modes:    Absolute Indirect
                        Indirect indexed (post Y)
*/
void MOS6510::FetchLowPointer (void)
{
    if (rdy && aec)
    {
        Cycle_Pointer = envReadMemByte (endian_32lo16 (Register_ProgramCounter));
        Register_ProgramCounter++;
        // Nextline used for Debug
        Instr_Operand = Cycle_Pointer;
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Read pointer from the address and add X to it
// Addressing Modes:    Indexed Indirect (pre X)
void MOS6510::FetchLowPointerX (void)
{
    if (rdy && aec)
    {
        endian_16hi8 (Cycle_Pointer, envReadMemDataByte (Cycle_Pointer));
        // Page boundary crossing is not handled
        Cycle_Pointer = (Cycle_Pointer + Register_X) & 0xFF;
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Fetch pointer address high, increment PC
// Addressing Modes:    Absolute Indirect
void MOS6510::FetchHighPointer (void)
{
    if (rdy && aec)
    {
        endian_16hi8 (Cycle_Pointer, envReadMemByte (endian_32lo16 (Register_ProgramCounter)));
        Register_ProgramCounter++;

        // Nextline used for Debug
        endian_16hi8 (Instr_Operand, endian_16hi8 (Cycle_Pointer));
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Fetch effective address low
/* Addressing Modes:    Indirect
                        Indexed Indirect (pre X)
                        Indirect indexed (post Y)
*/
void MOS6510::FetchLowEffAddr (void)
{
    if (rdy && aec)
        Cycle_EffectiveAddress = envReadMemDataByte (Cycle_Pointer);
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Fetch effective address high
/* Addressing Modes:    Indirect
                        Indexed Indirect (pre X)
*/
void MOS6510::FetchHighEffAddr (void)
{
    if (rdy && aec)
    {   // Rev 1.03 (Mike) - Extra +1 removed
        endian_16lo8 (Cycle_Pointer, (Cycle_Pointer + 1) & 0xff);
        endian_16hi8 (Cycle_EffectiveAddress, envReadMemDataByte (Cycle_Pointer));
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Fetch effective address high, add Y to low byte of effective address
// Addressing Modes:    Indirect indexed (post Y)
void MOS6510::FetchHighEffAddrY (void)
{
    uint8_t page;
    // Rev 1.05 (saw) - Call base Function
    FetchHighEffAddr ();
    page = endian_16hi8 (Cycle_EffectiveAddress);
    Cycle_EffectiveAddress += Register_Y;

#ifdef MOS6510_ACCURATE_CYCLES
    // Handle page boundary crossing
    if (endian_16hi8 (Cycle_EffectiveAddress) == page)
        cycleCount++;
#endif
}

// Same as above except dosen't worry about page crossing
void MOS6510::FetchHighEffAddrY2 (void)
{
    FetchHighEffAddr ();
    Cycle_EffectiveAddress += Register_Y;
}

//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Common Data Accessing Routines                                          //
// Data Accessing operations as described in 64doc by John West and        //
// Marko Makela                                                            //
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//

void MOS6510::FetchEffAddrDataByte (void)
{
    if (rdy && aec)
        Cycle_Data = envReadMemDataByte (Cycle_EffectiveAddress);
    else
    {   // Address bus not ready
        stealCycle();
    }
}

void MOS6510::PutEffAddrDataByte (void)
{
    if (aec)
        envWriteMemByte (Cycle_EffectiveAddress, Cycle_Data);
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Used for Read Modify Write (RMW) instructions
void MOS6510::FetchPutEffAddrDataByte (void)
{
    FetchEffAddrDataByte ();
    PutEffAddrDataByte ();
}

// Push Program Counter Low Byte on stack, decrement S
void MOS6510::PushLowPC (void)
{
    if (aec)
    {
        uint_least16_t addr;
        addr = Register_StackPointer;
        endian_16hi8 (addr, SP_PAGE);
        envWriteMemByte (addr, endian_32lo8 (Register_ProgramCounter));
        Register_StackPointer--;
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Push Program Counter High Byte on stack, decrement S
void MOS6510::PushHighPC (void)
{
    if (aec)
    {
        uint_least16_t addr;
        addr = Register_StackPointer;
        endian_16hi8 (addr, SP_PAGE);
        envWriteMemByte (addr, endian_32hi8 (Register_ProgramCounter));
        Register_StackPointer--;
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Increment stack and pull program counter low byte from stack,
void MOS6510::PopLowPC (void)
{
    if (rdy && aec)
    {
        uint_least16_t addr;
        Register_StackPointer++;
        addr = Register_StackPointer;
        endian_16hi8 (addr, SP_PAGE);
        endian_16lo8 (Cycle_EffectiveAddress, envReadMemDataByte (addr));
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

// Increment stack and pull program counter high byte from stack,
void MOS6510::PopHighPC (void)
{
    if (rdy && aec)
    {
        uint_least16_t addr;
        Register_StackPointer++;
        addr = Register_StackPointer;
        endian_16hi8 (addr, SP_PAGE);
        endian_16hi8 (Cycle_EffectiveAddress, envReadMemDataByte (addr));
    }
    else
    {   // Address bus not ready
        stealCycle();
    }
}

void MOS6510::WasteCycle (void)
{
}

void MOS6510::DebugCycle (void)
{
    if (dodump)
        DumpState ();
    clock ();
}


//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Common Instruction Opcodes                                              //
// See and 6510 Assembly Book for more information on these instructions   //
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//

void MOS6510::brk_instr (void)
{
    PushSR   ();
    setFlagI (true);
    interrupts.irqRequest = false;

    // Check for an NMI, and switch over if pending
    if (interrupts.pending & iNMI)
    {
        event_clock_t cycles = eventContext.getTime (interrupts.nmiClock);
        if (cycles >= interrupts.delay)
        {
            interrupts.pending &= ~iNMI;
            instrCurrent = &interruptTable[oNMI];
            procCycle    = &instrCurrent->cycle[cycleCount];
        }
    }
}

void MOS6510::cld_instr (void)
{
    setFlagD (false);
}

void MOS6510::cli_instr (void)
{
    bool oldFlagI = getFlagI ();
    setFlagI (false);
    // I flag change is delayed by 1 instruction
    interrupts.irqLatch = oldFlagI ^ getFlagI ();
    // Check to see if interrupts got re-enabled
    if (interrupts.irqs)
        interrupts.irqRequest = true;
}

void MOS6510::jmp_instr (void)
{
    endian_32lo16 (Register_ProgramCounter, Cycle_EffectiveAddress);
}

void MOS6510::jsr_instr (void)
{   // JSR uses absolute addressing in this emulation,
    // hence the -1.  The real SID does not use this addressing
    // mode.
    Register_ProgramCounter--;
    PushHighPC ();
}

void MOS6510::pha_instr (void)
{
    if (aec)
    {
        uint_least16_t addr;
        addr = Register_StackPointer;
        endian_16hi8 (addr, SP_PAGE);
        envWriteMemByte (addr, Register_Accumulator);
        Register_StackPointer--;
    }
    else
    {   // Address bus not ready
        cycleCount--;
        return;
    }
}

/* RTI does not delay the IRQ I flag change as it is set 3 cycles before
 * the end of the opcode, and thus the 6510 has enough time to call the
 * interrupt routine as soon as the opcode ends, if necessary. */
void MOS6510::rti_instr (void)
{
#ifdef MOS6510_DEBUG
    if (dodump)
        printf ("****************************************************\n\n");
#endif

    endian_32lo16 (Register_ProgramCounter, Cycle_EffectiveAddress);
    interrupts.irqLatch = false;
}

void MOS6510::rts_instr (void)
{
/*
    // Hack - Output character to screen
    if (Register_ProgramCounter == 0xffd3)
    {
        char ch = _sidtune_CHRtab[Register_Accumulator];
        switch (ch)
        {
        case 0:
            break;
        case 1:
            printf (" ");
            fprintf (stderr, " ");
        case 0xd:
            printf ("\n");
            fprintf (stderr, "\n");
            filepos = 0;
            break;
        default:
            filetmp[filepos++] = ch;
            printf ("%c", ch);
            fprintf (stderr, "%c", ch);
        }
    }

    if (Register_ProgramCounter == 0xe170)
    {
        filetmp[filepos] = '\0';
        envLoadFile (filetmp);
    }
*/
    endian_32lo16 (Register_ProgramCounter, Cycle_EffectiveAddress);
    Register_ProgramCounter++;
}

void MOS6510::sed_instr (void)
{
    setFlagD (true);
}

void MOS6510::sei_instr (void)
{
    bool oldFlagI = getFlagI ();
    setFlagI (true);
    // I flag change is delayed by 1 instruction
    interrupts.irqLatch   = oldFlagI ^ getFlagI ();
    interrupts.irqRequest = false;
}

void MOS6510::sta_instr (void)
{
    Cycle_Data = Register_Accumulator;
    PutEffAddrDataByte ();
}

void MOS6510::stx_instr (void)
{
    Cycle_Data = Register_X;
    PutEffAddrDataByte ();
}

void MOS6510::sty_instr (void)
{
    Cycle_Data = Register_Y;
    PutEffAddrDataByte ();
}



//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Common Instruction Undocumented Opcodes                                 //
// See documented 6502-nmo.opc by Adam Vardy for more details              //
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//

// Undocumented - This opcode stores the result of A AND X AND the high
// byte of the target address of the operand +1 in memory.
void MOS6510::axa_instr (void)
{
    Cycle_Data = Register_X & Register_Accumulator & (endian_16hi8 (Cycle_EffectiveAddress) + 1);
    PutEffAddrDataByte ();
}

// Undocumented - AXS ANDs the contents of the A and X registers (without changing the
// contents of either register) and stores the result in memory.
// AXS does not affect any flags in the processor status register.
void MOS6510::axs_instr (void)
{
    Cycle_Data = Register_Accumulator & Register_X;
}

/* Not required - Operation performed By another method
// Undocumented - HLT crashes the microprocessor.  When this opcode is executed, program
// execution ceases.  No hardware interrupts will execute either.  The author
// has characterized this instruction as a halt instruction since this is the
// most straightforward explanation for this opcode's behaviour.  Only a reset
// will restart execution.  This opcode leaves no trace of any operation
// performed!  No registers affected.
void MOS6510::hlt_instr (void)
{
}
*/

/* Not required - Operation performed By another method
void MOS6510::nop_instr (void)
{
}
*/

/* Not required - Operation performed By another method
void MOS6510::php_instr (void)
{
}
*/

// Undocumented - This opcode ANDs the contents of the Y register with <ab+1> and stores the
// result in memory.
void MOS6510::say_instr (void)
{
    Cycle_Data = Register_Y & (endian_16hi8 (Cycle_EffectiveAddress) + 1);
}

/* Not required - Operation performed By another method
// Undocumented - skip next byte.
void MOS6510::skb_instr (void)
{
    Register_ProgramCounter++;
}
*/

/* Not required - Operation performed By another method
// Undocumented - skip next word.
void MOS6510::skw_instr (void)
{
    Register_ProgramCounter += 2;
}
*/

// Undocumented - This opcode ANDs the contents of the X register with <ab+1> and stores the
// result in memory.
void MOS6510::xas_instr (void)
{
    Cycle_Data = Register_X & (endian_16hi8 (Cycle_EffectiveAddress) + 1);
}


#ifdef X86
#include "MOS6510\CYCLE_~1\X86.CPP"
//#include "MOS6510\CYCLE_BASED\X86.CPP"
#else

//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Generic Binary Coded Decimal Correction                                 //
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//

void MOS6510::Perform_ADC (void)
{
    uint C      = getFlagC ();
    uint A      = Register_Accumulator;
    uint s      = Cycle_Data;
    uint regAC2 = A + s + C;

    if (getFlagD ())
    {   // BCD mode
        uint lo = (A & 0x0f) + (s & 0x0f) + C;
        uint hi = (A & 0xf0) + (s & 0xf0);
        if (lo > 0x09) lo += 0x06;
        if (lo > 0x0f) hi += 0x10;

        setFlagZ (regAC2);
        setFlagN (hi);
        setFlagV (((hi ^ A) & 0x80) && !((A ^ s) & 0x80));
        if (hi > 0x90) hi += 0x60;

        setFlagC (hi > 0xff);
        Register_Accumulator = (hi | (lo & 0x0f));
    }
    else
    {   // Binary mode
        setFlagC   (regAC2 > 0xff);
        setFlagV   (((regAC2 ^ A) & 0x80) && !((A ^ s) & 0x80));
        setFlagsNZ (Register_Accumulator = regAC2 & 0xff);
    }
}

void MOS6510::Perform_SBC (void)
{
    uint C      = !getFlagC ();
    uint A      = Register_Accumulator;
    uint s      = Cycle_Data;
    uint regAC2 = A - s - C;

    setFlagC   (regAC2 < 0x100);
    setFlagV   (((regAC2 ^ A) & 0x80) && ((A ^ s) & 0x80));
    setFlagsNZ (regAC2);

    if (getFlagD ())
    {   // BCD mode
        uint lo = (A & 0x0f) - (s & 0x0f) - C;
        uint hi = (A & 0xf0) - (s & 0xf0);
        if (lo & 0x10)
        {
             lo -= 0x06;
             hi -= 0x10;
        }
        if (hi & 0x100) hi -= 0x60;
        Register_Accumulator = (hi | (lo & 0x0f));
    }
    else
    {   // Binary mode
        Register_Accumulator = regAC2 & 0xff;
    }
}



//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Generic Instruction Addressing Routines                                 //
//-------------------------------------------------------------------------/


//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Generic Instruction Opcodes                                             //
// See and 6510 Assembly Book for more information on these instructions   //
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//

void MOS6510::adc_instr (void)
{
    Perform_ADC ();
}

void MOS6510::and_instr (void)
{
    setFlagsNZ (Register_Accumulator &= Cycle_Data);
}

void MOS6510::ane_instr (void)
{
    setFlagsNZ (Register_Accumulator = (Register_Accumulator | 0xee) & Register_X & Cycle_Data);
}

void MOS6510::asl_instr (void)
{
    setFlagC   (Cycle_Data & 0x80);
    setFlagsNZ (Cycle_Data <<= 1);
}

void MOS6510::asla_instr (void)
{
    setFlagC   (Register_Accumulator & 0x80);
    setFlagsNZ (Register_Accumulator <<= 1);
}

void MOS6510::branch_instr (bool condition)
{
    if (condition)
#ifdef MOS6510_ACCURATE_CYCLES
    {
        uint8_t page;
        page = endian_32hi8 (Register_ProgramCounter);
        Register_ProgramCounter += (int8_t) Cycle_Data;

        // Handle page boundary crossing
        if (endian_32hi8 (Register_ProgramCounter) == page)
        {
            cycleCount++;
            interrupts.delay++;
        }
    }
    else
    {
        cycleCount += 2;
    }
#else
    Register_ProgramCounter += (int8_t) Cycle_Data;
#endif
}

void MOS6510::bcc_instr (void)
{
    branch_instr (!getFlagC ());
}

void MOS6510::bcs_instr (void)
{
    branch_instr (getFlagC ());
}

void MOS6510::beq_instr (void)
{
    branch_instr (getFlagZ ());
}

void MOS6510::bit_instr (void)
{
    setFlagZ (Register_Accumulator & Cycle_Data);
    setFlagN (Cycle_Data);
    setFlagV (Cycle_Data & 0x40);
}

void MOS6510::bmi_instr (void)
{
    branch_instr (getFlagN ());
}

void MOS6510::bne_instr (void)
{
    branch_instr (!getFlagZ ());
}

void MOS6510::bpl_instr(void)
{
    branch_instr (!getFlagN ());
}

void MOS6510::bvc_instr (void)
{
    branch_instr (!getFlagV ());
}

void MOS6510::bvs_instr (void)
{
    branch_instr (getFlagV ());
}

void MOS6510::clc_instr (void)
{
    setFlagC (false);
}

void MOS6510::clv_instr (void)
{
    setFlagV (false);
}

void MOS6510::cmp_instr (void)
{
    uint_least16_t tmp = (uint_least16_t) Register_Accumulator - Cycle_Data;
    setFlagsNZ (tmp);
    setFlagC   (tmp < 0x100);
}

void MOS6510::cpx_instr (void)
{
    uint_least16_t tmp = (uint_least16_t) Register_X - Cycle_Data;
    setFlagsNZ (tmp);
    setFlagC   (tmp < 0x100);
}

void MOS6510::cpy_instr (void)
{
    uint_least16_t tmp = (uint_least16_t) Register_Y - Cycle_Data;
    setFlagsNZ (tmp);
    setFlagC   (tmp < 0x100);
}

void MOS6510::dec_instr (void)
{
    setFlagsNZ (--Cycle_Data);
}

void MOS6510::dex_instr (void)
{
    setFlagsNZ (--Register_X);
}

void MOS6510::dey_instr (void)
{
    setFlagsNZ (--Register_Y);
}

void MOS6510::eor_instr (void)
{
    setFlagsNZ (Register_Accumulator^= Cycle_Data);
}

void MOS6510::inc_instr (void)
{
    setFlagsNZ (++Cycle_Data);
}

void MOS6510::inx_instr (void)
{
    setFlagsNZ (++Register_X);
}

void MOS6510::iny_instr (void)
{
    setFlagsNZ (++Register_Y);
}

void MOS6510::lda_instr (void)
{
    setFlagsNZ (Register_Accumulator = Cycle_Data);
}

void MOS6510::ldx_instr (void)
{
    setFlagsNZ (Register_X = Cycle_Data);
}

void MOS6510::ldy_instr (void)
{
    setFlagsNZ (Register_Y = Cycle_Data);
}

void MOS6510::lsr_instr (void)
{
    setFlagC   (Cycle_Data & 0x01);
    setFlagsNZ (Cycle_Data >>= 1);
}

void MOS6510::lsra_instr (void)
{
    setFlagC   (Register_Accumulator & 0x01);
    setFlagsNZ (Register_Accumulator >>= 1);
}

void MOS6510::ora_instr (void)
{
    setFlagsNZ (Register_Accumulator |= Cycle_Data);
}

void MOS6510::pla_instr (void)
{
    if (rdy && aec)
    {
        uint_least16_t addr;
        Register_StackPointer++;
        addr = Register_StackPointer;
        endian_16hi8 (addr, SP_PAGE);
        setFlagsNZ (Register_Accumulator = envReadMemByte (addr));
    }
    else
    {   // Address bus not ready
        cycleCount--;
        return;
    }
}

void MOS6510::rol_instr (void)
{
    uint8_t tmp = Cycle_Data & 0x80;
    Cycle_Data   <<= 1;
    if (getFlagC ()) Cycle_Data |= 0x01;
    setFlagsNZ (Cycle_Data);
    setFlagC   (tmp);
}

void MOS6510::rola_instr (void)
{
    uint8_t tmp = Register_Accumulator & 0x80;
    Register_Accumulator <<= 1;
    if (getFlagC ()) Register_Accumulator |= 0x01;
    setFlagsNZ (Register_Accumulator);
    setFlagC   (tmp);
}

void MOS6510::ror_instr (void)
{
    uint8_t tmp  = Cycle_Data & 0x01;
    Cycle_Data >>= 1;
    if (getFlagC ()) Cycle_Data |= 0x80;
    setFlagsNZ (Cycle_Data);
    setFlagC   (tmp);
}

void MOS6510::rora_instr (void)
{
    uint8_t tmp = Register_Accumulator & 0x01;
    Register_Accumulator >>= 1;
    if (getFlagC ()) Register_Accumulator |= 0x80;
    setFlagsNZ (Register_Accumulator);
    setFlagC   (tmp);
}

void MOS6510::sbx_instr (void)
{
    uint tmp = (Register_X & Register_Accumulator) - Cycle_Data;
    setFlagsNZ (Register_X = tmp & 0xff);
    setFlagC   (tmp < 0x100);
}

void MOS6510::sbc_instr (void)
{
    Perform_SBC ();
}

void MOS6510::sec_instr (void)
{
    setFlagC (true);
}

void MOS6510::shs_instr (void)
{
    endian_16lo8 (Register_StackPointer, (Register_Accumulator & Register_X));
    Cycle_Data = (endian_16hi8 (Cycle_EffectiveAddress) + 1) & Register_StackPointer;
}

void MOS6510::tax_instr (void)
{
    setFlagsNZ (Register_X = Register_Accumulator);
}

void MOS6510::tay_instr (void)
{
    setFlagsNZ (Register_Y = Register_Accumulator);
}

void MOS6510::tsx_instr (void)
{   // Rev 1.03 (saw) - Got these tsx and txs reversed
    setFlagsNZ (Register_X = endian_16lo8 (Register_StackPointer));
}

void MOS6510::txa_instr (void)
{
    setFlagsNZ (Register_Accumulator = Register_X);
}

void MOS6510::txs_instr (void)
{   // Rev 1.03 (saw) - Got these tsx and txs reversed
    endian_16lo8 (Register_StackPointer, Register_X);
}

void MOS6510::tya_instr (void)
{
    setFlagsNZ (Register_Accumulator = Register_Y);
}

void MOS6510::illegal_instr (void)
{
    printf ("\n\nILLEGAL INSTRUCTION, resetting emulation. **************\n");
    DumpState ();
    printf ("********************************************************\n");
    // Perform Environment Reset
    envReset ();
}


//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
// Generic Instruction Undocuemented Opcodes                               //
// See documented 6502-nmo.opc by Adam Vardy for more details              //
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//

// Undocumented - This opcode ANDs the contents of the A register with an immediate value and
// then LSRs the result.
void MOS6510::alr_instr (void)
{
    Register_Accumulator &= Cycle_Data;
    setFlagC   (Register_Accumulator & 0x01);
    setFlagsNZ (Register_Accumulator >>= 1);
}

// Undcouemented - ANC ANDs the contents of the A register with an immediate value and then
// moves bit 7 of A into the Carry flag.  This opcode works basically
// identically to AND #immed. except that the Carry flag is set to the same
// state that the Negative flag is set to.
void MOS6510::anc_instr (void)
{
    setFlagsNZ (Register_Accumulator &= Cycle_Data);
    setFlagC   (getFlagN ());
}

// Undocumented - This opcode ANDs the contents of the A register with an immediate value and
// then RORs the result (Implementation based on that of Frodo C64 Emulator)
void MOS6510::arr_instr (void)
{
    uint8_t data = Cycle_Data & Register_Accumulator;
    Register_Accumulator = data >> 1;
    if (getFlagC ()) Register_Accumulator |= 0x80;

    if (getFlagD ())
    {
        setFlagN (0);
        if (getFlagC ()) setFlagN (1 << SR_NEGATIVE);
        setFlagZ (Register_Accumulator);
        setFlagV ((data ^ Register_Accumulator) & 0x40);

        if ((data & 0x0f) + (data & 0x01) > 5)
            Register_Accumulator  = Register_Accumulator & 0xf0 | (Register_Accumulator + 6) & 0x0f;
        setFlagC (((data + (data & 0x10)) & 0x1f0) > 0x50);
        if (getFlagC ())
            Register_Accumulator += 0x60;
    }
    else
    {
        setFlagsNZ (Register_Accumulator);
        setFlagC   (Register_Accumulator & 0x40);
        setFlagV  ((Register_Accumulator & 0x40) ^ ((Register_Accumulator & 0x20) << 1));
    }
}

// Undocumented - This opcode ASLs the contents of a memory location and then ORs the result
// with the accumulator.
void MOS6510::aso_instr (void)
{
    setFlagC   (Cycle_Data & 0x80);
    Cycle_Data <<= 1;
    setFlagsNZ (Register_Accumulator |= Cycle_Data);
}

// Undocumented - This opcode DECs the contents of a memory location and then CMPs the result
// with the A register.
void MOS6510::dcm_instr (void)
{
    uint_least16_t tmp;
    Cycle_Data--;
    tmp = (uint_least16_t) Register_Accumulator - Cycle_Data;
    setFlagsNZ (tmp);
    setFlagC   (tmp < 0x100);
}

// Undocumented - This opcode INCs the contents of a memory location and then SBCs the result
// from the A register.
void MOS6510::ins_instr (void)
{
    Cycle_Data++;
    Perform_SBC ();
}

// Undocumented - This opcode ANDs the contents of a memory location with the contents of the
// stack pointer register and stores the result in the accumulator, the X
// register, and the stack pointer.  Affected flags: N Z.
void MOS6510::las_instr (void)
{
    setFlagsNZ (Cycle_Data &= endian_16lo8 (Register_StackPointer));
    Register_Accumulator  = Cycle_Data;
    Register_X            = Cycle_Data;
    Register_StackPointer = Cycle_Data;
}

// Undocumented - This opcode loads both the accumulator and the X register with the contents
// of a memory location.
void MOS6510::lax_instr (void)
{
    setFlagsNZ (Register_Accumulator = Register_X = Cycle_Data);
}

// Undocumented - LSE LSRs the contents of a memory location and then EORs the result with
// the accumulator.
void MOS6510::lse_instr (void)
{
    setFlagC   (Cycle_Data & 0x01);
    Cycle_Data >>= 1;
    setFlagsNZ (Register_Accumulator ^= Cycle_Data);
}

// Undocumented - This opcode ORs the A register with #xx, ANDs the result with an immediate
// value, and then stores the result in both A and X.
// xx may be EE,EF,FE, OR FF, but most emulators seem to use EE
void MOS6510::oal_instr (void)
{
    setFlagsNZ (Register_X = (Register_Accumulator = (Cycle_Data & (Register_Accumulator | 0xee))));
}

// Undocumented - RLA ROLs the contents of a memory location and then ANDs the result with
// the accumulator.
void MOS6510::rla_instr (void)
{
    uint8_t tmp = Cycle_Data & 0x80;
    Cycle_Data  = Cycle_Data << 1;
    if (getFlagC ()) Cycle_Data |= 0x01;
    setFlagC   (tmp);
    setFlagsNZ (Register_Accumulator &= Cycle_Data);
}

// Undocumented - RRA RORs the contents of a memory location and then ADCs the result with
// the accumulator.
void MOS6510::rra_instr (void)
{
    uint8_t tmp  = Cycle_Data & 0x01;
    Cycle_Data >>= 1;
    if (getFlagC ()) Cycle_Data |= 0x80;
    setFlagC (tmp);
    Perform_ADC ();
}

// Undocumented - This opcode ANDs the contents of the A and X registers (without changing
// the contents of either register) and transfers the result to the stack
// pointer.  It then ANDs that result with the contents of the high byte of
// the target address of the operand +1 and stores that final result in
// memory.
void MOS6510::tas_instr (void)
{
    endian_16lo8  (Register_StackPointer, Register_Accumulator & Register_X);
    uint_least16_t tmp = Register_StackPointer & (Cycle_EffectiveAddress + 1);
    Cycle_Data         = (signed) endian_16lo8 (tmp);
}

#endif // X86


//-------------------------------------------------------------------------//
// Initialise and create CPU Chip                                          //

//MOS6510::MOS6510 (model_t _model, const char *id)
MOS6510::MOS6510 (EventContext *context)
:eventContext(*context),
 Event("CPU")
{
    struct ProcessorOperations *instr;
    uint8_t legalMode  = true;
    uint8_t legalInstr = true;
    uint    i, pass;

    //----------------------------------------------------------------------
    // Build up the processor instruction table
    for (i = 0; i < 0x100; i++)
    {
#if MOS6510_DEBUG > 1
        printf ("Building Command %d[%02x]..", i, i);
#endif

        // Pass 1 allocates the memory, Pass 2 builds the instruction
        instr                = &instrTable[i];
        instr->cycle         = NULL;

        for (pass = 0; pass < 2; pass++)
        {
            enum {WRITE = 0, RMW = 1, READ = 2};
            int access = WRITE;
            cycleCount = -1;
            legalMode  = true;
            legalInstr = true;
            if (pass) procCycle = instr->cycle;

            switch (i)
            {
            // Accumulator or Implied addressing
            case ASLn: case CLCn: case CLDn: case CLIn: case CLVn:  case DEXn:
            case DEYn: case INXn: case INYn: case LSRn: case NOPn_: case PHAn:
            case PHPn: case PLAn: case PLPn: case ROLn: case RORn:  case RTIn:
            case RTSn: case SECn: case SEDn: case SEIn: case TAXn:  case TAYn:
            case TSXn: case TXAn: case TXSn: case TYAn:
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
            break;

            // Immediate and Relative Addressing Mode Handler
            case ADCb: case ANDb:  case ANCb_: case ANEb: case ASRb: case ARRb:
            case BCCr: case BCSr:  case BEQr:  case BMIr: case BNEr: case BPLr:
            case BRKn: case BVCr:  case BVSr:  case CMPb: case CPXb: case CPYb:
            case EORb: case LDAb:  case LDXb:  case LDYb: case LXAb: case NOPb_:
            case ORAb: case SBCb_: case SBXb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchDataByte;
            break;

            // Zero Page Addressing Mode Handler - Read & RMW
            case ADCz:  case ANDz: case BITz: case CMPz: case CPXz: case CPYz:
            case EORz:  case LAXz: case LDAz: case LDXz: case LDYz: case ORAz: 
            case NOPz_: case SBCz:
                access++;
            case ASLz: case DCPz: case DECz: case INCz: case ISBz: case LSRz:
            case ROLz: case RORz: case SREz: case SLOz: case RLAz: case RRAz:
                access++;
            case SAXz: case STAz: case STXz: case STYz:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowAddr;
                if (access == READ) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchEffAddrDataByte;
                } else if (access == RMW) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchPutEffAddrDataByte;
                }
            break;

            // Zero Page with X Offset Addressing Mode Handler
            case ADCzx: case ANDzx:  case CMPzx: case EORzx: case LDAzx: case LDYzx:
            case NOPzx_: case ORAzx: case SBCzx:
                access++;
            case ASLzx: case DCPzx: case DECzx: case INCzx: case ISBzx: case LSRzx:
            case RLAzx:    case ROLzx: case RORzx: case RRAzx: case SLOzx: case SREzx:
                access++;
            case STAzx: case STYzx:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowAddrX;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                if (access == READ) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchEffAddrDataByte;
                } else if (access == RMW) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchPutEffAddrDataByte;
                }
            break;

            // Zero Page with Y Offset Addressing Mode Handler
            case LDXzy: case LAXzy:
                access = READ;
            case STXzy: case SAXzy:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowAddrY;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                if (access == READ) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchEffAddrDataByte;
                }
            break;
            
            // Absolute Addressing Mode Handler
            case ADCa: case ANDa: case BITa: case CMPa: case CPXa: case CPYa:
            case EORa: case LAXa: case LDAa: case LDXa: case LDYa: case NOPa:
            case ORAa: case SBCa:
                access++;
            case ASLa: case DCPa: case DECa: case INCa: case ISBa: case LSRa: 
            case ROLa: case RORa: case SLOa: case SREa: case RLAa: case RRAa:
                access++;
            case JMPw: case JSRw: case SAXa: case STAa: case STXa: case STYa:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowAddr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighAddr;
                if (access == READ) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchEffAddrDataByte;
                } else if (access == RMW) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchPutEffAddrDataByte;
                }
            break;

            // Absolute With X Offset Addressing Mode Handler (Read)
            case ADCax: case ANDax:  case CMPax: case EORax: case LDAax:
            case LDYax: case NOPax_: case ORAax: case SBCax:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowAddr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighAddrX;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchEffAddrDataByte;
            break;

            // Absolute X (No page crossing handled)
            case ASLax: case DCPax: case DECax: case INCax: case ISBax:
            case LSRax: case RLAax: case ROLax: case RORax: case RRAax:
            case SLOax: case SREax:
                access = RMW;
            case SHYax: case STAax:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowAddr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighAddrX2;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                if (access == RMW) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchPutEffAddrDataByte;
                }
            break;

            // Absolute With Y Offset Addresing Mode Handler (Read)
            case ADCay: case ANDay: case CMPay: case EORay: case LASay:
            case LAXay: case LDAay: case LDXay: case ORAay: case SBCay:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowAddr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighAddrY;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchEffAddrDataByte;
            break;
            
            // Absolute Y (No page crossing handled)
            case DCPay: case ISBay: case RLAay: case RRAay: case SLOay:
            case SREay:
                access = RMW;
            case SHAay: case SHSay: case SHXay: case STAay:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowAddr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighAddrY2;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                if (access == RMW) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchPutEffAddrDataByte;
                }
            break;

            // Absolute Indirect Addressing Mode Handler
            case JMPi:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowPointer;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighPointer;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowEffAddr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighEffAddr;
            break;

            // Indexed with X Preinc Addressing Mode Handler
            case ADCix: case ANDix: case CMPix: case EORix: case LAXix: case LDAix:
            case ORAix: case SBCix: 
                access++;
            case DCPix: case ISBix: case SLOix: case SREix: case RLAix: case RRAix:
                access++;
            case SAXix: case STAix:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowPointer;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowPointerX;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowEffAddr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighEffAddr;
                if (access == READ) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchEffAddrDataByte;
                } else if (access == RMW) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchPutEffAddrDataByte;
                }
            break;

            // Indexed with Y Postinc Addressing Mode Handler (Read)
            case ADCiy: case ANDiy: case CMPiy: case EORiy: case LAXiy:
            case LDAiy: case ORAiy: case SBCiy:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowPointer;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowEffAddr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighEffAddrY;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchEffAddrDataByte;
            break;
            
            // Indexed Y (No page crossing handled)
            case DCPiy: case ISBiy: case RLAiy: case RRAiy: case SLOiy:
            case SREiy:
                access = RMW;
            case SHAiy: case STAiy:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowPointer;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchLowEffAddr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchHighEffAddrY2;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                if (access == RMW) {
                    cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchPutEffAddrDataByte;
                }
            break;

            default:
                legalMode = false;
            break;
            }

#ifdef MOS6510_DEBUG
            if (legalMode)
            {
                cycleCount++;
                if (pass) procCycle[cycleCount] = &MOS6510::DebugCycle;
            }
#endif // MOS6510_DEBUG

            //---------------------------------------------------------------------------------------
            // Addressing Modes Finished, other cycles are instruction dependent
            switch(i)
            {
            case ADCz:  case ADCzx: case ADCa: case ADCax: case ADCay: case ADCix:
            case ADCiy: case ADCb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::adc_instr;
            break;

            case ANCb_:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::anc_instr;
            break;

            case ANDz:  case ANDzx: case ANDa: case ANDax: case ANDay: case ANDix:
            case ANDiy: case ANDb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::and_instr;
            break;

            case ANEb: // Also known as XAA
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::ane_instr;
            break;

            case ARRb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::arr_instr;
            break;

            case ASLn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::asla_instr;
            break;

            case ASLz: case ASLzx: case ASLa: case ASLax:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::asl_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case ASRb: // Also known as ALR
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::alr_instr;
            break;

            case BCCr:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::bcc_instr;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
            break;

            case BCSr:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::bcs_instr;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
            break;

            case BEQr:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::beq_instr;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
            break;

            case BITz: case BITa:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::bit_instr;
            break;

            case BMIr:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::bmi_instr;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
            break;

            case BNEr:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::bne_instr;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
            break;

            case BPLr:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::bpl_instr;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
            break;

            case BRKn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PushHighPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PushLowPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::brk_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::IRQ1Request;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::IRQ2Request;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchOpcode;
            break;

            case BVCr:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::bvc_instr;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
            break;

            case BVSr:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::bvs_instr;
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
            break;

            case CLCn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::clc_instr;
            break;

            case CLDn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::cld_instr;
            break;

            case CLIn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::cli_instr;
            break;

            case CLVn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::clv_instr;
            break;

            case CMPz:  case CMPzx: case CMPa: case CMPax: case CMPay: case CMPix:
            case CMPiy: case CMPb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::cmp_instr;
            break;

            case CPXz: case CPXa: case CPXb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::cpx_instr;
            break;

            case CPYz: case CPYa: case CPYb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::cpy_instr;
            break;

            case DCPz: case DCPzx: case DCPa: case DCPax: case DCPay: case DCPix:
            case DCPiy: // Also known as DCM
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::dcm_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case DECz: case DECzx: case DECa: case DECax:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::dec_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case DEXn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::dex_instr;
            break;

            case DEYn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::dey_instr;
            break;

            case EORz:  case EORzx: case EORa: case EORax: case EORay: case EORix:
            case EORiy: case EORb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::eor_instr;
            break;

/* HLT // Also known as JAM
            case 0x02: case 0x12: case 0x22: case 0x32: case 0x42: case 0x52:
            case 0x62: case 0x72: case 0x92: case 0xb2: case 0xd2: case 0xf2:
            case 0x02: case 0x12: case 0x22: case 0x32: case 0x42: case 0x52:
            case 0x62: case 0x72: case 0x92: case 0xb2: case 0xd2: case 0xf2:
                cycleCount++; if (pass) procCycle[cycleCount] = hlt_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;
*/

            case INCz: case INCzx: case INCa: case INCax:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::inc_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case INXn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::inx_instr;
            break;

            case INYn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::iny_instr;
            break;

            case ISBz: case ISBzx: case ISBa: case ISBax: case ISBay: case ISBix:
            case ISBiy: // Also known as INS
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::ins_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case JMPw: case JMPi:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::jmp_instr;
            break;

            case JSRw:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::jsr_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PushLowPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::jmp_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case LASay:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::las_instr;
            break;

            case LAXz: case LAXzy: case LAXa: case LAXay: case LAXix: case LAXiy:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::lax_instr;
            break;

            case LDAz:  case LDAzx: case LDAa: case LDAax: case LDAay: case LDAix:
            case LDAiy: case LDAb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::lda_instr;
            break;

            case LDXz: case LDXzy: case LDXa: case LDXay: case LDXb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::ldx_instr;
            break;

            case LDYz: case LDYzx: case LDYa: case LDYax: case LDYb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::ldy_instr;
            break;

            case LSRn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::lsra_instr;
            break;

            case LSRz: case LSRzx: case LSRa: case LSRax:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::lsr_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case NOPn_: case NOPb_:
                // Should not be required!
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case NOPz_: case NOPzx_: case NOPa: case NOPax_:
            // NOPb NOPz NOPzx - Also known as SKBn
            // NOPa NOPax      - Also known as SKWn
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case LXAb: // Also known as OAL
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::oal_instr;
            break;

            case ORAz:  case ORAzx: case ORAa: case ORAax: case ORAay: case ORAix:
            case ORAiy: case ORAb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::ora_instr;
            break;

            case PHAn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::pha_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case PHPn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PushSR;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case PLAn:
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::pla_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case PLPn:
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PopSR;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case RLAz: case RLAzx: case RLAix: case RLAa: case RLAax: case RLAay:
            case RLAiy:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::rla_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case ROLn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::rola_instr;
            break;

            case ROLz: case ROLzx: case ROLa: case ROLax:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::rol_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case RORn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::rora_instr;
            break;

            case RORz: case RORzx: case RORa: case RORax:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::ror_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case RRAa: case RRAax: case RRAay: case RRAz: case RRAzx: case RRAix:
            case RRAiy:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::rra_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case RTIn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PopSR;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PopLowPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PopHighPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::rti_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case RTSn:
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PopLowPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PopHighPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::rts_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case SAXz: case SAXzy: case SAXa: case SAXix: // Also known as AXS
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::axs_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
            break;

            case SBCz:  case SBCzx: case SBCa: case SBCax: case SBCay: case SBCix:
            case SBCiy: case SBCb_:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::sbc_instr;
            break;

            case SBXb:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::sbx_instr;
            break;

            case SECn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::sec_instr;
            break;

            case SEDn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::sed_instr;
            break;

            case SEIn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::sei_instr;
            break;

            case SHAay: case SHAiy: // Also known as AXA
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::axa_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case SHSay: // Also known as TAS
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::shs_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
            break;

            case SHXay: // Also known as XAS
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::xas_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
            break;

            case SHYax: // Also known as SAY
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::say_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
            break;

            case SLOz: case SLOzx: case SLOa: case SLOax: case SLOay: case SLOix:
            case SLOiy: // Also known as ASO
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::aso_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case SREz: case SREzx: case SREa: case SREax: case SREay: case SREix:
            case SREiy: // Also known as LSE
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::lse_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PutEffAddrDataByte;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case STAz: case STAzx: case STAa: case STAax: case STAay: case STAix:
            case STAiy:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::sta_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case STXz: case STXzy: case STXa:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::stx_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case STYz: case STYzx: case STYa:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::sty_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            break;

            case TAXn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::tax_instr;
            break;

            case TAYn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::tay_instr;
            break;

            case TSXn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::tsx_instr;
            break;

            case TXAn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::txa_instr;
            break;

            case TXSn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::txs_instr;
            break;

            case TYAn:
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::tya_instr;
            break;

            default:
                legalInstr = false;
            break;
            }

            if (!(legalMode || legalInstr))
            {
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::illegal_instr;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
            }
            else if (!(legalMode && legalInstr))
            {
                printf ("\nInstruction 0x%x: Not built correctly.\n\n", i);
                exit(1);
            }
 
            cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::NextInstr;
            cycleCount++;
            if (!pass)
            {   // Pass 1 - Allocate Memory
                if (cycleCount)
                {
#if defined(_MSC_VER) && (_MSC_VER <= _MSC_VER_BAD_NEW)
                    typedef void (MOS6510::*ptr2cycle) (void);
                    instr->cycle = (ptr2cycle*) new char[sizeof (ptr2cycle) *cycleCount];
#else
#   ifdef HAVE_EXCEPTIONS
                    instr->cycle = new(std::nothrow) (void (MOS6510::*[cycleCount]) (void));
#   else
                    instr->cycle = new (void (MOS6510::*[cycleCount]) (void));
#   endif
#endif // _MSC_VER
                    if (!instr->cycle)
                        goto MOS6510_MemAllocFailed;
                }
            }
            else
                instr->opcode = i;

#if MOS6510_DEBUG > 1
            printf (".");
#endif
        }

        instr->cycles = cycleCount;
#if MOS6510_DEBUG > 1
        printf ("Done [%d Cycles]\n", cycleCount);
#endif
    }

    //----------------------------------------------------------------------
    // Build interrupts
    for (i = 0; i < 3; i++)
    {
#if MOS6510_DEBUG > 1
        printf ("Building Interrupt %d[%02x]..", i, i);
#endif

        // Pass 1 allocates the memory, Pass 2 builds the interrupt
        instr         = &interruptTable[i];
        instr->cycle  = NULL;
        instr->opcode = 0;

        for (int pass = 0; pass < 2; pass++)
        {
            cycleCount = -1;
            if (pass) procCycle = instr->cycle;

            switch (i)
            {
            case oRST:
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::RSTRequest;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchOpcode;
            break;

            case oNMI:
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PushHighPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PushLowPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::IRQRequest;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::NMIRequest;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::NMI1Request;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchOpcode;
            break;

            case oIRQ:
#ifdef MOS6510_ACCURATE_CYCLES
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::WasteCycle;
#endif
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PushHighPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::PushLowPC;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::IRQRequest;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::IRQ1Request;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::IRQ2Request;
                cycleCount++; if (pass) procCycle[cycleCount] = &MOS6510::FetchOpcode;
            break;
            }

            cycleCount++;
            if (!pass)
            {   // Pass 1 - Allocate Memory
                if (cycleCount)
                {
#if defined(_MSC_VER) && (_MSC_VER <= _MSC_VER_BAD_NEW)
                    typedef void (MOS6510::*ptr2cycle) (void);
                    instr->cycle = (ptr2cycle*) new char[sizeof (ptr2cycle) *cycleCount];
#else
#   ifdef HAVE_EXCEPTIONS
                    instr->cycle = new(std::nothrow) (void (MOS6510::*[cycleCount]) (void));
#   else
                    instr->cycle = new (void (MOS6510::*[cycleCount]) (void));
#   endif
#endif // _MSC_VER
                    if (!instr->cycle)
                        goto MOS6510_MemAllocFailed;
                }
            }

#if MOS6510_DEBUG > 1
            printf (".");
#endif
        }

        instr->cycles = cycleCount;
#if MOS6510_DEBUG > 1
        printf ("Done [%d Cycles]\n", cycleCount);
#endif
    }

    // Intialise Processor Registers
    Register_Accumulator   = 0;
    Register_X             = 0;
    Register_Y             = 0;

    Cycle_EffectiveAddress = 0;
    Cycle_Data             = 0;
    fetchCycle[0]          = &MOS6510::NextInstr;

    dodump = false;
    Initialise ();
return;

MOS6510_MemAllocFailed:
    printf ("Unable to allocate enough memory.\n\n");
exit (-1);
}

MOS6510::~MOS6510 ()
{
    struct ProcessorOperations *instr;
    uint i;

    // Remove Opcodes
    for (i = 0; i < 0x100; i++)
    {
        instr = &instrTable[i];
        if (instr->cycle != NULL) delete [] instr->cycle;
    }

    // Remove Interrupts
    for (i = 0; i < 3; i++)
    {
        instr = &interruptTable[i];
        if (instr->cycle != NULL) delete [] instr->cycle;
    }
}


//-------------------------------------------------------------------------//
// Initialise CPU Emulation (Registers)                                    //
void MOS6510::Initialise (void)
{
    // Reset stack
    Register_StackPointer = endian_16 (SP_PAGE, 0xFF);

    // Reset Cycle Count
    cycleCount = 0;
    procCycle  = fetchCycle;

    // Reset Status Register
    Register_Status = (1 << SR_NOTUSED) | (1 << SR_BREAK);
    // FLAGS are set from data directly and do not require
    // being calculated first before setting.  E.g. if you used
    // SetFlags (0), N flag would = 0, and Z flag would = 1.
    setFlagsNZ (1);
    setFlagC   (false);
    setFlagV   (false);

    // Set PC to some value
    Register_ProgramCounter = 0;
    // IRQs pending check
    interrupts.irqLatch   = false;
    interrupts.irqRequest = false;
    if (interrupts.irqs)
        interrupts.irqRequest = true;

    // Signals
    aec = true;
    rdy = true;

    m_blocked = false;
    eventContext.schedule (this, 1);
}

//-------------------------------------------------------------------------//
// Reset CPU Emulation                                                     //
void MOS6510::reset (void)
{
    // Reset Interrupts
    interrupts.pending = false;
    interrupts.irqs    = 0;
    interrupts.delay   = MOS6510_INTERRUPT_DELAY;

    // Internal Stuff
    Initialise ();

    // Requires External Bits
    // Read from reset vector for program entry point
    endian_16lo8 (Cycle_EffectiveAddress, envReadMemDataByte (0xFFFC));
    endian_16hi8 (Cycle_EffectiveAddress, envReadMemDataByte (0xFFFD));
    Register_ProgramCounter = Cycle_EffectiveAddress;
//    filepos = 0;
}

//-------------------------------------------------------------------------//
// Module Credits                                                          //
void MOS6510::credits (char *sbuffer)
{   // Copy credits to buffer
    sprintf (sbuffer, "%sModule     : MOS6510 Cycle Exact Emulation\n", sbuffer);
    sprintf (sbuffer, "%sWritten By : %s\n", sbuffer, MOS6510_AUTHOR);
    sprintf (sbuffer, "%sVersion    : %s\n", sbuffer, MOS6510_VERSION);
    sprintf (sbuffer, "%sReleased   : %s\n", sbuffer, MOS6510_DATE);
    sprintf (sbuffer, "%sEmail      : %s\n", sbuffer, MOS6510_EMAIL);
}