cardinal-uxn 0.1.0

Uxn CPU interpreter
Documentation
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
// x0 - stack pointer (&mut [u8; 256])
// x1 - stack index (u8)
// x2 - return stack pointer (&mut [u8; 256])
// x3 - return stack index (u8)
// x4 - RAM pointer (&mut [u8; 65536])
// x5 - program counter (u16), offset of the next value in RAM
// x6 - VM pointer (&mut Uxn)
// x7 - Device handle pointer (&DeviceHandle)
// x8 - Jump table pointer (loaded in aarch64_entry)
// x9-15 - scratch registers
//
// We do not use any callee-saved registers (besides x29 / x30)
.macro next
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    ldr x10, [x8, x9, lsl #3]
    br x10
.endm

.macro pop
    sub x1, x1, #1
    and x1, x1, #0xff
.endm

.macro rpop
    sub x3, x3, #1
    and x3, x3, #0xff
.endm

.macro push, reg
    add x1, x1, #1
    and x1, x1, #0xff
    strb \reg, [x0, x1]
.endm

.macro rpush, reg
    add x3, x3, #1
    and x3, x3, #0xff
    strb \reg, [x2, x3]
.endm

.macro peek, w, x, n
    sub \x, x1, \n
    and \x, \x, #0xff
    ldrb \w, [x0, \x]
.endm

.macro rpeek, w, x, n
    sub \x, x3, \n
    and \x, \x, #0xff
    ldrb \w, [x2, \x]
.endm

.macro precall
    // We have to write our stack index pointers back into the &mut Uxn
    ldp x11, x12, [sp, 0x10] // restore stack index pointers
    strb w1, [x11]   // modify stack index pointer
    strb w3, [x12]   // modify return stack index pointer

    stp x0, x1, [sp, #0x20]
    stp x2, x3, [sp, #0x30]
    stp x5, x4, [sp, #0x40]
    stp x6, x7, [sp, #0x50]
    str x8,     [sp, #0x60]

    mov x0, x6
    mov x1, x7
.endm

.macro postcall
    ldp x0, x1, [sp, #0x20]
    ldp x2, x3, [sp, #0x30]
    ldp x5, x4, [sp, #0x40]
    ldp x6, x7, [sp, #0x50]
    ldr x8,     [sp, #0x60]

    // The DEO operation may have changed stack pointers, so reload them here
    ldp x11, x12, [sp, 0x10]
    ldrb w1, [x11]
    ldrb w3, [x12]
.endm

ENTRY aarch64_entry
    sub sp, sp, #0x200          // make room in the stack
    stp   x29, x30, [sp, 0x0]   // store stack and frame pointer
    mov   x29, sp
    load_jump_table x8 // platform-dependent

    // Convert from index pointers to index values in w1 / w3
    stp x1, x3, [sp, 0x10]      // save stack index pointers
    ldrb w1, [x1]               // load stack index
    ldrb w3, [x3]               // load ret index

    // Jump into the instruction list
    next

_BRK:
    // Write index values back through index pointers
    ldp x9, x10, [sp, 0x10]     // restore stack index pointers
    strb w1, [x9]               // save stack index
    strb w3, [x10]              // save ret index

    ldp   x29, x30, [sp, 0x0]   // Restore stack and frame pointer
    add sp, sp, #0x200  // restore stack pointer

    mov x0, x5 // return PC from function
    ret

_INC:
    ldrb w9, [x0, x1]
    add w9, w9, #1
    strb w9, [x0, x1]
    next

_POP:
    pop
    next

_NIP:
    ldrb w9, [x0, x1]   // get the top byte
    pop
    strb w9, [x0, x1]   // overwrite the previous byte
    next

_SWP:
    ldrb w10, [x0, x1]   // get the top byte
    peek w11, x9, 1      // get the second-from-top byte
    strb w10, [x0, x9]   // do the swap!
    strb w11, [x0, x1]
    next

_ROT:
    // a b c -- b c a
    ldrb w10, [x0, x1] // c
    peek w12, x11, 1
    peek w14, x13, 2

    strb w14, [x0, x1]
    strb w12, [x0, x13]
    strb w10, [x0, x11]

    next

_DUP:
    ldrb w10, [x0, x1]   // get the top byte
    push w10
    next

_OVR:
    peek w10, x10, 1
    push w10
    next

.macro compare_op op
    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    cmp w11, w10
    cset w10, \op
    strb w10, [x0, x1]
    next
.endm

_EQU:
    compare_op eq

_NEQ:
    compare_op ne

_GTH:
    compare_op hi

_LTH:
    compare_op lo

_JMP:
    ldrsb x9, [x0, x1]
    pop
    add x5, x5, x9
    and x5, x5, 0xffff
    next

_JCN:
    ldrsb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    cmp w10, #0
    csel w10, wzr, w9, eq // choose the jump or not
    add x5, x5, x10 // jump or not
    and x5, x5, 0xffff
    next

_JSR:
    ldrsb w9, [x0, x1]
    pop
    lsr w10, w5, 8
    rpush w10
    rpush w5
    add x5, x5, x9
    and x5, x5, 0xffff
    next

_STH:
    ldrb w9, [x0, x1]
    pop
    rpush w9
    next

_LDZ:
    ldrb w9, [x0, x1]
    pop
    ldrb w9, [x4, x9]
    push w9
    next

_STZ:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    strb w10, [x4, x9]
    next

_LDR:
    ldrsb w9, [x0, x1]
    add x9, x5, x9
    and x9, x9, #0xffff
    ldrb w9, [x4, x9] // read from RAM
    strb w9, [x0, x1] // push to stack
    next

_STR:
    ldrsb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    add x9, x5, x9
    and x9, x9, #0xffff
    strb w10, [x4, x9] // write to RAM
    next

_LDA:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    orr w12, w9, w10, lsl #8
    ldrb w12, [x4, x12]
    strb w12, [x0, x1]
    next

_STA:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    orr w12, w9, w10, lsl #8
    ldrb w10, [x0, x1]
    pop
    strb w10, [x4, x12]
    next

_DEI:
    precall
    CALL dei_entry
    postcall
    next

_DEO:
    precall
    CALL deo_entry // todo check return value for early exit?
    postcall
    next

.macro binary_op op
    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    \op w10, w11, w10
    strb w10, [x0, x1]
    next
.endm

_ADD:
    binary_op add

_SUB:
    binary_op sub

_MUL:
    binary_op mul

_DIV:
    binary_op udiv

_AND:
    binary_op and

_ORA:
    binary_op orr

_EOR:
    binary_op eor

_SFT:
    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    lsr w12, w10, 4
    and w10, w10, #0xf
    lsr w11, w11, w10
    lsl w11, w11, w12
    strb w11, [x0, x1]
    next

_JCI:
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    ldrb w10, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    orr w12, w10, w9, lsl #8 // build the jump offset
    ldrb w10, [x0, x1] // read conditional byte
    pop
    cmp w10, #0
    csel w10, wzr, w12, eq // choose the jump or not
    add x5, x5, x10 // jump or not
    and x5, x5, 0xffff
    next

_INC2:
    ldrb w10, [x0, x1]  // get the top byte
    peek w11, x9, 1     // get the second-from-top byte
    orr w12, w10, w11, lsl #8
    add w12, w12, #1
    and w12, w12, #0xffff
    strb w12, [x0, x1]
    lsr w12, w12, 8
    strb w12, [x0, x9]
    next

_POP2:
    sub x1, x1, #2
    and x1, x1, #0xff
    next

_NIP2:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    strb w9, [x0, x1]
    sub x11, x1, #1
    and x11, x11, #0xff
    strb w10, [x0, x11]
    next

_SWP2:
    ldrb w11, [x0, x1]   // get the top byte
    peek w12, x9, 2       // get the second-from-top byte
    strb w11, [x0, x9]   // do the swap!
    strb w12, [x0, x1]

    peek w11, x9, 1
    peek w12, x10, 3
    strb w11, [x0, x10]
    strb w12, [x0, x9]

    next

_ROT2:
    ldrb w10, [x0, x1]
    peek w12, x11, 2
    peek w14, x13, 4
    strb w14, [x0, x1]
    strb w12, [x0, x13]
    strb w10, [x0, x11]

    peek w10, x15, 1
    peek w12, x11, 3
    peek w14, x13, 5
    strb w14, [x0, x15]
    strb w12, [x0, x13]
    strb w10, [x0, x11]

    next

_DUP2:
    ldrb w11, [x0, x1]
    peek w10, x10, 1
    push w10
    push w11
    next

_OVR2:
    peek w10, x9, 2
    peek w11, x9, 3
    push w11
    push w10
    next

.macro compare_op2 op
    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    pop
    orr w10, w10, w11, lsl #8
    ldrb w11, [x0, x1]
    pop
    ldrb w12, [x0, x1]
    orr w11, w11, w12, lsl #8
    cmp w11, w10
    cset w10, \op
    strb w10, [x0, x1]
    next
.endm

_EQU2:
    compare_op2 eq

_NEQ2:
    compare_op2 ne

_GTH2:
    compare_op2 hi

_LTH2:
    compare_op2 lo

_JMP2:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    orr w5, w9, w10, lsl #8 // update program counter
    next

_JCN2:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    pop
    orr w9, w9, w10, lsl #8 // update program counter
    cmp w11, #0
    csel w5, w5, w9, eq // choose the jump or not
    next

_JSR2:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    lsr w11, w5, 8
    rpush w11
    rpush w5
    orr w5, w9, w10, lsl #8 // update program counter
    next

_STH2:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    rpush w10
    rpush w9
    next

_LDZ2:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x4, x9]
    push w10
    add w9, w9, #1
    and w9, w9, #0xFFFF
    ldrb w10, [x4, x9]
    push w10
    next

_STZ2:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    pop
    strb w11, [x4, x9]
    add w9, w9, #1
    and w9, w9, #0xFFFF
    strb w10, [x4, x9]
    next

_LDR2:
    ldrsb w9, [x0, x1]
    add x9, x5, x9
    and x9, x9, #0xffff
    ldrb w10, [x4, x9] // read from RAM
    strb w10, [x0, x1] // push to stack
    add x9, x9, #1
    and x9, x9, #0xffff
    ldrb w10, [x4, x9] // read from RAM
    push w10
    next

_STR2:
    ldrsb w9, [x0, x1]
    pop
    ldrsb w10, [x0, x1]
    pop
    ldrsb w11, [x0, x1]
    pop
    add x9, x5, x9
    and x9, x9, #0xffff
    strb w11, [x4, x9] // write to RAM
    add x9, x9, #1
    and x9, x9, #0xffff
    strb w10, [x4, x9] // write to RAM
    next

_LDA2:
    ldrb w9, [x0, x1]
    peek w10, x12, 1
    orr w9, w9, w10, lsl #8

    ldrb w10, [x4, x9]
    strb w10, [x0, x12]
    add w9, w9, #1
    and w9, w9, #0xffff
    ldrb w10, [x4, x9]
    strb w10, [x0, x1]
    next

_STA2:
    ldrb w9, [x0, x1]
    pop
    ldrb w10, [x0, x1]
    pop
    orr w12, w9, w10, lsl #8
    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    pop
    strb w11, [x4, x12]
    add x12, x12, #1
    and x12, x12, #0xffff
    strb w10, [x4, x12]
    next

_DEI2:
    precall
    CALL dei_2_entry
    postcall
    next

_DEO2:
    precall
    CALL deo_2_entry // todo check return value for early exit?
    postcall
    next

.macro binary_op2 op
    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    pop
    orr w12, w10, w11, lsl #8

    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    orr w11, w10, w11, lsl #8

    \op w11, w11, w12
    lsr w12, w11, 8
    strb w12, [x0, x1]
    push w11
    next
.endm

_ADD2:
    binary_op2 add

_SUB2:
    binary_op2 sub

_MUL2:
    binary_op2 mul

_DIV2:
    binary_op2 udiv

_AND2:
    binary_op2 and

_ORA2:
    binary_op2 orr

_EOR2:
    binary_op2 eor

_SFT2:
    ldrb w10, [x0, x1]
    pop
    ldrb w11, [x0, x1]
    pop
    ldrb w12, [x0, x1]
    orr w11, w11, w12, lsl #8

    lsr w12, w10, 4
    and w10, w10, #0xf
    lsr w11, w11, w10
    lsl w11, w11, w12
    lsr w12, w11, 8
    strb w12, [x0, x1]
    push w11
    next

_JMI:
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    ldrb w10, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    orr w12, w10, w9, lsl #8 // build the jump offset
    add x5, x5, x12 // do the jump
    and x5, x5, 0xffff
    next

_INCr:
    ldrb w9, [x2, x3]
    add w9, w9, #1
    strb w9, [x2, x3]
    next

_POPr:
    sub x3, x3, #1
    and x3, x3, #0xff
    next

_NIPr:
    ldrb w9, [x2, x3]   // get the top byte
    rpop
    strb w9, [x2, x3]   // overwrite the previous byte
    next

_SWPr:
    ldrb w10, [x2, x3]  // get the top byte
    rpeek w11, x9, 1    // get the second-from-top byte
    strb w10, [x2, x9]  // do the swap!
    strb w11, [x2, x3]
    next

_ROTr:
    ldrb w10, [x2, x3]
    rpeek w12, x11, 1
    rpeek w14, x13, 2

    strb w14, [x2, x3]
    strb w12, [x2, x13]
    strb w10, [x2, x11]
    next

_DUPr:
    ldrb w10, [x2, x3]   // get the top byte
    rpush w10
    next

_OVRr:
    rpeek w10, x9, 1
    rpush w10
    next

.macro compare_opr op
    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    cmp w11, w10
    cset w10, \op
    strb w10, [x2, x3]
    next
.endm

_EQUr:
    compare_opr eq

_NEQr:
    compare_opr ne

_GTHr:
    compare_opr hi

_LTHr:
    compare_opr lo

_JMPr:
    ldrsb x9, [x2, x3]
    rpop
    add x5, x5, x9
    and x5, x5, 0xffff
    next

_JCNr:
    ldrsb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    cmp w10, #0
    csel w10, wzr, w9, eq // choose the jump or not
    add x5, x5, x10 // jump or not
    and x5, x5, 0xffff
    next

_JSRr:
    ldrsb w9, [x2, x3]
    rpop
    lsr w10, w5, 8
    push w10
    push w5
    add x5, x5, x9
    and x5, x5, 0xffff
    next

_STHr:
    ldrb w9, [x2, x3]
    rpop
    push w9
    next

_LDZr:
    ldrb w9, [x2, x3]
    rpop
    ldrb w9, [x4, x9]
    rpush w9
    next

_STZr:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    strb w10, [x4, x9]
    next

_LDRr:
    ldrsb w9, [x2, x3]
    add x9, x5, x9
    and x9, x9, #0xffff
    ldrb w9, [x4, x9] // read from RAM
    strb w9, [x2, x3] // push to stack
    next

_STRr:
    ldrsb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    add x9, x5, x9
    and x9, x9, #0xffff
    strb w10, [x4, x9] // write to RAM
    next

_LDAr:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    orr w12, w9, w10, lsl #8
    ldrb w12, [x4, x12]
    strb w12, [x2, x3]
    next

_STAr:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    orr w12, w9, w10, lsl #8
    ldrb w10, [x2, x3]
    rpop
    strb w10, [x4, x12]
    next

_DEIr:
    precall
    CALL dei_r_entry
    postcall
    next

_DEOr:
    precall
    CALL deo_r_entry // todo check return value for early exit?
    postcall
    next

.macro binary_opr op
    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    \op w10, w11, w10
    strb w10, [x2, x3]
    next
.endm

_ADDr:
    binary_opr add

_SUBr:
    binary_opr sub

_MULr:
    binary_opr mul

_DIVr:
    binary_opr udiv

_ANDr:
    binary_opr and

_ORAr:
    binary_opr orr

_EORr:
    binary_opr eor

_SFTr:
    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    lsr w12, w10, 4
    and w10, w10, #0xf
    lsr w11, w11, w10
    lsl w11, w11, w12
    strb w11, [x2, x3]
    next

_JSI:
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    ldrb w10, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff

    orr w12, w10, w9, lsl #8 // build the jump offset

    // Store PC + 2 to the return stack
    lsr w9, w5, 8
    rpush w9
    rpush w5

    add x5, x5, x12 // do the jump
    and x5, x5, 0xffff
    next

_INC2r:
    ldrb w10, [x2, x3]
    rpeek w11, x9, 1
    orr w12, w10, w11, lsl #8
    add w12, w12, #1
    and w12, w12, #0xffff
    strb w12, [x2, x3]
    lsr w12, w12, 8
    strb w12, [x2, x9]
    next

_POP2r:
    sub x3, x3, #2
    and x3, x3, #0xff
    next

_NIP2r:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    strb w9, [x2, x3]
    sub x11, x3, #1
    and x11, x11, #0xff
    strb w10, [x2, x11]
    next

_SWP2r:
    ldrb w11, [x2, x3]  // get the top byte
    rpeek w12, x9, 2    // get the second-from-top byte
    strb w11, [x2, x9]  // do the swap!
    strb w12, [x2, x3]

    rpeek w11, x9, 1
    rpeek w12, x10, 3

    strb w11, [x2, x10]
    strb w12, [x2, x9]

    next

_ROT2r:
    ldrb w10, [x2, x3]
    rpeek w12, x11, 2
    rpeek w14, x13, 4

    strb w14, [x2, x3]
    strb w12, [x2, x13]
    strb w10, [x2, x11]

    rpeek w10, x15, 1
    rpeek w12, x11, 3
    rpeek w14, x13, 5

    ldrb w14, [x2, x13]
    strb w14, [x2, x15]
    strb w12, [x2, x13]
    strb w10, [x2, x11]

    next

_DUP2r:
    ldrb w11, [x2, x3]
    sub w9, w3, #1
    and w9, w9, #0xff
    ldrb w10, [x2, x9]
    rpush w10
    rpush w11
    next

_OVR2r:
    rpeek w10, x9, 2
    rpeek w11, x9, 3
    rpush w11
    rpush w10
    next

.macro compare_op2r op
    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    rpop
    orr w10, w10, w11, lsl #8
    ldrb w11, [x2, x3]
    rpop
    ldrb w12, [x2, x3]
    orr w11, w11, w12, lsl #8
    cmp w11, w10
    cset w10, \op
    strb w10, [x2, x3]
    next
.endm

_EQU2r:
    compare_op2r eq

_NEQ2r:
    compare_op2r ne

_GTH2r:
    compare_op2r hi

_LTH2r:
    compare_op2r lo

_JMP2r:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    orr w5, w9, w10, lsl #8 // update program counter
    next

_JCN2r:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    rpop
    orr w9, w9, w10, lsl #8 // update program counter
    cmp w11, #0
    csel w5, w5, w9, eq // choose the jump or not
    next

_JSR2r:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    lsr w11, w5, 8
    push w11
    push w5
    orr w5, w9, w10, lsl #8 // update program counter
    next

_STH2r:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    push w10
    push w9
    next

_LDZ2r:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x4, x9]
    rpush w10
    add w9, w9, #1
    and w9, w9, #0xFFFF
    ldrb w10, [x4, x9]
    rpush w10
    next

_STZ2r:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    rpop
    strb w11, [x4, x9]
    add w9, w9, #1
    and w9, w9, #0xFFFF
    strb w10, [x4, x9]
    next

_LDR2r:
    ldrsb w9, [x2, x3]
    add x9, x5, x9
    and x9, x9, #0xffff
    ldrb w10, [x4, x9] // read from RAM
    strb w10, [x2, x3] // push to stack
    add x9, x9, #1
    and x9, x9, #0xffff
    ldrb w10, [x4, x9] // read from RAM
    rpush w10
    next

_STR2r:
    ldrsb w9, [x2, x3]
    rpop
    ldrsb w10, [x2, x3]
    rpop
    ldrsb w11, [x2, x3]
    rpop
    add x9, x5, x9
    and x9, x9, #0xffff
    strb w11, [x4, x9] // write to RAM
    add x9, x9, #1
    and x9, x9, #0xffff
    strb w10, [x4, x9] // write to RAM
    next

_LDA2r:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    orr w12, w9, w10, lsl #8
    ldrb w10, [x4, x12]
    strb w10, [x2, x3]
    add w12, w12, #1
    and w12, w12, #0xffff
    ldrb w10, [x4, x12]
    rpush w10
    next

_STA2r:
    ldrb w9, [x2, x3]
    rpop
    ldrb w10, [x2, x3]
    rpop
    orr w12, w9, w10, lsl #8
    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    rpop
    strb w11, [x4, x12]
    add x12, x12, #1
    and x12, x12, #0xffff
    strb w10, [x4, x12]
    next

_DEI2r:
    precall
    CALL dei_2r_entry
    postcall
    next

_DEO2r:
    precall
    CALL deo_2r_entry // todo check return value for early exit?
    postcall
    next

.macro binary_op2r op
    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    rpop
    orr w12, w10, w11, lsl #8

    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    orr w11, w10, w11, lsl #8

    \op w11, w11, w12
    lsr w12, w11, 8
    strb w12, [x2, x3]
    rpush w11
    next
.endm

_ADD2r:
    binary_op2r add

_SUB2r:
    binary_op2r sub

_MUL2r:
    binary_op2r mul

_DIV2r:
    binary_op2r udiv

_AND2r:
    binary_op2r and

_ORA2r:
    binary_op2r orr

_EOR2r:
    binary_op2r eor

_SFT2r:
    ldrb w10, [x2, x3]
    rpop
    ldrb w11, [x2, x3]
    rpop
    ldrb w12, [x2, x3]
    orr w11, w11, w12, lsl #8

    lsr w12, w10, 4
    and w10, w10, #0xf
    lsr w11, w11, w10
    lsl w11, w11, w12
    lsr w12, w11, 8
    strb w12, [x2, x3]
    rpush w11
    next

_LIT:
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    push w9
    next

_INCk:
    ldrb w9, [x0, x1]
    add w9, w9, #1
    push w9
    next

_POPk:
    next

_NIPk:
    ldrb w9, [x0, x1]
    push w9
    next

_SWPk:
    ldrb w10, [x0, x1]   // get the top byte
    peek w11, x9, 1      // get the second-from-top byte
    push w10
    push w11
    next

_ROTk:
    ldrb w13, [x0, x1]
    peek w10, x11, 1
    push w10
    push w13
    peek w10, x11, 4
    push w10
    next

_DUPk:
    ldrb w11, [x0, x1]
    push w11
    push w11
    next

_OVRk:
    peek w10, x9, 1 // get the second-from-top
    ldrb w11, [x0, x1]
    push w10
    push w11
    push w10
    next

.macro compare_opk op
    ldrb w10, [x0, x1]
    peek w11, x9, 1
    cmp w11, w10
    cset w10, \op
    push w10
    next
.endm

_EQUk:
    compare_opk eq

_NEQk:
    compare_opk ne

_GTHk:
    compare_opk hi

_LTHk:
    compare_opk lo

_JMPk:
    ldrsb x9, [x0, x1]
    add x5, x5, x9
    and x5, x5, 0xffff
    next

_JCNk:
    ldrsb w9, [x0, x1]
    peek w10, x10, 1
    cmp w10, #0
    csel w10, wzr, w9, eq // choose the jump or not
    add x5, x5, x10 // jump or not
    and x5, x5, 0xffff
    next

_JSRk:
    ldrsb w9, [x0, x1]
    lsr w10, w5, 8
    rpush w10
    rpush w5
    add x5, x5, x9
    and x5, x5, 0xffff
    next

_STHk:
    ldrb w9, [x0, x1]
    rpush w9
    next

_LDZk:
    ldrb w9, [x0, x1]
    ldrb w9, [x4, x9]
    push w9
    next

_STZk:
    ldrb w9, [x0, x1]
    peek w10, x10, 1
    strb w10, [x4, x9]
    next

_LDRk:
    ldrsb w9, [x0, x1]
    add x9, x5, x9
    and x9, x9, #0xffff
    ldrb w9, [x4, x9] // read from RAM
    push w9
    next

_STRk:
    ldrsb w9, [x0, x1]
    peek w10, x10, 1
    add x9, x5, x9
    and x9, x9, #0xffff
    strb w10, [x4, x9] // write to RAM
    next

_LDAk:
    ldrb w9, [x0, x1]
    sub w10, w1, #1
    and w10, w10, #0xff
    ldrb w10, [x0, x10]
    orr w10, w9, w10, lsl #8    // build address
    ldrb w10, [x4, x10]         // load byte from RAM
    push w10
    next

_STAk:
    ldrb w9, [x0, x1]
    peek w10, x10, 1
    orr w12, w9, w10, lsl #8
    peek w10, x10, 2
    strb w10, [x4, x12]
    next

_DEIk:
    precall
    CALL dei_k_entry
    postcall
    next

_DEOk:
    precall
    CALL deo_k_entry // todo check return value for early exit?
    postcall
    next

.macro binary_opk op
    peek w11, x9, 1
    ldrb w10, [x0, x1]
    \op w10, w11, w10
    push w10
    next
.endm

_ADDk:
    binary_opk add

_SUBk:
    binary_opk sub

_MULk:
    binary_opk mul

_DIVk:
    binary_opk udiv

_ANDk:
    binary_opk and

_ORAk:
    binary_opk orr

_EORk:
    binary_opk eor

_SFTk:
    ldrb w10, [x0, x1]
    peek w11, x9, 1
    lsr w12, w10, 4
    and w10, w10, #0xf
    lsr w11, w11, w10
    lsl w11, w11, w12
    push w11
    next

_LIT2:
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    push w9
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    push w9
    next

_INC2k:
    ldrb w10, [x0, x1]
    peek w11, x9, 1
    orr w12, w10, w11, lsl #8
    add w12, w12, #1
    and w12, w12, #0xffff

    add x10, x1, #1
    and x10, x10, #0xff
    add x1, x10, #1
    and x1, x1, #0xff
    strb w12, [x0, x1]
    lsr w12, w12, 8
    strb w12, [x0, x10]
    next

_POP2k:
    next

_NIP2k:
    ldrb w9, [x0, x1]
    peek w10, x11, 1
    push w10
    push w9
    next

_SWP2k:
    peek w11, x9, 1
    push w11
    peek w11, x9, 1
    push w11
    peek w11, x9, 5
    push w11
    peek w11, x9, 5
    push w11
    next

_ROT2k:
    peek w11, x9, 3
    push w11
    peek w11, x9, 3
    push w11
    peek w11, x9, 3
    push w11
    peek w11, x9, 3
    push w11
    peek w11, x9, 9
    push w11
    peek w11, x9, 9
    push w11
    next

_DUP2k:
    ldrb w11, [x0, x1]
    sub w9, w1, #1
    and w9, w9, #0xff
    ldrb w10, [x0, x9]

    push w10
    push w11
    push w10
    push w11
    next

_OVR2k:
    ldrb w10, [x0, x1]
    peek w11, x9, 1
    peek w12, x9, 2
    peek w13, x9, 3
    push w13
    push w12
    push w11
    push w10
    push w13
    push w12
    next

.macro compare_op2k op
    ldrb w10, [x0, x1]
    peek w11, x9, 1
    orr w10, w10, w11, lsl #8

    peek w11, x9, 2
    peek w12, x9, 3
    orr w11, w11, w12, lsl #8

    cmp w11, w10
    cset w10, \op
    push w10
    next
.endm

_EQU2k:
    compare_op2k eq

_NEQ2k:
    compare_op2k ne

_GTH2k:
    compare_op2k hi

_LTH2k:
    compare_op2k lo

_JMP2k:
    ldrb w9, [x0, x1]
    peek w10, x10, 1
    orr w5, w9, w10, lsl #8 // update program counter
    next

_JCN2k:
    ldrb w9, [x0, x1]
    peek w10, x12, 1
    peek w11, x12, 2

    orr w9, w9, w10, lsl #8 // update program counter
    cmp w11, #0
    csel w5, w5, w9, eq // choose the jump or not
    next

_JSR2k:
    ldrb w9, [x0, x1]
    peek w10, x10, 1

    lsr w11, w5, 8
    rpush w11
    rpush w5

    orr w5, w9, w10, lsl #8 // update program counter
    next

_STH2k:
    ldrb w9, [x0, x1]
    peek w10, x10, 1
    rpush w10
    rpush w9
    next

_LDZ2k:
    ldrb w9, [x0, x1]
    ldrb w10, [x4, x9]
    push w10
    add w9, w9, #1
    and w9, w9, #0xFFFF
    ldrb w10, [x4, x9]
    push w10
    next

_STZ2k:
    ldrb w9, [x0, x1]
    peek w10, x10, 1
    peek w11, x11, 2

    strb w11, [x4, x9]
    add w9, w9, #1
    and w9, w9, #0xFFFF
    strb w10, [x4, x9]
    next

_LDR2k:
    ldrsb w9, [x0, x1]
    add x9, x5, x9
    and x9, x9, #0xffff
    ldrb w10, [x4, x9] // read from RAM
    push w10
    add x9, x9, #1
    and x9, x9, #0xffff
    ldrb w10, [x4, x9] // read from RAM
    push w10
    next

_STR2k:
    ldrsb w9, [x0, x1]
    peek w10, x10, 1
    peek w11, x11, 2

    add x9, x5, x9
    and x9, x9, #0xffff
    strb w11, [x4, x9] // write to RAM
    add x9, x9, #1
    and x9, x9, #0xffff
    strb w10, [x4, x9] // write to RAM
    next

_LDA2k:
    ldrb w9, [x0, x1]
    peek w10, x10, 1
    orr w12, w9, w10, lsl #8
    ldrb w10, [x4, x12]
    push w10
    add w12, w12, #1
    and w12, w12, #0xffff
    ldrb w10, [x4, x12]
    push w10
    next

_STA2k:
    ldrb w9, [x0, x1]
    peek w10, x10, 1
    orr w12, w9, w10, lsl #8

    peek w10, x10, 2
    peek w11, x11, 3

    strb w11, [x4, x12]
    add x12, x12, #1
    and x12, x12, #0xffff
    strb w10, [x4, x12]
    next

_DEI2k:
    precall
    CALL dei_2k_entry
    postcall
    next

_DEO2k:
    precall
    CALL deo_2k_entry // todo check return value for early exit?
    postcall
    next

.macro binary_op2k op
    ldrb w10, [x0, x1]
    peek w11, x9, 1
    orr w12, w10, w11, lsl #8

    peek w10, x9, 2
    peek w11, x9, 3
    orr w11, w10, w11, lsl #8

    \op w11, w11, w12
    lsr w12, w11, 8
    push w12
    push w11
    next
.endm

_ADD2k:
    binary_op2k add

_SUB2k:
    binary_op2k sub

_MUL2k:
    binary_op2k mul

_DIV2k:
    binary_op2k udiv

_AND2k:
    binary_op2k and

_ORA2k:
    binary_op2k orr

_EOR2k:
    binary_op2k eor

_SFT2k:
    ldrb w10, [x0, x1]
    peek w11, x9, 1
    peek w12, x9, 2
    orr w11, w11, w12, lsl #8

    lsr w12, w10, 4
    and w10, w10, #0xf
    lsr w11, w11, w10
    lsl w11, w11, w12
    lsr w12, w11, 8
    push w12
    push w11
    next

_LITr:
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    rpush w9
    next

_INCkr:
    ldrb w9, [x2, x3]
    add w9, w9, #1
    rpush w9
    next

_POPkr:
    next

_NIPkr:
    ldrb w9, [x2, x3]
    rpush w9
    next

_SWPkr:
    ldrb w10, [x2, x3]   // get the top byte
    rpeek w11, x9, 1
    rpush w10
    rpush w11
    next

_ROTkr:
    ldrb w13, [x2, x3]
    rpeek w10, x11, 1
    rpush w10
    rpush w13
    rpeek w10, x11, 4
    rpush w10
    next

_DUPkr:
    ldrb w11, [x2, x3]
    rpush w11
    rpush w11
    next

_OVRkr:
    rpeek w10, x9, 1
    ldrb w11, [x2, x3]
    rpush w10
    rpush w11
    rpush w10
    next

.macro compare_opkr op
    ldrb w10, [x2, x3]
    rpeek w11, x9, 1
    cmp w11, w10
    cset w10, \op
    rpush w10
    next
.endm

_EQUkr:
    compare_opkr eq

_NEQkr:
    compare_opkr ne

_GTHkr:
    compare_opkr hi

_LTHkr:
    compare_opkr lo

_JMPkr:
    ldrsb x9, [x2, x3]
    add x5, x5, x9
    and x5, x5, 0xffff
    next

_JCNkr:
    ldrsb w9, [x2, x3]
    rpeek w10, x10, 1
    cmp w10, #0
    csel w10, wzr, w9, eq // choose the jump or not
    add x5, x5, x10 // jump or not
    and x5, x5, 0xffff
    next

_JSRkr:
    ldrsb w9, [x2, x3]
    lsr w10, w5, 8
    push w10
    push w5
    add x5, x5, x9
    and x5, x5, 0xffff
    next

_STHkr:
    ldrb w9, [x2, x3]
    push w9
    next

_LDZkr:
    ldrb w9, [x2, x3]
    ldrb w9, [x4, x9]
    rpush w9
    next

_STZkr:
    ldrb w9, [x2, x3]
    rpeek w10, x10, 1
    strb w10, [x4, x9]
    next

_LDRkr:
    ldrsb w9, [x2, x3]
    add x9, x5, x9
    and x9, x9, #0xffff
    ldrb w9, [x4, x9] // read from RAM
    rpush w9
    next

_STRkr:
    ldrsb w9, [x2, x3]
    rpeek w10, x10, 1
    add x9, x5, x9
    and x9, x9, #0xffff
    strb w10, [x4, x9] // write to RAM
    next

_LDAkr:
    ldrb w9, [x2, x3]
    sub w10, w3, #1
    and w10, w10, #0xff
    ldrb w10, [x2, x10]
    orr w10, w9, w10, lsl #8    // build address
    ldrb w10, [x4, x10]         // load byte from RAM
    rpush w10
    next

_STAkr:
    ldrb w9, [x2, x3]
    rpeek w10, x10, 1
    orr w12, w9, w10, lsl #8
    rpeek w10, x10, 2
    strb w10, [x4, x12]
    next

_DEIkr:
    precall
    CALL dei_kr_entry
    postcall
    next

_DEOkr:
    precall
    CALL deo_kr_entry // todo check return value for early exit?
    postcall
    next

.macro binary_opkr op
    ldrb w10, [x2, x3]
    rpeek w11, x9, 1
    \op w10, w11, w10
    rpush w10
    next
.endm

_ADDkr:
    binary_opkr add

_SUBkr:
    binary_opkr sub

_MULkr:
    binary_opkr mul

_DIVkr:
    binary_opkr udiv

_ANDkr:
    binary_opkr and

_ORAkr:
    binary_opkr orr

_EORkr:
    binary_opkr eor

_SFTkr:
    ldrb w10, [x2, x3]
    rpeek w11, x9, 1
    lsr w12, w10, 4
    and w10, w10, #0xf
    lsr w11, w11, w10
    lsl w11, w11, w12
    rpush w11
    next

_LIT2r:
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    rpush w9
    ldrb w9, [x4, x5]
    add x5, x5, #1
    and x5, x5, #0xffff
    rpush w9
    next

_INC2kr:
    ldrb w10, [x2, x3]
    rpeek w11, x9, 1
    orr w12, w10, w11, lsl #8
    add w12, w12, #1
    and w12, w12, #0xffff

    add x10, x3, #1
    and x10, x10, #0xff
    add x3, x10, #1
    and x3, x3, #0xff
    strb w12, [x2, x3]
    lsr w12, w12, 8
    strb w12, [x2, x10]
    next

_POP2kr:
    next

_NIP2kr:
    ldrb w9, [x2, x3]
    rpeek w10, x11, 1
    rpush w10
    rpush w9
    next

_SWP2kr:
    rpeek w11, x9, 1
    rpush w11
    rpeek w11, x9, 1
    rpush w11
    rpeek w11, x9, 5
    rpush w11
    rpeek w11, x9, 5
    rpush w11
    next

_ROT2kr:
    rpeek w11, x9, 3
    rpush w11
    rpeek w11, x9, 3
    rpush w11
    rpeek w11, x9, 3
    rpush w11
    rpeek w11, x9, 3
    rpush w11
    rpeek w11, x9, 9
    rpush w11
    rpeek w11, x9, 9
    rpush w11
    next

_DUP2kr:
    ldrb w11, [x2, x3]
    sub w9, w3, #1
    and w9, w9, #0xff
    ldrb w10, [x2, x9]

    rpush w10
    rpush w11
    rpush w10
    rpush w11
    next

_OVR2kr:
    ldrb w10, [x2, x3]
    rpeek w11, x9, 1
    rpeek w12, x9, 2
    rpeek w13, x9, 3
    rpush w13
    rpush w12
    rpush w11
    rpush w10
    rpush w13
    rpush w12
    next

.macro compare_op2kr op
    ldrb w10, [x2, x3]
    rpeek w11, x9, 1
    orr w10, w10, w11, lsl #8
    rpeek w11, x9, 2
    rpeek w12, x9, 3
    orr w11, w11, w12, lsl #8
    cmp w11, w10
    cset w10, \op
    rpush w10
    next
.endm

_EQU2kr:
    compare_op2kr eq

_NEQ2kr:
    compare_op2kr ne

_GTH2kr:
    compare_op2kr hi

_LTH2kr:
    compare_op2kr lo

_JMP2kr:
    ldrb w9, [x2, x3]
    rpeek w10, x10, 1
    orr w5, w9, w10, lsl #8 // update program counter
    next

_JCN2kr:
    ldrb w9, [x2, x3]
    rpeek w10, x12, 1
    rpeek w11, x12, 2
    orr w9, w9, w10, lsl #8 // update program counter
    cmp w11, #0
    csel w5, w5, w9, eq // choose the jump or not
    next

_JSR2kr:
    ldrb w9, [x2, x3]
    rpeek w10, x10, 1
    lsr w11, w5, 8
    push w11
    push w5
    orr w5, w9, w10, lsl #8 // update program counter
    next

_STH2kr:
    ldrb w9, [x2, x3]
    rpeek w10, x11, 1
    push w10
    push w9
    next

_LDZ2kr:
    ldrb w9, [x2, x3]
    ldrb w10, [x4, x9]
    rpush w10
    add w9, w9, #1
    and w9, w9, #0xFFFF
    ldrb w10, [x4, x9]
    rpush w10
    next

_STZ2kr:
    ldrb w9, [x2, x3]
    rpeek w10, x10, 1
    rpeek w11, x11, 2

    strb w11, [x4, x9]
    add w9, w9, #1
    and w9, w9, #0xFFFF
    strb w10, [x4, x9]
    next

_LDR2kr:
    ldrsb w9, [x2, x3]
    add x9, x5, x9
    and x9, x9, #0xffff
    ldrb w10, [x4, x9] // read from RAM
    rpush w10
    add x9, x9, #1
    and x9, x9, #0xffff
    ldrb w10, [x4, x9] // read from RAM
    rpush w10
    next

_STR2kr:
    ldrsb w9, [x2, x3]
    rpeek w10, x10, 1
    rpeek w11, x11, 2

    add x9, x5, x9
    and x9, x9, #0xffff
    strb w11, [x4, x9] // write to RAM
    add x9, x9, #1
    and x9, x9, #0xffff
    strb w10, [x4, x9] // write to RAM
    next

_LDA2kr:
    ldrb w9, [x2, x3]
    rpeek w10, x10, 1
    orr w12, w9, w10, lsl #8
    ldrb w10, [x4, x12]
    rpush w10
    add w12, w12, #1
    and w12, w12, #0xffff
    ldrb w10, [x4, x12]
    rpush w10
    next

_STA2kr:
    ldrb w9, [x2, x3]
    rpeek w10, x10, 1
    orr w12, w9, w10, lsl #8

    rpeek w10, x10, 2
    rpeek w11, x11, 3

    strb w11, [x4, x12]
    add x12, x12, #1
    and x12, x12, #0xffff
    strb w10, [x4, x12]
    next

_DEI2kr:
    precall
    CALL dei_2kr_entry
    postcall
    next

_DEO2kr:
    precall
    CALL deo_2kr_entry // todo check return value for early exit?
    postcall
    next

.macro binary_op2kr op
    ldrb w10, [x2, x3]
    rpeek w11, x9, 1
    orr w12, w10, w11, lsl #8

    rpeek w10, x9, 2
    rpeek w11, x9, 3
    orr w11, w10, w11, lsl #8

    \op w11, w11, w12
    lsr w12, w11, 8
    rpush w12
    rpush w11
    next
.endm

_ADD2kr:
    binary_op2kr add

_SUB2kr:
    binary_op2kr sub

_MUL2kr:
    binary_op2kr mul

_DIV2kr:
    binary_op2kr udiv

_AND2kr:
    binary_op2kr and

_ORA2kr:
    binary_op2kr orr

_EOR2kr:
    binary_op2kr eor

_SFT2kr:
    ldrb w10, [x2, x3]
    rpeek w11, x9, 1
    rpeek w12, x9, 2
    orr w11, w11, w12, lsl #8

    lsr w12, w10, 4
    and w10, w10, #0xf
    lsr w11, w11, w10
    lsl w11, w11, w12
    lsr w12, w11, 8
    rpush w12
    rpush w11
    next

.data
.balign 4096
.global JUMP_TABLE
JUMP_TABLE:
    .quad _BRK
    .quad _INC
    .quad _POP
    .quad _NIP
    .quad _SWP
    .quad _ROT
    .quad _DUP
    .quad _OVR
    .quad _EQU
    .quad _NEQ
    .quad _GTH
    .quad _LTH
    .quad _JMP
    .quad _JCN
    .quad _JSR
    .quad _STH
    .quad _LDZ
    .quad _STZ
    .quad _LDR
    .quad _STR
    .quad _LDA
    .quad _STA
    .quad _DEI
    .quad _DEO
    .quad _ADD
    .quad _SUB
    .quad _MUL
    .quad _DIV
    .quad _AND
    .quad _ORA
    .quad _EOR
    .quad _SFT
    .quad _JCI
    .quad _INC2
    .quad _POP2
    .quad _NIP2
    .quad _SWP2
    .quad _ROT2
    .quad _DUP2
    .quad _OVR2
    .quad _EQU2
    .quad _NEQ2
    .quad _GTH2
    .quad _LTH2
    .quad _JMP2
    .quad _JCN2
    .quad _JSR2
    .quad _STH2
    .quad _LDZ2
    .quad _STZ2
    .quad _LDR2
    .quad _STR2
    .quad _LDA2
    .quad _STA2
    .quad _DEI2
    .quad _DEO2
    .quad _ADD2
    .quad _SUB2
    .quad _MUL2
    .quad _DIV2
    .quad _AND2
    .quad _ORA2
    .quad _EOR2
    .quad _SFT2
    .quad _JMI
    .quad _INCr
    .quad _POPr
    .quad _NIPr
    .quad _SWPr
    .quad _ROTr
    .quad _DUPr
    .quad _OVRr
    .quad _EQUr
    .quad _NEQr
    .quad _GTHr
    .quad _LTHr
    .quad _JMPr
    .quad _JCNr
    .quad _JSRr
    .quad _STHr
    .quad _LDZr
    .quad _STZr
    .quad _LDRr
    .quad _STRr
    .quad _LDAr
    .quad _STAr
    .quad _DEIr
    .quad _DEOr
    .quad _ADDr
    .quad _SUBr
    .quad _MULr
    .quad _DIVr
    .quad _ANDr
    .quad _ORAr
    .quad _EORr
    .quad _SFTr
    .quad _JSI
    .quad _INC2r
    .quad _POP2r
    .quad _NIP2r
    .quad _SWP2r
    .quad _ROT2r
    .quad _DUP2r
    .quad _OVR2r
    .quad _EQU2r
    .quad _NEQ2r
    .quad _GTH2r
    .quad _LTH2r
    .quad _JMP2r
    .quad _JCN2r
    .quad _JSR2r
    .quad _STH2r
    .quad _LDZ2r
    .quad _STZ2r
    .quad _LDR2r
    .quad _STR2r
    .quad _LDA2r
    .quad _STA2r
    .quad _DEI2r
    .quad _DEO2r
    .quad _ADD2r
    .quad _SUB2r
    .quad _MUL2r
    .quad _DIV2r
    .quad _AND2r
    .quad _ORA2r
    .quad _EOR2r
    .quad _SFT2r
    .quad _LIT
    .quad _INCk
    .quad _POPk
    .quad _NIPk
    .quad _SWPk
    .quad _ROTk
    .quad _DUPk
    .quad _OVRk
    .quad _EQUk
    .quad _NEQk
    .quad _GTHk
    .quad _LTHk
    .quad _JMPk
    .quad _JCNk
    .quad _JSRk
    .quad _STHk
    .quad _LDZk
    .quad _STZk
    .quad _LDRk
    .quad _STRk
    .quad _LDAk
    .quad _STAk
    .quad _DEIk
    .quad _DEOk
    .quad _ADDk
    .quad _SUBk
    .quad _MULk
    .quad _DIVk
    .quad _ANDk
    .quad _ORAk
    .quad _EORk
    .quad _SFTk
    .quad _LIT2
    .quad _INC2k
    .quad _POP2k
    .quad _NIP2k
    .quad _SWP2k
    .quad _ROT2k
    .quad _DUP2k
    .quad _OVR2k
    .quad _EQU2k
    .quad _NEQ2k
    .quad _GTH2k
    .quad _LTH2k
    .quad _JMP2k
    .quad _JCN2k
    .quad _JSR2k
    .quad _STH2k
    .quad _LDZ2k
    .quad _STZ2k
    .quad _LDR2k
    .quad _STR2k
    .quad _LDA2k
    .quad _STA2k
    .quad _DEI2k
    .quad _DEO2k
    .quad _ADD2k
    .quad _SUB2k
    .quad _MUL2k
    .quad _DIV2k
    .quad _AND2k
    .quad _ORA2k
    .quad _EOR2k
    .quad _SFT2k
    .quad _LITr
    .quad _INCkr
    .quad _POPkr
    .quad _NIPkr
    .quad _SWPkr
    .quad _ROTkr
    .quad _DUPkr
    .quad _OVRkr
    .quad _EQUkr
    .quad _NEQkr
    .quad _GTHkr
    .quad _LTHkr
    .quad _JMPkr
    .quad _JCNkr
    .quad _JSRkr
    .quad _STHkr
    .quad _LDZkr
    .quad _STZkr
    .quad _LDRkr
    .quad _STRkr
    .quad _LDAkr
    .quad _STAkr
    .quad _DEIkr
    .quad _DEOkr
    .quad _ADDkr
    .quad _SUBkr
    .quad _MULkr
    .quad _DIVkr
    .quad _ANDkr
    .quad _ORAkr
    .quad _EORkr
    .quad _SFTkr
    .quad _LIT2r
    .quad _INC2kr
    .quad _POP2kr
    .quad _NIP2kr
    .quad _SWP2kr
    .quad _ROT2kr
    .quad _DUP2kr
    .quad _OVR2kr
    .quad _EQU2kr
    .quad _NEQ2kr
    .quad _GTH2kr
    .quad _LTH2kr
    .quad _JMP2kr
    .quad _JCN2kr
    .quad _JSR2kr
    .quad _STH2kr
    .quad _LDZ2kr
    .quad _STZ2kr
    .quad _LDR2kr
    .quad _STR2kr
    .quad _LDA2kr
    .quad _STA2kr
    .quad _DEI2kr
    .quad _DEO2kr
    .quad _ADD2kr
    .quad _SUB2kr
    .quad _MUL2kr
    .quad _DIV2kr
    .quad _AND2kr
    .quad _ORA2kr
    .quad _EOR2kr
    .quad _SFT2kr