File: cpu_m68k.s

package info (click to toggle)
atari800 0.8.6-2
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 1,364 kB
  • ctags: 2,457
  • sloc: ansic: 22,055; asm: 2,812; cpp: 581; makefile: 68; sh: 8
file content (2161 lines) | stat: -rw-r--r-- 50,015 bytes parent folder | download
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
;This code is copyrighted by Empty Head (c)1997, but
;you can use it without any charge in your own programs
;use it on your own risk ;)


;DEBUG ;if it is active it is possible to trace through 6502 memory
;SERVER ;if conrol server is active
;C_BUGGING ;debug in C - logical, isn't it ? :)

	ifd DEBUG
	bra START
	endc
	OPT		P=68040,O+,W-

	ifnd DEBUG
	xref _GETBYTE ;procedures for A800 bytes
	xref _PUTBYTE
	xref _Escape
	xref _break_addr
	xref _tisk
	xdef _regPC
	xdef _regA
	xdef _regP ;/* Processor Status Byte (Partial) */
	xdef _regS
	xdef _regX
	xdef _regY
	xref _memory
	xref _attrib
	xdef _IRQ
	xdef _NMI
	xdef _RTI
	xdef _GO
	xdef _CPUGET
	xdef _INIT
	xdef _OPMODE_TABLE
	ifd C_BUGGING
	xref _CEKEJ
	xdef _ADRESA
	xdef _AIRQ
	endc
	endc

regA
_regA	ds.b 1   ;d0 -A
regX
_regX	ds.b 1   ;d1 -X
regY
_regY	ds.b 1   ;d2 -Y
regS
_regS	ds.b 1   ;stack
regPC
_regPC	ds.w 1   ;a4 PC
regP
VBDIFLAG	         ;same as regP
_regP	ds.b 1   ;   -CCR
IRQ	         ;I have to reserve it there because other it is 32-bit number (in GCC)
_IRQ	ds.b 1


	even


CD	equr a6 ;cycles counter down
_pointer equr a1
memory_pointer equr a5
attrib_pointer equr a3
stack_pointer equr a2
PC6502	equr a4


ZFLAG	equr d5 ;we used it straight in shifting, because it is faster than move again
                    ;I mean rol, ror, asl, asr, lsr, lsl
NFLAG	equr d3 ;
CCR6502	equr d4 ;only carry is usable (Extended on 68000)
A	equr d0
X	equr d1
Y	equr d2  ;$206 ;d2

;d6	contains usually adress where we are working
;d7	contains is a working register


LoHi  macro		;change order of lo and hi byte in d6
      ror.w #8,d7
      endm

;	==========================================================
;	Emulated Registers and Flags are kept local to this module
;	==========================================================



;#define UPDATE_GLOBAL_REGS regPC=PC;regS=S;regA=A;regX=X;regY=Y
UPDATE_GLOBAL_REGS	macro
	sub.l memory_pointer,PC6502
	sub.l memory_pointer,stack_pointer
	lea -$101(stack_pointer),stack_pointer
	move.w stack_pointer,d6
	lea _regP(pc),a1
	move.w PC6502,-(a1) ;_PC6502
	move.b d6,-(a1) ;regS
	move.b Y,-(a1) ;regY
	move.b X,-(a1) ;regX
	move.b A,-(a1) ;regA
	endm

;#define UPDATE_LOCAL_REGS PC=regPC;S=regS;A=regA;X=regX;Y=regY
UPDATE_LOCAL_REGS macro
	lea _regA(pc),a1
	clr.w A
	move.b (a1)+,A ;A register
	clr.w X
	move.b (a1)+,X ;X
	clr.w Y
	move.b (a1)+,Y ;Y
	clr.l d7
	move.b (a1)+,d7 ;stack regS
	lea $101(memory_pointer),stack_pointer
	add.l d7,stack_pointer
	move.w (a1)+,d7
	move.l memory_pointer,PC6502
	add.l d7,PC6502
	endm





;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~ WAKE UP PROCESSOR IN DEBUG MODE ~
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


	ifd DEBUG
START:      bsr _INIT ;make from OPCODE TABLE & MODE TABLE -  OPMODE TABLE
	clr.l -(a7)
	move.w #32,-(a7)
	trap #1
	addq.l #6,a7
	move.l d0,-(a7)
	lea _memory,memory_pointer
	clr.b IRQ ;no interrupt
	move.b #$20,regP ;unused bit is always 1
	move.b #$ff,regS ;set up stack to the star
	move.w (memory_pointer,$fffc.l),d7 ;start addres
	;move.w #$0020,d7
	LoHi
	move.w d7,regPC
	UPDATE_LOCAL_REGS
	clr.l d0
	;move.l #3560,d0
	;lea TEST,a0
	move.l $4ba,-(a7)
ANOTHER:
	move.l #27360,-(a7)  ;one screen
	bsr _GO
	addq.l #4,a7
	;move.l (a7)+,a0
	;move.l d7,(a0)+
	sub.l #1,AAA
	bne.s ANOTHER
	move.l $4ba.l,d7
	sub.l (a7)+,d7
	move.l d7,$200.w
	move.w #32,-(a7)
	trap #1
	addq.l #6,a7
	illegal
	clr.w -(a7)
	trap #1
	endc

	section data
AAA:	dc.l 50
	section text



_INIT:
	movem.l a2/a3/d2,-(a7)  ;copy opcyc table
	lea OPMODE_TABLE,a1
	;move.l a1,d2 ;we need to start on a word adress
	;add.l #4,d2
	;and.l #$fffffc,d2
	;move.l d2,a1
	;lea OPCODE_TABLE,a1 ;3rd case of jump (mode, get, then this command)
	lea MODE_TABLE2,a0 ;1st case of jump (mode)
	lea MODE_TABLE1,a3 ;2nd case of jump (jump after mode (in put case it is instruction, if get then we will call get byte routine)
	move.w #255,d0
COPY

	move.l (a0)+,(a1)+ ;base jump (instruction or mode)
	move.l (a3)+,d2    ;jump
	cmp.l #opcode_81,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_84,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_85,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_86,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_8c,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_8d,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_8e,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_91,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_94,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_95,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_96,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_99,d2
	beq.s WRITE_WRITE
	cmp.l #opcode_9d,d2
	beq.s WRITE_WRITE
	;sub.l #JUMP_CODE+2,d2
	move.l d2,(a1)+ ;write jump down
	;addq.l #2,a1
	bra.s NEXT
WRITE_WRITE:
	;sub.l #JUMP_CODE+2,d2
	bset #31,d2
	move.l d2,(a1)+ ;write jump down
	;addq.l #2,a1

NEXT:	dbf d0,COPY
	movem.l (a7)+,a2/a3/d2
	rts


;these are bit in MC68000 CCR register
NB68	equ 3
EB68	equ 4 ;used as a carry in 6502 emulation
ZB68	equ 2
OB68	equ 1
CB68	equ 0

N_FLAG equ $80    ;tested in ZN flag in the same way as usual on 6502
N_FLAGB equ 7
V_FLAG equ $40
V_FLAGB equ 6
G_FLAG equ $20
G_FLAGB equ $5
B_FLAG equ $10
B_FLAGB equ 4
D_FLAG equ $08
D_FLAGB equ 3
I_FLAG equ $04
I_FLAGB equ 2
Z_FLAG equ $02
Z_FLAGB equ 1
C_FLAG equ $01
C_FLAGB equ 0

;void CPU_GetStatus (void) - assembler (CPU_ST.S)
;void CPU_PutStatus (void) - assembler (CPU_ST.S)
;void CPU_Reset (void) - C (CPU_ST.C)
;void SetRAM (int addr1, int addr2) - C (CPU_ST.C)
;void SetROM (int addr1, int addr2) - C (CPU_ST.C)
;void SetHARDWARE (int addr1, int addr2) - C (CPU_ST.C)
;void NMI (void) - C (CPU_ST.C)
;void GO (int cycles); - assembler (CPU_ST.S)



SetI	macro
	bset #I_FLAGB,_regP
	endm

ClrI	macro
	bclr #I_FLAGB,_regP
	endm

SetB	macro
	bset #B_FLAGB,_regP
	endm

ClrD	macro
	bclr #D_FLAGB,_regP
	endm

SetD	macro
	bset #D_FLAGB,_regP
	endm

;#define SetN regP|=N_FLAG
;#define ClrN regP&=(~N_FLAG)
;#define SetV regP|=V_FLAG
;#define ClrV regP&=(~V_FLAG)
;#define SetB regP|=B_FLAG
;#define ClrB regP&=(~B_FLAG)
;#define SetD regP|=D_FLAG
;#define ClrD regP&=(~D_FLAG)
;#define SetI regP|=I_FLAG
;#define ClrI regP&=(~I_FLAG)
;#define SetZ regP|=Z_FLAG
;#define ClrZ regP&=(~Z_FLAG)
;#define SetC regP|=C_FLAG
;#define ClrC regP&=(~C_FLAG)

;extern UBYTE memory[65536];

;extern int IRQ;

;#endif


goto 	macro
	bra \1
	endm


;static char *rcsid = "$Id: cpu_m68k.s,v 1.2 1998/02/21 15:02:46 david Exp $";



;static UBYTE	N;	/* bit7 zero (0) or bit 7 non-zero (1) */
;static UBYTE	Z;	/* zero (0) or non-zero (1) */
;static UBYTE	V;
;static UBYTE	C;	/* zero (0) or one(1) */

RAM 	equ 0
ROM 	equ 1
HARDWARE    equ 2


;/*
; * The following array is used for 6502 instruction profiling
; */

;int count[256];
;UBYTE memory[65536];

;int IRQ;

;static UBYTE attrib[65536];


;#define	GetByte(addr)	((attrib[addr] == HARDWARE) ? Atari800_GetByte(addr) : memory[addr])
;#define	PutByte(addr,byte)	if (attrib[addr] == RAM) memory[addr] = byte; else if (attrib[addr] == HARDWARE) if (Atari800_PutByte(addr,byte)) break;

;/*
;	===============================================================
;	Z flag: This actually contains the result of an operation which
;		would modify the Z flag. The value is tested for
;		equality by the BEQ and BNE instruction.
;	===============================================================
;*/

; Bit    : 76543210
; CCR6502: ***XNZVC
; _RegP  : NV*BDIZC

ConvertSTATUS_RegP macro
	move.b _regP,d6 ;put flag VBDI into d6
	bset #C_FLAGB,d6 ;put there carry flag
	btst #EB68,CCR6502
	bne.s .SETC\@
	bclr #C_FLAGB,d6
.SETC\@	bset #N_FLAGB,d6 ;put there Negative flag
	tst.b NFLAG
	bmi.s .SETN\@
	bclr #N_FLAGB,d6
.SETN\@	bset #Z_FLAGB,d6 ;put there zero flag
	tst.b ZFLAG
	beq.s .SETZ\@   ;is there beware! reverse compare is ok
	bclr #Z_FLAGB,d6
.SETZ\@
	endm

ConvertRegP_STATUS macro
	move.b _regP,d6
	moveq #-1,CCR6502
	btst #C_FLAGB,d6
	bne.s .SETC\@
	clr.w CCR6502
.SETC\@	clr.b ZFLAG
	btst #Z_FLAGB,d6
	bne.s .SETZ\@ ;reverse is ok
	not.b ZFLAG
.SETZ\@	clr.b NFLAG
	btst #N_FLAGB,d6
	beq.s .SETN\@
	not.b NFLAG
.SETN\@
	endm


CPU_GetStatus macro
	ConvertSTATUS_RegP
	move.b d6,_regP
	endm

CPU_PutStatus macro
	ConvertRegP_STATUS
	endm


PHP	macro
	ConvertSTATUS_RegP
	move.b d6,-(stack_pointer)
	endm

PLP	macro
	move.b (stack_pointer)+,_regP
	ConvertRegP_STATUS
	endm


;#define AND(t_data) data = t_data; Z = N = A &= data;
;#define CMP(t_data) data = t_data; Z = N = A - data; C = (A >= data)
;#define CPX(t_data) data = t_data; Z = N = X - data; C = (X >= data);
;#define CPY(t_data) data = t_data; Z = N = Y - data; C = (Y >= data);
;#define EOR(t_data) data = t_data; Z = N = A ^= data;
;#define LDA(data) Z = N = A = data;
;#define LDX(data) Z = N = X = data;
;#define LDY(data) Z = N = Y = data;
;#define ORA(t_data) data = t_data; Z = N = A |= data

;#define PHP data =  (N & 0x80); \
;            data |= V ? 0x40 : 0; \
;            data |= (regP & 0x3c); \
;	    data |= (Z == 0) ? 0x02 : 0; \
;	    data |= C; \
;	    memory[0x0100 + S--] = data;
;
;#define PLP data = memory[0x0100 + ++S]; \
;	    N = (data & 0x80); \
;	    V = (data & 0x40) ? 1 : 0; \
;	    Z = (data & 0x02) ? 0 : 1; \
;	    C = (data & 0x01); \
;            regP = (data & 0x3c) | 0x20;
;


CPUGET:
_CPUGET:
	ConvertSTATUS_RegP
	move.b d6,_regP
	rts



NMI:
_NMI:
	movem.l d0-d7/a0-a6,-(a7)
	lea _memory,memory_pointer
	move.w _regPC,d6 ;we will put adress on stack of 6502
	ror.w #8,d6
            clr.l d7	    ;find stack in memory
	move.b _regS,d7
            move.l d7,stack_pointer
	add.l memory_pointer,stack_pointer
	lea $101(stack_pointer),stack_pointer
	move.w d6,-(stack_pointer) ;put back adress onto stack
	move.b _regP,-(stack_pointer) ;put P onto stack YEAAAH!
	SetI

	;put regPC & Stack pointer adress on its place
	move.w (memory_pointer,$fffa.l),d7
	LoHi
	move.w d7,_regPC
	lea -$101(stack_pointer),stack_pointer
	move.l stack_pointer,d7 ;put stack
	sub.l memory_pointer,d7
	move.b d7,_regS
	movem.l (a7)+,d0-d7/a0-a6
	ifd SERVER
	move.w #1,WAIT
	endc
	rts

  	;UBYTE S = regS;
  	;UBYTE data;

  	;memory[0x0100 + S--] = regPC >> 8;
  	;memory[0x0100 + S--] = regPC & 0xff;
  	;PHP;
  	;SetI;
  	;regPC = (memory[0xfffb] << 8) | memory[0xfffa];
  	;regS = S;



;/*
;	==============================================================
;	The first switch statement is used to determine the addressing
;	mode, while the second switch implements the opcode. When I
;	have more confidence that these are working correctly they
;	will be combined into a single switch statement. At the
;	moment changes can be made very easily.
;	==============================================================
;*/

	section data
CYCLES:
	dc.w 7, 2, 0, 8, 3, 3, 5, 5, 3, 2, 2, 2, 4, 4, 6, 6
  	dc.w 2, 5, 0, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7
  	dc.w 6, 6, 0, 8, 3, 3, 5, 5, 4, 2, 2, 2, 4, 4, 6, 6
  	dc.w 2, 5, 0, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7

  	dc.w 6, 6, 0, 8, 3, 3, 7, 5, 3, 2, 2, 2, 3, 4, 6, 6
  	dc.w 2, 5, 0, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 0, 7
  	dc.w 6, 6, 0, 8, 3, 3, 5, 5, 4, 2, 2, 2, 5, 4, 6, 6
  	dc.w 2, 5, 0, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7

  	dc.w 2, 6, 2, 6, 3, 3, 3, 3, 2, 2, 2, 2, 4, 4, 4, 4
  	dc.w 2, 6, 0, 6, 4, 4, 4, 4, 2, 5, 2, 5, 5, 5, 5, 5
  	dc.w 2, 6, 2, 6, 3, 3, 3, 3, 2, 2, 2, 2, 4, 4, 4, 4
  	dc.w 2, 5, 0, 5, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 4, 4

  	dc.w 2, 6, 2, 8, 3, 3, 5, 5, 2, 2, 2, 2, 4, 4, 6, 6
  	dc.w 2, 5, 0, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7
  	dc.w 2, 6, 2, 8, 3, 3, 5, 5, 2, 2, 2, 2, 4, 4, 6, 6
  	dc.w 2, 5, 0, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7



MODE_TABLE1:
;when we will call instruction straight, this table is used
;when we want to get byte, this table contains addr to GET BYTE routine
;when we want to put byte, this table contains addr to own command
;when we want to put and get byte (rol, ror, inc etc.), this routine is the same as in the case GET BYTE
;if the instruction take byte straight from command, this routine contains addr to own comman as well


	dc.l opcode_00, opcode_01, opcode_02, opcode_03
	;     brk         ora        illegal     illegal

	dc.l opcode_04,opcode_05,opcode_06, opcode_07
	;     nop         ora          asl        nop

	dc.l opcode_08,opcode_09,opcode_0a,opcode_0b
	;     php         ora        asla       illegal

	dc.l opcode_0c,opcode_0d,opcode_0e,opcode_0f
	;     nop 3       ora          asl      illegal

	dc.l opcode_10,opcode_11,  opcode_12, opcode_13
	;     bpl         ora        illegal    illegal

	dc.l opcode_14,opcode_15, opcode_16,  opcode_17
	;     nop         ora         asl       illegal

	dc.l opcode_18,opcode_19, opcode_1a,   opcode_1b
	;     clc         ora         nop       illegal

      	dc.l opcode_1c,opcode_1d,  opcode_1e, opcode_1f
	;     nop         ora        asl        illegal

      	dc.l opcode_20,opcode_21,opcode_22,  opcode_23
	;     jsr         and       illegal     illegal

      	dc.l opcode_24,opcode_25,opcode_26,opcode_27
	;     bit         and        rol        illegal

      	dc.l opcode_28, opcode_29,    opcode_2a,  opcode_2b
	;     plp        and         rolA      illegal

      	dc.l opcode_2c, opcode_2d, opcode_2e, opcode_2f
	;      bit       and         rol       illegal

      	dc.l opcode_30, opcode_31, opcode_32, opcode_33
	;      bmi       and        illegal     illegal

      	dc.l opcode_34, opcode_35, opcode_36, opcode_37
	;     nop        and         rol       illegal

      	dc.l opcode_38, opcode_39, opcode_3a, opcode_3b
	;      sec        and        nop        illegal

      	dc.l opcode_3c, opcode_3d, opcode_3e, opcode_3f
	;      nop         and       rol        illegal

      	dc.l opcode_40, opcode_41, opcode_42, opcode_43
            ;      rti        eor        illegal   illegal

      	dc.l opcode_44, opcode_45, opcode_46, opcode_47
	;      nop        eor        lsr       illegal

      	dc.l opcode_48, opcode_49, opcode_4a, opcode_4b
      	;      pha        eor        lsrA       illegal

      	dc.l opcode_4c, opcode_4d, opcode_4e, opcode_4f
	;      jmp        eor        lsr       illegal

      	dc.l opcode_50, opcode_51,  opcode_52, opcode_53
	;      bvc       eor        illegal     illegal

      	dc.l opcode_54, opcode_55, opcode_56, opcode_57
	;      nop        eor      lsr       illegal

      	dc.l opcode_58, opcode_59, opcode_5a, opcode_5b
	;      cli        eor      nop         illegal

      	dc.l opcode_5c, opcode_5d, opcode_5e, opcode_5f
	;      nop        eor        lsr        illegal

      	dc.l opcode_60, opcode_61, opcode_62, opcode_63
	;      rts        adc        illegal    illegal

      	dc.l opcode_64, opcode_65, opcode_66, opcode_67
	;      nop        adc        ror        illegal

      	dc.l opcode_68, opcode_69,opcode_6a,opcode_6b
	;      pla       adc         rorA       illegal

      	dc.l opcode_6c, opcode_6d, opcode_6e, opcode_6f
	;    jmp (abcd)   adc      ror          illegal

      	dc.l opcode_70, opcode_71, opcode_72, opcode_73
	;      bvs        adc      illegal    illegal

      	dc.l opcode_74, opcode_75, opcode_76, opcode_77
	;      nop         adc       ror       illegal

      	dc.l opcode_78, opcode_79, opcode_7a, opcode_7b
	;     SEI          adc        illegal   illegal

      	dc.l opcode_7c, opcode_7d, opcode_7e, opcode_7f
	;     nop          adc       ror        illegal

      	dc.l opcode_80, opcode_81, opcode_82, opcode_83
	;     nop          sta       nop        illegal

      	dc.l opcode_84, opcode_85, opcode_86,opcode_87
	;     sty	       sta       stx        illegal

      	dc.l opcode_88, opcode_89, opcode_8a, opcode_8b
	;     dey          nop        txa       illegal

      	dc.l opcode_8c, opcode_8d, opcode_8e, opcode_8f
	;     sty	       sta       stx        illegal

      	dc.l opcode_90, opcode_91, opcode_92, opcode_93
	;      bcc         sta       illegal    illegal

      	dc.l opcode_94, opcode_95, opcode_96, opcode_97
	;      sty       sta         stx        illegal

      	dc.l opcode_98, opcode_99, opcode_9a, opcode_9b
	;     tya        sta         txs         illegal

      	dc.l opcode_9c, opcode_9d, opcode_9e, opcode_9f
	;     illegal     sta         ilegal     illegal

      	dc.l opcode_a0,    opcode_a1, opcode_a2, opcode_a3
	;      ldy         lda        ldx        lax

      	dc.l opcode_a4, opcode_a5, opcode_a6, opcode_a7
	;     ldy        lda        ldx         lax

      	dc.l opcode_a8, opcode_a9,     opcode_aa, opcode_ab
 	;     tay         lda         tax      illegal

      	dc.l opcode_ac, opcode_ad, opcode_ae, opcode_af
	;      ldy         lda        ldx        lax

      	dc.l opcode_b0, opcode_b1, opcode_b2, opcode_b3
	;      bcs       lda         illegal    lax

      	dc.l opcode_b4, opcode_b5, opcode_b6, opcode_b7
	;      ldy        lda         ldx       lax

      	dc.l opcode_b8, opcode_b9, opcode_ba, opcode_bb
	;      clv         lda        tsx        illegal

      	dc.l opcode_bc, opcode_bd, opcode_be, opcode_bf
	;       ldy        lda         ldx           lax

      	dc.l opcode_c0, opcode_c1, opcode_c2, opcode_c3
	;    cpy          cmp         nop        illegal

      	dc.l opcode_c4, opcode_c5, opcode_c6, opcode_c7
	;     cpy         cmp       dec         illegal

      	dc.l opcode_c8, opcode_c9,  opcode_ca, opcode_cb
	;      iny       cmp       dex         illegal

      	dc.l opcode_cc, opcode_cd, opcode_ce, opcode_cf
	;     cpy        cmp        dec        illegal

      	dc.l opcode_d0, opcode_d1, opcode_d2, opcode_d3
	;      bne       cmp         escrts      illegal

      	dc.l opcode_d4, opcode_d5, opcode_d6, opcode_d7
	;     nop         cmp      dec         illegal

      	dc.l opcode_d8, opcode_d9, opcode_da, opcode_db
	;     cld         cmp          nop     illegal

      	dc.l opcode_dc, opcode_dd, opcode_de, opcode_df
	;      nop         cmp        dec       illegal

      	dc.l opcode_e0, opcode_e1, opcode_e2, opcode_e3
 	;     cpx         sbc        nop         nop

      	dc.l opcode_e4, opcode_e5, opcode_e6, opcode_e7
	;     cpx        sbc          inc        illegal

      	dc.l opcode_e8, opcode_e9,opcode_ea, opcode_eb
	;      inx       sbc           nop      illegal

      	dc.l opcode_ec, opcode_ed, opcode_ee, opcode_ef
	;       cpx      sbc          inc         illegal

	dc.l opcode_f0, opcode_f1, opcode_f2, opcode_f3
	;     beq          sbc        esc#ab     illegal

      	dc.l opcode_f4, opcode_f5, opcode_f6, opcode_f7
	;     nop          sbc        inc       illegal

      	dc.l opcode_f8, opcode_f9, opcode_fa, opcode_fb
	;     sed         sbc         nop         illegal

      	dc.l opcode_fc, opcode_fd, opcode_fe, opcode_ff
	;     nop         sbc          inc        esc#ab



MODE_TABLE2:
	dc.l opcode_00, INDIRECT_X, opcode_02, opcode_03
	;     brk         ora        illegal     illegal

	dc.l opcode_04,  ZPAGE,      ZPAGE,   opcode_07
	;     nop         ora          asl        nop

	dc.l opcode_08,  DIRECT,   opcode_0a, opcode_0b
	;     php         ora          asla     illegal

	dc.l opcode_0c, ABSOLUTE,   ABSOLUTE,  opcode_0f
	;     nop 3       ora          asl      illegal

	dc.l opcode_10, INDIRECT_Y, opcode_12, opcode_13
	;     bpl         ora        illegal    illegal

	dc.l opcode_14,   ZPAGE_X,   ZPAGE_X,  opcode_17
	;     nop         ora         asl       illegal

	dc.l opcode_18, ABSOLUTE_Y, opcode_1a, opcode_1b
	;     clc         ora         nop       illegal

      	dc.l opcode_1c, ABSOLUTE_X,ABSOLUTE_X, opcode_1f
	;     nop         ora        asl        illegal

      	dc.l opcode_20, INDIRECT_X,opcode_22,  opcode_23
	;     jsr         and       illegal     illegal

      	dc.l ZPAGE,      ZPAGE,    ZPAGE,      opcode_27
	;     bit         and        rol        illegal

      	dc.l opcode_28, DIRECT,    opcode_2a,  opcode_2b
	;     plp        and         rolA      illegal

       	dc.l ABSOLUTE, ABSOLUTE,   ABSOLUTE,   opcode_2f
	;      bit       and         rol       illegal

      	dc.l opcode_30, INDIRECT_Y, opcode_32, opcode_33
	;      bmi       and        illegal     illegal

      	dc.l opcode_34, ZPAGE_X,    ZPAGE_X, opcode_37
	;     nop        and         rol       illegal

      	dc.l opcode_38, ABSOLUTE_Y, opcode_3a, opcode_3b
	;      sec        and        nop        illegal

      	dc.l opcode_3c, ABSOLUTE_X, ABSOLUTE_X, opcode_3f
	;      nop         and       rol        illegal

      	dc.l opcode_40, INDIRECT_X, opcode_42, opcode_43
            ;      rti        eor        illegal   illegal

      	dc.l opcode_44,  ZPAGE,    ZPAGE,      opcode_47
	;      nop        eor        lsr       illegal

      	dc.l opcode_48, DIRECT,     opcode_4a, opcode_4b
      	;      pha        eor        lsrA       illegal

	dc.l opcode_4c, ABSOLUTE, ABSOLUTE,    opcode_4f
	;      jmp        eor        lsr       illegal

      	dc.l opcode_50, INDIRECT_Y,  opcode_52, opcode_53
	;      bvc       eor        illegal     illegal

      	dc.l opcode_54, ZPAGE_X,  ZPAGE_X, opcode_57
	;      nop        eor      lsr       illegal

      	dc.l opcode_58, ABSOLUTE_Y, opcode_5a, opcode_5b
	;      cli        eor      nop         illegal

      	dc.l opcode_5c, ABSOLUTE_X, ABSOLUTE_X, opcode_5f
	;      nop        eor        lsr        illegal

      	dc.l opcode_60, INDIRECT_X, opcode_62, opcode_63
	;      rts        adc        illegal    illegal

      	dc.l opcode_64, ZPAGE,      ZPAGE,     opcode_67
	;      nop        adc        ror        illegal

      	dc.l opcode_68, DIRECT,   opcode_6a,  opcode_6b
	;      pla       adc         rorA       illegal

      	dc.l opcode_6c, ABSOLUTE, ABSOLUTE,   opcode_6f
	;    jmp (abcd)   adc      ror          illegal

      	dc.l opcode_70, INDIRECT_Y, opcode_72, opcode_73
	;      bvs        adc      illegal    illegal

      	dc.l opcode_74,  ZPAGE_X,   ZPAGE_X,   opcode_77
	;      nop         adc       ror       illegal

      	dc.l opcode_78, ABSOLUTE_Y, opcode_7a, opcode_7b
	;     SEI          adc        illegal   illegal

      	dc.l opcode_7c, ABSOLUTE_X, ABSOLUTE_X, opcode_7f
	;     nop          adc       ror        illegal

      	dc.l opcode_80, INDIRECT_X, opcode_82, opcode_83
	;     nop          sta       nop        illegal

      	dc.l ZPAGE,     ZPAGE,      ZPAGE,     opcode_87
	;     sty	       sta       stx        illegal

      	dc.l opcode_88, opcode_89, opcode_8a, opcode_8b
	;     dey          nop        txa       illegal

      	dc.l ABSOLUTE,  ABSOLUTE,  ABSOLUTE, opcode_8f
	;     sty	       sta       stx        illegal

      	dc.l opcode_90, INDIRECT_Y, opcode_92, opcode_93
	;      bcc         sta       illegal    illegal

      	dc.l ZPAGE_X,   ZPAGE_X,   ZPAGE_Y, opcode_97
	;      sty       sta         stx        illegal


      	dc.l opcode_98, ABSOLUTE_Y, opcode_9a, opcode_9b
	;     tya        sta         txs         illegal

      	dc.l opcode_9c, ABSOLUTE_X, opcode_9e, opcode_9f
	;     illegal     sta         ilegal     illegal

      	dc.l DIRECT,    INDIRECT_X, DIRECT,    INDIRECT_X
	;      ldy         lda        ldx        lax

      	dc.l ZPAGE,     ZPAGE,     ZPAGE,      ZPAGE
	;     ldy        lda        ldx         lax

      	dc.l opcode_a8, DIRECT,     opcode_aa, opcode_ab
 	;     tay         lda         tax      illegal

      	dc.l ABSOLUTE, ABSOLUTE,   ABSOLUTE,   ABSOLUTE
	;      ldy         lda        ldx        lax

      	dc.l opcode_b0, INDIRECT_Y, opcode_b2, INDIRECT_Y
	;      bcs       lda         illegal    lax

      	dc.l ZPAGE_X,  ZPAGE_X,    ZPAGE_Y,    ZPAGE_Y
	;      ldy        lda         ldx       lax

      	dc.l opcode_b8, ABSOLUTE_Y, opcode_ba, opcode_bb
	;      clv         lda        tsx        illegal

      	dc.l ABSOLUTE_X, ABSOLUTE_X, ABSOLUTE_Y,  ABSOLUTE_Y
	;       ldy        lda         ldx           lax

      	dc.l DIRECT,    INDIRECT_X, opcode_c2, opcode_c3
	;    cpy          cmp         nop        illegal

      	dc.l ZPAGE,       ZPAGE,    ZPAGE,     opcode_c7
	;     cpy         cmp       dec         illegal

      	dc.l opcode_c8, DIRECT,  opcode_ca, opcode_cb
	;      iny       cmp       dex         illegal

      	dc.l ABSOLUTE,  ABSOLUTE, ABSOLUTE, opcode_cf
	;     cpy        cmp        dec        illegal

      	dc.l opcode_d0, INDIRECT_Y, opcode_d2, opcode_d3
	;      bne       cmp         escrts      illegal

      	dc.l opcode_d4, ZPAGE_X,    ZPAGE_X,   opcode_d7
	;     nop         cmp      dec         illegal


      	dc.l opcode_d8, ABSOLUTE_Y, opcode_da, opcode_db
	;     cld         cmp          nop     illegal

      	dc.l opcode_dc, ABSOLUTE_X, ABSOLUTE_X, opcode_df
	;      nop         cmp        dec       illegal

      	dc.l DIRECT,    INDIRECT_X, opcode_e2, opcode_e3
 	;     cpx         sbc        nop         nop

      	dc.l ZPAGE,    ZPAGE,       ZPAGE,     opcode_e7
	;     cpx        sbc          inc        illegal

      	dc.l opcode_e8, DIRECT,     opcode_ea, opcode_eb
	;      inx       sbc           nop      illegal

      	dc.l ABSOLUTE,  ABSOLUTE,   ABSOLUTE, opcode_ef
	;       cpx      sbc          inc         illegal

	dc.l opcode_f0, INDIRECT_Y, opcode_f2, opcode_f3
	;     beq          sbc        esc#ab     illegal

      	dc.l opcode_f4, ZPAGE_X,     ZPAGE_X,  opcode_f7
	;     nop          sbc        inc       illegal

      	dc.l opcode_f8, ABSOLUTE_Y, opcode_fa, opcode_fb
	;     sed         sbc         nop         illegal

      	dc.l opcode_fc, ABSOLUTE_X, ABSOLUTE_X, opcode_ff
	;     nop         sbc          inc        esc#ab




	section text

_GO: ;cycles (d0)


;  UWORD PC;
;  UBYTE S;
;  UBYTE A;
;  UBYTE X;
;  UBYTE Y;
;
;  UWORD	addr;
;  UBYTE	data;

;/*
;   This used to be in the main loop but has been removed to improve
;   execution speed. It does not seem to have any adverse effect on
;   the emulation for two reasons:-
;
;   1. NMI's will can only be raised in atari_custom.c - there is
;      no way an NMI can be generated whilst in this routine.
;
;   2. The timing of the IRQs are not that critical.
;*/

	movem.l d0-d7/a0-a6,-(a7)
	move.w 66(a7),CD ;write how much cycles (should be 6)
	lea _memory,memory_pointer
	UPDATE_LOCAL_REGS
	ConvertRegP_STATUS
	lea OPMODE_TABLE,a0
	lea _attrib,attrib_pointer
	tst.b _IRQ
	beq GET_FIRST_INSTRUCTION
	move.b _regP,d6
	and.b #I_FLAG,d6 ;is interrupt active
	bne GET_FIRST_INSTRUCTION ;yes, no other interrupt
_AIRQ:	move.w _regPC,d7     ;UAAAA :)	;write back addr
	LoHi
	move.w d7,-(stack_pointer)
	move.b _regP,-(stack_pointer)
	;PHP
	SetI
	clr.l d7
	move.w (memory_pointer,$fffe.l),d7
	LoHi
	move.l d7,PC6502
	add.l memory_pointer,PC6502
	clr.b _IRQ ;clear interrupt.....
	ifd SERVER
	move.w #-1,WAIT
	endc
	bra GET_FIRST_INSTRUCTION

END_OF_CYCLE:
	;move.l (a7)+,a1
	ConvertSTATUS_RegP
 	move.b d6,_regP
	UPDATE_GLOBAL_REGS
	movem.l (a7)+,d0-d7/a0-a6
	rts
ADRESA:	dc.l 1

  ;UPDATE_LOCAL_REGS

  ;if (IRQ)
  ;  {
  ;    if (!(regP & I_FLAG))
  ;	{
  ;	  UWORD retadr = PC;

  ;  memory[0x0100 + S--] = retadr >> 8;
  ;  memory[0x0100 + S--] = retadr & 0xff;
  ;  PHP;
  ;  SetI;
  ;  PC = (memory[0xffff] << 8) | memory[0xfffe];
  ;  IRQ = 0;
  ;	}
  ;  }



;/*
;   =====================================
;   Extract Address if Required by Opcode
;   =====================================
;*/

;d6 contains final value for use in program



ZPAGE_X
	move.l 4(a0,d7.l*8),d6
	move.b (PC6502)+,d7
	add.b X,d7
	subq.w #2,CD
	bra.s TEST


ZPAGE_Y
	move.l 4(a0,d7.l*8),d6
	move.b (PC6502)+,d7
	add.b Y,d7
	subq.w #2,CD
	bra.s TEST


ABSOLUTE_Y
	move.l 4(a0,d7.l*8),d6
	move.w (PC6502)+,d7
	ror.w #8,d7
	add.w Y,d7
	subq.w #2,CD
	bra.s TEST

INDIRECT_Y
	move.l 4(a0,d7.l*8),d6
	move.b (PC6502)+,d7
	move.w (memory_pointer,d7.l),d7
	ror.w #8,d7 ;swap bytes
	add.w Y,d7
	subq.w #4,CD
	bra.s TEST

ABSOLUTE_X
	move.l 4(a0,d7.l*8),d6
	move.w (PC6502)+,d7
	ror.w #8,d7
	add.w X,d7 ;add x to
	subq.w #2,CD
	bra.s TEST

INDIRECT_X
	move.l 4(a0,d7.l*8),d6
	move.b (PC6502)+,d7
	add.b X,d7
	move.w (memory_pointer,d7.l),d7
	ror.w #8,d7
	subq.w #4,CD
	bra.s TEST

ABSOLUTE:
	move.l 4(a0,d7.l*8),d6
	move.w (PC6502)+,d7
	ror.w #8,d7 ;d7 contains reversed value
	subq.w #2,CD
	bra.s TEST


ZPAGE:
	move.l 4(a0,d7.l*8),d6
	move.b (PC6502)+,d7 ;d6 contains offset
	subq.w #1,CD
	bra.s TEST

DIRECT:
	move.l 4(a0,d7.l*8),a1
	move.b (PC6502)+,d6
	jmp (a1)

TEST:	bclr #31,d6  ;get highest byte away
	bne.s JUST_WRITE
	move.l d6,a1
GONEBYTE:	;get one byte from memory
	tst.b (attrib_pointer,d7.l)
	bne.s JGetbyte
GZPAGEBYTE: ;get one byte from zero page
	move.b (memory_pointer,d7.l),d6 ;get byte
AFTER_READ:

JUMP_CODE:	jmp (a1)
JUST_WRITE:	move.l d6,a1
	jmp (a1) ;bra.s JUMP_CODE
JGetbyte:
	bra A800GETB


PUTONEBYTE_FL:
	move.b d6,NFLAG
	move.b d6,ZFLAG
PUTONEBYTE:	tst.b (attrib_pointer,d7.l)
	bne.s .Putbyte
	move.b d6,(memory_pointer,d7.l)
            goto NEXTCHANGE_WITHOUT

.Putbyte
	bra A800PUTB



;#define	ABSOLUTE	addr=(memory[PC+1]<<8)+memory[PC];PC+=2;
;#define	ZPAGE	addr=memory[PC++];
;#define	ABSOLUTE_X	addr=((memory[PC+1]<<8)+memory[PC])+(UWORD)X;PC+=2;
;#define 	ABSOLUTE_Y	addr=((memory[PC+1]<<8)+memory[PC])+(UWORD)Y;PC+=2;
;#define	INDIRECT_X	addr=(UWORD)memory[PC++]+(UWORD)X;addr=(memory[addr+1]<<8)+memory[addr];
;#define	INDIRECT_Y	addr=memory[PC++];addr=(memory[addr+1]<<8)+memory[addr]+(UWORD)Y;
;#define	ZPAGE_X	addr=(memory[PC++]+X)&0xff;
;#define	ZPAGE_Y	addr=(memory[PC++]+Y)&0xff;

BB


opcode_8d: ;/* STA abcd */
opcode_85: ;/* STA ab */
opcode_81: ;/* STA (ab,x) */
opcode_91: ;/* STA (ab),y */
opcode_95: ;/* STA ab,x */
opcode_99: ;/* STA abcd,y */
opcode_9d: ;/* STA abcd,x */
	move.b A,d6
	bra.s PUTONEBYTE
opcode_8e: ;/* STX abcd */
opcode_86: ;/* STX ab */
opcode_96: ;/* STX ab,y */
	move.b X,d6
	bra.s PUTONEBYTE
opcode_8c: ;/* STY abcd */
opcode_84: ;/* STY ab */
opcode_94: ;/* STY ab,x */
	move.b Y,d6
	bra.s PUTONEBYTE


opcode_e6: ;/* INC ab */
opcode_ee: ;/* INC abcd */
opcode_f6: ;/* INC ab,x */
opcode_fe: ;/* INC abcd,x */
	addq.b #2,d6 ;again funny piece of code

opcode_c6: ;/* DEC ab */
opcode_ce: ;/* DEC abcd */
opcode_d6: ;/* DEC ab,x */
opcode_de: ;/* DEC abcd,x */
	subq.b #1,d6
	bra.s PUTONEBYTE_FL ;put one byte with flags

opcode_0e: ;/* ASL abcd */
opcode_06: ;/* ASL ab */
opcode_16: ;/* ASL ab,x */
opcode_1e: ;/* ASL abcd,x */
	clr.w CCR6502
opcode_2e: ;/* ROL abcd */
opcode_36: ;/* ROL ab,x */
opcode_26: ;/* ROL ab */
opcode_3e: ;/* ROL abcd,x */
	move.w CCR6502,CCR
	addx.b d6,d6 ;left
	move.w CCR,CCR6502
	bra.s PUTONEBYTE_FL

opcode_46: ;/* LSR ab */
opcode_4e: ;/* LSR abcd */
opcode_56: ;/* LSR ab,x */
opcode_5e: ;/* LSR abcd,x */
	clr.w CCR6502

opcode_6e: ;/* ROR abcd */
opcode_66: ;/* ROR ab */
opcode_76: ;/* ROR ab,x */
opcode_7e: ;/* ROR abcd,x */
	move.w CCR6502,CCR
	roxr.b #1,d6
	move.w CCR,CCR6502
	bra.s PUTONEBYTE_FL

opcode_a9: ;/* LDA #ab */
opcode_a5: ;/* LDA ab */
opcode_a1: ;/* LDA (ab,x) */
opcode_ad: ;/* LDA abcd */
opcode_b1: ;/* LDA (ab),y */
opcode_b5: ;/* LDA ab,x */
opcode_b9: ;/* LDA abcd,y */
opcode_bd: ;/* LDA abcd,x */
	move.b d6,A
	bra.s NEXTCHANGE


opcode_a2: ;/* LDX #ab */
opcode_a6: ;/* LDX ab */
opcode_ae: ;/* LDX abcd */
opcode_b6: ;/* LDX ab,y */
opcode_be: ;/* LDX abcd,y */
	move.b d6,X
	bra.s NEXTCHANGE

opcode_a0: ;/* LDY #ab */
opcode_a4: ;/* LDY ab */
opcode_ac: ;/* LDY abcd */
opcode_b4: ;/* LDY ab,x */
opcode_bc: ;/* LDY abcd,x */
	move.b d6,Y
	bra.s NEXTCHANGE


opcode_e0: ;/* CPX #ab */
opcode_e4: ;/* CPX ab */
opcode_ec: ;/* CPX abcd */
	move.b X,NFLAG
	bra.s COMPARE

opcode_c0: ;/* CPY #ab */
opcode_c4: ;/* CPY ab */
opcode_cc: ;/* CPY abcd */
	move.b Y,NFLAG
	bra.s COMPARE

opcode_c1: ;/* CMP (ab,x) */
opcode_c5: ;/* CMP ab */
opcode_c9: ;/* CMP #ab */
opcode_cd: ;/* CMP abcd */
opcode_d1: ;/* CMP (ab),y */
opcode_d5: ;/* CMP ab,x */
opcode_d9: ;/* CMP abcd,y */
opcode_dd: ;/* CMP abcd,x */
	move.b A,NFLAG

COMPARE:	sub.b d6,NFLAG
	move.w CCR,CCR6502
	not.w CCR6502
	move.b NFLAG,ZFLAG
	bra.s NEXTCHANGE_WITHOUT ;without flags


opcode_29: ;/* AND #ab */
opcode_21: ;/* AND (ab,x) */
opcode_25: ;/* AND ab */
opcode_2d: ;/* AND abcd */
opcode_31: ;/* AND (ab),y */
opcode_35: ;/* AND ab,x */
opcode_39: ;/* AND abcd,y */
opcode_3d: ;/* AND abcd,x */
	and.b d6,A
	bra.s NEXTCHANGE_A ;change flags as A is set

opcode_49: ;/* EOR #ab */
opcode_41: ;/* EOR (ab,x) */
opcode_45: ;/* EOR ab */
opcode_4d: ;/* EOR abcd */
opcode_51: ;/* EOR (ab),y */
opcode_55: ;/* EOR ab,x */
opcode_59: ;/* EOR abcd,y */
opcode_5d: ;/* EOR abcd,x */
	eor.b d6,A
	bra.s NEXTCHANGE_A

opcode_09: ;/* ORA #ab */
opcode_01: ;/* ORA (ab,x) */
opcode_05: ;/* ORA ab */
opcode_0d: ;/* ORA abcd */
opcode_11: ;/* ORA (ab),y */
opcode_15: ;/* ORA ab,x */
opcode_19: ;/* ORA abcd,y */
opcode_1d: ;/* ORA abcd,x */
	or.b d6,A



;MAIN LOOP , where we are counting cycles and working with other STUFF


NEXTCHANGE_A: move.b A,d6
NEXTCHANGE:	move.b d6,ZFLAG
	move.b d6,NFLAG
NEXTCHANGE_WITHOUT:

	ifd C_BUGGING
	tst.w CD
	bmi ENDGO ;should be .S
	endc

	ifd SERVER
	UPDATE_GLOBAL_REGS
	ConvertSTATUS_RegP
	move.b d6,_regP
	tst.w WAIT
	bne STRAIGHT_GO ;if interrupt is active we won't test and will run until the end of interrupt is reached
	lea $400000,a1
.WAIT_FOR_DATA:
	cmp.l #$12345678,-16(a1)
	bne.s .WAIT_FOR_DATA
	move.b _regP,d6
	cmp.b -1(a1),d6
	bne.s .PRINT_OUT
	move.b _regS,d6
	cmp.b -2(a1),d6
	bne.s .PRINT_OUT
	move.b _regA,d6
	cmp.b -3(a1),d6
	bne.s .PRINT_OUT
	move.b _regX,d6
	cmp.b -4(a1),d6
	bne.s .PRINT_OUT
	move.b _regY,d6
	cmp.b -5(a1),d6
	bne.s .PRINT_OUT
	move.w _regPC,d6
	cmp.w -8(a1),d6
	bne.s .PRINT_OUT
	bra CONTINUE


.PRINT_OUT:

      movem.l a0-a6,-(a7)
      CPU_GetStatus
      jsr _tisk /*in atari c*/
      CPU_PutStatus
      movem.l (a7)+,a0-a6

CONTINUE:   lea $400000,a1
	move.l #'A800',-16(a1)

STRAIGHT_GO: ;interrupt is active, dont try trace....
      UPDATE_LOCAL_REGS
	endc

	ifd C_BUGGING
	move.l PC6502,A1
	sub.l a5,a1
	cmp.l _CEKEJ,a1
	bne.s CONT
_ADRESA:	nop
	ifd DEBUG
	illegal
	endc
	endc

CONT:	;move.l a1,-(a7)
            subq.w #2,CD
	tst.w CD
	bmi.s ENDGO
GET_FIRST_INSTRUCTION:
****************************************
	ifd BREAKING		following block of code allows you to enter
	move ccr,-(sp)		a break address in monitor
	move.l PC6502,d7
	sub.l memory_pointer,d7
	cmp.w _break_addr,d7
	bne.s   .get_first
	move.b #$ff,d7
	move (sp)+,ccr
          bsr odskoc_si		on break monitor is invoked
	move ccr,-(sp)

.get_first
	move (sp)+,ccr
	endc
****************************************
	clr.l d7
	move.b (PC6502)+,d7
	;move.w 4(a0,d7.l*8),d6
JUMPMODE:	jmp ([a0,d7.l*8])
ENDGO: 	bra END_OF_CYCLE  ;the end of cycle

opcode_0a: ;/* ASLA */
	clr.w CCR6502

opcode_2a: ;/* ROLA */
	move.w CCR6502,CCR
	addx.b A,A  ;rolx #1,A
	move.w CCR,CCR6502
	bra NEXTCHANGE_A

opcode_4a: ;/* LSRA */
	clr.w CCR6502

opcode_6a: ;/* RORA */
	move.w CCR6502,CCR
	roxr.b #1,A
	move.w CCR,CCR6502
	bra NEXTCHANGE_A


opcode_90: ;/* BCC */
	btst #EB68,CCR6502
	beq.s SOLVE
	bra.s NOTHING

opcode_b0: ;/* BCS */
	btst #EB68,CCR6502
	bne.s SOLVE
	bra.s NOTHING


opcode_f0: ;/* BEQ */
	tst.b ZFLAG
	beq.s SOLVE
	bra.s NOTHING

opcode_d0: ;/* BNE */
	tst.b ZFLAG
	beq.s NOTHING

SOLVE:
	move.b (PC6502)+,d7
      	extb.l d7
      	add.l d7,PC6502
      	;lea (PC6502,d7.l),PC6502
      	bra NEXTCHANGE_WITHOUT
NOTHING:	addq.l #1,PC6502
	bra NEXTCHANGE_WITHOUT


opcode_ca: ;/* DEX */  ;funny code DEX(-2)+1
	subq.b #2,X

opcode_e8: ;/* INX */
	addq.b #1,X
	move.b X,d6
	bra NEXTCHANGE

opcode_88: ;/* DEY */ ;funny code (DEY(-2)+1
	subq.b #2,Y

opcode_c8: ;/* INY */
	addq.b #1,Y
	move.b Y,d6
	bra NEXTCHANGE

opcode_a8: ;/* TAY */
      move.b A,Y
opcode_98: ;/* TYA */
      move.b Y,A
      bra NEXTCHANGE_A

opcode_aa: ;/* TAX */
      move.b A,X
opcode_8a: ;/* TXA */
      move.b X,A
      bra NEXTCHANGE_A

opcode_20: ;/* JSR abcd */
      move.l PC6502,d7 ;current pointer
      sub.l memory_pointer,d7
      addq.l #1,d7 ;back addres
      LoHi ;(it puts result into d6)
      move.w d7,-(stack_pointer)
opcode_4c: ;/* JMP abcd */
      move.w (PC6502)+,d7
      LoHi ;(in d7 adress where we want to jump)
      lea (memory_pointer,d7.l),PC6502
      ;add.l d7,PC6502
      subq.w #4,CD
      goto NEXTCHANGE_WITHOUT


opcode_60: ;/* RTS */
      move.w (stack_pointer)+,d7
      LoHi
      lea 1(memory_pointer,d7.l),PC6502
      ;addq.l #1,PC6502
      subq.w #4,CD
      goto NEXTCHANGE_WITHOUT

opcode_50: ;/* BVC */
	btst #V_FLAGB,_regP
	beq.s SOLVE
	bra.s NOTHING

opcode_70: ;/* BVS */
	btst #V_FLAGB,_regP
	bne.s SOLVE
	bra.s NOTHING

opcode_10: ;/* BPL */
	and.b #N_FLAG,NFLAG
	beq.s SOLVE
	bra.s NOTHING

opcode_30: ;/* BMI */
	and.b #N_FLAG,NFLAG
	bne.s SOLVE
	bra.s NOTHING


opcode_24: ;/* BIT ab */
opcode_2c: ;/* BIT abcd */
	move.b d6,NFLAG
	btst #V_FLAGB,d6
	beq.s .UNSET
	bset #V_FLAGB,_regP
	move.b A,ZFLAG
	and.b NFLAG,ZFLAG
	bra NEXTCHANGE_WITHOUT
.UNSET:
	bclr #V_FLAGB,_regP
	move.b A,ZFLAG
	and.b NFLAG,ZFLAG
	bra NEXTCHANGE_WITHOUT



opcode_40: ;/* RTI */
_RTI:
      PLP
      move.w (stack_pointer)+,d7
      LoHi
      lea (memory_pointer,d7.l),PC6502
      subq.w #4,CD
      ifd SERVER
      clr.w WAIT
      endc
      goto NEXTCHANGE_WITHOUT


opcode_61: ;/* ADC (ab,x) */
opcode_65: ;/* ADC ab */
opcode_69: ;/* ADC #ab */
opcode_6d: ;/* ADC abcd */
opcode_71: ;/* ADC (ab),y */
opcode_75: ;/* ADC ab,x */
opcode_79: ;/* ADC abcd,y */
opcode_7d: ;/* ADC abcd,x */
adc:

;/* ADC */
;    unsigned int temp = src + AC + (IF_CARRY() ? 1 : 0);
;    SET_ZERO(temp & 0xff);	/* This is not valid in decimal mode */
;    if (IF_DECIMAL()) {
;        if (((AC & 0xf) + (src & 0xf) + (IF_CARRY() ? 1 : 0)) > 9) temp += 6;
;	SET_SIGN(temp);
;	SET_OVERFLOW(!((AC ^ src) & 0x80) && ((AC ^ temp) & 0x80));
;	if (temp > 0x99) temp += 96;
;	SET_CARRY(temp > 0x99);
;    } else {
;	SET_SIGN(temp);
;	SET_OVERFLOW(!((AC ^ src) & 0x80) && ((AC ^ temp) & 0x80));
;	SET_CARRY(temp > 0xff);
;    }
;    AC = ((BYTE) temp);

	;addq.l #1,AAAAA
     	btst #D_FLAGB,_regP
	bne.s BDC_ADC

	move.w CCR6502,CCR
	addx.b d6,A ;data are added with carry (in 68000 Extended bit in this case)
	move.w CCR,CCR6502
	svs d6
	and.b #V_FLAG,d6
*	and.b #-V_FLAG,_regP
	and.b #~V_FLAG,_regP
	or.b d6,_regP
	bra NEXTCHANGE_A


	;if (!(regP & D_FLAG))
	;{
	;  UWORD	temp;
            ;  UWORD t_data;

            ;t_data = (UWORD)data + (UWORD)C;
	;Z = N = temp = (UWORD)A + t_data;
            ;V = (~(A ^ t_data)) & (Z ^ A) & 0x80; ;cruel construction :((

	;C = temp >> 8;
	;A = Z;
	;}
BDC_ADC:
	move.b CCR6502,CCR
	ori.b #4,CCR ; set zero flag
	abcd d6,A
	move.b CCR,CCR6502  ;V flag isn't soluted
	bra NEXTCHANGE_A


      	;else
	;{
	; int	bcd1, bcd2;

	; bcd1 = BCDtoDEC[A];
	; bcd2 = BCDtoDEC[data];

	; bcd1 += bcd2 + C;

	; Z = N = DECtoBCD[bcd1];

	; V = (Z ^ A) & 0x80;
	; C = (bcd1 > 99);
	; A = Z;
	;}
	;goto next;


opcode_e1: ;/* SBC (ab,x) */
opcode_e5: ;/* SBC ab */
opcode_e9: ;/* SBC #ab */
opcode_ed: ;/* SBC abcd */
opcode_f1: ;/* SBC (ab),y */
opcode_f5: ;/* SBC ab,x */
opcode_f9: ;/* SBC abcd,y */
opcode_fd: ;/* SBC abcd,x */

sbc:
;/* SBC */
;    unsigned int temp = AC - src - (IF_CARRY() ? 0 : 1);
;    SET_SIGN(temp);
;    SET_ZERO(temp & 0xff);	/* Sign and Zero are invalid in decimal mode */
;    SET_OVERFLOW(((AC ^ temp) & 0x80) && ((AC ^ src) & 0x80));
;    if (IF_DECIMAL()) {
;	if ( ((AC & 0xf) - (IF_CARRY() ? 0 : 1)) < (src & 0xf)) /* EP */ temp -= 6;
;	if (temp > 0x99) temp -= 0x60;
;    }
;    SET_CARRY(temp < 0x100);
;    AC = (temp & 0xff);



 	;addq.l #1,AAAAA
	btst #D_FLAGB,_regP
	bne.s BDC_SBC

	not.w CCR6502 ;change it????
	move.w CCR6502,CCR
	subx.b d6,A
	move.w CCR,CCR6502
	svs d6
	not.w CCR6502
	and.b #V_FLAG,d6
*	and.b #-V_FLAG,_regP
	and.b #~V_FLAG,_regP
	or.b d6,_regP
	bra NEXTCHANGE_A


      	;if (!(regP & D_FLAG))
	;{
	; UWORD	temp;
            ; UWORD t_data;

            ;t_data = (UWORD)data + (UWORD)!C;
	;temp = (UWORD)A - t_data;

	;Z = N = temp & 0xff;


;/*
; * This was the old code that I have been using upto version 0.5.2
; *  C = (A < ((UWORD)data + (UWORD)!C)) ? 0 : 1;
; */
;	  V = (~(A ^ t_data)) & (Z ^ A) & 0x80;
;	  C = (temp > 255) ? 0 : 1;
;	  A = Z;
;	}

BDC_SBC:
	not.w CCR6502 ;change it ?????
	move.b CCR6502,CCR
	ori.b #4,CCR ; set zero flag
	sbcd d6,A
	move.b CCR,CCR6502  ;V flag isn't soluted
	not.w CCR6502
	goto NEXTCHANGE_A


      	;else
	;{
	;  int	bcd1, bcd2;
	;
	;  bcd1 = BCDtoDEC[A];
	;  bcd2 = BCDtoDEC[data];

	;  bcd1 = bcd1 - bcd2 - !C;

	;  if (bcd1 < 0) bcd1 = 100 - (-bcd1);
	;  Z = N = DECtoBCD[bcd1];

	;  C = (A < (data + !C)) ? 0 : 1;
	;  V = (Z ^ A) & 0x80;
	;  A = Z;
	;}
      	;goto next;


opcode_68: ;/* PLA */
      move.b (stack_pointer)+,A
      subq.w #1,CD
      bra NEXTCHANGE_A


opcode_08: ;/* PHP */
      PHP
      subq.w #1,CD
      bra NEXTCHANGE_WITHOUT


opcode_48: ;/* PHA */
      move.b A,-(stack_pointer)
      subq.w #1,CD
      bra NEXTCHANGE_WITHOUT



opcode_28: ;/* PLP */
      PLP
      subq.w #1,CD
	bra NEXTCHANGE_WITHOUT

opcode_18: ;/* CLC */
      	clr.w CCR6502
	bra NEXTCHANGE_WITHOUT

opcode_38: ;/* SEC */
	moveq.w #-1,CCR6502
	bra NEXTCHANGE_WITHOUT

opcode_58: ;/* CLI */
	ClrI
	bra NEXTCHANGE_WITHOUT


opcode_d8: ;/* CLD */
	ClrD
	bra NEXTCHANGE_WITHOUT

opcode_b8: ;/* CLV */
	bclr.b #V_FLAGB,_regP
	bra NEXTCHANGE_WITHOUT

opcode_6c: ;/* JMP (abcd) */
      	move.w (PC6502)+,d7
      	LoHi
      	move.w (memory_pointer,d7.l),d7
      	LoHi
      	lea (memory_pointer,d7.l),PC6502
      	subq.w #5,CD
	bra NEXTCHANGE_WITHOUT

opcode_78: ;/* SEI */
      	SetI
	bra NEXTCHANGE_WITHOUT

opcode_f8: ;/* SED */
      	SetD
	bra NEXTCHANGE_WITHOUT

opcode_9a: ;/* TXS */
      	lea (memory_pointer,X.w),stack_pointer
      	lea $101(stack_pointer),stack_pointer
	bra NEXTCHANGE_WITHOUT

opcode_ba: ;/* TSX */
      	move.l stack_pointer,X
      	sub.l memory_pointer,X
      	sub.l #$101,X ;HIHIHI :) 3rd discovered error in last 30 hours....
      	       ;this one is because logic of STACK on 6502 4th error in 32 hours :)
	move.b X,d6
      	bra NEXTCHANGE



opcode_0c: ;/* NOP (3 bytes) [unofficial] */
opcode_3c:
opcode_5c:
opcode_7c:
opcode_dc:
opcode_fc:

      addq.l #1,PC6502
      subq.w #3,CD



opcode_04: ;/* NOP (2 bytes) [unofficial] */
opcode_14:
opcode_1c:
opcode_34:
opcode_44:
opcode_54:
opcode_64:
opcode_74:
opcode_80:
opcode_82:
opcode_89:
opcode_c2:
opcode_d4:
opcode_e2:
opcode_f4:

      addq.l #1,PC6502
      subq.w #3,CD

opcode_1a: ;/* NOP (1 byte) [unofficial] */
opcode_3a:
opcode_5a:
opcode_7a:
opcode_da:
opcode_fa:
opcode_ea: ;/* NOP */ ;oficial
	goto NEXTCHANGE_WITHOUT

opcode_d2: ;/* ESCRTS #ab (JAM) */
      move.b (PC6502)+,d7
      UPDATE_GLOBAL_REGS
      movem.l a0-a6,-(a7)
      CPU_GetStatus
      move.l d7,-(a7)
      jsr _Escape /*in atari c*/
      addq.l #4,a7
      CPU_PutStatus
      movem.l (a7)+,a0-a6
      UPDATE_LOCAL_REGS
      move.w (stack_pointer)+,d7
      LoHi
      lea (memory_pointer,d7.l),PC6502
      addq.l #1,PC6502
      bra GET_FIRST_INSTRUCTION

opcode_f2: ;/* ESC #ab (JAM) */
opcode_ff: ;/* ESC #ab */
      move.b (PC6502)+,d7
      UPDATE_GLOBAL_REGS
      movem.l a0-a6,-(a7)
      CPU_GetStatus
      move.l d7,-(a7)
      jsr _Escape
      addq.l #4,a7
      CPU_PutStatus
      movem.l (a7)+,a0-a6
      UPDATE_LOCAL_REGS
      bra GET_FIRST_INSTRUCTION




opcode_a3: ;/* LAX (ind,x) [unofficial] */
      ;INDIRECT_X
      ;GetByte d7
      move.b d7,A
      move.b d7,X
      move.b A,NFLAG
      move.b A,ZFLAG
      bra NEXTCHANGE_WITHOUT




opcode_a7: ;/* LAX zpage [unofficial] */
      ;ZPAGE
      ;GetByte d7
      move.b d7,A
      move.b d7,X
      move.b A,NFLAG
      move.b A,ZFLAG
      bra NEXTCHANGE_WITHOUT





opcode_af: ;/* LAX absolute [unofficial] */
      ;ABSOLUTE
      ;GetByte d7
      move.b d7,X
      move.b d7,A
      move.b A,NFLAG
      move.b A,ZFLAG
      bra NEXTCHANGE_WITHOUT



opcode_b3: ;/* LAX (ind),y [unofficial] */
      ;INDIRECT_Y
      ;GetByte d7
      move.b d7,A
      move.b d7,X
      move.b A,NFLAG
      move.b A,ZFLAG
      bra NEXTCHANGE_WITHOUT



opcode_b7: ;/* LAX zpage,y [unofficial] */
      ;ZPAGE_Y
      ;GetByte d7
      move.b d7,A
      move.b d7,X
      move.b A,NFLAG
      move.b A,ZFLAG
      bra NEXTCHANGE_WITHOUT

opcode_bf: ;/* LAX absolute,y [unofficial] */
      ;ABSOLUTE_Y
      ;GetByte d7
      move.b d7,A
      move.b d7,X
      move.b X,ZFLAG
      move.b X,NFLAG
      bra NEXTCHANGE_WITHOUT









opcode_02:
opcode_03:
opcode_07:
opcode_0b:
opcode_0f:
opcode_12:
opcode_13:
opcode_17:
opcode_1b:
opcode_1f:
opcode_22:
opcode_23:
opcode_27:
opcode_2b:
opcode_2f:
opcode_32:
opcode_33:
opcode_37:
opcode_3b:
opcode_3f:
opcode_42:
opcode_43:
opcode_47:
opcode_4b:
opcode_4f:
opcode_52:
opcode_53:
opcode_57:
opcode_5b:
opcode_5f:
opcode_62:
opcode_63:
opcode_67:
opcode_6b:
opcode_6f:
opcode_72:
opcode_73:
opcode_77:
opcode_7b:
opcode_7f:
opcode_83:
opcode_87:
opcode_8b:
opcode_8f:
opcode_92:
opcode_93:
opcode_97:
opcode_9b:
opcode_9c:
opcode_9e:
opcode_9f:
opcode_ab:
opcode_b2:
opcode_bb:
opcode_c3:
opcode_c7:
opcode_cb:
opcode_cf:
opcode_d3:
opcode_d7:
opcode_db:
opcode_df:
opcode_e3:
opcode_e7:
opcode_eb:
opcode_ef:
opcode_f3:
opcode_f7:
opcode_fb:
	;pea text(pc)
	;move.w #9,-(a7)
	;trap #1
	;addq.l #1,ILEGALY
	;illegal
	bra NEXTCHANGE_WITHOUT
ILEGALY:	dc.l 0
text:	dc.b 'NEJAKA CHYBA TY BLBE'
	even

UPDATE_GLOBAL_REGS

      ;printf ("*** Invalid Opcode %02x at address %04x\n",
      ;	      memory[PC-1], PC-1);
      ;ncycles = 0;
      ;break;

opcode_00: ;/* BRK */
      btst #I_FLAGB,_regP
      bne NEXTCHANGE_WITHOUT
      move.l PC6502,d7
      sub.l memory_pointer,d7
      addq.w #1,d7
      LoHi
      move.w d7,-(stack_pointer)
      SetB
      PHP
      SetI
      move.w (memory_pointer,$fffe.l),d7
      LoHi
      move.l d7,PC6502
      add.l memory_pointer,PC6502
      subq.w #5,CD
      bra NEXTCHANGE_WITHOUT



A800GETB:
	cmp.b #1,(attrib_pointer,d7.l)
	beq.s .READ_FROM_ROM
	movem.l a0/a1/a6/d0/d1,-(a7)
	move.l d7,-(a7)
	jsr _GETBYTE
	move.w d0,d6 ;put stack onto right place
	addq.l #4,a7
	movem.l (a7)+,a0/a1/a6/d0/d1
	bra AFTER_READ

.READ_FROM_ROM:
	move.b (memory_pointer,d7.l),d6
	bra AFTER_READ

	ifd DEBUG
_GETBYTE:	move.b (memory_pointer,d7.l),d6
	rts
	endc

; I used to be really stupid about stacking in C :))
; I believed that I have to push onto stack every time I call
; C function all registers, Fortunately I don't have to, d0-d1 and a0-a1 is enough

A800PUTB:
	cmp.b #1,(attrib_pointer,d7.l)
	beq.s .DONT_WRITE_TO_ROM

	move.l a0,-(a7)
	move.w d0,-(a7)
	move.w d1,-(a7)
	move.w a6,-(a7)
	;movem.l a0/a6/d0/d1,-(a7)
	clr.l -(a7)
	move.b d6,3(a7) ;byte
	move.l d7,-(a7) ;adress
	jsr _PUTBYTE
	move.w d0,d6 ;put value onto right place
	addq.l #8,a7
	;movem.l (a7)+,a0/a6/d0/d1
	move.w (a7)+,a6
	move.w (a7)+,d1
	move.w (a7)+,d0
	move.l (a7)+,a0
	tst.w d6
	bne ENDGO
	bra NEXTCHANGE_WITHOUT

.DONT_WRITE_TO_ROM:
	bra NEXTCHANGE_WITHOUT

	ifd DEBUG
_PUTBYTE:	move.b d6,(memory_pointer,d7.l)
	endc

odskoc_si:
      UPDATE_GLOBAL_REGS
      movem.l a0-a6,-(a7)
      CPU_GetStatus
      move.l d7,-(a7)
      jsr _Escape
      addq.l #4,a7
      CPU_PutStatus
      movem.l (a7)+,a0-a6
      UPDATE_LOCAL_REGS
      rts

	ifd DEBUG
_CEKEJ:	dc.l 1
	endc

EE:	dc.w 0
WAIT:	dc.w 0

	section bss
_OPMODE_TABLE:
	ds.w 1
OPMODE_TABLE: ds.l 514 (2*256 long words)+2 as a reserve


	ifnd DEBUG
LE:	ds.l 1
STACKALL:   ds.l 16
	endc

	ifd DEBUG
	section data
;VBDIFLAG:	dc.w 1 ;information about CCR bits V B D I
	dc.l 0
	dc.l 0 ;
mem6502:	;dc.l MEMORY

	;ds.w 1
memory:
_memory:

	ds.b 40960
	incbin H:\ATARI800\ATARIBAS.ROM ;load basic
	incbin H:\ATARI800\ATARIXL.ROM  ;load Atari XL

	ds.b 8192
	dc.b $a9,50,$69,90,$e9,40,$20,$4c,00,20

	dc.b $a9,$00,$a2,0,$a0,0
	;lda #0, ldx #0, ldy #0
	dc.b $a5,$06,$ae,$1,$1,$bc,2,2
	;lda $06 ldx $0101 ldy $0202,x
	dc.b $a1,02,$a6,2,$b4,2
	;lda (2,x) ldx 2 ldy 2,x
	dc.b $81,0,$8e,01,01,$94,22
	;   sta (0,x) stx $0101 sty 22,y
	dc.b $e6,10,$6e,05,05,$e0,10
	;   INC 10, ROR 0505 CPX #10
	dc.b $4c,0,$20
;PL:	ds.b 65536-(PL-memory)


attrib:
_attrib:	dcb.b $A000,RAM
	dcb.b  $2000,ROM
	dcb.b  $1000,HARDWARE
	dcb.b  $3000,ROM
	;dc.b ROM
	;dc.b ROM
	;dc.b ROM
	;dcb.b 16383,RAM
	;65536,RAM ;set all the memory as RAM

	section bss
TEST2:	ds.l 20000
	section text
_Escape:	rts
	endc
	section text