aboutsummaryrefslogtreecommitdiffstats
path: root/src/dos.c
blob: 203eff017b6787ba533d4ca18a311c8f164f9f36 (plain) (blame)
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
#include "dos.h"
#include "codepage.h"
#include "dbg.h"
#include "dosnames.h"
#include "emu.h"
#include "env.h"
#include "keyb.h"
#include "loader.h"
#include "timer.h"
#include "utils.h"
#include "video.h"

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>

// NLS data pointers
static uint32_t nls_uppercase_table;
static uint32_t nls_terminator_table;
static uint32_t nls_collating_table;
static uint32_t nls_dbc_set_table;
static uint8_t *nls_country_info;
static uint32_t dos_sysvars;

// Disk Transfer Area, buffer for find-first-file output.
static int dosDTA;

// Allocates memory for static DOS tables, from "rom" memory
static uint32_t get_static_memory(uint16_t bytes, uint16_t align)
{
    static uint32_t current = 0xFE000; // Start allocating at F000:0000
    // Align
    if(align)
        current = (current + align - 1) & ~(align - 1);
    if(current + bytes >= 0xFF0000)
        print_error("not enough static DOS memory\n");
    current += bytes;
    return current - bytes;
}

// DOS file handles
#define max_handles (0x10000)
static FILE *handles[max_handles];
static int devinfo[max_handles];

static int guess_devinfo(FILE *f)
{
    int fn = fileno(f);
    if(isatty(fn))
        return 0x80D3;
    struct stat s;
    if(fstat(fn, &s))
        return 0x80C4;
    if(S_ISREG(s.st_mode))
        return 0x0002; // TODO: assuming C: drive
    return 0x80C0;
}

static void init_handles(void)
{
    handles[0] = stdin;
    handles[1] = stdout;
    handles[2] = stderr;
    handles[3] = 0; // AUX
    handles[4] = 0; // PRN
    // stdin,stdout,stderr: special, eof on input, is device
    for(int i = 0; i < 3; i++)
        devinfo[i] = guess_devinfo(handles[i]);
}

static int get_new_handle(void)
{
    int i;
    for(i = 5; i < max_handles; i++)
        if(!handles[i])
            return i;
    return 0;
}

static int dos_close_file(int h)
{
    FILE *f = handles[h];
    if(!f)
    {
        cpuSetFlag(cpuFlag_CF);
        cpuSetAX(6);
        return -1;
    }
    handles[h] = 0;
    devinfo[h] = 0;
    cpuClrFlag(cpuFlag_CF);
    for(int i = 0; i < max_handles; i++)
        if(handles[i] == f)
            return 0; // Still referenced, don't really close
    if(f == stdin || f == stdout || f == stderr)
        return 0; // Never close standard streams
    fclose(f);
    return 0;
}

static void create_dir(void)
{
    char *fname = dos_unix_path(cpuGetAddrDS(cpuGetDX()), 1);
    debug(debug_dos, "\tmkdir '%s' ", fname);
    if(0 != mkdir(fname, 0777))
    {
        free(fname);
        cpuSetFlag(cpuFlag_CF);
        if(errno == EACCES)
            cpuSetAX(5);
        else if(errno == ENAMETOOLONG || errno == ENOTDIR)
            cpuSetAX(3);
        else if(errno == ENOENT)
            cpuSetAX(2);
        else if(errno == EEXIST)
            cpuSetAX(5);
        else
            cpuSetAX(1);
        debug(debug_dos, "ERROR %d\n", cpuGetAX());
        return;
    }
    debug(debug_dos, "OK\n");
    free(fname);
    cpuClrFlag(cpuFlag_CF);
}

static void remove_dir(void)
{
    char *fname = dos_unix_path(cpuGetAddrDS(cpuGetDX()), 1);
    debug(debug_dos, "\trmdir '%s' ", fname);
    if(0 != rmdir(fname))
    {
        free(fname);
        cpuSetFlag(cpuFlag_CF);
        if(errno == EACCES)
            cpuSetAX(5);
        else if(errno == ENAMETOOLONG || errno == ENOTDIR)
            cpuSetAX(3);
        else if(errno == ENOENT)
            cpuSetAX(2);
        else
            cpuSetAX(1);
        debug(debug_dos, "ERROR %d\n", cpuGetAX());
        return;
    }
    debug(debug_dos, "OK\n");
    free(fname);
    cpuClrFlag(cpuFlag_CF);
}

static void dos_open_file(int create)
{
    int h = get_new_handle();
    int al = cpuGetAX() & 0xFF;
    if(!h)
    {
        cpuSetAX(4);
        cpuSetFlag(cpuFlag_CF);
        return;
    }
    int name_addr = cpuGetAddrDS(cpuGetDX());
    char *fname = dos_unix_path(name_addr, create);
    if(!memory[name_addr] || !fname)
    {
        debug(debug_dos, "\t(file not found)\n");
        cpuSetAX(2);
        cpuSetFlag(cpuFlag_CF);
        return;
    }
    const char *mode;
    if(create)
        mode = "w+b";
    else
    {
        switch(al & 7)
        {
        case 0:
            mode = "rb";
            break;
        case 1:
            mode = "r+b";
            break;
        case 2:
            mode = "r+b";
            break;
        default:
            free(fname);
            cpuSetFlag(cpuFlag_CF);
            return;
        }
    }
    debug(debug_dos, "\topen '%s', '%s', %04x ", fname, mode, h);
    if(create == 2)
    {
        // Force exclusive access. We could use mode = "x+" in glibc.
        int fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0666);
        if(fd != -1)
            handles[h] = fdopen(fd, mode);
        else
            handles[h] = 0;
    }
    else
        handles[h] = fopen(fname, mode);
    if(!handles[h])
    {
        if(errno != ENOENT)
        {
            debug(debug_dos, "%s.\n", strerror(errno));
            cpuSetAX(5);
        }
        else
        {
            debug(debug_dos, "not found.\n");
            cpuSetAX(2);
        }
        cpuSetFlag(cpuFlag_CF);
        free(fname);
        return;
    }
    // Set device info:
    if(!strcmp(fname, "/dev/null"))
        devinfo[h] = 0x80C4;
    else if(!strcmp(fname, "/dev/tty"))
        devinfo[h] = 0x80D3;
    else if(memory[name_addr + 1] == ':')
    {
        uint8_t c = memory[name_addr];
        c = (c >= 'a') ? c - 'a' : c - 'A';
        if(c > 26)
            c = dos_get_default_drive();
        devinfo[h] = 0x0000 + c;
    }
    else
        devinfo[h] = 0x0000 + dos_get_default_drive();
    debug(debug_dos, "OK.\n");
    cpuClrFlag(cpuFlag_CF);
    cpuSetAX(h);
    free(fname);
}

static int get_ex_fcb(void)
{
    return cpuGetAddrDS(cpuGetDX());
}

static int get_fcb(void)
{
    int fcb = cpuGetAddrDS(cpuGetDX());
    return memory[fcb] == 255 ? fcb + 7 : fcb;
}

static int get_fcb_handle(void)
{
    return get16(0x18 + get_fcb());
}

static void dos_show_fcb()
{
    if(!debug_active(debug_dos))
        return;

    int addr = cpuGetAddrDS(cpuGetDX());
    char *name = getstr(addr + 1, 11);
    debug(debug_dos,
          "\tFCB:"
          "[d=%02x:n=%.8s.%.3s:bn=%04x:rs=%04x:fs=%08x:h=%04x:rn=%02x:ra=%08x]\n",
          memory[addr], name, name + 8, get16(addr + 0x0C), get16(addr + 0x0E),
          get32(addr + 0x10), get16(addr + 0x18), memory[addr + 0x20],
          get32(addr + 0x21));
}

static void dos_open_file_fcb(int create)
{
    int h = get_new_handle();
    if(!h)
    {
        cpuSetAL(0xFF);
        cpuSetFlag(cpuFlag_CF);
        return;
    }
    int fcb_addr = get_fcb();
    char *fname = dos_unix_path_fcb(fcb_addr, create);
    if(!fname)
    {
        debug(debug_dos, "\t(file not found)\n");
        cpuSetAL(0xFF);
        cpuSetFlag(cpuFlag_CF);
        return;
    }
    const char *mode = create ? "w+b" : "r+b";
    debug(debug_dos, "\topen fcb '%s', '%s', %04x ", fname, mode, h);
    handles[h] = fopen(fname, mode);
    if(!handles[h])
    {
        debug(debug_dos, "%s.\n", strerror(errno));
        cpuSetAL(0xFF);
        cpuSetFlag(cpuFlag_CF);
        free(fname);
        return;
    }
    // Get file size
    fseek(handles[h], 0, SEEK_END);
    long sz = ftell(handles[h]);
    fseek(handles[h], 0, SEEK_SET);
    // Set FCB info:
    put16(fcb_addr + 0x0C, 0);   // block number
    put16(fcb_addr + 0x0E, 128); // record size
    put32(fcb_addr + 0x10, sz);  // file size
    put16(fcb_addr + 0x14, 0);   // date of last write
    put16(fcb_addr + 0x16, 0);   // time of last write
    put16(fcb_addr + 0x18, h);   // reserved - store DOS handle!
    memory[fcb_addr + 0x20] = 0; // current record

    debug(debug_dos, "OK.\n");
    cpuClrFlag(cpuFlag_CF);
    cpuSetAL(0x00);
    dos_show_fcb();
    free(fname);
}

static void dos_fcb_rand_to_block(int fcb)
{
    // Update block position from random position
    unsigned rnum = get32(0x21 + fcb);
    memory[0x20 + fcb] = rnum & 127;
    put16(0x0C, rnum / 128);
}

static int dos_read_record_fcb(int addr, int update)
{
    FILE *f = handles[get_fcb_handle()];
    if(!f)
        return 1; // no data read

    int fcb = get_fcb();
    unsigned rsize = get16(0x0E + fcb);
    unsigned pos = rsize * get32(0x21 + fcb);
    uint8_t *buf = getptr(addr, rsize);
    if(!buf || !rsize)
    {
        debug(debug_dos, "\tbuffer pointer invalid\n");
        return 2; // segment wrap in DTA
    }
    // Seek to block and read
    if(fseek(f, pos, SEEK_SET))
        return 1; // no data read
    // Read
    unsigned n = fread(buf, 1, rsize, f);
    // Update random and block positions
    if(update)
    {
        put32(0x21 + fcb, (pos + n) / rsize);
        dos_fcb_rand_to_block(fcb);
    }

    if(n == rsize)
        return 0; // read full record
    else if(!n)
        return 1; // EOF
    else
    {
        for(unsigned i = n; i < rsize; i++)
            buf[i] = 0;
        return 3; // read partial record
    }
}

int dos_write_record_fcb(int addr, int update)
{
    FILE *f = handles[get_fcb_handle()];
    if(!f)
        return 1; // no data write

    int fcb = get_fcb();
    unsigned rsize = get16(0x0E + fcb);
    unsigned pos = rsize * get32(0x21 + fcb);
    uint8_t *buf = getptr(addr, rsize);
    if(!buf || !rsize)
    {
        debug(debug_dos, "\tbuffer pointer invalid\n");
        return 2; // segment wrap in DTA
    }
    // Seek to block and read
    if(fseek(f, pos, SEEK_SET))
        return 1; // no data read
    // Write
    unsigned n = fwrite(buf, 1, rsize, f);
    // Update random and block positions
    if(update)
    {
        put32(0x21 + fcb, (pos + n) / rsize);
        dos_fcb_rand_to_block(fcb);
    }
    // Update file size
    if(pos + n > get32(fcb + 0x10))
        put32(fcb + 0x10, pos + n);

    if(n == rsize)
        return 0; // write full record
    else
        return 3; // disk full
}

// Converts Unix time_t to DOS time/date
static uint32_t get_time_date(time_t tm)
{
    struct tm lt;
    if(localtime_r(&tm, &lt))
    {
        unsigned t = (lt.tm_hour << 11) | (lt.tm_min << 5) | (lt.tm_sec / 2);
        unsigned d = ((lt.tm_year - 80) << 9) | ((lt.tm_mon + 1) << 5) | (lt.tm_mday);
        return (d << 16) | t;
    }
    else
        return (1 << 16) | 1;
}

// Converts Unix mode to DOS attributes
static int get_attributes(mode_t md)
{
    int r = 0;
    // See if it's a special file
    if(S_ISDIR(md))
        r |= 1 << 4; // DIR
    else if(!S_ISREG(md))
        r |= 1 << 2; // SYSTEM
    else
        r |= 1 << 5; // ARCHIVE
    // See if the file is writable
    if(0 == (md & (S_IWOTH | S_IWGRP | S_IWUSR)))
        r |= 1 << 0; // READ_ONLY
    return r;
}

// DOS int 21, ah=43
static void int21_43(void)
{
    unsigned al = cpuGetAX() & 0xFF;
    int dname = cpuGetAddrDS(cpuGetDX());
    if(al == 0)
    {
        char *fname = dos_unix_path(dname, 0);
        if(!fname)
        {
            debug(debug_dos, "\t(file not found)\n");
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(2);
            return;
        }
        debug(debug_dos, "\tattr '%s' = ", fname);
        // GET FILE ATTRIBUTES
        struct stat st;
        if(0 != stat(fname, &st))
        {
            cpuSetFlag(cpuFlag_CF);
            if(errno == EACCES)
                cpuSetAX(5);
            else if(errno == ENAMETOOLONG || errno == ENOTDIR)
                cpuSetAX(3);
            else if(errno == ENOENT)
                cpuSetAX(2);
            else
                cpuSetAX(1);
            free(fname);
            debug(debug_dos, "ERROR %d\n", cpuGetAX());
            return;
        }
        cpuClrFlag(cpuFlag_CF);
        cpuSetCX(get_attributes(st.st_mode));
        debug(debug_dos, "%04X\n", cpuGetCX());
        free(fname);
        return;
    }
    cpuSetFlag(cpuFlag_CF);
    cpuSetAX(1);
    return;
}

// Each DTA (Data Transfer Area) in memory can hold a find-first data
// block. We simply encode our pointer in this area and use this struct
// to hold the values.
#define NUM_FIND_FIRST_DTA 64
static struct find_first_dta
{
    // List of files to return and pointer to current.
    struct dos_file_list *find_first_list;
    struct dos_file_list *find_first_ptr;
    // Address of DTA in dos memory - 0 is unused
    unsigned dta_addr;
} find_first_dta[NUM_FIND_FIRST_DTA];

// Search the list an returns the position
static struct find_first_dta *get_find_first_dta(void)
{
    int i;
    for(i = 0; i < NUM_FIND_FIRST_DTA; i++)
        if(find_first_dta[i].dta_addr == dosDTA)
            break;
    if(i == NUM_FIND_FIRST_DTA)
    {
        for(i = 0; i < NUM_FIND_FIRST_DTA; i++)
            if(!find_first_dta[i].dta_addr)
                break;
        if(i == NUM_FIND_FIRST_DTA)
        {
            print_error("Too many find-first DTA areas opened\n");
            i = 0;
        }
        find_first_dta[i].dta_addr = dosDTA;
        find_first_dta[i].find_first_list = 0;
        find_first_dta[i].find_first_ptr = 0;
    }
    return &find_first_dta[i];
}

// Removes a DTA from the list
static void clear_find_first_dta(struct find_first_dta *p)
{
    unsigned x = find_first_dta - p;
    if(x >= NUM_FIND_FIRST_DTA)
        return;
    p->dta_addr = 0;
    p->find_first_ptr = 0;
    dos_free_file_list(p->find_first_list);
}

// DOS int 21, ah=4f
static void dos_find_next(int first)
{
    struct find_first_dta *p = get_find_first_dta();
    struct dos_file_list *d = p->find_first_ptr;
    if(!d || !p->find_first_ptr->unixname)
    {
        debug(debug_dos, "\t(end)\n");
        clear_find_first_dta(p);
        cpuSetFlag(cpuFlag_CF);
        cpuSetAX(first ? 0x02 : 0x12);
    }
    else
    {
        debug(debug_dos, "\t'%s' ('%s')\n", d->dosname, d->unixname);

        // Fills the Find First Data from a dos/unix name pair
        if(strcmp("//", d->unixname))
        {
            // Normal file/directory
            struct stat st;
            if(0 == stat(d->unixname, &st))
            {
                memory[dosDTA + 0x15] = get_attributes(st.st_mode);
                put32(dosDTA + 0x16, get_time_date(st.st_mtime));
                put32(dosDTA + 0x1A, (st.st_size > 0x7FFFFFFF) ? 0x7FFFFFFF : st.st_size);
            }
            else
            {
                memory[dosDTA + 0x15] = 0;
                put32(dosDTA + 0x16, 0x10001);
                put32(dosDTA + 0x1A, 0);
            }
        }
        else
        {
            // Fills volume label data
            memory[dosDTA + 0x15] = 8;
            put32(dosDTA + 0x16, get_time_date(time(0)));
            put32(dosDTA + 0x1A, 0);
        }
        // Fills dos file name
        putmem(dosDTA + 0x1E, d->dosname, 13);
        // Next file
        p->find_first_ptr++;
        cpuClrFlag(cpuFlag_CF);
    }
}

// DOS int 21, ah=4e
static void dos_find_first(void)
{
    struct find_first_dta *p = get_find_first_dta();
    // Gets all directory entries
    if(p->find_first_list)
        dos_free_file_list(p->find_first_list);

    // Check if we want the volume label
    if(cpuGetCX() & 8)
    {
        p->find_first_list = calloc(2, sizeof(struct dos_file_list));
        p->find_first_list[0].unixname = strdup("//");
        memcpy(p->find_first_list[0].dosname, "DISK LABEL", 11);
        p->find_first_list[1].unixname = 0;
    }
    else
        p->find_first_list = dos_find_first_file(cpuGetAddrDS(cpuGetDX()));

    p->find_first_ptr = p->find_first_list;
    return dos_find_next(1);
}

static void dos_find_next_fcb(void)
{
    struct find_first_dta *p = get_find_first_dta();
    struct dos_file_list *d = p->find_first_ptr;

    if(!d || !p->find_first_ptr->unixname)
    {
        debug(debug_dos, "\t(end)\n");
        clear_find_first_dta(p);
        cpuSetAL(0xFF);
    }
    else
    {
        debug(debug_dos, "\t'%s' ('%s')\n", d->dosname, d->unixname);
        // Fills output FCB at DTA - use extended or normal depending on input
        int ofcb = memory[get_ex_fcb()] == 0xFF ? dosDTA + 7 : dosDTA;
        int pos = 1;
        for(uint8_t *c = d->dosname; *c; c++)
        {
            if(*c != '.')
                memory[ofcb + pos++] = *c;
            else
                while(pos < 9)
                    memory[ofcb + pos++] = ' ';
        }
        while(pos < 12)
            memory[ofcb + pos++] = ' ';
        // Fill drive letter
        memory[ofcb] = memory[get_fcb()];
        // Get file info
        if(strcmp("//", d->unixname))
        {
            // Normal file/directory
            struct stat st;
            if(0 == stat(d->unixname, &st))
            {
                memory[ofcb + 0x0C] = get_attributes(st.st_mode);
                put32(ofcb + 0x17, get_time_date(st.st_mtime));
                put32(ofcb + 0x1D, (st.st_size > 0x7FFFFFFF) ? 0x7FFFFFFF : st.st_size);
            }
            else
            {
                memory[ofcb + 0x0C] = 0;
                put32(ofcb + 0x17, 0x10001);
                put32(ofcb + 0x1D, 0);
            }
        }
        else
        {
            memory[ofcb + 0x0C] = 8;
            put32(ofcb + 0x17, get_time_date(time(0)));
            put32(ofcb + 0x1D, 0);
        }
        p->find_first_ptr++;
        cpuSetAL(0x00);
    }
}

static void dos_find_first_fcb(void)
{
    struct find_first_dta *p = get_find_first_dta();
    // Gets all directory entries
    if(p->find_first_list)
        dos_free_file_list(p->find_first_list);

    int efcb = get_ex_fcb();
    if(memory[efcb] == 0xFF && memory[efcb + 6] == 0x08)
    {
        p->find_first_list = calloc(2, sizeof(struct dos_file_list));
        p->find_first_list[0].unixname = strdup("//");
        memcpy(p->find_first_list[0].dosname, "DISK LABEL", 11);
        p->find_first_list[1].unixname = 0;
    }
    else
        p->find_first_list = dos_find_first_file_fcb(get_fcb());
    p->find_first_ptr = p->find_first_list;
    return dos_find_next_fcb();
}

// DOS int 21, ah=57
static void int21_57(void)
{
    unsigned al = cpuGetAX() & 0xFF;
    FILE *f = handles[cpuGetBX()];
    if(!f)
    {
        cpuSetFlag(cpuFlag_CF);
        cpuSetAX(6); // invalid handle
        return;
    }
    if(al == 0)
    {
        // GET FILE LAST WRITTEN TIME
        struct stat st;
        if(0 != fstat(fileno(f), &st))
        {
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(1);
            return;
        }
        cpuClrFlag(cpuFlag_CF);
        uint32_t td = get_time_date(st.st_mtime);
        cpuSetCX(td & 0xFFFF);
        cpuSetDX(td >> 16);
        return;
    }
    else if(al == 1)
    {
        // SET FILE LAST WRITTEN TIME
        cpuClrFlag(cpuFlag_CF);
        return;
    }
    cpuSetFlag(cpuFlag_CF);
    cpuSetAX(1);
    return;
}

static void dos_get_drive_info(uint8_t drive)
{
    if(!drive)
        drive = dos_get_default_drive();
    else
        drive--;
    cpuSetAL(32);     // 16k clusters
    cpuSetCX(512);    // 512 bytes/sector
    cpuSetDX(0xFFFF); // total 1GB
    cpuSetBX(0x0000); // media ID byte, offset
    cpuSetDS(0x0000); // and segment
    cpuClrFlag(cpuFlag_CF);
}

// Writes a character to standard output.
static void dos_putchar(uint8_t ch)
{
    if(devinfo[1] == 0x80D3 && video_active())
        video_putch(ch);
    else if(!handles[1])
        putchar(ch);
    else
        fputc(ch, handles[1]);
}

static void int21_9(void)
{
    int i = cpuGetAddrDS(cpuGetDX());

    for(; memory[i] != 0x24 && i < 0x100000; i++)
        dos_putchar(memory[i]);

    cpuSetAL(0x24);
}

static int return_code;
// Runs the emulator again with given parameters
static int run_emulator(char *file, const char *prgname, char *cmdline, char *env)
{
    pid_t pid = fork();
    if(pid == -1)
        print_error("fork error, %s\n", strerror(errno));
    else if(pid != 0)
    {
        int ret, status;
        while((ret = waitpid(pid, &status, 0)) == -1)
        {
            if(errno != EINTR)
            {
                print_error("error waiting child, %s\n", strerror(errno));
                break;
            }
        }
        return_code = (WEXITSTATUS(status) & 0xFF);
        if(!WIFEXITED(status))
            return_code |= 0x100;
        if(return_code)
            debug(debug_dos, "child exited with code %04x\n", return_code);
        return return_code > 0xFF;
    }
    else
    {
        // Set program name
        setenv(ENV_PROGNAME, prgname, 1);
        // default drive
        char drv[2] = {0, 0};
        drv[0] = dos_get_default_drive() + 'A';
        setenv(ENV_DEF_DRIVE, drv, 1);
        // and CWD
        setenv(ENV_CWD, (const char *)dos_get_cwd(0), 1);
        // pass open file descriptors to child process
        for(unsigned i = 0; i < 3; i++)
            if(handles[i])
            {
                int f1 = fileno(handles[i]);
                int f2 = (f1 < 3) ? dup(f1) : f1;
                dup2(f2, i);
                close(f2);
                if(f1 >= 3)
                    close(f1);
            }
        // Accumulate args:
        char *args[64];
        int i;
        const char *exe_path = get_program_exe_path();
        if(!exe_path)
            print_error("can't get emulator path.\n");
        args[0] = prog_name;
        args[1] = file;
        args[2] = cmdline;
        args[3] = "--";
        for(i = 4; i < 63 && *env; i++)
        {
            int l = strlen(env);
            args[i] = env;
            env += l + 1;
        }
        for(; i < 64; i++)
            args[i] = 0;
        if(execv(exe_path, args) == -1)
        {
            raise(SIGABRT);
            exit(1);
        }
    }
    return 0;
}

// DOS exit
void int20()
{
    exit(0);
}

static void char_input(int brk)
{
    static uint16_t last_key;
    fflush(handles[1] ? handles[1] : stdout);

    if(last_key == 0)
    {
        if(devinfo[0] != 0x80D3 && handles[0])
            last_key = getc(handles[0]);
        else
            last_key = getch(brk);
    }
    debug(debug_dos, "\tgetch = %02x '%c'\n", last_key, (char)last_key);
    cpuSetAL(last_key);
    if((last_key & 0xFF) == 0)
        last_key = last_key >> 8;
    else
        last_key = 0;
}

static void int21_debug(void)
{
    static const char *func_names[] =
    {
        "terminate", "getchar", "putchar", "getc(aux)", "putc(aux)", // 0-4
        "putc(prn)", "console i/o", "getch", "getch", "puts", // 5-9
        "gets", "eof(stdin)", "flush(stdin)+", "disk reset", "set drive", // 0A-0E
        "open fcb", "close fcb", "find first fcb", "find next fcb", "del fcb", // 0F-13
        "read fcb", "write fcb", "creat fcb", "rename fcb", "n/a", // 14-18
        "get drive", "set DTA", "stat def drive", "stat drive", "n/a", // 19-1D
        "n/a", "get def DPB", "n/a", "read fcb", "write fcb", // 1E-22
        "size fcb", "set record fcb", "set int vect", "create PSP", "read blk fcb", // 23-27
        "write blk fcb", "parse filename", "get date", "set date", "get time", // 28-2C
        "set time", "set verify", "get DTA", "version", "go TSR", // 2D-31
        "get DPB", "g/set brk check", "InDOS addr", "get int vect", "get free", // 32-36
        "get/set switch", "country info", "mkdir", "rmdir", "chdir", // 37-3B
        "creat", "open", "close", "read", "write", // 3C-40
        "unlink", "lseek", "get/set attr", "g/set devinfo", "dup", // 41-45
        "dup2", "get CWD", "mem alloc", "mem free", "mem resize", // 46-4A
        "exec", "exit", "get errorlevel", "find first", "find next", // 4B-4F
        "set PSP", "get PSP", "get sysvars", "trans BPB to DPB", "get verify", // 50-54
        "create PSP", "rename", "g/set file dates", "g/set alloc type", "ext error", // 55-59
        "create tmpfile", "creat new file", "flock", "(server fn)", "(net fn)", // 5A-5E
        "(net redir)", "truename", "n/a", "get PSP", "intl char info", // 5F-63
        "(internal)", "get ext country info"
    };
    unsigned ax = cpuGetAX();
    const char *fn;
    if((ax >> 8) < (sizeof(func_names) / sizeof(func_names[0])))
        fn = func_names[ax >> 8];
    else
        fn = "(unknown)";

    debug(debug_dos, "D-21%04X: %-15s BX=%04X CX:%04X DX:%04X DI=%04X DS:%04X ES:%04X\n",
          ax, fn, cpuGetBX(), cpuGetCX(), cpuGetDX(), cpuGetDI(), cpuGetDS(), cpuGetES());
}

// DOS int 21
void int21()
{
    // Check CP/M call, INT 21h from address 0x000C0
    if(cpuGetAddress(cpuGetStack(2), cpuGetStack(0)) == 0xC2)
    {
        debug(debug_dos, "CP/M CALL: ");
        int old_ax = cpuGetAX();
        int ip = cpuGetStack(10), cs = cpuGetStack(8), flags = cpuGetStack(4);
        // Fix-up registers
        cpuSetAX((cpuGetCX() << 8) | (old_ax & 0xFF));
        cpuSetSP(cpuGetSP() + 6);
        // Fix-up return address, interchanges segment/ip:
        int stack = cpuGetAddress(cpuGetSS(), cpuGetSP());
        put16(stack, ip);
        put16(stack + 2, cs);
        put16(stack + 4, flags);
        // Call ourselves
        int21();
        // Restore AH
        cpuSetAX((old_ax & 0xFF00) | (cpuGetAX() & 0xFF));
        return;
    }
    debug(debug_int, "D-21%04X: BX=%04X\n", cpuGetAX(), cpuGetBX());
    if(debug_active(debug_dos))
        int21_debug();

    // Process interrupt
    unsigned ax = cpuGetAX(), ah = ax >> 8;

    // Store SS:SP into PSP, used at return from child process
    // According to DOSBOX, only set for certain functions:
    if(ah != 0x50 && ah != 0x51 && ah != 0x62 && ah != 0x64 && ah < 0x6c)
    {
        put16(cpuGetAddress(get_current_PSP(), 0x2E), cpuGetSP());
        put16(cpuGetAddress(get_current_PSP(), 0x30), cpuGetSS());
    }

    switch(ah)
    {
    case 0: // TERMINATE PROGRAM
        exit(0);
    case 1: // CHARACTER INPUT WITH ECHO
        char_input(1);
        dos_putchar(cpuGetAX() & 0xFF);
        break;
    case 2: // PUTCH
        dos_putchar(cpuGetDX() & 0xFF);
        cpuSetAX(0x0200 | (cpuGetDX() & 0xFF)); // from intlist.
        break;
    case 0x6: // CONSOLE OUTPUT
        if((cpuGetDX() & 0xFF) == 0xFF)
            char_input(1);
        else
        {
            dos_putchar(cpuGetDX() & 0xFF);
            cpuSetAL(cpuGetDX());
        }
        break;
    case 0x7: // DIRECT CHARACTER INPUT WITHOUT ECHO
        char_input(0);
        break;
    case 0x8: // CHARACTER INPUT WITHOUT ECHO
        char_input(1);
        break;
    case 0x9: // WRITE STRING
        int21_9();
        break;
    case 0xA: // BUFFERED INPUT
    {
        FILE *f = handles[0] ? handles[0] : stdin;
        int addr = cpuGetAddrDS(cpuGetDX());
        unsigned len = memory[addr], i = 2;
        while(i < len && addr + i < 0x100000)
        {
            int c = getc(f);
            if(c == '\n' || c == EOF)
                c = '\r';
            memory[addr + i] = (char)c;
            if(c == '\r')
                break;
            i++;
        }
        memory[addr + 1] = i - 2;
        break;
    }
    case 0xB: // STDIN STATUS
        if(devinfo[0] == 0x80D3)
            cpuSetAX(kbhit() ? 0x0BFF : 0x0B00);
        else
            cpuSetAX(0x0B00);
        break;
    case 0xC: // FLUSH AND READ STDIN
        tcflush(STDIN_FILENO, TCIFLUSH);
        fflush(stdin);
        switch(ax & 0xFF)
        {
        case 0x01:
        case 0x06:
        case 0x07:
        case 0x08:
        case 0x0A:
            cpuSetAX(ax << 8);
            int21();
            return;
        }
        break;
    case 0x0E: // SELECT DEFAULT DRIVE
        dos_set_default_drive(cpuGetDX() & 0xFF);
        // Number of drives = 3, 'A:', 'B:' and 'C:'
        cpuSetAX(0x0E03);
        break;
    case 0x0F: // OPEN FILE USING FCB
        dos_open_file_fcb(0);
        break;
    case 0x10: // CLOSE FILE USING FCB
        dos_show_fcb();
        // Set full AX because dos_close_file clobbers AX
        cpuSetAX(dos_close_file(get_fcb_handle()) ? 0x10FF : 0x1000);
        break;
    case 0x11: // FIND FIRST FILE USING FCB
        dos_find_first_fcb();
        break;
    case 0x12: // FIND NEXT FILE USING FCB
        dos_find_next_fcb();
        break;
    case 0x13: // DELETE FILE USING FCB
    {
        dos_show_fcb();
        /* TODO: Limited support. No wild cards */
        int fcb_addr = get_fcb();
        char *fname = dos_unix_path_fcb(fcb_addr, 0);
        if(!fname)
        {
            debug(debug_dos, "\t(file not found)\n");
            cpuSetAL(0xFF);
            break;
        }
        debug(debug_dos, "\tdelete fcb '%s'\n", fname);
        int e = unlink(fname);
        free(fname);
        if(e)
        {
            debug(debug_dos, "\tcould not delete file (%d).\n", errno);
            cpuSetAL(0xFF);
        }
        else
        {
            memory[fcb_addr + 0x1] = 0xE5; // Marker for file deleted
            cpuSetAL(0x00);
        }
        break;
    }
    case 0x14: // SEQUENTIAL READ USING FCB
        dos_show_fcb();
        cpuSetAL(dos_read_record_fcb(dosDTA, 1));
        break;
    case 0x15: // SEQUENTIAL WRITE USING FCB
        dos_show_fcb();
        cpuSetAL(dos_write_record_fcb(dosDTA, 1));
        break;
    case 0x16: // CREATE FILE USING FCB
        dos_open_file_fcb(1);
        break;
    case 0x19: // GET DEFAULT DRIVE
        debug(debug_dos, "\tget default drive = '%c'\n", dos_get_default_drive() + 'A');
        cpuSetAL(dos_get_default_drive());
        break;
    case 0x1A: // SET DTA
        dosDTA = 0xFFFFF & (cpuGetDS() * 16 + cpuGetDX());
        break;
    case 0x1B: // GET DEFAULT DRIVE INFO
        dos_get_drive_info(0);
        break;
    case 0x1C: // GET DRIVE INFO
        dos_get_drive_info(cpuGetDX() & 0xFF);
        break;
    case 0x21: // RANDOM READ USING FCB
        dos_show_fcb();
        cpuSetAL(dos_read_record_fcb(dosDTA, 0));
        break;
    case 0x22: // RANDOM WRITE USING FCB
        dos_show_fcb();
        cpuSetAL(dos_write_record_fcb(dosDTA, 0));
        break;
    case 0x25: // set interrupt vector
        put16(4 * (ax & 0xFF), cpuGetDX());
        put16(4 * (ax & 0xFF) + 2, cpuGetDS());
        break;
    case 0x26: // Create PSP (duplicate current PSP)
    {
        uint8_t *new_psp = getptr(cpuGetAddress(cpuGetDX(), 0), 0x100);
        uint8_t *orig_psp = getptr(cpuGetAddress(get_current_PSP(), 0), 0x100);
        if(!new_psp || !orig_psp)
        {
            debug(debug_dos, "\tinvalid new PSP segment %04x.\n", cpuGetDX());
            break;
        }
        // Copy PSP to the new segment, 0x80 is what DOS does - this excludes command line
        memcpy(new_psp, orig_psp, 0x80);
        break;
    }
    case 0x27: // BLOCK READ FROM FCB
    case 0x28: // BLOCK WRITE TO FCB
    {
        dos_show_fcb();
        int fcb = get_fcb();
        unsigned count = cpuGetCX();
        unsigned rsize = get16(0x0E + fcb);
        unsigned e = 0;
        int target = dosDTA;

        while(!e && count)
        {
            if(0x27 == (ax >> 8))
                e = dos_read_record_fcb(target, 1);
            else
                e = dos_write_record_fcb(target, 1);

            if(e == 0 || e == 3)
            {
                target += rsize;
                count--;
            }
        }
        cpuSetCX(cpuGetCX() - count);
        cpuSetAL(e);
        dos_show_fcb();
        break;
    }
    case 0x29: // PARSE FILENAME TO FCB
    {
        // TODO: length could be more than 64 bytes!
        char *fname = getstr(cpuGetAddrDS(cpuGetSI()), 64);
        char *orig = fname;
        uint8_t *dst = getptr(cpuGetAddrES(cpuGetDI()), 37);
        if(!dst)
        {
            debug(debug_dos, "\tinvalid destination\n");
            cpuSetAL(0xFF);
            break;
        }
        debug(debug_dos, "\t'%s' -> ", fname);
        if(ax & 1)
            // Skip separator
            if(*fname && strchr(":;.,=+", *fname))
                fname++;
        // Skip initial spaces
        while(*fname && strchr(" \t", *fname))
            fname++;
        // Check drive:
        int ret = 0;
        dst[0] = 0;
        if(*fname && fname[1] == ':')
        {
            char d = *fname;
            if(d >= 'A' && d <= 'Z')
                dst[0] = d - 'A' + 1;
            else if(d >= 'a' && d <= 'z')
                dst[0] = d - 'a' + 1;
            else
                ret = 0xFF;
            fname += 2;
        }
        int i = 1;
        while(i < 12)
        {
            char c = *fname;
            if(c == '.' && i <= 9)
            {
                for(; i < 9; i++)
                    dst[i] = ' ';
                fname++;
            }
            else if(!c || strchr(":.;,=+ \t/\"[]<>|\x0D\x10", c))
            {
                for(; i < 12; i++)
                    dst[i] = ' ';
                break;
            }
            else if(c == '*' && i < 9)
            {
                for(; i < 9; i++)
                    dst[i] = '?';
                fname++;
                ret = 1;
            }
            else if(c == '*')
            {
                for(; i < 12; i++)
                    dst[i] = '?';
                fname++;
                ret = 1;
                break;
            }
            else
            {
                if(c >= 'a' && c <= 'z')
                    dst[i] = c - 'a' + 'A';
                else
                    dst[i] = c;
                i++;
                fname++;
            }
        }
        // Update resulting pointer
        int si = cpuGetSI() + (fname - orig);
        while(si > 0xFFFF)
        {
            si = si - 0x10;
            cpuSetDS(cpuGetDS() + 1);
        }
        cpuSetSI((si));
        cpuSetAL(ret);
        debug(debug_dos, "%c:'%.11s'\n", dst[0] ? dst[0] + '@' : '*', dst + 1);
        break;
    }
    case 0x2A: // GET SYSTEM DATE
    {
        time_t tm = time(0);
        struct tm lt;
        if(localtime_r(&tm, &lt))
        {
            cpuSetAL(lt.tm_wday);
            cpuSetCX(lt.tm_year + 1900);
            cpuSetDX(((lt.tm_mon + 1) << 8) | (lt.tm_mday));
        }
        break;
    }
    case 0x2B: // SET SYSTEM DATE
        cpuSetAL(0xFF); // Invalid date - don't support setting date.
        break;
    case 0x2C: // GET SYSTEM TIME
    {
        // Use BIOS time: 1573040 ticks per day
        uint32_t bios_timer = get_bios_timer() * 1080;
        uint32_t bsec = bios_timer / 19663;
        uint32_t bsub = bios_timer % 19663;

        uint8_t thor = bsec / 3600;
        uint8_t tmin = (bsec / 60) % 60;
        uint8_t tsec = bsec % 60;
        uint8_t msec = bsub * 100 / 19663;

        cpuSetCX((thor << 8) | tmin);
        cpuSetDX((tsec << 8) | msec);
        break;
    }
    case 0x2D: // SET SYSTEM TIME
        cpuSetAL(0xFF); // Invalid time - don't support setting time.
        break;
    case 0x2F: // GET DTA
        cpuSetES((dosDTA & 0xFFF00) >> 4);
        cpuSetBX((dosDTA & 0x000FF));
        break;
    case 0x30: // DOS version: 3.30
        cpuSetAX(0x1E03);
        cpuSetBX(0x0000);
        break;
    case 0x33: // BREAK SETTINGS
        if(ax == 0x3300)
            cpuSetDX((cpuGetDX() & 0xFF00) | 1);
        else if(ax == 0x3301)
            cpuSetDX((cpuGetDX() & 0xFF00) | 1); // Ignore new state
        break;
    case 0x35: // get interrupt vector
        cpuSetBX(get16(4 * (ax & 0xFF)));
        cpuSetES(get16(4 * (ax & 0xFF) + 2));
        break;
    case 0x36: // get free space
        cpuSetAX(32);     // 16k clusters
        cpuSetBX(0xFFFF); // all free, 1GB
        cpuSetCX(512);    // 512 bytes/sector
        cpuSetDX(0xFFFF); // total 1GB
        break;
    case 0x37: // get/set switch character
        cpuSetDX('/');
        break;
    case 0x38: // GET COUNTRY INFO
        putmem(cpuGetAddrDS(cpuGetDX()), nls_country_info, 34);
        break;
    case 0x39: // MKDIR
        create_dir();
        break;
    case 0x3A: // RMDIR
        remove_dir();
        break;
    case 0x3B: // CHDIR
    {
        if(dos_change_dir(cpuGetAddrDS(cpuGetDX())))
        {
            cpuSetAX(2);
            cpuSetFlag(cpuFlag_CF);
        }
        else
            cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x3C: // CREATE FILE
        dos_open_file(1);
        break;
    case 0x3D: // OPEN EXISTING FILE
        dos_open_file(0);
        break;
    case 0x3E: // CLOSE FILE
        dos_close_file(cpuGetBX());
        break;
    case 0x3F: // READ
    {
        FILE *f = handles[cpuGetBX()];
        if(!f)
        {
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(6); // invalid handle
            break;
        }
        uint8_t *buf = getptr(cpuGetAddrDS(cpuGetDX()), cpuGetCX());
        if(!buf)
        {
            debug(debug_dos, "\tbuffer pointer invalid\n");
            cpuSetAX(5);
            cpuSetFlag(cpuFlag_CF);
            break;
        }
        // If read from "CON", reads up to the first "CR":
        if(devinfo[cpuGetBX()] == 0x80D3)
        {
            int i, max = cpuGetCX(), cr = 0;
            for(i = 0; i < max; i++)
            {
                int c = fgetc(f);
                if(c < 0)
                    break;
                if(c == '\n' && !cr && i < max)
                {
                    cr = 1;
                    buf[i] = '\r';
                    i++;
                }
                else if(c == '\r')
                    cr = 1;
                buf[i] = c;
                if(c == '\n')
                {
                    i++;
                    break;
                }
            }
            cpuSetAX(i);
        }
        else
        {
            unsigned n = fread(buf, 1, cpuGetCX(), f);
            cpuSetAX(n);
        }
        cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x40: // WRITE
    {
        FILE *f = handles[cpuGetBX()];
        if(!f)
        {
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(6); // invalid handle
            return;
        }
        unsigned len = cpuGetCX();
        uint8_t *buf = getptr(cpuGetAddrDS(cpuGetDX()), len);
        if(!buf)
        {
            debug(debug_dos, "\tbuffer pointer invalid\n");
            cpuSetAX(5);
            cpuSetFlag(cpuFlag_CF);
            break;
        }
        if(devinfo[cpuGetBX()] == 0x80D3 && video_active())
        {
            for(unsigned i = 0; i < len; i++)
                video_putch(buf[i]);
            cpuSetAX(len);
        }
        else
        {
            unsigned n = fwrite(buf, 1, len, f);
            cpuSetAX(n);
        }
        cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x41: // UNLINK
    {
        char *fname = dos_unix_path(cpuGetAddrDS(cpuGetDX()), 0);
        if(!fname)
        {
            debug(debug_dos, "\t(file not found)\n");
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(0x02);
            break;
        }
        debug(debug_dos, "\tunlink '%s'\n", fname);
        int e = unlink(fname);
        free(fname);
        if(e)
        {
            cpuSetFlag(cpuFlag_CF);
            if(errno == ENOTDIR)
                cpuSetAX(0x03);
            else if(errno == ENOENT)
                cpuSetAX(0x02);
            else
                cpuSetAX(0x05);
        }
        else
            cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x42: // LSEEK
    {
        FILE *f = handles[cpuGetBX()];
        long pos = cpuGetDX();
        if(cpuGetCX() >= 0x8000)
            pos = pos + (((long)cpuGetCX() - 0x10000) << 16);
        else
            pos = pos + (cpuGetCX() << 16);

        debug(debug_dos, "\tlseek-%02x pos = %ld\n", ax & 0xFF, pos);
        if(!f)
        {
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(6); // invalid handle
            break;
        }
        switch(ax & 0xFF)
        {
        case 0:
            fseek(f, pos, SEEK_SET);
            break;
        case 1:
            fseek(f, pos, SEEK_CUR);
            break;
        case 2:
            fseek(f, pos, SEEK_END);
            break;
        default:
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(1);
            return;
        }
        pos = ftell(f);
        cpuSetAX(pos & 0xFFFF);
        cpuSetDX((pos >> 16) & 0xFFFF);
        cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x43: // DOS 2+ file attributes
        int21_43();
        break;
    case 0x44:
    {
        int h = cpuGetBX();
        int al = ax & 0xFF;
        if((al < 4 || al == 6 || al == 7 || al == 10 || al == 12 || al == 16) &&
           !handles[h])
        {
            // Show error if it is a file handle.
            debug(debug_dos, "\t(invalid file handle)\n");
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(6); // invalid handle
            break;
        }
        cpuClrFlag(cpuFlag_CF);
        switch(al)
        {
        case 0x00: // GET DEV INFO
            debug(debug_dos, "\t= %04x\n", devinfo[h]);
            cpuSetDX(devinfo[h]);
            break;
        case 0x01: // SET DEV INFO
        case 0x02: // IOCTL CHAR DEV READ
        case 0x03: // IOCTL CHAR DEV WRITE
        case 0x04: // IOCTL BLOCK DEV READ
        case 0x05: // IOCTL BLOCK DEV |WRITE
            cpuSetAX(5);
            cpuSetFlag(cpuFlag_CF);
            break;
        case 0x06: // GET INPUT STATUS
            if(devinfo[h] == 0x80D3)
                cpuSetAX(kbhit() ? 0x44FF : 0x4400);
            else
                cpuSetAX(feof(handles[h]) ? 0x4400 : 0x44FF);
            break;
        case 0x07: // GET OUTPUT STATUS
            cpuSetAX(0x44FF);
            break;
        case 0x08: // CHECK DRIVE REMOVABLE
            h = h & 0xFF;
            h = h ? h - 1 : dos_get_default_drive();
            if(h < 2)
                cpuSetAX(0x0000);
            else
                cpuSetAX(0x0001);
            break;
        case 0x09: // CHECK DRIVE REMOTE
            cpuSetDX(0x0100);
            break;
        case 0x0A: // CHECK HANDLE REMOTE
            cpuSetDX(0);
            break;
        case 0x0B: // SET SHARING RETRY COUNT
        case 0x0C: // GENERIC CHAR DEV REQUEST
        case 0x0D: // GENERIC BLOCK DEV REQUEST
        case 0x0F: // SET LOGICAL DRIVE MAP
        case 0x10: // QUERY CHAR DEV CAPABILITY
        case 0x11: // QUERY BLOCK DEV CAPABILITY
            cpuSetAX(1);
            cpuSetFlag(cpuFlag_CF);
            break;
        case 0x0E: // GET LOGICAL DRIVE MAP
            cpuSetAX(0x4400);
        }
        break;
    }
    case 0x45:
    {
        if(!handles[cpuGetBX()])
        {
            // Show error if it is a file handle.
            debug(debug_dos, "\t(invalid file handle)\n");
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(6); // invalid handle
            break;
        }
        int h = get_new_handle();
        if(!h)
        {
            cpuSetAX(4);
            cpuSetFlag(cpuFlag_CF);
            break;
        }
        debug(debug_dos, "\t%04x -> %04x\n", cpuGetBX(), h);
        handles[h] = handles[cpuGetBX()];
        devinfo[h] = devinfo[cpuGetBX()];
        cpuSetAX(h);
        cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x46:
    {
        if(!handles[cpuGetBX()])
        {
            // Show error if it is a file handle.
            debug(debug_dos, "\t(invalid file handle)\n");
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(6); // invalid handle
            break;
        }
        if(handles[cpuGetCX()])
            dos_close_file(cpuGetCX());
        handles[cpuGetCX()] = handles[cpuGetBX()];
        devinfo[cpuGetCX()] = devinfo[cpuGetBX()];
        cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x47: // GET CWD
    {
        // Note: ignore drive letter in DL
        const uint8_t *path = dos_get_cwd(cpuGetDX() & 0xFF);
        debug(debug_dos, "\tcwd '%c' = '%s'\n", '@' + (cpuGetDX() & 0xFF), path);
        putmem(cpuGetAddrDS(cpuGetSI()), path, 64);
        cpuSetAX(0x0100);
        cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x48: // ALLOC MEMORY BLOCK
    {
        int seg, max = 0;
        seg = mem_alloc_segment(cpuGetBX(), &max);
        if(seg)
        {
            debug(debug_dos, "\tallocated at %04x.\n", seg);
            cpuSetAX(seg);
            cpuClrFlag(cpuFlag_CF);
        }
        else
        {
            debug(debug_dos, "\tnot enough memory, max=$%04x paragraphs\n", max);
            cpuSetAX(0x8);
            cpuSetBX(max);
            cpuSetFlag(cpuFlag_CF);
        }
        break;
    }
    case 0x49: // FREE MEMORY BLOCK
    {
        mem_free_segment(cpuGetES());
        cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x4A: // RESIZE MEMORY BLOCK
    {
        int sz;
        sz = mem_resize_segment(cpuGetES(), cpuGetBX());
        if(sz == cpuGetBX())
            cpuClrFlag(cpuFlag_CF);
        else
        {
            cpuSetAX(0x8);
            cpuSetBX(sz);
            cpuSetFlag(cpuFlag_CF);
            debug(debug_dos, "\tmax memory available: $%04x\n", sz);
        }
        break;
    }
    case 0x4B: // EXEC
    {
        char *fname = dos_unix_path(cpuGetAddrDS(cpuGetDX()), 0);
        if(!fname)
        {
            debug(debug_dos, "\texec error, file not found\n");
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(2);
            break;
        }
        // Flags:   0 = Load and Go, 1 = LOAD, 3 = Overlay
        //        128 = LoadHi
        if((ax & 0xFF) == 3)
        {
            debug(debug_dos, "\tload overlay '%s'\n", fname);
            int pb = cpuGetAddrES(cpuGetBX());
            uint16_t load_seg = get16(pb);
            uint16_t reloc_seg = get16(pb + 2);
            FILE *f = fopen(fname, "rb");
            if(!f || dos_read_overlay(f, load_seg, reloc_seg))
            {
                debug(debug_dos, "\tERROR\n");
                cpuSetFlag(cpuFlag_CF);
                cpuSetAX(11);
            }
            else
                cpuClrFlag(cpuFlag_CF);
        }
        else if((ax & 0xFF) == 0)
        {
            debug(debug_dos, "\texec: '%s'\n", fname);
            // Get executable file name:
            char *prgname = getstr(cpuGetAddrDS(cpuGetDX()), 64);
            // Read command line parameters:
            int pb = cpuGetAddrES(cpuGetBX());
            int cmd_addr = cpuGetAddress(get16(pb + 4), get16(pb + 2));
            int clen = memory[cmd_addr];
            char *cmdline = getstr(cmd_addr + 1, clen);
            debug(debug_dos, "\texec command line: '%s %.*s'\n", prgname, clen, cmdline);
            char *env = "\0\0";
            if(get16(pb) != 0)
            {
                // Sanitize env
                int eaddr = cpuGetAddress(get16(pb), 0);
                while(memory[eaddr] != 0 && eaddr < 0xFFFFF)
                {
                    while(memory[eaddr] != 0 && eaddr < 0xFFFFF)
                        eaddr++;
                    eaddr++;
                }
                if(eaddr < 0xFFFFF)
                    env = (char *)(memory + cpuGetAddress(get16(pb), 0));
            }
            if(run_emulator(fname, prgname, cmdline, env))
            {
                cpuSetAX(5); // access denied
                cpuSetFlag(cpuFlag_CF);
            }
            else
                cpuClrFlag(cpuFlag_CF);
        }
        else
        {
            debug(debug_dos, "\texec '%s': type %02xh not supported.\n", fname,
                  ax & 0xFF);
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(1);
        }
        free(fname);
        break;
    }
    case 0x4C: // EXIT
        // Detect if our PSP is last one
        debug(debug_dos, "\texit PSP:'%04x', PARENT:%04x.\n", get_current_PSP(),
              get16(cpuGetAddress(get_current_PSP(), 22)));
        if(0xFFFE == get16(cpuGetAddress(get_current_PSP(), 22)))
            exit(ax & 0xFF);
        else
        {
            // Exit to parent
            // TODO: we must close all child file descriptors and dealocate
            //       child memory.
            return_code = cpuGetAX() & 0xFF;
            // Patch INT 22h, 23h and 24h addresses to the ones saved in new PSP
            put16(0x88, get16(cpuGetAddress(get_current_PSP(), 10)));
            put16(0x8A, get16(cpuGetAddress(get_current_PSP(), 12)));
            put16(0x8C, get16(cpuGetAddress(get_current_PSP(), 14)));
            put16(0x8E, get16(cpuGetAddress(get_current_PSP(), 16)));
            put16(0x90, get16(cpuGetAddress(get_current_PSP(), 18)));
            put16(0x92, get16(cpuGetAddress(get_current_PSP(), 20)));
            // Set PSP to parent
            set_current_PSP(get16(cpuGetAddress(get_current_PSP(), 22)));
            // Get last stack
            cpuSetSS(get16(cpuGetAddress(get_current_PSP(), 0x30)));
            cpuSetSP(get16(cpuGetAddress(get_current_PSP(), 0x2E)));
            int stack = cpuGetAddress(cpuGetSS(), cpuGetSP());
            // Fixup interrupt return
            put16(stack, get16(0x22 * 4));
            put16(stack + 2, get16(0x22 * 4 + 2));
            put16(stack + 4, 0xf202);
            // And exit!
        }
        break;
    case 0x4D: // GET RETURN CODE (ERRORLEVEL)
        cpuSetAX(return_code);
        return_code = 0;
        cpuClrFlag(cpuFlag_CF);
        break;
    case 0x4E: // FIND FIRST MATCHING FILE
        dos_find_first();
        break;
    case 0x4F: // FIND NEXT MATCHING FILE
        dos_find_next(0);
        break;
    case 0x50: // SET CURRENT PSP
        set_current_PSP(cpuGetBX());
        break;
    case 0x51: // GET CURRENT PSP
        cpuSetBX(get_current_PSP());
        break;
    case 0x52: // GET SYSVARS
        cpuSetES(dos_sysvars >> 4);
        cpuSetBX((dos_sysvars & 0xF) + 24);
        break;
    case 0x55: // Create CHILD PSP
    {
        uint8_t *new_psp = getptr(cpuGetAddress(cpuGetDX(), 0), 0x100);
        uint8_t *orig_psp = getptr(cpuGetAddress(get_current_PSP(), 0), 0x100);
        if(!new_psp || !orig_psp)
        {
            debug(debug_dos, "\tinvalid new PSP segment %04x.\n", cpuGetDX());
            break;
        }
        // Copy PSP to the new segment, 0x80 is what DOS does - this excludes command line
        memcpy(new_psp, orig_psp, 0x80);
        // Set parent PSP to the current one
        new_psp[22] = get_current_PSP() & 0xFF;
        new_psp[23] = get_current_PSP() >> 8;
        set_current_PSP(cpuGetDX());
        break;
    }
    case 0x56: // RENAME
    {
        char *fname1 = dos_unix_path(cpuGetAddrDS(cpuGetDX()), 0);
        if(!fname1)
        {
            debug(debug_dos, "\t(file not found)\n");
            cpuSetAX(0x02);
            cpuSetFlag(cpuFlag_CF);
        }
        char *fname2 = dos_unix_path(cpuGetAddrES(cpuGetDI()), 1);
        debug(debug_dos, "\t'%s' -> '%s'\n", fname1, fname2);
        int e = rename(fname1, fname2);
        free(fname2);
        free(fname1);
        if(e)
        {
            cpuSetFlag(cpuFlag_CF);
            if(errno == ENOTDIR)
                cpuSetAX(0x03);
            else if(errno == ENOENT)
                cpuSetAX(0x02);
            else
                cpuSetAX(0x05);
        }
        else
            cpuClrFlag(cpuFlag_CF);
        break;
    }
    case 0x57: // DATE/TIME
        int21_57();
        break;
    case 0x58:
    {
        uint8_t al = ax & 0xFF;
        if(0 == al)
            cpuSetAX(mem_get_alloc_strategy());
        else if(1 == al)
            mem_set_alloc_strategy(cpuGetBX());
        else if(3 == al)
        {
            cpuSetFlag(cpuFlag_CF);
            cpuSetAX(1);
        }
    }
    case 0x5B: // CREATE NEW FILE
        dos_open_file(2);
        break;
    case 0x62: // GET PSP SEGMENT
        cpuSetBX(get_current_PSP());
        break;
    case 0x65: // GET NLS DATA
    {
        uint32_t addr = cpuGetAddrES(cpuGetDI());
        cpuClrFlag(cpuFlag_CF);
        switch(ax & 0xFF)
        {
        case 1:
        {
            static const uint8_t data[] = {1, 38, 0, 1, 0, 181, 1};
            putmem(addr, data, 7);
            putmem(addr + 7, nls_country_info, 34);
            cpuSetCX(41);
            break;
        }
        case 2:
            memory[addr] = 2;
            put16(addr + 1, nls_uppercase_table & 0xF);
            put16(addr + 3, nls_uppercase_table >> 4);
            cpuSetCX(5);
            break;
        case 4:
            memory[addr] = 4;
            put16(addr + 1, nls_uppercase_table & 0xF);
            put16(addr + 3, nls_uppercase_table >> 4);
            cpuSetCX(5);
            break;
        case 5:
            memory[addr] = 5;
            put16(addr + 1, nls_terminator_table & 0xF);
            put16(addr + 3, nls_terminator_table >> 4);
            cpuSetCX(5);
            break;
        case 6:
            memory[addr] = 6;
            put16(addr + 1, nls_collating_table & 0xF);
            put16(addr + 3, nls_collating_table >> 4);
            cpuSetCX(5);
            break;
        case 7:
            memory[addr] = 7;
            put16(addr + 1, nls_dbc_set_table & 0xF);
            put16(addr + 3, nls_dbc_set_table >> 4);
            cpuSetCX(5);
            break;
        default:
            cpuSetAX(1);
            cpuSetFlag(cpuFlag_CF);
            break;
        }
        break;
    case 0x66: // GET GLOBAL CODE PAGE
        cpuSetBX(437);
        cpuSetDX(437);
        cpuClrFlag(cpuFlag_CF);
        break;
    case 0x67: // SET HANDLE COUNT
        cpuClrFlag(cpuFlag_CF);
        break;
    }
    default:
        debug(debug_dos, "UNHANDLED INT 21, AX=%04x\n", cpuGetAX());
        debug(debug_int, "UNHANDLED INT 21, AX=%04x\n", cpuGetAX());
        cpuSetFlag(cpuFlag_CF);
        cpuSetAX(ax & 0xFF00);
    }
}

// DOS int 22 - TERMINATE ADDRESS
void int22()
{
    debug(debug_dos, "D-22: TERMINATE HANDLER CALLED\n");
    // If we reached here, we must terminate now
    exit(return_code & 0xFF);
}

static char *addstr(char *dst, const char *src, int limit)
{
    while(limit > 0 && *src)
    {
        *dst = *src;
        dst++;
        src++;
        limit--;
    }
    return dst;
}

// Initializes all NLS data for INT 21/38 and INT 21/65
static void init_nls_data(void)
{
    static const uint8_t uppercase_table[128] = {
        0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F,
        0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
        0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
        0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
        0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
        0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
        0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
        0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,
    };
    static const uint8_t collating_table[256] = {
        0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
        0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
        0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
        0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
        0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
        0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
        0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
        0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x7b,0x7c,0x7d,0x7e,0x7f,
        0x43,0x55,0x45,0x41,0x41,0x41,0x41,0x43,0x45,0x45,0x45,0x49,0x49,0x49,0x41,0x41,
        0x45,0x41,0x41,0x4f,0x4f,0x4f,0x55,0x55,0x59,0x4f,0x55,0x24,0x24,0x24,0x24,0x24,
        0x41,0x49,0x4f,0x55,0x4e,0x4e,0xa6,0xa7,0x3f,0xa9,0xaa,0xab,0xac,0x21,0x22,0x22,
        0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
        0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
        0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
        0xe0,0x53,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
        0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
    };
    static const uint8_t terminator_table[24] = {
        0x16, 0x00, // size of table = 22 bytes
        0x01,       // ???
        0x00,       // lowest char in filename
        0xFF,       // highest char in filename
        0x00,       // ???
        0x00,       // first excluded char
        0x20,       // last excluded char (all from 00-20 excluded)
        0x02,       // ???
        0x0E,       // number of terminator characters
        0x2E, 0x22, 0x2F, 0x5C, 0x5B, 0x5D, 0x3A,
        0x7C, 0x3C, 0x3E, 0x2B, 0x3D, 0x3B, 0x2C};
    static const uint8_t fn_uppercase[16] = {
        0x3C, 0x80,       //     CMP    AL,80
        0x72, 0x0B,       //     JB     xit
        0x53,             //     PUSH   BX
        0x30, 0xFF,       //     XOR    BH,BH
        0x88, 0xC3,       //     MOV    BL,AL
        0x2E,             //     CS:
        0x8A, 0x87, 2, 0, //     MOV    AL,[BX+0002] ; offset of uppercase table
        0x5B,             //     POP    BX
        0xCB              // xit: RETF
    };
    static uint8_t country_info[34] = {
        1, 0,            // Date format
        '$', 0, 0, 0, 0, // Currency symbol string
        ',', 0,          // Thousands separator
        '.', 0,          // Decimal separator
        '-', 0,          // Date separator
        ':', 0,          // Time separator
        0,               // Currency format
        2,               // Digits after decimal in currency
        0,               // Time format
        0, 0, 0, 0,      // Uppercase function address - patched in code
        ',', 0,          // Data list separator
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    };

    // Uppercase table
    nls_uppercase_table = get_static_memory(128 + 2 + 16, 0);
    put16(nls_uppercase_table, 128); // Length
    putmem(nls_uppercase_table + 2, uppercase_table, 128);
    // Uppercase function
    uint16_t fn_ucase_seg = nls_uppercase_table >> 4;
    uint16_t fn_ucase_off = (nls_uppercase_table & 0xF) + 128 + 2;
    putmem(nls_uppercase_table + 128 + 2, fn_uppercase, 16);

    // Country info
    country_info[18] = fn_ucase_off & 0xFF;
    country_info[19] = fn_ucase_off >> 8;
    country_info[20] = fn_ucase_seg & 0xFF;
    country_info[21] = fn_ucase_seg >> 8;
    nls_country_info = country_info;

    // Terminators table
    nls_terminator_table = get_static_memory(24, 0);
    putmem(nls_terminator_table, terminator_table, 24);

    // Collating table
    nls_collating_table = get_static_memory(256 + 2, 0);
    put16(nls_collating_table, 256); // Length
    putmem(nls_collating_table + 2, collating_table, 256);

    // Double-byte-chars table
    nls_dbc_set_table = get_static_memory(4, 0);
    put16(nls_dbc_set_table, 0); // Length
    put16(nls_dbc_set_table, 0); // one entry at least.
}

void init_dos(int argc, char **argv)
{
    char args[256], environ[4096];
    memset(args, 0, 256);
    memset(environ, 0, sizeof(environ));

    init_handles();
    init_codepage();
    init_nls_data();

    // Init INTERRUPT handlers - point to our own handlers
    for(int i = 0; i < 256; i++)
    {
        memory[i * 4 + 0] = i;
        memory[i * 4 + 1] = 0;
        memory[i * 4 + 2] = 0;
        memory[i * 4 + 3] = 0;
    }

    // Patch an INT 21 at address 0x000C0, this is for the CP/M emulation code
    memory[0x000C0] = 0xCD;
    memory[0x000C1] = 0x21;

    // Init memory handling - available start address at 0x800,
    // ending address at 0xA0000.
    mcb_init(0x80, 0xA000);

    // Init SYSVARS
    dos_sysvars = get_static_memory(128, 0);
    put16(dos_sysvars + 22, 0x0080); // First MCB

    // Setup default drive
    if(getenv(ENV_DEF_DRIVE))
    {
        char c = getenv(ENV_DEF_DRIVE)[0];
        c = (c >= 'a') ? c - 'a' : c - 'A';
        if(c >= 0 && c <= 25)
        {
            dos_set_default_drive(c);
            debug(debug_dos, "set default drive = '%c'\n", c + 'A');
        }
    }

    // Setup CWD
    if(getenv(ENV_CWD))
    {
        char path[64];
        memset(path, 0, 64);
        strncpy(path, getenv(ENV_CWD), 63);
        dos_change_cwd(path);
    }
    else
    {
        // No CWD given, translate from base path of default drive
        char *cwd = dos_real_path(dos_get_default_drive(), ".");
        if(cwd)
        {
            dos_change_cwd(cwd);
            free(cwd);
        }
        else
            debug(debug_dos, "\tWARNING: working directory outside default drive\n");
    }

    // Concat rest of arguments
    int i;
    char *p = args;
    for(i = 1; i < argc && strcmp(argv[i], "--"); i++)
    {
        if(i != 1)
            p = addstr(p, " ", 127 - (p - args));
        p = addstr(p, argv[i], 127 - (p - args));
    }
    *p = 0;

    // Copy environment
    int have_path = 0;
    p = environ;
    for(i++; i < argc; i++)
    {
        if(!strncmp("PATH=", argv[i], 5) || !strcmp("PATH", argv[i]))
            have_path = 1;
        p = addstr(p, argv[i], environ + sizeof(environ) - 2 - p);
        *p = 0;
        p++;
    }
    if(!have_path)
    {
        // Adds a PATH variable.
        p = addstr(p, "PATH=C:\\", environ + sizeof(environ) - 2 - p);
        *p = 0;
        p++;
    }

    const char *progname = getenv(ENV_PROGNAME);
    if(!progname)
    {
        progname = dos_real_path(dos_get_default_drive(), argv[0]);
        if(!progname)
            progname = argv[0];
    }

    // Create main PSP
    int psp_mcb = create_PSP(args, environ, p - environ + 1, progname);

    // Load program
    const char *name = argv[0];
    FILE *f = fopen(name, "rb");
    if(!f)
        print_error("can't open '%s': %s\n", name, strerror(errno));
    if(!dos_load_exe(f, psp_mcb))
        print_error("error loading EXE/COM file.\n");
    fclose(f);

    // Init DTA
    dosDTA = get_current_PSP() * 16 + 0x80;

    // Init DOS flags
    cpuSetStartupFlag(cpuFlag_IF);
    cpuClrStartupFlag(cpuFlag_DF);
    cpuClrStartupFlag(cpuFlag_TF);
}

void int28(void)
{
    usleep(1); // TODO: process messages?
}
Un proyecto texto-plano.xyz