-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmsdos.cpp
6831 lines (6244 loc) · 168 KB
/
msdos.cpp
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
/*
MS-DOS Player for Win32 console
Author : Takeda.Toshiya
Date : 2009.11.09-
*/
#include "msdos.h"
#include "win16.h"
#define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
#define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
#define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
#define fatalerror(...) { \
fprintf(stderr, __VA_ARGS__); \
exit(1); \
}
#define error(...) fprintf(stderr, "error: " __VA_ARGS__)
#if defined(__MINGW32__)
extern "C" int _CRT_glob = 0;
#endif
/*
kludge for "more-standardized" C++
*/
#if !defined(_MSC_VER)
inline int kludge_min(int a, int b) { return (a<b ? a:b); }
inline int kludge_max(int a, int b) { return (a>b ? a:b); }
#define min(a,b) kludge_min(a,b)
#define max(a,b) kludge_max(a,b)
#endif
void change_console_size_to_80x25();
/* ----------------------------------------------------------------------------
MAME i86/i386
---------------------------------------------------------------------------- */
//#define SUPPORT_DISASSEMBLER
#if defined(HAS_I86)
#define CPU_MODEL i8086
#elif defined(HAS_I186)
#define CPU_MODEL i80186
#elif defined(HAS_I286)
#define CPU_MODEL i80286
#elif defined(HAS_I386)
#define CPU_MODEL i386
#else
#if defined(HAS_I386SX)
#define CPU_MODEL i386SX
#else
#if defined(HAS_I486)
#define CPU_MODEL i486
#else
#if defined(HAS_PENTIUM)
#define CPU_MODEL pentium
#elif defined(HAS_MEDIAGX)
#define CPU_MODEL mediagx
#elif defined(HAS_PENTIUM_PRO)
#define CPU_MODEL pentium_pro
#elif defined(HAS_PENTIUM_MMX)
#define CPU_MODEL pentium_mmx
#elif defined(HAS_PENTIUM2)
#define CPU_MODEL pentium2
#elif defined(HAS_PENTIUM3)
#define CPU_MODEL pentium3
#elif defined(HAS_PENTIUM4)
#define CPU_MODEL pentium4
#endif
#define SUPPORT_RDTSC
#endif
#define SUPPORT_FPU
#endif
#define HAS_I386
#endif
#ifndef __BIG_ENDIAN__
#define LSB_FIRST
#endif
#ifndef INLINE
#define INLINE inline
#endif
#define U64(v) UINT64(v)
//#define logerror(...) fprintf(stderr, __VA_ARGS__)
#define logerror(...)
//#define popmessage(...) fprintf(stderr, __VA_ARGS__)
#define popmessage(...)
/*****************************************************************************/
/* src/emu/devcpu.h */
// CPU interface functions
#define CPU_INIT_NAME(name) cpu_init_##name
#define CPU_INIT(name) void CPU_INIT_NAME(name)()
#define CPU_INIT_CALL(name) CPU_INIT_NAME(name)()
#define CPU_RESET_NAME(name) cpu_reset_##name
#define CPU_RESET(name) void CPU_RESET_NAME(name)()
#define CPU_RESET_CALL(name) CPU_RESET_NAME(name)()
#define CPU_EXECUTE_NAME(name) cpu_execute_##name
#define CPU_EXECUTE(name) void CPU_EXECUTE_NAME(name)()
#define CPU_EXECUTE_CALL(name) CPU_EXECUTE_NAME(name)()
#define CPU_TRANSLATE_NAME(name) cpu_translate_##name
#define CPU_TRANSLATE(name) int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
#define CPU_TRANSLATE_CALL(name) CPU_TRANSLATE_NAME(name)(space, intention, address)
#define CPU_DISASSEMBLE_NAME(name) cpu_disassemble_##name
#define CPU_DISASSEMBLE(name) int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
#define CPU_DISASSEMBLE_CALL(name) CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
/*****************************************************************************/
/* src/emu/didisasm.h */
// Disassembler constants
const UINT32 DASMFLAG_SUPPORTED = 0x80000000; // are disassembly flags supported?
const UINT32 DASMFLAG_STEP_OUT = 0x40000000; // this instruction should be the end of a step out sequence
const UINT32 DASMFLAG_STEP_OVER = 0x20000000; // this instruction should be stepped over by setting a breakpoint afterwards
const UINT32 DASMFLAG_OVERINSTMASK = 0x18000000; // number of extra instructions to skip when stepping over
const UINT32 DASMFLAG_OVERINSTSHIFT = 27; // bits to shift after masking to get the value
const UINT32 DASMFLAG_LENGTHMASK = 0x0000ffff; // the low 16-bits contain the actual length
/*****************************************************************************/
/* src/emu/diexec.h */
// I/O line states
enum line_state
{
CLEAR_LINE = 0, // clear (a fired or held) line
ASSERT_LINE, // assert an interrupt immediately
HOLD_LINE, // hold interrupt line until acknowledged
PULSE_LINE // pulse interrupt line instantaneously (only for NMI, RESET)
};
// I/O line definitions
enum
{
INPUT_LINE_IRQ = 0,
INPUT_LINE_NMI
};
/*****************************************************************************/
/* src/emu/dimemory.h */
// Translation intentions
const int TRANSLATE_TYPE_MASK = 0x03; // read write or fetch
const int TRANSLATE_USER_MASK = 0x04; // user mode or fully privileged
const int TRANSLATE_DEBUG_MASK = 0x08; // debug mode (no side effects)
const int TRANSLATE_READ = 0; // translate for read
const int TRANSLATE_WRITE = 1; // translate for write
const int TRANSLATE_FETCH = 2; // translate for instruction fetch
const int TRANSLATE_READ_USER = (TRANSLATE_READ | TRANSLATE_USER_MASK);
const int TRANSLATE_WRITE_USER = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
const int TRANSLATE_FETCH_USER = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
const int TRANSLATE_READ_DEBUG = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
const int TRANSLATE_WRITE_DEBUG = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
const int TRANSLATE_FETCH_DEBUG = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
/*****************************************************************************/
/* src/emu/emucore.h */
// constants for expression endianness
enum endianness_t
{
ENDIANNESS_LITTLE,
ENDIANNESS_BIG
};
// declare native endianness to be one or the other
#ifdef LSB_FIRST
const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
#else
const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
#endif
// endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
#define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
// endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
#define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
// endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
#define ENDIAN_VALUE_NE_NNE(endian,leval,beval) (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
/*****************************************************************************/
/* src/emu/memory.h */
// address spaces
enum address_spacenum
{
AS_0, // first address space
AS_1, // second address space
AS_2, // third address space
AS_3, // fourth address space
ADDRESS_SPACES, // maximum number of address spaces
// alternate address space names for common use
AS_PROGRAM = AS_0, // program address space
AS_DATA = AS_1, // data address space
AS_IO = AS_2 // I/O address space
};
// offsets and addresses are 32-bit (for now...)
typedef UINT32 offs_t;
// read accessors
UINT8 read_byte(offs_t byteaddress)
{
#if defined(HAS_I386)
if(byteaddress < MAX_MEM) {
return mem[byteaddress];
// } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
// return read_byte(byteaddress & 0xfffff);
}
return 0;
#else
return mem[byteaddress];
#endif
}
UINT16 read_word(offs_t byteaddress)
{
#if defined(HAS_I386)
if(byteaddress < MAX_MEM - 1) {
return *(UINT16 *)(mem + byteaddress);
// } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
// return read_word(byteaddress & 0xfffff);
}
return 0;
#else
return *(UINT16 *)(mem + byteaddress);
#endif
}
UINT32 read_dword(offs_t byteaddress)
{
#if defined(HAS_I386)
if(byteaddress < MAX_MEM - 3) {
return *(UINT32 *)(mem + byteaddress);
// } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
// return read_dword(byteaddress & 0xfffff);
}
return 0;
#else
return *(UINT32 *)(mem + byteaddress);
#endif
}
// write accessors
void write_text_vram_byte(offs_t offset, UINT8 data)
{
// XXX: we need to consider a multi-byte character
COORD co;
DWORD num;
co.X = (offset >> 1) % 80;
co.Y = (offset >> 1) / 80;
if(offset & 1) {
scr_attr[0] = data;
WriteConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
} else {
scr_char[0] = data;
WriteConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
}
}
void write_text_vram_word(offs_t offset, UINT16 data)
{
// XXX: we need to consider a multi-byte character
if(offset & 1) {
// Attr, Char
write_text_vram_byte(offset , (data ) & 0xff);
write_text_vram_byte(offset + 1, (data >> 8) & 0xff);
} else {
// Char, Attr
COORD co;
DWORD num;
co.X = (offset >> 1) % 80;
co.Y = (offset >> 1) / 80;
scr_char[0] = (data ) & 0xff;
scr_attr[0] = (data >> 8) & 0xff;
WriteConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
WriteConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
}
}
void write_text_vram_dword(offs_t offset, UINT32 data)
{
// XXX: we need to consider a multi-byte character
if(offset & 1) {
// Attr, Char, Attr, Char
write_text_vram_byte(offset , (data ) & 0x00ff);
write_text_vram_word(offset + 1, (data >> 8) & 0xffff);
write_text_vram_byte(offset + 3, (data >> 24) & 0x00ff);
} else {
// Char, Attr, Char, Attr
COORD co;
DWORD num;
co.X = (offset >> 1) % 80;
co.Y = (offset >> 1) / 80;
scr_char[0] = (data ) & 0xff;
scr_attr[0] = (data >> 8) & 0xff;
scr_char[1] = (data >> 16) & 0xff;
scr_attr[1] = (data >> 24) & 0xff;
WriteConsoleOutputCharacter(hStdout, scr_char, 2, co, &num);
WriteConsoleOutputAttribute(hStdout, scr_attr, 2, co, &num);
}
}
void write_byte(offs_t byteaddress, UINT8 data)
{
if(byteaddress < MEMORY_END) {
mem[byteaddress] = data;
} else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
if(!restore_console_on_exit && (scr_width != 80 || scr_height != 25)) {
change_console_size_to_80x25();
restore_console_on_exit = true;
}
write_text_vram_byte(byteaddress - text_vram_top_address, data);
mem[byteaddress] = data;
} else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
if(int_10h_feh_called && !int_10h_ffh_called) {
write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
}
mem[byteaddress] = data;
#if defined(HAS_I386)
} else if(byteaddress < MAX_MEM) {
#else
} else {
#endif
mem[byteaddress] = data;
}
}
void write_word(offs_t byteaddress, UINT16 data)
{
if(byteaddress < MEMORY_END) {
*(UINT16 *)(mem + byteaddress) = data;
} else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
if(!restore_console_on_exit && (scr_width != 80 || scr_height != 25)) {
change_console_size_to_80x25();
restore_console_on_exit = true;
}
write_text_vram_word(byteaddress - text_vram_top_address, data);
*(UINT16 *)(mem + byteaddress) = data;
} else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
if(int_10h_feh_called && !int_10h_ffh_called) {
write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
}
*(UINT16 *)(mem + byteaddress) = data;
#if defined(HAS_I386)
} else if(byteaddress < MAX_MEM - 1) {
#else
} else {
#endif
*(UINT16 *)(mem + byteaddress) = data;
}
}
void write_dword(offs_t byteaddress, UINT32 data)
{
if(byteaddress < MEMORY_END) {
*(UINT32 *)(mem + byteaddress) = data;
} else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
if(!restore_console_on_exit && (scr_width != 80 || scr_height != 25)) {
change_console_size_to_80x25();
restore_console_on_exit = true;
}
write_text_vram_dword(byteaddress - text_vram_top_address, data);
*(UINT32 *)(mem + byteaddress) = data;
} else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
if(int_10h_feh_called && !int_10h_ffh_called) {
write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
}
*(UINT32 *)(mem + byteaddress) = data;
#if defined(HAS_I386)
} else if(byteaddress < MAX_MEM - 3) {
#else
} else {
#endif
*(UINT32 *)(mem + byteaddress) = data;
}
}
#define read_decrypted_byte read_byte
#define read_decrypted_word read_word
#define read_decrypted_dword read_dword
#define read_raw_byte read_byte
#define write_raw_byte write_byte
#define read_word_unaligned read_word
#define write_word_unaligned write_word
#define read_io_word_unaligned read_io_word
#define write_io_word_unaligned write_io_word
UINT8 read_io_byte(offs_t byteaddress);
UINT16 read_io_word(offs_t byteaddress);
UINT32 read_io_dword(offs_t byteaddress);
void write_io_byte(offs_t byteaddress, UINT8 data);
void write_io_word(offs_t byteaddress, UINT16 data);
void write_io_dword(offs_t byteaddress, UINT32 data);
/*****************************************************************************/
/* src/osd/osdcomm.h */
/* Highly useful macro for compile-time knowledge of an array size */
#define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))
#if defined(HAS_I386)
static CPU_TRANSLATE(i386);
#include "mame/lib/softfloat/softfloat.c"
#include "mame/emu/cpu/i386/i386.c"
#include "mame/emu/cpu/vtlb.c"
#elif defined(HAS_I286)
#include "mame/emu/cpu/i86/i286.c"
#else
#include "mame/emu/cpu/i86/i86.c"
#endif
#ifdef SUPPORT_DISASSEMBLER
#include "mame/emu/cpu/i386/i386dasm.c"
bool dasm = false;
#endif
#if defined(HAS_I386)
#define SREG(x) m_sreg[x].selector
#define SREG_BASE(x) m_sreg[x].base
int cpu_type, cpu_step;
#else
#define REG8(x) m_regs.b[x]
#define REG16(x) m_regs.w[x]
#define SREG(x) m_sregs[x]
#define SREG_BASE(x) m_base[x]
#define m_CF m_CarryVal
#define m_a20_mask AMASK
#define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
#if defined(HAS_I286)
#define i386_set_a20_line(x) i80286_set_a20_line(x)
#else
#define i386_set_a20_line(x)
#endif
#define i386_set_irq_line(x, y) set_irq_line(x, y)
#endif
void i386_jmp_far(UINT16 selector, UINT32 address)
{
#if defined(HAS_I386)
if(PROTECTED_MODE && !V8086_MODE) {
i386_protected_mode_jump(selector, address, 1, m_operand_size);
} else {
SREG(CS) = selector;
m_performed_intersegment_jump = 1;
i386_load_segment_descriptor(CS);
m_eip = address;
CHANGE_PC(m_eip);
}
#elif defined(HAS_I286)
i80286_code_descriptor(selector, address, 1);
#else
SREG(CS) = selector;
i386_load_segment_descriptor(CS);
m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
#endif
}
/* ----------------------------------------------------------------------------
main
---------------------------------------------------------------------------- */
bool is_started_from_command_prompt()
{
bool ret = false;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hSnapshot != INVALID_HANDLE_VALUE) {
DWORD dwParentProcessID = 0;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if(Process32First(hSnapshot, &pe32)) {
do {
if(pe32.th32ProcessID == GetCurrentProcessId()) {
dwParentProcessID = pe32.th32ParentProcessID;
break;
}
} while(Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
if(dwParentProcessID != 0) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
if(hProcess != NULL) {
HMODULE hMod;
DWORD cbNeeded;
if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
char module_name[MAX_PATH];
if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
}
}
CloseHandle(hProcess);
}
}
}
return(ret);
}
#define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
int main(int argc, char *argv[], char *envp[])
{
int arg_offset = 0;
int standard_env = 0;
BOOL bSuccess;
for(int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-enablevs") == 0) {//+OTVDM
enable_visualstyle(argv[0]);
arg_offset++;
}else if (_strnicmp(argv[i], "-e", 2) == 0) {
standard_env = 1;
arg_offset++;
} else if(_strnicmp(argv[i], "-v", 2) == 0) {
if(strlen(argv[i]) >= 6 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && IS_NUMERIC(argv[i][5])) {
major_version = argv[i][2] - '0';
minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] - '0');
}
arg_offset++;
}
else {
break;
}
}
if(argc < 2 + arg_offset) {
#ifdef _WIN64
fprintf(stderr, "MS-DOS Player for Win32-x64 console\n\n");
#else
fprintf(stderr, "MS-DOS Player for Win32 console\n\n");
#endif
fprintf(stderr, "Usage: MSDOS [-e] [-vX.XX] (command file) [opions]\n");
if(!is_started_from_command_prompt()) {
fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
while(!_kbhit()) {
Sleep(10);
}
}
return(EXIT_FAILURE);
}
CONSOLE_SCREEN_BUFFER_INFO csbi;
hStdin = GetStdHandle(STD_INPUT_HANDLE);
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
bSuccess = GetConsoleScreenBufferInfo(hStdout, &csbi);
for(int y = 0; y < SCR_BUF_SIZE; y++) {
for(int x = 0; x < 80; x++) {
scr_buf[y][x].Char.AsciiChar = ' ';
scr_buf[y][x].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
}
}
scr_buf_size.X = 80;
scr_buf_size.Y = SCR_BUF_SIZE;
scr_buf_pos.X = scr_buf_pos.Y = 0;
if(bSuccess) {
scr_width = csbi.dwSize.X;
scr_height = csbi.dwSize.Y;
} else {
// for a proof (not a console)
scr_width = 80;
scr_height = 25;
}
cursor_moved = false;
key_buf_char = new FIFO();
key_buf_scan = new FIFO();
hardware_init();
if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
retval = EXIT_FAILURE;
} else {
TIMECAPS caps;
timeGetDevCaps(&caps, sizeof(TIMECAPS));
timeBeginPeriod(caps.wPeriodMin);
hardware_run();
if (!is_started_from_command_prompt())
{
printf("\nStart this program from a command prompt!\n\nHit any key to quit...\n");
while (!_kbhit()) {
Sleep(10);
}
}
if(bSuccess) {
if (restore_console_on_exit) {
SMALL_RECT rect = {0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top};
SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
SetConsoleWindowInfo(hStdout, TRUE, &rect);
}
SetConsoleTextAttribute(hStdout, csbi.wAttributes); // hStdout (and all handles) will close in msdos_finish()...
}
msdos_finish();
timeEndPeriod(caps.wPeriodMin);
}
hardware_finish();
delete key_buf_char;
delete key_buf_scan;
// SetConsoleTextAttribute(hStdout, csbi.wAttributes);
return(retval);
}
void change_console_size_to_80x25()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
SMALL_RECT rect;
COORD co;
GetConsoleScreenBufferInfo(hStdout, &csbi);
if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > 24) {
if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == 80 && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == 25) {
ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &csbi.srWindow);
SET_RECT(rect, 0, 0, 79, 24);
WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
} else if(csbi.dwCursorPosition.Y > 24) {
SET_RECT(rect, 0, csbi.dwCursorPosition.Y - 24, 79, csbi.dwCursorPosition.Y);
ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
SET_RECT(rect, 0, 0, 79, 24);
WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
}
}
if(csbi.dwCursorPosition.Y > 24) {
co.X = csbi.dwCursorPosition.X;
co.Y = min(24, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
SetConsoleCursorPosition(hStdout, co);
cursor_moved = true;
}
SET_RECT(rect, 0, 0, 79, 24);
co.X = 80;
co.Y = 25;
SetConsoleWindowInfo(hStdout, TRUE, &rect);
SetConsoleScreenBufferSize(hStdout, co);
scr_width = 80;
scr_height = 25;
}
/* ----------------------------------------------------------------------------
MS-DOS virtual machine
---------------------------------------------------------------------------- */
void update_key_buffer_tmp()
{
DWORD dwNumberOfEvents = 0;
DWORD dwRead;
INPUT_RECORD ir[16];
if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
for(int i = 0; i < dwRead; i++) {
if((ir[i].EventType & KEY_EVENT) && ir[i].Event.KeyEvent.bKeyDown) {
if(ir[i].Event.KeyEvent.uChar.AsciiChar == 0) {
// ignore shift, ctrl and alt keys
if(ir[i].Event.KeyEvent.wVirtualScanCode != 0x1d &&
ir[i].Event.KeyEvent.wVirtualScanCode != 0x2a &&
ir[i].Event.KeyEvent.wVirtualScanCode != 0x36 &&
ir[i].Event.KeyEvent.wVirtualScanCode != 0x38) {
key_buf_char->write(0x00);
key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
key_buf_char->write(0x00);
key_buf_scan->write(ir[i].Event.KeyEvent.wVirtualScanCode & 0xff);
}
} else {
key_buf_char->write(ir[i].Event.KeyEvent.uChar.AsciiChar & 0xff);
key_buf_scan->write(ir[i].Event.KeyEvent.wVirtualScanCode & 0xff);
}
}
}
}
}
}
void update_key_buffer()
{
int prev_count = key_buf_char->count();
update_key_buffer_tmp();
key_input += key_buf_char->count() - prev_count;
if(key_buf_char->count() == 0) {
Sleep(10);
}
}
int check_key_input()
{
if(key_input == 0) {
int prev_count = key_buf_char->count();
update_key_buffer_tmp();
key_input = key_buf_char->count() - prev_count;
}
int val = key_input;
key_input = 0;
return(val);
}
// process info
process_t *msdos_process_info_create(UINT16 psp_seg)
{
for(int i = 0; i < MAX_PROCESS; i++) {
if(process[i].psp == 0 || process[i].psp == psp_seg) {
memset(&process[i], 0, sizeof(process_t));
process[i].psp = psp_seg;
return(&process[i]);
}
}
fatalerror("too many processes\n");
return(NULL);
}
process_t *msdos_process_info_get(UINT16 psp_seg)
{
for(int i = 0; i < MAX_PROCESS; i++) {
if(process[i].psp == psp_seg) {
return(&process[i]);
}
}
fatalerror("invalid psp address\n");
return(NULL);
}
void msdos_cds_update(int drv)
{
cds_t *cds = (cds_t *)(mem + CDS_TOP);
memset(mem + CDS_TOP, 0, CDS_SIZE);
sprintf(cds->path_name, "%c:\\", 'A' + drv);
cds->drive_attrib = 0x4000; // physical drive
cds->physical_drive_number = drv;
}
// dbcs
void msdos_dbcs_table_update()
{
UINT8 dbcs_data[DBCS_SIZE];
memset(dbcs_data, 0, sizeof(dbcs_data));
CPINFO info;
GetCPInfo(active_code_page, &info);
if(info.MaxCharSize != 1) {
for(int i = 0;; i += 2) {
UINT8 lo = info.LeadByte[i + 0];
UINT8 hi = info.LeadByte[i + 1];
dbcs_data[2 + i + 0] = lo;
dbcs_data[2 + i + 1] = hi;
if(lo == 0 && hi == 0) {
dbcs_data[0] = i + 2;
break;
}
}
} else {
dbcs_data[0] = 2; // ???
}
memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
}
void msdos_dbcs_table_init()
{
system_code_page = active_code_page = _getmbcp();
msdos_dbcs_table_update();
}
void msdos_dbcs_table_finish()
{
if(active_code_page != system_code_page) {
_setmbcp(system_code_page);
}
}
int msdos_lead_byte_check(UINT8 code)
{
UINT8 *dbcs_table = mem + DBCS_TABLE;
for(int i = 0;; i += 2) {
UINT8 lo = dbcs_table[i + 0];
UINT8 hi = dbcs_table[i + 1];
if(lo == 0 && hi == 0) {
break;
}
if(lo <= code && code <= hi) {
return(1);
}
}
return(0);
}
// file control
char *msdos_trimmed_path(char *path, int lfn)
{
static char tmp[MAX_PATH];
if(lfn) {
strcpy(tmp, path);
} else {
// remove space in the path
char *src = path, *dst = tmp;
while(*src != '\0') {
if(msdos_lead_byte_check(*src)) {
*dst++ = *src++;
*dst++ = *src++;
} else if(*src != ' ') {
*dst++ = *src++;
} else {
src++; // skip space
}
}
*dst = '\0';
}
return(tmp);
}
bool match(char *text, char *pattern)
{
//http://www.prefield.com/algorithm/string/wildcard.html
switch (*pattern) {
case '\0':
return !*text;
case '*':
return match(text, pattern + 1) || *text && match(text + 1, pattern);
case '?':
return *text && match(text + 1, pattern + 1);
default:
return (*text == *pattern) && match(text + 1, pattern + 1);
}
}
bool msdos_match_volume_label(char *path, char *volume)
{
char *p;
if((p = my_strchr(path, ':')) != NULL) {
return msdos_match_volume_label(p + 1, volume);
} else if((p = my_strchr(path, '\\')) != NULL) {
return msdos_match_volume_label(p + 1, volume);
} else if((p = my_strchr(path, '.')) != NULL) {
*p = '\0';
bool result = match(volume, path);
*p = '.';
return result;
} else {
return match(volume, path);
}
}
char *msdos_fcb_path(fcb_t *fcb)
{
static char tmp[MAX_PATH];
char name[9], ext[4];
memset(name, 0, sizeof(name));
memcpy(name, fcb->file_name, 8);
strcpy(name, msdos_trimmed_path(name, 0));
memset(ext, 0, sizeof(ext));
memcpy(ext, fcb->file_name + 8, 3);
strcpy(ext, msdos_trimmed_path(ext, 0));
if(name[0] == '\0' || strcmp(name, "????????") == 0) {
strcpy(name, "*");
}
if(ext[0] == '\0') {
strcpy(tmp, name);
} else {
if(strcmp(ext, "???") == 0) {
strcpy(ext, "*");
}
sprintf(tmp, "%s.%s", name, ext);
}
return(tmp);
}
void msdos_set_fcb_path(fcb_t *fcb, char *path)
{
char *ext = my_strchr(path, '.');
memset(fcb->file_name, 0x20, 8 + 3);
if(ext != NULL && path[0] != '.') {
*ext = '\0';
memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
}
memcpy(fcb->file_name, path, strlen(path));
}
char *msdos_short_path(char *path)
{
static char tmp[MAX_PATH];
GetShortPathName(path, tmp, MAX_PATH);
my_strupr(tmp);
return(tmp);
}
char *msdos_short_full_path(char *path)
{
static char tmp[MAX_PATH];
char full[MAX_PATH], *name;
GetFullPathName(path, MAX_PATH, full, &name);
GetShortPathName(full, tmp, MAX_PATH);
my_strupr(tmp);
return(tmp);
}
char *msdos_short_full_dir(char *path)
{
static char tmp[MAX_PATH];
char full[MAX_PATH], *name;
GetFullPathName(path, MAX_PATH, full, &name);
name[-1] = '\0';
GetShortPathName(full, tmp, MAX_PATH);
my_strupr(tmp);
return(tmp);
}
char *msdos_local_file_path(char *path, int lfn)
{
char *trimmed = msdos_trimmed_path(path, lfn);
if(_access(trimmed, 0) != 0) {
process_t *process = msdos_process_info_get(current_psp);
static char tmp[MAX_PATH];
sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
if(_access(tmp, 0) == 0) {
return(tmp);
}
}
return(trimmed);
}
bool msdos_is_con_path(char *path)
{
char full[MAX_PATH], *name;
GetFullPathName(path, MAX_PATH, full, &name);
return(_stricmp(full, "\\\\.\\CON") == 0);
}
char *msdos_remove_double_quote(char *path)
{
static char tmp[MAX_PATH];
memset(tmp, 0, sizeof(tmp));
if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
memcpy(tmp, path + 1, strlen(path) - 2);
} else {
sprintf(tmp, path);
}
return(tmp);
}
char *msdos_combine_path(char *dir, char *file)
{
static char tmp[MAX_PATH];
char *tmp_dir = msdos_remove_double_quote(dir);
if(strlen(tmp_dir) == 0) {
strcpy(tmp, file);
} else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
sprintf(tmp, "%s%s", tmp_dir, file);
} else {
sprintf(tmp, "%s\\%s", tmp_dir, file);
}
return(tmp);
}
char *msdos_search_command_com(char *command_path, char *env_path)
{
static char tmp[MAX_PATH];
char path[MAX_PATH], *file_name;