~amaurycarvalho/msxbas2asm/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
;---------------------------------------------------------------------------------------------------------
; Source code converted by MSXBAS2ASM - MSX BASIC TO Z80 ASSEMBLY CONVERTER
; MSXBAS2ASM developed by Amaury Carvalho, 2019, Brazil
; http://launchpad.net/msxbas2asm
;---------------------------------------------------------------------------------------------------------

;--------------------------------------------------------
; MSX BIOS DATA/FUNCTION POINTERS
;--------------------------------------------------------

;---------------------------------------------------------------------------------------------------------
; BIOS FUNCTIONS
;---------------------------------------------------------------------------------------------------------

BIOS_CALBAS:    equ 0x0159
BIOS_OUTDO:     equ 0x0018   ; output to current device (i.e. screen)
BIOS_CHPUT:     equ 0x00A2
BIOS_CLS:       equ 0x00C3
BIOS_POSIT:     equ 0x00C6
BIOS_BEEP:      equ 0x00C0
BIOS_CHGET:     equ 0x009F
BIOS_CHSNS:     equ 0x009C
BIOS_INLIN:     equ 0x00B1
BIOS_PINLIN:    equ 0x00AE
BIOS_QINLIN:    equ 0x00B4
BIOS_GTSTCK:    equ 0x00D5
BIOS_GTTRIG:    equ 0x00D8
BIOS_GTPAD:     equ 0x00DB
BIOS_GTPDL:     equ 0x00DE
BIOS_DISSCR:    equ 0x0041
BIOS_ENASCR:    equ 0x0044
BIOS_CHGMOD:    equ 0x005F
BIOS_CHGCLR:    equ 0x0062
BIOS_CLRSPR:    equ 0x0069
BIOS_INITXT:    equ 0x006C    ; init text mode 40 columns
BIOS_INIT32:    equ 0x006F    ; init text mode 32 columns
BIOS_INIGRP:    equ 0x0072
BIOS_INIMLT:    equ 0x0075
BIOS_SETTXT:    equ 0x0078    ; set text mode 40 columns
BIOS_SETT32:    equ 0x007B    ; set text mode 32 columns
BIOS_SETGRP:    equ 0x007E
BIOS_SETMLT:    equ 0x0081
BIOS_CALPAT:    equ 0x0084
BIOS_CALATR:    equ 0x0087
BIOS_GSPSIZ:    equ 0x008A
BIOS_GRPPRT:    equ 0x008D
BIOS_ERAFNK:    equ 0x00CC
BIOS_DSPFNK:    equ 0x00CF
BIOS_TOTEXT:    equ 0x00D2
BIOS_BREAKX:    equ 0x00B7
BIOS_ISCNTC:    equ 0x03FB
BIOS_CHKRAM:    equ 0x0000
BIOS_GICINI:    equ 0x0090
BIOS_WRTPSG:    equ 0x0093
BIOS_REDPSG:    equ 0x0096
BIOS_STRTMS:    equ 0x0099
BIOS_KEYINT:    equ 0x0038
BIOS_CALSLT:    equ 0x001C
BIOS_ENASLT:    equ 0x0024
BIOS_RSLREG:    equ 0x0138
BIOS_SCALXY:    equ 0x010E
BIOS_MAPXYC:    equ 0x0111      ; in BC = X, DE = Y
BIOS_READC:     equ 0x011D      ; out A = color of current pixel
BIOS_SETATR:    equ 0x011A      ; in A = color code
BIOS_SETC:      equ 0x0120      ; set current pixel to color from SETATR
BIOS_NSETCX:    equ 0x0123      ; in HL = pixel fill count
BIOS_SCANR:     equ 0x012C      ; in B=Fill switch, DE=Skip count, out DE=Skip remainder, HL=Pixel count
BIOS_SCANL:     equ 0x012F      ; out HL=Pixel count
BIOS_FETCHC:    equ 0x0114      ; out A = cursor mask, HL = VRAM address of cursor
BIOS_STOREC:    equ 0x0117      ; in A = cursor mask, HL = VRAM address of cursor
BIOS_RESET:     equ 0x7D17      ; restart BASIC
BIOS_IOALLOC:   equ 0X7e6b      ; memory setup

BIOS_GETVCP:    equ 0x0150      ; get PSG voice buffer address (in A = voice number, out HL = address of byte 2)
BIOS_GETVC2:    equ 0x0153      ; get PSG voice buffer address (VOICEN = voice number, in L = byte number 0-36, out HL = address)

BIOS_CHPUT_LF:  equ 0x0908
BIOS_CHPUT_CR:  equ 0x0A81
BIOS_CHPUT_TAB: equ 0x0A71

; MSX2
BIOS_CHKNEW:    equ 0x0165      ; C-flag set if screenmode = 5, 6, 7 or 8
BIOS_EXTROM:	equ	0x015F
BIOS_SCALXY2:   equ 0x008D      ; in BC = X, DE = Y
BIOS_MAPXYC2:   equ 0x0091      ; in BC = X, DE = Y
BIOS_SETC2:     equ 0x009D      ; set current pixel to color from SETATR
BIOS_READC2:    equ 0x0095      ; out A = color of current pixel
BIOS_CHGMOD2:   equ 0x00D1      ; in A = screenmode
BIOS_DOBOXF:    equ 0x0079      ; hl = basic text pointer
BIOS_GRPPRT2:   equ 0x0089      ; a = character
BIOS_CHGCLR2:   equ 0x0111      ; change color, a = screen mode
BIOS_CALPAT2:   equ 0x00F9
BIOS_CALATR2:   equ 0x00FD
BIOS_GSPSIZ2:   equ 0x0101
BIOS_CLRSPR2:   equ 0x00F5

;---------------------------------------------------------------------------------------------------------
; BIOS WORK AREAS
;---------------------------------------------------------------------------------------------------------

BIOS_VERSION:   equ 0x002D   ; 0 = MSX1, 1 = MSX2, 2 = MSX2+, 3 = MSXturboR
BIOS_FORCLR:    equ 0xF3E9
BIOS_BAKCLR:    equ 0xF3EA
BIOS_BDRCLR:    equ 0xF3EB
BIOS_ATRBYT:    equ 0xF3F2
BIOS_INTFLG:    equ 0xFC9B
BIOS_EXPTBL:    equ 0xFCC1
BIOS_JIFFY:     equ 0xFC9E
BIOS_BOTTOM:    equ 0xFC48
BIOS_HIMEM:     equ 0xFC4A
BIOS_SCRMOD:    equ 0xFCAF   ; 0=40x24 Text Mode, 1=32x24 Text Mode, 2=Graphics Mode, 3=Multicolour Mode.
BIOS_CLIKSW:    equ 0xF3DB   ; 0=keyboard click off, 1=keyboard click on
BIOS_GRPACX:    equ 0xFCB7
BIOS_GRPACY:    equ 0xFCB9
BIOS_DATLIN:    equ 0xF6A3   ; 2 - line number of DATA statement read by READ statement
BIOS_DATPTR:    equ 0xF6C8   ; 2 - address of data read by executing READ statement
BIOS_FLGINP:    equ 0xF6A6   ; 1 - flag used in INPUT or READ
BIOS_TEMP:      equ 0xF6A7   ; 2
BIOS_TEMP2:     equ 0xF6BC   ; 2
BIOS_TEMP3:     equ 0xF69D   ; 2
BIOS_TEMP8:     equ 0xF69F   ; 2
BIOS_TEMP9:     equ 0xF7B8   ; 2
BIOS_OLDSCR:    equ 0xFCB0   ; screen mode of the last text mode set
BIOS_LINL40:    equ 0xF3AE   ; width for 40 columns screen mode
BIOS_LINL32:    equ 0xF3AF   ; width for 32 columns screen mode
BIOS_LINLEN:    equ 0xF3B0   ; current width for text screen mode
BIOS_CLMLST:    equ 0xF3B2   ; minimum number of columns that must still be available on a line for a CRLF
BIOS_TXTNAM:    equ 0xF3B3   ; characters table name

BIOS_VOICEN:    equ 0xFB38   ; PSG voice number
BIOS_MCLTAB:    equ 0xF956
BIOS_PRSCNT:    equ 0xFB35
BIOS_SAVSP:     equ 0xFB36
BIOS_QUEUEN:    equ 0xFB3E
BIOS_MUSICF:    equ 0xFB3F   ;contains 3 bit flags set by the STRTMS. Bits 0, 1 and 2 correspond to VOICAQ, VOICBQ and VOICCQ.
BIOS_PLYCNT:    equ 0xFB40

BIOS_DRWFLG:    equ 0xFCBB
BIOS_MCLFLG:    equ 0xF958

BIOS_SLTROM:    equ 0xFCC1
BIOS_RAMAD0:	equ	0xF341	; Main-RAM Slot (00000h~03FFFh)
BIOS_RAMAD1:	equ	0xF342	; Main-RAM Slot (04000h~07FFFh)
BIOS_RAMAD2:	equ	0xF343	; Main-RAM Slot (08000h~0BFFFh)
BIOS_RAMAD3:	equ	0xF344	; Main-RAM Slot (0C000h~0FFFFh)



;--------------------------------------------------------
; MSX BASIC DATA/FUNCTION POINTERS
;--------------------------------------------------------

;---------------------------------------------------------------------------------------------------------
; MSX BASIC FUNCTIONS
;---------------------------------------------------------------------------------------------------------

BASIC_AUTO:   equ 0x3973
BASIC_AND:    equ 0x3A18
BASIC_ATTR:   equ 0x39FE
BASIC_BASE:   equ 0x39BE
BASIC_BSAVE:  equ 0x39CC
BASIC_BLOAD:  equ 0x39CA
BASIC_BEEP:   equ 0x39AC
BASIC_CALL:   equ 0x39C0
BASIC_CLOSE:  equ 0x3994
BASIC_COPY:   equ 0x39D8
BASIC_CONT:   equ 0x395E
BASIC_CLEAR:  equ 0x3950
BASIC_CLOAD:  equ 0x3962
BASIC_CSAVE:  equ 0x3960
BASIC_CSRLIN: equ 0x39FC
BASIC_CIRCLE: equ 0x39A4
BASIC_COLOR:  equ 0x39A6
BASIC_CLS:    equ 0x396A
BASIC_CMD:    equ 0x39DA
BASIC_DELETE: equ 0x397C
BASIC_DATA:   equ 0x3934
BASIC_DIM:    equ 0x3938
BASIC_DEFSTR: equ 0x3982
BASIC_DEFINT: equ 0x3984
BASIC_DEFSNG: equ 0x3986
BASIC_DEFDBL: equ 0x3988
BASIC_DSKO:   equ 0x39CE
BASIC_DEF:    equ 0x395A
BASIC_DSKI:   equ 0x3A00
BASIC_DRAW:   equ 0x39A8
BASIC_ELSE:   equ 0x396E
BASIC_END:    equ 0x392E
BASIC_ERASE:  equ 0x3976
BASIC_ERROR:  equ 0x3978
BASIC_ERL:    equ 0x39EE
BASIC_ERR:    equ 0x39F0
BASIC_EQU:    equ 0x3A1E
BASIC_FOR:    equ 0x3920
BASIC_FIELD:  equ 0x398E
BASIC_FILES:  equ 0x39AA
BASIC_FN:     equ 0x39E8
BASIC_GOTO:   equ 0x393E
BASIC_GOSUB:  equ 0x3948
BASIC_GET:    equ 0x3990
BASIC_INPUT:  equ 0x3936
BASIC_IF:     equ 0x3942
BASIC_INSTR:  equ 0x39F6
BASIC_IMP:    equ 0x3A20
BASIC_INKEY:  equ 0x3A04
BASIC_IPL:    equ 0x39D6
BASIC_KILL:   equ 0x39D4
BASIC_KEY:    equ 0x3964
BASIC_LPRINT: equ 0x394C
BASIC_LLIST:  equ 0x3968
BASIC_LET:    equ 0x393C
BASIC_LOCATE: equ 0x39DC
BASIC_LINE:   equ 0x398A
BASIC_LOAD:   equ 0x3996
BASIC_LSET:   equ 0x399C
BASIC_LIST:   equ 0x3952
BASIC_LFILES: equ 0x39A2
BASIC_MOTOR:  equ 0x39C8
BASIC_MERGE:  equ 0x3998
BASIC_MOD:    equ 0x3A22
BASIC_MAX:    equ 0x39C6
BASIC_NEXT:   equ 0x3932
BASIC_NAME:   equ 0x39D2
BASIC_NEW:    equ 0x3954
BASIC_NOT:    equ 0x39EC
BASIC_OPEN:   equ 0x398C
BASIC_OUT:    equ 0x3964
BASIC_ON:     equ 0x3956
BASIC_OR:     equ 0x3A1A
BASIC_OFF:    equ 0x3A02
BASIC_PRINT:  equ 0x394E
BASIC_PUT:    equ 0x3992
BASIC_POKE:   equ 0x395C
BASIC_PSET:   equ 0x39B0
BASIC_PRESET: equ 0x39B2
BASIC_POINT:  equ 0x3A06
BASIC_PAINT:  equ 0x39AA
BASIC_PLAY:   equ 0x39AE
BASIC_RETURN: equ 0x3948
BASIC_READ:   equ 0x393A
BASIC_RUN:    equ 0x3940
BASIC_RESTORE:equ 0x3944
BASIC_REM:    equ 0x394A
BASIC_RESUME: equ 0x397A
BASIC_RSET:   equ 0x399E
BASIC_RENUM:  equ 0x3980
BASIC_SCREEN: equ 0x39B6
BASIC_SPRITE: equ 0x39BA
BASIC_STOP:   equ 0x394C
BASIC_SWAP:   equ 0x3974
BASIC_SET:    equ 0x39D0
BASIC_SAVE:   equ 0x39A0
BASIC_SPC:    equ 0x39EA
BASIC_STEP:   equ 0x39E4
BASIC_STRING: equ 0x39F2
BASIC_SPACE1: equ 0x397E
BASIC_SOUND:  equ 0x39B4
BASIC_THEN:   equ 0x39E0
BASIC_TRON:   equ 0x3970
BASIC_TROFF:  equ 0x3972
BASIC_TAB:    equ 0x39E2
BASIC_TO:     equ 0x39DE
BASIC_TIME:   equ 0x39C2
BASIC_USING:  equ 0x39F4
BASIC_USR:    equ 0x39E6
BASIC_VARPTR: equ 0x39FA
BASIC_VDP:    equ 0x39BC
BASIC_VPOKE:  equ 0x39B8
BASIC_WIDTH:  equ 0x396C
BASIC_WAIT:   equ 0x3958
BASIC_XOR:    equ 0x3A1C
BASIC_ABS:    equ 0x39E8
BASIC_ATN:    equ 0x39F8
BASIC_ASC:    equ 0x3A06
BASIC_BIN:    equ 0x3A16
BASIC_CINT:   equ 0x3A18
BASIC_CSNG:   equ 0x3A1A
BASIC_CDBL:   equ 0x3A1C
BASIC_CVI:    equ 0x3A2C
BASIC_CVS:    equ 0x3A2E
BASIC_CVD:    equ 0x3A30
BASIC_COS:    equ 0x39F4
BASIC_CHR:    equ 0x3A08
BASIC_DSKF:   equ 0x3A28
BASIC_EXP:    equ 0x39F2
BASIC_EOF:    equ 0x3A32
BASIC_FRE:    equ 0x39FA
BASIC_FIX:    equ 0x3A1E
BASIC_FPOS:   equ 0x3A2A
BASIC_HEX:    equ 0x3A12
BASIC_INT:    equ 0x39E6
BASIC_INP:    equ 0x39FC
BASIC_LPOS:   equ 0x3A14
BASIC_LOG:    equ 0x39F0
BASIC_LOC:    equ 0x3A34
BASIC_LEN:    equ 0x3A00
BASIC_LEFT:   equ 0x39DE
BASIC_LOF:    equ 0x3A36
BASIC_MKI:    equ 0x3A38
BASIC_MKS:    equ 0x3A3A
BASIC_MKD:    equ 0x3A3C
BASIC_MID:    equ 0x39E2
BASIC_OCT:    equ 0x3A10
BASIC_POS:    equ 0x39FE
BASIC_PEEK:   equ 0x3A0A
BASIC_PDL:    equ 0x3A24
BASIC_PAD:    equ 0x3A26
BASIC_RIGHT:  equ 0x39E0
BASIC_RND:    equ 0x39EC
BASIC_SGN:    equ 0x39E4
BASIC_SQR:    equ 0x39EA
BASIC_SIN:    equ 0x39EE
BASIC_STR:    equ 0x3A02
BASIC_SPACE2: equ 0x3A0E
BASIC_STICK:  equ 0x3A20
BASIC_STRIG:  equ 0x3A22
BASIC_TAN:    equ 0x39F6
BASIC_VAL:    equ 0x3A04
BASIC_VPEEK:  equ 0x3A0C

BASIC_TRAP_ENABLE:  equ 0x631B    ; ON INTERVAL/KEY/SPRITE/STOP/STRIG - hl = pointer to trap block
BASIC_TRAP_DISABLE: equ 0x632B    ; hl = pointer to trap block
BASIC_TRAP_ACKNW:   equ 0x6358    ; hl, acknowledge trap (handle trap: sts=5? has handler? ackn, pause, run trap, sts=1? unpause)
BASIC_TRAP_PAUSE:   equ 0x6331    ; hl
BASIC_TRAP_UNPAUSE: equ 0x633E    ; hl
BASIC_TRAP_CLEAR:   equ 0x636E

BASIC_PLAY_DIRECT:  equ 0x744C
BASIC_DRAW_DIRECT:  equ 0x568C

BASIC_READYR:       equ 0x409B
BASIC_READYC:       equ 0x7D17
BASIC_FACEVAL:      equ 0x4DC7

BASIC_ERROR_HANDLER:equ 0x406F
BASIC_ERROR_SYNTAX: equ 0x4055
BASIC_ERROR_DIVZER: equ 0x4058
BASIC_ERROR_OVRFLW: equ 0x4067
BASIC_ERROR_ARRAY:  equ 0x405E
BASIC_ERROR_TYPMIS: equ 0x406D

; BASIC ERROR CODES TO BASIC_ERROR_HANDLER
; 01 NEXT without FOR             19 Device I/O error
; 02 Syntax error                 20 Verify error
; 03 RETURN without GOSUB         21 No RESUME
; 04 Out of DATA                  22 RESUME without error
; 05 Illegal function call        23 Unprintable error
; 06 Overflow                     24 Missing operand
; 07 Out of memory                25 Line buffer overflow
; 08 Undefined line number        50 FIELD overflow
; 09 Subscript out of range       51 Internal error
; 10 Redimensioned array          52 Bad file number
; 11 Division by zero             53 File not found
; 12 Illegal direct               54 File already open
; 13 Type mismatch                55 Input past end
; 14 Out of string space          56 Bad file name
; 15 String too long              57 Direct statement in file
; 16 String formula too complex   58 Sequential I/O only
; 17 Can't CONTINUE               59 File not OPEN
; 18 Undefined user function

;---------------------------------------------------------------------------------------------------------
; MSX BASIC WORK AREAS
;---------------------------------------------------------------------------------------------------------

BASIC_DAC:    equ 0xF7F6    ; 16
BASIC_ARG:    equ 0xF847    ; 16
BASIC_VALTYP: equ 0xF663
BASIC_RNDX:   equ 0xF857
BASIC_BUF:    equ 0xF55E    ; 259
BASIC_KBUF:   equ 0xF41F    ; 318
BASIC_SWPTMP: equ 0xF7BC    ; 8
BASIC_STRBUF: equ 0xF7C5    ; 43
BASIC_TXTTAB: equ 0xF676
BASIC_VARTAB: equ 0xF6C2
BASIC_ARYTAB: equ 0xF6C4
BASIC_STREND: equ 0xF6C6
BASIC_STKTOP: equ 0xF674
BASIC_FRETOP: equ 0xF69B
BASIC_MEMSIZ: equ 0xF672

BASIC_TEMPPT: equ 0xF678    ; 2	Starting address of unused area of temporary descriptor.
BASIC_TEMPST: equ 0xF67A    ; 30 Temporary descriptors.

BASIC_DATPTR: equ 0xF6C8    ; 2 Pointer to next data to read from the instruction DATA. Modified by RESTORE.
BASIC_DATLIN: equ 0xF6A3    ; 2 Número de linha do comando DATA para o comando READ.
BASIC_DORES:  equ 0xF664    ; 1 Usada pelo comando DATA para manter o texto no formato ASCII.
BASIC_DEFTBL: equ 0xF6CA    ; 26 table of variables defined by DEFINT, DEFSTR, DEFSNG and DEFDBL for each alphabet letter (2 = integer, 3 = String, 4 = Simple precision, 8 = Double precision).

BASIC_CURLIN: equ 0xF41C    ; BASIC current line number
BASIC_INTVAL: equ 0xFCA0    ; interval value
BASIC_INTCNT: equ 0xFCA2    ; interval current count

BASIC_PRMPRV: equ 0xF74C    ; Pointer to previous parameter block in PARM1

BASIC_TRPTBL: equ 0xFC4C    ; 78 trap table - array of 3 bytes - state[1] (bit 0=on, bit 1=stop, bit 2=active) + address[2]

BASIC_TRPTBL_KEY:        equ 0xFC4C  ; 30 ON KEY GOSUB
BASIC_TRPTBL_STOP:       equ 0xFC6A  ; 3  ON STOP GOSUB
BASIC_TRPTBL_SPRITE:     equ 0xFC6D  ; 3  ON SPRITE GOSUB
BASIC_TRPTBL_STRIG:      equ 0xFC70  ; 15 ON STRIG GOSUB
BASIC_TRPTBL_INTERVAL:   equ 0xFC7F  ; 3  ON INTERVAL GOSUB
BASIC_TRPTBL_OTHER:      equ 0xFC82  ; 24 reserved for expansion

BASIC_ONGSBF:            equ 0xFBD8  ; 1  trap occurred counter (0=not occurred)



;--------------------------------------------------------
; MATH PACK ROUTINES
;--------------------------------------------------------
FAST_MATH: EQU 1

;--------------------------------------------------------
; SUPPORT MACROS
;--------------------------------------------------------
COMPILE_TO_ROM: EQU 1

MACRO __call_basic,CALL_PARM
    ld ix, CALL_PARM
    call BIOS_CALBAS
ENDM

if defined COMPILE_TO_DOS

  MACRO __call_bios,CALL_PARM
    ;ld iy,(BIOS_EXPTBL-1)
    ld ix, CALL_PARM
    call BIOS_CALBAS   ; BIOS_CALSLT
  ENDM

else

  MACRO __call_bios,CALL_PARM
    call CALL_PARM
  ENDM

endif

MACRO push.parm
    push hl ; save parameter
ENDM

MACRO pop.parm
    pop iy  ; restore PC of caller
    pop hl  ; get next parameter
    push iy ; save PC of caller
ENDM

MACRO push.ret.parm
    pop iy  ; restore PC of caller
    push hl ; save return parameter
    push iy ; save PC of caller
ENDM

MACRO ret.parm
    pop iy         ; restore PC of caller
    push hl        ; save return parameter
    push iy        ; save PC of caller
    ret            ; return
ENDM

MACRO set.line.number, line_number
    ld bc, line_number          ; current line number
    ld (BASIC_CURLIN), bc
ENDM

MACRO verify.break
    ld a, (BIOS_INTFLG)         ; verify CTRL+BREAK
    or a
    jp nz, end_pgm
ENDM

MACRO check.traps
     ld a, (BASIC_ONGSBF)       ; trap occured counter
     or a
     call nz, RUN_TRAPS
ENDM


;---------------------------------------------------------------------------------------------------------
; PROGRAM HEADER
;---------------------------------------------------------------------------------------------------------

    romSize:       equ  0x8000                 ; ROM size (32k)
    pageSize:      equ  0x4000                 ; Page size (16k)
    lowLimitSize:  equ  0x400                  ; 10% of a page size

if defined COMPILE_TO_BIN

    pgmArea:    equ  0x8000                 ; page 2 - program area
    ramArea:    equ  0xc000                 ; page 3 - free RAM start area

    org  pgmArea                ; program binary type start address
    db	 0FEh		            ; binary file ID
    dw	 start_pgm	            ; begin address
    dw	 end_file - 1	        ; end address
    dw	 start_pgm	            ; program execution address (for ,R option)

else
if defined COMPILE_TO_ROM

    pgmArea:    equ  0x4000                 ; page 1 and 2 - program area
    ramArea:    equ  0xc000                 ; page 3 - free RAM start area

    org  pgmArea                ; program rom type start address
    db   'AB'                   ; rom file ID
    dw   start_pgm              ; INIT
    dw   0x0000                 ; STATEMENT
    dw   0x0000                 ; DEVICE
    dw   0x0000                 ; TEXT
    ds   6,0                    ; RESERVED

else

    pgmArea:    equ  0x8000                 ; page 2 - program area
    ramArea:    equ  0xc000                 ; page 3 - free RAM start area

    org  pgmArea                ; program DOS type start address    ; 0x0100

endif
endif


;---------------------------------------------------------------------------------------------------------
; PROGRAM ROUTINES
;---------------------------------------------------------------------------------------------------------

if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS

PROGRAM_TO_BASIC:
    call PROGRAM_SLOT_2_RESTORE
    __call_basic BASIC_READYR    ; warm start Basic
    ret

PROGRAM_SLOT_2_SAVE:
    ld h,080h
    call PROGRAM_SLOT_GET
    ld (BIOS_RAMAD2), a ; Save RAM slot of page 8000h-BFFFh
    ret

PROGRAM_SLOT_2_RESTORE:
    ld a, (BIOS_RAMAD2)
	ld h,080h
	jp BIOS_ENASLT	    ; Select the RAM on page 8000h-BFFFh

PROGRAM_SLOT_2_ENABLE:
    call BIOS_RSLREG
	rrca
	rrca
	and	3	;Keep bits corresponding to the page
	ld	c,a
	ld	b,0
	ld	hl,BIOS_EXPTBL
	add	hl,bc
	ld	a,(hl)
	and	80h
	or	c
	ld	c,a
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	ld	a,(hl)
	and	0Ch
	or	c
	ld	h,080h
	jp BIOS_ENASLT		; Select the ROM on page 8000h-BFFFh

; h = memory page
; a <- slot ID formatted FxxxSSPP
; Modifies: af, bc, de, hl
; ref: https://www.msx.org/forum/msx-talk/development/fusion-c-and-htimi#comment-366469
PROGRAM_SLOT_GET:
	call BIOS_RSLREG
	bit 7,h
	jr z,PrimaryShiftContinue
	rrca
	rrca
	rrca
	rrca
PrimaryShiftContinue:
	bit 6,h
	jr z,PrimaryShiftDone
	rrca
	rrca
PrimaryShiftDone:
	and 00000011B
	ld c,a
	ld b,0
	ex de,hl
	ld hl,BIOS_EXPTBL
	add hl,bc
	ld c,a
	ld a,(hl)
	and 80H
	or c
	ld c,a
	inc hl  ; move to SLTTBL
	inc hl
	inc hl
	inc hl
	ld a,(hl)
	ex de,hl
	bit 7,h
	jr z,SecondaryShiftContinue
	rrca
	rrca
	rrca
	rrca
SecondaryShiftContinue:
	bit 6,h
	jr nz,SecondaryShiftDone
	rlca
	rlca
SecondaryShiftDone:
	and 00001100B
	or c
	ret

endif

if defined COMPILE_TO_DOS

BIOS_SLOT_ENABLE:
               ld a, (BIOS_EXPTBL)
               ld hl,0
               __call_bios BIOS_ENASLT ; Select main ROM on page 0 (0000h~3FFFh)
               ret

endif



;---------------------------------------------------------------------------------------------------------
; PROGRAM MAIN CODE
;---------------------------------------------------------------------------------------------------------

start_pgm:                               ; start of the program

    if defined COMPILE_TO_DOS

       call BIOS_SLOT_ENABLE   ; enable bios on page 0
       ;call BASIC_SLOT_ENABLE  ; enable basic on page 1

    endif

    if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS

       call PROGRAM_SLOT_2_SAVE     ; save slot on page 2
       call PROGRAM_SLOT_2_ENABLE   ; enable program on page 2

    endif

    __call_bios BIOS_ERAFNK      ; turn off function keys display
    __call_bios BIOS_GICINI      ; initialize sound system
    __call_bios BIOS_INITXT      ; initialize text screen
    xor a
    ld (BIOS_CLIKSW), a          ; disable keyboard click
    ld bc, 0xFFFF                ;
    ld (BASIC_CURLIN), bc        ; interpreter in direct mode
    __call_basic BASIC_TRAP_CLEAR        ; clear traps work space
    ;call INITIALIZE_PARAMETERS   ; initialize parameters stack
    call memory.init             ; initialize memory allocation
    call INITIALIZE_VARIABLES    ; initialize variables


TAG_10:
            ld hl, LIT_4                ; parameter
            push.parm
            call SET_PLAY_VOICE_1       ; action call
            ld hl, LIT_6                ; parameter
            push.parm
            call SET_PLAY_VOICE_2       ; action call
            call DO_PLAY                ; action call

TAG_20:
            ld hl, LIT_11               ; parameter
            push.parm
            call INPUT.FUNCTION.STR     ; action call
            ld hl, IDF_8                ; parameter
            push.parm
            call LET                    ; action call

TAG_30:
            ld hl, LIT_12               ; parameter
            push.parm
            call SET_PLAY_VOICE_1       ; action call
            ld hl, LIT_13               ; parameter
            push.parm
            call SET_PLAY_VOICE_2       ; action call
            call DO_PLAY                ; action call

TAG_40:
            ld hl, LIT_14               ; parameter
            push.parm
            call INPUT.FUNCTION.STR     ; action call
            ld hl, IDF_8                ; parameter
            push.parm
            call LET                    ; action call

TAG_50:
            ld hl, LIT_15               ; parameter
            push.parm
            call SET_PLAY_VOICE_1       ; action call
            call DO_PLAY                ; action call

;---------------------------------------------------------------------------------------------------------
; PROGRAM END CODE
;---------------------------------------------------------------------------------------------------------

    end_pgm:    __call_bios BIOS_DSPFNK      ; turn on function keys display
                ld a, 1                      ;
                ld (BIOS_CLIKSW), a          ; enable keyboard click

                if defined COMPILE_TO_ROM
                    jp PROGRAM_TO_BASIC
                else
                   if defined COMPILE_TO_DOS
                      ; go back to DOS
                   else
                      __call_basic BASIC_READYR    ; warm start Basic
                   endif
                endif

                ret                          ; end of the program

                ;__call_bios BIOS_GICINI      ; initialize sound system
                ;if defined COMPILE_TO_DOS or defined COMPILE_TO_ROM
                ;   __call_bios BIOS_RESET       ; restart Basic
                ;else
                ;   __call_basic BASIC_END       ; end to Basic
                ;endif


;---------------------------------------------------------------------------------------------------------
; MSX BASIC KEYWORDS
;---------------------------------------------------------------------------------------------------------

; keyword
LET:

                    ; out IX = variable assigned address
                    pop.parm                ; get variable address parameter
                    push hl                 ; just to transfer hl to ix
                    pop ix                  ;
                    ld a, (ix)              ; get variable type
                    cp 3                    ; test if string
                    jr nz, LET.PARM         ; if not a string, it isn't necessary to free memory
                    ld a, (ix + 3)          ; get variable string length
                    or a                    ; cp 0
                    jr z, LET.PARM          ; if zero, it isn't necessary to free memory
                    ld c, (ix + 4)          ; get old string address low
                    ld b, (ix + 5)          ; get old string address high
                    push ix                 ; save variable address
                      push bc               ; just to transfer bc (old string address) to ix
                      pop ix                ;
                      call memory.free      ; free memory
                    pop ix                  ; restore variable address
        LET.PARM:   pop.parm                ; get data address parameter (out hl = data address)
                    ld a, (ix + 2)          ; get variable type flag
                    or a                    ; cp 0 - test type flag (0=any, 255=fixed)
                    jr nz, LET.FIXED        ; if type flag is fixed, so casting is necessary
        LET.ANY:    push ix                 ; just to transfer ix (variable address) to de
                    pop de                  ;
                    ldi                     ; copy 1 byte from hl (data address) to de (variable address)
                    inc de                  ; go to variable data area
                    inc de                  ;
                    inc hl                  ; go to data data area
                    inc hl                  ;
                    ld bc, 8                ; data = 8 bytes
                    ldir                    ; copy bc bytes from hl (data address) to de (variable address)
                    ld a, (ix)              ; get variable type
                    cp 3                    ; test if string
                    ret nz                  ; if not string, return
                    jp LET.STRING           ; else do string treatment (in ix = variable address)
        LET.FIXED:  push ix                 ; save variable destination address
		            push hl                 ; save variable source address
                      ld a, (ix)            ; get variable fixed type, and hl has parameter data address
                      call CAST_TO          ; cast data to type (in hl = variable address, a = type to, out hl = casted data address)
					pop de
                    pop ix                  ; restore variable address
                    ld a, (ix)              ; get variable destination type again
                    cp 3                    ; test if string
                    jr nz, LET.VALUE        ; if not string, do value treatment
					ld a, (de)              ; get variable source type again
                    cp 3                    ; test if string
                    jr nz, LET.FIX1         ; if not string, get casted string size
					inc de
					inc de
					inc de
					ld a, (de)
					ld (ix + 3), a          ; source string size
					jr LET.FIX2
		LET.FIX1:   call GET_STR.LENGTH     ; get string length (in HL, out a)
                    ld (ix + 3), a          ; set variable length
		LET.FIX2:   ld (ix + 4), l          ; casted data address low
                    ld (ix + 5), h          ; casted data address high
                    jp LET.STRING           ; do string treatment (in ix = variable address)
        LET.VALUE:  push ix                 ; just to transfer ix (variable address) to de
                    pop de                  ;
                    inc de                  ; go to variable data area (and the data from its casted)
                    inc de                  ;
                    inc de                  ;
                    ld bc, 8                ; data = 8 bytes
                    ldir                    ; copy bc bytes from hl (data address) to de (variable address)
                    ret                     ;
        LET.STRING: ld a, (ix + 3)          ; string size
                    or a                    ; cp 0 - test if null
                    jr nz, LET.ALLOC        ; if not null, allocate new string (in ix = variable address)
                    ld bc, LIT_NULL_STR     ; else, set to a null string literal
                    ld (ix + 4), c          ; variable address low
                    ld (ix + 5), b          ; variable address high
                    ret                     ;
        LET.ALLOC:  push ix                 ; save variable address
                      ld l, (ix + 4)        ; source string address low
                      ld h, (ix + 5)        ; source string address high
                      push hl               ; save copy from address
                        ld c, (ix + 3)      ; get variable length
                        ld b, 0             ;
                        inc bc              ; string length have one more byte from zero terminator
                        push bc             ; save variable lenght + 1
                          call memory.alloc ; in bc = size, out ix = address, nz=OK
		                  jp z, memory.error
                          push ix           ; just to transfer memory address from ix to de
                          pop de            ;
                        pop bc              ; restore bytes to be copied
                      pop hl                ; restore copy from string address
                      push de               ; save copy to address
                        ldir                ; copy bc bytes from hl (data address) to de (variable address)
						;xor a
						;ld (de), a
                      pop de                ; restore copy to address
                    pop ix                  ; restore variable address
                    ld (ix + 4), e          ; put memory address low into variable
                    ld (ix + 5), d          ; put memory address high into variable
                    ret                     ; variable assigned
        
; keyword
BOOLEAN.IF:

                       pop.parm               ; get parameter boolean result in hl
                       push hl                ; ix = hl
                       pop ix                 ;
                       ld a, (ix+5)           ; put boolean integer result in a
                       ret                    ;
        
; keyword
SET_PLAY_VOICE_1:

					ld a, 0
					jp SET_PLAY_VOICE
        
; keyword
SET_PLAY_VOICE_2:

					ld a, 1
					jp SET_PLAY_VOICE
        
; keyword
DO_PLAY:

             jp PLAY_HOOK
        
; keyword
INPUT.FUNCTION.STR:

                    pop.parm                ; get first parameter
                    call CAST_TO.INT        ;
                    call GET_INT.VALUE      ; output BC with integer value
                    call GET_STRING_SPACE   ; in bc = size, out hl = address, out a = string size
                    or a
                    jr z, INPUT.FUNCTION.STR.2
                    push af
                    push hl
                      ld b, a
            INPUT.FUNCTION.STR.1:
                      __call_bios BIOS_CHGET
                      ld (hl), a
                      inc hl
                      xor a
                      ld (hl), a
                      djnz INPUT.FUNCTION.STR.1
					pop hl
					pop af
            INPUT.FUNCTION.STR.2:
                    call COPY_TO.VAR_DUMMY.STR  ; create a fake string variable from HL in HL
                    ret.parm                    ;
        


;---------------------------------------------------------------------------------------------------------
; MSX BASIC SUPPORT CODE
;---------------------------------------------------------------------------------------------------------

if defined CHR or defined INKEY.STR

; in a = char
; out hl = string
COPY_CHAR_TO_STR:
              push af
                ld bc, 2                     ; string size
                call memory.alloc          ; in bc size, out ix new memory address, nz=OK
                jr z, COPY_CHAR_TO_STR.ERROR
              pop af
              ld (ix), a
              xor a
              ld (ix+1), a
              push ix
              pop hl                       ; HL = string address
              ld a, 1                      ; A = size
              ret
COPY_CHAR_TO_STR.ERROR:
              pop af
              jp memory.error

endif

;if defined INPUT or defined LINE_INPUT or defined SPC or defined SPACE or defined INPUT.FUNCTION.STR

; bc = size
; out: a = size, hl = address
GET_STRING_SPACE:
              ld hl, LIT_NULL_STR
              ld a, c
              or a
              ret z
              push de
              push bc
                inc bc
                call memory.alloc       ; in bc = size, out ix = address, nz=OK
              pop bc
              pop de
              jp z, memory.error
              ld a, 32
              ld b, c
              push ix
GET_STRING_SPACE.1:
                ld (ix), a
                inc ix
                djnz GET_STRING_SPACE.1
                xor a
                ld (ix), a
              pop hl
              ld a, c   ; size
              ret

; in hl = string, out hl = temp string
COPY_TO.TEMP_STR:
              call GET_STR.LENGTH         ; get string length, in hl = address, out a = size
              ex de, hl
              ld c, a
              ld b, 0
              call GET_STRING_SPACE       ; in bc = size, out hl = address, out a = string size
              or 0
              ret z
              push af
              push hl
                push hl
                  call COPY_TO.VAR_DUMMY.STR  ; create a fake string variable from HL in HL
                pop hl
                ex de, hl
                ld c, a
                ld b, 0
                ldir                      ; copy bc bytes from hl to de
              pop hl
              pop af
              ret

;endif

if defined INPUT or defined LINE_INPUT

INPUT.CONTINUE:
              jr c, INPUT.EXIT            ; exit if CTRL+STOP

              inc hl                      ; string start
              call COPY_TO.TEMP_STR
              call COPY_TO.VAR_DUMMY.STR  ; make a fake string variable from HL
              push.parm                   ; LET parameter 2 - fake string variable as right operand
              ld hl, (BIOS_TEMP)
              push.parm                   ; LET parameter 1 - input variable as left operand
              call LET                    ; put string into variable
INPUT.EXIT:
              xor a                       ;
              ld (BIOS_CLIKSW), a         ; disable keyboard click
              ret                         ;

endif

if defined ON_ERROR or defined ON_INTERVAL or defined ON_KEY_START or defined ON_SPRITE or defined ON_STOP or defined ON_STRIG_START or defined TRAP_ENABLED or defined TRAP_DISABLED or defined TRAP_PAUSE or defined TRAP_UNPAUSE

RUN_TRAPS:    ld b, 26
              ld hl, BASIC_TRPTBL
RUN_TRAPS.1:  push hl
                push bc
                  call TRAP_HANDLER
                pop bc
              pop hl
              inc hl
              inc hl
              inc hl
              djnz RUN_TRAPS.1
              ret

; in hl = trap block address (handle trap: sts=5? has handler? ackn, pause, run trap, sts=1? unpause)
TRAP_HANDLER:
                ld a, (hl)    ; trap status
                cp 5          ; trap occured AND trap not paused AND trap enabled ?
                ret nz        ; return if false
                inc hl
                ld e, (hl)    ; get trap address
                inc hl
                ld d, (hl)
                dec hl
                dec hl
                ld a, d
                or e
                ret z         ; return if address zero
                push hl
                  __call_basic BASIC_TRAP_ACKNW
                  __call_basic BASIC_TRAP_PAUSE
                  ld hl, TRAP_HANDLER.1
                  ld a, (BASIC_ONGSBF)  ; save traps execution
                  push af
                  xor a
                  ld (BASIC_ONGSBF), a  ; disable traps execution
                  push hl  ; next return will be to trap handler
                  push de  ; indirect jump to trap address
                  ret
TRAP_HANDLER.1: pop af
                ld (BASIC_ONGSBF), a    ; restore traps execution
                pop hl
                ld a, (hl)
                cp 1       ; trap enabled?
                ret z
                __call_basic BASIC_TRAP_UNPAUSE
                ret

; hl = trap block, de = trap handler
SET_TRAP:       xor a
                ld (hl), a                  ; trap block status
                inc hl
                ld (hl), e                  ; trap block handler (pointer)
                inc hl
                ld (hl), d
                ret

endif

if defined SET_PLAY_VOICE_1 or defined SET_PLAY_VOICE_2 or defined SET_PLAY_VOICE_3 or defined DO_PLAY or defined MUSIC_PLAY or defined MUSIC_NEXT or defined MUSIC_STOP

   SET_PLAY_VOICE:
        ld (BIOS_TEMP), a       ; save voice number
        pop.parm
		ld a, (hl)
		cp 3
		ret nz                  ; return if not string
        call GET_STR.ADDR
    SET_PLAY_VOICE.1:
		ld (BIOS_TEMP2), a      ; save string size
        push hl                 ; string address
		  ld a, (BIOS_TEMP)     ; restore voice number
		  call BIOS_GETVCP      ; get PSG voice buffer address (in A = voice number, out HL = address of byte 2)
		pop de
		ld a, (BIOS_TEMP2)      ; restore string size
		ld (hl), a              ; string size
		inc hl
		ld (hl), e              ; string address
		inc hl
		ld (hl), d
		inc hl
        ld D,H                  ; voice stack
        ld E,L
        ld BC,001CH
        add HL,BC
        ex DE,HL
        ld (HL),E
        inc HL
        ld (HL),D
		ret

endif



;---------------------------------------------------------------------------------------------------------
; VARIABLES ROUTINES
;---------------------------------------------------------------------------------------------------------

; input hl = variable address
; input bc = variable name
; input d =  variable type
INIT_VAR:          ld (hl), d    ; variable type
                   inc hl
                   ld (hl), c    ; variable name 1
                   inc hl
                   ld (hl), b    ; variable name 2
                   ld a, d
                   cp 3
                   jp nz, CLEAR.VAR
                   ld de, LIT_NULL_STR
                   inc hl
                   ld (hl), 0
                   inc hl
                   ld (hl), e
                   inc hl
                   ld (hl), d
                   ld b, 5
                   jr CLEAR.VAR.LOOP
CLEAR.VAR:         ld b, 8
CLEAR.VAR.LOOP:    inc hl
                   ld (hl), 0    ; data address/value
                   djnz CLEAR.VAR.LOOP
                   ret
; input HL = variable address
; input A = variable output type
; output HL = casted data address
CAST_TO:           cp 2
                   jp z, CAST_TO.INT
                   cp 3
                   jp z, CAST_TO.STR
                   cp 4
                   jp z, CAST_TO.SGL
                   cp 8
                   jp z, CAST_TO.DBL
                   ret
; input HL = variable address
; output HL = variable address
CAST_TO.INT:       ;push af
                     ld a, (HL)
                     cp 2
                     jp z, GET_INT.ADDR
                     cp 3
                     jp z, CAST_STR_TO.INT
                     cp 4
                     jp z, CAST_SGL_TO.INT
                     cp 8
                     jp z, CAST_DBL_TO.INT
                   ;pop af
                   ret
; input HL = variable address
; output HL = variable address
CAST_TO.STR:       ;push af
                     ld a, (HL)
                     cp 2
                     jp z, CAST_INT_TO.STR
                     cp 3
                     jp z, GET_STR.ADDR
                     cp 4
                     jp z, CAST_SGL_TO.STR
                     cp 8
                     jp z, CAST_DBL_TO.STR
                   ;pop af
                   ret
; input HL = variable address
; output HL = variable address
CAST_TO.SGL:       ;push af
                     ld a, (HL)
                     cp 2
                     jp z, CAST_INT_TO.SGL
                     cp 3
                     jp z, CAST_STR_TO.SGL
                     cp 4
                     jp z, GET_SGL.ADDR
                     cp 8
                     jp z, CAST_DBL_TO.SGL
                   ;pop af
                   ret
; input HL = variable address
; output HL = variable address
CAST_TO.DBL:       ;push af
                     ld a, (hl)
                     cp 2
                     jp z, CAST_INT_TO.DBL
                     cp 3
                     jp z, CAST_STR_TO.DBL
                     cp 4
                     jp z, CAST_SGL_TO.DBL
                     cp 8
                     jp z, GET_DBL.ADDR
                   ;pop af
                   ret
CAST_SGL_TO.STR:                           ; same as CAST_INT_TO.STR
CAST_DBL_TO.STR:                           ; same as CAST_INT_TO.STR
CAST_INT_TO.STR:   call COPY_TO.DAC
                   xor a
                   __call_bios MATH_FOUT    ; convert DAC to string
                   call COPY_TO.TEMP_STR
                   ret
CAST_INT_TO.SGL:   call COPY_TO.DAC
                   __call_bios MATH_FRCSGL
                   ld hl, BASIC_DAC
                   ret
CAST_INT_TO.DBL:   call COPY_TO.DAC
                   __call_bios MATH_FRCDBL
                   ld hl, BASIC_DAC
                   ret
CAST_SGL_TO.INT:                           ; same as CAST_DBL_TO.INT
CAST_DBL_TO.INT:   call COPY_TO.DAC
                   __call_bios MATH_FRCINT
                   ld hl, BASIC_DAC
                   ret
CAST_STR_TO.INT:   ld a, 2
                   call CAST_STR_TO.VAL    ;
                   if defined BCD_MATH
                      __call_bios MATH_FRCINT
                   endif
                   ld hl, BASIC_DAC        ;
                   ret                     ;
CAST_STR_TO.SGL:   ld a, 4
                   call CAST_STR_TO.VAL    ;
                   if defined BCD_MATH
                      __call_bios MATH_FRCSGL
                   endif
                   ld hl, BASIC_DAC        ;
                   ret                     ;
CAST_STR_TO.DBL:   ld a, 8
                   call CAST_STR_TO.VAL    ;
                   if defined BCD_MATH
                      __call_bios MATH_FRCDBL
                   endif
                   ld hl, BASIC_DAC        ;
                   ret                     ;
CAST_STR_TO.VAL:   ld (BASIC_VALTYP), a
                   call GET_STR.ADDR       ;
                   ld a, (hl)              ;
                   __call_bios MATH_FIN    ; convert string to a value type
                   ld hl, BASIC_DAC        ;
                   ret                     ;
GET_INT.VALUE:     inc hl                  ; output BC with integer value
                   inc hl                  ;
                   ld c, (hl)              ;
                   inc hl                  ;
                   ld b, (hl)              ;
                   ret                     ;
CAST_SGL_TO.DBL:                           ; same as GET_DBL.ADDR
CAST_DBL_TO.SGL:                           ; same as GET_DBL.ADDR
GET_INT.ADDR:                              ; same as GET_DBL.ADDR
GET_SGL.ADDR:                              ; same as GET_DBL.ADDR
GET_DBL.ADDR:      inc hl
                   inc hl
                   inc hl
                   ;pop af
                   ret
GET_STR.ADDR:      push hl
                   pop ix
				   ld a, (ix + 3)
                   ld l, (ix + 4)
                   ld h, (ix + 5)
                   ret
; input hl = string address
; output a = string length
GET_STR.LENGTH:    push hl
                     ld bc, 255
                     xor a
                     cpir      ; hl = source, a = compare char, bc = count
                   pop hl
                   ret nz      ; not found
                   ld a, c
                   cpl
                   dec a
                   ret
STRING.COMPARE:    ld ix, (BASIC_DAC+1)     ; string 1
                   ld iy, (BASIC_ARG+1)     ; string 2
STRING.COMPARE.NX: ld a, (ix)               ; next char from string 1
                   cp (iy)                  ; char s1 = char s2?
                   jr nz, STRING.COMPARE.NE ; if not equal...
                   cp 0                     ;
                   jr z, STRING.COMPARE.F1  ; if string 1 has finished...
                   ld a, (iy)               ; next char from string 2
                   cp 0                     ;
                   jr z, STRING.COMPARE.GT  ; if s2 has finished, s1 has not finished yet, so s1 is greater than s2
                   inc ix                   ;
                   inc iy                   ;
                   jr STRING.COMPARE.NX     ; get next char pair
STRING.COMPARE.F1: ld a, (iy)               ; verify if string 2 has finished too
                   cp 0                     ;
                   jr z, STRING.COMPARE.EQ  ; if s2 has finished, then they are equals
                   jr STRING.COMPARE.LT     ; else, result = s1 is less than s2
STRING.COMPARE.NE: jr c, STRING.COMPARE.GT  ; verify if s1 is greater than s2...
STRING.COMPARE.LT: ld a, 1                  ; ...else, result = s1 less than s2
                   ret                      ;
STRING.COMPARE.GT: ld a, 0xFF               ; result = s1 is greater than s2
                   ret                      ;
STRING.COMPARE.EQ: xor a                    ; result = s1 is equal to s2
                   ret                      ;
STRING.CONCAT:     ld ix, BASIC_DAC           ; s1 size
				   ld a, (BASIC_ARG)          ; s2 size
				   add a, (ix)                ; s3 size = s1 size + s2 size
				   push af
                     ld b, 0
                     ld c, a                    ;
                     inc bc                     ; add 1 byte to size
                     call memory.alloc          ; in bc size, out ix new memory address, nz=OK
                     jp z, memory.error         ;
                     push ix                    ; save ix
                       push ix                  ; save ix
                       pop de                   ; de = ix
					   ld a, (BASIC_DAC)        ; s1 size
                       ld hl, (BASIC_DAC + 1)   ; string 1
                       call COPY_TO.STR         ; copy to new memory
					   ld a, (BASIC_ARG)        ; s2 size
                       ld hl, (BASIC_ARG + 1)   ; string 2
                       call COPY_TO.STR         ; copy to new memory
					   xor a
					   ld (de), a               ; null terminated
                     pop hl                     ; hl = ix
                   pop af
                   call COPY_TO.VAR_DUMMY.STR ;
                   ret.parm                   ; WARNING - VERIFY STRING MEMORY LEAKs
STRING.PRINT:      ld a, (BIOS_SCRMOD)        ; 0=40x24 Text Mode, 1=32x24 Text Mode, 2=Graphics Mode, 3=Multicolour Mode
                   cp 5                       ;
                   jr nc, STRING.PRINT.G2     ; jump if graphic screen mode MSX2 (>=5)
                   cp 2                       ;
                   jr nc, STRING.PRINT.G1     ; jump if graphic screen mode MSX1 (>=2)
STRING.PRINT.T:    ld a, (hl)                 ; get a char from a string parameter
                   or a                       ; cp 0 - is it the string end?
                   ret z                      ; exit if yes
                   __call_bios BIOS_CHPUT     ; put the char (a) into text screen
                   inc hl                     ; next char
                   jr STRING.PRINT.T          ; repeat
STRING.PRINT.G1:   ld a, (hl)                 ; get a char from a string parameter
                   or a                       ; cp 0 - is it the string end?
                   ret z                      ; exit if yes
                   __call_bios BIOS_GRPPRT    ; put the char (a) into graphical screen
                   inc hl                     ; next char
                   jr STRING.PRINT.G1         ; repeat
STRING.PRINT.G2:   ld a, (hl)                 ; get a char from a string parameter
                   or a                       ; cp 0 - is it the string end?
                   ret z                      ; exit if yes
                   ld ix, BIOS_GRPPRT2        ; put the char (a) into graphical screen
                   call BIOS_EXTROM
                   inc hl                     ; next char
                   jr STRING.PRINT.G2         ; repeat

; a = string size to copy
; input hl = string from
; input de = string to
COPY_TO.STR:       or a
                   ret z                      ; avoid copy if size = zero
                   ld b, 0
                   ld c, a                    ; string size
                   ldir                       ; copy bc bytes from hl to de
                   ret                        ;
COPY_TO.BASIC_BUF: ld bc, BASIC_BUF
                   ld a, (LIT_QUOTE_CHAR)
                   ld (bc), a
                   inc bc
COPY_BAS_BUF.LOOP: ld a, (hl)
                   or a                      ; cp 0
                   jr z, COPY_BAS_BUF.EXIT
                   ld (bc), a
                   inc bc
                   inc hl
                   jr COPY_BAS_BUF.LOOP
COPY_BAS_BUF.EXIT: ld a, (LIT_QUOTE_CHAR)
                   ld (bc), a
                   inc bc
                   xor a
                   ld (bc), a
                   ld hl, BASIC_BUF
                   ret
COPY_TO.VAR_DUMMY:     ld a, (BASIC_VALTYP)    ; create dummy variable from VALTYPE
                       cp 3                    ;
                       jr nz, COPY_TO.VAR_DUMMY.DBL
                       call GET_STR.LENGTH     ; get string length, in hl = address, out a = size
COPY_TO.VAR_DUMMY.STR: call GET_VAR_DUMMY.ADDR ; create dummy string variable from HL
                       ld (ix), 3            ; data type string
                       ld (ix+1), 0          ;
                       ld (ix+2), 255        ; var type fixed
                       ld (ix+3), a          ; string length
                       ld (ix+4), l          ; data address low
                       ld (ix+5), h          ; data address high
                       push ix               ; output var address...
                       pop hl                ; ...into hl
                       ret                   ;
COPY_TO.VAR_DUMMY.INT: call GET_VAR_DUMMY.ADDR ; create dummy integer variable from BC
                       ld (ix),    2           ; data type string
                       ld (ix+1),  0           ;
                       ld (ix+2),  0           ;
                       ld (ix+3),  0           ;
                       ld (ix+4),  0           ;
                       ld (ix+5),  c           ;
                       ld (ix+6),  b           ;
                       ld (ix+7),  0           ;
                       ld (ix+8),  0           ;
                       ld (ix+9),  0           ;
                       ld (ix+10), 0           ;
                       push ix                 ; output var address...
                       pop hl                  ; ...into hl
                       ret                     ;
COPY_TO.VAR_DUMMY.DBL: call GET_VAR_DUMMY.ADDR  ; create dummy value variable from DAC
                       ld (ix), a            ; data type
                       ld (ix+1), 0          ;
                       ld (ix+2), 0          ;
                       ld bc, 8              ;
                       ld hl, BASIC_DAC      ;
                       push ix               ; just to copy ix to de
                       pop de                ;
                       inc de                ;
                       inc de                ;
                       inc de                ;
                       ldir                  ; copy bc bytes from hl (data address) to de (variable address)
                       push ix               ; output var address...
                       pop hl                ; ...into hl
                       ret                   ;
GET_VAR_DUMMY.ADDR:    push af                       ;
                       push de
                         ld de, 11                   ;
                         ld ix, (VAR_DUMMY.POINTER)  ;
                         ld a, (VAR_DUMMY.COUNTER)   ;
GET_VAR_DUMMY.NEXT:      add ix, de                  ;
                         inc a                       ;
                         cp VAR_DUMMY.SIZE           ;
                         jr nz, GET_VAR_DUMMY.EXIT   ;
                           xor a                     ;
                           ld ix, VAR_DUMMY.DATA     ;
GET_VAR_DUMMY.EXIT:      ld (VAR_DUMMY.POINTER), ix  ;
                         ld (VAR_DUMMY.COUNTER), a   ;
						 ld a, (ix)                  ; get last var dummy type
						 cp 3                        ; is it string?
						 call z, GET_VAR_DUMMY.FREE  ; free string memory
                       pop de
                       pop af                        ;
                       ret                           ;
GET_VAR_DUMMY.FREE:
                   push hl
                   push ix
					 ld a, (ix+3)                    ; string size
				     ld l, (ix+4)                    ; get string data address
					 ld h, (ix+5)
					 push hl
					 pop ix
					 or a
                     call nz, memory.free            ; free memory
				   pop ix
				   pop hl
				   ret
; input hl = variable address
COPY_TO.DAC:       ld de, BASIC_DAC
COPY_TO.DAC.DATA:  ld a, (hl)
                   ld (BASIC_VALTYP), a
                   inc hl
                   inc hl
                   inc hl
                   ld bc, 8                ; data = 8 bytes
                   ldir                    ; copy bc bytes from hl (data address) to de (variable address)
                   ret
COPY_TO.ARG:       ld de, BASIC_ARG        ;
                   jr COPY_TO.DAC.DATA     ;
COPY_TO.DAC_ARG:   ld hl, BASIC_DAC        ;
                   ld de, BASIC_ARG        ;
                   ld bc, 8                ; data = 8 bytes
                   ldir                    ; copy bc bytes from hl (data address) to de (variable address)
                   ret                     ;
COPY_TO.ARG_DAC:   ld hl, BASIC_ARG        ;
                   ld de, BASIC_DAC        ;
                   ld bc, 8                ; data = 8 bytes
                   ldir                    ; copy bc bytes from hl (data address) to de (variable address)
                   ret                     ;
COPY_TO.DAC_TMP:   ld hl, BASIC_DAC        ;
                   ld de, BASIC_SWPTMP     ;
                   ld bc, 8                ; data = 8 bytes
                   ldir                    ; copy bc bytes from hl (data address) to de (variable address)
                   ret                     ;
COPY_TO.TMP_DAC:   ld hl, BASIC_SWPTMP     ;
                   ld de, BASIC_DAC        ;
                   ld bc, 8                ; data = 8 bytes
                   ldir                    ; copy bc bytes from hl (data address) to de (variable address)
                   ret                     ;
SWAP.DAC.ARG:      di
                   exx                     ; save registers
                     ld bc, 8              ;
                     ld hl, BASIC_DAC      ;
                     ld de, BASIC_SWPTMP   ;
                     ldir                  ; copy bc bytes from hl to de
                     ld bc, 8              ;
                     ld hl, BASIC_ARG      ;
                     ld de, BASIC_DAC      ;
                     ldir                  ; copy bc bytes from hl to de
                     ld bc, 8              ;
                     ld hl, BASIC_SWPTMP   ;
                     ld de, BASIC_ARG      ;
                     ldir                  ; copy bc bytes from hl to de
                   exx                     ; restore registers
                   ei
                   ret                     ;
CLEAR.DAC:         ld de, BASIC_DAC
CLEAR.DAC.DATA:    ld hl, BASIC_VALTYP
                   ld (hl), 2
                   ld hl, LIT_NULL_DBL
                   ld bc, 8                ; data = 8 bytes
                   ldir                    ; copy bc bytes from hl (data address) to de (variable address)
                   ret
CLEAR.ARG:         ld de, BASIC_ARG
                   jr CLEAR.DAC.DATA



;---------------------------------------------------------------------------------------------------------
; MATH 16 BITS ROUTINES
;---------------------------------------------------------------------------------------------------------

MATH.PARM.POP:  pop af                       ; get PC from caller stack
                ex af, af'                   ; save PC to temp
                  pop.parm                   ; get first parameter
                  call COPY_TO.ARG           ; put HL in ARG (return var type in A)
                  pop.parm                   ; get second parameter
                ex af, af'                   ; restore PC from temp
                push af                      ; put again PC from caller in stack
                ex af, af'                   ; restore 1st data type
                push af                      ; save 1st data type
                  call COPY_TO.DAC           ; put HL in DAC (return var type in A)
                pop bc                       ; restore 1st data type (ARG) in B
                cp b                         ; test if data type in A (DAC) = data type in B (ARG)
                ret z                        ; return if is equal data types
MATH.PARM.CAST: push bc                      ; else cast both to double
                  and 12                     ; test if single/double
                  jr nz, MATH.PARM.CST1      ; avoid cast if already single/double
                  __call_bios MATH_FRCDBL    ; convert DAC to double
MATH.PARM.CST1: pop af                       ;
                and 12                       ; test if single/double
                jr nz, MATH.PARM.CST2        ; avoid cast if already single/double
                ld (BASIC_VALTYP), a         ;
                call COPY_TO.DAC_TMP         ;
                call COPY_TO.ARG_DAC         ;
                __call_bios MATH_FRCDBL      ; convert ARG to double
                call COPY_TO.DAC_ARG         ;
                call COPY_TO.TMP_DAC         ;
MATH.PARM.CST2: ld a, 8                      ;
                ld (BASIC_VALTYP), a         ;
                ret                          ;
MATH.PARM.POP.INT:                           ; return result in DAC/ARG as integer
                pop af                       ; get PC from caller stack
                  ex af, af'                 ; save PC to temp
                    pop.parm                 ; get first parameter
                    ld a, (hl)               ; get parameter type
                    and 2                    ; test if integer
                    jr z, MATH.PARM.POP.I1   ; do cast if not integer
                    call COPY_TO.ARG         ; put HL in ARG (return var type in A)
                    jr MATH.PARM.POP.I2      ; go to next parameter
MATH.PARM.POP.I1:   call COPY_TO.DAC         ; put HL in DAC (return var type in A)
                    __call_bios MATH_FRCINT  ; convert DAC to int
                    call COPY_TO.DAC_ARG     ; copy DAC to ARG
MATH.PARM.POP.I2:   pop.parm                 ; get second parameter
                    call COPY_TO.DAC         ; put HL in DAC (return var type in A)
                    and 2                    ; test if integer
                    jr nz, MATH.PARM.POP.I3  ; avoid cast if already integer
                    __call_bios MATH_FRCINT  ; convert DAC to int
                    ld a, 2                  ;
                    ld (BASIC_VALTYP), a     ;
MATH.PARM.POP.I3:
                    ex af, af'                 ; restore PC from temp
                push af                      ; put again PC from caller in stack
                ret                          ;
MATH.PARM.PUSH: call COPY_TO.VAR_DUMMY       ;
                ret.parm                     ;

if defined MATH.ADD

; input DAC, ARG
; output in parm stack
; http://www.z80.info/zip/zaks_book.pdf - page 104
MATH.ADD.INT:  ld hl, (BASIC_DAC+2)  ;
               ld bc, (BASIC_ARG+2)  ;
               add hl, bc            ;
               ld (BASIC_DAC+2), hl  ;
               jp MATH.PARM.PUSH     ;

endif

if defined MATH.SUB or defined MATH.NEG

; input DAC, ARG
; output in parm stack
; http://www.z80.info/zip/zaks_book.pdf - page 104
MATH.SUB.INT:  ld hl, (BASIC_DAC+2)  ;
               ld de, (BASIC_ARG+2)  ;
               and a                 ; clear carry
               sbc hl, de            ;
               ld (BASIC_DAC+2), hl  ;
               jp MATH.PARM.PUSH     ;

endif

if defined MATH.MULT

; input DAC, ARG
; output in parm stack
MATH.MULT.INT: ld hl, (BASIC_DAC+2)  ;
               ld bc, (BASIC_ARG+2)  ;
               call MATH.MULT.16     ;
               ld (BASIC_DAC+2), hl  ;
               jp MATH.PARM.PUSH     ;

; input HL = multiplicand
; input BC = multiplier
; output HL = result
; http://www.z80.info/zip/zaks_book.pdf - page 131
MATH.MULT.16:  ld a, c				; low multiplier
               ld c, b				; high multiplier
               ld b, 16
               ld d, h 			; multiplicand
               ld e, l
               ld hl, 0
MULT16LOOP:    srl c 				; right shift multiplier high
               rra					; rotate right multiplier low
               jr nc, MULT16NOADD	; test carry
               add hl, de			; add multiplicand to result
MULT16NOADD:   ex de, hl
               add hl, hl			; double - shift multiplicand
               ex de, hl
               djnz MULT16LOOP
               ret

endif

if defined MATH.DIV or defined MATH.IDIV or defined MATH.MOD

; input AC = dividend
; input DE = divisor
; output AC = quotient
; output HL = remainder
; http://www.z80.info/zip/zaks_book.pdf - page 140
MATH.DIV.16:   ld hl, 0				; clear accumulator
               ld b, 16				; set counter
DIV16LOOP:     rl c					; rotate accumulator result left
               rla
               adc hl, hl				; left shift
               sbc hl, de				; trial subtract divisor
               jr nc, $ + 3			; subtract was OK ($ = current location)
               add hl, de				; restore accumulator
               ccf						; calculate result bit
               djnz DIV16LOOP			; counter not zero
               rl c 					; shift in last result bit
               rla
               ret

endif

if defined GFX_FAST or defined LINE

; compare two signed 16 bits integers
; HL < DE: Carry flag
; HL = DE: Zero flag
; http://www.z80.info/zip/zaks_book.pdf - page 531
MATH.COMP.S16: ld a, h                       ; test high order byte
               and 0x80                      ; test sign, clear carry
			   jr nz, MATH.COMP.S16.NEGM1    ; jump if hl is negative
			   bit 7, d
			   ret nz                        ; de is negative (and hl is positive)
			   ld a, h
			   cp d                          ; signs are both positive, so normal compare
			   ret nz
			   ld a, l                       ; test low order byte
			   cp e
               ret
MATH.COMP.S16.NEGM1:
               xor d
               rla                           ; sign bit into carry
               ret c                         ; signs different
               ld a, h
               cp d                          ; both signs negative
			   ret nz
			   ld a, l
			   cp e
			   ret

endif

if defined MATH.ADD

MATH.ADD.SGL:  ld a, 8                  ;
               ld (BASIC_VALTYP), a     ;
MATH.ADD.DBL:  __call_bios MATH_DECADD  ;
               jp MATH.PARM.PUSH        ;

endif

if defined MATH.SUB or defined MATH.NEG

MATH.SUB.SGL:  ld a, 8                  ;
               ld (BASIC_VALTYP), a     ;
MATH.SUB.DBL:  __call_bios MATH_DECSUB  ;
               jp MATH.PARM.PUSH        ;

endif

if defined MATH.MULT

MATH.MULT.SGL: ld a, 8                  ;
               ld (BASIC_VALTYP), a     ;
MATH.MULT.DBL: __call_bios MATH_DECMUL  ;
               jp MATH.PARM.PUSH        ;

endif

if defined MATH.DIV

; input DAC, ARG
; output in parm stack
MATH.DIV.INT:  __call_bios MATH_FRCDBL  ; convert DAC to double
               call SWAP.DAC.ARG        ;
               ld a, 2                  ;
               ld (BASIC_VALTYP), a     ;
               __call_bios MATH_FRCDBL  ; convert ARG to double
               call SWAP.DAC.ARG        ;
MATH.DIV.SGL:  ld a, 8                  ;
               ld (BASIC_VALTYP), a     ;
MATH.DIV.DBL:  __call_bios MATH_DECDIV  ;
               jp MATH.PARM.PUSH        ;

endif

if defined MATH.IDIV

; input DAC, ARG
; output in parm stack
MATH.IDIV.SGL: ld a, 8                  ;
               ld (BASIC_VALTYP), a     ;
MATH.IDIV.DBL: __call_bios MATH_FRCINT  ; convert DAC to integer
               call SWAP.DAC.ARG        ;
               ld a, 8                  ;
               ld (BASIC_VALTYP), a     ;
               __call_bios MATH_FRCINT  ; convert ARG to integer
               call SWAP.DAC.ARG        ;
MATH.IDIV.INT: ld hl, (BASIC_DAC+2)     ;
               ld a, h                  ;
               ld c, l                  ;
               ld de, (BASIC_ARG+2)     ;
               call MATH.DIV.16         ;
               ld h, a                  ;
               ld l, c                  ;
               ld (BASIC_DAC+2), hl     ; quotient
               jp MATH.PARM.PUSH        ;

endif

if defined MATH.POW

MATH.POW.INT:  ld (BASIC_VALTYP), a     ;
               __call_bios MATH_FRCDBL  ; convert DAC to double
               call SWAP.DAC.ARG        ;
               ld a, 2                  ;
               ld (BASIC_VALTYP), a     ;
               __call_bios MATH_FRCDBL  ; convert ARG to double
               call SWAP.DAC.ARG        ;
MATH.POW.SGL:  ld a, 8                  ;
               ld (BASIC_VALTYP), a     ;
MATH.POW.DBL:  __call_bios MATH_DBLEXP  ;
               jp MATH.PARM.PUSH        ;

endif

if defined MATH.MOD

;MATH.MOD.SGL:  ld a, 8                  ;
;               ld (BASIC_VALTYP), a     ;
;MATH.MOD.DBL:  __call_bios MATH_FRCINT  ; convert DAC to integer
;               call SWAP.DAC.ARG        ;
;		         ld a, 8                  ;
;               ld (BASIC_VALTYP), a     ;
;               __call_bios MATH_FRCINT  ; convert ARG to integer
;               call SWAP.DAC.ARG        ;
MATH.MOD.INT:  ld hl, (BASIC_DAC+2)     ;
               ld a, h                  ;
               ld c, l                  ;
               ld de, (BASIC_ARG+2)     ;
               call MATH.DIV.16         ;
               ld (BASIC_DAC+2), hl     ; remainder
               jp MATH.PARM.PUSH        ;

endif

if defined ISQR

; fast 16-bit integer square root
; http://www.retroprogramming.com/2017/07/a-fast-z80-integer-square-root.html
; 92 bytes, 344-379 cycles (average 362)
; v2 - 3 t-state optimization spotted by Russ McNulty
; call with hl = number to square root
; returns    a = square root
; corrupts  hl, de

MATH.INT.SQR:
  ld a,h
  ld de,0B0C0h
  add a,e
  jr c,sq7
  ld a,h
  ld d,0F0h
sq7:
  add a,d
  jr nc,sq6
  res 5,d
  db 254
sq6:
  sub d
  sra d
  set 2,d
  add a,d
  jr nc,sq5
  res 3,d
  db 254
sq5:
  sub d
  sra d
  inc d
  add a,d
  jr nc,sq4
  res 1,d
  db 254
sq4:
  sub d
  sra d
  ld h,a
  add hl,de
  jr nc,sq3
  ld e,040h
  db 210
sq3:
  sbc hl,de
  sra d
  ld a,e
  rra
  or 010h
  ld e,a
  add hl,de
  jr nc,sq2
  and 0DFh
  db 218
sq2:
  sbc hl,de
  sra d
  rra
  or 04h
  ld e,a
  add hl,de
  jr nc,sq1
  and 0F7h
  db 218
sq1:
  sbc hl,de
  sra d
  rra
  inc a
  ld e,a
  add hl,de
  jr nc,sq0
  and 0FDh
sq0:
  sra d
  rra
  cpl
  ret

endif

if defined RANDOMIZE or defined SEED

MATH.RANDOMIZE:    di                          ;
                     ld bc, (BIOS_JIFFY)       ;
                   ei                          ;

MATH.SEED:         ld (BASIC_RNDX), bc         ; seed to IRND
                   push bc                     ; in bc = new integer seed
                     call CLEAR.DAC            ;
                   pop bc                      ;
                   ;ld ix, BASIC_DAC            ;
                   ld (BASIC_DAC+2), bc        ; copy bc to dac
                   ld a, 2                     ; type integer
                   ld (BASIC_VALTYP), a        ;
                   __call_bios MATH_FRCDBL     ; convert DAC integer to DAC double
                   __call_bios MATH_NEG        ; DAC = -DAC
                   __call_bios MATH_RND        ; put in DAC a new random number from previous DAC parameter
                   ret                         ;

endif

MATH.ERROR:        ld e, 13                          ; type mismatch
                   __call_basic BASIC_ERROR_HANDLER  ;
                   ret


;---------------------------------------------------------------------------------------------------------
; BOOLEAN ROUTINES
;---------------------------------------------------------------------------------------------------------

BOOLEAN.RET.TRUE:  ld hl, LIT_TRUE             ;
                   ret.parm                    ;
BOOLEAN.RET.FALSE: ld hl, LIT_FALSE            ;
                   ret.parm                    ;
BOOLEAN.CMP.INT:   ld hl, (BASIC_DAC+2)        ;
                   ld de, (BASIC_ARG+2)        ;
                   __call_bios MATH_ICOMP      ;
                   ret                         ;
BOOLEAN.CMP.SGL:   ld bc, (BASIC_ARG)          ;
                   ld de, (BASIC_ARG+2)        ;
                   __call_bios MATH_DCOMP      ;
                   ret                         ;
BOOLEAN.CMP.DBL:   __call_bios MATH_XDCOMP     ;
                   ret                         ;
BOOLEAN.CMP.STR:   call STRING.COMPARE         ;
                   ret                         ;

if defined BOOLEAN.GT

BOOLEAN.GT.INT:    call BOOLEAN.CMP.INT        ;
                   jr BOOLEAN.GT.RET           ;
BOOLEAN.GT.STR:    call BOOLEAN.CMP.STR        ;
                   jr BOOLEAN.GT.RET           ;
BOOLEAN.GT.SGL:    call BOOLEAN.CMP.SGL        ;
                   jr BOOLEAN.GT.RET           ;
BOOLEAN.GT.DBL:    call BOOLEAN.CMP.DBL        ;
                   jr BOOLEAN.GT.RET           ;
BOOLEAN.GT.RET:    cp 0x01                     ;
                   jp z, BOOLEAN.RET.TRUE      ;
                   jp BOOLEAN.RET.FALSE        ;
endif

if defined BOOLEAN.LT

BOOLEAN.LT.INT:    call BOOLEAN.CMP.INT        ;
                   jr BOOLEAN.LT.RET           ;
BOOLEAN.LT.STR:    call BOOLEAN.CMP.STR        ;
                   jr BOOLEAN.LT.RET           ;
BOOLEAN.LT.SGL:    call BOOLEAN.CMP.SGL        ;
                   jr BOOLEAN.LT.RET           ;
BOOLEAN.LT.DBL:    call BOOLEAN.CMP.DBL        ;
                   jr BOOLEAN.LT.RET           ;
BOOLEAN.LT.RET:    cp 0xFF                     ;
                   jp z, BOOLEAN.RET.TRUE      ;
                   jp BOOLEAN.RET.FALSE        ;

endif

if defined BOOLEAN.GE

BOOLEAN.GE.INT:    call BOOLEAN.CMP.INT        ;
                   jr BOOLEAN.GE.RET           ;
BOOLEAN.GE.STR:    call BOOLEAN.CMP.STR        ;
                   jr BOOLEAN.GE.RET           ;
BOOLEAN.GE.SGL:    call BOOLEAN.CMP.SGL        ;
                   jr BOOLEAN.GE.RET           ;
BOOLEAN.GE.DBL:    call BOOLEAN.CMP.DBL        ;
                   jr BOOLEAN.GE.RET           ;
BOOLEAN.GE.RET:    cp 0x01                     ;
                   jp z, BOOLEAN.RET.TRUE      ;
                   or a                        ; cp 0
                   jp z, BOOLEAN.RET.TRUE      ;
                   jp BOOLEAN.RET.FALSE        ;

endif

if defined BOOLEAN.LE

BOOLEAN.LE.INT:    call BOOLEAN.CMP.INT        ;
                   jr BOOLEAN.LE.RET           ;
BOOLEAN.LE.STR:    call BOOLEAN.CMP.STR        ;
                   jr BOOLEAN.LE.RET           ;
BOOLEAN.LE.SGL:    call BOOLEAN.CMP.SGL        ;
                   jr BOOLEAN.LE.RET           ;
BOOLEAN.LE.DBL:    call BOOLEAN.CMP.DBL        ;
                   jr BOOLEAN.LE.RET           ;
BOOLEAN.LE.RET:    cp 0xFF                     ;
                   jp z, BOOLEAN.RET.TRUE      ;
                   or a                        ; cp 0
                   jp z, BOOLEAN.RET.TRUE      ;
                   jp BOOLEAN.RET.FALSE        ;

endif

if defined BOOLEAN.NE

BOOLEAN.NE.INT:    call BOOLEAN.CMP.INT        ;
                   jr BOOLEAN.NE.RET           ;
BOOLEAN.NE.STR:    call BOOLEAN.CMP.STR        ;
                   jr BOOLEAN.NE.RET           ;
BOOLEAN.NE.SGL:    call BOOLEAN.CMP.SGL        ;
                   jr BOOLEAN.NE.RET           ;
BOOLEAN.NE.DBL:    call BOOLEAN.CMP.DBL        ;
                   jr BOOLEAN.NE.RET           ;
BOOLEAN.NE.RET:    or a                        ; cp 0
                   jp nz, BOOLEAN.RET.TRUE     ;
                   jp BOOLEAN.RET.FALSE        ;

endif

if defined BOOLEAN.EQ

BOOLEAN.EQ.INT:    call BOOLEAN.CMP.INT        ;
                   jr BOOLEAN.EQ.RET           ;
BOOLEAN.EQ.STR:    call BOOLEAN.CMP.STR        ;
                   jr BOOLEAN.EQ.RET           ;
BOOLEAN.EQ.SGL:    call BOOLEAN.CMP.SGL        ;
                   jr BOOLEAN.EQ.RET           ;
BOOLEAN.EQ.DBL:    call BOOLEAN.CMP.DBL        ;
                   jr BOOLEAN.EQ.RET           ;
BOOLEAN.EQ.RET:    or a                        ; cp 0
                   jp z, BOOLEAN.RET.TRUE      ;
                   jp BOOLEAN.RET.FALSE        ;

endif

if defined BOOLEAN.AND

BOOLEAN.AND.INT:   ld a, (BASIC_DAC+2)         ;
                   ld hl, BASIC_ARG+2          ;
                   and (hl)                    ;
                   ld (BASIC_DAC+2), a         ;
                   inc hl                      ;
                   ld a, (BASIC_DAC+3)         ;
                   and (hl)                    ;
                   ld (BASIC_DAC+3), a         ;
                   ld a, 2                     ;
                   jp MATH.PARM.PUSH           ;

endif

if defined BOOLEAN.OR

BOOLEAN.OR.INT:    ld a, (BASIC_DAC+2)         ;
                   ld hl, BASIC_ARG+2          ;
                   or (hl)                     ;
                   ld (BASIC_DAC+2), a         ;
                   inc hl                      ;
                   ld a, (BASIC_DAC+3)         ;
                   or (hl)                     ;
                   ld (BASIC_DAC+3), a         ;
                   ld a, 2                     ;
                   jp MATH.PARM.PUSH           ;

endif

if defined BOOLEAN.XOR

BOOLEAN.XOR.INT:   ld a, (BASIC_DAC+2)         ;
                   ld hl, BASIC_ARG+2          ;
                   xor (hl)                    ;
                   ld (BASIC_DAC+2), a         ;
                   inc hl                      ;
                   ld a, (BASIC_DAC+3)         ;
                   xor (hl)                    ;
                   ld (BASIC_DAC+3), a         ;
                   ld a, 2                     ;
                   jp MATH.PARM.PUSH           ;

endif

if defined BOOLEAN.EQV

BOOLEAN.EQV.INT:   ld a, (BASIC_DAC+2)         ;
                   ld hl, BASIC_ARG+2          ;
                   xor (hl)                    ;
                   cpl                         ;
                   ld (BASIC_DAC+2), a         ;
                   inc hl                      ;
                   ld a, (BASIC_DAC+3)         ;
                   xor (hl)                    ;
                   cpl                         ;
                   ld (BASIC_DAC+3), a         ;
                   ld a, 2                     ;
                   jp MATH.PARM.PUSH           ;

endif

if defined BOOLEAN.IMP

BOOLEAN.IMP.INT:   ld a, (BASIC_DAC+2)         ;
                   ld hl, BASIC_ARG+2          ;
                   cpl                         ;
                   or (hl)                     ;
                   ld (BASIC_DAC+2), a         ;
                   inc hl                      ;
                   ld a, (BASIC_DAC+3)         ;
                   cpl                         ;
                   or (hl)                     ;
                   ld (BASIC_DAC+3), a         ;
                   ld a, 2                     ;
                   jp MATH.PARM.PUSH           ;

endif

if defined BOOLEAN.SHR

BOOLEAN.SHR.INT:   ld ix, BASIC_DAC+2          ; shift DAC integer to right (bits 15...0-->)
                   ld a, (BASIC_ARG+2)         ;
                   or a                        ; clear carry
                   jp z, MATH.PARM.PUSH        ; return if not shift
                   ld b, a                     ; shift count
BOOLEAN.SHR.INT.N: rr (ix+1)                   ;
                   rr (ix)                     ;
                   or a                        ; clear carry
                   djnz BOOLEAN.SHR.INT.N      ; next shift
                   ld a, 2                     ;
                   jp MATH.PARM.PUSH           ; return DAC

endif

if defined BOOLEAN.SHL

BOOLEAN.SHL.INT:   ld ix, BASIC_DAC+2          ; shift DAC integer to left (<--bits 15...0)
                   ld a, (BASIC_ARG+2)         ;
                   or a                        ; clear carry
                   jp z, MATH.PARM.PUSH        ; return if not shift
                   ld b, a                     ; shift count
BOOLEAN.SHL.INT.N: rl (ix)                     ;
                   rl (ix+1)                   ;
                   or a                        ; clear carry
                   djnz BOOLEAN.SHL.INT.N      ; next shift
                   ld a, 2                     ;
                   jp MATH.PARM.PUSH           ; return DAC

endif

if defined BOOLEAN.NOT

BOOLEAN.NOT.INT:   ld a, (BASIC_DAC+2)         ;
                   cpl                         ;
                   ld (BASIC_DAC+2), a         ;
                   ld a, (BASIC_DAC+3)         ;
                   cpl                         ;
                   ld (BASIC_DAC+3), a         ;
                   ld a, 2                     ;
                   jp MATH.PARM.PUSH           ;

endif



;---------------------------------------------------------------------------------------------------------
; MEMORY ALLOCATION ROUTINES
;---------------------------------------------------------------------------------------------------------
; Adapted from memory allocator code by SamSaga2, Spain, 2015
; https://www.msx.org/forum/msx-talk/development/asm-memory-allocator
; https://www.msx.org/users/samsaga2
;---------------------------------------------------------------------------------------------------------
memory.heap_start: equ VAR_STACK.END + 1    ; start at end of variable stack
memory.heap_end:   equ 0xF0A0 - 100         ; end at start of work area for stack (100 bytes reserved), BIOS and BASIC interpreter
block.next:        equ 0                    ; next free block address
block.size:        equ 2                    ; size of block including header
block:             equ 4                    ; block.next + block.size

;; init
memory.init:
       ld ix,memory.heap_start              ; first block
       ld hl,memory.heap_start+block        ; second block
       ;; first block NEXT=secondblock, SIZE=0
       ;; with this block we have a fixed start location
       ;; because never will be allocated
       ld (ix+block.next),l
       ld (ix+block.next+1),h
       ld (ix+block.size),0
       ld (ix+block.size+1),0
       ;; second block NEXT=0, SIZE=all
       ;; the first and only free block have all available memory
       ld (ix+block.next+block),0
       ld (ix+block.next+block+1),0
       xor a
       ;ld hl,memory.heap_end          ; size = @heap_end (stack) - heap_start - block_header * 2 - 100 (buffer for stack)
	   ld (BIOS_TEMP), sp
	   ld hl, (BIOS_TEMP)
       ld de, memory.heap_start + (block * 2) + 100
       sbc hl,de
	   ;ld de, block * 2 + 100
	   ;sbc hl, de
       ld (ix+block.size+block),l
       ld (ix+block.size+block+1),h
       ret

;; alloc
;; IN BC=size, OUT IX=memptr, NZ=ok
memory.alloc:
       ld hl,block
       add hl,bc
       ;push hl
       ;pop bc
	   ld b, h
	   ld c, l
       ld ix,memory.heap_start       ; this
       ld iy,0                       ; prev
memory.alloc.find:
       ld l,(ix+block.size)
       ld h,(ix+block.size+1)
       xor a
       sbc hl,bc
       jp z, memory.alloc.exactfit
       jp c, memory.alloc.nextblock
;; split found block
memory.alloc.splitfit:
       ;; free space must allow at least two blocks headers (current + next)
	   or h
	   jr nz, memory.alloc.splitfit.do   ; if free space > 0xFF, do split
	     ld a, l
	     cp 4
	     jr c, memory.alloc.nextblock    ; if free space < 4, skip to next block
memory.alloc.splitfit.do:
       ;; newfreeblock = this + BC
       push ix
       pop hl
       add hl,bc
       ;; prevblock->next = newfreeblock
       ld (iy+block.next),l
       ld (iy+block.next+1),h
       ;; newfreeblock->next = this->next
       push hl
       pop iy                        ; iy = newfreeblock
       ld l,(ix+block.next)
       ld h,(ix+block.next+1)
       ld (iy+block.next),l
       ld (iy+block.next+1),h
       ;; newfreeblock->size = this->size - BC
       ld l,(ix+block.size)
       ld h,(ix+block.size+1)
       xor a
       sbc hl,bc
       ld (iy+block.size),l
       ld (iy+block.size+1),h
       ;; this->size = BC
       ld (ix+block.size),c
       ld (ix+block.size+1),b
       jr memory.alloc.ok
;; use whole found block
memory.alloc.exactfit:
       ;; prevblock->next = this->next - remove block from free list
       ld l,(ix+block.next)
       ld h,(ix+block.next+1)
       ld (iy+block.next),l
       ld (iy+block.next+1),h
memory.alloc.ok:
       ;; ix = first byte
       ld de,block
       add ix,de
       ;; enable z-flag
       ld a,1
       or a
       ret
memory.alloc.nextblock:
       ld l,(ix+block.next)
       ld h,(ix+block.next+1)
       ld a,l
       cp h
       ret z
         ;; prevblock = this
         push ix
         pop iy
         ;; this = this->next
         push hl
         pop ix
         jp memory.alloc.find

;; free
;; IN IX=memptr
memory.free:
       ;; HL = IX - block_header_size
       push ix
       pop hl
       ld de, block
	   xor a
       sbc hl,de
       ;; start of search
       ld ix,memory.heap_start
memory.free.find:
       ld e,(ix+block.next)
       ld d,(ix+block.next+1)
       ld a,d
       or e
       jp z, memory.free.passedend
         sbc hl,de                     ; test this (HL) against next (DE)
         jr c, memory.free.found       ; if DE > HL
           add hl,de                     ; restore hl value
	       push de
	       pop ix                        ; current = next
           jr memory.free.find

;; ix=prev, hl=this, de=next
memory.free.found:
       add hl,de                     ; restore hl value
	   ld (ix+block.next), l
	   ld (ix+block.next+1), h       ; prev->next = this
	   push hl
	   pop iy
	   ld (iy+block.next), e
	   ld (iy+block.next+1), d       ; this->next = next
	   push ix 					     ; prev x this
	   pop iy
	   push hl
	   pop ix
	   push de
	     call memory.free.coalesce
	   pop ix                        ; this x next
       jr memory.free.coalesce

;; parm1 = *next
;; parm2 = *this
memory.free.coalesce:
	   ld c, (iy+block.size)
	   ld b, (iy+block.size+1)  ; bc = this->size
       push iy
	   pop hl
	   xor a
	   adc hl, bc     ; hl = this + this->size
	   push ix
	   pop de
	   xor a
	   sbc hl, de     ; if this + this->size == next, then this->size += next->size, this->next = next->next
	   jr z, memory.free.coalesce.do
	     push ix                ; else, new *this = *next
         pop iy
		 ret
memory.free.coalesce.do:
       ld l, (ix+block.size)
	   ld h, (ix+block.size+1)  ; hl = next->size
	   xor a
	   adc hl, bc               ; hl += this->size
	   ld (iy+block.size), l
	   ld (iy+block.size+1), h  ; this->size = hl
	   ld l, (ix+block.next)
	   ld h, (ix+block.next+1)  ; hl = next->next
       ld (iy+block.next), l
	   ld (iy+block.next+1), h  ; this->next = hl
	   ret

memory.free.passedend:
       ;; append block at the end of the free list
       ld (ix+block.next),l
       ld (ix+block.next+1),h
       push hl
       pop iy
       ld (iy+block.next),0
       ld (iy+block.next+1),0
	   ret

;; get_free
;; OUT BC=freespace
memory.get_free:
       ld ix,memory.heap_start
       ld bc,0
memory.get_free.count:
       ld a,c
       add a,(ix+block.size)
       ld c,a
       ld a,b
       adc a,(ix+block.size+1)
       ld b,a
       ld l,(ix+block.next)
       ld h,(ix+block.next+1)
       ld a,h
       or l
       ret z
       push hl
       pop ix
       jr memory.get_free.count

memory.error:  ld e, 7                           ; out of memory
               __call_basic BASIC_ERROR_HANDLER  ;
               ret



;---------------------------------------------------------------------------------------------------------
; MATH PACK WRAPPER
;---------------------------------------------------------------------------------------------------------

CALL_MATH_LIB: exx
			     ld hl, RET_MATH_LIB
			     push hl
                   ld hl, BASIC_DAC
                   ld de, BASIC_ARG
			       ld bc, BASIC_SWPTMP
                   jp (ix)
RET_MATH_LIB:    call COPY_TO.TMP_DAC
               exx
               ret

if defined MATH.ADD

MATH_DECADD:   ld ix, addSingle
               jp CALL_MATH_LIB

endif

if defined MATH.SUB or defined MATH.NEG

MATH_DECSUB:   ld ix, subSingle
			   jp CALL_MATH_LIB

endif

if defined MATH.MULT

MATH_DECMUL:   ld ix, mulSingle
			   jp CALL_MATH_LIB

endif

if defined MATH.DIV

MATH_DECDIV:   ld ix, divSingle
			   jp CALL_MATH_LIB

endif

if defined MATH.POW

MATH_DBLEXP:
MATH_SNGEXP:   ld ix, powSingle
			   jp CALL_MATH_LIB

endif

if defined COS

MATH_COS:      ld ix, cosSingle
			   jp CALL_MATH_LIB

endif

if defined SIN

MATH_SIN:      ld ix, sinSingle
			   jp CALL_MATH_LIB

endif

if defined TAN

MATH_TAN:      ld ix, tanSingle
			   jp CALL_MATH_LIB

endif

if defined ATN

MATH_ATN:      ld ix, atanSingle
			   jp CALL_MATH_LIB

endif

if defined SQR

MATH_SQR:      ld ix, sqrtSingle
			   jp CALL_MATH_LIB

endif

if defined LOG

MATH_LOG:      ld ix, lnSingle
			   jp CALL_MATH_LIB

endif

if defined EXP

MATH_EXP:      ld ix, expSingle
			   jp CALL_MATH_LIB

endif

if defined ABS

MATH_ABSFN:    ld ix, absSingle
			   jp CALL_MATH_LIB

endif

if defined MATH.SEED or defined MATH.NEG

MATH_NEG:      ld ix, negSingle
			   jp CALL_MATH_LIB

endif

if defined SGN

MATH_SGN:      ld ix, sgnSingle
			   jp CALL_MATH_LIB

endif

if defined RND or defined MATH.SEED

MATH_RND:      ld ix, randSingle
               jp CALL_MATH_LIB

endif

MATH_FRCINT:   ld hl, BASIC_DAC
               ld bc, BASIC_DAC+2
			   call single2Int
			   ld ix, BASIC_DAC
			   ld (ix), 0
			   ld (ix+1), 0
			   ;ld (ix+2), l
			   ;ld (ix+3), h
			   ld (ix+4), 0
			   ld (ix+5), 0
			   ld (ix+6), 0
			   ld (ix+7), 0
               ld a, 2
               ld (BASIC_VALTYP), a
               ret

MATH_FRCDBL:                         ; same as MATH_FRCSGL
MATH_FRCSGL:   ld hl, BASIC_DAC+2    ; input address
               ld bc, BASIC_DAC      ; output address
               call int2Single
               ld a, 8
               ld (BASIC_VALTYP), a
               ret

MATH_ICOMP:         ld a, h   ; cp hl, de (alternative to bios DCOMPR)
                    cp d
			        jr nz, MATH_ICOMP.NE.HIGH
			          ld a, l
			          cp e
			          jr nz, MATH_ICOMP.NE.LOW
                        jr MATH_DCOMP.EQ
MATH_ICOMP.NE.HIGH: jr c, MATH_ICOMP.GT.HIGH
                    bit 7, a
                    jr nz, MATH_DCOMP.GT
			          jr MATH_DCOMP.LT
MATH_ICOMP.GT.HIGH: bit 7, d
                    jr z, MATH_DCOMP.GT
			          jr MATH_DCOMP.LT
MATH_ICOMP.NE.LOW:  jr c, MATH_DCOMP.GT
  			          jr MATH_DCOMP.LT

MATH_XDCOMP:                          ; same as MATH_DCOMP
MATH_DCOMP:    ld ix, cmpSingle
			   call CALL_MATH_LIB
			   jr z, MATH_DCOMP.EQ
			   jr c, MATH_DCOMP.LT
MATH_DCOMP.GT: ld a, 0xFF             ; DAC > ARG
               ret
MATH_DCOMP.EQ: ld a, 0                ; DAC = ARG
               ret
MATH_DCOMP.LT: ld a, 1                ; DAC < ARG
               ret

if defined CAST_STR_TO.VAL

MATH_FIN:      ; HL has the source string
               ld a, (BASIC_VALTYP)
               cp 2                   ; test if integer
			   jr nz, MATH_FIN.1
			   ;ld hl, (BASIC_DAC+2)
			   ;ld de, BASIC_STRBUF
			   ex de, hl
			   call StrToInt
			   ld bc, 0
			   ld (BASIC_DAC), bc
			   ld (BASIC_DAC+2), hl
			   ld (BASIC_DAC+4), bc
			   ld (BASIC_DAC+6), bc
			   ;ld hl, BASIC_STRBUF
			   ret
MATH_FIN.1:	   ld BC, BASIC_DAC
			   call str2single
               ret

endif

if defined CAST_INT_TO.STR

MATH_FOUT:     ld a, (BASIC_VALTYP)
               cp 2                   ; test if integer
			   jr nz, MATH_FOUT.1
			   ld hl, (BASIC_DAC+2)
			   ld de, BASIC_STRBUF
			   call IntToStr
			   ld hl, BASIC_STRBUF
			   ret
MATH_FOUT.1:   ld hl, BASIC_DAC
               ld bc, BASIC_STRBUF
               call single2str
			   ld hl, BASIC_STRBUF
               ret

endif




;---------------------------------------------------------------------------------------------------------
; Z80FLOAT LIBRARY
; Copyright 2018 Zeda A.K. Thomas
;---------------------------------------------------------------------------------------------------------
; References:
; https://github.com/Zeda/z80float
; https://www.omnimaga.org/asm-language/(z80)-floating-point-routines/
; https://en.wikipedia.org/wiki/Single-precision_floating-point_format
;---------------------------------------------------------------------------------------------------------
; Parameters:
; HL points to the first operand
; DE points to the second operand (if needed)
; IX points to the third operand (if needed, rare)
; BC points to where the result should be output
; Floats are stored by a little-endian 24-bit mantissa. However, the highest bit
; is taken as implicitly 1, so we replace it as a sign bit. Next comes an 8-bit
; exponent biased by +128.
;---------------------------------------------------------------------------------------------------------
; Adapted to MSXBas2Asm by Amaury Carvalho, 2019
;---------------------------------------------------------------------------------------------------------

;---------------------------------------------------------------------------------------------------------
; Work area
;---------------------------------------------------------------------------------------------------------

BASIC_HOLD8: equ 0xF806  ; 	48	Work area for decimal multiplications.
BASIC_HOLD2: equ 0xF836  ;	8	Work area in the execution of numerical operators.
BASIC_HOLD:  equ 0xF83E  ;  8	Work area in the execution of numerical operators.
scrap:   equ BASIC_HOLD8
seed0:   equ BASIC_RNDX
seed1:   equ seed0 + 4
var48:   equ scrap + 4
quot:    equ scrap + 1
addend:  equ scrap
addend2: equ scrap+7           ;4 bytes
var_x:   equ BASIC_HOLD8 + 4   ;4 bytes
var_y:   equ var_x + 4         ;4 bytes
var_z:   equ var_y + 4         ;4 bytes
var_a:   equ var_z + 4         ;4 bytes
var_b:   equ var_a + 4         ;4 bytes
var_c:   equ var_b + 4         ;4 bytes
temp:    equ var_c + 4         ;4 bytes
temp1:   equ temp  + 4         ;4 bytes
temp2:   equ temp1 + 4         ;4 bytes
temp3:   equ temp2 + 4         ;4 bytes

pow10exp_single: equ scrap+9
strout_single:   equ 0xF750    ;  PARM2 - BASIC_BUF   ;pow10exp_single+2

;---------------------------------------------------------------------------------------------------------
; addSingle
;---------------------------------------------------------------------------------------------------------

;;Still need to tend to special cases
addSingle:
;;x+y
    push af
    push hl
    push de
    push bc
addInject:
    inc de
    inc de
    inc hl
    inc hl
    ld a,(de)
    xor (hl)
    push af
    inc de
    inc hl
    ex de,hl
    ld a,(de)
    sub (hl)
    ex de,hl
    jr nc,$+5
    ex de,hl
    neg
    cp 24
    jp nc,add_unneeded
    push hl
    ld hl,addend+6
    dec de
    ld bc,0408h
    dec hl
    ld (hl),0
    sub c
    jr nc,$-5
    add a,c
    push af
    push hl
    ex de,hl
    ld a,(hl)
    or 80h
    ld (de),a
    dec de
    dec hl
    ldd
    ldd
    ex de,hl
    dec b
    jr z,$+7
    ld (hl),0
    dec hl
    djnz $-3
    pop hl
    pop af
    ld b,a
    jr z,noshift
    set 7,(hl)
_1:
    push hl
    srl (hl)
    dec hl
    rr (hl)
    dec hl
    rr (hl)
    dec hl
    rr (hl)
    pop hl
    djnz _1
noshift:
    pop hl  ;bigger float
    dec hl
    ld b,(hl)
    dec hl
    dec hl
    ex de,hl
    pop af
    jp m,subtract
    ld hl,addend+2
    ld a,(hl)
    rla
    inc hl
    ld a,(de)
    adc a,(hl)
    ld (hl),a
    inc hl
    inc de
    ld a,(de)
    adc a,(hl)
    ld (hl),a
    inc hl
    inc de
    ld a,(de)
    set 7,a
    adc a,(hl)
    ld (hl),a
    inc hl
    inc de
    ld a,(de)
    ld (hl),a
    jp nc,add_done
    inc (hl)
    jp z,add_overflow
    dec hl
    rr (hl)
    dec hl
    rr (hl)
    dec hl
    rr (hl)
    jp add_done
subtract:
    ld hl,addend
    xor a
    ld c,a
    sub (hl)
    ld (hl),a
    inc hl
    ld a,c
    sbc a,(hl)
    ld (hl),a
    inc hl
    ld a,c
    sbc a,(hl)
    ld (hl),a
    inc hl
    ld a,(de)
    sbc a,(hl)
    ld (hl),a
    inc hl
    inc de
    ld a,(de)
    sbc a,(hl)
    ld (hl),a
    inc hl
    inc de
    ld a,(de)
    set 7,a
    sbc a,(hl)
    ld (hl),a
    inc hl
    inc de
    ld a,(de)
    ld (hl),a
    dec de
    ex de,hl
    jr nc,negated
    ld hl,addend
    ld a,80h
    xor b
    ld b,a
    ld a,c
    sub (hl)
    ld (hl),a
    inc hl
    ld a,c
    sbc a,(hl)
    ld (hl),a
    inc hl
    ld a,c
    sbc a,(hl)
    ld (hl),a
    inc hl
    ld a,c
    sbc a,(hl)
    ld (hl),a
    inc hl
    ld a,c
    sbc a,(hl)
    ld (hl),a
    inc hl
    ld a,c
    sbc a,(hl)
    ld (hl),a
negated:
    jp m,add_done
    push bc
    ld hl,(addend)
    ld de,(addend+2)
    ld bc,(addend+4)
    ld a,h
    or l
    or d
    or e
    or b
    or c
    jp z,add_underflow
    ld a,(addend+6)
normalize:
    dec a
    jr z,add_underflow
    add hl,hl
    rl e
    rl d
    rl c
    rl b
    jp p,normalize
    ld (addend),hl
    ld (addend+2),de
    ld (addend+4),bc
    ld (addend+6),a
    pop bc
add_done:
;;Need to adjust sign flag
    ld hl,addend+5
    ld a,(hl)
    rla
    rl b
    rra
    ld (hl),a
    dec hl
    dec hl
add_copy:
    pop de
    push de
    ldi
    ldi
    ldi
    ld a,(hl)
    ld (de),a
    pop bc
    pop de
    pop hl
    pop af
    ret
add_underflow:
;;How many push/pops are needed?
;;return ZERO
    ld hl,0
    ld (addend+3),hl
    ld (addend+5),hl
    pop bc
    jr add_done
add_overflow:
;;How many push/pops are needed?
;;return INF
    dec hl
    ld (hl),40h
    jr add_done
add_unneeded:
;;How many push/pops are needed?
;;Return bigger number
    pop af
    dec hl
    dec hl
    dec hl
    jr add_copy

;---------------------------------------------------------------------------------------------------------
; subSingle
;---------------------------------------------------------------------------------------------------------

subSingle:
;;x-y
    push af
    push hl
    push de
    push bc
    push hl
    ex de,hl
    ld de,addend2
    ldi
    ldi
    ld a,(hl)
    xor 80h
    ld (de),a
    inc de
    inc hl
    ld a,(hl)
    ld (de),a
    ex de,hl
    pop hl
    ld de,addend2
    jp addInject    ;jumps in to the addSingle routine

;---------------------------------------------------------------------------------------------------------
; mulSingle
;---------------------------------------------------------------------------------------------------------

mulSingle:
;Inputs: HL points to float1, DE points to float2, BC points to where the result is copied
;Outputs: float1*float2 is stored to (BC)
;573+mul24+{0,35}+{0,30}
;min: 1398cc
;max: 2564cc
;avg: 2055.13839751681cc
    push af
    push hl
    push de
    push bc

    call _2   ;CHLB
    ld a,c
    ex de,hl
    pop hl
    push hl
    ld (hl),b
    inc hl
    ld (hl),e
    inc hl
    ld (hl),d
    inc hl
    ld (hl),a
    pop bc
    pop de
    pop hl
    pop af
    ret


_2:
;;return float in CHLB
    push de
    ld e,(hl)
    inc hl
    ld d,(hl)
    inc hl
    ld c,(hl)
    inc hl
    ld a,(hl)
    or a
    jr z,mulSingle_case0
    ex de,hl
    ex (sp),hl
    ld e,(hl)
    inc hl
    ld d,(hl)
    inc hl
    ld b,(hl)
    inc hl

    ;inc (hl)
    ;dec (hl)
    ;jr z,mulSingle_case1
    push af
    ld a, (hl)
    or a
    jp z,mulSingle_case1
    pop af

    add a,(hl)      ;\
    pop hl          ; |
    rra             ; |Lots of help from Runer112 and
    adc a,a         ; |calc84maniac for optimizing
    jp po,bad       ; |this exponent check.
    xor 80h         ; |
    jr z,underflow  ;/
    push af         ;exponent
    ld a,b
    xor c
    push af         ;sign
    set 7,b
    set 7,c
    call mul24      ;BDE*CHL->HLBCDE, returns sign info
    pop de
    ld a,e
    pop de
    jp m,_3
    rl c
    rl b
    adc hl,hl
    dec d
_3:
    inc d
    jr z,overflow
    rl c
    ld c,d
    ld de,0
    push af
    ld a,b
    adc a,e
    ld b,a
    adc hl,de
    jr nc,_4
    inc c
    jr z,overflow
    rr h
    rr l
    rr b
_4:
    pop af
    cpl
    and $80
    xor h
    ld h,a
    ret
bad:
    jr nc,overflow
underflow:
    ld hl,0
    rl b
    rr h
    ld c,l
    ld b,l
    ret
overflow:
    ld hl,$8000
    jr underflow+3
mulSingle_case1:
;x*0   -> 0
;x*inf -> inf
;x*NaN -> NaN
  pop af
  pop hl
  ld h,b
  ld l,d
  ld b,e
  ld c,0
  ret
mulSingle_case0:
;special*x = special
;NaN*x = NaN
;0*0 = 0
;0*NaN = NaN
;0*Inf = NaN
;Inf*Inf  = Inf
;Inf*-Inf =-Inf
  ;0CDE
  pop hl
  inc hl
  inc hl
  inc hl
  ld a,(hl)
  or a
  jr z,_5
  ld h,c
  ld c,0
  ret
_5:
  dec hl
  ld b,(hl)
;basically, if b|c has bit 5 set, return NaN
  ld a,b
  or c
  ld h,$20
  and h
  jr z,_6
  ld c,0
  ret
_6:
  ld a,c
  xor b
  rl b
  rlca
  rr b
  res 4,b

  rl c
  rrca
  rr c

  ld a,c
  and $E0
  add a,b
  rra
  ld h,a
  ld c,0
  ret
mul24:
;;BDE*CHL -> HLBCDE
;;155 bytes
;;402+3*C_Times_BDE
;;fastest:1201cc
;;slowest:1753cc
;;avg    :1464.9033203125cc (1464+925/1024)
;min: 825cc
;max: 1926cc
;avg: 1449.63839751681cc

    push bc
    ld c,l
    push hl
    call C_Times_BDE
    ld (var48),hl
    ld l,a
    ld h,c
    ld (var48+2),hl

    pop hl
    ld c,h
    call C_Times_BDE
    push bc
    ld bc,(var48+1)
    add hl,bc
    ld (var48+1),hl
    pop bc
    ld b,c
    ld c,a
    ld hl,(var48+3)
    ld h,0
    adc hl,bc
    ld (var48+3),hl

    pop bc
    call C_Times_BDE
    ld de,(var48+2)
    add hl,de
    ld (var48+2),hl
    ld d,c
    ld e,a
    ld b,h
    ld c,l
    ld hl,(var48+4)
    ld h,0
    adc hl,de
    ld de,(var48)
    ret

;---------------------------------------------------------------------------------------------------------
; divSingle
;---------------------------------------------------------------------------------------------------------

divSingle:
;;HL points to numerator
;;DE points to denominator
;;BC points to where the quotient gets written
  call pushpop
divSingle_no_pushpop:
    inc hl
    inc de
    inc hl
    inc de
    ld a,(de)   ;\
    xor (hl)    ; |Get sign of output
    add a,a     ; |
    push af     ;/
    push bc
    inc hl
    inc de
    ld a,(hl)   ;\
    ex de,hl    ; |Get exponent
    sub (hl)    ; |
    ex de,hl    ; |

    ld b,-1
    jr nc,_7
    dec b
_7:
    add a,128
    jr nc,_8
    inc b
_8:
    inc b
    jr z,_9
    jp p,divunderflow
    jp m,divoverflow
_9:
    ld (quot+3),a
    dec hl
    dec de
    ld b,(hl)
    dec hl
    ld a,(hl)
    dec hl
    ld l,(hl)
    ld h,a
    ex de,hl

    ld c,(hl)
    dec hl
    ld a,(hl)
    dec hl
    ld l,(hl)
    ld h,a
    ex de,hl

    set 7,c
    ld a,b
    or 80h
    sbc hl,de
    sbc a,c
    jr nz,_10
    or h
    or l
    jr z,setmantissa0
    xor a
_10:
    jr nc,startdiv
    ld b,a
    ld a,(quot+3)
    dec a
    ld (quot+3),a
    ld a,b
    add hl,hl
    adc a,a
    add hl,de
    adc a,c
startdiv:
    ld b,1
    call divsub0+3
    ld (quot+1),bc
    call divsub0
    ld (quot),bc
    call divsub0
    ld (quot-1),bc
    add hl,hl
    rla
    jr c,_11
    sbc hl,de
    sbc a,c
    ccf
_11:
    ld hl,(quot)
    ld de,(quot+2)
    ld bc,0
    adc hl,bc
    ex de,hl
    adc hl,bc
    ld b,h
    ld c,l
writeback:
    pop hl
    ld (hl),e
    inc hl
    ld (hl),d
    inc hl
    rl c
    pop af
    rr c
    ld (hl),c
    inc hl
    ld (hl),b
    ret
divoverflow:
    ld b,$40
    jr _12
divunderflow:
  ld b,0
  jr _12
setmantissa0:
  ld bc,(quot+2)
_12:
  ld de,0
  ld c,e
  jr writeback
divsub0:
;;882cc max
    call divsub1    ;34 or 66
    call divsub1    ;
    call divsub1
    call divsub1
    call divsub1
    call divsub1
    call divsub1
    call divsub1
    or a
    sbc hl,de
    sbc a,c
    inc b
    ret nc
    dec b
    add hl,de
    adc a,c
    ret
divsub1:
;34cc or 66cc or 93cc
    sla b
    add hl,hl
    rla
    ret nc
    or a
    inc b
    sbc hl,de
    sbc a,c
    ret c
    inc b
    sbc hl,de
    sbc a,c
    ret

;---------------------------------------------------------------------------------------------------------
; powSingle
; https://www.geeksforgeeks.org/write-a-c-program-to-calculate-powxn/
; https://stackoverflow.com/questions/3518973/floating-point-exponentiation-without-power-function
;---------------------------------------------------------------------------------------------------------
;double mypow( double base, double power, double precision )
;{
;   if ( power < 0 ) return 1 / mypow( base, -power, precision );
;   else if ( power >= 1 ) return base * mypow( base, power-1, precision );
;   else if ( precision >= 1 ) {
;	   if( base >= 0 ) return sqrt( base );
;	   else return sqrt( -base );
;   } else return sqrt( mypow( base, power*2, precision*2 ) );
;}

if defined MATH.POW or defined MATH_EXP or defined MATH_LOG or defined MATH_LN

powSingle:
;;Computes y^x
;;HL points to y
;;DE points to x
;;BC points to output
    call pushpop
    push bc
      push de
	    ld bc, var_y     ; power
	    call copySingle
	  pop hl
	  ld bc, var_x       ; base
	  call copySingle
	  ld hl, const_precision
	  ld bc, var_a       ; precision
	  call copySingle
	  ld hl, const_0
	  ld bc, var_z       ; result
	  call copySingle
	  call powSingle.loop
	pop bc
	ld hl, var_z
	call copySingle
	ret

powSingle.loop:
;   if ( power < 0 ) return 1 / mypow( base, -power, precision );
    ld hl, var_y
	ld de, const_0
	call cmpSingle
	jp c, powSingle.1

;   else if ( power >= 1 ) return base * mypow( base, power-1, precision );
    ld hl, var_y
	ld de, const_1
	call cmpSingle
	jp nc, powSingle.2

;   else if ( precision >= 1 ) {
;	   if( base >= 0 ) return sqrt( base );
;	   else return sqrt( -base );
    ld hl, var_a
	ld de, const_1
	call cmpSingle
	jp nc, powSingle.3

;   } else return sqrt( mypow( base, power*2, precision*2 ) );
    ld hl, var_y
	ld de, const_2
	ld bc, var_b
	call mulSingle
	ld hl, var_b
	ld bc, var_y
	call copySingle
    ld hl, var_a
	ld de, const_2
	ld bc, var_b
	call mulSingle
	ld hl, var_b
	ld bc, var_a
	call copySingle
	call powSingle.loop
	ld hl, var_z
	ld bc, var_b
	call sqrtSingle
	ld hl, var_b
	ld bc, var_z
	call copySingle
	ret

powSingle.1:
; return 1 / mypow( base, -power, precision );
    ld hl, const_0
	ld de, var_y
	ld bc, var_b
	call subSingle
	ld hl, var_b
	ld bc, var_y
	call copySingle
	call powSingle.loop
	ld hl, const_1
	ld de, var_z
	ld bc, var_b
	call divSingle
	ld hl, var_b
	ld bc, var_z
	call copySingle
    ret

powSingle.2:
; return base * mypow( base, power-1, precision );
    ld hl, var_y
	ld de, const_1
	ld bc, var_b
	call subSingle
	ld hl, var_b
	ld bc, var_y
	call copySingle
	call powSingle.loop
	ld hl, var_z
	ld de, var_x
	ld bc, var_b
	call mulSingle
	ld hl, var_b
	ld bc, var_z
	call copySingle
    ret

powSingle.3:
;	   if( base >= 0 ) return sqrt( base );
;	   else return sqrt( -base );
    ld hl, var_x
	ld de, const_0
	call cmpSingle
	jp nc, powSingle.1
	;ld hl, var_x
	ld bc, var_b
	call negSingle
	ld hl, var_b
	;ld bc, var_z
	;call sqrtSingle
	;ret

powSingle.3.1:
    ;ld hl, var_x
	ld bc, var_z
	call sqrtSingle
    ret

pow2Single:
;;Computes 2^x
  call pushpop
  push bc

exp_inject:
;if x is on [0,1):
;  2^x = 1.000000001752 + x * (0.693146989552 + x * (0.2402298085906 + x * (5.54833215071e-2 + x * (9.67907584392e-3 + x * (1.243632065103e-3 + x * 2.171671843714e-4)))))
;Please note that usually I like to reduce to [-.5,.5] as the extra overhead is usually worth it.
;In this case, our polynomial is the same degree, with error different by less than 1 bit, so it's just a waste to range-reduce in this way.
;
;int(x) -> out_exp
;x-=int(x)  ;leaves x in [0,1)
;;If x==0    -> out==1
;;if x==inf  -> out==inf
;;if x==-inf -> out==0
;;if x==NAN  -> out==NAN
  ld de,var48+10
  call mov4
  ld hl,(var48+10)
  ld de,(var48+12)
  ld a,e
  add a,a
  push af   ;keep track of sign
  rrca
  ld (var48+12),a
  ld c,a
  ld a,d
    or a
    jp z,exp_spec
    cp 80h-23
    jp c,exp_underflow
    sub 128   ; sub a,128
    jr c,_pow_1 ;int(x)=0
    inc a
    cp 7
    jp nc,exp_overflow
    set 7,c
    ld b,a
    xor a
    add hl,hl
    rl c
    rla
    djnz $-4
    ld b,7Fh
    bit 7,c
    jr nz,exp_normalized
    ld e,a
    ld a,h
    or l
    or c
    ld a,e
    jr z,exp_zeroed
    dec b
    add hl,hl
    rl c
    jp p,$-4
    jr exp_normalized  ;.db $11 ;start of `ld de,**`
exp_zeroed:
    ld b,0
exp_normalized:
    ld (var48+10),hl
    res 7,c
    ld (var48+12),bc
    jr comp_exp   ;.db $06 ;start of 'ld b,*` just to eat the next byte
_pow_1:
    xor a
comp_exp:
  pop hl
  rr l
  jr nc,_pow_2
  cpl
  or a
  jp z,exp_underflow+1
  ;perform 1-(var48+10)--> var48+10
  ld hl,const_1
  ld de,var48+10
  ld b,d
  ld c,e
  call subSingle
_pow_2:
  push af
;our 'x' is at var48+10
;our `temp` is at var48+6 so as not to cause issues with mulSingle)
;uses 14 bytes of RAM
  ld hl,var48+10
  ld de,exp_a6
  ld bc,var48+6
  call mulSingle
  ld d,b
  ld e,c
  ld hl,exp_a5
  call addSingle
  ld hl,var48+10
  call mulSingle
  ld hl,exp_a4
  call addSingle
  ld hl,var48+10
  call mulSingle
  ld hl,exp_a3
  call addSingle
  ld hl,var48+10
  call mulSingle
  ld hl,exp_a2
  call addSingle
  ld hl,var48+10
  call mulSingle
  ld hl,exp_a1
  call addSingle
  ld hl,var48+10
  call mulSingle
  ld hl,const_1
  call addSingle
  ld hl,var48+9
  pop af
  add a,(hl)
  ld (hl),a
  ex de,hl
  pop de
  jp mov4
exp_spec:
;bit 6 means INF
;bit 5 means NAN
;no bits means zero
;NAN -> NAN
;+inf -> +inf
;-inf -> +0  because lim approaches 0 from the right
    ld a,c
    add a,a
    jr z,exp_zero
    jp m,exp_inf
;exp_NAN
    pop af
    ld de,0040h
exp_return_spec:
    pop hl
    rr e
    ld (hl),a
    inc hl
    ld (hl),a
    inc hl
    ld (hl),e
    inc hl
    ld (hl),d
    ret
exp_overflow:
exp_inf:
;+inf -> +inf
;-inf -> +0  because lim approaches 0 from the right
    pop af
    sbc a,a ;FF if should be 0,
    cpl
    and 80h
    ld d,0
    ld e,a
    jr exp_return_spec
exp_underflow:
exp_zero:
    pop af
    or a
    ld de,$8000
    jr exp_return_spec

endif

;---------------------------------------------------------------------------------------------------------
; sqrtSingle
;---------------------------------------------------------------------------------------------------------

if defined MATH_SQR or defined MATH_EXP

;Uses 3 bytes at scrap
sqrtSingle:
;552+{0,19}+8{0,3+{0,3}}+pushpop+sqrtHLIX
;min: 1784
;max: 1987
;avg: 1872
  call pushpop
  push bc
  ld c,(hl)
  inc hl
  ld e,(hl)
  inc hl
  ld a,(hl)
  add a,a
  jp c,sqrtSingle_NaN
  scf
  rra
  ld d,a
  inc hl
  ld a,(hl)
  or a
  jp z,sqrtSingle_special
  add a,80h
  rra
  push af   ;new exponent
  jr c,_13
  srl d
  rr e
  rr c
_13:
  ex de,hl
  ld ixh,c
  ld ixl,0
  call sqrtHLIX
;AHL is the new remainder
;Need to divide by 2, then divide by the 16-bit (var_x+4)
  rra
  ld a,h
;HL/DE to 8 bits
;We are just going to approximate it
  res 0,l
  jr c,$+5
  cp d
  jr c,$+4
  sub d
  inc l
  sla l
  rla
  jr c,$+5
  cp d
  jr c,$+4
  sub d
  inc l
  sla l
  rla
  jr c,$+5
  cp d
  jr c,$+4
  sub d
  inc l
  sla l
  rla
  jr c,$+5
  cp d
  jr c,$+4
  sub d
  inc l
  sla l
  rla
  jr c,$+5
  cp d
  jr c,$+4
  sub d
  inc l
  sla l
  rla
  jr c,$+5
  cp d
  jr c,$+4
  sub d
  inc l
  sla l
  rla
  jr c,$+5
  cp d
  jr c,$+4
  sub d
  inc l
  sla l
  rla
  jr c,$+5
  cp d
  jr c,$+4
  sub d
  inc l

  pop bc
  ld a,l
  pop hl
  ;BDEA
  ld (hl),a
  inc hl
  ld (hl),e
  inc hl
  res 7,d
  ld (hl),d
  inc hl
  ld (hl),b
  ret
sqrtSingle_NaN:
  ld hl,const_NaN
  pop de
  jp mov4
sqrtSingle_special:
  dec hl
  dec hl
  pop de
  jp mov4

sqrtHLIX:
;Input: HLIX
;Output: DE is the sqrt, AHL is the remainder
;speed: 754+{0,1}+6{0,6}+{0,3+{0,18}}+{0,38}+sqrtHL
;min: 1130
;max: 1266
;avg: 1190.5


  call sqrtHL
  add a,a
  ld e,a
  jr nc,_14
  inc d
_14:

  ld a,ixh
  sll e
  rl d
  add a,a
  adc hl,hl
  add a,a
  adc hl,hl
  sbc hl,de
  jr nc,_15
  add hl,de
  dec e
  jr _15a      ;.db $FE     ;start of `cp *`
_15:
  inc e
_15a:
  sll e
  rl d
  add a,a
  adc hl,hl
  add a,a
  adc hl,hl
  sbc hl,de
  jr nc,_16
  add hl,de
  dec e
  jr _16a   ;.db $FE     ;start of `cp *`
_16:
  inc e
_16a:
  sll e
  rl d
  add a,a
  adc hl,hl
  add a,a
  adc hl,hl
  sbc hl,de
  jr nc,_17
  add hl,de
  dec e
  jr _17a  ;.db $FE     ;start of `cp *`
_17:
  inc e
_17a:
  sll e
  rl d
  add a,a
  adc hl,hl
  add a,a
  adc hl,hl
  sbc hl,de
  jr nc,_18
  add hl,de
  dec e
  jr _18a  ;.db $FE     ;start of `cp *`
_18:
  inc e
_18a:
;Now we have four more iterations
;The first two are no problem
  ld a,ixl
  sll e
  rl d
  add a,a
  adc hl,hl
  add a,a
  adc hl,hl
  sbc hl,de
  jr nc,_19
  add hl,de
  dec e
  jr _19a  ;.db $FE     ;start of `cp *`
_19:
  inc e
_19a:
  sll e
  rl d
  add a,a
  adc hl,hl
  add a,a
  adc hl,hl
  sbc hl,de
  jr nc,_20
  add hl,de
  dec e
  jr _20a  ;.db $FE     ;start of `cp *`
_20:
  inc e
_20a:
sqrt32_iter15:
;On the next iteration, HL might temporarily overflow by 1 bit
  sll e
  rl d      ;sla e \ rl d \ inc e
  add a,a
  adc hl,hl
  add a,a
  adc hl,hl       ;This might overflow!
  jr c,sqrt32_iter15_br0
;
  sbc hl,de
  jr nc,_21
  add hl,de
  dec e
  jr sqrt32_iter16
sqrt32_iter15_br0:
  or a
  sbc hl,de
_21:
  inc e

;On the next iteration, HL is allowed to overflow, DE could overflow with our current routine, but it needs to be shifted right at the end, anyways
sqrt32_iter16:
  add a,a
  ld b,a        ;either 0x00 or 0x80
  adc hl,hl
  rla
  adc hl,hl
  rla
;AHL - (DE+DE+1)
  sbc hl,de
  sbc a,b
  inc e
  or a
  sbc hl,de
  sbc a,b
  ret p
  add hl,de
  adc a,b
  dec e
  add hl,de
  adc a,b
  ret

sqrtHL:
;returns A as the sqrt, HL as the remainder, D = 0
;min: 376cc
;max: 416cc
;avg: 393cc
  ld de,$5040
  ld a,h
  sub e
  jr nc,_22
  add a,e
  ld d,$10
_22:
  sub d
  jr nc,_23
  add a,d
  jr _23a  ;.db $01   ;start of ld bc,** which is 10cc to skip the next two bytes.
_23:
  set 5,d
_23a:
  res 4,d
  srl d

  set 2,d
  sub d
  jr nc,_24
  add a,d
  jr _24a  ;.db $01   ;start of ld bc,** which is 10cc to skip the next two bytes.
_24:
  set 3,d
_24a:
  res 2,d
  srl d

  inc d
  sub d
  jr nc,_25
  add a,d
  dec d   ;this resets the low bit of D, so `srl d` resets carry.
  jr _25a  ;.db $06   ;start of ld b,* which is 7cc to skip the next byte.
_25:
  inc d
_25a:
  srl d
  ld h,a


  sbc hl,de
  ld a,e
  jr nc,_26
  add hl,de
_26:
  ccf
  rra
  srl d
  rra
  ld e,a

  sbc hl,de
  jr nc,_27
  add hl,de
  jr _27a  ;.db $01   ;start of ld bc,** which is 10cc to skip the next two bytes.
_27:
  or %00100000
_27a:
  xor %00011000
  srl d
  rra
  ld e,a


  sbc hl,de
  jr nc,_28
  add hl,de
  jr _28a  ;.db $01   ;start of ld bc,** which is 10cc to skip the next two bytes.
_28:
  or %00001000
_28a:
  xor %00000110
  srl d
  rra
  ld e,a
  sbc hl,de
  jr nc,_29
  add hl,de
  srl d
  rra
  ret
_29:
  inc a
  srl d
  rra
  ret

endif

;---------------------------------------------------------------------------------------------------------
; lnSingle
;---------------------------------------------------------------------------------------------------------

if defined MATH_LOG or defined MATH_LN

lnSingle:
; x / (1 + x/(2-x+4x/(3-2x+9x/(4-3x+16x/(5-4x)))))
; a * x ^ (1/a) - a, where a = 100
  call pushpop
  push bc
    ld de, const_100_inv
	ld bc, temp
	call powSingle         ; temp = x ^ (1/100)
	ld hl, temp
	ld de, const_100
	ld bc, temp1
	call mulSingle         ; temp1 = temp * 100
	ld hl, temp1
  pop bc
  call subSingle           ; bc = temp1 - 100
  ret

endif

;---------------------------------------------------------------------------------------------------------
; logSingle
;---------------------------------------------------------------------------------------------------------

if defined MATH_LOG

logSingle:
  call pushpop
  push bc
    ld bc, temp
    call lnSingle
    ld hl, temp
    ld de, const_lg10
  pop bc
  call divSingle
  ret

endif

;---------------------------------------------------------------------------------------------------------
; expSingle
;---------------------------------------------------------------------------------------------------------

if defined MATH_EXP

expSingle:
;;Computes e^x
;;HL points to x
;;BC points to the output
  call pushpop
  ld de,const_lg_e
  push bc
pow_inject:
;;DE points to lg(y), HL points to x, BC points to output
  ld bc,var_x
  call mulSingle
  ld h,b
  ld l,c
  jp exp_inject

endif

;---------------------------------------------------------------------------------------------------------
; sinSingle
; https://en.wikipedia.org/wiki/List_of_trigonometric_identities
; https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions
; https://cs.stackexchange.com/questions/89245/how-approximate-sine-using-taylor-series
; https://stackoverflow.com/questions/42217069/approximating-sinex-with-a-taylor-series-in-c-and-having-a-lot-of-problems
;---------------------------------------------------------------------------------------------------------

if defined MATH_SIN or defined MATH_TAN or defined MATH_COS

sinSingle:
; taylor: x - x^3/6 + x^5/120 - x^7/5040
;         x(1 - x^2(1/6 - x^2(1/120 - x^2/5040)) )
; reduction:
;         var_b = round( x / (2*PI), 0 )
;         var_c = x - var_b*2*PI
;         temp1 = if( var_c >= 0, var_c, var_c + 2*PI )
;         temp2 = if( temp1 > PI, temp1 - PI, temp1 )
;         var_a = if( temp2 > PI/2, PI - temp2, temp2 ) * if( temp1 > PI, -1, 1 )

  call pushpop
  ld de, const_0
  call cmpSingle
  jr nz, sinSingle.1

  call copySingle      ; return 0
  ret

sinSingle.1:
  call trigRangeReductionSinCos
  push bc
    ld hl, var_a
    ld de, var_a
    ld bc, var_b
    call mulSingle    ; var_b = var_a * var_a
    ld hl, var_b
    ld de, sin_a3
    ld bc, temp
    call mulSingle    ; temp = x^2/5040
    ld hl, sin_a2
    ld de, temp
    ld bc, temp1
    call subSingle    ; temp1 = 1/120 - temp
    ld hl, var_b
    ld de, temp1
    ld bc, temp
    call mulSingle    ; temp = x^2 * temp1
    ld hl, sin_a1
    ld de, temp
    ld bc, temp1
    call subSingle    ; temp1 = 1/6 - temp
    ld hl, var_b
    ld de, temp1
    ld bc, temp
    call mulSingle    ; temp = x^2 * temp1
    ld hl, const_1
    ld de, temp
    ld bc, temp1
    call subSingle    ; temp1 = 1 - temp
    ld hl, var_a
    ld de, temp1
  pop bc
  call mulSingle      ; return x * temp1
  ret

trigRangeReductionSinCos:
  call pushpop
  push hl
; var_b = round( x / (2*PI), 0 )
    ld de, const_2pi
    ld bc, var_c
    call divSingle
    ld hl, var_c
	ld de, 0
	ld bc, var_b
	call roundSingle
; var_c = x - var_b*2*PI
    ld hl, var_b
    ld de, const_2pi
    ld bc, temp
    call mulSingle     ; temp = var_b*2*PI
  pop hl
  ld de, temp
  ld bc, var_c
  call subSingle     ; var_c = x - temp
; temp1 = if( var_c >= 0, var_c, var_c + 2*PI )
  ld hl, var_c
  ld de, const_0
  call cmpSingle
  jr nc, trigRangeReductionSinCos.else.2
    ld hl, var_c
    ld bc, temp1
    call copySingle     ; temp1 = var_c
    jr trigRangeReductionSinCos.endif.2
trigRangeReductionSinCos.else.2:
    ld hl, var_c
    ld de, const_2pi
    ld bc, temp1
    call addSingle     ; temp1 = var_c + 2*PI
trigRangeReductionSinCos.endif.2:
; temp2 = if( temp1 > PI, temp1 - PI, temp1 )
  ld hl, const_pi
  ld de, temp1
  call cmpSingle
  jr c, trigRangeReductionSinCos.else.3
  jr z, trigRangeReductionSinCos.else.3
    ld hl, temp1
    ld de, const_pi
    ld bc, temp2
    call subSingle     ; temp2
    jr trigRangeReductionSinCos.endif.3
trigRangeReductionSinCos.else.3:
    ld hl, temp1
    ld bc, temp2
    call copySingle     ; temp2 = temp1
trigRangeReductionSinCos.endif.3:
; var_a = if( temp2 > PI/2, PI - temp2, temp2 ) * if( temp1 > PI, -1, 1 )
  ld hl, const_half_pi
  ld de, temp2
  call cmpSingle
  jr c, trigRangeReductionSinCos.else.4
  jr z, trigRangeReductionSinCos.else.4
    ld hl, const_pi
    ld de, temp2
    ld bc, var_a
    call subSingle     ; var_a
    jr trigRangeReductionSinCos.endif.4
trigRangeReductionSinCos.else.4:
    ld hl, temp2
    ld bc, var_a
    call copySingle     ; var_a = temp2
trigRangeReductionSinCos.endif.4:
; if( temp > PI, -1, 1 )
  ld hl, temp1
  ld de, const_pi
  call cmpSingle
  jr nc, trigRangeReductionSinCos.endif.5
    ld ix, var_a
    ld a, (ix+2)
    set 7, a
    ld (ix+2), a   ; turn var_a to negative
trigRangeReductionSinCos.endif.5:
; return var_a
  ret

endif

;---------------------------------------------------------------------------------------------------------
; cosSingle
;---------------------------------------------------------------------------------------------------------

if defined MATH_COS or defined MATH_TAN

cosSingle:
; taylor: 1 - x^2/2 + x^4/24 - x^6/720
;         1 - x^2(1/2 - x^2(1/24 - x^2/720) )
; reduction: same as sin
;            cos = cos * sign

  call pushpop
  ld de, const_0
  call cmpSingle
  jr nz, cosSingle.1

  ld hl, const_1
  call copySingle      ; return 1
  ret

cosSingle.1:
  ; 1 - x^2(1/2 - x^2(1/24 - x^2/720) )
  call trigRangeReductionSinCos
  push bc
    ld hl, var_a
    ld de, var_a
    ld bc, var_b
    call mulSingle    ; var_b = var_a * var_a
    ld hl, var_b
    ld de, cos_a3
    ld bc, temp
    call mulSingle    ; temp = x^2/720
    ld hl, cos_a2
    ld de, temp
    ld bc, temp1
    call subSingle    ; temp1 = 1/24 - temp
    ld hl, var_b
    ld de, temp1
    ld bc, temp
    call mulSingle    ; temp = x^2 * temp1
    ld hl, cos_a1
    ld de, temp
    ld bc, temp1
    call subSingle    ; temp1 = 1/2 - temp
    ld hl, var_b
    ld de, temp1
    ld bc, temp
    call mulSingle    ; temp = x^2 * temp1
    ld hl, const_1
    ld de, temp
    ld bc, temp1
    call subSingle    ; temp1 = 1 - temp

    ; temp3 = abs(var_c)
    ; temp1 = temp1 * if( temp3 >= PI/2, -1, 1 )       ==> cos sign
	ld hl, var_c
	ld bc, temp3
	call copySingle
	ld ix, temp3
	ld a, (ix+2)
    res 7, a
	ld (ix+2), a      ; temp3 = abs(var_c)
	ld hl, temp3
	ld de, const_half_pi
    call cmpSingle    ; if temp3 >= PI/2 then temp1 = -temp1
    jr nc, cosSingle.endif.1
	ld ix, temp1
	ld a, (ix+2)
    set 7, a
	ld (ix+2), a      ; temp1 = -temp1
    cosSingle.endif.1:
  pop bc
  ld hl, temp1
  call copySingle      ; return temp1
  ret

endif

;---------------------------------------------------------------------------------------------------------
; tanSingle
;---------------------------------------------------------------------------------------------------------

if defined MATH_TAN

tanSingle:
  call pushpop
  push bc
  ;HL points to input
  ld bc,var_z
  ld d,b
  ld e,c
  call cosSingle
  ld bc,var_x
  call sinSingle
  ld h,b
  ld l,c
  pop bc
  jp divSingle

endif

;---------------------------------------------------------------------------------------------------------
; atanSingle
;---------------------------------------------------------------------------------------------------------

if defined MATH_ATN

atanSingle:
;taylor:    x/(1 + x^2/(3 + (2*x)^2/(5 + (3*x)^2/(7+(4*x)^2/9) ) ) )
;           x < -1: atan - PI/2
;           x >= 1: PI/2 - atan
;reduction: abs(X) > 1 : Y = 1 / X
;           abs(X) <= 1: Y = X
;           X < 0: Y = -Y

  call pushpop
  ld de, const_0
  call cmpSingle
  jr nz, atanSingle.1

  ld hl, const_0
  call copySingle      ; return 0
  ret

atanSingle.1:
  ;x/(1 + x^2/(3 + (2*x)^2/(5 + (3*x)^2/(7+(4*x)^2/9) ) ) )
  call trigRangeReductionAtan
  push bc
  push hl
    ld hl, var_a
    ld de, var_a
    ld bc, var_b
    call mulSingle    ; var_b = var_a * var_a
    ld hl, var_b
    ld de, const_16
    ld bc, temp
    call mulSingle    ; temp = (4*x)^2
    ld hl, temp
    ld de, const_9
    ld bc, temp1
    call divSingle    ; temp1 = temp/9
    ld hl, temp1
    ld de, const_7
    ld bc, temp
    call addSingle    ; temp = 7 + temp1
    ld hl, var_b
    ld de, const_9
    ld bc, temp1
    call mulSingle    ; temp1 = var_b * 9
    ld hl, temp1
    ld de, temp
    ld bc, temp2
    call divSingle    ; temp2 = temp1 / temp
    ld hl, temp2
    ld de, const_5
    ld bc, temp
    call addSingle    ; temp = 5 + temp2
    ld hl, var_b
    ld de, const_4
    ld bc, temp1
    call mulSingle    ; temp1 = var_b * 4
    ld hl, temp1
    ld de, temp
    ld bc, temp2
    call divSingle    ; temp2 = temp1 / temp
    ld hl, temp2
    ld de, const_3
    ld bc, temp
    call addSingle    ; temp = 3 + temp2
    ld hl, var_b
    ld de, temp
    ld bc, temp2
    call divSingle    ; temp2 = var_b / temp
    ld hl, temp2
    ld de, const_1
    ld bc, temp
    call addSingle    ; temp = 1 + temp2
    ld hl, var_a
    ld de, temp
    ld bc, temp2
    call divSingle    ; temp2 = var_a / temp
  pop hl
; x >= 1: PI/2 - atan
  ld de, const_1
  call cmpSingle
  jr nc, atanSingle.2
    ld hl, const_half_pi
    ld de, temp2
    ld bc, temp
    call subSingle
    ld hl, temp
    jr atanSingle.4
atanSingle.2:
; x < -1: atan - PI/2
  push hl
    ld hl, const_0
	ld de, const_1
	ld bc, temp
	call subSingle
  pop hl
  ld de, temp
  call cmpSingle
  jr c, atanSingle.3
    ld hl, temp2
    ld de, const_half_pi
    ld bc, temp
    call subSingle
    ld hl, temp
    jr atanSingle.4
atanSingle.3:
  ld hl, temp2
atanSingle.4:
  pop bc
  call copySingle      ; return temp2
  ret

trigRangeReductionAtan:
;reduction: abs(X) > 1 : Y = 1 / X
;           abs(X) <= 1: Y = X
;           X < 0: Y = -Y
  call pushpop
  push hl
    ld bc, temp
    call copySingle
    ld ix, temp
    ld a, (ix+2)
    res 7, a
    ld (ix+2), a   ; abs(x)
    ld hl, temp
    ld de, const_1
    call cmpSingle
    jr nc, trigRangeReductionAtan.1
      ld hl, const_1
	  pop de
	  push de
	  ld bc, var_a
	  call divSingle
	  jr trigRangeReductionAtan.2
trigRangeReductionAtan.1:
	  pop hl
	  push hl
	  ld bc, var_a
	  call copySingle
trigRangeReductionAtan.2:
  pop hl
  ld de, const_0
  call cmpSingle
  jr c, trigRangeReductionAtan.3
    ld ix, var_a
    ld a, (ix+2)
    set 7, a
    ld (ix+2), a   ; y = -y
trigRangeReductionAtan.3:
  ret

endif

if defined MATH_SIN or defined MATH_TAN or defined MATH_COS

;---------------------------------------------------------------------------------------------------------
; copySingle
;---------------------------------------------------------------------------------------------------------

copySingle:
    call pushpop
	;push bc
	;pop de
	ld d, b
	ld e, c
	ldi
	ldi
	ldi
	ldi
	ret

;---------------------------------------------------------------------------------------------------------
; roundSingle
;---------------------------------------------------------------------------------------------------------

roundSingle:
    call pushpop
	call copySingle
	;push bc
	;pop hl
	ld h, b
	ld l, c
	push de
	  ld a, e
	  ld de, const_10
roundSingle.1:
	  or 0
	  jr z, roundSingle.2
	  ld bc, temp
	  call mulSingle
	  ;push hl
	  ;pop bc
	  ld b, h
	  ld c, l
	  ld hl, temp
	  call copySingle
	  ;push bc
	  ;pop hl
	  ld h, b
	  ld l, c
	  dec a
	  jr roundSingle.1
roundSingle.2:
      ld de, const_half_1
	  ld bc, temp
	  call addSingle
      push hl
	    ld hl, temp
	    ld bc, temp1
	    call single2Int
	    ld hl, temp1
	  pop bc
	  call int2Single
	  ;push bc
	  ;pop hl
	  ld h, b
	  ld l, c
	pop de
    ld a, e
	ld de, const_10
roundSingle.3:
	or 0
	jr z, roundSingle.4
	ld bc, temp
	call divSingle
	;push hl
	;pop bc
	ld b, h
	ld c, l
	ld hl, temp
	call copySingle
	;push bc
	;pop hl
	ld h, b
	ld l, c
	dec a
	jr roundSingle.3
roundSingle.4:
	ret

endif

if defined MATH_ABSFN

;---------------------------------------------------------------------------------------------------------
; absSingle
;---------------------------------------------------------------------------------------------------------

absSingle:
;;HL points to the float
;;BC points to where to output the result
    call pushpop
    ld d,b
    ld e,c
    ldi
    ldi
    ld a,(hl)
    and %01111111
    ld (de),a
    inc hl
    inc de
    ld a,(hl)
    ld (de),a
    ret

endif

if defined MATH_SGN

;---------------------------------------------------------------------------------------------------------
; sgnSingle
;---------------------------------------------------------------------------------------------------------

sgnSingle:
;;HL points to the float
;;BC points to where to output the result
    jp negSingle

endif

if defined powSingle or defined sgnSingle or defined MATH_NEG

;---------------------------------------------------------------------------------------------------------
; negSingle
;---------------------------------------------------------------------------------------------------------

negSingle:
;;HL points to the float
;;BC points to where to output the result
    call pushpop
	push hl
	pop ix
	ld a, (ix+3)
	or 0
	jr nz, negSingle.test.sign
	ld a, (ix+2)
	or 0
	jr nz, negSingle.test.sign
	ld a, (ix+1)
	or 0
	jr nz, negSingle.test.sign
	ld a, (ix)
	or 0
	jr nz, negSingle.test.sign
    ;push bc
    ;pop de
	ld d, b
	ld e, c
    ld hl, const_0
    ldi
    ldi
    ldi
    ldi
    ret
negSingle.test.sign:
	ld a, (ix+2)
	bit 7, a
	jr z, negSingle.positive
negSingle.negative:
    push bc
	pop ix
	call negSingle.positive
	ld a, (ix+2)
	set 7, a
	ld (ix+2), a
    ret
negSingle.positive:
    ;push bc
    ;pop de
	ld d, b
	ld e, c
    ld hl, const_1
    ldi
    ldi
    ldi
    ldi
    ret

endif

if defined MATH_DCOMP or defined MATH.POW or defined MATH_EXP or defined MATH_LOG or defined MATH_LN or defined MATH_SIN or defined MATH_TAN or defined MATH_COS or defined MATH_ATN

;---------------------------------------------------------------------------------------------------------
; cmpSingle
;---------------------------------------------------------------------------------------------------------

cmpSingle:
;Input: HL points to float1, DE points to float2
;Output:
;      float1 >= float2 : nc
;      float1 <  float2 : c,nz
;      float1 == float2 : z
;  There is a margin of error allowed in the lower 2 bits of the mantissa.
;
;Currently fails when both numbers have magnitude less than about 2^-106
  push hl
  push de
  push bc
  ld c, a
  push bc
    ex de, hl
    call _30
  pop bc
  ld a, c
  pop bc
  pop de
  pop hl
  ret
_30:
  inc de
  inc de
  inc de
  ld a,(de)
  inc hl
  inc hl
  inc hl
  cp (hl)
  jr nc,_31
  ld a,(hl)
_31:
  dec hl
  dec hl
  dec hl
  dec de
  dec de
  dec de
  push af
  ld bc,scrap
  call subSingle
  ld a,(scrap+3)    ;new power
  pop bc            ;B is old power
  or a
  jr z,cmp_close
  sub b
  jr nc,cmp_is_sign
  dec a
  add a,22
  jr nc,cmp_close
cmp_is_sign:
  ld a,(scrap+2)
  or 1    ;not equal, so reset z flag
  rla     ;if negative, float1<float2, setting c flag as wanted, else nc.
  ret
cmp_close:
  xor a
  ret

endif

if defined MATH_RND

;---------------------------------------------------------------------------------------------------------
; randSingle
;---------------------------------------------------------------------------------------------------------

randSingle:
;Stores a pseudo-random number on [0,1)
;it won't produce values on (0,2^-23)
  call pushpop
  push bc
  call rand
  push hl
  call rand
  pop de
  ex de,hl
  ld bc,$207F
;DEHL is the mantissa, B is the exponent
  ld a,d
  or a
  jp m,rand_normed
_32:
  dec c
  add hl,hl
  rl e
  rl d
  jp m,rand_normed
  djnz _32
rand_zero:
  ld c,l
  ld b,l
  jr rand_done
rand_normed:
;If we needed to shift more than 8 bits, we'll load in more random data
  ld a,b
  cp 8
  jr c,rand_zero
  sub 24
  jp nc,rand_no_more_rand_data
  push bc
  push de
  call rand
  pop de
  ld e,h
  ld h,l
  pop bc
rand_no_more_rand_data:
  ld b,e
  ld e,d
  ld d,c
  ld c,h
  res 7,e
rand_done:
  pop hl
  ;DEBC
  ld (hl),b
  inc hl
  ld (hl),c
  inc hl
  ld (hl),e
  inc hl
  ld (hl),d
  ret

rand:
;;Tested and passes all CAcert tests
;;Uses a very simple 32-bit LCG and 32-bit LFSR
;;it has a period of 18,446,744,069,414,584,320
;;roughly 18.4 quintillion.
;;LFSR taps: 0,2,6,7  = 11000101
;;323cc
;;Thanks to Runer112 for his help on optimizing the LCG and suggesting to try the much simpler LCG. On their own, the two are terrible, but together they are great.
;Uses 64 bits of state
  ld hl,(seed0)
  ld de,(seed0+2)
  ld b,h
  ld c,l
  add hl,hl
  rl e
  rl d
  add hl,hl
  rl e
  rl d
  inc l
  add hl,bc
  ld (seed0),hl
  ld hl,(seed0+2)
  adc hl,de
  ld (seed0+2),hl
  ex de,hl
;;lfsr
  ld hl,(seed1)
  ld bc,(seed1+2)
  add hl,hl
  rl c
  rl b
  ld (seed1+2),bc
  sbc a,a
  and %11000101
  xor l
  ld l,a
  ld (seed1),hl
  ex de,hl
  add hl,bc
  ret

endif

if defined MATH_FOUT

;---------------------------------------------------------------------------------------------------------
; single2Str
; in  HL = Single address
;     BC = String address
; out A = String size
; http://0x80.pl/notesen/2015-12-29-float-to-string.html
; http://0x80.pl/articles/convert-float-to-integer.html
;---------------------------------------------------------------------------------------------------------

single2str:
  call pushpop
  push bc
  call _33
  pop de
  xor a
  cp (hl)
  ldi
  jr nz,$-3

  ret
_33:
; Move the float to scrap
  ld de,scrap
  call mov4

; Make the float negative, write a '-' if already negative
  ld de,strout_single
  ld hl,scrap+2
  ld a,(hl)
  ;rlca
  ;scf
  ;rra
  bit 7, a
  jr z, _34
  ld a,'-'      ; write '-' simbol
  ld (de),a
  inc de
  ld a,(hl)
_34:
  set 7, a
  ld (hl),a

; Check if the exponent field is 0 (a special value)
  inc hl
  ld a,(hl)
  or a
  jp z,strcase_single


; We should write '0' next. When rounding 9.999999... for example, not padding with a 0 will return '.' instead of '1.'
  ex de,hl
  ld (hl),'0'
  inc hl

; Save the pointer
  push hl

; Now we need to perform signed (A-128)*77 (approximation of exponent*log10(2))
  ld de,77
  ld h,a
  ld l,d
  call mul8_preset
  ld de,-77*128
  add hl,de
  ld a,h
  ld (pow10exp_single),a    ;The base-10 exponent
  ld de,pown10LUT
  jr c,_35
  neg
  ld de,pow10LUT   ;get the table of 10^-(2^k)
_35:
  ld hl, pow10exp_single
  ld bc,scrap
  call singletostr_mul
  call singletostr_mul
  call singletostr_mul
  call singletostr_mul
  call singletostr_mul
  call singletostr_mul
;now the number is pretty close to a nice value

; If it is less than 1, multiply by 10
  ld a,(scrap+3)
  sub 128
  jr nc,_36
  ld de,const_10
  ;ld hl,scrap    ;Since singletostr_mul returns BC = scrap, can do this cheaper
  ;ld b,h
  ;ld c,l
  ld h,b
  ld l,c
  call mulSingle
  ld hl,pow10exp_single
  dec (hl)
  ld a,(scrap+3)
  sub 128
_36:

; Convert to a fixed-point number !
  inc a
  ld b,a
  xor a
_37:
  ld hl,scrap
  sla (hl)
  inc hl
  rl (hl)
  inc hl
  rl (hl)
  rla
  djnz _37

;We need to get 7 digits
  ld b,6
  pop hl    ;Points to the string

;The first digit can be as large as 20, so it'll actually be two digits
  cp 10
  jr c,_38
  dec b
;Increment the exponent :)
  ld de,(pow10exp_single-1)
  inc d
  ld (pow10exp_single-1),de
;
  ld (hl),'0'-1
  inc (hl)
  sub 10
  jr nc,$-3
  add a,10
  inc hl
_38:
; Get the remaining digits.
_39:
  add a,'0'
  ld (hl),a
  inc hl
  push hl
  push bc
  call singletostrmul10
  pop bc
  pop hl
  djnz _39

;Save the pointer to the end of the string
  ld d,h
  ld e,l
  ;ld (hl), 0

;Now let's round!
  cp 5
  jr c,rounding_done_single
  jr _40a  ;.db $DA ;start of `jp c,*` in order to skip the next instruction
_40:
  ld (hl),'0'
_40a:
  dec hl
  inc (hl)
  ld a,(hl)
  cp $3A
  jr z,_40
rounding_done_single:


;Strip the leading zero if it exists (rounding may have bumped this to `1`)
  ld hl,strout_single
  ld a,(hl)
  cp '-'
  jr nz,_41
  inc hl
  ld a,(hl)
_41:
  cp '0'
  jr nz,_42
  dec de
  ex de,hl
  ;Now lets move HL-DE bytes at DE+1 to DE
  sbc hl,de
  ld b,h
  ld c,l
  ld h,d
  ld l,e
  inc hl
  ldir
  cp a
_42:

  push de
;If z flag is reset, this means that the exponent should be bumped up 1
  ld a,(pow10exp_single)
  jr z,_43
  inc a
  ld (pow10exp_single),a
_43:

  ;if -4<=A<=6, then need to insert the decimal place somewhere.
  add a,4
  cp 10
  jp c,movdec_single
_44:
  ;for this, we need to insert the decimal after the first digit
  ;Then, we need to append the exponent string
  ld hl,strout_single
  ld de,strout_single-1
  ld a,(hl)
  cp '-'    ;negative sign
  jr nz,_45
  ldi
_45:
  ldi
  ld a,'.'
  ld (de),a

;remove any stray zeroes at the end before appending the exponent
  pop hl
  call strip_zeroes

; Write the exponent
  ld (hl),'e'
  inc hl
  ld a,(pow10exp_single)
  or a
  jp p,_46
  ld (hl),'-'    ;negative sign
  inc hl
  neg
_46:
  cp 10
  jr c,_47
  ld (hl),'0'-1
  inc (hl)
  sub 10
  jr nc,$-3
  add a,10
  inc hl
_47:
  add a,'0'
  ld (hl),a
  inc hl
  ld (hl),0

  ld de, strout_single
  xor a
  sbc hl, de
  ld a, l         ; string size

  ld hl,strout_single-1
  ret

movdec_single:
  ld a,(pow10exp_single)
  or a
  jp p,posdec_single
  ld l,a
;need to put zeroes before everything
  ld de,strout_single
  ld a,(de)
  cp '-'    ;negative sign
  push af
  ld a,'0'
  jr z,$+3
_48:
  dec de
  ld (de),a
  inc l
  jr nz,_48
_49:
  ex de,hl
  ld (hl),'.'
  pop af
  jr nz,_50
  dec hl
  ld (hl),a
_50:
  ex de,hl
  pop hl
  call strip_zeroes
  ld (hl),0
  ex de,hl
  ret

posdec_single:
  ld hl,strout_single
  ld de,strout_single-1
  ld c,a
  ld a,(hl)
  ld b,0
  cp '-'    ;negative sign
  jr nz,_51
  inc c
_51:
  inc c
  ldir
  ld a,'.'
  ld (de),a
  pop hl
  call strip_zeroes
  ld (hl),0
  ld hl,strout_single-1
  ret

strcase_single:
  ld hl,str_Zero
  ld a,(scrap+2)
  add a,a
  and $C0
  jr z,_52
  ld hl,str_Inf
  jp pe,_52
  ld hl,str_NaN
_52:
  call mov4
  ld hl,strout_single
  ret

singletostrmul10:
;multiply the 0.24 fixed point number at scrap by 10
;overflow in A register
  ld a,(scrap+2)
  ld e,a
  ld hl,(scrap)
  xor a
  ld d,e
  ld b,h
  ld c,l
  add hl,hl
  rl d
  rla
  add hl,hl
  rl d
  rla
  add hl,bc
  ld b,a
  ld a,d
  adc a,e
  ld d,a
  ld a,b
  adc a,0
  add hl,hl
  rl d
  rla
  ld (scrap+1),de
  ld (scrap),hl
  ret

strip_zeroes:
  ld a,'0'
_53:
  dec hl
  cp (hl)
  jr z,_53

;Check that the last  digit isn't a decimal!
  ld a,'.'
  cp (hl)
  ret z
  inc hl
  ret

singletostr_mul:
  rra
  call c,_54
  ld hl,4
  add hl,de
  ex de,hl
  ret
_54:
  ld h,b
  ld l,c
  jp mulSingle
mul8:
;H*E => HL
  ld l,0
  ld d,l
mul8_preset:
  sla h
  jr nc,$+3
  ld l,e
  add hl,hl
  jr nc,$+3
  add hl,de
  add hl,hl
  jr nc,$+3
  add hl,de
  add hl,hl
  jr nc,$+3
  add hl,de
  add hl,hl
  jr nc,$+3
  add hl,de
  add hl,hl
  jr nc,$+3
  add hl,de
  add hl,hl
  jr nc,$+3
  add hl,de
  add hl,hl
  ret nc
  add hl,de
  ret

endif


if defined MATH_FIN

;---------------------------------------------------------------------------------------------------------
; str2Single
; https://www.ticalc.org/pub/86/asm/source/routines/atof.asm
;---------------------------------------------------------------------------------------------------------

char_NEG: equ  '-'
char_ENG: equ  ','
char_DEC: equ  '.'
ptr_sto: equ scrap+9

;;#Routines/Single Precision
;;Inputs:
;;  HL points to the string
;;  BC points to where the float is output
;;Output:
;;  scrap+9 is the pointer to the end of the string
;;Destroys:
;;  11 bytes at scrap ?

str2single:
  call pushpop
  push bc
;Check if there is a negative sign.
;   Save for later
;   Advance ptr
  ld a,(hl)
  sub char_NEG
  sub 1
  push af
  jr nc,$+3
  inc hl
;Skip all leading zeroes
  ld a,(hl)
  cp '0'
  jr z,$-4      ;jumps back to the `inc hl`
;Set exponent to 0
  ld b,0
;Check if the next char is char_DEC
  sub char_DEC
  or a      ;to reset the carry flag
  jr nz,_55
  jr _54a   ;.db $FE   ;start of cp *
;Get rid of zeroes
  dec b
_54a:
  inc hl
  ld a,(hl)
  cp '0'
  jr z,$-5      ;jumps back to the `dec b`
  scf
_55:
;Now we read in the next 8 digits
  ld de,scrap+3
  call ascii_to_uint8
  call ascii_to_uint8
  call ascii_to_uint8
  call ascii_to_uint8
;Now `scrap` holds the 4-digit base-100 number.
;b is the exponent
;if carry flag is set, just need to get rid of remaining digits
;Otherwise, need to get rid of remaining digits, while incrementing the exponent
  sbc a,a
  inc a
  ld c,a
_56:
  ld a,(hl)
  cp 30h
  jr nz,_57
  inc hl
  ld a,b
  add a,c
  jp z,strToSingle_inf
  ld b,a
  jr _56
;Now check for engineering `E` to modify the exponent
_57:
  cp char_NEG
  call z,str_eng_exp
;Gotta multiply the number at (scrap) by 2^24
  ld (ptr_sto),hl
  ld d,100
  call scrap_times_256
  ld a,c
  ld (scrap+6),a
  call scrap_times_256
  ld a,c
  ld (scrap+5),a
  call scrap_times_256
  ld a,c
  ld (scrap+4),a
  call scrap_times_256
  ld a,c
  ld (scrap+3),a
;Now scrap+3 is a 4-byte mantissa that needs to be normalized
;
  ld hl,(scrap+3)
  ld a,h
  or l
  ld hl,(scrap+5)
  or l
  or h
  jp z,strToSingle_zero-1
  ld c,$7F
  ld a,h
  or a
  jp m,strToSingle_normed
  ;Will need to iterate at most three times
_58:
  dec c
  ld hl,scrap+3
  sla (hl)
  inc hl
  rl (hl)
  inc hl
  rl (hl)
  inc hl
  adc a,a
  jp p,_58
strToSingle_normed:
;Move the number to scrap
  ld hl,(scrap+4)
  ld (scrap),hl
  ld l,a
  ld h,c
  sla l
  pop af
  rr l
  ld (scrap+2),hl
;now (scrap) is our number, need to multiply by power of 10!
;Power of 10 is stored in B, need to put in A first
  xor a
  sub b
  ld de,pown10LUT
  jp p,_59
  ld a,b
  ld de,pow10LUT
  cp 40
  jp nc,strToSingle_inf+1
_59:
  cp 40
  jp nc,strToSingle_zero
  ld hl,scrap
  ld b,h
  ld c,l
  call _60
  call _60
  call _60
  call _60
  call _60
  call _60
  pop de
  jp mov4
_60:
  rra
  call c,mulSingle
  inc de
  inc de
  inc de
  inc de
  ret
str_eng_exp:
  ld de,0
  inc hl
  ld a,(hl)
  cp char_NEG    ;negative exponent?
  push af
  jr nz,$+3
  inc hl
_61:
  ld a,(hl)
  sub 3Ah
  add a,10
  jr nc,_62
  inc hl
  push hl
  ld h,d
  ld l,e
  add hl,hl
  add hl,hl
  add hl,de
  add hl,hl
  add a,l
  ld l,a
  ex de,hl
  pop hl
  jp c,eng_overflow
  inc d
  dec d
  jp z,_61
  jp nz,eng_overflow
_62:
  ld a,e
  cp 40
  jr nc,eng_overflow
  pop af
  ld a,b
  jr nz,_63
  sub e
  ld b,a
  ret
_63:
  add a,e
  ld b,a
  ret
scrap_times_256:
  ld e,8
_64:
  or a
  ld hl,scrap
  call _65
  call _65
  rl c
  dec e
  jr nz,_64
  ret
_65:
  call scrap_times_sub
scrap_times_sub:
  ld a,(hl)
  rla
  cp d
  jr c,$+3
  sub d
  ld (hl),a
  inc hl
  ccf
  ret
eng_overflow:
  pop af
  jr nz,strToSingle_inf
  pop af
strToSingle_zero:
  ld hl,const_0
  pop de
  jp mov4
strToSingle_inf:
;return inf
  pop af
  ld hl,const_inf
  jr nc,_66
  ld hl,const_NegInf
_66:
  pop de
  jp mov4

endif

if defined roundSingle or defined MATH_FRCSGL

;---------------------------------------------------------------------------------------------------------
; int2Single
; http://wikiti.brandonw.net/index.php?title=Z80_Routines:Math:Division#24.2F8_division
;---------------------------------------------------------------------------------------------------------

int2Single:
    call pushpop
	push bc
	    push hl
		pop ix
	    ld l, (ix)            ; convert integer parameter to single float
		ld h, (ix+1)
		ld bc, 0x1000         ; bynary digits count + sign

int2Single.test.zero:
        xor a
		or h                  ; test if hl is not zero
		jr nz, int2Single.test.negative
		or l
		jr nz, int2Single.test.negative
		ld hl, 0
		ld de, 0
		jp int2Single.save

int2Single.test.negative:
        bit 7, h              ; test if hl is negative
		jr z, int2Single.normalize
		ld c, 0x80            ; sign negative
		ld a, h               ;\
		cpl                   ; |
		ld h, a               ; | abs(hl)
		ld a, l               ; |
		cpl                   ; |
		ld l, a               ;/
		inc hl

int2Single.normalize:
        dec b
        bit 7, h
		jr nz, int2Single.mount
		sla l
		rl h
		jr int2Single.normalize

int2Single.mount:
		res 7, h              ; turn off upper bit

        ld a, c               ; restore sign
        or h                  ; put sign...
        ld h, a               ; ...into upper mantissa

		ld e, h               ; sign+mantissa
		ld h, l               ; high mantissa
		ld l, 0               ; low mantissa

        ld a, b               ; binary digits count
        or 0x80               ; exponent bias
        ld d, a               ; exponent

int2Single.save:
    pop ix
	ld (ix),   l          ; low mantissa
	ld (ix+1), h          ; high mantissa
	ld (ix+2), e          ; sign + mantissa
	ld (ix+3), d          ; expoent
	ld (ix+4), 0
	ld (ix+5), 0
	ld (ix+6), 0
	ld (ix+7), 0
	ret

endif

if defined roundSingle or defined MATH_FRCINT

;---------------------------------------------------------------------------------------------------------
; single2Int
; http://0x80.pl/articles/convert-float-to-integer.html
;---------------------------------------------------------------------------------------------------------
single2Int:
;Input:
; HL points to the single-precision float
;Output:
; HL is the 16-bit signed integer part of the float
; BC points to 16-bit signed integer
  call pushpop
  push bc
    ld e,(hl)
    inc hl
    ld d,(hl)
    inc hl
    ld a,(hl)
    add a,a
    push af
    scf
    rra
    ld c,a
    inc hl
    ld a,(hl)
    ld hl,0
    sub 80h
    jr c,no_shift_single_to_int16
    cp 39
    jr nc,no_shift_single_to_int16
    sub 8
    jr c,_67
    ld l,c
    ld c,d
    ld d,e
    ld e,h
    sub 8
    jr c,_67
    ld h,l
    ld l,c
    ld c,d
    ld d,e
    sub 8
    jr c,_67
    ld h,l
    ld l,c
    ld c,d
    sub 8
    jr c,_67
    ld h,l
    ld l,c
    jr _67a ;.db $11 ;start of ld de,*
_67:
    add a,9
_67a:
    ld b,a
    ld a,e
_68:
    add a,a
    rl d
    rl c
    adc hl,hl
    djnz _68
no_shift_single_to_int16:
    pop af
    jr nc,_69
    ;need to negate
    xor a
    sub e
    ld e,0
    ld a,e
    sbc a,d
    ld a,e
    sbc a,c
    ld d,e
    ex de,hl
    sbc hl,de
_69:
  pop ix
  ld (ix), l
  ld (ix+1), h
  ret

endif

;---------------------------------------------------------------------------------------------------------
; Auxiliary routines
;---------------------------------------------------------------------------------------------------------

str_Zero: db "0",0
str_Inf:  db "inf",0
str_NaN:  db "NaN",0

start_const:
const_pi:      db $DB,$0F,$49,$81
const_e:       db $54,$f8,$2d,$81
const_lg_e:    db $3b,$AA,$38,$80
const_ln_2:    db $18,$72,$31,$7f
const_log2:    db $9b,$20,$1a,$7e
const_lg10:    db $78,$9a,$54,$81
const_0:       db $00,$00,$00,$00
const_1:       db $00,$00,$00,$80
const_2:       dw 0, 33024
const_3:       dw 0, 33088
const_4:       dw 0, 33280
const_5:       dw 0, 33312
const_7:       dw 0, 33376
const_9:       dw 0, 33552
const_16:      dw 0, 33792
const_100:     db $00,$00,$48,$86
const_100_inv: dw 55050, 31011
const_precision: db $77,$CC,$2B,$65  ;10^-8
const_half_1:  dw 0, 32512
const_inf:     db $00,$00,$40,$00
const_NegInf:  db $00,$00,$C0,$00
const_NaN:     db $00,$00,$20,$00
const_log10_e: db $D9,$5B,$5E,$7E
const_2pi:     db $DB,$0F,$49,$82
const_2pi_inv: db $83,$F9,$22,$7D
const_half_pi: dw 4059, 32841
const_p25:     db $00,$00,$00,$7E
const_p5:      db $00,$00,$00,$7F
;     db $,$,$,$
end_const:
sin_a1: dw 43691, 32042
sin_a2: dw 34952, 30984
sin_a3: dw 3329, 29520
cos_a1: equ const_half_1
cos_a2: dw 43691, 31530
cos_a3: dw 2914, 30262
exp_a1: db $15,$72,$31,$7F  ;.693146989552
exp_a2: db $CE,$FE,$75,$7D  ;.2402298085906
exp_a3: db $7B,$42,$63,$7B  ;.0554833215071
exp_a4: db $FD,$94,$1E,$79  ;.00967907584392
exp_a5: db $5E,$01,$23,$76  ;.001243632065103
exp_a6: db $5F,$B7,$63,$73  ;.0002171671843714
const_1p40625: db $00,$00,$34,$80  ;1.40625

if defined MATH_CONSTSINGLE

iconstSingle:
    ex (sp),hl
    ld a,(hl)
    inc hl
    ex (sp),hl
constSingle:
;A is the constant ID#
;returns nc if failed, c otherwise
;HL points to the constant
    cp (end_const-start_const)>>2
    ret nc
    ld hl,start_const
    add a,a
    add a,a
    add a,l
    ld l,a
;#if ((end_const-4)>>8)!=(start_const>>8)
;    ccf
;    ret c
;    inc h
;#endif
    scf
    ret

endif

;;LUTs used
lut:
pown10LUT:
db $CD,$CC,$4C,$7C  ;.1
db $0A,$D7,$23,$79  ;.01
db $17,$B7,$51,$72  ;.0001
db $77,$CC,$2B,$65  ;10^-8
db $95,$95,$66,$4A  ;10^-16
db $1F,$B1,$4F,$15  ;10^-32
pow10LUT:
const_10:
db $00,$00,$20,$83 ;10
db $00,$00,$48,$86 ;100
db $00,$40,$1C,$8D ;10000
db $20,$BC,$3E,$9A ;10^8
db $CA,$1B,$0E,$B5 ;10^16
db $AE,$C5,$1D,$EA ;10^32

C_Times_BDE:
;;C*BDE => CAHL
;C = 0     157
;C = 1     141
;141+
;C>=128    135+6{0,33+{0,1}}+{0,20+{0,8}}
;C>=64     115+5{0,33+{0,1}}+{0,20+{0,8}}
;C>=32     95+4{0,33+{0,1}}+{0,20+{0,8}}
;C>=16     75+3{0,33+{0,1}}+{0,20+{0,8}}
;C>=8      55+2{0,33+{0,1}}+{0,20+{0,8}}
;C>=4      35+{0,33+{0,1}}+{0,20+{0,8}}
;C>=2      15+{0,20+{0,8}}
;min: 141cc
;max: 508cc
;avg: 349.21279907227cc

  ld a,b
  ld h,d
  ld l,e
  sla c
  jr c,mul8_24_1
  sla c
  jr c,mul8_24_2
  sla c
  jr c,mul8_24_3
  sla c
  jr c,mul8_24_4
  sla c
  jr c,mul8_24_5
  sla c
  jr c,mul8_24_6
  sla c
  jr c,mul8_24_7
  sla c
  ret c
  ld a,c
  ld h,c
  ld l,c
  ret
mul8_24_1:
    add hl,hl
    rla
    rl c
    jr nc,$+7
    add hl,de
    adc a,b
    jr nc,$+3
    inc c
mul8_24_2:
    add hl,hl
    rla
    rl c
    jr nc,$+7
    add hl,de
    adc a,b
    jr nc,$+3
    inc c
mul8_24_3:
    add hl,hl
    rla
    rl c
    jr nc,$+7
    add hl,de
    adc a,b
    jr nc,$+3
    inc c
mul8_24_4:
    add hl,hl
    rla
    rl c
    jr nc,$+7
    add hl,de
    adc a,b
    jr nc,$+3
    inc c
mul8_24_5:
    add hl,hl
    rla
    rl c
    jr nc,$+7
    add hl,de
    adc a,b
    jr nc,$+3
    inc c
mul8_24_6:
    add hl,hl
    rla
    rl c
    jr nc,$+7
    add hl,de
    adc a,b
    jr nc,$+3
    inc c
mul8_24_7:
    add hl,hl
    rla
    rl c
    ret nc
    add hl,de
    adc a,b
    ret nc
    inc c
    ret

pushpop:
;26 bytes, adds 118cc to the traditional routine
  ex (sp),hl
  push de
  push bc
  push af
  push hl
  ld hl,pushpopret
  ex (sp),hl
  push hl
  push af
  ld hl,12
  add hl,sp
  ld a,(hl)
  inc hl
  ld h,(hl)
  ld l,a
  pop af
  ret
pushpopret:
  pop af
  pop bc
  pop de
  pop hl
  ret

mov4:
  ldi
  ldi
  ldi
  ldi
  ret

if defined MATH_FIN

ascii_to_uint8:
;c flag means don't increment the exponent
  ld c,0
  ld a,(hl)
  jr c,ascii_to_uint8_noexp
  cp char_DEC
  jr z,ascii_to_uint8_noexp-2
_70:
  sub 3Ah
  add a,10
  jr nc,ascii_to_uint8_noexp_end
  inc b
  ld c,a
  add a,a
  add a,a
  add a,c
  add a,a
  ld c,a
  inc hl
_71:
  ld a,(hl)
  cp char_DEC
  jr z,ascii_to_uint8_noexp_2nd
_72:
  sub 3Ah
  add a,10
  jr nc,ascii_to_uint8_noexp_end
  inc b
  add a,c
  inc hl
  ld (de),a
  dec de
  or a
  ret

  inc hl
  ld a,(hl)
ascii_to_uint8_noexp:
  sub 3Ah
  add a,10
  jr nc,ascii_to_uint8_noexp_end
  ld c,a
  add a,a
  add a,a
  add a,c
  add a,a
  ld c,a
ascii_to_uint8_noexp_2nd:
  inc hl
  ld a,(hl)
  sub 3Ah
  add a,10
  jr nc,ascii_to_uint8_noexp_end
  add a,c
  inc hl
  jr ascii_2  ;.db $FE   ;start of `cp **`, saves 1cc
ascii_to_uint8_noexp_end:
  ld a,c
ascii_2:
  ld (de),a
  dec de
  scf
  ret

endif

if defined MATH_RSUBSINGLE

rsubSingle:
;;-x+y
    push af
    push hl
    push de
    push bc
    push de
    ld de,addend2
    ldi
    ldi
    ld a,(hl)
    xor 80h
    ld (de),a
    inc de
    inc hl
    ld a,(hl)
    ld (de),a
    pop de
    ld hl,addend2
    jp addInject    ;jumps in to the addSingle routine

endif

if defined MATH_MOD1SINGLE

;This routine performs `x mod 1`, returning a non-negative value.
;+inf -> NaN
;-inf -> NaN
;NaN  -> NaN
mod1Single:
  call pushpop
  push bc
  ld e,(hl)
  inc hl
  ld d,(hl)
  inc hl
  ld c,(hl)
  ld a,c
  xor 80h
  push af
  jp p,mod1Single.1
  ld c,a
mod1Single.1:

  inc hl
  ld a,(hl)
  ld b,a
  or a
  jr z,mod1Single_special
  sub $80
  jr c,mod1_end
  inc a
  ld b,a
  ld a,c
  ex de,hl
mod1Single.2:
  add hl,hl
  rla
  djnz mod1Single.2
  ld c,a

;If it is zero, need to set exponent to zero and return
  or h
  or l
  ex de,hl
  jr z,mod1_end

;Need to normalize
  ld b,$7F
  ld a,c
  or a
  jp m,mod1_end
  ex de,hl
mod1Single.3:
  dec b
  add hl,hl
  adc a,a
  jp p,mod1Single.3
  ld c,a
  ex de,hl
mod1_end:
  pop af
  pop hl
  jp m,mod1Single.4
  ;make sure it isn't zero else we need to add 1
  ld a,b
  or a
  jr z,mod1Single.4
  ld (scrap),de
  ld (scrap+2),bc
  ld b,h
  ld c,l
  ld hl,scrap
  ld de,const_1
  jp addSingle
mod1Single_special:
;If INF, need to return NaN instead
;For 0 and NaN, just return itself :)
  pop af
  pop hl
  ld a,c
  add a,a
  jp p,mod1Single.4
  ld c,$40
mod1Single.4:
  res 7,c
  ld (hl),e
  inc hl
  ld (hl),d
  inc hl
  ld (hl),c
  inc hl
  ld (hl),b
  ret

endif

if defined MATH_FOUT

; --------------------------------------------------------------
; Converts a signed integer value to a zero-terminated ASCII
; string representative of that value (using radix 10).
; References:
; Brandon Wilson WikiTI
; http://wikiti.brandonw.net/index.php?title=Z80_Routines:Other:DispA#Decimal_Signed_Version
; --------------------------------------------------------------
; INPUTS:
;     HL     Value to convert (two's complement integer).
;     DE     Base address of string destination. (pointer).
; --------------------------------------------------------------
; OUTPUTS:
;     A      Size of string
; --------------------------------------------------------------
; REGISTERS/MEMORY DESTROYED
; AF HL
; --------------------------------------------------------------

IntToStr:
   push    de
   push    bc

; Detect sign of HL.
    bit    7, h
    jr     z, _DoConvert

; HL is negative. Output '-' to string and negate HL.
    ld     a, '-'
    ld     (de), a
    inc    de

; Negate HL (using two's complement)
    xor    a
    sub    l
    ld     l, a
    ld     a, 0     ; Note that XOR A or SUB A would disturb CF
    sbc    a, h
    ld     h, a

; Convert HL to digit characters
_DoConvert:
    ld     b, 0     ; B will count character length of number
_DoConvert.1:
    ld     c, 10
    call div_hl_c; HL = HL / A, A = remainder
    push   af
    inc    b
    ld     a, h
    or     l
    jr     nz, _DoConvert.1

; Retrieve digits from stack
_DoConvert.2:
    pop    af
    or     $30
    ld     (de), a
    inc    de
    djnz   _DoConvert.2

; Terminate string with NULL
    xor    a
    ld     (de), a

    ld h, d
    ld l, e

    pop    bc
    pop    de

    sbc hl, de
    ld a, l           ; string size

    ret

endif

if defined MATH_FIN

;===============================================================
; Convert a string of base-10 digits to a 16-bit value.
; http://z80-heaven.wikidot.com/math#toc32
;Input:
;     DE points to the base 10 number string in RAM.
;Outputs:
;     HL is the 16-bit value of the number
;     DE points to the byte after the number
;     BC is HL/10
;     z flag reset (nz)
;     c flag reset (nc)
;Destroys:
;     A (actually, add 30h and you get the ending token)
;Size:  23 bytes
;Speed: 104n+42+11c
;       n is the number of digits
;       c is at most n-2
;       at most 595 cycles for any 16-bit decimal value
;===============================================================

StrToInt:
     ld hl,0          ;  10 : 210000
     dec de
StrToInt.Skip_spaces:
     inc de           ;   6 : 13
     ld a,(de)        ;   7 : 1A
     or 0
     ret z
     cp 32
     jr z, StrToInt.Skip_spaces
StrToInt.Init:
     push af
     cp '-'
     jr nz, StrToInt.ConvLoop
     inc de
StrToInt.ConvLoop:             ;
     ld a,(de)        ;   7 : 1A
     or 0
     jr z, StrToInt.End
     sub 30h          ;   7 : D630
     cp 10            ;   7 : FE0A
     jr nc, StrToInt.End
                      ;
     inc de           ;   6 : 13
     ld b,h           ;   4 : 44
     ld c,l           ;   4 : 4D
     add hl,hl        ;  11 : 29
     add hl,hl        ;  11 : 29
     add hl,bc        ;  11 : 09
     add hl,hl        ;  11 : 29
                      ;
     add a,l          ;   4 : 85
     ld l,a           ;   4 : 6F
     jr nc, StrToInt.ConvLoop   ;12|23: 30EE
     inc h            ; --- : 24
     jr StrToInt.ConvLoop      ; --- : 18EB

StrToInt.End:
     pop af
     cp '-'
     ret nz
     ld de, 0xffff
     ex de, hl
     dec de
     xor a
     sbc hl, de
     ret

endif

if defined IntToStr

; divides hl by c
; return remainder in a
; http://wikiti.brandonw.net/index.php?title=Z80_Routines:Math:Division
div_hl_c:
   push bc
   xor	a
   ld	b, 16
div_hl_c.loop:
   add	hl, hl
   rla
   jr	c, $+5
   cp	c
   jr	c, $+4
   sub	c
   inc	l
   djnz	div_hl_c.loop
   pop bc
   ret

endif

if defined DIV_EHL

; http://wikiti.brandonw.net/index.php?title=Z80_Routines:Math:Division#24.2F8_division
div_ehl_d:
   xor	a
   ld	b, 24
div_ehl_d.loop:
   add	hl, hl
   rl	e
   rla
   jr	c, $+5
   cp	d
   jr	c, $+4
   sub	d
   inc	l
   djnz	div_ehl_d.loop
   ret

div_dehl_c:
   push bc
   xor	a
   ld	b, 32
div_dehl_c.loop:
   add	hl, hl
   rl	e
   rl	d
   rla
   jr	c, $+5
   cp	c
   jr	c, $+4
   sub	c
   inc	l
   djnz div_dehl_c.loop
   pop bc
   ret

endif



;---------------------------------------------------------------------------------------------------------
; VARIABLES INITIALIZE
;---------------------------------------------------------------------------------------------------------

INITIALIZE_DUMMY:
    xor a
    ld (VAR_DUMMY.COUNTER), a                    ; max circular queue = 8 dummys
    ld hl, VAR_DUMMY.DATA                        ; start of variable dummy circular queue
    ld (VAR_DUMMY.POINTER), hl
    ld b, VAR_DUMMY.LENGTH
    ld c, 0
INITIALIZE_DUMMY.1:
    ld (hl), a
    inc hl
    djnz INITIALIZE_DUMMY.1
    ret

INITIALIZE_DATA:
    ld hl, DATA_ITEMS
    ld (BASIC_DATPTR), hl        ; next DATA pointer to use by READ command
    ld hl, 0
    ld (BASIC_DATLIN), hl        ; index of DATA item to use by READ command
    ret

INITIALIZE_VARIABLES:
    call INITIALIZE_DATA
    call INITIALIZE_DUMMY

    if defined SCREEN
       call gfxInitSpriteCollisionTable
    endif

    ;if defined COMPILE_TO_ROM
    ;   ld ix, BIOS_JIFFY            ; initialize rom clock
    ;   di
    ;     ld (ix), 0
    ;     ld (ix+1), 0
    ;   ei
    ;endif

              ld hl, IDF_8
              ld d, 3       ; string
              ld c, 0       ; variable name 1 (variable number)
              ld b, 255     ; variable name 2 (type flag=fixed)
              call INIT_VAR ; variable initialize
              ret


;---------------------------------------------------------------------------------------------------------
; MAIN WORK AREA - LITERALS / VARIABLES / CONFIGURATIONS
;---------------------------------------------------------------------------------------------------------

if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS

   workAreaPad:
   pgmPage1.pad: equ pageSize - (workAreaPad - pgmArea)

   if pgmPage1.pad >= 0
      ds pgmPage1.pad, 0
   ;else
   ;   .WARNING "There's no free space left on program page 1"
   endif

endif

VAR_STACK.START:     equ ramArea
    ;VAR_STACK.END:       equ VAR_STACK.START + 0x800   ; 2kb (~200 variables)

VAR_STACK.POINTER:   equ VAR_STACK.START

PRINT.CRLF:      db 3, 0, 0, 2
                 dw PRINT.CRLF.DATA, 0, 0, 0
PRINT.CRLF.DATA: db 13,10,0

PRINT.TAB:       db 3, 0, 0, 1
                 dw PRINT.TAB.DATA, 0, 0, 0
PRINT.TAB.DATA:  db 09,0

; null double
LIT_NULL_DBL: dw 0, 0, 0, 0

; null string
LIT_NULL_STR: db 0

; quote string
LIT_QUOTE_CHAR: db '\"'

; logical true
LIT_TRUE: db 2, 0, 0
          dw 0, 0xFFFF, 0, 0

; logical false
LIT_FALSE: db 2, 0, 0
           dw 0, 0, 0, 0


; string literal
LIT_4: db 3, 0, 0, 3
            dw LIT_4_DATA, 0, 0
            db 0
LIT_4_DATA: db "CCC", 0

; string literal
LIT_6: db 3, 0, 0, 3
            dw LIT_6_DATA, 0, 0
            db 0
LIT_6_DATA: db "CDE", 0

; identifier A$
IDF_8:   equ VAR_STACK.POINTER + 0

; numerical literal
LIT_11:   db 2, 0, 0
      dw 0, 1, 0, 0

; string literal
LIT_12: db 3, 0, 0, 3
            dw LIT_12_DATA, 0, 0
            db 0
LIT_12_DATA: db "DDD", 0

; string literal
LIT_13: db 3, 0, 0, 3
            dw LIT_13_DATA, 0, 0
            db 0
LIT_13_DATA: db "EDC", 0

; numerical literal
LIT_14:   db 2, 0, 0
      dw 0, 1, 0, 0

; string literal
LIT_15: db 3, 0, 0, 3
            dw LIT_15_DATA, 0, 0
            db 0
LIT_15_DATA: db "EEE", 0

AFTER_LAST_VARIABLE:   equ VAR_STACK.POINTER + 11

VAR_DUMMY.START:       equ AFTER_LAST_VARIABLE    ; variable dummy circular queue area
VAR_DUMMY.COUNTER:     equ VAR_DUMMY.START        ; variable dummy circular queue count
VAR_DUMMY.POINTER:     equ VAR_DUMMY.COUNTER + 1  ; pointer to next variable dummy
VAR_DUMMY.DATA:        equ VAR_DUMMY.POINTER + 2  ; first variable dummy

VAR_DUMMY.SIZE:        equ 8
VAR_DUMMY.LENGTH:      equ (11 * VAR_DUMMY.SIZE)
VAR_DUMMY.END:         equ VAR_DUMMY.DATA + VAR_DUMMY.LENGTH
VAR_STACK.END:         equ VAR_DUMMY.END + 1

;--------------------------------------------------------
; DATA SIMBOLS
;--------------------------------------------------------

DATA_ITEMS:
DATA_ITEMS_COUNT:   equ 0

DATA_SET_ITEMS_START:
DATA_SET_ITEMS_COUNT:   equ 0


;---------------------------------------------------------------------------------------------------------
; PROGRAM BASIC ROM HOOKS
;---------------------------------------------------------------------------------------------------------

if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS

BASIC_SLOT_ENABLE:
    ld a, (BIOS_EXPTBL)
    ld hl,04000h
    __call_bios BIOS_ENASLT ; Select main ROM on page 1 (4000h~7FFFh)
    ret

PROGRAM_SLOT_1_ENABLE:
    call BIOS_RSLREG
	rrca
	rrca
	rrca
	rrca
	and	3	;Keep bits corresponding to the page
	ld	c,a
	ld	b,0
	ld	hl,BIOS_EXPTBL
	add	hl,bc
	ld	a,(hl)
	and	80h
	or	c
	ld	c,a
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	ld	a,(hl)
	and	0Ch
	or	c
	ld	h,040h
	jp BIOS_ENASLT	; Select the ROM on page 4000h-7FFFh

endif

if defined DO_DRAW

DRAW_HOOK:
   if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS
     ld de, PROGRAM_SLOT_1_ENABLE
     push de
   endif

   ld bc, 0
   push bc
   push bc
   push bc
   push hl                 ; address of string
   push af                 ; size of string

   ld de, 0x5D83
   ld (BIOS_MCLTAB),de
   xor a
   ld (BIOS_DRWFLG),a
   ld (BIOS_MCLFLG),a

   if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS
     call BASIC_SLOT_ENABLE
   endif

   jp BASIC_DRAW_DIRECT

endif

if defined SET_PLAY_VOICE_1 or defined SET_PLAY_VOICE_2 or defined SET_PLAY_VOICE_3 or defined DO_PLAY or defined MUSIC_PLAY or defined MUSIC_NEXT or defined MUSIC_STOP

PLAY_HOOK:
   if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS
     ld de, PROGRAM_SLOT_1_ENABLE
     push de
   endif

   ld HL, 0x752E
   ld (BIOS_MCLTAB),HL
   ld a, 1
   ld (BIOS_MCLFLG),A
   ld hl, BIOS_TEMP      ; voice count
   ld a, 3
   sub (hl)
   dec a
   ld (BIOS_PRSCNT), a
   xor a
   ld (BIOS_VOICEN), a
   ld (BIOS_QUEUEN), a
   ld (BIOS_MUSICF), a
   ld HL,-12 ; -10
   add HL,SP
   ld (BIOS_SAVSP),HL
   ld HL, BIOS_PLYCNT
   ld (HL), 0

   if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS
     call BASIC_SLOT_ENABLE
   endif

   jp BASIC_PLAY_DIRECT

endif

if defined gfxBorderFill

PAINT_HOOK:
   if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS
     ld de, PROGRAM_SLOT_1_ENABLE
     push de
   endif

   ld bc, (BIOS_GRPACX)
   ld (BIOS_GXPOS), bc
   ld de, (BIOS_GRPACY)
   ld (BIOS_GYPOS), de
   push bc
   push de

   xor a
   ld hl, BASIC_BUF
   ld (hl), a

   ld a, (BIOS_BDRATR)
   xor b
   ld e, a
   ld a, (BIOS_ATRBYT)
   xor d
   ld c, a

   push af
   push bc
   push hl
   push de

   call gfxIsScreenModeMSX2
   jr nc, PAINT_HOOK.1  ; if MSX2 and screen mode above 3, jump (BASIC_SUB_PAINT2)
     ld a, (BIOS_BDRATR)
     ld (BIOS_ATRBYT), a
     ld e, a
PAINT_HOOK.1:
   if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS
     call BASIC_SLOT_ENABLE
   endif

   pop de
   pop hl
   pop bc
   pop af

   jp BASIC_SUB_PAINT1

endif

;---------------------------------------------------------------------------------------------------------
; PROGRAM FOOTER
;---------------------------------------------------------------------------------------------------------

    if defined COMPILE_TO_ROM or defined COMPILE_TO_DOS

        romPad:

        pgmPage2.pad: equ romSize - (romPad - pgmArea)

        if pgmPage2.pad >= 0
           ds pgmPage2.pad, 0

           if pgmPage2.pad < lowLimitSize
                .WARNING "There's only less than 5% free space on this ROM"
           endif
        else
           .ERROR "There's no free space left on this ROM"
        endif

    endif

    end_file: end start_pgm           ; label start is the entry point