SHAOS.A 49.7 KB
Newer Older
Alexander Shabarshin's avatar
Alexander Shabarshin committed
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
\ *********************************************************
\ * SHAOS.A - OPERATING SYSTEM FOR MICROCOMPUTERS ON 8080 *
\ * WRITTEN BY A.A.SHABARHIN FROM JAN 1997 TO JUN 1998    *
\ * BASED ON OS DESIGN STARTED IN 1992 FOR RADIO-86RK     *
\ *********************************************************

\ ALPHA TEST FOR ZX-SPECTRUM 48K
\ COMPILED BY RASM (SEE SF.NET/PROJECTS/NEDOPC)

\ ROM-DISK SIMULATION:
\ C:\ --------> #0000
\ C:\SYSTEM --> #0800
\ C:\BATCH ---> #1000
\ RUN #D000

        ORG     #CC00

@SP_BEG EQU     #FE00
@VECTOR EQU     #CC00
@MEM1   EQU     #7000
@MEM2   EQU     #CC00
@BLO1   EQU     #E200
@CARD   EQU     #E000
@SIZE   EQU     256
@CMD    EQU     #CE00
@MCMD   EQU     32
@ENV    EQU     #CE20
@MEN    EQU     480
@GBIT   EQU     1
@MGX    EQU     256
@MGY    EQU     192
@MTX    EQU     32
@MTY    EQU     22
@SCREEN EQU     #E400
@FLAG_D EQU     #CDA0
@FLAG_V EQU     #CDA1
@FLAG_A EQU     #CDA2
@FLAG_S EQU     #CDA3
@FLAG_P EQU     #CDA4
@SYSTEM EQU     #0800
\@HLH    EQU     #CCEF
@UTIL   EQU     #E800

BEG:    JMP     RST_
ERR:    JMP     ERR_
MMR:    JMP     MMR_
MMF:    JMP     MMF_
MMA:    JMP     MMA_
\ INTERRUPTS ENABLED (#00-N,#FF-Y)
FEI     DB      0
VRS:    MVI_A,  0
        RET
SAV:    JMP     SAV_
VRF:    JMP     VRF_
LOA:    JMP     LOA_
TYP:    MVI_A,  #02
        RET
        DB      0
COM:    JMP     COM_
CON:    JMP     CON_
COG:    JMP     COG_
BEE:    JMP     BEE_
SOU:    JMP     SOU_
        DB      0
IRE:    JMP     IRE_
IRD:    JMP     IRD_
IRA:    JMP     IRA_
H3D:    JMP     H3D_
PRI:    JMP     PRI_
\ DEVICE INDEX
NUU     DB      0
H3R:    JMP     H3R_
H3W:    JMP     H3W_
JOY:    JMP     JOY_
KEY:    JMP     KEY_
CLS:    JMP     CLS_
\ HIGHEST ADDRESS FOR H003
NR2     DB      0
SCR:    JMP     SCR_
GCR:    JMP     GCR_
WRI:    JMP     WRI_
REA:    JMP     REA_
INP:    JMP     INP_
\ KEYBOARD TYPE
KBD     DB      0
WST:    JMP     WST_
HEX:    JMP     HEX_
CRR:    JMP     CRR_
S2L:    JMP     S2L_
SUM:    JMP     SUM_
        DB      0
FOP:    JMP     FOP_
FOA:    JMP     FOA_
FSY:    JMP     FSY_
FSH:    JMP     FSH_
FBM:    JMP     FBM_
\ LAST USED IDENTIFIER
FID     DB      2
SPX:    JMP     SPX_
GPX:    JMP     GPX_
MGR:    JMP     MGR_
FDS:    JMP     FDS_
FLL:    JMP     FLL_
        DB      0
BAT:    JMP     BAT_
PUX:    JMP     PUX_
GUX:    JMP     GUX_
BAS:    JMP     BAS_
OVL:    JMP     OVL_
\ IDENTIFIER OF LOADED OVERLAY
NOV     DB      0
CHD:    JMP     CHD_
LEN:    JMP     LEN_
CPY:    JMP     CPY_
CAT:    JMP     CAT_
FND:    JMP     FND_
        DB      0
CPR:    JMP     CPR_
NLN:    JMP     NLN_
FPD:    JMP     FOP_D
PCH:    JMP     PUTCH
ROL:    JMP     ROL_
        DB      0
D1H:    JMP     D1H_
D2H:    JMP     D2H_
D4H:    JMP     D4H_
D2S:    JMP     D2S_
D4S:    JMP     D4S_
        DB      0
EMU:    JMP     TEST_EM
MEM:    JMP     MEM_
MMC:    JMP     MMC_
ENV:    JMP     ENV_
GRB:    JMP     GRB_
        DB      0
XGR:    JMP     XGR_
XFI:    JMP     XFI_
XWI:    JMP     XWI_
XMA:    JMP     XMA_
XAD:    JMP     XAD_
HLH     DB      0
HLR:    JMP     HLR_
HLL:    JMP     HLL_
WHL:    JMP     WHL_
        JMP     RET_
        JMP     RET_
        DB      0

\ SYSTEM VARIABLES

HNDL0   DB      16 DUP(0)
HNDL1   DB      16 DUP(0)
HNDL2   DB      16 DUP(0)
HNDL3   DB      16 DUP(0)
HNDL4   DB      16 DUP(0)
HNDL5   DB      16 DUP(0)
HNDL6   DB      16 DUP(0)
HNDL7   DB      16 DUP(0)
ADRAM   DW      0
ACMD    DW      0
AENV    DW      0
ATBL    DW      0
NB      DB      0
GBIT    DB      1
MGX     DW      @MGX
MGY     DW      @MGY
MTX     DB      @MTX
MTY     DB      @MTY
AMEM    DW      0
MCOM    DB      0
NCOM    DB      0
MENV    DW      0
NNID    DW      0
NID     DW      0
        DW      0
STMP    DB      4 DUP(0)
FLAGS   DB      #FF,0,0,#FF,0
        DB      11 DUP(0)
FNBUF   DB      8 DUP(0)
FEX     DB      3 DUP(0)
FAT     DB      0
FA0     DB      0
FA1     DB      0
FA2     DB      0
FA3     DB      0
CDIR    DB      16 DUP(0)
DISK_A  DB      0
DISK_B  DB      0
DISK_C  DB      0
DISK_D  DB      0
DISK_E  DB      0
DISK_F  DB      0
DISK_G  DB      0
DISK_H  DB      0
        DB      8 DUP(0)
ND      DB      0
CT      DB      0
CC      DB      0
CR      DB      0
CS      DB      4
CB      DB      0
        DB      0
DISK    DB      0
CPTR    DB      4 DUP(0)
MEM1    DW      0
MEM2    DW      0
HDBUF   DB      8 DUP(0)
HDEXT   DB      0
HDLEN   DW      0
HDADR   DW      0
HDATR   DB      0
HDCRC   DW      0

\ COMMAND LINE AND ENVIRONMENT VARIABLES
        DB      512 DUP(0)

INI_:   JMP     INIT
COPYR   DB      "S004.0-ZX "
        DB      #7F
        DB      "1997 SHABARSHIN A",0
EXT     DB      "   "
        DB      "BAT"
        DB      "SYS"
        DB      "COM"
        DB      "EXE"
        DB      "OVL"
        DB      "BAS"
        DB      "LIB"
        DB      "RDL"
        DB      "PLG"
        DB      "ASM"
        DB      "BAK"
        DB      "CPP"
        DB      "DAT"
        DB      "TXT"
        DB      "$$$"
ERRMSG  DB      "ERROR",0
RWORD1  DB      "C:\"
RWORD2  DB      "SYSTEM"
RWORD3  DB      "BATCH"
RWORD4  DB      "AUTO",0
        DB      23 DUP(#FF)

\ ################################################################
\ #                                                              #
\ #                      $ZX  KERNEL.COM                         #
\ #                                                              #
\ ################################################################

\ COLD REBOOT

RST_:   LXI_SP, @SP_BEG
RST_1:  LDA     @FLAG_P
        CPI     #FF
        JNZ     RST_2
        MVI_A,  #9A
        OUT     #7F
        MVI_A,  #00
        OUT     #5F
        \ ZEROING DESCRIPTORS
RST_2:  MVI_C,  8
        MVI_A,  0
        LXI_H,  HNDL0
        LXI_D,  #0010
RST_3:  MOV_M,A
        DAD_D
        DCR_C
        JNZ     RST_3
        RET

\$ZX H003 DRIVER
\ INPUT:
\    B - DEVICE INDEX
\    C - REGISTER INDEX
\    A - DATA (FOR WRITE)
\ OUTPUT:
\    A - DATA (READ)
\    C - DATA FROM PORT "C"
\    FLAG-C - BUSY

\H3D_:   NOP
\        NOP
\        NOP
H3D_:   DI
        PUSH_D
        PUSH_H
        MOV_D,A
        LDA     FEI
        ORA_A
        JNZ     H3_1
        MVI_A,  0
        STA     ADRAM
        JMP     H3_2
H3_1:   MVI_A,  #FB
        STA     ADRAM
H3_2:   MVI_A,  #98
        OUT     #7F
        \ TRANSFER DEVICE INDEX TO REGISTER 7
		MOV_A,B
        OUT     #3F
        MVI_A,  #0E
        OUT     #5F
        INR_A
        OUT     #5F
        DCR_A
        OUT     #5F
        \ CHECK IF DEVICE IS BUSY
		IN      #5F
        RAL
        JC      H3_EE
        \ CHECK REGISTER INDEX
		MOV_A,C
        CPI     3
        JZ      H3_R
        CPI     5
        JZ      H3_R
        \ WRITE WITH STROBE
        MOV_A,D
        OUT     #3F
        MOV_A,C
        RLC
        OUT     #5F
        INR_A
        OUT     #5F
        DCR_A
        OUT     #5F
        JMP     H3_E
        \ READ WITHOUT STROBE
H3_R:   MVI_A,  #9A
        OUT     #7F
        MOV_A,C
        RLC
        OUT     #5F
        IN      #3F
        MOV_D,A
        MVI_A,  #98
        OUT     #7F
        MOV_A,D
H3_E:   IN      #5F
        MOV_C,A
        RAL
        MOV_A,D
H3_EE:  POP_H
        POP_D
        JMP     ADRAM

\ ENABLE INTERRUPTS

IRE_:   PUSH_PSW
        MVI_A,  #FF
        STA     FEI
        POP_PSW
        EI
        RET

\ DIABLE INTERRUPTS

IRD_:   DI
        PUSH_PSW
        MVI_A,  #00
        STA     FEI
        POP_PSW
        RET

\ CHECK IF INTERRUPTS ARE ENABLED
\  | CALL IRA
\  | JZ   YES

IRA_:   LDA     FEI
        CPI     #FF
        RET

\ ACCESS TO MEMORY MAP
\ INPUT:  HL - ADDRESS OF THE MEMORY
\ OUTPUT: HL - ADDRESS IN THE MAP
\         A - CODE FROM THE MAP

MEM_:   PUSH_D
        MOV_E,H
        MVI_D,  0
        LHLD    AMEM
        DAD_D
        MOV_A,M
        POP_D
        RET

\ MEMORY FUNCTIONS THROUGH MAP
\ INPUT:  HL - BEGINNING OF THE BLOCK
\         DE - LENGTH OF THE BLOCK
\         A - 0 FOR FREE, OTHERWISE PROCESS ID
\ OUTPUT: CHANGES IN MEMORY MAP

MMC_:   PUSH_PSW
        PUSH_B
        PUSH_D
        PUSH_H
        PUSH_H
        MOV_B,A
        CALL    MEM
        XTHL
        DAD_D
        DCX_H
        CALL    MEM
        XCHG
        POP_H
        \ HL - BEGINNING IN THE MAP
        \ DE - ENDING IN THE MAP
MMC_1:  MOV_M,B
        MOV_A,H
        CMP_D
        JNZ     MMC_2
        MOV_A,L
        CMP_E
        JZ      MMC_3
MMC_2:  INX_H
        JMP     MMC_1
MMC_3:  POP_H
        POP_D
        POP_B
        POP_PSW
        RET

\ CONVERT HL TO PARAGRAPHS (HL=HL/16)
\ INPUT:  HL - WORD
\         @HLH - HIGHER BYTE
\ OUTPUT: HL - COVERTED WORD

HLR_:   PUSH_PSW
        MOV_A,L
        ANI     #F0
        RRC
        RRC
        RRC
        RRC
        MOV_L,A
        MOV_A,H
        ANI     #0F
        RLC
        RLC
        RLC
        RLC
        ORA_L
        MOV_L,A
        MOV_A,H
        ANI     #F0
        RRC
        RRC
        RRC
        RRC
        MOV_H,A
        LDA     HLH
        ANI     #0F
        RLC
        RLC
        RLC
        RLC
        ORA_H
        MOV_H,A
        POP_PSW
        RET

\ CONVERT PARAGRAPHS TO HL (HL=HL*16)
\ INPUT:  HL - WORD
\ OUTPUT: HL - CONVERTED WORD
\         @HLH - HIGHER BYTE

HLL_:   PUSH_PSW
        MOV_A,H
        ANI     #F0
        RRC
        RRC
        RRC
        RRC
        STA     HLH
        MOV_A,H
        ANI     #0F
        RLC
        RLC
        RLC
        RLC
        MOV_H,A
        MOV_A,L
        ANI     #F0
        RRC
        RRC
        RRC
        RRC
        ORA_H
        MOV_H,A
        MOV_A,L
        ANI     #0F
        RLC
        RLC
        RLC
        RLC
        MOV_L,A
        POP_PSW
        RET

\ PRINT ERROR TO CURRENT POSITION
\ INPUT: A - ERROR ID

ERR_:   POP_D
        PUSH_PSW
        CALL    NLN
        LXI_H,  ERRMSG
        CALL    WST
        MVI_C,  #20
        MOV_A,C
        CALL    WRI
        POP_PSW
        ADI     #30
        CALL    WRI
        MOV_A,C
        CALL    WRI
        MVI_A,  #28
        CALL    WRI
        MOV_A,D
        CALL    HEX
        MOV_A,E
        CALL    HEX
        MVI_A,  #29
        CALL    WRI
        CALL    NLN
        CALL    NLN
        CALL    BEE

\        MVI_A,  #FF
\        CALL    KEY
\        JMP     RES

        JMP     CPR

\        PUSH_D
\        RET

\ ALLOCATE MEMORY BLOCK
\ INPUT:  DE - REQUIRED SIZE
\         A - PROCESS IDENTIFIER
\ OUTPUT: HL - ADDRESS OF THE BEGINNING OF THE BLOCK (0 IF FAILED)
\         FLAG_C - SUCCESS (JNC ERROR)
\ TODO: should it be UNSUCCESS and JC ERROR?

MMR_:   PUSH_B
        PUSH_D
        PUSH_PSW
        MOV_A,E
        ANA_A
        JZ      MMR_1
        INR_D
MMR_1:  MOV_B,D
        LHLD    AMEM
        XCHG
        LXI_H,  #FFFF
        CALL    MEM
MMR_2:  MOV_C,B
MMR_3:  MOV_A,M
        ANA_A
        JNZ     MMR_8
        DCR_C
        JZ      MMR_7
        MOV_A,H
        CMP_D
        JNZ     MMR_4
        MOV_A,L
        CMP_E
        JZ      MMR_9
MMR_4:  DCX_H
        JMP     MMR_3
MMR_8:  DCX_H
        JMP     MMR_2
        \ SUCCESS
MMR_7:  POP_PSW
        MOV_H,L
        MVI_L,  0
        POP_D
        CALL    MMC
        POP_B
        STC
        RET
        \ FAILURE
MMR_9:  LXI_H,  0
        POP_PSW
        STC
        CMC
        POP_D
        POP_B
        RET

\ FREE MEMORY BLOCK
\ INPUT:  HL - ADDRESS OF THE BEGINNING OF THE BLOCK
\ OUTPUT: A - PROCESS IDENTIFIER
\ TODO: what if process is trying to free wrong memory?
\ TODO: what if there are 2 blocks for the same process one after another?

MMF_:   PUSH_H
        CALL    MEM
        ANA_A
        JZ      MMF_9
        CPI     #FF
        JZ      MMF_9
MMF_1:  MVI_M,  0
        INX_H
        CMP_M
        JZ      MMF_1
MMF_9:  POP_H
        RET

\ CHECK IF MEMORY BLOCK IS FREE
\ INPUT:  HL - ADDRESS OF THE BEGINNING OF THE BLOCK
\         DE - BLOCK LENGTH
\ OUTPUT: FLAG_Z - IF OCCUPIED (JZ BUSY)

MMA_:   PUSH_D
        PUSH_H
        PUSH_H
        CALL    MEM
        XTHL
        DAD_D
        DCX_H
        CALL    MEM
        XCHG
        POP_H
        \ HL - BEGINNING IN THE MAP
        \ DE - ENDING IN THE MAP
MMA_1:  MOV_A,M
        ANA_A
        JNZ     MMA_9
        MOV_A,H
        CMP_D
        JNZ     MMA_2
        MOV_A,L
        CMP_E
        JZ      MMA_3
MMA_2:  INX_H
        JMP     MMA_1
MMA_3:  XRA_A
MMA_9:  POP_H
        POP_D
        RET

SAV_:
VRF_:
LOA_:

\$ZX BEEPER

BEE_:   PUSH_H
        PUSH_D
        PUSH_B
        PUSH_PSW
        LXI_H,  407
        LXI_D,  200
        CALL    #03B5
        POP_PSW
        POP_B
        POP_D
        POP_H
        RET

SOU_:


\$ZX READ JOYSTICK
\ OUTPUT: A - FIVE LOWER BITS

JOY_:   IN      #1F
        ANI     #1F
        RET

\$ZX READ KEYBOARD
\ INPUT:
\   A - #00-NO WAIT, #FF-WAIT
\ OUTPUT:
\   A - #00-NO KEY PRESSED, OTHERWISE ASCII CODE

KEY_:   PUSH_B
        PUSH_D
        PUSH_H
        PUSH_PSW
KEY_0:  MVI_A,  #FE
        IN      #FE
        ANI     1
        MOV_B,A
        MVI_A,  #7F
        IN      #FE
        ANI     2
        MOV_C,A
        ORA_B
        JNZ     KEY_1
        \ PRESSED CS AND SS
        MVI_D,  #FE
        JMP     KEY_E
KEY_1:  MOV_A,B
        ORA_A
        JNZ     KEY_2
        LXI_H,  KTAB_CS
        JMP     KEY_4
KEY_2:  MOV_A,C
        ORA_A
        JNZ     KEY_3
        LXI_H,  KTAB_SS
        JMP     KEY_4
KEY_3:  LXI_H,  KTAB
KEY_4:  LXI_B,  #0005
        MVI_E,  #FF
KEY0:   MVI_A,  #FE
        MOV_D,A
        IN      #FE
        CALL    KEY_F0
        CMP_E
        JNZ     KEY_10
        DAD_B
KEY1:   MVI_A,  #FD
        MOV_D,A
        IN      #FE
        CALL    KEY_F0
        CMP_E
        JNZ     KEY_10
        DAD_B
KEY2:   MVI_A,  #FB
        MOV_D,A
        IN      #FE
        CALL    KEY_F0
        CMP_E
        JNZ     KEY_10
        DAD_B
KEY3:   MVI_A,  #F7
        MOV_D,A
        IN      #FE
        CALL    KEY_F0
        CMP_E
        JNZ     KEY_10
        DAD_B
KEY4:   MVI_A,  #EF
        MOV_D,A
        IN      #FE
        CALL    KEY_F0
        CMP_E
        JNZ     KEY_10
        DAD_B
KEY5:   MVI_A,  #DF
        MOV_D,A
        IN      #FE
        CALL    KEY_F0
        CMP_E
        JNZ     KEY_10
        DAD_B
KEY6:   MVI_A,  #BF
        MOV_D,A
        IN      #FE
        CALL    KEY_F0
        CMP_E
        JNZ     KEY_10
        DAD_B
KEY7:   MVI_A,  #7F
        MOV_D,A
        IN      #FE
        CALL    KEY_F0
        CMP_E
        JNZ     KEY_10
        \ NO KEYPRESSES
        POP_PSW
        PUSH_PSW
        CPI     #FF
        JZ      KEY_0
        MVI_D,  0
        JMP     KEY_E
        \ KEYPRESSED
KEY_10: MOV_C,A
        MVI_B,  0
        DAD_B
        MOV_D,M
        POP_PSW
        PUSH_PSW
        CPI     #FF
        JNZ     KEY_E
KEY_11: MVI_A,  0
        CALL    KEY
        ORA_A
        JNZ     KEY_11
KEY_E:  POP_PSW
        MOV_A,D
        POP_H
        POP_D
        POP_B
        RET

\$ZX FIND FIRST ZERO IN A
\ INPUT:  A - BYTE TO TEST
\         D - ADDRESS OF KEYS
\ OUTPUT: A - FIRST ZERO

KEY_F0: PUSH_D
        PUSH_B
        MOV_B,D
        MVI_E,  0
        MOV_D,A
KEY_F1: MOV_A,D
        ANI     1
        JZ      KEY_FF
KEY_F2: MOV_A,D
        RRC
        MOV_D,A
        INR_E
        MVI_A,  5
        CMP_E
        JNZ     KEY_F1
        MVI_E,  #FF
KEY_FF: MOV_A,B
        CPI     #FE
        JNZ     KEY_F3
        MOV_A,E
        CPI     0
        JZ      KEY_F2
KEY_F3: CPI     #7F
        JNZ     KEY_F4
        MOV_A,E
        CPI     1
        JZ      KEY_F2
KEY_F4: MOV_A,E
        POP_B
        POP_D
        RET

\ $ZX KEYBOARD

KTAB    DB      0,"ZXCV"
        DB      "ASDFG"
        DB      "QWERT"
        DB      "12345"
        DB      "09876"
        DB      "POIUY"
        DB      #0D,"LKJH"
        DB      #20,0,"MNB"
KTAB_CS DB      0,"zxcv"
        DB      "asdfg"
        DB      "qwert"
        DB      #F1,#F2,#F3,#F4,#FB
        DB      #08,#F0,#FA,#F8,#F9
        DB      "poiuy"
        DB      #0E,"lkjh"
        DB      #FF,#FE,"mnb"
KTAB_SS DB      #FE,#3A,#60,#3F,#2F
        DB      #7E,#7C,#5C,#7B,#7D
        DB      #09,#FC,#FD,#3C,#3E
        DB      #21,#40,#23,#24,#25
        DB      #5F,#29,#28,#27,#26
        DB      #22,#3B,#7F,#5D,#5B
        DB      #0F,#3D,#2B,#2D,#5E
        DB      #F7,#00,#2E,#2C,#2A

\$ZX PUT CHARACTER TO THE SCREEN
\ INPUT: A - ASCII CODE

PUTCH:  PUSH_H
        PUSH_D
        PUSH_B
        PUSH_PSW
        MVI_A,  2
        CALL    #1601
        \ AT ROW,COL
        MVI_A,  #16
        RST_2
        LDA     CR
        RST_2
        LDA     CC
        RST_2
        \ INK COLOR
        MVI_A,  #10
        RST_2
        LDA     CS
        RST_2
        \ PAPER COLOR
        MVI_A,  #11
        RST_2
        LDA     CB
        RST_2
        CALL    GETSCR
        POP_PSW
        PUSH_PSW
        MOV_M,A
        RST_2
        POP_PSW
        POP_B
        POP_D
        POP_H
        RET

\$ZX READ CHARACTER FROM CURRENT POSITION
\ OUTPUT:
\   A - ASCII CODE
\   D - INK COLOR
\   E - PAPER COLOR
\ TODO: make it more logical

REA_:   PUSH_H
        CALL    GETSCR
        MOV_A,M
        PUSH_PSW
        LXI_D,  #8F00
        DAD_D
        MOV_A,M
        ADI     #07
        MOV_D,A
        MOV_A,M
        ADI     #38
        MOV_E,A
        POP_PSW
        POP_H
        RET

\$ZX RETURN CURRENT CHARACTER ADDRESS

GETSCR: PUSH_PSW
        PUSH_D
        LDA     CC
        MOV_L,A
        LDA     CR
        ANI     #18
        RRC
        RRC
        RRC
        MOV_H,A
        LDA     CR
        ANI     #07
        RLC
        RLC
        RLC
        RLC
        RLC
        ORA_L
        MOV_L,A
        LXI_D,  @SCREEN
        DAD_D
        POP_D
        POP_PSW
        RET

SPX_:
GPX_:

\$ZX SCREEN SCROLL
\ TODO: make it faster

ROL_:   PUSH_PSW
        PUSH_H
        PUSH_D
        LDA     CC
        MOV_D,A
        LDA     CR
        MOV_E,A
        PUSH_D
        MVI_A,  0
        STA     CC
        STA     CR
        LXI_D,  @MTX
        LDA     MTY
        MOV_C,A
        DCR_C
ROL_0:  LDA     MTX
        MOV_B,A
ROL_1:  CALL    GETSCR
        DAD_D
        MOV_A,M
        CALL    WRI
        DCR_B
        JNZ     ROL_1
        DCR_C
        JNZ     ROL_0
        LDA     MTX
        MOV_C,A
        DCR_C
ROL_3:  MVI_A,  #20
        CALL    WRI
        DCR_C
        JNZ     ROL_3
        CALL    PCH
        POP_D
        MOV_A,E
        CPI     0
        JZ      ROL_2
        DCR_A
ROL_2:  STA     CR
        MOV_A,D
        STA     CC
        POP_D
        POP_H
        POP_PSW
        RET

\$ZX INITIALIZATION

INIT:   LXI_SP, @SP_BEG
        CALL    RST_1
        \ SET ADRAM
        LXI_H,  ADRAM
        MVI_M,  #00
        INX_H
        MVI_M,  #C9
        \ COPY "VECTOR.DAT"
        LDA     @FLAG_V
        CPI     #FF
        JNZ     INIT01
        MVI_B,  0
        MVI_C,  2
        MVI_A,  #20
\        CALL    @H003
        MVI_C,  1
        MVI_A,  #08
\        CALL    @H003
        MVI_D,  #10
        LXI_H,  @VECTOR
INIT02: MOV_A,D
        MVI_C,  0
\        CALL    @H003
        MVI_C,  4
\        CALL    @H003
        MOV_M,A
        INX_H
        INR_D
        MOV_A,D
        CPI     #FF
        JNZ     INIT02
        \ MEMORY INITIALIZATION
INIT01: LXI_H,  @MEM1
        SHLD    MEM1
        LXI_H,  @MEM2
        SHLD    MEM2
        LXI_H,  @BLO1
        SHLD    ATBL
        LXI_H,  @CARD
        SHLD    AMEM
        LXI_D,  @SIZE
        MVI_A,  0
        CALL    FLL
        LXI_H,  0
        SHLD    NNID
        SHLD    NID
        MOV_A,H
        STA     NB
        \ MEMORY MAP CREATION
        XCHG
        LHLD    MEM1
        XCHG
        MVI_A,  #FF
        CALL    MMC
        LHLD    MEM2
        XCHG
        LXI_H,  0
        MOV_A,L
        SUB_E
        MOV_L,A
        MOV_A,H
        SBB_D
        MOV_H,A
        XCHG
        MVI_A,  #FF
        CALL    MMC
        \ SET CURSOR
        MVI_A,  1
        STA     CT
        MVI_A,  0
        STA     CC
        STA     CR
        STA     CB
        MVI_A,  4
        STA     CS
        \ SET MAXIMAL RESOLUTION
        MVI_A,  @GBIT
        STA     GBIT
        LXI_H,  @MGX
        SHLD    MGX
        LXI_H,  @MGY
        SHLD    MGY
        MVI_A,  @MTX
        STA     MTX
        MVI_A,  @MTY
        STA     MTY
        \ SET COMMAND LINE ADDRESS
        LXI_H,  @CMD
        SHLD    ACMD
        MVI_A,  @MCMD
        STA     MCOM
        MVI_A,  0
        STA     NCOM
        \ SET ENVIRONMENT VARIABLES
        LXI_H,  @ENV
        SHLD    AENV
        LXI_H,  @MEN
        SHLD    MENV
        LHLD    AENV
        MVI_M,  0
        INX_H
        MVI_M,  0
        \ INITIALIZE SEARCH BUFFER
        MVI_A,  0
        STA     FNBUF
        \ SET FONT KBD
        MVI_A,  0
        STA     KBD
        \ INITIALIZE DISC SUBSYSTEM
        LDA     @FLAG_D
        CPI     #FF
        JNZ     INIT10
        \ SET DISKS
        MVI_C,  8
        LXI_H,  DISK_A
        MVI_B,  0
INIT03: MOV_M,B
        INR_B
        INX_H
        DCR_C
        JNZ     INIT03
        \ DISC A - RAM
        MVI_A,  #A0
        STA     DISK_A
        \ DISC C - ROM
        MVI_A,  #92
        STA     DISK_C
        \ NUMBER OF DISCS
        MVI_A,  2
        STA     ND
        \ SET DISC C: AS CURRENT
        LXI_H,  CDIR
        MVI_M,  'C'
        INX_H
        MVI_M,  #3A
        INX_H
        MVI_M,  '\'
        INX_H
        MVI_A,  0
        MOV_M,A
        MOV_C,A
        LXI_H,  CPTR
        MOV_M,C
        INX_H
        MOV_M,C
        INX_H
        MVI_M,  #20
        INX_H
        MOV_M,C
        MVI_A,  #02
        STA     DISK
        \ ENABLE INTERRUPTS
INIT10: CALL    IRE
        \ RUN "AUTO.BAT"
        LDA     @FLAG_A
        CPI     #FF
        JNZ     INIT04
        LXI_H,  RWORD4
        \ BUT IF <<AUTO.BAT>> NOT FOUND IT WILL BE ERROR !!!
        CALL    BAT
        \ CLEAN THE SCREEN AND PRINT THE STRING
INIT04: LDA     @FLAG_S
        CPI     #FF
        JNZ     INIT06
        MVI_A,  1
        MVI_B,  0
        MOV_C,B
        MOV_E,B
        MVI_D,  4
        CALL    SCR
        CALL    CLS
        LXI_H,  COPYR
        CALL    WST
        CALL    NLN
        CALL    NLN
        DB      0,0
INIT06: CALL    CPR


\ ################################################################
\ #                                                              #
\ #                           STD.OVL                            #
\ #                                                              #
\ ################################################################


\ PACKET READING FROM H003
\ INPUT:
\    A - DISC NAME
\    HL - MEMORY BUFFER
\    DE - ADDRESS OF BEGINNING
\    BC - LENGTH
\  IF A=#FF THEN IT'S FOR DIFFERENT DEVICE
\    @NUU - DEVICE IDENTIFIER
\    @NR2 - HIGHER ADDRESS
\ OUTPUT: BUFFER COPIED

H3R_:   DI
        PUSH_B
        PUSH_D
        PUSH_H
        PUSH_PSW
        CPI     #FF
        JZ      H3R_F
        \ DISCS
        PUSH_B
        CALL    FPD
        MOV_A,C
        POP_B
        RLC
        RLC
        RLC
        RLC
        STA     NR2
        MVI_A,  1
        STA     NUU
        \ DEVICES
H3R_F:  PUSH_B
        LDA     NUU
        MOV_B,A
        MVI_C,  2
        LDA     NR2
        CALL    H3D
        POP_B
H3R_0:  PUSH_B
        LDA     NUU
        MOV_B,A
        MVI_C,  1
        MOV_A,D
        CALL    H3D
        MVI_C,  0
        MOV_A,E
        CALL    H3D
        MVI_C,  4
        CALL    H3D
        MOV_M,A
        INX_H
        INX_D
        POP_B
        DCX_B
        MOV_A,B
        ORA_C
        JNZ     H3R_0
        CALL    IRA
        JNZ     H3R_E
        EI
H3R_E:  POP_PSW
        POP_H
        POP_D
        POP_B
        RET

\ PACKET WRITING TO H003
\ INPUT:
\    A - DISC NAME
\    HL - MEMORY BUFFER
\    DE - ADDRESS OF BEGINNING
\    BC - LENGTH
\  IF A=#FF THEN IT'S FOR DIFFERENT DEVICE
\    @NUU - DEVICE IDENTIFIER
\    @NR2 - HIGHER ADDRESS
\ OUTPUT: BUFFER COPIED

H3W_:   DI
        PUSH_B
        PUSH_D
        PUSH_H
        PUSH_PSW
        CPI     #FF
        JZ      H3W_F
        \ DISCS
        PUSH_B
        CALL    FPD
        MOV_A,C
        POP_B
        RLC
        RLC
        RLC
        RLC
        STA     NR2
        MVI_A,  1
        STA     NUU
        \ DEVICES
H3W_F:  PUSH_B
        LDA     NUU
        MOV_B,A
        MVI_C,  2
        LDA     NR2
        CALL    H3D
        POP_B
H3W_0:  PUSH_B
        LDA     NUU
        MOV_B,A
        MVI_C,  1
        MOV_A,D
        CALL    H3D
        MVI_C,  0
        MOV_A,E
        CALL    H3D
        MVI_C,  3
        MOV_A,M
        CALL    H3D
        INX_H
        INX_D
        POP_B
        DCX_B
        MOV_A,B
        ORA_C
        JNZ     H3W_0
        CALL    IRA
        JNZ     H3W_E
        EI
H3W_E:  POP_PSW
        POP_H
        POP_D
        POP_B
        RET


\ EXECUTE STRING AS COMMAND
\ INPUT: HL - ADDRESS OF THE STRING

COM_:   MOV_A,M
        \ CHECK FOR EMPTY STRING
        CPI     0
        JZ      COM_E
        \ CHECK FOR TEST
        CPI     '#'
        JNZ     COM_0
        CALL    TEST_EM
        JMP     COM_E
        \ CHECK FOR OUTPUT FOR EMULATOR
COM_0:  CPI     '^'
        JNZ     COM_1
        HLT
        \ CHECK FOR OUTPUT FOR REAL ZX
COM_1:  CPI     '%'
        JNZ     COM_2
        POP_H
        POP_H
        JMP     COM_E
        \ CHECK FOR DISC CHANGE
COM_2:  INX_H
        MOV_A,M
        CPI     #3A
        JNZ     COM_2_
        DCX_H
        MOV_D,M
        \ D - NEW DISK
        LDA     CDIR
        CMP_D
        JZ      COM_E
        MOV_A,D
        CALL    FPD
        XRA_A
        LXI_H,  CPTR
        MOV_M,A
        INX_H
        MOV_M,A
        INX_H
        MOV_M,C
        LXI_H,  CDIR
        MOV_M,D
        INX_H
        MVI_M,  #3A
        INX_H
        MVI_M,  #5C
        INX_H
        MVI_M,  0
        JMP     COM_E
COM_2_: DCX_H
        XCHG
        LHLD    ACMD
        MVI_A,  0
        CALL    CPY
        \ A = 0
        CALL    COG
        LXI_H,  FNBUF
        PUSH_H
        MVI_B,  #20
COM_3:  XCHG
        MOV_A,M
        XCHG
        ORA_A
        JZ      COM_4
        CMP_B
        JZ      COM_4
        MOV_M,A
        INX_H
        INX_D
        JMP     COM_3
COM_4:  MVI_M,  0
        POP_H
        PUSH_H
        \ SEARCH IN CURRENT FOLDER
COM_4_: MVI_A,  #11
        CALL    FOP
        JNC     COM_40
        MOV_D,A
        \ CHECK FOR .COM
COM_41: LDA     HDEXT
        CPI     3
        JZ      COM_5
        XRA_A
        CALL    FOP
        JC      COM_41
        \ SEARCH IN C:\SYSTEM
COM_40: POP_H
        PUSH_H
        CALL    FSY
        JNC     COM_ER
        MOV_D,A
COM_5:  POP_H
        LXI_B,  #0010
        LHLD    FA0
        DAD_B
        MOV_A,D
        XCHG
        LHLD    HDLEN
        CALL    HLL
        MOV_B,H
        MOV_C,L
        LHLD    HDADR
        CALL    H3R
        PCHL
COM_ER: MVI_A,  5
        CALL    ERR
COM_E:  RET

\ COUNT WORDS IN STRING
\ INPUT:
\    HL - ADDRESS OF THE STRING
\ OUTPUT:
\    A - NUMBER OF WORDS

CON_:   PUSH_H
        PUSH_B
        MVI_C,  0
        MVI_B,  #20
CON_1:  MOV_A,M
        ORA_A
        JZ      CON_E
        CMP_B
        JNZ     CON_NS
        INX_H
        JMP     CON_1
CON_NS: INR_C
CON_2:  MOV_A,M
        ORA_A
        JZ      CON_E
        CMP_B
        JZ      CON_1
        INX_H
        MOV_A,H
        ORA_L
        JNZ     CON_2
CON_E:  MOV_A,C
        POP_B
        POP_H
        RET

\ RETURN POINTER TO NTH WORD IN THE STRING
\ INPUT:
\     HL - ADDRESS OF THE STRING
\     A - WORD INDEX (0 FOR 1ST WORD)
\ OUTPUT:
\     DE - ADDRESS OF THE NTH WORD

COG_:   PUSH_PSW
        PUSH_H
        PUSH_B
        MOV_C,A
        MVI_B,  #20
COG_1:  MOV_A,M
        CMP_B
        JNZ     COG_NS
        INX_H
        JMP     COG_1
COG_NS: MOV_A,C
        ORA_A
        JZ      COG_E
        DCR_C
COG_2:  MOV_A,M
        CMP_B
        JZ      COG_1
        INX_H
        MOV_A,H
        ORA_L
        JZ      COG_E
        JMP     COG_2
COG_E:  MOV_D,H
        MOV_E,L
        POP_B
        POP_H
        POP_PSW
        RET


\ SEND CHARACTER TO PRINTER
\ INPUT:  A - ASCII CODE TO PRINT
\ OUTPUT: FLAG-C - ERROR

PRI_:   PUSH_B
        MVI_B,  0
        MVI_C,  4
        CALL    H3D
        POP_B
        RET


\ CLEAN THE SCREEN

CLS_:   PUSH_PSW
        PUSH_H
        MVI_A,  0
        STA     CC
        STA     CR
        LDA     MTY
        MOV_L,A
CLS_0:  LDA     MTX
        MOV_H,A
        MOV_A,L
        CPI     1
        JNZ     CLS_1
        DCR_H
CLS_1:  MVI_A,  #20
        CALL    WRI
        DCR_H
        JNZ     CLS_1
        DCR_L
        JNZ     CLS_0
        MVI_A,  #20
        CALL    PCH
        MVI_A,  0
        STA     CC
        STA     CR
        POP_H
        POP_PSW
        RET

\ SET CURSOR ATTRIBUTES
\ INPUT:
\    A - VIEW (0-INVISIBLE 1-UNDERSCRIBED 2-RECTANGULAR)
\    B - COLUMN (X)
\    C - ROW (Y)
\    D - INK COLOR
\    E - PAPER COLOR

SCR_:   PUSH_H
        STA     CT
        MOV_A,B
        STA     CC
        MOV_A,C
        STA     CR
        MOV_A,D
        STA     CS
        MOV_A,E
        STA     CB
        POP_H
        RET

\ GET CURSOR ATTRIBUTES
\ OUTPUT:
\    A - VIEW (0-INVISIBKE 1-UNDERSCRIBED 2-RECTANGULAR)
\    B - COLUMN (X)
\    C - ROW  (Y)
\    D - INK COLOR
\    E - PAPER COLOR

GCR_:   PUSH_H
        LDA     CC
        MOV_B,A
        LDA     CR
        MOV_C,A
        LDA     CS
        MOV_D,A
        LDA     CB
        MOV_E,A
        LDA     CT
        POP_H
        RET

\ PRINT CHARACTER TO CURRENT POSITION AND MOVE CURSOR
\ INPUT: A - ASCII CODE

WRI_:   CALL    PCH
        CALL    CRR
        RET


\ INPUT STRING EDITOR
\ INPUT:  HL - EDITED STRING

INP_:   PUSH_PSW
        PUSH_B
        PUSH_D
        PUSH_H
        LXI_D,  0
        ORA_A
        JNZ     INP_0
        MVI_C,  16
        MVI_B,  '*'
        JMP     INP_00
INP_0:  MOV_C,A
        MVI_B,  0
INP_00: PUSH_H
INP_01: DAD_D
        MOV_A,M
        ORA_A
        JZ      INP_E0
        INX_D
        POP_H
        PUSH_H
        JMP     INP_01
INP_E0: MVI_M,  #20
        INX_H
        MVI_M,  #00
        POP_H
        INR_E
        MOV_A,E
        CMP_C
        JC      INP_02
        JMP     INP_03
INP_02: MOV_A,D
        ORA_A
        JZ      INP_11
        MVI_D,  #00
INP_03: MOV_E,C
        DCR_E
INP_11: JMP     INP_RR
INP_1:  MVI_A,  #FF
        CALL    KEY
        \ ARROW RIGHT
        CPI     #FA
        JNZ     INP_2
        INR_D
        MOV_A,D
        CMP_E
        JC      INP_RR
\        CMP_C
\        JNZ     INP_RR
        DCR_D
        JMP     INP_1
        \ ARROW LEFT
INP_2:  CPI     #FB
        JNZ     INP_3
        MOV_A,D
        ORA_A
        JZ      INP_1
        DCR_D
        JMP     INP_RR
        \ CLEAN
INP_3:  CPI     #08
        JNZ     INP_4
        MOV_A,D
        ORA_A
        JZ      INP_1
        DCR_E
        DCR_D
        MOV_A,D
        PUSH_H
        PUSH_D
        MOV_E,D
        MVI_D,  0
        DAD_D
INP_31: INX_H
        MOV_A,M
        DCX_H
        MOV_M,A
        INX_H
        DCR_E
        CMP_C
        JNZ     INP_31
        POP_D
        POP_H
        JMP     INP_RR
        \ ENTER
INP_4:  CPI     #0D
        JZ      INP_EN
        \ CHARACTER
        CPI     #20
        JC      INP_1
        CPI     #80
        JNC     INP_1
        PUSH_PSW
        INR_E
        MOV_A,E
        CMP_C
        JNZ     INP_5
        DCR_E
        POP_PSW
        JMP     INP_1
INP_5:  POP_PSW
        PUSH_H
        PUSH_D
        PUSH_PSW
        MOV_A,D
        MVI_D,  0
        MOV_E,C
        DCR_E
        DCR_E
        DAD_D
        MOV_D,A
INP_6:  MOV_A,M
        INX_H
        MOV_M,A
        DCX_H
        DCX_H
        MOV_A,E
        DCR_E
        CMP_D
        JNZ     INP_6
        POP_PSW
        INX_H
        MOV_M,A
        POP_D
        POP_H
        INR_D
INP_RR: CALL    INP_RE
        JMP     INP_1
INP_EN: MVI_D,  #FF
        CALL    INP_RE
        MVI_D,  0
        DAD_D
        DCX_H
        MVI_M,  0
        POP_H
        POP_D
        POP_B
        POP_PSW
        RET

        \ REDRAW
INP_RE: PUSH_H
        LDA     CC
        PUSH_PSW
        LDA     CR
        PUSH_PSW
        MVI_A,  0
INP_R1: CMP_D
        JNZ     INP_R2
        PUSH_PSW
        LDA     CS
        PUSH_PSW
        LDA     CB
        STA     CS
        POP_PSW
        STA     CB
        MOV_A,B
        ORA_A
        JNZ     INP_P1
        MOV_A,M
        JMP     INP_W1
INP_P1: MOV_A,B
INP_W1: CALL    WRI
        LDA     CS
        PUSH_PSW
        LDA     CB
        STA     CS
        POP_PSW
        STA     CB
        POP_PSW
        JMP     INP_R3
INP_R2: PUSH_PSW
        MOV_A,B
        ORA_A
        JNZ     INP_P2
        MOV_A,M
        JMP     INP_W2
INP_P2: MOV_A,B
INP_W2: CALL    WRI
        POP_PSW
INP_R3: INX_H
        INR_A
        CMP_E
        JNZ     INP_R1
        MVI_A,  #20
        CALL    WRI
        POP_PSW
        STA     CR
        POP_PSW
        STA     CC
        POP_H
        RET

\ PRINT STRING TO CURRENT POSITION

WST_:   PUSH_PSW
WST_1:  MOV_A,M
        CPI     0
        JZ      WST_0
        CPI     #0A
        JNZ     WST_3
        CALL    NLN
        JMP     WST_2
WST_3:  CALL    WRI
WST_2:  INX_H
        JMP     WST_1
WST_0:  POP_PSW
        RET

\ PRINT BYTE IN HEXADECIMAL FORM
\ INPUT: A - BYTE

HEX_:   PUSH_PSW
        PUSH_D
        CALL    D1H
        MOV_A,D
        CALL    WRI
        MOV_A,E
        CALL    WRI
        POP_D
        POP_PSW
        RET

\ MOVE CURSOR TO THE RIGHT

CRR_:   PUSH_PSW
        PUSH_H
        LDA     MTX
        MOV_H,A
        LDA     CC
        INR_A
        CMP_H
        JZ      CRR_EQ
        STA     CC
        JMP     CRR_EN
CRR_EQ: CALL    NLN
CRR_EN: POP_H
        POP_PSW
        RET

S2L_:
SUM_:

\ FILE OPERATIONS
\ INPUT:
\    A - FUNCTION
\    ...
\ OUTPUT:
\    ...

FOP_:   CPI    #00
        JNZ    FOP_01
        \ REPEAT SEARCH
        LDA    FA3
        CALL   FPD
        CPI    3
        JC     FOP_00
        XRA_A
        JMP    FOA
FOP_00: ORA_A
        JZ     FOP000
        MVI_H, 0
        JMP    FOP_FN
FOP_01: CPI    #01
        JNZ    FOP_1
        \ FORMAT DISC D (A,B,...)
        MOV_A,D
        CALL   FPD
        CPI    3
        JC     FOP_02
        MVI_A, #01
        JMP    FOA
FOP_02: ORA_A
        JZ     FOP001

        JMP    FOP_ER

        RET
FOP_1:  CPI    #20
        JNC    FOP_2
        \ CURRENT DISC (1*)
        PUSH_PSW
        LDA    CDIR
        CALL   FPD
        CPI    3
        JC     FOP_10
        POP_PSW
        JMP    FOA
FOP01_: POP_PSW
        JMP    FOP010
FOP_10: ORA_A
        JZ     FOP01_
        POP_PSW
        CPI    #10
        JNZ    FOP_11


        RET
FOP_11: CPI    #11
        JNZ    FOP_12
        \ SEARCH IN CURRENT DIRECTORY
FOP_FN: CALL   FNF_
FOP_CC: CPI    #FF
        STC
        JNZ    FO11_1
        CMC
FO11_1: RET
FOP_12:

        RET
FOP_2:  CPI    #30
        JNC    FOP_3
        \ BY DESCRIPTOR D (2*)
        PUSH_PSW
        PUSH_D
        PUSH_H
        LXI_H, HNDL0
        MOV_A,D
        RLC
        RLC
        RLC
        RLC
        MOV_E,A
        MVI_D, 0
        DAD_D
        MOV_A,M
        ORA_A
        JZ     FOP_ER
        MVI_E, 5
        DAD_D
        MOV_A,M
        POP_H
        POP_D
        CALL   FPD
        CPI    3
        JC     FOP_20
        POP_PSW
        JMP    FOA
FOP02_: POP_PSW
        JMP    FOP020
FOP_20: ORA_A
        JZ     FOP02_
        POP_PSW

        RET
FOP_3:  CPI    #40
        JNC    FOP_ER
        \ BY PATH HL (3*)
        PUSH_PSW
        MOV_A,M
        CALL   FPD
        CPI    3
        JC     FOP_30
        POP_PSW
        JMP    FOA
FOP_30: ORA_A
        JZ     FOP_E2
        POP_PSW

        RET
FOP_E2: POP_PSW
FOP_ER: MVI_A, 7
        CALL   ERR
        RET

\ FROM DISC MAME TO A->B,C (FPD)

FOP_D:  PUSH_D
        PUSH_H
        SUI    'A'
        MOV_E,A
        MVI_D, #00
        LXI_H, DISK_A
        DAD_D
        MOV_A,M
        RAL
        JC     FOP_D1
        MVI_A, 7
        CALL   ERR
        RET
FOP_D1: ANI    #E0
        RLC
        RLC
        RLC
        MOV_B,A
        MOV_A,M
        ANI    #0F
        MOV_C,A
        MOV_A,B
        POP_H
        POP_D
        RET

\ MAIN ROM
FOP010:

FOP020:

FOP000:

FOP001:


\ ALTERNATE FILE OPERATIONS
\ INPUT:
\    A - FUNCTION
\    B - DISC IDENTIFIER
\    C - ADDITIONAL IDENTIFIER
\    ...
\ OUTPUT:
\    ...

FOA_:  MVI_A,  7
       CALL    ERR
       RET

\ FIND FILE IN C:\SYSTEM
\ INPUT:
\    HL - ADDRESS OF THE FILENAME
\ OUTPUT:
\    A - DISC NAME
\    HL - ADDRESS IN PARAGRAPHS
\    FLAG-C - SUCCESS

FSY_:   MOV_A,H
        ORA_A
        JZ      FOP_FN
        PUSH_D
        XCHG
        LXI_H,  FNBUF
        XRA_A
        CALL    CPY
        LXI_D,  @SYSTEM
        MVI_A,  'C'
        STA     FA3
        CALL    FNF_1
        POP_D
        JMP     FOP_CC

\ FIND FILE IN CURRENT FOLDER
\ INPUT:
\    HL - ADDRESS OF THE STRING (IF 0 THEN REPEAT)
\ OUTPUT:
\    A - DISC (255 IF NOT FOUND)
\    HL - ADDRESS IN PARAGRAPHS

FNF_:   MOV_A,H
        ORA_A
        JZ      FNF_NX
        MVI_A,  0
        XCHG
        LXI_H,  FNBUF
        CALL    CPY
        LHLD    CPTR
        XCHG
        LDA     CDIR
        STA     FA3
        \ @FA3 - DISC NAME
        \ @FNBUF - WILDCARD FOR SEARCH
        \ DE - CURRENT POINTER TO THE FILE
FNF_1:  LXI_H,  HDBUF
        LDA     FA3
        LXI_B,  #0010
        CALL    H3R
        MOV_A,M
        CPI     #EE
        JZ      FNF_OT
        CPI     #FF
        JZ      FNF_EN
        PUSH_D
        LXI_D,  FNBUF
        CALL    FSH
        JZ      FNF_EE
        \ FILE IS NOT GOOD
FNF_2:  LHLD    HDLEN
        INX_H
        CALL    HLL
        MOV_B,H
        MOV_C,L
        POP_D
        XCHG
        DAD_B
        XCHG
        JMP     FNF_1
        \ CHECK FOR JUMP |EE|NN|NN|
FNF_OT: PUSH_H
        INX_H
        MOV_E,M
        INX_H
        MOV_D,M
        POP_H
        JMP     FNF_1
        \ NOT FOUND IN CURRENT FOLDER
FNF_EN: MVI_A,  #FF
        RET
        \ FILE FOUND
FNF_EE: POP_H
        SHLD    FA0
        XRA_A
        STA     HLH
        CALL    HLR
        LDA     FA3
        RET
        \ NEXT SEARCH
FNF_NX: LHLD    FA0
        XCHG
        LXI_H,  HDBUF
        LDA     FA3
        LXI_B,  #0010
        CALL    H3R
        PUSH_D
        JMP     FNF_2

MGR_:
FDS_:

\ FILL MEMORY WITH BYTE
\ INPUT:
\    A - BYTE VALUE
\    HL - ADDRESS OF THE START 
\    DE - LENGTH
\ OUTPUT:
\    MEMORY MODIFIED
\ TODO: check if memory is occupied

FLL_:   PUSH_H
        PUSH_D
        PUSH_PSW
FLL0:   POP_PSW
        MOV_M,A
        INX_H
        DCX_D
        PUSH_PSW
        MOV_A,D
        ORA_E
        JNZ     FLL0
        POP_PSW
        POP_D
        POP_H
        RET

BAT_:

\ RETURN FILE EXTENTION IDENTIFIER
\ INPUT:
\    HL - ADDRESS OF THE STRING
\ OUTPUT:
\    A - IDENTIFIER
\ TODO: make max number arbitrary, but below 32

PUX_:   PUSH_D
        PUSH_H
        XRA_A
        XCHG
        LXI_H,  STMP
PUX_1:  CALL    GUX
        PUSH_PSW
        MVI_A,  0
        CALL    CHD
        JZ      PUX_EE
        POP_PSW
        INR_A
        \ LESS THAN 16
        CPI     16
        JC      PUX_1
        LDAX_D
        JMP     PUX_E
PUX_EE: POP_PSW
PUX_E:  POP_H
        POP_D
        RET

\ RETURN STRING OF FILE EXTENTION BY ID
\ INPUT:
\    A - IDENTIFIER
\    HL - BUFFER FOR STRING
\ OUTPUT:
\    FILE EXTENTION COPIED INTO THE BUFFER
\ TODO: make max number arbitrary, but below 32

GUX_:   PUSH_H
        PUSH_D
        PUSH_PSW
        \ A GREATER OR EQUAL 16
        CPI     16
        JNC     GUX_1
        LXI_D,  EXT
GUX_2:  ORA_A
        JZ      GUX_3
        INX_D
        INX_D
        INX_D
        DCR_A
        JMP     GUX_2
GUX_3:  LDAX_D
        MOV_M,A
        INX_H
        INX_D
        LDAX_D
        MOV_M,A
        INX_H
        INX_D
        LDAX_D
        MOV_M,A
        INX_H
        MVI_M,  0
        JMP     GUX_E
GUX_1:  MOV_M,A
        INX_H
        MVI_M,  0
GUX_E:  POP_PSW
        POP_D
        POP_H
        RET

BAS_:
OVL_:

\ STRING COMPARISON
\ INPUT:
\    A - TERMINAL CHARACTER (255 IF LENGTH IN BC)
\    HL - ADDRESS OF THE 1ST STRING
\    DE - ADDRESS OF THE 2ND STRING
\    BE - (ONLY IF A=255) OPTIONAL LENGTH
\ OUTPUT:
\    A - 255-LESS THAN, 0-EQUAL, 1-GREATER
\    FLAG-Z - IF EQUAL

CHD_:   PUSH_B
        PUSH_D
        PUSH_H
        CPI     #FF
        JZ      CHD_BC
        MOV_B,A
CHD_1:  MOV_A,M
        CMP_B
        JZ      CHD_K
        XCHG
        CMP_M
        XCHG
        INX_H
        INX_D
        \ JUMP IF LESS
CHD_PP: JC      CHD_ME
        JZ      CHD_1
        JMP     CHD_BO
CHD_K:  XCHG
        CMP_M
        JZ      CHD_EE
        JMP     CHD_PP
CHD_BC: MOV_A,B
        ORA_C
        JZ      CHD_EE
        MOV_A,M
        XCHG
        CMP_M
        XCHG
        INX_H
        INX_D
        DCX_B
        JC      CHD_ME
        JZ      CHD_BC
CHD_BO: SUB_A
        INR_A
        JMP     CHD_E
CHD_EE: SUB_A
        JMP     CHD_E
CHD_ME: SUB_A
        DCR_A
CHD_E:  POP_H
        POP_D
        POP_B
        RET

\ CALCULATE STRING LENGTH
\ INPUT:
\    A - TERMINAL CHARACTER
\    HL - ADDRESS OF THE STRING
\ OUTPUT:
\    BC - LENGTH (INDEX OF THE 1ST APPEARANCE OF A)

LEN_:   PUSH_PSW
        PUSH_D
        PUSH_H
        MOV_D,A
        LXI_B,  0
LEN_1:  MOV_A,M
        CMP_D
        JZ      LEN_E
        INX_H
        INX_B
        MOV_A,B
        ORA_C
        JZ      LEN_E
        JMP     LEN_1
LEN_E:  POP_H
        POP_D
        POP_PSW
        RET

\ STRING COPY
\ INPUT:
\    A - TERMINAL CHARACTER (255 IF LENGTH IN BC)
\    HL - DESTINATION ADDRESS
\    DE - SOURCE ADDRESS
\    BE - (ONLY IF A=255) OPTIONAL LENGTH

CPY_:   PUSH_B
        PUSH_D
        PUSH_H
        PUSH_PSW
        MOV_A,H
        CMP_D
        JNZ     CPY_1
        MOV_A,L
        CMP_E
        JZ      CPY_E
CPY_1:  POP_PSW
        PUSH_PSW
        CPI     #FF
        JZ      CPY_BC
        MOV_B,A
CPY_2:  XCHG
        MOV_A,M
        XCHG
        CMP_B
        JZ      CPY_T
        MOV_M,A
        INX_H
        INX_D
        MOV_A,L
        ORA_H
        \ ERROR - CAN'T FIND THE TERMINAL CHARACTER
        JZ      CPY_E
        JMP     CPY_2
CPY_T:  MVI_M,  0
        JMP     CPY_E
CPY_BC: MOV_A,B
        ORA_C
        JZ      CPY_T
        XCHG
        MOV_A,M
        XCHG
        MOV_M,A
        INX_H
        INX_D
        DCX_B
        JMP     CPY_BC
CPY_E:  POP_PSW
        POP_H
        POP_D
        POP_B
        RET

\ STRING CONCATENATION
\ INPUT:
\    A - TERMINAL CHARACTER (255 IF LENGTH IN BC)
\    HL - DESTINATION ADDRESS
\    DE - SOURCE ADDRESS
\    BE - (ONLY IF A=255) OPTIONAL LENGTH


CAT_:   PUSH_B
        PUSH_D
        PUSH_H
        PUSH_PSW
        CPI     #FF
        JZ      CAT_BC
        MOV_B,A
CAT_1:  MOV_A,M
        CMP_B
        JZ      CAT_2
        INX_H
        MOV_A,H
        ORA_L
        \ ERROR - CAN'T FIND THE TERMINAL CHARACTER
        JZ      CAT_E
        JMP     CAT_1
CAT_2:  MOV_A,B
        CALL    CPY
        JMP     CAT_E
CAT_BC: PUSH_B
CAT_3:  INX_H
        DCX_B
        MOV_A,B
        ORA_C
        JNZ     CAT_3
        POP_B
        MVI_A,  #FF
        CALL    CPY
CAT_E:  POP_PSW
        POP_H
        POP_D
        POP_B
        RET

\ FIND SUBSTRING IN THE STRING
\ INPUT:
\    HL - ADDRESS OF THE STRING
\    BC - LENGTH
\    DE - SUBSTRING TO FIND
\    A - SUBSTRING LENGTH
\ OUTPUT:
\    HL - ADDRESS OF FOUND SUBSTRING
\    FLAG-C - NOT FOUND
\ TODO: terminal character???

FND_:   MVI_A,  #0B
        CALL    ERR




        RET

\ EXECUTE COMMAND PROCESSOR (NOT EXIT!!)

CPR_:   LXI_H,  CDIR
        CALL    WST
        MVI_A,  '>'
        CALL    WRI
        LHLD    ACMD
        MVI_M,  0
        \ MAXIMAL LENGTH OF COMMAND LINE
        LDA     MCOM
        CALL    INP
        CALL    NLN
        CALL    COM
        JMP     CPR_

\ NEW LINE

NLN_:   PUSH_PSW
        PUSH_H
        LDA     MTY
        MOV_H,A
        LDA     CR
        INR_A
        CMP_H
        JZ      NLN_EQ
        STA     CR
        JMP     NLN_EN
NLN_EQ: STA     CR
        CALL    ROL
NLN_EN: MVI_A,  0
        STA     CC
        POP_H
        POP_PSW
        RET

\ COMPARE STING WITH WILDCARD
\ INPUT:
\    HL - ADDRESS OF THE STRING
\    DE - WILDCARD
\ OUTPUT: FLAG-Z IF EQUAL

FSH_:   PUSH_H
        PUSH_D
CHD_F_: XCHG
        MOV_A,M
        XCHG
        CPI     '*'
        JZ      CHD_F1
        CMP_M
        JNZ     CHD_F2
        CPI     0
        JZ      CHD_F1
        INX_H
        INX_D
        JMP     CHD_F_
CHD_F2: SUB_A
        INR_A
        JMP     CHD_F0
CHD_F1: SUB_A
CHD_F0: POP_D
        POP_H
        RET

FBM_:
        RET

\ CONVERT NIBBLE TO HEXADECIMAL
\ INPUT:  A
\ OUTPUT: A

D0H:    ANI     #0F
        ADI     #30
        CPI     #3A
        RC
        ADI     #07
        RET

\ CONVERT BYTE TO HEXADECIMAL
\ INPUT:  A
\ OUTPUT: DE

D1H_:   PUSH_PSW
        PUSH_B
        PUSH_H
        MOV_B,A
        CALL    D0H
        MOV_E,A
        MOV_A,B
        RRC
        RRC
        RRC
        RRC
        CALL    D0H
        MOV_D,A
        POP_H
        POP_B
        POP_PSW
        RET

\ CONVERT WORD TO HEXADECIMAL
\ INPUT:  BC, HL-BUFFER
\ OUTPUT: BUFFER COPIED
\         HL - ADDRESS OF THE NEXT CHARACTER

D2H_:   PUSH_PSW
        PUSH_D
        PUSH_B
        MOV_A,B
        CALL    D1H
        MOV_M,D
        INX_H
        MOV_M,E
        INX_H
        MOV_A,C
        CALL    D1H
        MOV_M,D
        INX_H
        MOV_M,E
        INX_H
        MVI_M,  0
        POP_B
        POP_D
        POP_PSW
        RET

\ CONVERT DWORD TO HEXADECIMAL
\ INPUT:  BC,DE, HL-BUFFER
\ OUTPUT: BUFFER COPIED
\         HL - ADDRESS OF THE NEXT CHARACTER

D4H_:   PUSH_PSW
        PUSH_D
        PUSH_B
        CALL    D2H
        PUSH_D
        POP_B
        CALL    D2H
        POP_B
        POP_D
        POP_PSW
        RET

\ CONVERT WORD INTO DECIMAL
\ INPUT:
\    BC - WORD
\    HL - BUFFER
\ OUTPUT:
\    BUFFER COPIED

D2S_:   PUSH_PSW
        PUSH_B
        PUSH_D
        PUSH_H
        XCHG
        \ -10.000
        LXI_H,  #D8F0
        SHLD    STMP
        CALL    D2S_S
        \ -1.000
        LXI_H,  #FC18
        SHLD    STMP
        CALL    D2S_S
        \ -100
        LXI_H,  #FF9C
        SHLD    STMP
        CALL    D2S_S
        \ -10
        LXI_H,  #FFF6
        SHLD    STMP
        CALL    D2S_S
        \ -1
        LXI_H,  #FFFF
        SHLD    STMP
        CALL    D2S_S
        XCHG
        MVI_M,  0
        POP_H
        PUSH_H
        XRA_A
        MOV_C,A
D2S_2:  MOV_A,M
        ORA_A
        JZ      D2S_4
        CPI     '0'
        JNZ     D2S_3
        MOV_A,C
        ORA_A
        JNZ     D2S_3
        MVI_M,  #20
        INX_H
        JMP     D2S_2
D2S_3:  INX_H
        INR_C
        JMP     D2S_2
D2S_4:  POP_H
        POP_D
        POP_B
        POP_PSW
        RET

D2S_S:  XRA_A
        PUSH_B
D2S_1:  LHLD    STMP
        POP_B
        PUSH_B
        DAD_B
        MOV_C,A
        MOV_A,H
        ORA_A
        JM      D2S_1M
        POP_PSW
        INR_C
        MOV_A,C
        MOV_B,H
        MOV_C,L
        PUSH_B
        JMP     D2S_1
D2S_1M: MOV_A,C
        POP_B
        XCHG
        ADI     '0'
D2S_1W: MOV_M,A
        INX_H
        XCHG
        RET


D4S_:


\ PRINT HL IN HEXADECIMAL FORM
\ INPUT:  HL - WORD
\ OUTPUT: SCREEN CHANGED

WHL_:   PUSH_PSW
        MOV_A,H
        CALL    HEX
        MOV_A,L
        CALL    HEX
        POP_PSW
        RET

GRB_:
ENV_:

XGR_:
XFI_:
XWI_:
XMA_:
XAD_:



RET_:   RET


\ EMULATOR TEST

TEST_EM:
        PUSH_H
        LXI_H,  TEST_S
        CALL    WST
        CALL    NLN

        LXI_D,  300
        MVI_A,  #AA
        CALL    MMR
        CALL    WHL
        CALL    NLN

        LXI_D,  100
        MVI_A,  #BB
        CALL    MMR
        CALL    WHL
        CALL    NLN
        PUSH_H

        LXI_D,  512
        MVI_A,  #CC
        CALL    MMR
        CALL    WHL
        CALL    NLN

        POP_H
        CALL    MMF
        CALL    HEX
        CALL    NLN

        POP_H
        RET

TEST_U: CALL    @UTIL
        POP_H
        RET

TEST_S  DB      "SHABARSHIN A.A.",0
TEST_SH DB      "DIR",0
TEST_SY DB      "YES",0
TEST_SN DB      "NO",0