|
|
1.1 root 1: /*
2: MS-DOS Player for Win32 console
3:
4: Author : Takeda.Toshiya
5: Date : 2009.11.09-
6: */
7:
8: #include "msdos.h"
9:
10: #ifdef _MSC_VER
11: #pragma warning( disable : 4018 )
12: #pragma warning( disable : 4065 )
13: #pragma warning( disable : 4146 )
14: #pragma warning( disable : 4244 )
15: #pragma warning( disable : 4267 )
16: #endif
17:
18: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
19: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
20: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
21:
22: #define fatalerror(...) { \
23: fprintf(stderr, __VA_ARGS__); \
24: exit(1); \
25: }
26: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
27:
28: /* ----------------------------------------------------------------------------
1.1.1.3 root 29: MAME i86/i386
1.1 root 30: ---------------------------------------------------------------------------- */
31:
32: //#define SUPPORT_DISASSEMBLER
33:
1.1.1.3 root 34: #if defined(HAS_I386)
35: #define CPU_MODEL i386
36: //#define CPU_MODEL i386SX
37: //#define CPU_MODEL i486
38: //#define CPU_MODEL pentium
39: //#define CPU_MODEL mediagx
40: //#define CPU_MODEL pentium_pro
41: //#define CPU_MODEL pentium_mmx
42: //#define CPU_MODEL pentium2
43: //#define CPU_MODEL pentium3
44: //#define CPU_MODEL pentium4
45: #elif defined(HAS_I286)
46: #define CPU_MODEL i80286
47: #else
48: #define CPU_MODEL i8086
49: //#define CPU_MODEL i80186
50: #endif
1.1 root 51:
52: #define LSB_FIRST
53:
54: #ifndef INLINE
55: #define INLINE inline
56: #endif
57: #define U64(v) UINT64(v)
58:
59: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
60: #define logerror(...)
61: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
62: #define popmessage(...)
63:
64: /*****************************************************************************/
65: /* src/emu/diexec.h */
66:
67: // I/O line states
68: enum line_state
69: {
70: CLEAR_LINE = 0, // clear (a fired or held) line
71: ASSERT_LINE, // assert an interrupt immediately
72: HOLD_LINE, // hold interrupt line until acknowledged
73: PULSE_LINE // pulse interrupt line instantaneously (only for NMI, RESET)
74: };
75:
76: // I/O line definitions
77: enum
78: {
79: INPUT_LINE_IRQ = 0,
80: INPUT_LINE_NMI
81: };
82:
83: /*****************************************************************************/
84: /* src/emu/devcpu.h */
85:
86: // CPU interface functions
87: #define CPU_INIT_NAME(name) cpu_init_##name
1.1.1.3 root 88: #define CPU_INIT(name) void CPU_INIT_NAME(name)()
1.1 root 89: #define CPU_INIT_CALL(name) CPU_INIT_NAME(name)()
90:
91: #define CPU_RESET_NAME(name) cpu_reset_##name
1.1.1.3 root 92: #define CPU_RESET(name) void CPU_RESET_NAME(name)()
93: #define CPU_RESET_CALL(name) CPU_RESET_NAME(name)()
1.1 root 94:
95: #define CPU_EXECUTE_NAME(name) cpu_execute_##name
1.1.1.3 root 96: #define CPU_EXECUTE(name) void CPU_EXECUTE_NAME(name)()
97: #define CPU_EXECUTE_CALL(name) CPU_EXECUTE_NAME(name)()
1.1 root 98:
99: #define CPU_DISASSEMBLE_NAME(name) cpu_disassemble_##name
100: #define CPU_DISASSEMBLE(name) int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
101: #define CPU_DISASSEMBLE_CALL(name) CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
102:
103: /*****************************************************************************/
104: /* src/emu/memory.h */
105:
106: // offsets and addresses are 32-bit (for now...)
107: typedef UINT32 offs_t;
108:
109: // read accessors
110: UINT8 read_byte(offs_t byteaddress)
111: {
1.1.1.4 root 112: #if defined(HAS_I386)
1.1 root 113: if(byteaddress < MAX_MEM) {
114: return mem[byteaddress];
1.1.1.3 root 115: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
116: // return read_byte(byteaddress & 0xfffff);
1.1 root 117: }
118: return 0;
1.1.1.4 root 119: #else
120: return mem[byteaddress];
121: #endif
1.1 root 122: }
123:
124: UINT16 read_word(offs_t byteaddress)
125: {
1.1.1.4 root 126: #if defined(HAS_I386)
1.1 root 127: if(byteaddress < MAX_MEM - 1) {
128: return *(UINT16 *)(mem + byteaddress);
1.1.1.3 root 129: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
130: // return read_word(byteaddress & 0xfffff);
1.1 root 131: }
132: return 0;
1.1.1.4 root 133: #else
134: return *(UINT16 *)(mem + byteaddress);
135: #endif
1.1 root 136: }
137:
138: UINT32 read_dword(offs_t byteaddress)
139: {
1.1.1.4 root 140: #if defined(HAS_I386)
1.1 root 141: if(byteaddress < MAX_MEM - 3) {
142: return *(UINT32 *)(mem + byteaddress);
1.1.1.3 root 143: // } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
144: // return read_dword(byteaddress & 0xfffff);
1.1 root 145: }
146: return 0;
1.1.1.4 root 147: #else
148: return *(UINT32 *)(mem + byteaddress);
149: #endif
1.1 root 150: }
151:
152: // write accessors
153: void write_byte(offs_t byteaddress, UINT8 data)
154: {
1.1.1.3 root 155: if(byteaddress < tvram_top_address) {
156: mem[byteaddress] = data;
157: } else if(byteaddress < tvram_end_address) {
158: if(int_10h_feh_called && !int_10h_ffh_called && mem[byteaddress] != data) {
159: COORD co;
160: DWORD num;
161:
162: co.X = ((byteaddress - tvram_top_address) >> 1) % 80;
163: co.Y = ((byteaddress - tvram_top_address) >> 1) / 80;
164:
165: if(byteaddress & 1) {
166: scr_attr[0] = data;
167: WriteConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
168: } else {
169: scr_char[0] = data;
170: WriteConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
1.1 root 171: }
172: }
173: mem[byteaddress] = data;
1.1.1.4 root 174: #if defined(HAS_I386)
1.1.1.3 root 175: } else if(byteaddress < MAX_MEM) {
1.1.1.4 root 176: #else
177: } else {
178: #endif
1.1.1.3 root 179: mem[byteaddress] = data;
1.1 root 180: }
181: }
182:
183: void write_word(offs_t byteaddress, UINT16 data)
184: {
1.1.1.3 root 185: if(byteaddress < tvram_top_address) {
186: *(UINT16 *)(mem + byteaddress) = data;
187: } else if(byteaddress < tvram_end_address) {
188: if(int_10h_feh_called && !int_10h_ffh_called && *(UINT16 *)(mem + byteaddress) != data) {
189: write_byte(byteaddress , data );
190: write_byte(byteaddress + 1, data >> 8);
191: return;
1.1 root 192: }
193: *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4 root 194: #if defined(HAS_I386)
1.1.1.3 root 195: } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4 root 196: #else
197: } else {
198: #endif
1.1.1.3 root 199: *(UINT16 *)(mem + byteaddress) = data;
1.1 root 200: }
201: }
202:
203: void write_dword(offs_t byteaddress, UINT32 data)
204: {
1.1.1.3 root 205: if(byteaddress < tvram_top_address) {
206: *(UINT32 *)(mem + byteaddress) = data;
207: } else if(byteaddress < tvram_end_address) {
208: if(int_10h_feh_called && !int_10h_ffh_called && *(UINT32 *)(mem + byteaddress) != data) {
209: write_byte(byteaddress , data );
210: write_byte(byteaddress + 1, data >> 8);
211: write_byte(byteaddress + 2, data >> 16);
212: write_byte(byteaddress + 3, data >> 24);
213: return;
1.1 root 214: }
215: *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4 root 216: #if defined(HAS_I386)
1.1.1.3 root 217: } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4 root 218: #else
219: } else {
220: #endif
1.1.1.3 root 221: *(UINT32 *)(mem + byteaddress) = data;
1.1 root 222: }
223: }
224:
225: #define read_decrypted_byte read_byte
226: #define read_decrypted_word read_word
227: #define read_decrypted_dword read_dword
228:
1.1.1.3 root 229: #define read_raw_byte read_byte
230: #define write_raw_byte write_byte
231:
232: #define read_word_unaligned read_word
233: #define write_word_unaligned write_word
234:
235: #define read_io_word_unaligned read_io_word
236: #define write_io_word_unaligned write_io_word
237:
1.1 root 238: UINT8 read_io_byte(offs_t byteaddress);
239: UINT16 read_io_word(offs_t byteaddress);
240: UINT32 read_io_dword(offs_t byteaddress);
241:
242: void write_io_byte(offs_t byteaddress, UINT8 data);
243: void write_io_word(offs_t byteaddress, UINT16 data);
244: void write_io_dword(offs_t byteaddress, UINT32 data);
245:
246: /*****************************************************************************/
247: /* src/emu/emucore.h */
248:
249: // constants for expression endianness
250: enum endianness_t
251: {
252: ENDIANNESS_LITTLE,
253: ENDIANNESS_BIG
254: };
255:
256: // declare native endianness to be one or the other
257: #ifdef LSB_FIRST
258: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
259: #else
260: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
261: #endif
262:
263: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
264: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
265:
266: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
267: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
268:
269: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
270: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval) (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
271:
272: /*****************************************************************************/
273: /* src/emu/didisasm.h */
274:
275: // Disassembler constants
276: const UINT32 DASMFLAG_SUPPORTED = 0x80000000; // are disassembly flags supported?
277: const UINT32 DASMFLAG_STEP_OUT = 0x40000000; // this instruction should be the end of a step out sequence
278: const UINT32 DASMFLAG_STEP_OVER = 0x20000000; // this instruction should be stepped over by setting a breakpoint afterwards
279: const UINT32 DASMFLAG_OVERINSTMASK = 0x18000000; // number of extra instructions to skip when stepping over
280: const UINT32 DASMFLAG_OVERINSTSHIFT = 27; // bits to shift after masking to get the value
281: const UINT32 DASMFLAG_LENGTHMASK = 0x0000ffff; // the low 16-bits contain the actual length
282:
283: /*****************************************************************************/
284: /* src/osd/osdcomm.h */
285:
286: /* Highly useful macro for compile-time knowledge of an array size */
287: #define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))
288:
1.1.1.3 root 289: #if defined(HAS_I386)
290: #include "softfloat/softfloat.c"
291: #include "i386/i386.c"
292: #elif defined(HAS_I286)
293: #include "i86/i286.c"
294: #else
295: #include "i86/i86.c"
296: #endif
1.1 root 297: #ifdef SUPPORT_DISASSEMBLER
1.1.1.3 root 298: #include "i386/i386dasm.c"
299: bool dasm = false;
1.1 root 300: #endif
301:
1.1.1.3 root 302: #if defined(HAS_I386)
303: #define SREG(x) m_sreg[x].selector
304: #define SREG_BASE(x) m_sreg[x].base
305:
306: int cpu_type, cpu_step;
307: #else
308: #define REG8(x) m_regs.b[x]
309: #define REG16(x) m_regs.w[x]
310: #define SREG(x) m_sregs[x]
311: #define SREG_BASE(x) m_base[x]
312: #define m_CF m_CarryVal
313: #define m_a20_mask AMASK
314: #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
315: #if defined(HAS_I286)
316: #define i386_set_a20_line(x) i80286_set_a20_line(x)
317: #else
318: #define i386_set_a20_line(x)
319: #endif
320: #define i386_set_irq_line(x, y) set_irq_line(x, y)
321: #endif
1.1 root 322:
323: void i386_jmp_far(UINT16 selector, UINT32 address)
324: {
1.1.1.3 root 325: #if defined(HAS_I386)
1.1 root 326: if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3 root 327: i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1 root 328: } else {
1.1.1.3 root 329: SREG(CS) = selector;
330: m_performed_intersegment_jump = 1;
331: i386_load_segment_descriptor(CS);
332: m_eip = address;
333: CHANGE_PC(m_eip);
1.1 root 334: }
1.1.1.3 root 335: #elif defined(HAS_I286)
336: i80286_code_descriptor(selector, address, 1);
337: #else
338: SREG(CS) = selector;
339: i386_load_segment_descriptor(CS);
340: m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
341: #endif
1.1 root 342: }
343:
344: /* ----------------------------------------------------------------------------
345: main
346: ---------------------------------------------------------------------------- */
347:
348: int main(int argc, char *argv[], char *envp[])
349: {
350: int standard_env = (argc > 1 && _stricmp(argv[1], "-e") == 0);
351:
352: if(argc < 2 + standard_env) {
353: #ifdef _WIN64
354: fprintf(stderr, "MS-DOS Player for Win32-x64 console\n\n");
355: #else
356: fprintf(stderr, "MS-DOS Player for Win32 console\n\n");
357: #endif
358: fprintf(stderr, "Usage: MSDOS [-e] (command file) [opions]\n");
359: return(EXIT_FAILURE);
360: }
361:
362: CONSOLE_SCREEN_BUFFER_INFO csbi;
363: hStdin = GetStdHandle(STD_INPUT_HANDLE);
364: hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
365: GetConsoleScreenBufferInfo(hStdout, &csbi);
366:
367: for(int y = 0; y < SCR_BUF_SIZE; y++) {
368: for(int x = 0; x < 80; x++) {
369: scr_buf[y][x].Char.AsciiChar = ' ';
370: scr_buf[y][x].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
371: }
372: }
373: scr_buf_size.X = 80;
374: scr_buf_size.Y = SCR_BUF_SIZE;
375: scr_buf_pos.X = scr_buf_pos.Y = 0;
376: scr_width = csbi.dwSize.X;
377: scr_height = csbi.dwSize.Y;
378: cursor_moved = false;
379:
380: key_buf_char = new FIFO();
381: key_buf_scan = new FIFO();
382:
383: hardware_init();
384:
385: if(msdos_init(argc - (standard_env + 1), argv + (standard_env + 1), envp, standard_env)) {
386: retval = EXIT_FAILURE;
387: } else {
388: timeBeginPeriod(1);
389: hardware_run();
390: msdos_finish();
391: timeEndPeriod(1);
392: }
393:
394: delete key_buf_char;
395: delete key_buf_scan;
396:
397: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
398:
399: return(retval);
400: }
401:
402: /* ----------------------------------------------------------------------------
403: MS-DOS virtual machine
404: ---------------------------------------------------------------------------- */
405:
406: void update_key_buffer()
407: {
408: DWORD dwRead;
409: INPUT_RECORD ir[16];
410:
411: if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
412: for(int i = 0; i < dwRead; i++) {
413: if((ir[i].EventType & KEY_EVENT) && ir[i].Event.KeyEvent.bKeyDown) {
414: if(ir[i].Event.KeyEvent.uChar.AsciiChar == 0) {
415: // ignore shift, ctrl and alt keys
416: if(ir[i].Event.KeyEvent.wVirtualScanCode != 0x1d &&
417: ir[i].Event.KeyEvent.wVirtualScanCode != 0x2a &&
418: ir[i].Event.KeyEvent.wVirtualScanCode != 0x36 &&
419: ir[i].Event.KeyEvent.wVirtualScanCode != 0x38) {
420: key_buf_char->write(0x00);
421: key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
422: key_buf_char->write(0x00);
423: key_buf_scan->write(ir[i].Event.KeyEvent.wVirtualScanCode & 0xff);
424: }
425: } else {
426: key_buf_char->write(ir[i].Event.KeyEvent.uChar.AsciiChar & 0xff);
427: key_buf_scan->write(ir[i].Event.KeyEvent.wVirtualScanCode & 0xff);
428: }
429: }
430: }
431: }
432: if(key_buf_char->count() == 0) {
433: Sleep(10);
434: }
435: }
436:
437: // process info
438:
439: process_t *msdos_process_info_create(UINT16 psp_seg)
440: {
441: for(int i = 0; i < MAX_PROCESS; i++) {
442: if(process[i].psp == 0 || process[i].psp == psp_seg) {
443: memset(&process[i], 0, sizeof(process_t));
444: process[i].psp = psp_seg;
445: return(&process[i]);
446: }
447: }
448: fatalerror("too many processes\n");
449: return(NULL);
450: }
451:
452: process_t *msdos_process_info_get(UINT16 psp_seg)
453: {
454: for(int i = 0; i < MAX_PROCESS; i++) {
455: if(process[i].psp == psp_seg) {
456: return(&process[i]);
457: }
458: }
459: fatalerror("invalid psp address\n");
460: return(NULL);
461: }
462:
463: void msdos_cds_update(int drv)
464: {
465: cds_t *cds = (cds_t *)(mem + CDS_TOP);
466:
467: memset(mem + CDS_TOP, 0, CDS_SIZE);
468: sprintf(cds->path_name, "%c:\\", 'A' + drv);
469: cds->drive_attrib = 0x4000; // physical drive
470: cds->physical_drive_number = drv;
471: }
472:
473: // dbcs
474:
475: void msdos_dbcs_table_update()
476: {
477: UINT8 dbcs_data[DBCS_SIZE];
478: memset(dbcs_data, 0, sizeof(dbcs_data));
479:
480: CPINFO info;
481: GetCPInfo(active_code_page, &info);
482:
483: if(info.MaxCharSize != 1) {
484: for(int i = 0;; i += 2) {
485: UINT8 lo = info.LeadByte[i + 0];
486: UINT8 hi = info.LeadByte[i + 1];
487: dbcs_data[2 + i + 0] = lo;
488: dbcs_data[2 + i + 1] = hi;
489: if(lo == 0 && hi == 0) {
490: dbcs_data[0] = i + 2;
491: break;
492: }
493: }
494: } else {
495: dbcs_data[0] = 2; // ???
496: }
497: memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
498: }
499:
500: void msdos_dbcs_table_init()
501: {
502: system_code_page = active_code_page = _getmbcp();
503: msdos_dbcs_table_update();
504: }
505:
506: void msdos_dbcs_table_finish()
507: {
508: if(active_code_page != system_code_page) {
509: _setmbcp(system_code_page);
510: }
511: }
512:
513: int msdos_lead_byte_check(UINT8 code)
514: {
515: UINT8 *dbcs_table = mem + DBCS_TABLE;
516:
517: for(int i = 0;; i += 2) {
518: UINT8 lo = dbcs_table[i + 0];
519: UINT8 hi = dbcs_table[i + 1];
520: if(lo == 0 && hi == 0) {
521: break;
522: }
523: if(lo <= code && code <= hi) {
524: return(1);
525: }
526: }
527: return(0);
528: }
529:
530: // file control
531:
532: char *msdos_trimmed_path(char *path, int lfn)
533: {
534: static char tmp[MAX_PATH];
535:
536: if(lfn) {
537: strcpy(tmp, path);
538: } else {
539: // remove space in the path
540: char *src = path, *dst = tmp;
541:
542: while(*src != '\0') {
543: if(msdos_lead_byte_check(*src)) {
544: *dst++ = *src++;
545: *dst++ = *src++;
546: } else if(*src != ' ') {
547: *dst++ = *src++;
548: } else {
549: src++; // skip space
550: }
551: }
552: *dst = '\0';
553: }
554: return(tmp);
555: }
556:
557: bool match(char *text, char *pattern)
558: {
559: //http://www.prefield.com/algorithm/string/wildcard.html
560: switch (*pattern) {
561: case '\0':
562: return !*text;
563: case '*':
564: return match(text, pattern + 1) || *text && match(text + 1, pattern);
565: case '?':
566: return *text && match(text + 1, pattern + 1);
567: default:
568: return (*text == *pattern) && match(text + 1, pattern + 1);
569: }
570: }
571:
572: bool msdos_match_volume_label(char *path, char *volume)
573: {
574: char *p;
575:
576: if((p = my_strchr(path, ':')) != NULL) {
577: return msdos_match_volume_label(p + 1, volume);
578: } else if((p = my_strchr(path, '\\')) != NULL) {
579: return msdos_match_volume_label(p + 1, volume);
580: } else if((p = my_strchr(path, '.')) != NULL) {
581: *p = '\0';
582: bool result = match(volume, path);
583: *p = '.';
584: return result;
585: } else {
586: return match(volume, path);
587: }
588: }
589:
590: char *msdos_fcb_path(fcb_t *fcb)
591: {
592: static char tmp[MAX_PATH];
593: char name[9], ext[4];
594:
595: memset(name, 0, sizeof(name));
596: memcpy(name, fcb->file_name, 8);
597: strcpy(name, msdos_trimmed_path(name, 0));
598:
599: memset(ext, 0, sizeof(ext));
600: memcpy(ext, fcb->file_name + 8, 3);
601: strcpy(ext, msdos_trimmed_path(ext, 0));
602:
603: if(name[0] == '\0' || strcmp(name, "????????") == 0) {
604: strcpy(name, "*");
605: }
606: if(ext[0] == '\0') {
607: strcpy(tmp, name);
608: } else {
609: if(strcmp(ext, "???") == 0) {
610: strcpy(ext, "*");
611: }
612: sprintf(tmp, "%s.%s", name, ext);
613: }
614: return(tmp);
615: }
616:
617: void msdos_set_fcb_path(fcb_t *fcb, char *path)
618: {
619: char *ext = my_strchr(path, '.');
620:
621: memset(fcb->file_name, 0x20, 8 + 3);
622: if(ext != NULL && path[0] != '.') {
623: *ext = '\0';
624: memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
625: }
626: memcpy(fcb->file_name, path, strlen(path));
627: }
628:
629: char *msdos_short_path(char *path)
630: {
631: static char tmp[MAX_PATH];
632:
633: GetShortPathName(path, tmp, MAX_PATH);
634: my_strupr(tmp);
635: return(tmp);
636: }
637:
638: char *msdos_short_full_path(char *path)
639: {
640: static char tmp[MAX_PATH];
641: char full[MAX_PATH], *name;
642:
643: GetFullPathName(path, MAX_PATH, full, &name);
644: GetShortPathName(full, tmp, MAX_PATH);
645: my_strupr(tmp);
646: return(tmp);
647: }
648:
649: char *msdos_short_full_dir(char *path)
650: {
651: static char tmp[MAX_PATH];
652: char full[MAX_PATH], *name;
653:
654: GetFullPathName(path, MAX_PATH, full, &name);
655: name[-1] = '\0';
656: GetShortPathName(full, tmp, MAX_PATH);
657: my_strupr(tmp);
658: return(tmp);
659: }
660:
661: char *msdos_local_file_path(char *path, int lfn)
662: {
663: char *trimmed = msdos_trimmed_path(path, lfn);
664:
665: if(_access(trimmed, 0) != 0) {
666: process_t *process = msdos_process_info_get(current_psp);
667: static char tmp[MAX_PATH];
668:
669: sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
670: if(_access(tmp, 0) == 0) {
671: return(tmp);
672: }
673: }
674: return(trimmed);
675: }
676:
677: int msdos_drive_number(char *path)
678: {
679: char tmp[MAX_PATH], *name;
680:
681: GetFullPathName(path, MAX_PATH, tmp, &name);
682: if(tmp[0] >= 'a' && tmp[0] <= 'z') {
683: return(tmp[0] - 'a');
684: } else {
685: return(tmp[0] - 'A');
686: }
687: }
688:
689: char *msdos_volume_label(char *path)
690: {
691: static char tmp[MAX_PATH];
692: char volume[] = "A:\\";
693:
694: if(path[1] == ':') {
695: volume[0] = path[0];
696: } else {
697: volume[0] = 'A' + _getdrive() - 1;
698: }
699: if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
700: memset(tmp, 0, sizeof(tmp));
701: }
702: return(tmp);
703: }
704:
705: char *msdos_short_volume_label(char *label)
706: {
707: static char tmp[(8 + 1 + 3) + 1];
708: char *src = label;
709: int remain = strlen(label);
710: char *dst_n = tmp;
711: char *dst_e = tmp + 9;
712:
713: strcpy(tmp, " . ");
714: for(int i = 0; i < 8 && remain > 0; i++) {
715: if(msdos_lead_byte_check(*src)) {
716: if(++i == 8) {
717: break;
718: }
719: *dst_n++ = *src++;
720: remain--;
721: }
722: *dst_n++ = *src++;
723: remain--;
724: }
725: if(remain > 0) {
726: for(int i = 0; i < 3 && remain > 0; i++) {
727: if(msdos_lead_byte_check(*src)) {
728: if(++i == 3) {
729: break;
730: }
731: *dst_e++ = *src++;
732: remain--;
733: }
734: *dst_e++ = *src++;
735: remain--;
736: }
737: *dst_e = '\0';
738: } else {
739: *dst_n = '\0';
740: }
741: my_strupr(tmp);
742: return(tmp);
743: }
744:
745: void msdos_file_handler_open(int fd, char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
746: {
747: static int id = 0;
748: char full[MAX_PATH], *name;
749:
750: if(psp_seg && fd < 20) {
751: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
752: psp->file_table[fd] = fd;
753: }
754: if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
755: strcpy(file_handler[fd].path, full);
756: } else {
757: strcpy(file_handler[fd].path, path);
758: }
759: file_handler[fd].valid = 1;
760: file_handler[fd].id = id++; // dummy id for int 21h ax=71a6h
761: file_handler[fd].atty = atty;
762: file_handler[fd].mode = mode;
763: file_handler[fd].info = info;
764: file_handler[fd].psp = psp_seg;
765: }
766:
767: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
768: {
769: if(psp_seg && dst < 20) {
770: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
771: psp->file_table[dst] = dst;
772: }
773: strcpy(file_handler[dst].path, file_handler[src].path);
774: file_handler[dst].valid = 1;
775: file_handler[dst].id = file_handler[src].id;
776: file_handler[dst].atty = file_handler[src].atty;
777: file_handler[dst].mode = file_handler[src].mode;
778: file_handler[dst].info = file_handler[src].info;
779: file_handler[dst].psp = psp_seg;
780: }
781:
782: void msdos_file_handler_close(int fd, UINT16 psp_seg)
783: {
784: if(psp_seg && fd < 20) {
785: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
786: psp->file_table[fd] = 0xff;
787: }
788: file_handler[fd].valid = 0;
789: }
790:
791: int msdos_file_attribute_create(UINT16 new_attr)
792: {
793: int attr = 0;
794:
795: if(REG16(CX) & 0x01) {
796: attr |= FILE_ATTRIBUTE_READONLY;
797: }
798: if(REG16(CX) & 0x02) {
799: attr |= FILE_ATTRIBUTE_HIDDEN;
800: }
801: if(REG16(CX) & 0x04) {
802: attr |= FILE_ATTRIBUTE_SYSTEM;
803: }
804: if(REG16(CX) & 0x20) {
805: attr |= FILE_ATTRIBUTE_ARCHIVE;
806: }
807: return(attr);
808: }
809:
810: // find file
811:
812: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
813: {
814: if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
815: return(0); // search directory only !!!
816: } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
817: return(0);
818: } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
819: return(0);
820: } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
821: return(0);
822: } else if((attribute & required_mask) != required_mask) {
823: return(0);
824: } else {
825: return(1);
826: }
827: }
828:
829: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
830: {
831: FILETIME local;
832:
833: FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
834: fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
835: fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
836:
837: FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
838: fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
839: fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
840:
841: FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
842: fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
843: fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
844: }
845:
846: // i/o
847:
848: void msdos_putch(UINT8 data);
849:
850: void msdos_stdio_reopen()
851: {
852: if(!file_handler[0].valid) {
853: _dup2(DUP_STDIN, 0);
854: msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
855: }
856: if(!file_handler[1].valid) {
857: _dup2(DUP_STDOUT, 1);
858: msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
859: }
860: if(!file_handler[2].valid) {
861: _dup2(DUP_STDERR, 2);
862: msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
863: }
864: }
865:
866: int msdos_kbhit()
867: {
868: msdos_stdio_reopen();
869:
870: if(!file_handler[0].atty) {
871: // stdin is redirected to file
872: return(eof(0) == 0);
873: }
874:
875: // check keyboard status
1.1.1.5 root 876: if(key_buf_char->count() != 0 || key_code != 0) {
1.1 root 877: return(1);
878: } else {
879: return(_kbhit());
880: }
881: }
882:
883: int msdos_getch_ex(int echo)
884: {
885: static char prev = 0;
886:
887: msdos_stdio_reopen();
888:
889: if(!file_handler[0].atty) {
890: // stdin is redirected to file
891: retry:
892: char data;
893: if(_read(0, &data, 1) == 1) {
894: char tmp = data;
895: if(data == 0x0a) {
896: if(prev == 0x0d) {
897: goto retry; // CRLF -> skip LF
898: } else {
899: data = 0x0d; // LF only -> CR
900: }
901: }
902: prev = tmp;
903: return(data);
904: }
905: return(EOF);
906: }
907:
908: // input from console
1.1.1.5 root 909: int key_char, key_scan;
910: if(key_code != 0) {
911: key_char = (key_code >> 0) & 0xff;
912: key_scan = (key_code >> 8) & 0xff;
913: key_code >>= 16;
914: } else {
915: while(key_buf_char->count() == 0) {
916: update_key_buffer();
917: }
918: key_char = key_buf_char->read();
919: key_scan = key_buf_scan->read();
1.1 root 920: }
921: if(echo && key_char) {
922: msdos_putch(key_char);
923: }
924: return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
925: }
926:
927: inline int msdos_getch()
928: {
929: return(msdos_getch_ex(0));
930: }
931:
932: inline int msdos_getche()
933: {
934: return(msdos_getch_ex(1));
935: }
936:
937: int msdos_write(int fd, const void *buffer, unsigned int count)
938: {
939: static int is_cr = 0;
940:
941: if(fd == 1 && !file_handler[1].atty) {
942: // CR+LF -> LF
943: UINT8 *buf = (UINT8 *)buffer;
944: for(unsigned int i = 0; i < count; i++) {
945: UINT8 data = buf[i];
946: if(is_cr) {
947: if(data != 0x0a) {
948: UINT8 tmp = 0x0d;
949: _write(1, &tmp, 1);
950: }
951: _write(1, &data, 1);
952: is_cr = 0;
953: } else if(data == 0x0d) {
954: is_cr = 1;
955: } else {
956: _write(1, &data, 1);
957: }
958: }
959: return(count);
960: }
961: return(_write(fd, buffer, count));
962: }
963:
964: void msdos_putch(UINT8 data)
965: {
966: static int p = 0;
967: static int is_kanji = 0;
968: static int is_esc = 0;
969: static int stored_x;
970: static int stored_y;
971: static WORD stored_a;
972: static char tmp[64];
973:
974: msdos_stdio_reopen();
975:
976: if(!file_handler[1].atty) {
977: // stdout is redirected to file
978: msdos_write(1, &data, 1);
979: return;
980: }
981:
982: // output to console
983: tmp[p++] = data;
984:
985: if(is_kanji) {
986: // kanji character
987: is_kanji = 0;
988: } else if(is_esc) {
989: // escape sequense
990: if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
991: p = is_esc = 0;
992: } else if(tmp[1] == '=' && p == 4) {
993: COORD co;
994: co.X = tmp[3] - 0x20;
995: co.Y = tmp[2] - 0x20;
996: SetConsoleCursorPosition(hStdout, co);
997: mem[0x450 + mem[0x462] * 2] = co.X;
998: mem[0x451 + mem[0x462] * 2] = co.Y;
999: cursor_moved = false;
1000: p = is_esc = 0;
1001: } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
1002: CONSOLE_SCREEN_BUFFER_INFO csbi;
1003: COORD co;
1004: GetConsoleScreenBufferInfo(hStdout, &csbi);
1005: co.X = csbi.dwCursorPosition.X;
1006: co.Y = csbi.dwCursorPosition.Y;
1007: WORD wAttributes = csbi.wAttributes;
1008:
1009: if(tmp[1] == 'D') {
1010: co.Y++;
1011: } else if(tmp[1] == 'E') {
1012: co.X = 0;
1013: co.Y++;
1014: } else if(tmp[1] == 'M') {
1015: co.Y--;
1016: } else if(tmp[1] == '*') {
1017: SMALL_RECT rect;
1018: SET_RECT(rect, 0, 0, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
1019: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1020: co.X = co.Y = 0;
1021: } else if(tmp[1] == '[') {
1022: int param[256], params = 0;
1023: memset(param, 0, sizeof(param));
1024: for(int i = 2; i < p; i++) {
1025: if(tmp[i] >= '0' && tmp[i] <= '9') {
1026: param[params] *= 10;
1027: param[params] += tmp[i] - '0';
1028: } else {
1029: params++;
1030: }
1031: }
1032: if(data == 'A') {
1033: co.Y -= param[0];
1034: } else if(data == 'B') {
1035: co.Y += param[0];
1036: } else if(data == 'C') {
1037: co.X += param[0];
1038: } else if(data == 'D') {
1039: co.X -= param[0];
1040: } else if(data == 'H' || data == 'f') {
1041: co.X = param[1] - 1;
1042: co.Y = param[0] - 1;
1043: } else if(data == 'J') {
1044: SMALL_RECT rect;
1045: if(param[0] == 0) {
1046: SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1047: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1048: if(co.Y < csbi.dwSize.Y - 1) {
1049: SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
1050: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1051: }
1052: } else if(param[0] == 1) {
1053: if(co.Y > 0) {
1054: SET_RECT(rect, 0, 0, csbi.dwSize.X - 1, co.Y - 1);
1055: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1056: }
1057: SET_RECT(rect, 0, co.Y, co.X, co.Y);
1058: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1059: } else if(param[0] == 2) {
1060: SET_RECT(rect, 0, 0, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
1061: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1062: co.X = co.Y = 0;
1063: }
1064: } else if(data == 'K') {
1065: SMALL_RECT rect;
1066: if(param[0] == 0) {
1067: SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1068: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1069: } else if(param[0] == 1) {
1070: SET_RECT(rect, 0, co.Y, co.X, co.Y);
1071: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1072: } else if(param[0] == 2) {
1073: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1074: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1075: }
1076: } else if(data == 'L') {
1077: SMALL_RECT rect;
1078: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
1079: ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1080: SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
1081: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1082: // clear buffer
1083: for(int y = 0; y < SCR_BUF_SIZE; y++) {
1084: for(int x = 0; x < 80; x++) {
1085: scr_buf[y][x].Char.AsciiChar = ' ';
1086: scr_buf[y][x].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1087: }
1088: }
1089: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1090: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1091: co.X = 0;
1092: } else if(data == 'M') {
1093: SMALL_RECT rect;
1094: if(co.Y + param[0] > csbi.dwSize.Y - 1) {
1095: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
1096: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1097: } else {
1098: SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
1099: ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1100: SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.dwSize.Y - 1);
1101: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1102: // clear buffer
1103: for(int y = 0; y < SCR_BUF_SIZE; y++) {
1104: for(int x = 0; x < 80; x++) {
1105: scr_buf[y][x].Char.AsciiChar = ' ';
1106: scr_buf[y][x].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1107: }
1108: }
1109: }
1110: co.X = 0;
1111: } else if(data == 'h') {
1112: if(tmp[2] == '>' && tmp[3] == '5') {
1113: CONSOLE_CURSOR_INFO cur;
1114: GetConsoleCursorInfo(hStdout, &cur);
1115: if(cur.bVisible) {
1116: cur.bVisible = FALSE;
1117: GetConsoleCursorInfo(hStdout, &cur);
1118: }
1119: }
1120: } else if(data == 'l') {
1121: if(tmp[2] == '>' && tmp[3] == '5') {
1122: CONSOLE_CURSOR_INFO cur;
1123: GetConsoleCursorInfo(hStdout, &cur);
1124: if(!cur.bVisible) {
1125: cur.bVisible = TRUE;
1126: GetConsoleCursorInfo(hStdout, &cur);
1127: }
1128: }
1129: } else if(data == 'm') {
1130: wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1131: int reverse = 0, hidden = 0;
1132: for(int i = 0; i < params; i++) {
1133: if(param[i] == 1) {
1134: wAttributes |= FOREGROUND_INTENSITY;
1135: } else if(param[i] == 4) {
1136: wAttributes |= COMMON_LVB_UNDERSCORE;
1137: } else if(param[i] == 7) {
1138: reverse = 1;
1139: } else if(param[i] == 8 || param[i] == 16) {
1140: hidden = 1;
1141: } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
1142: wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1143: if(param[i] >= 17 && param[i] <= 23) {
1144: param[i] -= 16;
1145: } else {
1146: param[i] -= 30;
1147: }
1148: if(param[i] & 1) {
1149: wAttributes |= FOREGROUND_RED;
1150: }
1151: if(param[i] & 2) {
1152: wAttributes |= FOREGROUND_GREEN;
1153: }
1154: if(param[i] & 4) {
1155: wAttributes |= FOREGROUND_BLUE;
1156: }
1157: } else if(param[i] >= 40 && param[i] <= 47) {
1158: wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
1159: if((param[i] - 40) & 1) {
1160: wAttributes |= BACKGROUND_RED;
1161: }
1162: if((param[i] - 40) & 2) {
1163: wAttributes |= BACKGROUND_GREEN;
1164: }
1165: if((param[i] - 40) & 4) {
1166: wAttributes |= BACKGROUND_BLUE;
1167: }
1168: }
1169: }
1170: if(reverse) {
1171: wAttributes &= ~0xff;
1172: wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
1173: }
1174: if(hidden) {
1175: wAttributes &= ~0x0f;
1176: wAttributes |= (wAttributes >> 4) & 0x0f;
1177: }
1178: } else if(data == 'n') {
1179: if(param[0] == 6) {
1180: char tmp[16];
1181: sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
1182: int len = strlen(tmp);
1183: for(int i = 0; i < len; i++) {
1184: key_buf_char->write(tmp[i]);
1185: key_buf_scan->write(0x00);
1186: }
1187: }
1188: } else if(data == 's') {
1189: stored_x = co.X;
1190: stored_y = co.Y;
1191: stored_a = wAttributes;
1192: } else if(data == 'u') {
1193: co.X = stored_x;
1194: co.Y = stored_y;
1195: wAttributes = stored_a;
1196: }
1197: }
1198: if(co.X < 0) {
1199: co.X = 0;
1200: } else if(co.X >= csbi.dwSize.X) {
1201: co.X = csbi.dwSize.X - 1;
1202: }
1203: if(co.Y < 0) {
1204: co.Y = 0;
1205: } else if(co.Y >= csbi.dwSize.Y) {
1206: co.Y = csbi.dwSize.Y - 1;
1207: }
1208: if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
1209: SetConsoleCursorPosition(hStdout, co);
1210: mem[0x450 + mem[0x462] * 2] = co.X;
1211: mem[0x451 + mem[0x462] * 2] = co.Y;
1212: cursor_moved = false;
1213: }
1214: if(wAttributes != csbi.wAttributes) {
1215: SetConsoleTextAttribute(hStdout, wAttributes);
1216: }
1217: p = is_esc = 0;
1218: }
1219: return;
1220: } else {
1221: if(msdos_lead_byte_check(data)) {
1222: is_kanji = 1;
1223: return;
1224: } else if(data == 0x1b) {
1225: is_esc = 1;
1226: return;
1227: }
1228: }
1229: tmp[p++] = '\0';
1230: p = 0;
1231: printf("%s", tmp);
1232: cursor_moved = true;
1233: }
1234:
1235: int msdos_aux_in()
1236: {
1237: #ifdef SUPPORT_AUX_PRN
1238: if(file_handler[3].valid && !eof(3)) {
1239: char data = 0;
1240: _read(3, &data, 1);
1241: return(data);
1242: } else {
1243: return(EOF);
1244: }
1245: #else
1246: return(0);
1247: #endif
1248: }
1249:
1250: void msdos_aux_out(char data)
1251: {
1252: #ifdef SUPPORT_AUX_PRN
1253: if(file_handler[3].valid) {
1254: msdos_write(3, &data, 1);
1255: }
1256: #endif
1257: }
1258:
1259: void msdos_prn_out(char data)
1260: {
1261: #ifdef SUPPORT_AUX_PRN
1262: if(file_handler[4].valid) {
1263: msdos_write(4, &data, 1);
1264: }
1265: #endif
1266: }
1267:
1268: // memory control
1269:
1270: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, UINT16 paragraphs)
1271: {
1272: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1273:
1274: mcb->mz = mz;
1275: mcb->psp = psp;
1276: mcb->paragraphs = paragraphs;
1277: return(mcb);
1278: }
1279:
1280: void msdos_mcb_check(mcb_t *mcb)
1281: {
1282: if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1283: fatalerror("broken mcb\n");
1284: }
1285: }
1286:
1287: int msdos_mem_split(int seg, int paragraphs)
1288: {
1289: int mcb_seg = seg - 1;
1290: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1291: msdos_mcb_check(mcb);
1292:
1293: if(mcb->paragraphs > paragraphs) {
1294: int new_seg = mcb_seg + 1 + paragraphs;
1295: int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1296:
1297: msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
1298: mcb->mz = 'M';
1299: mcb->paragraphs = paragraphs;
1300: return(0);
1301: }
1302: return(-1);
1303: }
1304:
1305: void msdos_mem_merge(int seg)
1306: {
1307: int mcb_seg = seg - 1;
1308: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1309: msdos_mcb_check(mcb);
1310:
1311: while(1) {
1312: if(mcb->mz == 'Z') {
1313: break;
1314: }
1315: int next_seg = mcb_seg + 1 + mcb->paragraphs;
1316: mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
1317: msdos_mcb_check(next_mcb);
1318:
1319: if(next_mcb->psp != 0) {
1320: break;
1321: }
1322: mcb->mz = next_mcb->mz;
1323: mcb->paragraphs += 1 + next_mcb->paragraphs;
1324: }
1325: }
1326:
1327: int msdos_mem_alloc(int paragraphs, int new_process)
1328: {
1329: int mcb_seg = current_psp - 1;
1330:
1331: while(1) {
1332: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1333: msdos_mcb_check(mcb);
1334:
1335: if(!new_process || mcb->mz == 'Z') {
1336: if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1337: msdos_mem_split(mcb_seg + 1, paragraphs);
1338: mcb->psp = current_psp;
1339: return(mcb_seg + 1);
1340: }
1341: }
1342: if(mcb->mz == 'Z') {
1343: break;
1344: }
1345: mcb_seg += 1 + mcb->paragraphs;
1346: }
1347: return(-1);
1348: }
1349:
1350: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
1351: {
1352: int mcb_seg = seg - 1;
1353: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1354: msdos_mcb_check(mcb);
1355: int current_paragraphs = mcb->paragraphs;
1356:
1357: msdos_mem_merge(seg);
1358: if(paragraphs > mcb->paragraphs) {
1359: *max_paragraphs = mcb->paragraphs;
1360: msdos_mem_split(seg, current_paragraphs);
1361: return(-1);
1362: }
1363: msdos_mem_split(seg, paragraphs);
1364: return(0);
1365: }
1366:
1367: void msdos_mem_free(int seg)
1368: {
1369: int mcb_seg = seg - 1;
1370: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1371: msdos_mcb_check(mcb);
1372:
1373: mcb->psp = 0;
1374: msdos_mem_merge(seg);
1375: }
1376:
1377: int msdos_mem_get_free(int new_process)
1378: {
1379: int mcb_seg = current_psp - 1;
1380: int max_paragraphs = 0;
1381:
1382: while(1) {
1383: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1384: msdos_mcb_check(mcb);
1385:
1386: if(!new_process || mcb->mz == 'Z') {
1387: if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
1388: max_paragraphs = mcb->paragraphs;
1389: }
1390: }
1391: if(mcb->mz == 'Z') {
1392: break;
1393: }
1394: mcb_seg += 1 + mcb->paragraphs;
1395: }
1396: return(max_paragraphs);
1397: }
1398:
1399: // environment
1400:
1401: void msdos_env_set_argv(int env_seg, char *argv)
1402: {
1403: char *dst = (char *)(mem + (env_seg << 4));
1404:
1405: while(1) {
1406: if(dst[0] == 0) {
1407: break;
1408: }
1409: dst += strlen(dst) + 1;
1410: }
1411: *dst++ = 0; // end of environment
1412: *dst++ = 1; // top of argv[0]
1413: *dst++ = 0;
1414: memcpy(dst, argv, strlen(argv));
1415: dst += strlen(argv);
1416: *dst++ = 0;
1417: *dst++ = 0;
1418: }
1419:
1420: char *msdos_env_get_argv(int env_seg)
1421: {
1422: static char env[ENV_SIZE];
1423: char *src = env;
1424:
1425: memcpy(src, mem + (env_seg << 4), ENV_SIZE);
1426: while(1) {
1427: if(src[0] == 0) {
1428: if(src[1] == 1) {
1429: return(src + 3);
1430: }
1431: break;
1432: }
1433: src += strlen(src) + 1;
1434: }
1435: return(NULL);
1436: }
1437:
1438: char *msdos_env_get(int env_seg, const char *name)
1439: {
1440: static char env[ENV_SIZE];
1441: char *src = env;
1442:
1443: memcpy(src, mem + (env_seg << 4), ENV_SIZE);
1444: while(1) {
1445: if(src[0] == 0) {
1446: break;
1447: }
1448: int len = strlen(src);
1449: char *n = my_strtok(src, "=");
1450: char *v = src + strlen(n) + 1;
1451:
1452: if(_stricmp(name, n) == 0) {
1453: return(v);
1454: }
1455: src += len + 1;
1456: }
1457: return(NULL);
1458: }
1459:
1460: void msdos_env_set(int env_seg, char *name, char *value)
1461: {
1462: char env[ENV_SIZE];
1463: char *src = env;
1464: char *dst = (char *)(mem + (env_seg << 4));
1465: char *argv = msdos_env_get_argv(env_seg);
1466: int done = 0;
1467:
1468: memcpy(src, dst, ENV_SIZE);
1469: memset(dst, 0, ENV_SIZE);
1470: while(1) {
1471: if(src[0] == 0) {
1472: break;
1473: }
1474: int len = strlen(src);
1475: char *n = my_strtok(src, "=");
1476: char *v = src + strlen(n) + 1;
1477: char tmp[1024];
1478:
1479: if(_stricmp(name, n) == 0) {
1480: sprintf(tmp, "%s=%s", n, value);
1481: done = 1;
1482: } else {
1483: sprintf(tmp, "%s=%s", n, v);
1484: }
1485: memcpy(dst, tmp, strlen(tmp));
1486: dst += strlen(tmp) + 1;
1487: src += len + 1;
1488: }
1489: if(!done) {
1490: char tmp[1024];
1491:
1492: sprintf(tmp, "%s=%s", name, value);
1493: memcpy(dst, tmp, strlen(tmp));
1494: dst += strlen(tmp) + 1;
1495: }
1496: if(argv) {
1497: *dst++ = 0; // end of environment
1498: *dst++ = 1; // top of argv[0]
1499: *dst++ = 0;
1500: memcpy(dst, argv, strlen(argv));
1501: dst += strlen(argv);
1502: *dst++ = 0;
1503: *dst++ = 0;
1504: }
1505: }
1506:
1507: // process
1508:
1509: psp_t *msdos_psp_create(int psp_seg, UINT16 first_mcb, UINT16 parent_psp, UINT16 env_seg)
1510: {
1511: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
1512:
1513: memset(psp, 0, PSP_SIZE);
1514: psp->exit[0] = 0xcd;
1515: psp->exit[1] = 0x20;
1516: psp->first_mcb = first_mcb;
1517: psp->far_call = 0xea;
1518: psp->cpm_entry.w.l = 0xfff1; // int 21h, retf
1519: psp->cpm_entry.w.h = 0xf000;
1520: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
1521: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
1522: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
1523: psp->parent_psp = parent_psp;
1524: for(int i = 0; i < 20; i++) {
1525: if(file_handler[i].valid) {
1526: psp->file_table[i] = i;
1527: } else {
1528: psp->file_table[i] = 0xff;
1529: }
1530: }
1531: psp->env_seg = env_seg;
1532: psp->stack.w.l = REG16(SP);
1.1.1.3 root 1533: psp->stack.w.h = SREG(SS);
1.1 root 1534: psp->service[0] = 0xcd;
1535: psp->service[1] = 0x21;
1536: psp->service[2] = 0xcb;
1537: return(psp);
1538: }
1539:
1540: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
1541: {
1542: // load command file
1543: int fd = -1;
1544: int dos_command = 0;
1.1.1.4 root 1545: char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name, name_tmp[MAX_PATH];
1.1 root 1546:
1547: strcpy(command, cmd);
1548: int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
1549: int opt_len = mem[opt_ofs];
1550: memset(opt, 0, sizeof(opt));
1551: memcpy(opt, mem + opt_ofs + 1, opt_len);
1552:
1553: // check command.com
1554: GetFullPathName(command, MAX_PATH, path, &name);
1.1.1.4 root 1555: memset(name_tmp, 0, sizeof(name_tmp));
1556: strcpy(name_tmp, name);
1557:
1.1 root 1558: if(_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "CMD.EXE") == 0) {
1559: for(int i = 0; i < opt_len; i++) {
1560: if(opt[i] == ' ') {
1561: continue;
1562: }
1563: if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
1564: dos_command = 1;
1565: for(int j = i + 3; j < opt_len; j++) {
1566: if(opt[j] == ' ') {
1567: continue;
1568: }
1569: char *token = my_strtok(opt + j, " ");
1570: strcpy(command, token);
1571: char tmp[MAX_PATH];
1572: strcpy(tmp, token + strlen(token) + 1);
1573: strcpy(opt, tmp);
1574: opt_len = strlen(opt);
1575: mem[opt_ofs] = opt_len;
1576: strcpy((char *)(mem + opt_ofs + 1), opt);
1577: break;
1578: }
1579: }
1580: break;
1581: }
1582: }
1583:
1584: // load command file
1585: strcpy(path, command);
1586: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1587: sprintf(path, "%s.COM", command);
1588: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1589: sprintf(path, "%s.EXE", command);
1590: if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1591: // search path in parent environments
1592: psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1593: char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1594:
1595: if(env != NULL) {
1596: char env_path[1024];
1597: strcpy(env_path, env);
1598: char *token = my_strtok(env_path, ";");
1599:
1600: while(token != NULL) {
1601: sprintf(path, "%s\\%s", token, command);
1602: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
1603: break;
1604: }
1605: sprintf(path, "%s\\%s.COM", token, command);
1606: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
1607: break;
1608: }
1609: sprintf(path, "%s\\%s.EXE", token, command);
1610: if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
1611: break;
1612: }
1613: token = my_strtok(NULL, ";");
1614: }
1615: }
1616: }
1617: }
1618: }
1619: if(fd == -1) {
1620: if(dos_command) {
1621: // may be dos command
1622: char tmp[MAX_PATH];
1623: sprintf(tmp, "%s %s", command, opt);
1624: system(tmp);
1625: return(0);
1626: } else {
1627: return(-1);
1628: }
1629: }
1630: _read(fd, file_buffer, sizeof(file_buffer));
1631: _close(fd);
1632:
1633: // copy environment
1634: int env_seg, psp_seg;
1635:
1636: if((env_seg = msdos_mem_alloc(ENV_SIZE >> 4, 1)) == -1) {
1637: return(-1);
1638: }
1639: if(param->env_seg == 0) {
1640: psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1641: memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
1642: } else {
1643: memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
1644: }
1645: msdos_env_set_argv(env_seg, msdos_short_full_path(path));
1646:
1647: // check exe header
1648: exe_header_t *header = (exe_header_t *)file_buffer;
1649: int paragraphs, free_paragraphs = msdos_mem_get_free(1);
1650: UINT16 cs, ss, ip, sp;
1651:
1652: if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
1653: // memory allocation
1654: int header_size = header->header_size * 16;
1655: int load_size = header->pages * 512 - header_size;
1656: if(header_size + load_size < 512) {
1657: load_size = 512 - header_size;
1658: }
1659: paragraphs = (PSP_SIZE + load_size) >> 4;
1660: if(paragraphs + header->min_alloc > free_paragraphs) {
1661: msdos_mem_free(env_seg);
1662: return(-1);
1663: }
1664: paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
1665: if(paragraphs > free_paragraphs) {
1666: paragraphs = free_paragraphs;
1667: }
1668: if((psp_seg = msdos_mem_alloc(paragraphs, 1)) == -1) {
1669: msdos_mem_free(env_seg);
1670: return(-1);
1671: }
1672: // relocation
1673: int start_seg = psp_seg + (PSP_SIZE >> 4);
1674: for(int i = 0; i < header->relocations; i++) {
1675: int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
1676: int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
1677: *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
1678: }
1679: memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
1680: // segments
1681: cs = header->init_cs + start_seg;
1682: ss = header->init_ss + start_seg;
1683: ip = header->init_ip;
1684: sp = header->init_sp - 2; // for symdeb
1685: } else {
1686: // memory allocation
1687: paragraphs = free_paragraphs;
1688: if((psp_seg = msdos_mem_alloc(paragraphs, 1)) == -1) {
1689: msdos_mem_free(env_seg);
1690: return(-1);
1691: }
1692: int start_seg = psp_seg + (PSP_SIZE >> 4);
1693: memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
1694: // segments
1695: cs = ss = psp_seg;
1696: ip = 0x100;
1697: sp = 0xfffe;
1698: }
1699:
1700: // create psp
1.1.1.3 root 1701: #if defined(HAS_I386)
1702: *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
1703: #else
1704: *(UINT16 *)(mem + 4 * 0x22 + 0) = m_pc - SREG_BASE(CS);
1705: #endif
1706: *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1 root 1707: psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
1708: memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
1709: memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
1710: memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
1711:
1712: mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
1713: mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
1714: mcb_psp->psp = mcb_env->psp = psp_seg;
1715:
1.1.1.4 root 1716: for(int i = 0; i < 8; i++) {
1717: if(name_tmp[i] == '.') {
1718: mcb_psp->prog_name[i] = '\0';
1719: break;
1720: } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
1721: mcb_psp->prog_name[i] = name_tmp[i];
1722: i++;
1723: mcb_psp->prog_name[i] = name_tmp[i];
1724: } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
1725: mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
1726: } else {
1727: mcb_psp->prog_name[i] = name_tmp[i];
1728: }
1729: }
1730:
1.1 root 1731: // process info
1732: process_t *process = msdos_process_info_create(psp_seg);
1733: strcpy(process->module_dir, msdos_short_full_dir(path));
1734: process->dta.w.l = 0x80;
1735: process->dta.w.h = psp_seg;
1736: process->switchar = '/';
1737: process->max_files = 20;
1738: process->find_handle = INVALID_HANDLE_VALUE;
1739: process->parent_int_10h_feh_called = int_10h_feh_called;
1740: process->parent_int_10h_ffh_called = int_10h_ffh_called;
1741:
1742: current_psp = psp_seg;
1743:
1744: if(al == 0x00) {
1745: int_10h_feh_called = int_10h_ffh_called = false;
1746:
1747: // registers and segments
1748: REG16(AX) = REG16(BX) = 0x00;
1749: REG16(CX) = 0xff;
1750: REG16(DX) = psp_seg;
1751: REG16(SI) = ip;
1752: REG16(DI) = sp;
1753: REG16(SP) = sp;
1.1.1.3 root 1754: SREG(DS) = SREG(ES) = psp_seg;
1755: SREG(SS) = ss;
1756: i386_load_segment_descriptor(DS);
1757: i386_load_segment_descriptor(ES);
1758: i386_load_segment_descriptor(SS);
1.1 root 1759:
1760: *(UINT16 *)(mem + (ss << 4) + sp) = 0;
1761: i386_jmp_far(cs, ip);
1762: } else if(al == 0x01) {
1763: // copy ss:sp and cs:ip to param block
1764: param->sp = sp;
1765: param->ss = ss;
1766: param->ip = ip;
1767: param->cs = cs;
1768: }
1769: return(0);
1770: }
1771:
1772: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
1773: {
1774: psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
1775:
1776: *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
1777: *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
1778: *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
1779:
1.1.1.3 root 1780: SREG(SS) = psp->stack.w.h;
1781: i386_load_segment_descriptor(SS);
1.1 root 1782: REG16(SP) = psp->stack.w.l;
1783: i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
1784:
1785: process_t *process = msdos_process_info_get(psp_seg);
1786: int_10h_feh_called = process->parent_int_10h_feh_called;
1787: int_10h_ffh_called = process->parent_int_10h_ffh_called;
1788:
1789: if(mem_free) {
1790: int mcb_seg = psp->env_seg - 1;
1791: msdos_mcb_create(mcb_seg, 'Z', 0, (MEMORY_END >> 4) - mcb_seg - 1);
1792:
1793: for(int i = 0; i < MAX_FILES; i++) {
1794: if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
1795: _close(i);
1796: msdos_file_handler_close(i, psp_seg);
1797: }
1798: }
1799: msdos_stdio_reopen();
1800:
1801: if(process->find_handle != INVALID_HANDLE_VALUE) {
1802: FindClose(process->find_handle);
1803: process->find_handle = INVALID_HANDLE_VALUE;
1804: }
1805: }
1806:
1807: memset(process, 0, sizeof(process_t));
1808:
1809: current_psp = psp->parent_psp;
1810: retval = ret;
1811: }
1812:
1813: // drive
1814:
1815: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
1816: {
1817: *seg = DPB_TOP >> 4;
1818: *ofs = sizeof(dpb_t) * drive_num;
1819: dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
1820:
1821: if(!force_update && dpb->free_clusters != 0) {
1822: return(dpb->bytes_per_sector ? 1 : 0);
1823: }
1824: memset(dpb, 0, sizeof(dpb_t));
1825:
1826: int res = 0;
1827: char dev[64];
1828: sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
1829:
1830: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1831: if(hFile != INVALID_HANDLE_VALUE) {
1832: DISK_GEOMETRY geo;
1833: DWORD dwSize;
1834: if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
1835: dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
1836: dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
1837: dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1838: switch(geo.MediaType) {
1839: case F5_320_512: // floppy, double-sided, 8 sectors per track (320K)
1840: dpb->media_type = 0xff;
1841: break;
1842: case F5_160_512: // floppy, single-sided, 8 sectors per track (160K)
1843: dpb->media_type = 0xfe;
1844: break;
1845: case F5_360_512: // floppy, double-sided, 9 sectors per track (360K)
1846: dpb->media_type = 0xfd;
1847: break;
1848: case F5_180_512: // floppy, single-sided, 9 sectors per track (180K)
1849: dpb->media_type = 0xfc;
1850: break;
1851: case F5_1Pt2_512: // floppy, double-sided, 15 sectors per track (1.2M)
1852: case F3_720_512: // floppy, double-sided, 9 sectors per track (720K,3.5")
1853: dpb->media_type = 0xf9;
1854: break;
1855: case FixedMedia: // hard disk
1856: case RemovableMedia:
1857: dpb->media_type = 0xf8;
1858: break;
1859: default:
1860: dpb->media_type = 0xf0;
1861: break;
1862: }
1863: res = 1;
1864: }
1865: dpb->drive_num = drive_num;
1866: dpb->unit_num = drive_num;
1867: dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
1868: dpb->next_dpb_seg = *seg;
1869: dpb->free_clusters = 0xffff;
1870: CloseHandle(hFile);
1871: }
1872: return(res);
1873: }
1874:
1875: // pc bios
1876:
1877: int get_tvram_address(int page)
1878: {
1879: if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1880: return TVRAM_TOP;
1881: } else {
1882: return TVRAM_TOP + 0x1000 * (page & 7);
1883: }
1884: }
1885:
1886: inline int get_tvram_address(int page, int x, int y)
1887: {
1888: return get_tvram_address(page) + (x + y * 80) * 2;
1889: }
1890:
1891: inline void pcbios_int_10h_00h()
1892: {
1893: mem[0x449] = REG8(AL) & 0x7f;
1.1.1.3 root 1894: tvram_top_address = get_tvram_address(mem[0x462]);
1895: tvram_end_address = tvram_top_address + 4000;
1.1 root 1896:
1897: if(REG8(AL) & 0x80) {
1898: mem[0x487] |= 0x80;
1899: } else {
1900: for(int y = 0, ofs = get_tvram_address(mem[0x462]); y < 25; y++) {
1901: for(int x = 0; x < 80; x++) {
1902: scr_buf[y][x].Char.AsciiChar = ' ';
1903: scr_buf[y][x].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1904: mem[ofs++] = 0x20;
1905: mem[ofs++] = 0x07;
1906: }
1907: }
1908: SMALL_RECT rect;
1909: SET_RECT(rect, 0, 0, 79, 24);
1910: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1911: mem[0x487] &= ~0x80;
1912: }
1913: }
1914:
1915: inline void pcbios_int_10h_01h()
1916: {
1917: mem[0x460] = REG8(CL);
1918: mem[0x461] = REG8(CH);
1919: }
1920:
1921: inline void pcbios_int_10h_02h()
1922: {
1923: if(mem[0x462] == REG8(BH)) {
1924: COORD co;
1925: co.X = REG8(DL);
1926: co.Y = REG8(DH);
1927: SetConsoleCursorPosition(hStdout, co);
1928: }
1929: mem[0x450 + (REG8(BH) & 7) * 2] = REG8(DL);
1930: mem[0x451 + (REG8(BH) & 7) * 2] = REG8(DH);
1931: }
1932:
1933: inline void pcbios_int_10h_03h()
1934: {
1935: REG8(DL) = mem[0x450 + (REG8(BH) & 7) * 2];
1936: REG8(DH) = mem[0x451 + (REG8(BH) & 7) * 2];
1937: REG8(CL) = mem[0x460];
1938: REG8(CH) = mem[0x461];
1939: }
1940:
1941: inline void pcbios_int_10h_05h()
1942: {
1943: if(mem[0x462] != REG8(BH)) {
1944: SMALL_RECT rect;
1945: SET_RECT(rect, 0, 0, 79, 24);
1946: ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1947:
1948: for(int y = 0, ofs = get_tvram_address(mem[0x462]); y < 25; y++) {
1949: for(int x = 0; x < 80; x++) {
1950: mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
1951: mem[ofs++] = scr_buf[y][x].Attributes;
1952: }
1953: }
1954: for(int y = 0, ofs = get_tvram_address(REG8(BH)); y < 25; y++) {
1955: for(int x = 0; x < 80; x++) {
1956: scr_buf[y][x].Char.AsciiChar = mem[ofs++];
1957: scr_buf[y][x].Attributes = mem[ofs++];
1958: }
1959: }
1960: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1961:
1962: COORD co;
1963: co.X = mem[0x450 + (REG8(BH) & 7) * 2];
1964: co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
1965: SetConsoleCursorPosition(hStdout, co);
1966: }
1967: mem[0x462] = REG8(BH) & 7;
1968: mem[0x44e] = 0;
1969: mem[0x44f] = REG8(BH) << 4;
1.1.1.3 root 1970: tvram_top_address = get_tvram_address(mem[0x462]);
1971: tvram_end_address = tvram_top_address + 4000;
1.1 root 1972: }
1973:
1974: inline void pcbios_int_10h_06h()
1975: {
1976: SMALL_RECT rect;
1977: SET_RECT(rect, 0, 0, 79, 24);
1978: ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1979:
1980: if(REG8(AL) == 0) {
1981: for(int y = REG8(CH); y <= REG8(DH); y++) {
1982: for(int x = REG8(CL), ofs = get_tvram_address(mem[0x462], REG8(CL), y); x <= REG8(DL); x++) {
1983: scr_buf[y][x].Char.AsciiChar = ' ';
1984: scr_buf[y][x].Attributes = REG8(BH);
1985: mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
1986: mem[ofs++] = scr_buf[y][x].Attributes;
1987: }
1988: }
1989: } else {
1990: ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
1991: for(int y = REG8(CH), y2 = REG8(CH) + REG8(AL); y <= REG8(DH); y++, y2++) {
1992: for(int x = REG8(CL), ofs = get_tvram_address(mem[0x462], REG8(CL), y); x <= REG8(DL); x++) {
1993: if(y2 <= REG8(DH) && y2 >= 0 && y2 < SCR_BUF_SIZE) {
1994: scr_buf[y][x] = scr_buf[y2][x];
1995: } else {
1996: scr_buf[y][x].Char.AsciiChar = ' ';
1997: scr_buf[y][x].Attributes = REG8(BH);
1998: }
1999: mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
2000: mem[ofs++] = scr_buf[y][x].Attributes;
2001: }
2002: }
2003: }
2004: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
2005: }
2006:
2007: inline void pcbios_int_10h_07h()
2008: {
2009: SMALL_RECT rect;
2010: SET_RECT(rect, 0, 0, 79, 24);
2011: ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
2012:
2013: if(REG8(AL) == 0) {
2014: for(int y = REG8(CH); y <= REG8(DH); y++) {
2015: for(int x = REG8(CL), ofs = get_tvram_address(mem[0x462], REG8(CL), y); x <= REG8(DL); x++) {
2016: scr_buf[y][x].Char.AsciiChar = ' ';
2017: scr_buf[y][x].Attributes = REG8(BH);
2018: mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
2019: mem[ofs++] = scr_buf[y][x].Attributes;
2020: }
2021: }
2022: } else {
2023: ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
2024: for(int y = REG8(DH), y2 = REG8(DH) - REG8(AL); y >= REG8(CH); y--, y2--) {
2025: for(int x = REG8(CL), ofs = get_tvram_address(mem[0x462], REG8(CL), y); x <= REG8(DL); x++) {
2026: if(y2 >= REG8(CH) && y2 >= 0 && y2 < SCR_BUF_SIZE) {
2027: scr_buf[y][x] = scr_buf[y2][x];
2028: } else {
2029: scr_buf[y][x].Char.AsciiChar = ' ';
2030: scr_buf[y][x].Attributes = REG8(BH);
2031: }
2032: mem[ofs++] = scr_buf[y][x].Char.AsciiChar;
2033: mem[ofs++] = scr_buf[y][x].Attributes;
2034: }
2035: }
2036: }
2037: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
2038: }
2039:
2040: inline void pcbios_int_10h_08h()
2041: {
2042: COORD co;
2043: DWORD num;
2044:
2045: co.X = mem[0x450 + (REG8(BH) & 7) * 2];
2046: co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
2047:
2048: if(mem[0x462] == REG8(BH)) {
2049: ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
2050: ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
2051: REG8(AL) = scr_char[0];
2052: REG8(AH) = scr_attr[0];
2053: } else {
2054: REG16(AX) = *(UINT16 *)(mem + get_tvram_address(REG8(BH), co.X, co.Y));
2055: }
2056: }
2057:
2058: inline void pcbios_int_10h_09h()
2059: {
2060: COORD co;
2061: DWORD num;
2062:
2063: co.X = mem[0x450 + (REG8(BH) & 7) * 2];
2064: co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
2065:
2066: if(mem[0x462] == REG8(BH)) {
2067: for(int i = 0; i < REG16(CX) && i < 80 * 25; i++) {
2068: scr_char[i] = REG8(AL);
2069: scr_attr[i] = REG8(BL);
2070: }
2071: WriteConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
2072: WriteConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
2073: } else {
2074: for(int i = 0, dest = get_tvram_address(REG8(BH), co.X, co.Y); i < REG16(CX); i++) {
2075: mem[dest++] = REG8(AL);
2076: mem[dest++] = REG8(BL);
2077: if(++co.X == 80) {
2078: if(++co.Y == 25) {
2079: break;
2080: }
2081: co.X = 0;
2082: }
2083: }
2084: }
2085: }
2086:
2087: inline void pcbios_int_10h_0ah()
2088: {
2089: COORD co;
2090: DWORD num;
2091:
2092: co.X = mem[0x450 + (REG8(BH) & 7) * 2];
2093: co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
2094:
2095: if(mem[0x462] == REG8(BH)) {
2096: for(int i = 0; i < REG16(CX) && i < 80 * 25; i++) {
2097: scr_char[i] = REG8(AL);
2098: // scr_attr[i] = REG8(BL);
2099: }
2100: WriteConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
2101: // WriteConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
2102: } else {
2103: for(int i = 0, dest = get_tvram_address(REG8(BH), co.X, co.Y); i < REG16(CX); i++) {
2104: mem[dest++] = REG8(AL);
2105: // mem[dest++] = REG8(BL);
2106: dest++;
2107: if(++co.X == 80) {
2108: if(++co.Y == 25) {
2109: break;
2110: }
2111: co.X = 0;
2112: }
2113: }
2114: }
2115: }
2116:
2117: inline void pcbios_int_10h_0eh()
2118: {
2119: msdos_putch(REG8(AL));
2120: }
2121:
2122: inline void pcbios_int_10h_0fh()
2123: {
2124: REG8(AL) = mem[0x449];
2125: REG8(AH) = mem[0x44a];
2126: REG8(BH) = mem[0x462];
2127: }
2128:
2129: inline void pcbios_int_10h_13h()
2130: {
1.1.1.3 root 2131: int ofs = SREG_BASE(ES) + REG16(BP);
1.1 root 2132: COORD co;
2133: DWORD num;
2134:
2135: co.X = REG8(DL);
2136: co.Y = REG8(DH);
2137:
2138: switch(REG8(AL)) {
2139: case 0x00:
2140: case 0x01:
2141: if(mem[0x462] == REG8(BH)) {
2142: CONSOLE_SCREEN_BUFFER_INFO csbi;
2143: GetConsoleScreenBufferInfo(hStdout, &csbi);
2144: SetConsoleCursorPosition(hStdout, co);
2145:
2146: if(csbi.wAttributes != REG8(BL)) {
2147: SetConsoleTextAttribute(hStdout, REG8(BL));
2148: }
2149: for(int i = 0; i < REG16(CX); i++) {
2150: msdos_putch(mem[ofs++]);
2151: }
2152: if(csbi.wAttributes != REG8(BL)) {
2153: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
2154: }
2155: if(REG8(AL) == 0x00) {
2156: co.X = mem[0x450 + (REG8(BH) & 7) * 2];
2157: co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
2158: SetConsoleCursorPosition(hStdout, co);
2159: } else {
2160: cursor_moved = true;
2161: }
2162: } else {
1.1.1.3 root 2163: m_CF = 1;
1.1 root 2164: }
2165: break;
2166: case 0x02:
2167: case 0x03:
2168: if(mem[0x462] == REG8(BH)) {
2169: CONSOLE_SCREEN_BUFFER_INFO csbi;
2170: GetConsoleScreenBufferInfo(hStdout, &csbi);
2171: SetConsoleCursorPosition(hStdout, co);
2172:
2173: WORD wAttributes = csbi.wAttributes;
2174: for(int i = 0; i < REG16(CX); i++, ofs += 2) {
2175: if(wAttributes != mem[ofs + 1]) {
2176: SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
2177: wAttributes = mem[ofs + 1];
2178: }
2179: msdos_putch(mem[ofs]);
2180: }
2181: if(csbi.wAttributes != wAttributes) {
2182: SetConsoleTextAttribute(hStdout, csbi.wAttributes);
2183: }
2184: if(REG8(AL) == 0x02) {
2185: co.X = mem[0x450 + (REG8(BH) & 7) * 2];
2186: co.Y = mem[0x451 + (REG8(BH) & 7) * 2];
2187: SetConsoleCursorPosition(hStdout, co);
2188: } else {
2189: cursor_moved = true;
2190: }
2191: } else {
1.1.1.3 root 2192: m_CF = 1;
1.1 root 2193: }
2194: break;
2195: case 0x10:
2196: case 0x11:
2197: if(mem[0x462] == REG8(BH)) {
2198: ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
2199: ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
2200: for(int i = 0; i < num; i++) {
2201: mem[ofs++] = scr_char[i];
2202: mem[ofs++] = scr_attr[i];
2203: if(REG8(AL) == 0x11) {
2204: mem[ofs++] = 0;
2205: mem[ofs++] = 0;
2206: }
2207: }
2208: } else {
2209: for(int i = 0, src = get_tvram_address(REG8(BH), co.X, co.Y); i < REG16(CX); i++) {
2210: mem[ofs++] = mem[src++];
2211: mem[ofs++] = mem[src++];
2212: if(REG8(AL) == 0x11) {
2213: mem[ofs++] = 0;
2214: mem[ofs++] = 0;
2215: }
2216: if(++co.X == 80) {
2217: if(++co.Y == 25) {
2218: break;
2219: }
2220: co.X = 0;
2221: }
2222: }
2223: }
2224: break;
2225: case 0x20:
2226: case 0x21:
2227: if(mem[0x462] == REG8(BH)) {
2228: for(int i = 0; i < REG16(CX) && i < 80 * 25; i++) {
2229: scr_char[i] = mem[ofs++];
2230: scr_attr[i] = mem[ofs++];
2231: if(REG8(AL) == 0x21) {
2232: ofs += 2;
2233: }
2234: }
2235: WriteConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
2236: WriteConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
2237: } else {
2238: for(int i = 0, dest = get_tvram_address(REG8(BH), co.X, co.Y); i < REG16(CX); i++) {
2239: mem[dest++] = mem[ofs++];
2240: mem[dest++] = mem[ofs++];
2241: if(REG8(AL) == 0x21) {
2242: ofs += 2;
2243: }
2244: if(++co.X == 80) {
2245: if(++co.Y == 25) {
2246: break;
2247: }
2248: co.X = 0;
2249: }
2250: }
2251: }
2252: break;
2253: default:
1.1.1.3 root 2254: m_CF = 1;
1.1 root 2255: break;
2256: }
2257: }
2258:
2259: inline void pcbios_int_10h_1dh()
2260: {
2261: switch(REG8(AL)) {
2262: case 0x01:
2263: break;
2264: case 0x02:
2265: REG16(BX) = 0;
2266: break;
2267: default:
1.1.1.3 root 2268: m_CF = 1;
1.1 root 2269: break;
2270: }
2271: }
2272:
2273: inline void pcbios_int_10h_82h()
2274: {
2275: static UINT8 mode = 0;
2276:
2277: switch(REG8(AL)) {
2278: case 0:
2279: if(REG8(BL) != 0xff) {
2280: mode = REG8(BL);
2281: }
2282: REG8(AL) = mode;
2283: break;
2284: default:
1.1.1.3 root 2285: m_CF = 1;
1.1 root 2286: break;
2287: }
2288: }
2289:
2290: inline void pcbios_int_10h_feh()
2291: {
2292: if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.3 root 2293: SREG(ES) = (TVRAM_TOP >> 4);
2294: i386_load_segment_descriptor(ES);
1.1 root 2295: REG16(DI) = (TVRAM_TOP & 0x0f);
2296: }
2297: int_10h_feh_called = true;
2298: }
2299:
2300: inline void pcbios_int_10h_ffh()
2301: {
2302: if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
2303: COORD co;
2304: DWORD num;
2305:
2306: co.X = (REG16(DI) >> 1) % 80;
2307: co.Y = (REG16(DI) >> 1) / 80;
2308: for(int i = 0, ofs = get_tvram_address(0, co.X, co.Y); i < REG16(CX) && i < 80 * 25; i++) {
2309: scr_char[i] = mem[ofs++];
2310: scr_attr[i] = mem[ofs++];
2311: }
2312: WriteConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
2313: WriteConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
2314: }
2315: int_10h_ffh_called = true;
2316: }
2317:
2318: inline void pcbios_int_15h_23h()
2319: {
2320: switch(REG8(AL)) {
2321: case 0:
2322: REG8(CL) = cmos[0x2d];
2323: REG8(CH) = cmos[0x2e];
2324: break;
2325: case 1:
2326: cmos[0x2d] = REG8(CL);
2327: cmos[0x2e] = REG8(CH);
2328: break;
2329: default:
2330: REG8(AH) = 0x86;
1.1.1.3 root 2331: m_CF = 1;
1.1 root 2332: break;
2333: }
2334: }
2335:
2336: inline void pcbios_int_15h_24h()
2337: {
2338: switch(REG8(AL)) {
2339: case 0:
1.1.1.3 root 2340: i386_set_a20_line(0);
1.1 root 2341: REG8(AH) = 0;
2342: break;
2343: case 1:
1.1.1.3 root 2344: i386_set_a20_line(1);
1.1 root 2345: REG8(AH) = 0;
2346: break;
2347: case 2:
2348: REG8(AH) = 0;
1.1.1.3 root 2349: REG8(AL) = (m_a20_mask >> 20) & 1;
1.1 root 2350: REG16(CX) = 0;
2351: break;
2352: case 3:
2353: REG16(AX) = 0;
2354: REG16(BX) = 0;
2355: break;
2356: }
2357: }
2358:
2359: inline void pcbios_int_15h_49h()
2360: {
2361: REG8(AL) = 0;
2362: REG8(BL) = 0; // DOS/V;
2363: }
2364:
2365: inline void pcbios_int_15h_86h()
2366: {
2367: UINT32 usec = (REG16(CX) << 16) | REG16(DX);
2368: Sleep(usec / 1000);
2369: }
2370:
2371: inline void pcbios_int_15h_87h()
2372: {
2373: // copy extended memory (from DOSBox)
2374: int len = REG16(CX) * 2;
1.1.1.3 root 2375: int ofs = SREG_BASE(ES) + REG16(SI);
1.1 root 2376: int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
2377: int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
2378: memcpy(mem + dst, mem + src, len);
2379: REG16(AX) = 0x00;
2380: }
2381:
2382: inline void pcbios_int_15h_88h()
2383: {
2384: REG16(AX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
2385: }
2386:
2387: inline void pcbios_int_15h_89h()
2388: {
1.1.1.3 root 2389: #if defined(HAS_I386) || defined(HAS_I286)
1.1 root 2390: // switch to protected mode (from DOSBox)
2391: write_io_byte(0x20, 0x10);
2392: write_io_byte(0x21, REG8(BH));
2393: write_io_byte(0x21, 0x00);
2394: write_io_byte(0xa0, 0x10);
2395: write_io_byte(0xa1, REG8(BL));
2396: write_io_byte(0xa1, 0x00);
1.1.1.3 root 2397: i386_set_a20_line(1);
2398: int ofs = SREG_BASE(ES) + REG16(SI);
2399: m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
2400: m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
2401: m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
2402: m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
2403: #if defined(HAS_I386)
2404: m_cr[0] |= 1;
2405: #else
2406: m_msw |= 1;
2407: #endif
2408: SREG(DS) = 0x18;
2409: SREG(ES) = 0x20;
2410: SREG(SS) = 0x28;
2411: i386_load_segment_descriptor(DS);
2412: i386_load_segment_descriptor(ES);
2413: i386_load_segment_descriptor(SS);
1.1 root 2414: REG16(SP) += 6;
1.1.1.3 root 2415: #if defined(HAS_I386)
2416: set_flags(0); // ???
2417: #else
2418: m_flags = 2;
2419: ExpandFlags(m_flags);
2420: #endif
1.1 root 2421: REG16(AX) = 0x00;
2422: i386_jmp_far(0x30, REG16(CX));
2423: #else
2424: REG8(AH) = 0x86;
1.1.1.3 root 2425: m_CF = 1;
1.1 root 2426: #endif
2427: }
2428:
1.1.1.3 root 2429: #if defined(HAS_I386)
1.1 root 2430: inline void pcbios_int_15h_c9h()
2431: {
2432: REG8(AH) = 0x00;
2433: REG8(CH) = cpu_type;
2434: REG8(CL) = cpu_step;
2435: }
1.1.1.3 root 2436: #endif
1.1 root 2437:
2438: inline void pcbios_int_15h_cah()
2439: {
2440: switch(REG8(AL)) {
2441: case 0:
2442: if(REG8(BL) > 0x3f) {
2443: REG8(AH) = 0x03;
1.1.1.3 root 2444: m_CF = 1;
1.1 root 2445: } else if(REG8(BL) < 0x0e) {
2446: REG8(AH) = 0x04;
1.1.1.3 root 2447: m_CF = 1;
1.1 root 2448: } else {
2449: REG8(CL) = cmos[REG8(BL)];
2450: }
2451: break;
2452: case 1:
2453: if(REG8(BL) > 0x3f) {
2454: REG8(AH) = 0x03;
1.1.1.3 root 2455: m_CF = 1;
1.1 root 2456: } else if(REG8(BL) < 0x0e) {
2457: REG8(AH) = 0x04;
1.1.1.3 root 2458: m_CF = 1;
1.1 root 2459: } else {
2460: cmos[REG8(BL)] = REG8(CL);
2461: }
2462: break;
2463: default:
2464: REG8(AH) = 0x86;
1.1.1.3 root 2465: m_CF = 1;
1.1 root 2466: break;
2467: }
2468: }
2469:
2470: UINT32 get_key_code(bool clear_buffer)
2471: {
2472: UINT32 code = 0;
2473:
2474: if(key_buf_char->count() == 0) {
2475: update_key_buffer();
2476: }
2477: if(!clear_buffer) {
2478: key_buf_char->store_buffer();
2479: key_buf_scan->store_buffer();
2480: }
2481: if(key_buf_char->count() != 0) {
2482: code = key_buf_char->read() | (key_buf_scan->read() << 8);
2483: }
2484: if(key_buf_char->count() != 0) {
2485: code |= (key_buf_char->read() << 16) | (key_buf_scan->read() << 24);
2486: }
2487: if(!clear_buffer) {
2488: key_buf_char->restore_buffer();
2489: key_buf_scan->restore_buffer();
2490: }
2491: return code;
2492: }
2493:
2494: inline void pcbios_int_16h_00h()
2495: {
2496: while(key_code == 0) {
2497: key_code = get_key_code(true);
2498: }
2499: if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
2500: if(REG8(AH) == 0x10) {
2501: key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
2502: } else {
2503: key_code = ((key_code >> 16) & 0xff00);
2504: }
2505: }
2506: REG16(AX) = key_code & 0xffff;
2507: key_code >>= 16;
2508: }
2509:
2510: inline void pcbios_int_16h_01h()
2511: {
1.1.1.5 root 2512: static UINT32 key_code_prev = 0;
2513: UINT32 key_code_tmp = key_code;
1.1 root 2514:
1.1.1.5 root 2515: if(key_code_tmp == 0) {
2516: key_code_tmp = get_key_code(false);
2517: }
2518: if(key_code_prev == key_code_tmp && (key_code_tmp & 0xffffff) == 0x00e000 && (key_code_tmp & 0xff000000) != 0) {
2519: key_code_prev = key_code_tmp = 0;
2520: } else {
2521: key_code_prev = key_code_tmp;
2522: }
2523: if(key_code_tmp != 0) {
2524: if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
1.1 root 2525: if(REG8(AH) == 0x11) {
1.1.1.5 root 2526: key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
1.1 root 2527: } else {
1.1.1.5 root 2528: key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
1.1 root 2529: }
2530: }
2531: }
1.1.1.5 root 2532: if(key_code_tmp != 0) {
2533: REG16(AX) = key_code_tmp & 0xffff;
1.1 root 2534: }
1.1.1.3 root 2535: #if defined(HAS_I386)
1.1.1.5 root 2536: m_ZF = (key_code_tmp == 0);
1.1.1.3 root 2537: #else
1.1.1.5 root 2538: m_ZeroVal = (key_code_tmp != 0);
1.1.1.3 root 2539: #endif
1.1 root 2540: }
2541:
2542: inline void pcbios_int_16h_02h()
2543: {
2544: REG8(AL) = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
2545: REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
2546: REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
2547: REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
2548: REG8(AL) |= (GetAsyncKeyState(VK_MENU ) & 0x8000) ? 0x08 : 0;
2549: REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
2550: REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
2551: REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
2552: }
2553:
2554: inline void pcbios_int_16h_03h()
2555: {
2556: static UINT16 status = 0;
2557:
2558: switch(REG8(AL)) {
2559: case 0x05:
2560: status = REG16(BX);
2561: break;
2562: case 0x06:
2563: REG16(BX) = status;
2564: break;
2565: default:
1.1.1.3 root 2566: m_CF = 1;
1.1 root 2567: break;
2568: }
2569: }
2570:
2571: inline void pcbios_int_16h_05h()
2572: {
2573: _ungetch(REG8(CL));
2574: REG8(AL) = 0x00;
2575: }
2576:
2577: inline void pcbios_int_16h_12h()
2578: {
2579: pcbios_int_16h_02h();
2580:
2581: REG8(AH) = 0;//(GetAsyncKeyState(VK_SYSREQ ) & 0x8000) ? 0x80 : 0;
2582: REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
2583: REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
2584: REG8(AH) |= (GetAsyncKeyState(VK_SCROLL ) & 0x8000) ? 0x10 : 0;
2585: REG8(AH) |= (GetAsyncKeyState(VK_RMENU ) & 0x8000) ? 0x08 : 0;
2586: REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
2587: REG8(AH) |= (GetAsyncKeyState(VK_LMENU ) & 0x8000) ? 0x02 : 0;
2588: REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
2589: }
2590:
2591: inline void pcbios_int_16h_13h()
2592: {
2593: static UINT16 status = 0;
2594:
2595: switch(REG8(AL)) {
2596: case 0x00:
2597: status = REG16(DX);
2598: break;
2599: case 0x01:
2600: REG16(DX) = status;
2601: break;
2602: default:
1.1.1.3 root 2603: m_CF = 1;
1.1 root 2604: break;
2605: }
2606: }
2607:
2608: inline void pcbios_int_16h_14h()
2609: {
2610: static UINT8 status = 0;
2611:
2612: switch(REG8(AL)) {
2613: case 0x00:
2614: case 0x01:
2615: status = REG8(AL);
2616: break;
2617: case 0x02:
2618: REG8(AL) = status;
2619: break;
2620: default:
1.1.1.3 root 2621: m_CF = 1;
1.1 root 2622: break;
2623: }
2624: }
2625:
2626: inline void pcbios_int_1ah_00h()
2627: {
2628: static WORD prev_day = 0;
2629: SYSTEMTIME time;
2630:
2631: GetLocalTime(&time);
2632: unsigned __int64 msec = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
2633: unsigned __int64 tick = msec * 0x1800b0 / 24 / 60 / 60 / 1000;
2634: REG16(CX) = (tick >> 16) & 0xffff;
2635: REG16(DX) = (tick ) & 0xffff;
2636: REG8(AL) = (prev_day != 0 && prev_day != time.wDay) ? 1 : 0;
2637: prev_day = time.wDay;
2638: }
2639:
2640: inline int to_bcd(int t)
2641: {
2642: int u = (t % 100) / 10;
2643: return (u << 4) | (t % 10);
2644: }
2645:
2646: inline void pcbios_int_1ah_02h()
2647: {
2648: SYSTEMTIME time;
2649:
2650: GetLocalTime(&time);
2651: REG8(CH) = to_bcd(time.wHour);
2652: REG8(CL) = to_bcd(time.wMinute);
2653: REG8(DH) = to_bcd(time.wSecond);
2654: REG8(DL) = 0x00;
2655: }
2656:
2657: inline void pcbios_int_1ah_04h()
2658: {
2659: SYSTEMTIME time;
2660:
2661: GetLocalTime(&time);
2662: REG8(CH) = to_bcd(time.wYear / 100);
2663: REG8(CL) = to_bcd(time.wYear);
2664: REG8(DH) = to_bcd(time.wMonth);
2665: REG8(DL) = to_bcd(time.wDay);
2666: }
2667:
2668: inline void pcbios_int_1ah_0ah()
2669: {
2670: SYSTEMTIME time;
2671: FILETIME file_time;
2672: WORD dos_date, dos_time;
2673:
2674: GetLocalTime(&time);
2675: SystemTimeToFileTime(&time, &file_time);
2676: FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
2677: REG16(CX) = dos_date;
2678: }
2679:
2680: // msdos system call
2681:
2682: inline void msdos_int_21h_00h()
2683: {
1.1.1.3 root 2684: msdos_process_terminate(SREG(CS), retval, 1);
1.1 root 2685: }
2686:
2687: inline void msdos_int_21h_01h()
2688: {
2689: REG8(AL) = msdos_getche();
2690: #ifdef SUPPORT_HARDWARE
2691: hardware_update();
2692: #endif
2693: }
2694:
2695: inline void msdos_int_21h_02h()
2696: {
2697: msdos_putch(REG8(DL));
2698: }
2699:
2700: inline void msdos_int_21h_03h()
2701: {
2702: REG8(AL) = msdos_aux_in();
2703: }
2704:
2705: inline void msdos_int_21h_04h()
2706: {
2707: msdos_aux_out(REG8(DL));
2708: }
2709:
2710: inline void msdos_int_21h_05h()
2711: {
2712: msdos_prn_out(REG8(DL));
2713: }
2714:
2715: inline void msdos_int_21h_06h()
2716: {
2717: if(REG8(DL) == 0xff) {
2718: if(msdos_kbhit()) {
2719: REG8(AL) = msdos_getch();
1.1.1.3 root 2720: #if defined(HAS_I386)
2721: m_ZF = 0;
2722: #else
2723: m_ZeroVal = 1;
2724: #endif
1.1 root 2725: } else {
2726: REG8(AL) = 0;
1.1.1.3 root 2727: #if defined(HAS_I386)
2728: m_ZF = 1;
2729: #else
2730: m_ZeroVal = 0;
2731: #endif
1.1 root 2732: Sleep(10);
2733: }
2734: } else {
2735: msdos_putch(REG8(DL));
2736: }
2737: }
2738:
2739: inline void msdos_int_21h_07h()
2740: {
2741: REG8(AL) = msdos_getch();
2742: #ifdef SUPPORT_HARDWARE
2743: hardware_update();
2744: #endif
2745: }
2746:
2747: inline void msdos_int_21h_08h()
2748: {
2749: REG8(AL) = msdos_getch();
2750: #ifdef SUPPORT_HARDWARE
2751: hardware_update();
2752: #endif
2753: }
2754:
2755: inline void msdos_int_21h_09h()
2756: {
2757: char tmp[0x10000];
1.1.1.3 root 2758: memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), sizeof(tmp));
1.1 root 2759: tmp[sizeof(tmp) - 1] = '\0';
2760: int len = strlen(my_strtok(tmp, "$"));
2761:
2762: if(file_handler[1].valid && !file_handler[1].atty) {
2763: // stdout is redirected to file
2764: msdos_write(1, tmp, len);
2765: } else {
2766: for(int i = 0; i < len; i++) {
2767: msdos_putch(tmp[i]);
2768: }
2769: }
2770: }
2771:
2772: inline void msdos_int_21h_0ah()
2773: {
1.1.1.3 root 2774: int ofs = SREG_BASE(DS) + REG16(DX);
1.1 root 2775: int max = mem[ofs] - 1;
2776: UINT8 *buf = mem + ofs + 2;
2777: int chr, p = 0;
2778:
2779: while((chr = msdos_getch()) != 0x0d) {
2780: if(chr == 0x00) {
2781: // skip 2nd byte
2782: msdos_getch();
2783: } else if(chr == 0x08) {
2784: // back space
2785: if(p > 0) {
2786: p--;
2787: msdos_putch(chr);
2788: msdos_putch(' ');
2789: msdos_putch(chr);
2790: }
2791: } else if(p < max) {
2792: buf[p++] = chr;
2793: msdos_putch(chr);
2794: }
2795: }
2796: buf[p] = 0x0d;
2797: mem[ofs + 1] = p;
2798: #ifdef SUPPORT_HARDWARE
2799: hardware_update();
2800: #endif
2801: }
2802:
2803: inline void msdos_int_21h_0bh()
2804: {
2805: if(msdos_kbhit()) {
2806: REG8(AL) = 0xff;
2807: } else {
2808: REG8(AL) = 0x00;
2809: Sleep(10);
2810: }
2811: }
2812:
2813: inline void msdos_int_21h_0ch()
2814: {
2815: // clear key buffer
2816: if(file_handler[0].valid && !file_handler[0].atty) {
2817: // stdin is redirected to file
2818: } else {
2819: while(msdos_kbhit()) {
2820: msdos_getch();
2821: }
2822: }
2823:
2824: switch(REG8(AL)) {
2825: case 0x01:
2826: msdos_int_21h_01h();
2827: break;
2828: case 0x06:
2829: msdos_int_21h_06h();
2830: break;
2831: case 0x07:
2832: msdos_int_21h_07h();
2833: break;
2834: case 0x08:
2835: msdos_int_21h_08h();
2836: break;
2837: case 0x0a:
2838: msdos_int_21h_0ah();
2839: break;
2840: default:
2841: REG16(AX) = 0x01;
1.1.1.3 root 2842: m_CF = 1;
1.1 root 2843: break;
2844: }
2845: }
2846:
2847: inline void msdos_int_21h_0dh()
2848: {
2849: }
2850:
2851: inline void msdos_int_21h_0eh()
2852: {
2853: if(REG8(DL) < 26) {
2854: _chdrive(REG8(DL) + 1);
2855: msdos_cds_update(REG8(DL));
2856: }
2857: REG8(AL) = 26; // zdrive
2858: }
2859:
2860: inline void msdos_int_21h_11h()
2861: {
1.1.1.3 root 2862: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
2863: fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 2864:
2865: process_t *process = msdos_process_info_get(current_psp);
2866: ext_fcb_t *ext_find = (ext_fcb_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
2867: find_fcb_t *find = (find_fcb_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l + (ext_fcb->flag == 0xff ? 7 : 0));
2868: char *path = msdos_fcb_path(fcb);
2869: WIN32_FIND_DATA fd;
2870:
2871: if(process->find_handle != INVALID_HANDLE_VALUE) {
2872: FindClose(process->find_handle);
2873: process->find_handle = INVALID_HANDLE_VALUE;
2874: }
2875: strcpy(process->volume_label, msdos_volume_label(path));
2876: process->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
2877:
2878: if((process->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
2879: process->allowable_mask &= ~8;
2880: }
2881: if((process->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
2882: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, 0)) {
2883: if(!FindNextFile(process->find_handle, &fd)) {
2884: FindClose(process->find_handle);
2885: process->find_handle = INVALID_HANDLE_VALUE;
2886: break;
2887: }
2888: }
2889: }
2890: if(process->find_handle != INVALID_HANDLE_VALUE) {
2891: if(ext_fcb->flag == 0xff) {
2892: ext_find->flag = 0xff;
2893: memset(ext_find->reserved, 0, 5);
2894: ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
2895: }
2896: find->drive = _getdrive();
2897: msdos_set_fcb_path((fcb_t *)find, msdos_short_path(fd.cFileName));
2898: find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
2899: find->nt_res = 0;
2900: msdos_find_file_conv_local_time(&fd);
2901: find->create_time_ms = 0;
2902: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
2903: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
2904: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
2905: find->cluster_hi = find->cluster_lo = 0;
2906: find->file_size = fd.nFileSizeLow;
2907: REG8(AL) = 0x00;
2908: } else if(process->allowable_mask & 8) {
2909: if(ext_fcb->flag == 0xff) {
2910: ext_find->flag = 0xff;
2911: memset(ext_find->reserved, 0, 5);
2912: ext_find->attribute = 8;
2913: }
2914: find->drive = _getdrive();
2915: msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
2916: find->attribute = 8;
2917: find->nt_res = 0;
2918: msdos_find_file_conv_local_time(&fd);
2919: find->create_time_ms = 0;
2920: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
2921: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
2922: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
2923: find->cluster_hi = find->cluster_lo = 0;
2924: find->file_size = 0;
2925: process->allowable_mask &= ~8;
2926: REG8(AL) = 0x00;
2927: } else {
2928: REG8(AL) = 0xff;
2929: }
2930: }
2931:
2932: inline void msdos_int_21h_12h()
2933: {
1.1.1.3 root 2934: ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
2935: fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1 root 2936:
2937: process_t *process = msdos_process_info_get(current_psp);
2938: ext_fcb_t *ext_find = (ext_fcb_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
2939: find_fcb_t *find = (find_fcb_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l + (ext_fcb->flag == 0xff ? 7 : 0));
2940: WIN32_FIND_DATA fd;
2941:
2942: if(process->find_handle != INVALID_HANDLE_VALUE) {
2943: if(FindNextFile(process->find_handle, &fd)) {
2944: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, 0)) {
2945: if(!FindNextFile(process->find_handle, &fd)) {
2946: FindClose(process->find_handle);
2947: process->find_handle = INVALID_HANDLE_VALUE;
2948: break;
2949: }
2950: }
2951: } else {
2952: FindClose(process->find_handle);
2953: process->find_handle = INVALID_HANDLE_VALUE;
2954: }
2955: }
2956: if(process->find_handle != INVALID_HANDLE_VALUE) {
2957: if(ext_fcb->flag == 0xff) {
2958: ext_find->flag = 0xff;
2959: memset(ext_find->reserved, 0, 5);
2960: ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
2961: }
2962: find->drive = _getdrive();
2963: msdos_set_fcb_path((fcb_t *)find, msdos_short_path(fd.cFileName));
2964: find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
2965: find->nt_res = 0;
2966: msdos_find_file_conv_local_time(&fd);
2967: find->create_time_ms = 0;
2968: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
2969: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
2970: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
2971: find->cluster_hi = find->cluster_lo = 0;
2972: find->file_size = fd.nFileSizeLow;
2973: REG8(AL) = 0x00;
2974: } else if(process->allowable_mask & 8) {
2975: if(ext_fcb->flag == 0xff) {
2976: ext_find->flag = 0xff;
2977: memset(ext_find->reserved, 0, 5);
2978: ext_find->attribute = 8;
2979: }
2980: find->drive = _getdrive();
2981: msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
2982: find->attribute = 8;
2983: find->nt_res = 0;
2984: msdos_find_file_conv_local_time(&fd);
2985: find->create_time_ms = 0;
2986: FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
2987: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
2988: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
2989: find->cluster_hi = find->cluster_lo = 0;
2990: find->file_size = 0;
2991: process->allowable_mask &= ~8;
2992: REG8(AL) = 0x00;
2993: } else {
2994: REG8(AL) = 0xff;
2995: }
2996: }
2997:
2998: inline void msdos_int_21h_13h()
2999: {
1.1.1.3 root 3000: if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1 root 3001: REG8(AL) = 0xff;
3002: } else {
3003: REG8(AL) = 0x00;
3004: }
3005: }
3006:
3007: inline void msdos_int_21h_18h()
3008: {
3009: REG8(AL) = 0x00;
3010: }
3011:
3012: inline void msdos_int_21h_19h()
3013: {
3014: REG8(AL) = _getdrive() - 1;
3015: }
3016:
3017: inline void msdos_int_21h_1ah()
3018: {
3019: process_t *process = msdos_process_info_get(current_psp);
3020:
3021: process->dta.w.l = REG16(DX);
1.1.1.3 root 3022: process->dta.w.h = SREG(DS);
1.1 root 3023: }
3024:
3025: inline void msdos_int_21h_1bh()
3026: {
3027: int drive_num = _getdrive() - 1;
3028: UINT16 seg, ofs;
3029:
3030: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
3031: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
3032: REG8(AL) = dpb->highest_sector_num + 1;
3033: REG16(CX) = dpb->bytes_per_sector;
3034: REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3 root 3035: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1 root 3036: } else {
3037: REG8(AL) = 0xff;
1.1.1.3 root 3038: m_CF = 1;
1.1 root 3039: }
3040:
3041: }
3042:
3043: inline void msdos_int_21h_1ch()
3044: {
3045: int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
3046: UINT16 seg, ofs;
3047:
3048: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
3049: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
3050: REG8(AL) = dpb->highest_sector_num + 1;
3051: REG16(CX) = dpb->bytes_per_sector;
3052: REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3 root 3053: *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1 root 3054: } else {
3055: REG8(AL) = 0xff;
1.1.1.3 root 3056: m_CF = 1;
1.1 root 3057: }
3058:
3059: }
3060:
3061: inline void msdos_int_21h_1dh()
3062: {
3063: REG8(AL) = 0;
3064: }
3065:
3066: inline void msdos_int_21h_1eh()
3067: {
3068: REG8(AL) = 0;
3069: }
3070:
3071: inline void msdos_int_21h_1fh()
3072: {
3073: int drive_num = _getdrive() - 1;
3074: UINT16 seg, ofs;
3075:
3076: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
3077: REG8(AL) = 0;
1.1.1.3 root 3078: SREG(DS) = seg;
3079: i386_load_segment_descriptor(DS);
1.1 root 3080: REG16(BX) = ofs;
3081: } else {
3082: REG8(AL) = 0xff;
1.1.1.3 root 3083: m_CF = 1;
1.1 root 3084: }
3085: }
3086:
3087: inline void msdos_int_21h_20h()
3088: {
3089: REG8(AL) = 0;
3090: }
3091:
3092: inline void msdos_int_21h_25h()
3093: {
3094: *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3 root 3095: *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1 root 3096: }
3097:
3098: inline void msdos_int_21h_26h()
3099: {
3100: psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
3101:
3102: memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
3103: psp->first_mcb = REG16(DX) + 16;
3104: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
3105: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
3106: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
3107: psp->parent_psp = 0;
3108: }
3109:
3110: inline void msdos_int_21h_29h()
3111: {
1.1.1.3 root 3112: int ofs = SREG_BASE(DS) + REG16(SI);
1.1 root 3113: char name[MAX_PATH], ext[MAX_PATH];
3114: UINT8 drv = 0;
3115: char sep_chars[] = ":.;,=+";
3116: char end_chars[] = "\\<>|/\"[]";
3117: char spc_chars[] = " \t";
3118:
3119: if(REG8(AL) & 1) {
3120: ofs += strspn((char *)&mem[ofs], spc_chars);
3121: if(my_strchr(sep_chars, mem[ofs]) && mem[ofs] != '\0') {
3122: ofs++;
3123: }
3124: }
3125: ofs += strspn((char *)&mem[ofs], spc_chars);
3126:
3127: if(mem[ofs + 1] == ':') {
3128: UINT8 c = mem[ofs];
3129: if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
3130: ; // invalid drive letter
3131: } else {
3132: if(c >= 'a' && c <= 'z') {
3133: drv = c - 'a' + 1;
3134: } else {
3135: drv = c - 'A' + 1;
3136: }
3137: ofs += 2;
3138: }
3139: }
3140: memset(name, 0x20, sizeof(name));
3141: memset(ext, 0x20, sizeof(ext));
3142: for(int i = 0; i < MAX_PATH; i++) {
3143: UINT8 c = mem[ofs];
3144: if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
3145: break;
3146: } else if(c >= 'a' && c <= 'z') {
3147: c -= 0x20;
3148: }
3149: ofs++;
3150: name[i] = c;
3151: }
3152: if(mem[ofs] == '.') {
3153: ofs++;
3154: for(int i = 0; i < MAX_PATH; i++) {
3155: UINT8 c = mem[ofs];
3156: if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
3157: break;
3158: } else if(c >= 'a' && c <= 'z') {
3159: c -= 0x20;
3160: }
3161: ofs++;
3162: ext[i] = c;
3163: }
3164: }
1.1.1.3 root 3165: int si = ofs - SREG_BASE(DS);
3166: int ds = SREG(DS);
1.1 root 3167: while(si > 0xffff) {
3168: si -= 0x10;
3169: ds++;
3170: }
3171: REG16(SI) = si;
1.1.1.3 root 3172: SREG(DS) = ds;
3173: i386_load_segment_descriptor(DS);
1.1 root 3174:
1.1.1.3 root 3175: UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1 root 3176: fcb[0] = drv;
3177: memcpy(fcb + 1, name, 8);
3178: int found_star = 0;
3179: for(int i = 1; i < 1 + 8; i++) {
3180: if(fcb[i] == '*') {
3181: found_star = 1;
3182: }
3183: if(found_star) {
3184: fcb[i] = '?';
3185: }
3186: }
3187: memcpy(fcb + 9, ext, 3);
3188: found_star = 0;
3189: for(int i = 9; i < 9 + 3; i++) {
3190: if(fcb[i] == '*') {
3191: found_star = 1;
3192: }
3193: if(found_star) {
3194: fcb[i] = '?';
3195: }
3196: }
3197:
3198: REG8(AL) = 0x00;
3199: if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
3200: if(memchr(fcb + 1, '?', 8 + 3)) {
3201: REG8(AL) = 0x01;
3202: }
3203: } else {
3204: REG8(AL) = 0xff;
3205: }
3206: }
3207:
3208: inline void msdos_int_21h_2ah()
3209: {
3210: SYSTEMTIME sTime;
3211:
3212: GetLocalTime(&sTime);
3213: REG16(CX) = sTime.wYear;
3214: REG8(DH) = (UINT8)sTime.wMonth;
3215: REG8(DL) = (UINT8)sTime.wDay;
3216: REG8(AL) = (UINT8)sTime.wDayOfWeek;
3217: }
3218:
3219: inline void msdos_int_21h_2bh()
3220: {
3221: REG8(AL) = 0x00;
3222: }
3223:
3224: inline void msdos_int_21h_2ch()
3225: {
3226: SYSTEMTIME sTime;
3227:
3228: GetLocalTime(&sTime);
3229: REG8(CH) = (UINT8)sTime.wHour;
3230: REG8(CL) = (UINT8)sTime.wMinute;
3231: REG8(DH) = (UINT8)sTime.wSecond;
3232: }
3233:
3234: inline void msdos_int_21h_2dh()
3235: {
3236: REG8(AL) = 0x00;
3237: }
3238:
3239: inline void msdos_int_21h_2eh()
3240: {
3241: process_t *process = msdos_process_info_get(current_psp);
3242:
3243: process->verify = REG8(AL);
3244: }
3245:
3246: inline void msdos_int_21h_2fh()
3247: {
3248: process_t *process = msdos_process_info_get(current_psp);
3249:
3250: REG16(BX) = process->dta.w.l;
1.1.1.3 root 3251: SREG(ES) = process->dta.w.h;
3252: i386_load_segment_descriptor(ES);
1.1 root 3253: }
3254:
3255: inline void msdos_int_21h_30h()
3256: {
3257: // Version Flag / OEM
3258: if(REG8(AL) == 1) {
3259: REG8(BH) = 0x00; // not in ROM
3260: } else {
3261: REG8(BH) = 0xff; // OEM = Microsoft
3262: }
3263: // MS-DOS version (7.10)
3264: REG8(AL) = 7;
3265: REG8(AH) = 10;
3266: }
3267:
3268: inline void msdos_int_21h_31h()
3269: {
3270: int mcb_seg = current_psp - 1;
3271: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
3272:
3273: mcb->paragraphs = REG16(DX);
3274: mcb_seg += mcb->paragraphs + 1;
3275: msdos_mcb_create(mcb_seg, 'Z', 0, (MEMORY_END >> 4) - mcb_seg - 1);
3276:
3277: msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
3278: }
3279:
3280: inline void msdos_int_21h_32h()
3281: {
3282: int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
3283: UINT16 seg, ofs;
3284:
3285: if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
3286: REG8(AL) = 0;
1.1.1.3 root 3287: SREG(DS) = seg;
3288: i386_load_segment_descriptor(DS);
1.1 root 3289: REG16(BX) = ofs;
3290: } else {
3291: REG8(AL) = 0xff;
1.1.1.3 root 3292: m_CF = 1;
1.1 root 3293: }
3294: }
3295:
3296: inline void msdos_int_21h_33h()
3297: {
3298: static UINT8 state = 0x00;
3299: char path[MAX_PATH];
3300:
3301: switch(REG8(AL)) {
3302: case 0x00:
3303: REG8(DL) = state;
3304: break;
3305: case 0x01:
3306: state = REG8(DL);
3307: break;
3308: case 0x05:
3309: GetSystemDirectory(path, MAX_PATH);
3310: if(path[0] >= 'a' && path[0] <= 'z') {
3311: REG8(DL) = path[0] - 'a' + 1;
3312: } else {
3313: REG8(DL) = path[0] - 'A' + 1;
3314: }
3315: break;
3316: case 0x06:
1.1.1.2 root 3317: // MS-DOS version (7.10)
1.1 root 3318: REG8(BL) = 7;
1.1.1.2 root 3319: REG8(BH) = 10;
1.1 root 3320: REG8(DL) = 0;
3321: REG8(DH) = 0x10; // in HMA
3322: break;
1.1.1.6 ! root 3323: case 0x07:
! 3324: if(REG8(DL) == 0) {
! 3325: ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
! 3326: } else if(REG8(DL) == 1) {
! 3327: ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
! 3328: }
! 3329: break;
1.1 root 3330: default:
3331: REG16(AX) = 0x01;
1.1.1.3 root 3332: m_CF = 1;
1.1 root 3333: break;
3334: }
3335: }
3336:
3337: inline void msdos_int_21h_35h()
3338: {
3339: REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3 root 3340: SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
3341: i386_load_segment_descriptor(ES);
1.1 root 3342: }
3343:
3344: inline void msdos_int_21h_36h()
3345: {
3346: struct _diskfree_t df = {0};
3347:
3348: if(_getdiskfree(REG8(DL), &df) == 0) {
3349: REG16(AX) = (UINT16)df.sectors_per_cluster;
3350: REG16(CX) = (UINT16)df.bytes_per_sector;
3351: REG16(BX) = (UINT16)df.avail_clusters;
3352: REG16(DX) = (UINT16)df.total_clusters;
3353: } else {
3354: REG16(AX) = 0xffff;
3355: }
3356: }
3357:
3358: inline void msdos_int_21h_37h()
3359: {
3360: process_t *process = msdos_process_info_get(current_psp);
3361:
3362: switch(REG8(AL)) {
3363: case 0x00:
3364: REG8(AL) = 0x00;
3365: REG8(DL) = process->switchar;
3366: break;
3367: case 0x01:
3368: REG8(AL) = 0x00;
3369: process->switchar = REG8(DL);
3370: break;
3371: default:
3372: REG16(AX) = 1;
3373: break;
3374: }
3375: }
3376:
3377: inline void msdos_int_21h_39h(int lfn)
3378: {
1.1.1.3 root 3379: if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 3380: REG16(AX) = errno;
1.1.1.3 root 3381: m_CF = 1;
1.1 root 3382: }
3383: }
3384:
3385: inline void msdos_int_21h_3ah(int lfn)
3386: {
1.1.1.3 root 3387: if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 3388: REG16(AX) = errno;
1.1.1.3 root 3389: m_CF = 1;
1.1 root 3390: }
3391: }
3392:
3393: inline void msdos_int_21h_3bh(int lfn)
3394: {
1.1.1.3 root 3395: if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 3396: REG16(AX) = errno;
1.1.1.3 root 3397: m_CF = 1;
1.1 root 3398: }
3399: }
3400:
3401: inline void msdos_int_21h_3ch()
3402: {
1.1.1.3 root 3403: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 3404: int attr = GetFileAttributes(path);
3405: int fd = -1;
3406:
3407: if(_stricmp(path, "CON") == 0) {
3408: fd = _open(path, _O_WRONLY | _O_BINARY);
3409: } else {
3410: fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
3411: }
3412: if(fd != -1) {
3413: if(attr == -1) {
3414: attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
3415: }
3416: SetFileAttributes(path, attr);
3417: REG16(AX) = fd;
3418: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
3419: } else {
3420: REG16(AX) = errno;
1.1.1.3 root 3421: m_CF = 1;
1.1 root 3422: }
3423: }
3424:
3425: inline void msdos_int_21h_3dh()
3426: {
1.1.1.3 root 3427: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 3428: int mode = REG8(AL) & 0x03;
3429:
3430: if(mode < 0x03) {
3431: int fd = _open(path, file_mode[mode].mode);
3432:
3433: if(fd != -1) {
3434: REG16(AX) = fd;
3435: msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_drive_number(path), current_psp);
3436: } else {
3437: REG16(AX) = errno;
1.1.1.3 root 3438: m_CF = 1;
1.1 root 3439: }
3440: } else {
3441: REG16(AX) = 0x0c;
1.1.1.3 root 3442: m_CF = 1;
1.1 root 3443: }
3444: }
3445:
3446: inline void msdos_int_21h_3eh()
3447: {
3448: process_t *process = msdos_process_info_get(current_psp);
3449:
3450: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
3451: _close(REG16(BX));
3452: msdos_file_handler_close(REG16(BX), current_psp);
3453: } else {
3454: REG16(AX) = 0x06;
1.1.1.3 root 3455: m_CF = 1;
1.1 root 3456: }
3457: }
3458:
3459: inline void msdos_int_21h_3fh()
3460: {
3461: process_t *process = msdos_process_info_get(current_psp);
3462:
3463: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
3464: if(file_mode[file_handler[REG16(BX)].mode].in) {
3465: if(file_handler[REG16(BX)].atty) {
3466: // BX is stdin or is redirected to stdin
1.1.1.3 root 3467: UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
1.1 root 3468: int max = REG16(CX);
3469: int p = 0;
3470:
3471: while(max > p) {
3472: int chr = msdos_getch();
3473:
3474: if(chr == 0x00) {
3475: // skip 2nd byte
3476: msdos_getch();
3477: } else if(chr == 0x0d) {
3478: // carriage return
3479: buf[p++] = 0x0d;
3480: if(max > p) {
3481: buf[p++] = 0x0a;
3482: }
3483: break;
3484: } else if(chr == 0x08) {
3485: // back space
3486: if(p > 0) {
3487: p--;
3488: msdos_putch(chr);
3489: msdos_putch(' ');
3490: msdos_putch(chr);
3491: }
3492: } else {
3493: buf[p++] = chr;
3494: msdos_putch(chr);
3495: }
3496: }
3497: REG16(AX) = p;
3498: #ifdef SUPPORT_HARDWARE
3499: hardware_update();
3500: #endif
3501: } else {
1.1.1.3 root 3502: REG16(AX) = _read(REG16(BX), mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 3503: }
3504: } else {
3505: REG16(AX) = 0x05;
1.1.1.3 root 3506: m_CF = 1;
1.1 root 3507: }
3508: } else {
3509: REG16(AX) = 0x06;
1.1.1.3 root 3510: m_CF = 1;
1.1 root 3511: }
3512: }
3513:
3514: inline void msdos_int_21h_40h()
3515: {
3516: process_t *process = msdos_process_info_get(current_psp);
3517:
3518: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
3519: if(file_mode[file_handler[REG16(BX)].mode].out) {
3520: if(REG16(CX)) {
3521: if(file_handler[REG16(BX)].atty) {
3522: // BX is stdout/stderr or is redirected to stdout
3523: for(int i = 0; i < REG16(CX); i++) {
1.1.1.3 root 3524: msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1 root 3525: }
3526: REG16(AX) = REG16(CX);
3527: } else {
1.1.1.3 root 3528: REG16(AX) = msdos_write(REG16(BX), mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 3529: }
3530: } else {
3531: UINT32 pos = _tell(REG16(BX));
3532: _lseek(REG16(BX), 0, SEEK_END);
3533: UINT32 size = _tell(REG16(BX));
3534: REG16(AX) = 0;
3535: for(UINT32 i = size; i < pos; i++) {
3536: UINT8 tmp = 0;
3537: REG16(AX) += msdos_write(REG16(BX), &tmp, 1);
3538: }
3539: _lseek(REG16(BX), pos, SEEK_SET);
3540: }
3541: } else {
3542: REG16(AX) = 0x05;
1.1.1.3 root 3543: m_CF = 1;
1.1 root 3544: }
3545: } else {
3546: REG16(AX) = 0x06;
1.1.1.3 root 3547: m_CF = 1;
1.1 root 3548: }
3549: }
3550:
3551: inline void msdos_int_21h_41h(int lfn)
3552: {
1.1.1.3 root 3553: if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1 root 3554: REG16(AX) = errno;
1.1.1.3 root 3555: m_CF = 1;
1.1 root 3556: }
3557: }
3558:
3559: inline void msdos_int_21h_42h()
3560: {
3561: process_t *process = msdos_process_info_get(current_psp);
3562:
3563: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
3564: if(REG8(AL) < 0x03) {
3565: static int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
3566: _lseek(REG16(BX), (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
3567: UINT32 pos = _tell(REG16(BX));
3568: REG16(AX) = pos & 0xffff;
3569: REG16(DX) = (pos >> 16);
3570: } else {
3571: REG16(AX) = 0x01;
1.1.1.3 root 3572: m_CF = 1;
1.1 root 3573: }
3574: } else {
3575: REG16(AX) = 0x06;
1.1.1.3 root 3576: m_CF = 1;
1.1 root 3577: }
3578: }
3579:
3580: inline void msdos_int_21h_43h(int lfn)
3581: {
1.1.1.3 root 3582: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1 root 3583: int attr;
3584:
3585: switch(REG8(AL)) {
3586: case 0x00:
3587: if((attr = GetFileAttributes(path)) != -1) {
3588: REG16(CX) = 0;
3589: if(attr & FILE_ATTRIBUTE_READONLY) {
3590: REG16(CX) |= 0x01;
3591: }
3592: if(attr & FILE_ATTRIBUTE_HIDDEN) {
3593: REG16(CX) |= 0x02;
3594: }
3595: if(attr & FILE_ATTRIBUTE_SYSTEM) {
3596: REG16(CX) |= 0x04;
3597: }
3598: if(attr & FILE_ATTRIBUTE_ARCHIVE) {
3599: REG16(CX) |= 0x20;
3600: }
3601: } else {
3602: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 3603: m_CF = 1;
1.1 root 3604: }
3605: break;
3606: case 0x01:
3607: if(SetFileAttributes(path, msdos_file_attribute_create(REG16(CX))) != 0) {
3608: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 3609: m_CF = 1;
1.1 root 3610: }
3611: break;
3612: case 0x02:
3613: break;
3614: case 0x03:
3615: REG16(CX) = 0x00;
3616: break;
3617: default:
3618: REG16(AX) = 0x01;
1.1.1.3 root 3619: m_CF = 1;
1.1 root 3620: break;
3621: }
3622: }
3623:
3624: inline void msdos_int_21h_44h()
3625: {
3626: switch(REG8(AL)) {
3627: case 0x00: // get ioctrl data
3628: REG16(DX) = file_handler[REG16(BX)].info;
3629: break;
3630: case 0x01: // set ioctrl data
3631: file_handler[REG16(BX)].info |= REG8(DL);
3632: break;
3633: case 0x02: // recv from character device
3634: case 0x03: // send to character device
3635: REG16(AX) = 0x06;
1.1.1.3 root 3636: m_CF = 1;
1.1 root 3637: break;
3638: case 0x04: // recv from block device
3639: case 0x05: // send to block device
3640: REG16(AX) = 0x05;
1.1.1.3 root 3641: m_CF = 1;
1.1 root 3642: break;
3643: case 0x06: // get read status
3644: {
3645: process_t *process = msdos_process_info_get(current_psp);
3646:
3647: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
3648: if(file_mode[file_handler[REG16(BX)].mode].in) {
3649: if(file_handler[REG16(BX)].atty) {
3650: REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
3651: } else {
3652: REG8(AL) = eof(REG16(BX)) ? 0x00 : 0xff;
3653: }
3654: } else {
3655: REG8(AL) = 0x00;
3656: }
3657: } else {
3658: REG16(AX) = 0x06;
1.1.1.3 root 3659: m_CF = 1;
1.1 root 3660: }
3661: }
3662: break;
3663: case 0x07: // get write status
3664: {
3665: process_t *process = msdos_process_info_get(current_psp);
3666:
3667: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
3668: if(file_mode[file_handler[REG16(BX)].mode].out) {
3669: REG8(AL) = 0xff;
3670: } else {
3671: REG8(AL) = 0x00;
3672: }
3673: } else {
3674: REG16(AX) = 0x06;
1.1.1.3 root 3675: m_CF = 1;
1.1 root 3676: }
3677: }
3678: break;
3679: case 0x08: // check removable drive
3680: if(REG8(BL) < ('Z' - 'A' + 1)) {
3681: UINT32 val;
3682: if(REG8(BL) == 0) {
3683: val = GetDriveType(NULL);
3684: } else if(REG8(BL) < ('Z' - 'A' + 1)) {
3685: char tmp[8];
3686: sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
3687: val = GetDriveType(tmp);
3688: }
3689: if(val == DRIVE_NO_ROOT_DIR) {
3690: // no drive
3691: REG16(AX) = 0x0f;
1.1.1.3 root 3692: m_CF = 1;
1.1 root 3693: } else if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
3694: // removable drive
3695: REG16(AX) = 0x00;
3696: } else {
3697: // fixed drive
3698: REG16(AX) = 0x01;
3699: }
3700: } else {
3701: // invalid drive number
3702: REG16(AX) = 0x0f;
1.1.1.3 root 3703: m_CF = 1;
1.1 root 3704: }
3705: break;
3706: case 0x09: // check remote drive
3707: if(REG8(BL) < ('Z' - 'A' + 1)) {
3708: UINT32 val;
3709: if(REG8(BL) == 0) {
3710: val = GetDriveType(NULL);
3711: } else if(REG8(BL) < ('Z' - 'A' + 1)) {
3712: char tmp[8];
3713: sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
3714: val = GetDriveType(tmp);
3715: }
3716: if(val == DRIVE_NO_ROOT_DIR) {
3717: // no drive
3718: REG16(AX) = 0x0f;
1.1.1.3 root 3719: m_CF = 1;
1.1 root 3720: } else if(val == DRIVE_REMOTE) {
3721: // remote drive
3722: REG16(DX) = 0x1000;
3723: } else {
3724: // local drive
3725: REG16(DX) = 0x00;
3726: }
3727: } else {
3728: // invalid drive number
3729: REG16(AX) = 0x0f;
1.1.1.3 root 3730: m_CF = 1;
1.1 root 3731: }
3732: break;
3733: case 0x0b: // set retry count
3734: break;
3735: default:
3736: REG16(AX) = 0x01;
1.1.1.3 root 3737: m_CF = 1;
1.1 root 3738: break;
3739: }
3740: }
3741:
3742: inline void msdos_int_21h_45h()
3743: {
3744: process_t *process = msdos_process_info_get(current_psp);
3745:
3746: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
3747: int fd = _dup(REG16(BX));
3748: if(fd != -1) {
3749: REG16(AX) = fd;
3750: msdos_file_handler_dup(REG16(AX), REG16(BX), current_psp);
3751: } else {
3752: REG16(AX) = errno;
1.1.1.3 root 3753: m_CF = 1;
1.1 root 3754: }
3755: } else {
3756: REG16(AX) = 0x06;
1.1.1.3 root 3757: m_CF = 1;
1.1 root 3758: }
3759: }
3760:
3761: inline void msdos_int_21h_46h()
3762: {
3763: process_t *process = msdos_process_info_get(current_psp);
3764:
3765: if(REG16(BX) < process->max_files && REG16(CX) < process->max_files && file_handler[REG16(BX)].valid) {
3766: if(_dup2(REG16(BX), REG16(CX)) != -1) {
3767: msdos_file_handler_dup(REG16(CX), REG16(BX), current_psp);
3768: } else {
3769: REG16(AX) = errno;
1.1.1.3 root 3770: m_CF = 1;
1.1 root 3771: }
3772: } else {
3773: REG16(AX) = 0x06;
1.1.1.3 root 3774: m_CF = 1;
1.1 root 3775: }
3776: }
3777:
3778: inline void msdos_int_21h_47h(int lfn)
3779: {
3780: char path[MAX_PATH];
3781:
3782: if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
3783: if(path[1] == ':') {
3784: // the returned path does not include a drive or the initial backslash
1.1.1.3 root 3785: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1 root 3786: } else {
1.1.1.3 root 3787: strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1 root 3788: }
3789: } else {
3790: REG16(AX) = errno;
1.1.1.3 root 3791: m_CF = 1;
1.1 root 3792: }
3793: }
3794:
3795: inline void msdos_int_21h_48h()
3796: {
3797: int seg;
3798:
3799: if((seg = msdos_mem_alloc(REG16(BX), 0)) != -1) {
3800: REG16(AX) = seg;
3801: } else {
3802: REG16(AX) = 0x08;
3803: REG16(BX) = msdos_mem_get_free(0);
1.1.1.3 root 3804: m_CF = 1;
1.1 root 3805: }
3806: }
3807:
3808: inline void msdos_int_21h_49h()
3809: {
1.1.1.3 root 3810: msdos_mem_free(SREG(ES));
1.1 root 3811: }
3812:
3813: inline void msdos_int_21h_4ah()
3814: {
3815: int max_paragraphs;
3816:
1.1.1.3 root 3817: if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
1.1 root 3818: REG16(AX) = 0x08;
3819: REG16(BX) = max_paragraphs;
1.1.1.3 root 3820: m_CF = 1;
1.1 root 3821: }
3822: }
3823:
3824: inline void msdos_int_21h_4bh()
3825: {
1.1.1.3 root 3826: char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
3827: param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1 root 3828:
3829: switch(REG8(AL)) {
3830: case 0x00:
3831: case 0x01:
3832: if(msdos_process_exec(command, param, REG8(AL))) {
3833: REG16(AX) = 0x02;
1.1.1.3 root 3834: m_CF = 1;
1.1 root 3835: }
3836: break;
3837: default:
3838: REG16(AX) = 0x01;
1.1.1.3 root 3839: m_CF = 1;
1.1 root 3840: break;
3841: }
3842: }
3843:
3844: inline void msdos_int_21h_4ch()
3845: {
3846: msdos_process_terminate(current_psp, REG8(AL), 1);
3847: }
3848:
3849: inline void msdos_int_21h_4dh()
3850: {
3851: REG16(AX) = retval;
3852: }
3853:
3854: inline void msdos_int_21h_4eh()
3855: {
3856: process_t *process = msdos_process_info_get(current_psp);
3857: find_t *find = (find_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
1.1.1.3 root 3858: char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 3859: WIN32_FIND_DATA fd;
3860:
3861: if(process->find_handle != INVALID_HANDLE_VALUE) {
3862: FindClose(process->find_handle);
3863: process->find_handle = INVALID_HANDLE_VALUE;
3864: }
3865: strcpy(process->volume_label, msdos_volume_label(path));
3866: process->allowable_mask = REG8(CL);
3867:
3868: if((process->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
3869: process->allowable_mask &= ~8;
3870: }
3871: if((process->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
3872: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, 0)) {
3873: if(!FindNextFile(process->find_handle, &fd)) {
3874: FindClose(process->find_handle);
3875: process->find_handle = INVALID_HANDLE_VALUE;
3876: break;
3877: }
3878: }
3879: }
3880: if(process->find_handle != INVALID_HANDLE_VALUE) {
3881: find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
3882: msdos_find_file_conv_local_time(&fd);
3883: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
3884: find->size = fd.nFileSizeLow;
3885: strcpy(find->name, msdos_short_path(fd.cFileName));
3886: REG16(AX) = 0;
3887: } else if(process->allowable_mask & 8) {
3888: find->attrib = 8;
3889: find->size = 0;
3890: strcpy(find->name, msdos_short_volume_label(process->volume_label));
3891: process->allowable_mask &= ~8;
3892: REG16(AX) = 0;
3893: } else {
3894: REG16(AX) = 0x12; // NOTE: return 0x02 if file path is invalid
1.1.1.3 root 3895: m_CF = 1;
1.1 root 3896: }
3897: }
3898:
3899: inline void msdos_int_21h_4fh()
3900: {
3901: process_t *process = msdos_process_info_get(current_psp);
3902: find_t *find = (find_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
3903: WIN32_FIND_DATA fd;
3904:
3905: if(process->find_handle != INVALID_HANDLE_VALUE) {
3906: if(FindNextFile(process->find_handle, &fd)) {
3907: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, 0)) {
3908: if(!FindNextFile(process->find_handle, &fd)) {
3909: FindClose(process->find_handle);
3910: process->find_handle = INVALID_HANDLE_VALUE;
3911: break;
3912: }
3913: }
3914: } else {
3915: FindClose(process->find_handle);
3916: process->find_handle = INVALID_HANDLE_VALUE;
3917: }
3918: }
3919: if(process->find_handle != INVALID_HANDLE_VALUE) {
3920: find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
3921: msdos_find_file_conv_local_time(&fd);
3922: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
3923: find->size = fd.nFileSizeLow;
3924: strcpy(find->name, msdos_short_path(fd.cFileName));
3925: REG16(AX) = 0;
3926: } else if(process->allowable_mask & 8) {
3927: find->attrib = 8;
3928: find->size = 0;
3929: strcpy(find->name, msdos_short_volume_label(process->volume_label));
3930: process->allowable_mask &= ~8;
3931: REG16(AX) = 0;
3932: } else {
3933: REG16(AX) = 0x12;
1.1.1.3 root 3934: m_CF = 1;
1.1 root 3935: }
3936: }
3937:
3938: inline void msdos_int_21h_50h()
3939: {
3940: current_psp = REG16(BX);
3941: }
3942:
3943: inline void msdos_int_21h_51h()
3944: {
3945: REG16(BX) = current_psp;
3946: }
3947:
3948: inline void msdos_int_21h_52h()
3949: {
1.1.1.3 root 3950: SREG(ES) = (DOS_INFO_BASE >> 4);
3951: i386_load_segment_descriptor(ES);
1.1 root 3952: REG16(BX) = (DOS_INFO_BASE & 0x0f);
3953: }
3954:
3955: inline void msdos_int_21h_54h()
3956: {
3957: process_t *process = msdos_process_info_get(current_psp);
3958:
3959: REG8(AL) = process->verify;
3960: }
3961:
3962: inline void msdos_int_21h_55h()
3963: {
3964: psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
3965:
3966: memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
3967: psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
3968: psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
3969: psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
3970: psp->parent_psp = current_psp;
3971: }
3972:
3973: inline void msdos_int_21h_56h(int lfn)
3974: {
3975: char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3 root 3976: strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
3977: strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1 root 3978:
3979: if(rename(src, dst)) {
3980: REG16(AX) = errno;
1.1.1.3 root 3981: m_CF = 1;
1.1 root 3982: }
3983: }
3984:
3985: inline void msdos_int_21h_57h()
3986: {
3987: FILETIME time, local;
1.1.1.6 ! root 3988: HANDLE handle;
1.1 root 3989:
3990: switch(REG8(AL)) {
3991: case 0x00:
1.1.1.6 ! root 3992: if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
! 3993: REG16(AX) = (UINT16)GetLastError();
! 3994: m_CF = 1;
! 3995: } if(!GetFileTime(handle, NULL, NULL, &time)) {
! 3996: REG16(AX) = (UINT16)GetLastError();
! 3997: m_CF = 1;
! 3998: } else {
1.1 root 3999: FileTimeToLocalFileTime(&time, &local);
4000: FileTimeToDosDateTime(&local, ®16(DX), ®16(CX));
1.1.1.6 ! root 4001: }
! 4002: break;
! 4003: case 0x01:
! 4004: DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
! 4005: LocalFileTimeToFileTime(&local, &time);
! 4006: if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
! 4007: REG16(AX) = (UINT16)GetLastError();
! 4008: m_CF = 1;
! 4009: } else if(!SetFileTime(handle, NULL, NULL, &time)) {
! 4010: REG16(AX) = (UINT16)GetLastError();
! 4011: m_CF = 1;
! 4012: }
! 4013: break;
! 4014: case 0x04:
! 4015: if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
! 4016: REG16(AX) = (UINT16)GetLastError();
! 4017: m_CF = 1;
! 4018: } if(!GetFileTime(handle, NULL, &time, NULL)) {
! 4019: REG16(AX) = (UINT16)GetLastError();
! 4020: m_CF = 1;
1.1 root 4021: } else {
1.1.1.6 ! root 4022: FileTimeToLocalFileTime(&time, &local);
! 4023: FileTimeToDosDateTime(&local, ®16(DX), ®16(CX));
! 4024: }
! 4025: break;
! 4026: case 0x05:
! 4027: DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
! 4028: LocalFileTimeToFileTime(&local, &time);
! 4029: if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
! 4030: REG16(AX) = (UINT16)GetLastError();
! 4031: m_CF = 1;
! 4032: } else if(!SetFileTime(handle, NULL, &time, NULL)) {
1.1 root 4033: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 4034: m_CF = 1;
1.1 root 4035: }
4036: break;
1.1.1.6 ! root 4037: case 0x06:
! 4038: if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
! 4039: REG16(AX) = (UINT16)GetLastError();
! 4040: m_CF = 1;
! 4041: } if(!GetFileTime(handle, &time, NULL, NULL)) {
! 4042: REG16(AX) = (UINT16)GetLastError();
! 4043: m_CF = 1;
! 4044: } else {
! 4045: FileTimeToLocalFileTime(&time, &local);
! 4046: FileTimeToDosDateTime(&local, ®16(DX), ®16(CX));
! 4047: }
! 4048: break;
! 4049: case 0x07:
1.1 root 4050: DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
4051: LocalFileTimeToFileTime(&local, &time);
1.1.1.6 ! root 4052: if((handle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
! 4053: REG16(AX) = (UINT16)GetLastError();
! 4054: m_CF = 1;
! 4055: } else if(!SetFileTime(handle, &time, NULL, NULL)) {
1.1 root 4056: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 4057: m_CF = 1;
1.1 root 4058: }
4059: break;
4060: default:
4061: REG16(AX) = 0x01;
1.1.1.3 root 4062: m_CF = 1;
1.1 root 4063: break;
4064: }
4065: }
4066:
4067: inline void msdos_int_21h_58h()
4068: {
4069: switch(REG8(AL)) {
4070: case 0x00:
4071: REG16(AX) = 0x00;
4072: break;
4073: default:
4074: REG16(AX) = 0x01;
1.1.1.3 root 4075: m_CF = 1;
1.1 root 4076: break;
4077: }
4078: }
4079:
4080: inline void msdos_int_21h_59h()
4081: {
4082: REG16(AX) = error_code;
4083: switch(error_code) {
4084: case 4: // Too many open files
4085: case 8: // Insufficient memory
4086: REG8(BH) = 1; // Out of resource
4087: break;
4088: case 5: // Access denied
4089: REG8(BH) = 3; // Authorization
4090: break;
4091: case 7: // Memory control block destroyed
4092: REG8(BH) = 4; // Internal
4093: break;
4094: case 2: // File not found
4095: case 3: // Path not found
4096: case 15: // Invaid drive specified
4097: case 18: // No more files
4098: REG8(BH) = 8; // Not found
4099: break;
4100: case 32: // Sharing violation
4101: case 33: // Lock violation
4102: REG8(BH) = 10; // Locked
4103: break;
4104: // case 16: // Removal of current directory attempted
4105: case 19: // Attempted write on protected disk
4106: case 21: // Drive not ready
4107: // case 29: // Write failure
4108: // case 30: // Read failure
4109: // case 82: // Cannot create subdirectory
4110: REG8(BH) = 11; // Media
4111: break;
4112: case 80: // File already exists
4113: REG8(BH) = 12; // Already exist
4114: break;
4115: default:
4116: REG8(BH) = 13; // Unknown
4117: break;
4118: }
4119: REG8(BL) = 1; // Retry
4120: REG8(CH) = 1; // Unknown
4121: }
4122:
4123: inline void msdos_int_21h_5ah()
4124: {
1.1.1.3 root 4125: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 4126: int len = strlen(path);
4127: char tmp[MAX_PATH];
4128:
4129: if(GetTempFileName(path, "TMP", 0, tmp)) {
4130: int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
4131:
4132: SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
4133: REG16(AX) = fd;
4134: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
4135:
4136: strcpy(path, tmp);
4137: int dx = REG16(DX) + len;
1.1.1.3 root 4138: int ds = SREG(DS);
1.1 root 4139: while(dx > 0xffff) {
4140: dx -= 0x10;
4141: ds++;
4142: }
4143: REG16(DX) = dx;
1.1.1.3 root 4144: SREG(DS) = ds;
4145: i386_load_segment_descriptor(DS);
1.1 root 4146: } else {
4147: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 4148: m_CF = 1;
1.1 root 4149: }
4150: }
4151:
4152: inline void msdos_int_21h_5bh()
4153: {
1.1.1.3 root 4154: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1 root 4155:
4156: if(_access(path, 0) == 0) {
4157: // already exists
4158: REG16(AX) = 0x50;
1.1.1.3 root 4159: m_CF = 1;
1.1 root 4160: } else {
4161: int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
4162:
4163: if(fd != -1) {
4164: SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
4165: REG16(AX) = fd;
4166: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
4167: } else {
4168: REG16(AX) = errno;
1.1.1.3 root 4169: m_CF = 1;
1.1 root 4170: }
4171: }
4172: }
4173:
4174: inline void msdos_int_21h_5ch()
4175: {
4176: process_t *process = msdos_process_info_get(current_psp);
4177:
4178: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
4179: if(REG8(AL) == 0 || REG8(AL) == 1) {
4180: static int modes[2] = {_LK_LOCK, _LK_UNLCK};
4181: UINT32 pos = _tell(REG16(BX));
4182: _lseek(REG16(BX), (REG16(CX) << 16) | REG16(DX), SEEK_SET);
4183: if(_locking(REG16(BX), modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
4184: REG16(AX) = errno;
1.1.1.3 root 4185: m_CF = 1;
1.1 root 4186: }
4187: _lseek(REG16(BX), pos, SEEK_SET);
4188: #ifdef SUPPORT_HARDWARE
4189: // some seconds may be passed in _locking()
4190: hardware_update();
4191: #endif
4192: } else {
4193: REG16(AX) = 0x01;
1.1.1.3 root 4194: m_CF = 1;
1.1 root 4195: }
4196: } else {
4197: REG16(AX) = 0x06;
1.1.1.3 root 4198: m_CF = 1;
1.1 root 4199: }
4200: }
4201:
4202: inline void msdos_int_21h_60h(int lfn)
4203: {
4204: if(lfn) {
4205: char full[MAX_PATH], *name;
1.1.1.3 root 4206: GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
4207: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), full);
1.1 root 4208: } else {
1.1.1.3 root 4209: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1 root 4210: }
4211: }
4212:
4213: inline void msdos_int_21h_61h()
4214: {
4215: REG8(AL) = 0;
4216: }
4217:
4218: inline void msdos_int_21h_62h()
4219: {
4220: REG16(BX) = current_psp;
4221: }
4222:
4223: inline void msdos_int_21h_63h()
4224: {
4225: switch(REG8(AL)) {
4226: case 0x00:
1.1.1.3 root 4227: SREG(DS) = (DBCS_TABLE >> 4);
4228: i386_load_segment_descriptor(DS);
1.1 root 4229: REG16(SI) = (DBCS_TABLE & 0x0f);
4230: REG8(AL) = 0x00;
4231: break;
4232: default:
4233: REG16(AX) = 0x01;
1.1.1.3 root 4234: m_CF = 1;
1.1 root 4235: break;
4236: }
4237: }
4238:
4239: inline void msdos_int_21h_65h()
4240: {
4241: char tmp[0x10000];
4242:
4243: switch(REG8(AL)) {
4244: case 0x07:
1.1.1.3 root 4245: *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
4246: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
4247: *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1 root 4248: REG16(CX) = 0x05;
4249: break;
4250: case 0x20:
4251: sprintf(tmp, "%c", REG8(DL));
4252: my_strupr(tmp);
4253: REG8(DL) = tmp[0];
4254: break;
4255: case 0x21:
4256: memset(tmp, 0, sizeof(tmp));
1.1.1.3 root 4257: memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1 root 4258: my_strupr(tmp);
1.1.1.3 root 4259: memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1 root 4260: break;
4261: case 0x22:
1.1.1.3 root 4262: my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1 root 4263: break;
4264: default:
4265: REG16(AX) = 0x01;
1.1.1.3 root 4266: m_CF = 1;
1.1 root 4267: break;
4268: }
4269: }
4270:
4271: inline void msdos_int_21h_66h()
4272: {
4273: switch(REG8(AL)) {
4274: case 0x01:
4275: REG16(BX) = active_code_page;
4276: REG16(DX) = system_code_page;
4277: break;
4278: case 0x02:
4279: if(active_code_page == REG16(BX)) {
4280: REG16(AX) = 0xeb41;
4281: } else if(_setmbcp(REG16(BX)) == 0) {
4282: active_code_page = REG16(BX);
4283: msdos_dbcs_table_update();
4284: REG16(AX) = 0xeb41;
4285: } else {
4286: REG16(AX) = 0x25;
1.1.1.3 root 4287: m_CF = 1;
1.1 root 4288: }
4289: break;
4290: default:
4291: REG16(AX) = 0x01;
1.1.1.3 root 4292: m_CF = 1;
1.1 root 4293: break;
4294: }
4295: }
4296:
4297: inline void msdos_int_21h_67h()
4298: {
4299: process_t *process = msdos_process_info_get(current_psp);
4300:
4301: if(REG16(BX) <= MAX_FILES) {
4302: process->max_files = max(REG16(BX), 20);
4303: } else {
4304: REG16(AX) = 0x08;
1.1.1.3 root 4305: m_CF = 1;
1.1 root 4306: }
4307: }
4308:
4309: inline void msdos_int_21h_68h()
4310: {
4311: process_t *process = msdos_process_info_get(current_psp);
4312:
4313: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
4314: // fflush(_fdopen(REG16(BX), ""));
4315: } else {
4316: REG16(AX) = 0x06;
1.1.1.3 root 4317: m_CF = 1;
1.1 root 4318: }
4319: }
4320:
4321: inline void msdos_int_21h_69h()
4322: {
1.1.1.3 root 4323: drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 4324: char path[] = "A:\\";
4325: char volume_label[MAX_PATH];
4326: DWORD serial_number = 0;
4327: char file_system[MAX_PATH];
4328:
4329: if(REG8(BL) == 0) {
4330: path[0] = 'A' + _getdrive() - 1;
4331: } else {
4332: path[0] = 'A' + REG8(BL) - 1;
4333: }
4334:
4335: switch(REG8(AL)) {
4336: case 0x00:
4337: if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
4338: info->info_level = 0;
4339: info->serial_number = serial_number;
4340: memset(info->volume_label, 0x20, 11);
4341: memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
4342: memset(info->file_system, 0x20, 8);
4343: memcpy(info->file_system, file_system, min(strlen(file_system), 8));
4344: } else {
4345: REG16(AX) = errno;
1.1.1.3 root 4346: m_CF = 1;
1.1 root 4347: }
4348: break;
4349: case 0x01:
4350: REG16(AX) = 0x03;
1.1.1.3 root 4351: m_CF = 1;
1.1 root 4352: }
4353: }
4354:
4355: inline void msdos_int_21h_6ah()
4356: {
4357: REG8(AH) = 0x68;
4358: msdos_int_21h_68h();
4359: }
4360:
4361: inline void msdos_int_21h_6bh()
4362: {
4363: REG8(AL) = 0;
4364: }
4365:
4366: inline void msdos_int_21h_6ch(int lfn)
4367: {
1.1.1.3 root 4368: char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1 root 4369: int mode = REG8(BL) & 0x03;
4370:
4371: if(mode < 0x03) {
4372: if(_access(path, 0) == 0) {
4373: // file exists
4374: if(REG8(DL) & 1) {
4375: int fd = _open(path, file_mode[mode].mode);
4376:
4377: if(fd != -1) {
4378: REG16(AX) = fd;
4379: REG16(CX) = 1;
4380: msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_drive_number(path), current_psp);
4381: } else {
4382: REG16(AX) = errno;
1.1.1.3 root 4383: m_CF = 1;
1.1 root 4384: }
4385: } else if(REG8(DL) & 2) {
4386: int attr = GetFileAttributes(path);
4387: int fd = -1;
4388:
4389: if(_stricmp(path, "CON") == 0) {
4390: fd = _open(path, file_mode[mode].mode);
4391: } else {
4392: fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
4393: }
4394: if(fd != -1) {
4395: if(attr == -1) {
4396: attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
4397: }
4398: SetFileAttributes(path, attr);
4399: REG16(AX) = fd;
4400: REG16(CX) = 3;
4401: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
4402: } else {
4403: REG16(AX) = errno;
1.1.1.3 root 4404: m_CF = 1;
1.1 root 4405: }
4406: } else {
4407: REG16(AX) = 0x50;
1.1.1.3 root 4408: m_CF = 1;
1.1 root 4409: }
4410: } else {
4411: // file not exists
4412: if(REG8(DL) & 0x10) {
4413: int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
4414:
4415: if(fd != -1) {
4416: SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
4417: REG16(AX) = fd;
4418: REG16(CX) = 2;
4419: msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
4420: } else {
4421: REG16(AX) = errno;
1.1.1.3 root 4422: m_CF = 1;
1.1 root 4423: }
4424: } else {
4425: REG16(AX) = 0x02;
1.1.1.3 root 4426: m_CF = 1;
1.1 root 4427: }
4428: }
4429: } else {
4430: REG16(AX) = 0x0c;
1.1.1.3 root 4431: m_CF = 1;
1.1 root 4432: }
4433: }
4434:
4435: inline void msdos_int_21h_710dh()
4436: {
4437: // reset drive
4438: }
4439:
4440: inline void msdos_int_21h_714eh()
4441: {
4442: process_t *process = msdos_process_info_get(current_psp);
1.1.1.3 root 4443: find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
4444: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 4445: WIN32_FIND_DATA fd;
4446:
4447: if(process->find_handle != INVALID_HANDLE_VALUE) {
4448: FindClose(process->find_handle);
4449: process->find_handle = INVALID_HANDLE_VALUE;
4450: }
4451: strcpy(process->volume_label, msdos_volume_label(path));
4452: process->allowable_mask = REG8(CL);
4453: process->required_mask = REG8(CH);
4454:
4455: if((process->allowable_mask & 8) && !msdos_match_volume_label(path, process->volume_label) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
4456: process->allowable_mask &= ~8;
4457: }
4458: if((process->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
4459: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, process->required_mask)) {
4460: if(!FindNextFile(process->find_handle, &fd)) {
4461: FindClose(process->find_handle);
4462: process->find_handle = INVALID_HANDLE_VALUE;
4463: break;
4464: }
4465: }
4466: }
4467: if(process->find_handle != INVALID_HANDLE_VALUE) {
4468: find->attrib = fd.dwFileAttributes;
4469: msdos_find_file_conv_local_time(&fd);
4470: if(REG16(SI) == 0) {
4471: find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
4472: find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
4473: find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
4474: find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
4475: find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
4476: find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
4477: } else {
4478: FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
4479: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
4480: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
4481: }
4482: find->size_hi = fd.nFileSizeHigh;
4483: find->size_lo = fd.nFileSizeLow;
4484: strcpy(find->full_name, fd.cFileName);
4485: strcpy(find->short_name, msdos_short_path(fd.cFileName));
4486: } else if(process->allowable_mask & 8) {
4487: // volume label
4488: find->attrib = 8;
4489: find->size_hi = find->size_lo = 0;
4490: strcpy(find->full_name, process->volume_label);
4491: strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
4492: process->allowable_mask &= ~8;
4493: } else {
4494: REG16(AX) = 0x12; // NOTE: return 0x02 if file path is invalid
1.1.1.3 root 4495: m_CF = 1;
1.1 root 4496: }
4497: }
4498:
4499: inline void msdos_int_21h_714fh()
4500: {
4501: process_t *process = msdos_process_info_get(current_psp);
1.1.1.3 root 4502: find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1 root 4503: WIN32_FIND_DATA fd;
4504:
4505: if(process->find_handle != INVALID_HANDLE_VALUE) {
4506: if(FindNextFile(process->find_handle, &fd)) {
4507: while(!msdos_find_file_check_attribute(fd.dwFileAttributes, process->allowable_mask, process->required_mask)) {
4508: if(!FindNextFile(process->find_handle, &fd)) {
4509: FindClose(process->find_handle);
4510: process->find_handle = INVALID_HANDLE_VALUE;
4511: break;
4512: }
4513: }
4514: } else {
4515: FindClose(process->find_handle);
4516: process->find_handle = INVALID_HANDLE_VALUE;
4517: }
4518: }
4519: if(process->find_handle != INVALID_HANDLE_VALUE) {
4520: find->attrib = fd.dwFileAttributes;
4521: msdos_find_file_conv_local_time(&fd);
4522: if(REG16(SI) == 0) {
4523: find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
4524: find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
4525: find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
4526: find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
4527: find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
4528: find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
4529: } else {
4530: FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
4531: FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
4532: FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
4533: }
4534: find->size_hi = fd.nFileSizeHigh;
4535: find->size_lo = fd.nFileSizeLow;
4536: strcpy(find->full_name, fd.cFileName);
4537: strcpy(find->short_name, msdos_short_path(fd.cFileName));
4538: } else if(process->allowable_mask & 8) {
4539: // volume label
4540: find->attrib = 8;
4541: find->size_hi = find->size_lo = 0;
4542: strcpy(find->full_name, process->volume_label);
4543: strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
4544: process->allowable_mask &= ~8;
4545: } else {
4546: REG16(AX) = 0x12;
1.1.1.3 root 4547: m_CF = 1;
1.1 root 4548: }
4549: }
4550:
4551: inline void msdos_int_21h_71a0h()
4552: {
4553: DWORD max_component_len, file_sys_flag;
4554:
1.1.1.3 root 4555: if(GetVolumeInformation((char *)(mem + SREG_BASE(DS) + REG16(DX)), NULL, 0, NULL, &max_component_len, &file_sys_flag, (char *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX))) {
1.1 root 4556: REG16(BX) = (UINT16)file_sys_flag;
4557: REG16(CX) = (UINT16)max_component_len; // 255
4558: REG16(DX) = (UINT16)max_component_len + 5; // 260
4559: } else {
4560: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 4561: m_CF = 1;
1.1 root 4562: }
4563: }
4564:
4565: inline void msdos_int_21h_71a1h()
4566: {
4567: process_t *process = msdos_process_info_get(current_psp);
4568: find_t *find = (find_t *)(mem + (process->dta.w.h << 4) + process->dta.w.l);
4569:
4570: if(process->find_handle != INVALID_HANDLE_VALUE) {
4571: FindClose(process->find_handle);
4572: process->find_handle = INVALID_HANDLE_VALUE;
4573: }
4574: }
4575:
4576: inline void msdos_int_21h_71a6h()
4577: {
4578: process_t *process = msdos_process_info_get(current_psp);
1.1.1.3 root 4579: UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1 root 4580: struct _stat64 status;
4581: DWORD serial_number = 0;
4582:
4583: if(REG16(BX) < process->max_files && file_handler[REG16(BX)].valid) {
4584: if(_fstat64(REG16(BX), &status) == 0) {
4585: if(file_handler[REG16(BX)].path[1] == ':') {
4586: // NOTE: we need to consider the network file path "\\host\share\"
4587: char volume[] = "A:\\";
4588: volume[0] = file_handler[REG16(BX)].path[1];
4589: GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
4590: }
4591: *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[REG16(BX)].path);
4592: *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
4593: *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
4594: *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
4595: *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
4596: *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
4597: *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
4598: *(UINT32 *)(buffer + 0x1c) = serial_number;
4599: *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
4600: *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
4601: *(UINT32 *)(buffer + 0x28) = status.st_nlink;
4602: // this is dummy id and it will be changed when it is reopend...
4603: *(UINT32 *)(buffer + 0x2c) = 0;
4604: *(UINT32 *)(buffer + 0x30) = file_handler[REG16(BX)].id;
4605: } else {
4606: REG16(AX) = errno;
1.1.1.3 root 4607: m_CF = 1;
1.1 root 4608: }
4609: } else {
4610: REG16(AX) = 0x06;
1.1.1.3 root 4611: m_CF = 1;
1.1 root 4612: }
4613: }
4614:
4615: inline void msdos_int_21h_71a7h()
4616: {
4617: switch(REG8(BL)) {
4618: case 0x00:
1.1.1.3 root 4619: if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), ®16(DX), ®16(CX))) {
1.1 root 4620: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 4621: m_CF = 1;
1.1 root 4622: }
4623: break;
4624: case 0x01:
4625: // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3 root 4626: if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1 root 4627: REG16(AX) = (UINT16)GetLastError();
1.1.1.3 root 4628: m_CF = 1;
1.1 root 4629: }
4630: break;
4631: default:
4632: REG16(AX) = 0x01;
1.1.1.3 root 4633: m_CF = 1;
1.1 root 4634: break;
4635: }
4636: }
4637:
4638: inline void msdos_int_21h_71a8h()
4639: {
4640: if(REG8(DH) == 0) {
4641: char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3 root 4642: strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1 root 4643: memset(fcb, 0x20, sizeof(fcb));
4644: int len = strlen(tmp);
4645: int pos = 0;
4646: for(int i = 0; i < len; i++) {
4647: if(tmp[i] == '.') {
4648: pos = 8;
4649: } else {
4650: if(msdos_lead_byte_check(tmp[i])) {
4651: fcb[pos++] = tmp[i++];
4652: }
4653: fcb[pos++] = tmp[i];
4654: }
4655: }
1.1.1.3 root 4656: memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1 root 4657: } else {
1.1.1.3 root 4658: strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1 root 4659: }
4660: }
4661:
4662: inline void msdos_int_21h_7303h()
4663: {
1.1.1.3 root 4664: char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
4665: ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1 root 4666: DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
4667:
4668: if(GetDiskFreeSpace(path, §ors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
4669: info->size_of_structure = sizeof(ext_space_info_t);
4670: info->structure_version = 0;
4671: info->sectors_per_cluster = sectors_per_cluster;
4672: info->bytes_per_sector = bytes_per_sector;
4673: info->available_clusters_on_drive = free_clusters;
4674: info->total_clusters_on_drive = total_clusters;
4675: info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
4676: info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
4677: info->available_allocation_units = free_clusters; // ???
4678: info->total_allocation_units = total_clusters; // ???
4679: } else {
4680: REG16(AX) = errno;
1.1.1.3 root 4681: m_CF = 1;
1.1 root 4682: }
4683: }
4684:
4685: inline void msdos_int_25h()
4686: {
4687: UINT16 seg, ofs;
4688: DWORD dwSize;
4689:
1.1.1.3 root 4690: #if defined(HAS_I386)
4691: I386OP(pushf)();
4692: #else
4693: PREFIX86(_pushf());
4694: #endif
1.1 root 4695:
4696: if(!(REG8(AL) < 26)) {
4697: REG8(AL) = 0x01; // unit unknown
1.1.1.3 root 4698: m_CF = 1;
1.1 root 4699: } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
4700: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 4701: m_CF = 1;
1.1 root 4702: } else {
4703: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
4704: char dev[64];
4705: sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
4706:
4707: HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
4708: if(hFile == INVALID_HANDLE_VALUE) {
4709: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 4710: m_CF = 1;
1.1 root 4711: } else {
4712: if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
4713: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 4714: m_CF = 1;
1.1 root 4715: } else if(SetFilePointer(hFile, REG16(DX) * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
4716: REG8(AL) = 0x08; // sector not found
1.1.1.3 root 4717: m_CF = 1;
4718: } else if(ReadFile(hFile, mem + SREG_BASE(DS) + REG16(BX), REG16(CX) * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1 root 4719: REG8(AL) = 0x0b; // read error
1.1.1.3 root 4720: m_CF = 1;
1.1 root 4721: }
4722: CloseHandle(hFile);
4723: }
4724: }
4725: }
4726:
4727: inline void msdos_int_26h()
4728: {
4729: // this operation may cause serious damage for drives, so always returns error...
4730: UINT16 seg, ofs;
4731: DWORD dwSize;
4732:
1.1.1.3 root 4733: #if defined(HAS_I386)
4734: I386OP(pushf)();
4735: #else
4736: PREFIX86(_pushf());
4737: #endif
1.1 root 4738:
4739: if(!(REG8(AL) < 26)) {
4740: REG8(AL) = 0x01; // unit unknown
1.1.1.3 root 4741: m_CF = 1;
1.1 root 4742: } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
4743: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 4744: m_CF = 1;
1.1 root 4745: } else {
4746: dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
4747: char dev[64];
4748: sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
4749:
4750: if(dpb->media_type == 0xf8) {
4751: // this drive is not a floppy
1.1.1.6 ! root 4752: // if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
! 4753: // fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
! 4754: // }
1.1 root 4755: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 4756: m_CF = 1;
1.1 root 4757: } else {
4758: HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
4759: if(hFile == INVALID_HANDLE_VALUE) {
4760: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 4761: m_CF = 1;
1.1 root 4762: } else {
4763: if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
4764: REG8(AL) = 0x02; // drive not ready
1.1.1.3 root 4765: m_CF = 1;
1.1 root 4766: } else if(SetFilePointer(hFile, REG16(DX) * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
4767: REG8(AL) = 0x08; // sector not found
1.1.1.3 root 4768: m_CF = 1;
4769: } else if(WriteFile(hFile, mem + SREG_BASE(DS) + REG16(BX), REG16(CX) * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1 root 4770: REG8(AL) = 0x0a; // write error
1.1.1.3 root 4771: m_CF = 1;
1.1 root 4772: }
4773: CloseHandle(hFile);
4774: }
4775: }
4776: }
4777: }
4778:
4779: inline void msdos_int_27h()
4780: {
1.1.1.3 root 4781: int mcb_seg = SREG(CS) - 1;
1.1 root 4782: mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
4783:
4784: mcb->paragraphs = (REG16(DX) >> 4);
4785: mcb_seg += mcb->paragraphs + 1;
4786: msdos_mcb_create(mcb_seg, 'Z', 0, (MEMORY_END >> 4) - mcb_seg - 1);
4787:
1.1.1.3 root 4788: msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1 root 4789: }
4790:
4791: inline void msdos_int_29h()
4792: {
4793: msdos_putch(REG8(AL));
4794: }
4795:
4796: inline void msdos_int_2eh()
4797: {
4798: char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
4799: memset(tmp, 0, sizeof(tmp));
1.1.1.3 root 4800: strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1 root 4801: char *token = my_strtok(tmp, " ");
4802: strcpy(command, token);
4803: strcpy(opt, token + strlen(token) + 1);
4804:
4805: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
4806: param->env_seg = 0;
4807: param->cmd_line.w.l = 44;
4808: param->cmd_line.w.h = (WORK_TOP >> 4);
4809: param->fcb1.w.l = 24;
4810: param->fcb1.w.h = (WORK_TOP >> 4);
4811: param->fcb2.w.l = 24;
4812: param->fcb2.w.h = (WORK_TOP >> 4);
4813:
4814: memset(mem + WORK_TOP + 24, 0x20, 20);
4815:
4816: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
4817: cmd_line->len = strlen(opt);
4818: strcpy(cmd_line->cmd, opt);
4819: cmd_line->cmd[cmd_line->len] = 0x0d;
4820:
4821: msdos_process_exec(command, param, 0);
4822: REG8(AL) = 0;
4823: }
4824:
4825: inline void msdos_int_2fh_16h()
4826: {
4827: switch(REG8(AL)) {
4828: case 0x00:
4829: {
4830: OSVERSIONINFO osvi;
4831: ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
4832: osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
4833: GetVersionEx(&osvi);
4834: REG8(AL) = osvi.dwMajorVersion;
4835: REG8(AH) = osvi.dwMinorVersion;
4836: }
4837: break;
4838: default:
4839: REG16(AX) = 0x01;
1.1.1.3 root 4840: m_CF = 1;
1.1 root 4841: break;
4842: }
4843: }
4844:
4845: inline void msdos_int_2fh_1ah()
4846: {
4847: switch(REG8(AL)) {
4848: case 0x00:
4849: // ansi.sys is installed
4850: REG8(AL) = 0xff;
4851: break;
4852: default:
4853: REG16(AX) = 0x01;
1.1.1.3 root 4854: m_CF = 1;
1.1 root 4855: break;
4856: }
4857: }
4858:
4859: inline void msdos_int_2fh_43h()
4860: {
4861: switch(REG8(AL)) {
4862: case 0x00:
4863: // xms is not installed
4864: REG8(AL) = 0;
4865: break;
4866: default:
4867: REG16(AX) = 0x01;
1.1.1.3 root 4868: m_CF = 1;
1.1 root 4869: break;
4870: }
4871: }
4872:
4873: inline void msdos_int_2fh_4ah()
4874: {
4875: switch(REG8(AL)) {
4876: case 0x01:
4877: case 0x02:
4878: // hma is not installed
4879: REG16(BX) = 0;
1.1.1.3 root 4880: SREG(ES) = 0xffff;
4881: i386_load_segment_descriptor(ES);
1.1 root 4882: REG16(DI) = 0xffff;
4883: break;
4884: default:
4885: REG16(AX) = 0x01;
1.1.1.3 root 4886: m_CF = 1;
1.1 root 4887: break;
4888: }
4889: }
4890:
4891: inline void msdos_int_2fh_4fh()
4892: {
4893: switch(REG8(AL)) {
4894: case 0x00:
4895: REG16(AX) = 0;
4896: REG8(DL) = 1; // major version
4897: REG8(DH) = 0; // minor version
4898: break;
4899: case 0x01:
4900: REG16(AX) = 0;
4901: REG16(BX) = active_code_page;
4902: break;
4903: default:
4904: REG16(AX) = 0x01;
1.1.1.3 root 4905: m_CF = 1;
1.1 root 4906: break;
4907: }
4908: }
4909:
4910: inline void msdos_int_2fh_aeh()
4911: {
4912: switch(REG8(AL)) {
4913: case 0x00:
4914: REG8(AL) = 0;
4915: break;
4916: case 0x01:
4917: {
4918: char command[MAX_PATH];
4919: memset(command, 0, sizeof(command));
1.1.1.3 root 4920: memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1 root 4921:
4922: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
4923: param->env_seg = 0;
4924: param->cmd_line.w.l = 44;
4925: param->cmd_line.w.h = (WORK_TOP >> 4);
4926: param->fcb1.w.l = 24;
4927: param->fcb1.w.h = (WORK_TOP >> 4);
4928: param->fcb2.w.l = 24;
4929: param->fcb2.w.h = (WORK_TOP >> 4);
4930:
4931: memset(mem + WORK_TOP + 24, 0x20, 20);
4932:
4933: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3 root 4934: cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
4935: memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1 root 4936: cmd_line->cmd[cmd_line->len] = 0x0d;
4937:
4938: if(msdos_process_exec(command, param, 0)) {
4939: REG16(AX) = 0x02;
1.1.1.3 root 4940: m_CF = 1;
1.1 root 4941: }
4942: }
4943: break;
4944: default:
4945: REG16(AX) = 0x01;
1.1.1.3 root 4946: m_CF = 1;
1.1 root 4947: break;
4948: }
4949: }
4950:
4951: inline void msdos_int_2fh_b7h()
4952: {
4953: switch(REG8(AL)) {
4954: case 0x00:
4955: // append is not installed
4956: REG8(AL) = 0;
4957: break;
4958: default:
4959: REG16(AX) = 0x01;
1.1.1.3 root 4960: m_CF = 1;
1.1 root 4961: break;
4962: }
4963: }
4964:
4965: void msdos_syscall(unsigned num)
4966: {
4967: switch(num) {
4968: case 0x00:
4969: error("division by zero\n");
4970: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
4971: break;
4972: case 0x04:
4973: error("overflow\n");
4974: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
4975: break;
4976: case 0x06:
4977: // NOTE: ish.com has illegal instruction...
4978: // error("illegal instruction\n");
4979: // msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
4980: break;
4981: case 0x10:
4982: // PC BIOS - Video
4983: if(scr_width != 80 || scr_height != 25) {
4984: CONSOLE_SCREEN_BUFFER_INFO csbi;
4985: SMALL_RECT rect;
4986: COORD co;
4987:
4988: GetConsoleScreenBufferInfo(hStdout, &csbi);
4989: if(csbi.dwCursorPosition.Y > 24) {
4990: SET_RECT(rect, 0, 0, scr_width - 1, scr_height - 1);
4991: ReadConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
4992: for(int y = 0, y2 = csbi.dwCursorPosition.Y - 24; y < 25; y++, y2++) {
4993: for(int x = 0; x < 80; x++) {
4994: scr_buf[y][x] = scr_buf[y2][x];
4995: }
4996: }
4997: WriteConsoleOutput(hStdout, &scr_buf[0][0], scr_buf_size, scr_buf_pos, &rect);
4998:
4999: co.X = csbi.dwCursorPosition.X;
5000: co.Y = 24;
5001: SetConsoleCursorPosition(hStdout, co);
5002: cursor_moved = true;
5003: }
5004: SET_RECT(rect, 0, 0, 79, 24);
5005: co.X = 80;
5006: co.Y = 25;
5007: SetConsoleWindowInfo(hStdout, TRUE, &rect);
5008: SetConsoleScreenBufferSize(hStdout, co);
5009: scr_width = 80;
5010: scr_height = 25;
5011: }
1.1.1.3 root 5012: m_CF = 0;
1.1 root 5013: switch(REG8(AH)) {
5014: case 0x00: pcbios_int_10h_00h(); break;
5015: case 0x01: pcbios_int_10h_01h(); break;
5016: case 0x02: pcbios_int_10h_02h(); break;
5017: case 0x03: pcbios_int_10h_03h(); break;
5018: case 0x05: pcbios_int_10h_05h(); break;
5019: case 0x06: pcbios_int_10h_06h(); break;
5020: case 0x07: pcbios_int_10h_07h(); break;
5021: case 0x08: pcbios_int_10h_08h(); break;
5022: case 0x09: pcbios_int_10h_09h(); break;
5023: case 0x0a: pcbios_int_10h_0ah(); break;
5024: case 0x0b: break;
5025: case 0x0c: break;
5026: case 0x0d: break;
5027: case 0x0e: pcbios_int_10h_0eh(); break;
5028: case 0x0f: pcbios_int_10h_0fh(); break;
5029: case 0x10: break;
5030: case 0x11: break;
5031: case 0x12: REG8(AL) = 0x00; break;
5032: case 0x13: pcbios_int_10h_13h(); break;
5033: case 0x18: REG8(AL) = 0x86; break;
5034: case 0x1a: REG8(AL) = 0x00; break;
5035: case 0x1c: REG8(AL) = 0x00; break;
5036: case 0x1d: pcbios_int_10h_1dh(); break;
5037: case 0x82: pcbios_int_10h_82h(); break;
5038: case 0xfe: pcbios_int_10h_feh(); break;
5039: case 0xff: pcbios_int_10h_ffh(); break;
5040: default:
5041: fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5042: break;
5043: }
5044: break;
5045: case 0x11:
5046: // PC BIOS - Get Equipment List
5047: REG16(AX) = 0x20;
5048: break;
5049: case 0x12:
5050: // PC BIOS - Get Memory Size
5051: REG16(AX) = MEMORY_END / 1024;
5052: break;
5053: case 0x13:
5054: // PC BIOS - Disk
5055: // fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5056: REG8(AH) = 0xff;
1.1.1.3 root 5057: m_CF = 1;
1.1 root 5058: break;
5059: case 0x14:
5060: // PC BIOS - Serial I/O
5061: // fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5062: REG8(AH) = 0xff;
1.1.1.3 root 5063: m_CF = 1;
1.1 root 5064: break;
5065: case 0x15:
5066: // PC BIOS
1.1.1.3 root 5067: m_CF = 0;
1.1 root 5068: switch(REG8(AH)) {
5069: case 0x23: pcbios_int_15h_23h(); break;
5070: case 0x24: pcbios_int_15h_24h(); break;
5071: case 0x49: pcbios_int_15h_49h(); break;
5072: case 0x86: pcbios_int_15h_86h(); break;
5073: case 0x87: pcbios_int_15h_87h(); break;
5074: case 0x88: pcbios_int_15h_88h(); break;
5075: case 0x89: pcbios_int_15h_89h(); break;
1.1.1.3 root 5076: #if defined(HAS_I386)
1.1 root 5077: case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3 root 5078: #endif
1.1 root 5079: case 0xca: pcbios_int_15h_cah(); break;
5080: default:
5081: // fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5082: REG8(AH)=0x86;
1.1.1.3 root 5083: m_CF = 1;
1.1 root 5084: break;
5085: }
5086: break;
5087: case 0x16:
5088: // PC BIOS - Keyboard
1.1.1.3 root 5089: m_CF = 0;
1.1 root 5090: switch(REG8(AH)) {
5091: case 0x00: pcbios_int_16h_00h(); break;
5092: case 0x01: pcbios_int_16h_01h(); break;
5093: case 0x02: pcbios_int_16h_02h(); break;
5094: case 0x03: pcbios_int_16h_03h(); break;
5095: case 0x05: pcbios_int_16h_05h(); break;
5096: case 0x10: pcbios_int_16h_00h(); break;
5097: case 0x11: pcbios_int_16h_01h(); break;
5098: case 0x12: pcbios_int_16h_12h(); break;
5099: case 0x13: pcbios_int_16h_13h(); break;
5100: case 0x14: pcbios_int_16h_14h(); break;
5101: default:
5102: fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5103: break;
5104: }
5105: break;
5106: case 0x17:
5107: // PC BIOS - Printer
5108: fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5109: break;
5110: case 0x1a:
5111: // PC BIOS - Timer
1.1.1.3 root 5112: m_CF = 0;
1.1 root 5113: switch(REG8(AH)) {
5114: case 0x00: pcbios_int_1ah_00h(); break;
5115: case 0x01: break;
5116: case 0x02: pcbios_int_1ah_02h(); break;
5117: case 0x03: break;
5118: case 0x04: pcbios_int_1ah_04h(); break;
5119: case 0x05: break;
5120: case 0x0a: pcbios_int_1ah_0ah(); break;
5121: case 0x0b: break;
5122: default:
5123: fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5124: break;
5125: }
5126: break;
5127: case 0x20:
1.1.1.3 root 5128: msdos_process_terminate(SREG(CS), retval, 1);
1.1 root 5129: break;
5130: case 0x21:
5131: // MS-DOS System Call
1.1.1.3 root 5132: m_CF = 0;
1.1 root 5133: switch(REG8(AH)) {
5134: case 0x00: msdos_int_21h_00h(); break;
5135: case 0x01: msdos_int_21h_01h(); break;
5136: case 0x02: msdos_int_21h_02h(); break;
5137: case 0x03: msdos_int_21h_03h(); break;
5138: case 0x04: msdos_int_21h_04h(); break;
5139: case 0x05: msdos_int_21h_05h(); break;
5140: case 0x06: msdos_int_21h_06h(); break;
5141: case 0x07: msdos_int_21h_07h(); break;
5142: case 0x08: msdos_int_21h_08h(); break;
5143: case 0x09: msdos_int_21h_09h(); break;
5144: case 0x0a: msdos_int_21h_0ah(); break;
5145: case 0x0b: msdos_int_21h_0bh(); break;
5146: case 0x0c: msdos_int_21h_0ch(); break;
5147: case 0x0d: msdos_int_21h_0dh(); break;
5148: case 0x0e: msdos_int_21h_0eh(); break;
5149: // 0x0f: open file with fcb
5150: // 0x10: close file with fcb
5151: case 0x11: msdos_int_21h_11h(); break;
5152: case 0x12: msdos_int_21h_12h(); break;
5153: case 0x13: msdos_int_21h_13h(); break;
5154: // 0x14: sequential read with fcb
5155: // 0x15: sequential write with fcb
5156: // 0x16: create new file with fcb
5157: // 0x17: rename file with fcb
5158: case 0x18: msdos_int_21h_18h(); break;
5159: case 0x19: msdos_int_21h_19h(); break;
5160: case 0x1a: msdos_int_21h_1ah(); break;
5161: case 0x1b: msdos_int_21h_1bh(); break;
5162: case 0x1c: msdos_int_21h_1ch(); break;
5163: case 0x1d: msdos_int_21h_1dh(); break;
5164: case 0x1e: msdos_int_21h_1eh(); break;
5165: case 0x1f: msdos_int_21h_1fh(); break;
5166: case 0x20: msdos_int_21h_20h(); break;
5167: // 0x21: random read with fcb
5168: // 0x22: randome write with fcb
5169: // 0x23: get file size with fcb
5170: // 0x24: set relative record field with fcb
5171: case 0x25: msdos_int_21h_25h(); break;
5172: case 0x26: msdos_int_21h_26h(); break;
5173: // 0x27: random block read with fcb
5174: // 0x28: random block write with fcb
5175: case 0x29: msdos_int_21h_29h(); break;
5176: case 0x2a: msdos_int_21h_2ah(); break;
5177: case 0x2b: msdos_int_21h_2bh(); break;
5178: case 0x2c: msdos_int_21h_2ch(); break;
5179: case 0x2d: msdos_int_21h_2dh(); break;
5180: case 0x2e: msdos_int_21h_2eh(); break;
5181: case 0x2f: msdos_int_21h_2fh(); break;
5182: case 0x30: msdos_int_21h_30h(); break;
5183: case 0x31: msdos_int_21h_31h(); break;
5184: case 0x32: msdos_int_21h_32h(); break;
5185: case 0x33: msdos_int_21h_33h(); break;
5186: // 0x34: get address of indos flag
5187: case 0x35: msdos_int_21h_35h(); break;
5188: case 0x36: msdos_int_21h_36h(); break;
5189: case 0x37: msdos_int_21h_37h(); break;
5190: // 0x38: get country-specific information
5191: case 0x39: msdos_int_21h_39h(0); break;
5192: case 0x3a: msdos_int_21h_3ah(0); break;
5193: case 0x3b: msdos_int_21h_3bh(0); break;
5194: case 0x3c: msdos_int_21h_3ch(); break;
5195: case 0x3d: msdos_int_21h_3dh(); break;
5196: case 0x3e: msdos_int_21h_3eh(); break;
5197: case 0x3f: msdos_int_21h_3fh(); break;
5198: case 0x40: msdos_int_21h_40h(); break;
5199: case 0x41: msdos_int_21h_41h(0); break;
5200: case 0x42: msdos_int_21h_42h(); break;
5201: case 0x43: msdos_int_21h_43h(0); break;
5202: case 0x44: msdos_int_21h_44h(); break;
5203: case 0x45: msdos_int_21h_45h(); break;
5204: case 0x46: msdos_int_21h_46h(); break;
5205: case 0x47: msdos_int_21h_47h(0); break;
5206: case 0x48: msdos_int_21h_48h(); break;
5207: case 0x49: msdos_int_21h_49h(); break;
5208: case 0x4a: msdos_int_21h_4ah(); break;
5209: case 0x4b: msdos_int_21h_4bh(); break;
5210: case 0x4c: msdos_int_21h_4ch(); break;
5211: case 0x4d: msdos_int_21h_4dh(); break;
5212: case 0x4e: msdos_int_21h_4eh(); break;
5213: case 0x4f: msdos_int_21h_4fh(); break;
5214: case 0x50: msdos_int_21h_50h(); break;
5215: case 0x51: msdos_int_21h_51h(); break;
5216: case 0x52: msdos_int_21h_52h(); break;
5217: // 0x53: translate bios parameter block to drive param bock
5218: case 0x54: msdos_int_21h_54h(); break;
5219: case 0x55: msdos_int_21h_55h(); break;
5220: case 0x56: msdos_int_21h_56h(0); break;
5221: case 0x57: msdos_int_21h_57h(); break;
5222: case 0x58: msdos_int_21h_58h(); break;
5223: case 0x59: msdos_int_21h_59h(); break;
5224: case 0x5a: msdos_int_21h_5ah(); break;
5225: case 0x5b: msdos_int_21h_5bh(); break;
5226: case 0x5c: msdos_int_21h_5ch(); break;
5227: // 0x5e: ms-network
5228: // 0x5f: ms-network
5229: case 0x60: msdos_int_21h_60h(0); break;
5230: case 0x61: msdos_int_21h_61h(); break;
5231: case 0x62: msdos_int_21h_62h(); break;
5232: case 0x63: msdos_int_21h_63h(); break;
5233: // 0x64: set device driver lockahead flag
5234: case 0x65: msdos_int_21h_65h(); break;
5235: case 0x66: msdos_int_21h_66h(); break;
5236: case 0x67: msdos_int_21h_67h(); break;
5237: case 0x68: msdos_int_21h_68h(); break;
5238: case 0x69: msdos_int_21h_69h(); break;
5239: case 0x6a: msdos_int_21h_6ah(); break;
5240: case 0x6b: msdos_int_21h_6bh(); break;
5241: case 0x6c: msdos_int_21h_6ch(0); break;
5242: // 0x6d: find first rom program
5243: // 0x6e: find next rom program
5244: // 0x6f: get/set rom scan start address
5245: // 0x70: windows95 get/set internationalization information
5246: case 0x71:
5247: // windows95 long filename functions
5248: switch(REG8(AL)) {
5249: case 0x0d: msdos_int_21h_710dh(); break;
5250: case 0x39: msdos_int_21h_39h(1); break;
5251: case 0x3a: msdos_int_21h_3ah(1); break;
5252: case 0x3b: msdos_int_21h_3bh(1); break;
5253: case 0x41: msdos_int_21h_41h(1); break;
5254: case 0x43: msdos_int_21h_43h(1); break;
5255: case 0x47: msdos_int_21h_47h(1); break;
5256: case 0x4e: msdos_int_21h_714eh(); break;
5257: case 0x4f: msdos_int_21h_714fh(); break;
5258: case 0x56: msdos_int_21h_56h(1); break;
5259: case 0x60: msdos_int_21h_60h(1); break;
5260: case 0x6c: msdos_int_21h_6ch(1); break;
5261: case 0xa0: msdos_int_21h_71a0h(); break;
5262: case 0xa1: msdos_int_21h_71a1h(); break;
5263: case 0xa6: msdos_int_21h_71a6h(); break;
5264: case 0xa7: msdos_int_21h_71a7h(); break;
5265: case 0xa8: msdos_int_21h_71a8h(); break;
5266: // 0xa9: server create/open file
5267: // 0xaa: create/terminate SUBST
5268: default:
5269: // fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5270: REG16(AX) = 0x7100;
1.1.1.3 root 5271: m_CF = 1;
1.1 root 5272: break;
5273: }
5274: break;
5275: // 0x72: Windows95 beta - LFN FindClose
5276: case 0x73:
5277: // windows95 fat32 functions
5278: switch(REG8(AL)) {
5279: // 0x00: drive locking ???
5280: // 0x01: drive locking ???
5281: // 0x02: get extended dpb
5282: case 0x03: msdos_int_21h_7303h(); break;
5283: // 0x04: set dpb to use for formatting
5284: default:
5285: // fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5286: REG16(AX) = 0x7300;
1.1.1.3 root 5287: m_CF = 1;
1.1 root 5288: break;
5289: }
5290: break;
5291: default:
5292: // fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5293: REG16(AX) = 0x01;
1.1.1.3 root 5294: m_CF = 1;
1.1 root 5295: break;
5296: }
1.1.1.3 root 5297: if(m_CF) {
1.1 root 5298: error_code = REG16(AX);
5299: }
5300: break;
5301: case 0x22:
5302: fatalerror("int 22h (terminate address)\n");
5303: case 0x23:
5304: msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
5305: break;
5306: case 0x24:
5307: msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
5308: break;
5309: case 0x25:
5310: msdos_int_25h();
5311: break;
5312: case 0x26:
5313: msdos_int_26h();
5314: break;
5315: case 0x27:
5316: msdos_int_27h();
5317: break;
5318: case 0x28:
5319: Sleep(10);
5320: break;
5321: case 0x29:
5322: msdos_int_29h();
5323: break;
5324: case 0x2e:
5325: msdos_int_2eh();
5326: break;
5327: case 0x2f:
5328: // multiplex interrupt
1.1.1.3 root 5329: m_CF = 0;
1.1 root 5330: switch(REG8(AH)) {
5331: case 0x16: msdos_int_2fh_16h(); break;
5332: case 0x1a: msdos_int_2fh_1ah(); break;
5333: case 0x43: msdos_int_2fh_43h(); break;
5334: case 0x4a: msdos_int_2fh_4ah(); break;
5335: case 0x4f: msdos_int_2fh_4fh(); break;
5336: case 0xae: msdos_int_2fh_aeh(); break;
5337: case 0xb7: msdos_int_2fh_b7h(); break;
5338: default:
5339: // fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5340: REG16(AX) = 0x01; // ???
1.1.1.3 root 5341: m_CF = 1;
1.1 root 5342: break;
5343: }
1.1.1.3 root 5344: if(m_CF) {
1.1 root 5345: error_code = REG16(AX);
5346: }
5347: break;
5348: default:
5349: // fatalerror("int %02xh (ax=%04xh bx=%04xh cx=%04xh dx=%04x)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX));
5350: break;
5351: }
5352:
5353: // update cursor position
5354: if(cursor_moved) {
5355: CONSOLE_SCREEN_BUFFER_INFO csbi;
5356: GetConsoleScreenBufferInfo(hStdout, &csbi);
5357: mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
5358: mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y;
5359: cursor_moved = false;
5360: }
5361: }
5362:
5363: // init
5364:
5365: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
5366: {
5367: // init file handler
5368: memset(file_handler, 0, sizeof(file_handler));
5369: msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
5370: msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
5371: msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
5372: #ifdef SUPPORT_AUX_PRN
5373: if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
5374: msdos_file_handler_open(3, 0, 2, 0x80c0, 0);
5375: }
5376: if(_open("stdprn.txt", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
5377: msdos_file_handler_open(4, 0, 1, 0xa8c0, 0);
5378: }
5379: #endif
5380: _dup2(0, DUP_STDIN);
5381: _dup2(1, DUP_STDOUT);
5382: _dup2(2, DUP_STDERR);
5383:
5384: // init process
5385: memset(process, 0, sizeof(process));
5386:
5387: // init memory
5388: memset(mem, 0, sizeof(mem));
5389: for(int i = 0; i < 0x100; i++) {
5390: *(UINT16 *)(mem + 4 * i + 0) = i;
5391: *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
5392: }
5393: *(UINT16 *)(mem + 4 * 0x22 + 0) = 0xfff0;
5394: *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xf000;
5395: memset(mem + IRET_TOP, 0xcf, IRET_SIZE);
5396:
5397: // bios data area
5398: CONSOLE_SCREEN_BUFFER_INFO csbi;
5399: GetConsoleScreenBufferInfo(hStdout, &csbi);
5400:
5401: *(UINT8 *)(mem + 0x411) = 0x20;
5402: *(UINT16 *)(mem + 0x410) = 0x20;
5403: *(UINT16 *)(mem + 0x413) = MEMORY_END / 1024;
5404: *(UINT8 *)(mem + 0x449) = 0x03;//0x73;
5405: *(UINT16 *)(mem + 0x44a) = 80;
5406: *(UINT16 *)(mem + 0x44c) = 4096;
5407: *(UINT16 *)(mem + 0x44e) = 0;
5408: *(UINT8 *)(mem + 0x450) = csbi.dwCursorPosition.X;
5409: *(UINT8 *)(mem + 0x451) = csbi.dwCursorPosition.Y;
5410: *(UINT8 *)(mem + 0x460) = 7;
5411: *(UINT8 *)(mem + 0x461) = 7;
5412: *(UINT8 *)(mem + 0x462) = 0;
5413: *(UINT16 *)(mem + 0x463) = 0x3d4;
5414: *(UINT8 *)(mem + 0x484) = 24;
5415: *(UINT8 *)(mem + 0x485) = 19;
5416: *(UINT8 *)(mem + 0x487) = 0; // is this okay?
5417:
5418: // dos info
5419: dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
5420: dos_info->first_mcb = MEMORY_TOP >> 4;
5421: dos_info->first_dpb.w.l = 0;
5422: dos_info->first_dpb.w.h = DPB_TOP >> 4;
5423: dos_info->first_sft.w.l = 0;
5424: dos_info->first_sft.w.h = FILE_TABLE_TOP >> 4;
5425: dos_info->cds.w.l = 0;
5426: dos_info->cds.w.h = CDS_TOP >> 4;
5427: dos_info->fcb_table.w.l = 0;
5428: dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
5429: dos_info->last_drive = 'Z' - 'A' + 1;
5430: dos_info->buffers_x = 20;
5431: dos_info->buffers_y = 0;
5432: char *env;
5433: if((env = getenv("LASTDRIVE")) != NULL) {
5434: if(env[0] >= 'A' && env[0] <= 'Z') {
5435: dos_info->last_drive = env[0] - 'A' + 1;
5436: } else if(env[0] >= 'a' && env[0] <= 'z') {
5437: dos_info->last_drive = env[0] - 'a' + 1;
5438: }
5439: }
5440: if((env = getenv("windir")) != NULL) {
5441: if(env[0] >= 'A' && env[0] <= 'Z') {
5442: dos_info->boot_drive = env[0] - 'A' + 1;
5443: } else if(env[0] >= 'a' && env[0] <= 'z') {
5444: dos_info->boot_drive = env[0] - 'a' + 1;
5445: }
5446: }
1.1.1.3 root 5447: #if defined(HAS_I386)
1.1 root 5448: dos_info->i386_or_later = 1;
1.1.1.3 root 5449: #else
5450: dos_info->i386_or_later = 0;
5451: #endif
1.1 root 5452: dos_info->ext_mem_size = (MAX_MEM - 0x100000) >> 10;
5453:
5454: // environment
5455: int seg = MEMORY_TOP >> 4;
5456: msdos_mcb_create(seg++, 'M', -1, ENV_SIZE >> 4);
5457: int env_seg = seg;
5458: int ofs = 0;
5459:
5460: for(char **p = envp; p != NULL && *p != NULL; p++) {
5461: // lower to upper
5462: char tmp[ENV_SIZE], name[ENV_SIZE], value[ENV_SIZE];
5463: int value_pos = 0;
5464: strcpy(tmp, *p);
5465: for(int i = 0;; i++) {
5466: if(tmp[i] == '=') {
5467: tmp[i] = '\0';
5468: sprintf(name, ";%s;", tmp);
5469: strcpy(value, tmp + (value_pos = i + 1));
5470: tmp[i] = '=';
5471: break;
5472: } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
5473: tmp[i] = tmp[i] - 'a' + 'A';
5474: }
5475: }
5476: if(!(standard_env && strstr(";COMSPEC;INCLUDE;LIB;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL)) {
5477: if(strncmp(tmp, "COMSPEC=", 8) == 0) {
5478: char full_path[MAX_PATH], short_path[MAX_PATH], *file_name;
5479: GetFullPathName(argv[0], MAX_PATH, full_path, &file_name);
5480: if(_stricmp(file_name, "COMMAND.COM") == 0 || _stricmp(file_name, "COMMAND") == 0) {
5481: sprintf(file_name, "COMMAND.COM");
5482: if(_access(full_path, 0) == 0) {
5483: GetShortPathName(full_path, short_path, MAX_PATH);
5484: my_strupr(short_path);
5485: sprintf(tmp, "COMSPEC=%s", short_path);
5486: }
5487: }
5488: } else if(strncmp(tmp, "PATH=", 5) == 0 || strncmp(tmp, "TEMP=", 5) == 0 || strncmp(tmp, "TMP=", 4) == 0) {
5489: tmp[value_pos] = '\0';
5490: char *token = my_strtok(value, ";");
5491: while(token != NULL) {
5492: if(strlen(token) != 0) {
5493: char path[MAX_PATH], short_path[MAX_PATH];
5494: if(token[0] == '"' && token[strlen(token) - 1] == '"') {
5495: memset(path, 0, sizeof(path));
5496: memcpy(path, token + 1, strlen(token) - 2);
5497: } else {
5498: sprintf(path, token);
5499: }
5500: GetShortPathName(path, short_path, MAX_PATH);
5501: strcat(tmp, short_path);
5502: strcat(tmp, ";");
5503: }
5504: token = my_strtok(NULL, ";");
5505: }
5506: tmp[strlen(tmp) - 1] = '\0';
5507: my_strupr(tmp);
5508: }
5509: int len = strlen(tmp);
5510: if (ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
5511: fatalerror("too many environments\n");
5512: }
5513: memcpy(mem + (seg << 4) + ofs, tmp, len);
5514: ofs += len + 1;
5515: }
5516: }
5517: seg += (ENV_SIZE >> 4);
5518:
5519: // psp
5520: msdos_mcb_create(seg++, 'M', -1, PSP_SIZE >> 4);
5521: current_psp = seg;
5522: psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
5523: seg += (PSP_SIZE >> 4);
5524:
5525: // first mcb
5526: msdos_mcb_create(seg, 'Z', 0, (MEMORY_END >> 4) - seg - 1);
5527:
5528: // boot
5529: mem[0xffff0] = 0xf4; // halt
5530: mem[0xffff1] = 0xcd; // int 21h
5531: mem[0xffff2] = 0x21;
5532: mem[0xffff3] = 0xcb; // retf
5533:
5534: // param block
5535: // + 0: param block (22bytes)
5536: // +24: fcb1/2 (20bytes)
5537: // +44: command tail (128bytes)
5538: param_block_t *param = (param_block_t *)(mem + WORK_TOP);
5539: param->env_seg = 0;
5540: param->cmd_line.w.l = 44;
5541: param->cmd_line.w.h = (WORK_TOP >> 4);
5542: param->fcb1.w.l = 24;
5543: param->fcb1.w.h = (WORK_TOP >> 4);
5544: param->fcb2.w.l = 24;
5545: param->fcb2.w.h = (WORK_TOP >> 4);
5546:
5547: memset(mem + WORK_TOP + 24, 0x20, 20);
5548:
5549: cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
5550: if(argc > 1) {
5551: sprintf(cmd_line->cmd, " %s", argv[1]);
5552: for(int i = 2; i < argc; i++) {
5553: char tmp[128];
5554: sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
5555: strcpy(cmd_line->cmd, tmp);
5556: }
5557: cmd_line->len = (UINT8)strlen(cmd_line->cmd);
5558: } else {
5559: cmd_line->len = 0;
5560: }
5561: cmd_line->cmd[cmd_line->len] = 0x0d;
5562:
5563: // system file table
5564: *(UINT16 *)(mem + FILE_TABLE_TOP + 0) = 6;
5565: *(UINT16 *)(mem + FILE_TABLE_TOP + 2) = FILE_TABLE_TOP >> 4;
5566: *(UINT16 *)(mem + FILE_TABLE_TOP + 4) = 100;
5567: *(UINT32 *)(mem + FILE_TABLE_TOP + 6) = 0xffffffff;
5568: *(UINT16 *)(mem + FILE_TABLE_TOP + 10) = 100;
5569:
5570: // current directory structure
5571: msdos_cds_update(_getdrive() - 1);
5572:
5573: // fcb table
5574: *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
5575: *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 100;
5576:
5577: // dbcs table
5578: msdos_dbcs_table_init();
5579:
5580: // execute command
5581: if(msdos_process_exec(argv[0], param, 0)) {
5582: fatalerror("'%s' not found\n", argv[0]);
5583: }
5584: retval = 0;
5585: return(0);
5586: }
5587:
5588: #define remove_std_file(path) { \
5589: int fd = _open(path, _O_RDONLY | _O_BINARY); \
5590: if(fd != -1) { \
5591: _lseek(fd, 0, SEEK_END); \
5592: int size = _tell(fd); \
5593: _close(fd); \
5594: if(size == 0) { \
5595: remove(path); \
5596: } \
5597: } \
5598: }
5599:
5600: void msdos_finish()
5601: {
5602: for(int i = 0; i < MAX_FILES; i++) {
5603: if(file_handler[i].valid) {
5604: _close(i);
5605: }
5606: }
5607: #ifdef SUPPORT_AUX_PRN
5608: remove_std_file("stdaux.txt");
5609: remove_std_file("stdprn.txt");
5610: #endif
5611: msdos_dbcs_table_finish();
5612: }
5613:
5614: /* ----------------------------------------------------------------------------
5615: PC/AT hardware emulation
5616: ---------------------------------------------------------------------------- */
5617:
5618: void hardware_init()
5619: {
1.1.1.3 root 5620: CPU_INIT_CALL(CPU_MODEL);
1.1 root 5621: CPU_RESET_CALL(CPU_MODEL);
1.1.1.3 root 5622: #if defined(HAS_I386)
1.1 root 5623: cpu_type = (REG32(EDX) >> 8) & 0x0f;
5624: cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3 root 5625: #endif
5626: i386_set_a20_line(0);
1.1 root 5627: #ifdef SUPPORT_HARDWARE
5628: pic_init();
5629: //pit_init();
5630: pit_active = 0;
5631: #endif
5632: }
5633:
5634: void hardware_run()
5635: {
5636: int ops = 0;
5637:
1.1.1.3 root 5638: while(!m_halted) {
1.1 root 5639: #ifdef SUPPORT_DISASSEMBLER
5640: if(dasm) {
5641: char buffer[256];
1.1.1.3 root 5642: #if defined(HAS_I386)
5643: UINT64 eip = m_eip;
5644: #else
5645: UINT64 eip = m_pc - SREG_BASE(CS);
5646: #endif
5647: UINT8 *oprom = mem + SREG_BASE(CS) + eip;
1.1 root 5648:
1.1.1.3 root 5649: #if defined(HAS_I386)
5650: if(m_operand_size) {
1.1 root 5651: CPU_DISASSEMBLE_CALL(x86_32);
1.1.1.3 root 5652: } else
5653: #endif
5654: CPU_DISASSEMBLE_CALL(x86_16);
5655: fprintf(stderr, "%04x:%04x\t%s\n", SREG(CS), eip, buffer);
1.1 root 5656: }
5657: #endif
1.1.1.3 root 5658: #if defined(HAS_I386)
5659: m_cycles = 1;
1.1 root 5660: CPU_EXECUTE_CALL(i386);
1.1.1.3 root 5661: #else
5662: CPU_EXECUTE_CALL(CPU_MODEL);
5663: #endif
1.1 root 5664: #ifdef SUPPORT_HARDWARE
5665: if(++ops == 1024) {
5666: hardware_update();
5667: ops = 0;
5668: }
5669: #endif
5670: }
5671: }
5672:
5673: #ifdef SUPPORT_HARDWARE
5674: void hardware_update()
5675: {
5676: if(pit_active) {
5677: pit_run();
5678: }
5679: }
5680: #endif
5681:
5682: // pic
5683:
5684: void pic_init()
5685: {
5686: for(int c = 0; c < 2; c++) {
5687: pic[c].imr = 0xff;
5688: pic[c].irr = pic[c].isr = pic[c].prio = 0;
5689: pic[c].icw1 = pic[c].icw2 = pic[c].icw3 = pic[c].icw4 = 0;
5690: pic[c].ocw3 = 0;
5691: pic[c].icw2_r = pic[c].icw3_r = pic[c].icw4_r = 0;
5692: }
5693:
5694: // from bochs bios
5695: pic_write(0, 0, 0x11); // icw1 = 11h
5696: pic_write(0, 1, 0x08); // icw2 = 08h
5697: pic_write(0, 1, 0x04); // icw3 = 04h
5698: pic_write(0, 1, 0x01); // icw4 = 01h
5699: pic_write(0, 1, 0xb8); // ocw1 = b8h
5700: pic_write(1, 0, 0x11); // icw1 = 11h
5701: pic_write(1, 1, 0x70); // icw2 = 70h
5702: pic_write(1, 1, 0x02); // icw3 = 02h
5703: pic_write(1, 1, 0x01); // icw4 = 01h
5704: }
5705:
5706: void pic_write(int c, UINT32 addr, UINT8 data)
5707: {
5708: if(addr & 1) {
5709: if(pic[c].icw2_r) {
5710: // icw2
5711: pic[c].icw2 = data;
5712: pic[c].icw2_r = 0;
5713: } else if(pic[c].icw3_r) {
5714: // icw3
5715: pic[c].icw3 = data;
5716: pic[c].icw3_r = 0;
5717: } else if(pic[c].icw4_r) {
5718: // icw4
5719: pic[c].icw4 = data;
5720: pic[c].icw4_r = 0;
5721: } else {
5722: // ocw1
5723: pic[c].imr = data;
5724: }
5725: } else {
5726: if(data & 0x10) {
5727: // icw1
5728: pic[c].icw1 = data;
5729: pic[c].icw2_r = 1;
5730: pic[c].icw3_r = (data & 2) ? 0 : 1;
5731: pic[c].icw4_r = data & 1;
5732:
5733: pic[c].irr = 0;
5734: pic[c].isr = 0;
5735: pic[c].imr = 0;
5736: pic[c].prio = 0;
5737: if(!(pic[c].icw1 & 1)) {
5738: pic[c].icw4 = 0;
5739: }
5740: pic[c].ocw3 = 0;
5741: } else if(data & 8) {
5742: // ocw3
5743: if(!(data & 2)) {
5744: data = (data & ~1) | (pic[c].ocw3 & 1);
5745: }
5746: if(!(data & 0x40)) {
5747: data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
5748: }
5749: pic[c].ocw3 = data;
5750: } else {
5751: // ocw2
5752: int level = 0;
5753: if(data & 0x40) {
5754: level = data & 7;
5755: } else {
5756: if(!pic[c].isr) {
5757: return;
5758: }
5759: level = pic[c].prio;
5760: while(!(pic[c].isr & (1 << level))) {
5761: level = (level + 1) & 7;
5762: }
5763: }
5764: if(data & 0x80) {
5765: pic[c].prio = (level + 1) & 7;
5766: }
5767: if(data & 0x20) {
5768: pic[c].isr &= ~(1 << level);
5769: }
5770: }
5771: }
5772: pic_update();
5773: }
5774:
5775: UINT8 pic_read(int c, UINT32 addr)
5776: {
5777: if(addr & 1) {
5778: return(pic[c].imr);
5779: } else {
5780: // polling mode is not supported...
5781: //if(pic[c].ocw3 & 4) {
5782: // return ???;
5783: //}
5784: if(pic[c].ocw3 & 1) {
5785: return(pic[c].isr);
5786: } else {
5787: return(pic[c].irr);
5788: }
5789: }
5790: }
5791:
5792: void pic_req(int c, int level, int signal)
5793: {
5794: if(signal) {
5795: pic[c].irr |= (1 << level);
5796: } else {
5797: pic[c].irr &= ~(1 << level);
5798: }
5799: pic_update();
5800: }
5801:
5802: int pic_ack()
5803: {
5804: // ack (INTA=L)
5805: pic[pic_req_chip].isr |= pic_req_bit;
5806: pic[pic_req_chip].irr &= ~pic_req_bit;
5807: if(pic_req_chip > 0) {
5808: // update isr and irr of master
5809: UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
5810: pic[pic_req_chip - 1].isr |= slave;
5811: pic[pic_req_chip - 1].irr &= ~slave;
5812: }
5813: //if(pic[pic_req_chip].icw4 & 1) {
5814: // 8086 mode
5815: int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
5816: //} else {
5817: // // 8080 mode
5818: // UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
5819: // if(pic[pic_req_chip].icw1 & 4) {
5820: // addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
5821: // } else {
5822: // addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
5823: // }
5824: // vector = 0xcd | (addr << 8);
5825: //}
5826: if(pic[pic_req_chip].icw4 & 2) {
5827: // auto eoi
5828: pic[pic_req_chip].isr &= ~pic_req_bit;
5829: }
5830: return(vector);
5831: }
5832:
5833: void pic_update()
5834: {
5835: for(int c = 0; c < 2; c++) {
5836: UINT8 irr = pic[c].irr;
5837: if(c + 1 < 2) {
5838: // this is master
5839: if(pic[c + 1].irr & (~pic[c + 1].imr)) {
5840: // request from slave
5841: irr |= 1 << (pic[c + 1].icw3 & 7);
5842: }
5843: }
5844: irr &= (~pic[c].imr);
5845: if(!irr) {
5846: break;
5847: }
5848: if(!(pic[c].ocw3 & 0x20)) {
5849: irr |= pic[c].isr;
5850: }
5851: int level = pic[c].prio;
5852: UINT8 bit = 1 << level;
5853: while(!(irr & bit)) {
5854: level = (level + 1) & 7;
5855: bit = 1 << level;
5856: }
5857: if((c + 1 < 2) && (pic[c].icw3 & bit)) {
5858: // check slave
5859: continue;
5860: }
5861: if(pic[c].isr & bit) {
5862: break;
5863: }
5864: // interrupt request
5865: pic_req_chip = c;
5866: pic_req_level = level;
5867: pic_req_bit = bit;
1.1.1.3 root 5868: i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1 root 5869: return;
5870: }
1.1.1.3 root 5871: i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2 root 5872: }
1.1 root 5873:
5874: // pit
5875:
5876: #define PIT_FREQ 1193182
5877: #define PIT_COUNT_VALUE(n) ((pit[n].count_reg == 0) ? 0x10000 : (pit[n].mode == 3 && pit[n].count_reg == 1) ? 0x10001 : pit[n].count_reg)
5878:
5879: void pit_init()
5880: {
5881: for(int ch = 0; ch < 3; ch++) {
5882: pit[ch].prev_out = 1;
5883: //pit[ch].gate = 1;
5884: pit[ch].count = 0x10000;
5885: pit[ch].count_reg = 0;
5886: pit[ch].ctrl_reg = 0x34;
5887: pit[ch].mode = 3;
5888: pit[ch].count_latched = 0;
5889: pit[ch].low_read = pit[ch].high_read = 0;
5890: pit[ch].low_write = pit[ch].high_write = 0;
5891: pit[ch].delay = 0;
5892: pit[ch].start = 0;
5893: pit[ch].null_count = 1;
5894: pit[ch].status_latched = 0;
5895: }
5896:
5897: // from bochs bios
5898: pit_write(3, 0x34);
5899: pit_write(0, 0x00);
5900: pit_write(0, 0x00);
5901: }
5902:
5903: void pit_write(int ch, UINT8 val)
5904: {
5905: if(!pit_active) {
5906: pit_active = 1;
5907: pit_init();
5908: }
5909:
5910: switch(ch) {
5911: case 0:
5912: case 1:
5913: case 2:
5914: // write count register
5915: if(!pit[ch].low_write && !pit[ch].high_write) {
5916: if(pit[ch].ctrl_reg & 0x10) {
5917: pit[ch].low_write = 1;
5918: }
5919: if(pit[ch].ctrl_reg & 0x20) {
5920: pit[ch].high_write = 1;
5921: }
5922: }
5923: if(pit[ch].low_write) {
5924: pit[ch].count_reg = val;
5925: pit[ch].low_write = 0;
5926: } else if(pit[ch].high_write) {
5927: if((pit[ch].ctrl_reg & 0x30) == 0x20) {
5928: pit[ch].count_reg = val << 8;
5929: } else {
5930: pit[ch].count_reg |= val << 8;
5931: }
5932: pit[ch].high_write = 0;
5933: }
5934: pit[ch].null_count = 1;
5935: // set signal
5936: if(pit[ch].mode == 0) {
5937: pit_set_signal(ch, 0);
5938: } else {
5939: pit_set_signal(ch, 1);
5940: }
5941: // start count
5942: if(pit[ch].mode == 0 || pit[ch].mode == 4) {
5943: // restart with new count
5944: pit_stop_count(ch);
5945: pit[ch].delay = 1;
5946: pit_start_count(ch);
5947: } else if(pit[ch].mode == 2 || pit[ch].mode == 3) {
5948: // start with new pit after the current count is finished
5949: if(!pit[ch].start) {
5950: pit[ch].delay = 1;
5951: pit_start_count(ch);
5952: }
5953: }
5954: break;
5955: case 3: // ctrl reg
5956: if((val & 0xc0) == 0xc0) {
5957: // i8254 read-back command
5958: for(ch = 0; ch < 3; ch++) {
5959: UINT8 bit = 2 << ch;
5960: if(!(val & 0x10) && !pit[ch].status_latched) {
5961: pit[ch].status = pit[ch].ctrl_reg & 0x3f;
5962: if(pit[ch].prev_out) {
5963: pit[ch].status |= 0x80;
5964: }
5965: if(pit[ch].null_count) {
5966: pit[ch].status |= 0x40;
5967: }
5968: pit[ch].status_latched = 1;
5969: }
5970: if(!(val & 0x20) && !pit[ch].count_latched) {
5971: pit_latch_count(ch);
5972: }
5973: }
5974: break;
5975: }
5976: ch = (val >> 6) & 3;
5977: if(val & 0x30) {
5978: static int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
5979: pit[ch].mode = modes[(val >> 1) & 7];
5980: pit[ch].count_latched = 0;
5981: pit[ch].low_read = pit[ch].high_read = 0;
5982: pit[ch].low_write = pit[ch].high_write = 0;
5983: pit[ch].ctrl_reg = val;
5984: // set signal
5985: if(pit[ch].mode == 0) {
5986: pit_set_signal(ch, 0);
5987: } else {
5988: pit_set_signal(ch, 1);
5989: }
5990: // stop count
5991: pit_stop_count(ch);
5992: pit[ch].count_reg = 0;
5993: pit[ch].null_count = 1;
5994: } else if(!pit[ch].count_latched) {
5995: pit_latch_count(ch);
5996: }
5997: break;
5998: }
5999: }
6000:
6001: UINT8 pit_read(int ch)
6002: {
6003: if(!pit_active) {
6004: pit_active = 1;
6005: pit_init();
6006: }
6007:
6008: switch(ch) {
6009: case 0:
6010: case 1:
6011: case 2:
6012: if(pit[ch].status_latched) {
6013: pit[ch].status_latched = 0;
6014: return(pit[ch].status);
6015: }
6016: // if not latched, through current count
6017: if(!pit[ch].count_latched) {
6018: if(!pit[ch].low_read && !pit[ch].high_read) {
6019: pit_latch_count(ch);
6020: }
6021: }
6022: // return latched count
6023: if(pit[ch].low_read) {
6024: pit[ch].low_read = 0;
6025: if(!pit[ch].high_read) {
6026: pit[ch].count_latched = 0;
6027: }
6028: return(pit[ch].latch & 0xff);
6029: } else if(pit[ch].high_read) {
6030: pit[ch].high_read = 0;
6031: pit[ch].count_latched = 0;
6032: return((pit[ch].latch >> 8) & 0xff);
6033: }
6034: }
6035: return(0xff);
6036: }
6037:
6038: void pit_run()
6039: {
6040: static UINT32 prev_time = 0;
6041: UINT32 cur_time = timeGetTime();
6042:
6043: if(prev_time == cur_time) {
6044: return;
6045: }
6046: prev_time = cur_time;
6047:
6048: for(int ch = 0; ch < 3; ch++) {
6049: if(pit[ch].start && cur_time >= pit[ch].expired_time) {
6050: pit_input_clock(ch, pit[ch].input_clk);
6051:
6052: // next expired time
6053: if(pit[ch].start) {
6054: pit[ch].input_clk = pit[ch].delay ? 1 : pit_get_next_count(ch);
6055: pit[ch].prev_time = pit[ch].expired_time;
6056: pit[ch].expired_time += pit_get_expired_time(pit[ch].input_clk);
6057: if(pit[ch].expired_time <= cur_time) {
6058: pit[ch].prev_time = cur_time;
6059: pit[ch].expired_time = cur_time + pit_get_expired_time(pit[ch].input_clk);
6060: }
6061: }
6062: }
6063: }
6064: }
6065:
6066: void pit_input_clock(int ch, int clock)
6067: {
6068: if(!(pit[ch].start && clock)) {
6069: return;
6070: }
6071: if(pit[ch].delay) {
6072: clock -= 1;
6073: pit[ch].delay = 0;
6074: pit[ch].count = PIT_COUNT_VALUE(ch);
6075: pit[ch].null_count = 0;
6076: }
6077:
6078: // update pit
6079: pit[ch].count -= clock;
6080: INT32 tmp = PIT_COUNT_VALUE(ch);
6081: loop:
6082: if(pit[ch].mode == 3) {
6083: INT32 half = tmp >> 1;
6084: if(pit[ch].count > half) {
6085: pit_set_signal(ch, 1);
6086: } else {
6087: pit_set_signal(ch, 0);
6088: }
6089: } else {
6090: if(pit[ch].count <= 1) {
6091: if(pit[ch].mode == 2 || pit[ch].mode == 4 || pit[ch].mode == 5) {
6092: pit_set_signal(ch, 0);
6093: }
6094: }
6095: if(pit[ch].count <= 0) {
6096: pit_set_signal(ch, 1);
6097: }
6098: }
6099: if(pit[ch].count <= 0) {
6100: if(pit[ch].mode == 0 || pit[ch].mode == 2 || pit[ch].mode == 3) {
6101: pit[ch].count += tmp;
6102: pit[ch].null_count = 0;
6103: goto loop;
6104: } else {
6105: pit[ch].start = 0;
6106: pit[ch].count = 0x10000;
6107: }
6108: }
6109: }
6110:
6111: void pit_start_count(int ch)
6112: {
6113: if(pit[ch].low_write || pit[ch].high_write) {
6114: return;
6115: }
6116: //if(!pit[ch].gate) {
6117: // return;
6118: //}
6119: pit[ch].start = 1;
6120:
6121: // next expired time
6122: pit[ch].input_clk = pit[ch].delay ? 1 : pit_get_next_count(ch);
6123: UINT32 cur_time = timeGetTime();
6124: pit[ch].prev_time = cur_time;
6125: pit[ch].expired_time = cur_time + pit_get_expired_time(pit[ch].input_clk);
6126: }
6127:
6128: void pit_stop_count(int ch)
6129: {
6130: pit[ch].start = 0;
6131: }
6132:
6133: void pit_latch_count(int ch)
6134: {
6135: if(pit[ch].start) {
6136: // update pit
6137: UINT32 cur_time = timeGetTime();
6138: if(cur_time > pit[ch].prev_time) {
6139: UINT32 input = PIT_FREQ * (cur_time - pit[ch].prev_time) / 1000;
6140: pit_input_clock(ch, input);
6141:
6142: if(pit[ch].input_clk <= input) {
6143: // next expired time
6144: if(pit[ch].start) {
6145: pit[ch].input_clk = pit[ch].delay ? 1 : pit_get_next_count(ch);
6146: pit[ch].prev_time = pit[ch].expired_time;
6147: pit[ch].expired_time += pit_get_expired_time(pit[ch].input_clk);
6148: if(pit[ch].expired_time <= cur_time) {
6149: pit[ch].prev_time = cur_time;
6150: pit[ch].expired_time = cur_time + pit_get_expired_time(pit[ch].input_clk);
6151: }
6152: }
6153: } else {
6154: pit[ch].input_clk -= input;
6155: pit[ch].prev_time = cur_time;
6156: }
6157: }
6158: }
6159: // latch pit
6160: pit[ch].latch = (UINT16)pit[ch].count;
6161: pit[ch].count_latched = 1;
6162: if((pit[ch].ctrl_reg & 0x30) == 0x10) {
6163: // lower byte
6164: pit[ch].low_read = 1;
6165: pit[ch].high_read = 0;
6166: } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
6167: // upper byte
6168: pit[ch].low_read = 0;
6169: pit[ch].high_read = 1;
6170: } else {
6171: // lower -> upper
6172: pit[ch].low_read = pit[ch].low_read = 1;
6173: }
6174: }
6175:
6176: void pit_set_signal(int ch, int signal)
6177: {
6178: int prev = pit[ch].prev_out;
6179: pit[ch].prev_out = signal;
6180:
6181: if(prev && !signal) {
6182: // H->L
6183: if(ch == 0) {
6184: pic_req(0, 0, 0);
6185: }
6186: } else if(!prev && signal) {
6187: // L->H
6188: if(ch == 0) {
6189: pic_req(0, 0, 1);
6190: }
6191: }
6192: }
6193:
6194: int pit_get_next_count(int ch)
6195: {
6196: if(pit[ch].mode == 2 || pit[ch].mode == 4 || pit[ch].mode == 5) {
6197: if(pit[ch].count > 1) {
6198: return(pit[ch].count - 1);
6199: } else {
6200: return(1);
6201: }
6202: }
6203: if(pit[ch].mode == 3) {
6204: INT32 half = PIT_COUNT_VALUE(ch) >> 1;
6205: if(pit[ch].count > half) {
6206: return(pit[ch].count - half);
6207: } else {
6208: return(pit[ch].count);
6209: }
6210: }
6211: return(pit[ch].count);
6212: }
6213:
6214: int pit_get_expired_time(int clock)
6215: {
6216: UINT32 val = 1000 * clock / PIT_FREQ;
6217:
6218: if(val > 0) {
6219: return(val);
6220: } else {
6221: return(1);
6222: }
6223: }
6224:
6225: // i/o bus
6226:
6227: UINT8 read_io_byte(offs_t addr)
6228: {
6229: switch(addr) {
6230: #ifdef SUPPORT_HARDWARE
6231: case 0x20:
6232: case 0x21:
6233: return(pic_read(0, addr));
6234: case 0x40:
6235: case 0x41:
6236: case 0x42:
6237: case 0x43:
6238: return(pit_read(addr & 0x03));
6239: #endif
6240: case 0x71:
6241: return(cmos[cmos_addr & 0x7f]);
6242: case 0x92:
1.1.1.3 root 6243: return((m_a20_mask >> 19) & 2);
1.1 root 6244: #ifdef SUPPORT_HARDWARE
6245: case 0xa0:
6246: case 0xa1:
6247: return(pic_read(1, addr));
6248: #endif
6249: default:
6250: // error("inb %4x\n", addr);
6251: break;
6252: }
6253: return(0xff);
6254: }
6255:
6256: UINT16 read_io_word(offs_t addr)
6257: {
6258: return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
6259: }
6260:
6261: UINT32 read_io_dword(offs_t addr)
6262: {
6263: return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
6264: }
6265:
6266: void write_io_byte(offs_t addr, UINT8 val)
6267: {
6268: switch(addr) {
6269: #ifdef SUPPORT_HARDWARE
6270: case 0x20:
6271: case 0x21:
6272: pic_write(0, addr, val);
6273: break;
6274: case 0x40:
6275: case 0x41:
6276: case 0x42:
6277: case 0x43:
6278: pit_write(addr & 0x03, val);
6279: break;
6280: #endif
6281: case 0x64:
6282: if(val == 0xfe) {
6283: if(cmos[0x0f] == 5) {
6284: // reset pic
6285: pic_init();
6286: pic[0].irr = pic[1].irr = 0x00;
6287: pic[0].imr = pic[1].imr = 0xff;
6288: }
6289: CPU_RESET_CALL(CPU_MODEL);
6290: i386_jmp_far(0x40, 0x67);
6291: }
6292: break;
6293: case 0x70:
6294: cmos_addr = val;
6295: break;
6296: case 0x71:
1.1.1.3 root 6297: cmos[cmos_addr & 0x07] = val;
1.1 root 6298: break;
6299: case 0x92:
1.1.1.3 root 6300: i386_set_a20_line(val & 2);
1.1 root 6301: break;
6302: #ifdef SUPPORT_HARDWARE
6303: case 0xa0:
6304: case 0xa1:
6305: pic_write(1, addr, val);
6306: break;
6307: #endif
6308: default:
6309: // error("outb %4x,%2x\n", addr, val);
6310: break;
6311: }
6312: }
6313:
6314: void write_io_word(offs_t addr, UINT16 val)
6315: {
6316: write_io_byte(addr + 0, (val >> 0) & 0xff);
6317: write_io_byte(addr + 1, (val >> 8) & 0xff);
6318: }
6319:
6320: void write_io_dword(offs_t addr, UINT32 val)
6321: {
6322: write_io_byte(addr + 0, (val >> 0) & 0xff);
6323: write_io_byte(addr + 1, (val >> 8) & 0xff);
6324: write_io_byte(addr + 2, (val >> 16) & 0xff);
6325: write_io_byte(addr + 3, (val >> 24) & 0xff);
6326: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.