Annotation of msdos-player/source/msdos.cpp, revision 1.1.1.61

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: 
1.1.1.28  root       10: void exit_handler();
                     11: 
1.1       root       12: #define fatalerror(...) { \
                     13:        fprintf(stderr, __VA_ARGS__); \
1.1.1.28  root       14:        exit_handler(); \
1.1       root       15:        exit(1); \
                     16: }
                     17: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
1.1.1.22  root       18: #define nolog(...)
                     19: 
1.1.1.33  root       20: //#define ENABLE_DEBUG_LOG
                     21: #ifdef ENABLE_DEBUG_LOG
1.1.1.22  root       22:        #define EXPORT_DEBUG_TO_FILE
                     23:        #define ENABLE_DEBUG_SYSCALL
                     24:        #define ENABLE_DEBUG_UNIMPLEMENTED
1.1.1.25  root       25:        #define ENABLE_DEBUG_IOPORT
1.1.1.22  root       26:        
                     27:        #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root       28:                FILE* fp_debug_log = NULL;
1.1.1.22  root       29:        #else
1.1.1.33  root       30:                #define fp_debug_log stderr
1.1.1.22  root       31:        #endif
                     32:        #ifdef ENABLE_DEBUG_UNIMPLEMENTED
                     33:                #define unimplemented_10h fatalerror
1.1.1.42  root       34:                #define unimplemented_13h fatalerror
1.1.1.25  root       35:                #define unimplemented_14h fatalerror
1.1.1.22  root       36:                #define unimplemented_15h fatalerror
                     37:                #define unimplemented_16h fatalerror
1.1.1.37  root       38:                #define unimplemented_17h fatalerror
1.1.1.22  root       39:                #define unimplemented_1ah fatalerror
                     40:                #define unimplemented_21h fatalerror
                     41:                #define unimplemented_2fh fatalerror
1.1.1.24  root       42:                #define unimplemented_33h fatalerror
1.1.1.22  root       43:                #define unimplemented_67h fatalerror
                     44:                #define unimplemented_xms fatalerror
                     45:        #endif
                     46: #endif
                     47: #ifndef unimplemented_10h
                     48:        #define unimplemented_10h nolog
                     49: #endif
1.1.1.42  root       50: #ifndef unimplemented_13h
                     51:        #define unimplemented_13h nolog
                     52: #endif
1.1.1.25  root       53: #ifndef unimplemented_14h
                     54:        #define unimplemented_14h nolog
                     55: #endif
1.1.1.22  root       56: #ifndef unimplemented_15h
                     57:        #define unimplemented_15h nolog
                     58: #endif
                     59: #ifndef unimplemented_16h
                     60:        #define unimplemented_16h nolog
                     61: #endif
1.1.1.37  root       62: #ifndef unimplemented_17h
                     63:        #define unimplemented_17h nolog
                     64: #endif
1.1.1.22  root       65: #ifndef unimplemented_1ah
                     66:        #define unimplemented_1ah nolog
                     67: #endif
                     68: #ifndef unimplemented_21h
                     69:        #define unimplemented_21h nolog
                     70: #endif
                     71: #ifndef unimplemented_2fh
                     72:        #define unimplemented_2fh nolog
                     73: #endif
1.1.1.24  root       74: #ifndef unimplemented_33h
                     75:        #define unimplemented_33h nolog
                     76: #endif
1.1.1.22  root       77: #ifndef unimplemented_67h
                     78:        #define unimplemented_67h nolog
                     79: #endif
                     80: #ifndef unimplemented_xms
                     81:        #define unimplemented_xms nolog
                     82: #endif
                     83: 
1.1.1.60  root       84: #ifdef _MBCS
                     85: inline char *my_strchr(char *str, int chr)
                     86: {
                     87:        return (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr));
                     88: }
                     89: inline const char *my_strchr(const char *str, int chr)
                     90: {
                     91:        return (const char *)_mbschr((const unsigned char *)(str), (unsigned int)(chr));
                     92: }
                     93: inline char *my_strrchr(char *str, int chr)
                     94: {
                     95:        return (char *)_mbsrchr((unsigned char *)(str), (unsigned int)(chr));
                     96: }
                     97: inline const char *my_strrchr(const char *str, int chr)
                     98: {
                     99:        return (const char *)_mbsrchr((const unsigned char *)(str), (unsigned int)(chr));
                    100: }
                    101: inline char *my_strtok(char *tok, const char *del)
                    102: {
                    103:        return (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del));
                    104: }
                    105: inline char *my_strupr(char *str)
                    106: {
                    107:        return (char *)_mbsupr((unsigned char *)(str));
                    108: }
                    109: #else
                    110: #define my_strchr(str, chr) strchr((str), (chr))
                    111: #define my_strrchr(str, chr) strrchr((str), (chr))
                    112: #define my_strtok(tok, del) strtok((tok), (del))
                    113: #define my_strupr(str) _strupr((str))
                    114: #endif
1.1.1.32  root      115: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1       root      116: 
1.1.1.12  root      117: #if defined(__MINGW32__)
                    118: extern "C" int _CRT_glob = 0;
                    119: #endif
                    120: 
                    121: /*
                    122:        kludge for "more-standardized" C++
                    123: */
                    124: #if !defined(_MSC_VER)
                    125: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                    126: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                    127: #define min(a,b) kludge_min(a,b)
                    128: #define max(a,b) kludge_max(a,b)
1.1.1.14  root      129: #elif _MSC_VER >= 1400
                    130: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                    131: {
                    132: }
                    133: #endif
                    134: 
1.1.1.35  root      135: #define USE_VRAM_THREAD
1.1.1.14  root      136: 
1.1.1.35  root      137: #ifdef USE_VRAM_THREAD
1.1.1.14  root      138: static CRITICAL_SECTION vram_crit_sect;
                    139: #else
                    140: #define vram_flush()
1.1.1.12  root      141: #endif
                    142: 
1.1.1.14  root      143: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    144: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    145: 
                    146: void change_console_size(int width, int height);
                    147: void clear_scr_buffer(WORD attr);
                    148: 
                    149: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    150: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    151: static COORD vram_coord_char, vram_coord_attr;
                    152: 
1.1.1.28  root      153: char temp_file_path[MAX_PATH];
                    154: bool temp_file_created = false;
                    155: 
1.1.1.14  root      156: bool ignore_illegal_insn = false;
                    157: bool limit_max_memory = false;
                    158: bool no_windows = false;
                    159: bool stay_busy = false;
1.1.1.19  root      160: bool support_ems = false;
                    161: #ifdef SUPPORT_XMS
                    162: bool support_xms = false;
                    163: #endif
1.1.1.29  root      164: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      165: 
1.1.1.54  root      166: BOOL is_xp_64_or_later;
1.1.1.14  root      167: BOOL is_vista_or_later;
                    168: 
1.1.1.35  root      169: #define UPDATE_OPS 16384
                    170: #define REQUEST_HARDWRE_UPDATE() { \
                    171:        update_ops = UPDATE_OPS - 1; \
                    172: }
                    173: UINT32 update_ops = 0;
                    174: UINT32 idle_ops = 0;
                    175: 
1.1.1.54  root      176: inline BOOL is_sse2_ready()
                    177: {
                    178:        static int result = -1;
                    179:        int cpu_info[4];
                    180:        
                    181:        if(result == -1) {
                    182:                result = 0;
                    183:                __cpuid(cpu_info, 0);
                    184:                if(cpu_info[0] >= 1){
                    185:                        __cpuid(cpu_info, 1);
                    186:                        if(cpu_info[3] & (1 << 26)) {
                    187:                                result = 1;
                    188:                        }
                    189:                }
                    190:        }
                    191:        return(result == 1);
                    192: }
                    193: 
1.1.1.14  root      194: inline void maybe_idle()
                    195: {
                    196:        // if it appears to be in a tight loop, assume waiting for input
                    197:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      198:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.54  root      199:                if(is_sse2_ready()) {
                    200:                        _mm_pause();    // SSE2 pause
                    201:                } else if(is_xp_64_or_later) {
                    202:                        Sleep(0);       // switch to other thread that is ready to run, without checking priority
                    203:                } else {
                    204:                        Sleep(1);
                    205:                        REQUEST_HARDWRE_UPDATE();
                    206:                }
1.1.1.14  root      207:        }
1.1.1.35  root      208:        idle_ops = 0;
1.1.1.14  root      209: }
1.1.1.12  root      210: 
1.1       root      211: /* ----------------------------------------------------------------------------
1.1.1.3   root      212:        MAME i86/i386
1.1       root      213: ---------------------------------------------------------------------------- */
                    214: 
1.1.1.10  root      215: #ifndef __BIG_ENDIAN__
1.1       root      216: #define LSB_FIRST
1.1.1.10  root      217: #endif
1.1       root      218: 
                    219: #ifndef INLINE
                    220: #define INLINE inline
                    221: #endif
                    222: #define U64(v) UINT64(v)
                    223: 
                    224: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    225: #define logerror(...)
                    226: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    227: #define popmessage(...)
                    228: 
                    229: /*****************************************************************************/
1.1.1.10  root      230: /* src/emu/devcpu.h */
                    231: 
                    232: // CPU interface functions
                    233: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    234: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    235: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    236: 
                    237: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    238: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    239: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    240: 
                    241: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    242: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    243: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    244: 
                    245: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    246: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    247: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    248: 
                    249: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    250: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    251: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    252: 
1.1.1.14  root      253: #define CPU_MODEL_STR(name)                    #name
                    254: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    255: 
1.1.1.10  root      256: /*****************************************************************************/
                    257: /* src/emu/didisasm.h */
                    258: 
                    259: // Disassembler constants
                    260: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    261: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    262: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    263: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    264: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    265: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    266: 
                    267: /*****************************************************************************/
1.1       root      268: /* src/emu/diexec.h */
                    269: 
                    270: // I/O line states
                    271: enum line_state
                    272: {
                    273:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    274:        ASSERT_LINE,                            // assert an interrupt immediately
                    275:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    276:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    277: };
                    278: 
                    279: // I/O line definitions
                    280: enum
                    281: {
                    282:        INPUT_LINE_IRQ = 0,
                    283:        INPUT_LINE_NMI
                    284: };
                    285: 
                    286: /*****************************************************************************/
1.1.1.10  root      287: /* src/emu/dimemory.h */
1.1       root      288: 
1.1.1.10  root      289: // Translation intentions
                    290: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    291: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    292: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    293: 
                    294: const int TRANSLATE_READ            = 0;        // translate for read
                    295: const int TRANSLATE_WRITE           = 1;        // translate for write
                    296: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    297: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    298: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    299: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    300: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    301: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    302: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      303: 
1.1.1.10  root      304: /*****************************************************************************/
                    305: /* src/emu/emucore.h */
1.1       root      306: 
1.1.1.10  root      307: // constants for expression endianness
                    308: enum endianness_t
                    309: {
                    310:        ENDIANNESS_LITTLE,
                    311:        ENDIANNESS_BIG
                    312: };
1.1       root      313: 
1.1.1.10  root      314: // declare native endianness to be one or the other
                    315: #ifdef LSB_FIRST
                    316: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    317: #else
                    318: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    319: #endif
                    320: 
                    321: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    322: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    323: 
                    324: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    325: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    326: 
                    327: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    328: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      329: 
                    330: /*****************************************************************************/
                    331: /* src/emu/memory.h */
                    332: 
1.1.1.10  root      333: // address spaces
                    334: enum address_spacenum
                    335: {
                    336:        AS_0,                           // first address space
                    337:        AS_1,                           // second address space
                    338:        AS_2,                           // third address space
                    339:        AS_3,                           // fourth address space
                    340:        ADDRESS_SPACES,                 // maximum number of address spaces
                    341: 
                    342:        // alternate address space names for common use
                    343:        AS_PROGRAM = AS_0,              // program address space
                    344:        AS_DATA = AS_1,                 // data address space
                    345:        AS_IO = AS_2                    // I/O address space
                    346: };
                    347: 
1.1       root      348: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      349: //typedef UINT32       offs_t;
1.1       root      350: 
                    351: // read accessors
                    352: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      353: #ifdef USE_DEBUGGER
                    354: {
                    355:        if(now_debugging) {
                    356:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    357:                        if(rd_break_point.table[i].status == 1) {
                    358:                                if(byteaddress == rd_break_point.table[i].addr) {
                    359:                                        rd_break_point.hit = i + 1;
                    360:                                        now_suspended = true;
                    361:                                        break;
                    362:                                }
                    363:                        }
                    364:                }
                    365:        }
                    366:        return(debugger_read_byte(byteaddress));
                    367: }
                    368: UINT8 debugger_read_byte(offs_t byteaddress)
                    369: #endif
1.1       root      370: {
1.1.1.4   root      371: #if defined(HAS_I386)
1.1       root      372:        if(byteaddress < MAX_MEM) {
                    373:                return mem[byteaddress];
1.1.1.3   root      374: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    375: //             return read_byte(byteaddress & 0xfffff);
1.1       root      376:        }
                    377:        return 0;
1.1.1.4   root      378: #else
                    379:        return mem[byteaddress];
                    380: #endif
1.1       root      381: }
                    382: 
                    383: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      384: #ifdef USE_DEBUGGER
                    385: {
                    386:        if(now_debugging) {
                    387:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    388:                        if(rd_break_point.table[i].status == 1) {
                    389:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    390:                                        rd_break_point.hit = i + 1;
                    391:                                        now_suspended = true;
                    392:                                        break;
                    393:                                }
                    394:                        }
                    395:                }
                    396:        }
                    397:        return(debugger_read_word(byteaddress));
                    398: }
                    399: UINT16 debugger_read_word(offs_t byteaddress)
                    400: #endif
1.1       root      401: {
1.1.1.14  root      402:        if(byteaddress == 0x41c) {
                    403:                // pointer to first free slot in keyboard buffer
1.1.1.35  root      404:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    405: #ifdef USE_SERVICE_THREAD
                    406:                        EnterCriticalSection(&key_buf_crit_sect);
                    407: #endif
1.1.1.55  root      408:                        bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root      409: #ifdef USE_SERVICE_THREAD
                    410:                        LeaveCriticalSection(&key_buf_crit_sect);
                    411: #endif
1.1.1.51  root      412:                        if(empty) maybe_idle();
1.1.1.14  root      413:                }
                    414:        }
1.1.1.4   root      415: #if defined(HAS_I386)
1.1       root      416:        if(byteaddress < MAX_MEM - 1) {
                    417:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      418: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    419: //             return read_word(byteaddress & 0xfffff);
1.1       root      420:        }
                    421:        return 0;
1.1.1.4   root      422: #else
                    423:        return *(UINT16 *)(mem + byteaddress);
                    424: #endif
1.1       root      425: }
                    426: 
                    427: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      428: #ifdef USE_DEBUGGER
                    429: {
                    430:        if(now_debugging) {
                    431:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    432:                        if(rd_break_point.table[i].status == 1) {
                    433:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    434:                                        rd_break_point.hit = i + 1;
                    435:                                        now_suspended = true;
                    436:                                        break;
                    437:                                }
                    438:                        }
                    439:                }
                    440:        }
                    441:        return(debugger_read_dword(byteaddress));
                    442: }
                    443: UINT32 debugger_read_dword(offs_t byteaddress)
                    444: #endif
1.1       root      445: {
1.1.1.4   root      446: #if defined(HAS_I386)
1.1       root      447:        if(byteaddress < MAX_MEM - 3) {
                    448:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      449: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    450: //             return read_dword(byteaddress & 0xfffff);
1.1       root      451:        }
                    452:        return 0;
1.1.1.4   root      453: #else
                    454:        return *(UINT32 *)(mem + byteaddress);
                    455: #endif
1.1       root      456: }
                    457: 
                    458: // write accessors
1.1.1.35  root      459: #ifdef USE_VRAM_THREAD
1.1.1.14  root      460: void vram_flush_char()
                    461: {
                    462:        if(vram_length_char != 0) {
                    463:                DWORD num;
1.1.1.60  root      464:                WriteConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      465:                vram_length_char = vram_last_length_char = 0;
                    466:        }
                    467: }
                    468: 
                    469: void vram_flush_attr()
                    470: {
                    471:        if(vram_length_attr != 0) {
                    472:                DWORD num;
1.1.1.23  root      473:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      474:                vram_length_attr = vram_last_length_attr = 0;
                    475:        }
                    476: }
                    477: 
                    478: void vram_flush()
                    479: {
                    480:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    481:                EnterCriticalSection(&vram_crit_sect);
                    482:                vram_flush_char();
                    483:                vram_flush_attr();
                    484:                LeaveCriticalSection(&vram_crit_sect);
                    485:        }
                    486: }
                    487: #endif
                    488: 
                    489: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      490: {
1.1.1.35  root      491: #ifdef USE_VRAM_THREAD
1.1.1.14  root      492:        static offs_t first_offset_char, last_offset_char;
                    493:        
                    494:        if(vram_length_char != 0) {
                    495:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    496:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    497:                        return;
                    498:                }
                    499:                if(offset != last_offset_char + 2) {
                    500:                        vram_flush_char();
                    501:                }
                    502:        }
                    503:        if(vram_length_char == 0) {
                    504:                first_offset_char = offset;
                    505:                vram_coord_char.X = (offset >> 1) % scr_width;
                    506:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    507:        }
                    508:        scr_char[vram_length_char++] = data;
                    509:        last_offset_char = offset;
                    510: #else
1.1.1.8   root      511:        COORD co;
                    512:        DWORD num;
                    513:        
1.1.1.14  root      514:        co.X = (offset >> 1) % scr_width;
                    515:        co.Y = (offset >> 1) / scr_width;
                    516:        scr_char[0] = data;
1.1.1.60  root      517:        WriteConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      518: #endif
                    519: }
                    520: 
                    521: void write_text_vram_attr(offs_t offset, UINT8 data)
                    522: {
1.1.1.35  root      523: #ifdef USE_VRAM_THREAD
1.1.1.14  root      524:        static offs_t first_offset_attr, last_offset_attr;
                    525:        
                    526:        if(vram_length_attr != 0) {
                    527:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    528:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    529:                        return;
                    530:                }
                    531:                if(offset != last_offset_attr + 2) {
                    532:                        vram_flush_attr();
                    533:                }
                    534:        }
                    535:        if(vram_length_attr == 0) {
                    536:                first_offset_attr = offset;
                    537:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    538:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    539:        }
                    540:        scr_attr[vram_length_attr++] = data;
                    541:        last_offset_attr = offset;
                    542: #else
                    543:        COORD co;
                    544:        DWORD num;
1.1.1.8   root      545:        
1.1.1.14  root      546:        co.X = (offset >> 1) % scr_width;
                    547:        co.Y = (offset >> 1) / scr_width;
                    548:        scr_attr[0] = data;
1.1.1.23  root      549:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      550: #endif
                    551: }
                    552: 
                    553: void write_text_vram_byte(offs_t offset, UINT8 data)
                    554: {
1.1.1.35  root      555: #ifdef USE_VRAM_THREAD
1.1.1.14  root      556:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      557: #endif
1.1.1.8   root      558:        if(offset & 1) {
1.1.1.14  root      559:                write_text_vram_attr(offset, data);
1.1.1.8   root      560:        } else {
1.1.1.14  root      561:                write_text_vram_char(offset, data);
1.1.1.8   root      562:        }
1.1.1.35  root      563: #ifdef USE_VRAM_THREAD
1.1.1.14  root      564:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      565: #endif
1.1.1.8   root      566: }
                    567: 
                    568: void write_text_vram_word(offs_t offset, UINT16 data)
                    569: {
1.1.1.35  root      570: #ifdef USE_VRAM_THREAD
1.1.1.14  root      571:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      572: #endif
1.1.1.8   root      573:        if(offset & 1) {
1.1.1.14  root      574:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    575:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      576:        } else {
1.1.1.14  root      577:                write_text_vram_char(offset    , (data     ) & 0xff);
                    578:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      579:        }
1.1.1.35  root      580: #ifdef USE_VRAM_THREAD
1.1.1.14  root      581:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      582: #endif
1.1.1.8   root      583: }
                    584: 
                    585: void write_text_vram_dword(offs_t offset, UINT32 data)
                    586: {
1.1.1.35  root      587: #ifdef USE_VRAM_THREAD
1.1.1.14  root      588:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      589: #endif
1.1.1.8   root      590:        if(offset & 1) {
1.1.1.14  root      591:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    592:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    593:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    594:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    595:        } else {
                    596:                write_text_vram_char(offset    , (data      ) & 0xff);
                    597:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    598:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    599:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      600:        }
1.1.1.35  root      601: #ifdef USE_VRAM_THREAD
1.1.1.14  root      602:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      603: #endif
1.1.1.8   root      604: }
                    605: 
1.1       root      606: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      607: #ifdef USE_DEBUGGER
                    608: {
                    609:        if(now_debugging) {
                    610:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    611:                        if(wr_break_point.table[i].status == 1) {
                    612:                                if(byteaddress == wr_break_point.table[i].addr) {
                    613:                                        wr_break_point.hit = i + 1;
                    614:                                        now_suspended = true;
                    615:                                        break;
                    616:                                }
                    617:                        }
                    618:                }
                    619:        }
                    620:        debugger_write_byte(byteaddress, data);
                    621: }
                    622: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    623: #endif
1.1       root      624: {
1.1.1.8   root      625:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      626:                mem[byteaddress] = data;
1.1.1.8   root      627:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      628:                if(!restore_console_on_exit) {
                    629:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      630:                }
1.1.1.8   root      631:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    632:                mem[byteaddress] = data;
                    633:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    634:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    635:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      636:                }
                    637:                mem[byteaddress] = data;
1.1.1.4   root      638: #if defined(HAS_I386)
1.1.1.3   root      639:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      640: #else
                    641:        } else {
                    642: #endif
1.1.1.3   root      643:                mem[byteaddress] = data;
1.1       root      644:        }
                    645: }
                    646: 
                    647: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      648: #ifdef USE_DEBUGGER
                    649: {
                    650:        if(now_debugging) {
                    651:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    652:                        if(wr_break_point.table[i].status == 1) {
                    653:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    654:                                        wr_break_point.hit = i + 1;
                    655:                                        now_suspended = true;
                    656:                                        break;
                    657:                                }
                    658:                        }
                    659:                }
                    660:        }
                    661:        debugger_write_word(byteaddress, data);
                    662: }
                    663: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    664: #endif
1.1       root      665: {
1.1.1.8   root      666:        if(byteaddress < MEMORY_END) {
1.1.1.51  root      667:                if(byteaddress == cursor_position_address) {
                    668:                        if(*(UINT16 *)(mem + byteaddress) != data) {
                    669:                                COORD co;
                    670:                                co.X = data & 0xff;
                    671:                                co.Y = (data >> 8) + scr_top;
                    672:                                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.59  root      673:                                cursor_moved = false;
                    674:                                cursor_moved_by_crtc = false;
1.1.1.51  root      675:                        }
1.1.1.14  root      676:                }
1.1.1.3   root      677:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      678:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      679:                if(!restore_console_on_exit) {
                    680:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      681:                }
1.1.1.8   root      682:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    683:                *(UINT16 *)(mem + byteaddress) = data;
                    684:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    685:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    686:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      687:                }
                    688:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      689: #if defined(HAS_I386)
1.1.1.3   root      690:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      691: #else
                    692:        } else {
                    693: #endif
1.1.1.3   root      694:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      695:        }
                    696: }
                    697: 
                    698: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      699: #ifdef USE_DEBUGGER
                    700: {
                    701:        if(now_debugging) {
                    702:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    703:                        if(wr_break_point.table[i].status == 1) {
                    704:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    705:                                        wr_break_point.hit = i + 1;
                    706:                                        now_suspended = true;
                    707:                                        break;
                    708:                                }
                    709:                        }
                    710:                }
                    711:        }
                    712:        debugger_write_dword(byteaddress, data);
                    713: }
                    714: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    715: #endif
1.1       root      716: {
1.1.1.8   root      717:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      718:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      719:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      720:                if(!restore_console_on_exit) {
                    721:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      722:                }
1.1.1.8   root      723:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    724:                *(UINT32 *)(mem + byteaddress) = data;
                    725:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    726:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    727:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      728:                }
                    729:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      730: #if defined(HAS_I386)
1.1.1.3   root      731:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      732: #else
                    733:        } else {
                    734: #endif
1.1.1.3   root      735:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      736:        }
                    737: }
                    738: 
                    739: #define read_decrypted_byte read_byte
                    740: #define read_decrypted_word read_word
                    741: #define read_decrypted_dword read_dword
                    742: 
1.1.1.3   root      743: #define read_raw_byte read_byte
                    744: #define write_raw_byte write_byte
                    745: 
                    746: #define read_word_unaligned read_word
                    747: #define write_word_unaligned write_word
                    748: 
                    749: #define read_io_word_unaligned read_io_word
                    750: #define write_io_word_unaligned write_io_word
                    751: 
1.1       root      752: UINT8 read_io_byte(offs_t byteaddress);
                    753: UINT16 read_io_word(offs_t byteaddress);
                    754: UINT32 read_io_dword(offs_t byteaddress);
                    755: 
                    756: void write_io_byte(offs_t byteaddress, UINT8 data);
                    757: void write_io_word(offs_t byteaddress, UINT16 data);
                    758: void write_io_dword(offs_t byteaddress, UINT32 data);
                    759: 
                    760: /*****************************************************************************/
                    761: /* src/osd/osdcomm.h */
                    762: 
                    763: /* Highly useful macro for compile-time knowledge of an array size */
                    764: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    765: 
1.1.1.54  root      766: // flag to exit MS-DOS Player
                    767: // this is set when the first process is terminated and jump to FFFF:0000 HALT
                    768: int m_exit = 0;
                    769: 
1.1.1.3   root      770: #if defined(HAS_I386)
1.1.1.10  root      771:        static CPU_TRANSLATE(i386);
                    772:        #include "mame/lib/softfloat/softfloat.c"
                    773:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      774:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      775: #elif defined(HAS_I286)
1.1.1.10  root      776:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      777: #else
1.1.1.10  root      778:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      779: #endif
1.1.1.33  root      780: #ifdef USE_DEBUGGER
1.1.1.10  root      781:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      782: #endif
                    783: 
1.1.1.3   root      784: #if defined(HAS_I386)
                    785:        #define SREG(x)                         m_sreg[x].selector
                    786:        #define SREG_BASE(x)                    m_sreg[x].base
                    787:        int cpu_type, cpu_step;
                    788: #else
                    789:        #define REG8(x)                         m_regs.b[x]
                    790:        #define REG16(x)                        m_regs.w[x]
                    791:        #define SREG(x)                         m_sregs[x]
                    792:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      793:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      794:        #define m_CF                            m_CarryVal
                    795:        #define m_a20_mask                      AMASK
                    796:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    797:        #if defined(HAS_I286)
                    798:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    799:        #else
                    800:                #define i386_set_a20_line(x)
                    801:        #endif
                    802:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    803: #endif
1.1       root      804: 
                    805: void i386_jmp_far(UINT16 selector, UINT32 address)
                    806: {
1.1.1.3   root      807: #if defined(HAS_I386)
1.1       root      808:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      809:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      810:        } else {
1.1.1.3   root      811:                SREG(CS) = selector;
                    812:                m_performed_intersegment_jump = 1;
                    813:                i386_load_segment_descriptor(CS);
                    814:                m_eip = address;
                    815:                CHANGE_PC(m_eip);
1.1       root      816:        }
1.1.1.3   root      817: #elif defined(HAS_I286)
                    818:        i80286_code_descriptor(selector, address, 1);
                    819: #else
                    820:        SREG(CS) = selector;
                    821:        i386_load_segment_descriptor(CS);
                    822:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    823: #endif
1.1       root      824: }
                    825: 
1.1.1.24  root      826: void i386_call_far(UINT16 selector, UINT32 address)
                    827: {
                    828: #if defined(HAS_I386)
                    829:        if(PROTECTED_MODE && !V8086_MODE) {
                    830:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    831:        } else {
                    832:                PUSH16(SREG(CS));
                    833:                PUSH16(m_eip);
                    834:                SREG(CS) = selector;
                    835:                m_performed_intersegment_jump = 1;
                    836:                i386_load_segment_descriptor(CS);
                    837:                m_eip = address;
                    838:                CHANGE_PC(m_eip);
                    839:        }
                    840: #else
                    841:        UINT16 ip = m_pc - SREG_BASE(CS);
                    842:        UINT16 cs = SREG(CS);
                    843: #if defined(HAS_I286)
                    844:        i80286_code_descriptor(selector, address, 2);
                    845: #else
                    846:        SREG(CS) = selector;
                    847:        i386_load_segment_descriptor(CS);
                    848:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    849: #endif
                    850:        PUSH(cs);
                    851:        PUSH(ip);
                    852:        CHANGE_PC(m_pc);
                    853: #endif
                    854: }
1.1.1.49  root      855: 
                    856: void i386_push16(UINT16 value)
                    857: {
                    858: #if defined(HAS_I386)
                    859:        PUSH16(value);
                    860: #else
                    861:        PUSH(value);
1.1.1.35  root      862: #endif
1.1.1.49  root      863: }
                    864: 
                    865: UINT16 i386_pop16()
                    866: {
                    867: #if defined(HAS_I386)
                    868:        return POP16();
                    869: #else
                    870:        UINT16 value;
                    871:        POP(value);
                    872:        return value;
                    873: #endif
                    874: }
1.1.1.24  root      875: 
1.1.1.29  root      876: UINT16 i386_read_stack()
                    877: {
                    878: #if defined(HAS_I386)
                    879:        UINT32 ea, new_esp;
                    880:        if( STACK_32BIT ) {
                    881:                new_esp = REG32(ESP) + 2;
                    882:                ea = i386_translate(SS, new_esp - 2, 0);
                    883:        } else {
                    884:                new_esp = REG16(SP) + 2;
                    885:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    886:        }
                    887:        return READ16(ea);
                    888: #else
                    889:        UINT16 sp = m_regs.w[SP] + 2;
                    890:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    891: #endif
                    892: }
                    893: 
1.1.1.53  root      894: void i386_write_stack(UINT16 value)
                    895: {
                    896: #if defined(HAS_I386)
                    897:        UINT32 ea, new_esp;
                    898:        if( STACK_32BIT ) {
                    899:                new_esp = REG32(ESP) + 2;
                    900:                ea = i386_translate(SS, new_esp - 2, 0);
                    901:        } else {
                    902:                new_esp = REG16(SP) + 2;
                    903:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    904:        }
                    905:        WRITE16(ea, value);
                    906: #else
                    907:        UINT16 sp = m_regs.w[SP] + 2;
                    908:        WriteWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK), value);
                    909: #endif
                    910: }
                    911: 
1.1       root      912: /* ----------------------------------------------------------------------------
1.1.1.33  root      913:        debugger
                    914: ---------------------------------------------------------------------------- */
                    915: 
                    916: #ifdef USE_DEBUGGER
                    917: #define TELNET_BLUE      0x0004 // text color contains blue.
                    918: #define TELNET_GREEN     0x0002 // text color contains green.
                    919: #define TELNET_RED       0x0001 // text color contains red.
                    920: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    921: 
                    922: int svr_socket = 0;
                    923: int cli_socket = 0;
                    924: 
1.1.1.55  root      925: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
1.1.1.33  root      926: 
                    927: void debugger_init()
                    928: {
                    929:        now_debugging = false;
                    930:        now_going = false;
                    931:        now_suspended = false;
                    932:        force_suspend = false;
                    933:        
                    934:        memset(&break_point, 0, sizeof(break_point_t));
                    935:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    936:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    937:        memset(&in_break_point, 0, sizeof(break_point_t));
                    938:        memset(&out_break_point, 0, sizeof(break_point_t));
                    939:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    940: }
                    941: 
1.1.1.45  root      942: void telnet_send(const char *string)
1.1.1.33  root      943: {
                    944:        char buffer[8192], *ptr;
                    945:        strcpy(buffer, string);
                    946:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    947:                char tmp[8192];
                    948:                *ptr = '\0';
                    949:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    950:                strcpy(buffer, tmp);
                    951:        }
                    952:        
                    953:        int len = strlen(buffer), res;
                    954:        ptr = buffer;
                    955:        while(len > 0) {
                    956:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    957:                        len -= res;
                    958:                        ptr += res;
                    959:                }
                    960:        }
                    961: }
                    962: 
                    963: void telnet_command(const char *format, ...)
                    964: {
                    965:        char buffer[1024];
                    966:        va_list ap;
                    967:        va_start(ap, format);
                    968:        vsprintf(buffer, format, ap);
                    969:        va_end(ap);
                    970:        
                    971:        telnet_send(buffer);
                    972: }
                    973: 
                    974: void telnet_printf(const char *format, ...)
                    975: {
                    976:        char buffer[1024];
                    977:        va_list ap;
                    978:        va_start(ap, format);
                    979:        vsprintf(buffer, format, ap);
                    980:        va_end(ap);
                    981:        
                    982:        if(fp_debugger != NULL) {
                    983:                fprintf(fp_debugger, "%s", buffer);
                    984:        }
                    985:        telnet_send(buffer);
                    986: }
                    987: 
                    988: bool telnet_gets(char *str, int n)
                    989: {
                    990:        char buffer[1024];
                    991:        int ptr = 0;
                    992:        
                    993:        telnet_command("\033[12l"); // local echo on
                    994:        telnet_command("\033[2l");  // key unlock
                    995:        
1.1.1.54  root      996:        while(!m_exit) {
1.1.1.33  root      997:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    998:                
                    999:                if(len > 0 && buffer[0] != 0xff) {
                   1000:                        for(int i = 0; i < len; i++) {
                   1001:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1002:                                        str[ptr] = 0;
                   1003:                                        telnet_command("\033[2h");  // key lock
                   1004:                                        telnet_command("\033[12h"); // local echo off
1.1.1.54  root     1005:                                        return(!m_exit);
1.1.1.33  root     1006:                                } else if(buffer[i] == 0x08) {
                   1007:                                        if(ptr > 0) {
                   1008:                                                telnet_command("\033[0K"); // erase from cursor position
                   1009:                                                ptr--;
                   1010:                                        } else {
                   1011:                                                telnet_command("\033[1C"); // move cursor forward
                   1012:                                        }
                   1013:                                } else if(ptr < n - 1) {
1.1.1.37  root     1014:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root     1015:                                                str[ptr++] = buffer[i];
                   1016:                                        }
                   1017:                                } else {
                   1018:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                   1019:                                }
                   1020:                        }
                   1021:                } else if(len == -1) {
                   1022:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1023:                                return(false);
                   1024:                        }
                   1025:                } else if(len == 0) {
                   1026:                        return(false);
                   1027:                }
                   1028:                Sleep(10);
                   1029:        }
1.1.1.54  root     1030:        return(!m_exit);
1.1.1.33  root     1031: }
                   1032: 
                   1033: bool telnet_kbhit()
                   1034: {
                   1035:        char buffer[1024];
                   1036:        
1.1.1.54  root     1037:        if(!m_exit) {
1.1.1.33  root     1038:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1039:                
                   1040:                if(len > 0) {
                   1041:                        for(int i = 0; i < len; i++) {
                   1042:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1043:                                        return(true);
                   1044:                                }
                   1045:                        }
                   1046:                } else if(len == 0) {
                   1047:                        return(true); // disconnected
                   1048:                }
                   1049:        }
                   1050:        return(false);
                   1051: }
                   1052: 
                   1053: bool telnet_disconnected()
                   1054: {
                   1055:        char buffer[1024];
                   1056:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1057:        
                   1058:        if(len == 0) {
                   1059:                return(true);
                   1060:        } else if(len == -1) {
                   1061:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1062:                        return(true);
                   1063:                }
                   1064:        }
                   1065:        return(false);
                   1066: }
                   1067: 
                   1068: void telnet_set_color(int color)
                   1069: {
                   1070:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                   1071: }
                   1072: 
                   1073: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                   1074: {
                   1075: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                   1076:        UINT8 ops[16];
                   1077:        for(int i = 0; i < 16; i++) {
                   1078:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1079:        }
                   1080:        UINT8 *oprom = ops;
                   1081:        
                   1082: #if defined(HAS_I386)
                   1083:        if(m_operand_size) {
                   1084:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1085:        } else
                   1086: #endif
                   1087:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1088: }
                   1089: 
                   1090: void debugger_regs_info(char *buffer)
                   1091: {
                   1092: #if defined(HAS_I386)
                   1093:        UINT32 flags = get_flags();
                   1094: #else
                   1095:        UINT32 flags = CompressFlags();
                   1096: #endif
                   1097: #if defined(HAS_I386)
                   1098:        if(m_operand_size) {
                   1099:                sprintf(buffer, "EAX=%08X  EBX=%08X  ECX=%08X  EDX=%08X\nESP=%08X  EBP=%08X  ESI=%08X  EDI=%08X\nEIP=%08X  DS=%04X  ES=%04X  SS=%04X  CS=%04X  FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
                   1100:                REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
                   1101:                PROTECTED_MODE ? "PE" : "--",
                   1102:                (flags & 0x40000) ? 'A' : '-',
                   1103:                (flags & 0x20000) ? 'V' : '-',
                   1104:                (flags & 0x10000) ? 'R' : '-',
                   1105:                (flags & 0x04000) ? 'N' : '-',
                   1106:                (flags & 0x02000) ? '1' : '0',
                   1107:                (flags & 0x01000) ? '1' : '0',
                   1108:                (flags & 0x00800) ? 'O' : '-',
                   1109:                (flags & 0x00400) ? 'D' : '-',
                   1110:                (flags & 0x00200) ? 'I' : '-',
                   1111:                (flags & 0x00100) ? 'T' : '-',
                   1112:                (flags & 0x00080) ? 'S' : '-',
                   1113:                (flags & 0x00040) ? 'Z' : '-',
                   1114:                (flags & 0x00010) ? 'A' : '-',
                   1115:                (flags & 0x00004) ? 'P' : '-',
                   1116:                (flags & 0x00001) ? 'C' : '-');
                   1117:        } else {
                   1118: #endif
                   1119:                sprintf(buffer, "AX=%04X  BX=%04X  CX=%04X  DX=%04X  SP=%04X  BP=%04X  SI=%04X  DI=%04X\nIP=%04X  DS=%04X  ES=%04X  SS=%04X  CS=%04X  FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
                   1120:                REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
                   1121: #if defined(HAS_I386)
                   1122:                PROTECTED_MODE ? "PE" : "--",
                   1123: #else
                   1124:                "--",
                   1125: #endif
                   1126:                (flags & 0x40000) ? 'A' : '-',
                   1127:                (flags & 0x20000) ? 'V' : '-',
                   1128:                (flags & 0x10000) ? 'R' : '-',
                   1129:                (flags & 0x04000) ? 'N' : '-',
                   1130:                (flags & 0x02000) ? '1' : '0',
                   1131:                (flags & 0x01000) ? '1' : '0',
                   1132:                (flags & 0x00800) ? 'O' : '-',
                   1133:                (flags & 0x00400) ? 'D' : '-',
                   1134:                (flags & 0x00200) ? 'I' : '-',
                   1135:                (flags & 0x00100) ? 'T' : '-',
                   1136:                (flags & 0x00080) ? 'S' : '-',
                   1137:                (flags & 0x00040) ? 'Z' : '-',
                   1138:                (flags & 0x00010) ? 'A' : '-',
                   1139:                (flags & 0x00004) ? 'P' : '-',
                   1140:                (flags & 0x00001) ? 'C' : '-');
                   1141: #if defined(HAS_I386)
                   1142:        }
                   1143: #endif
                   1144: }
                   1145: 
                   1146: void debugger_process_info(char *buffer)
                   1147: {
                   1148:        UINT16 psp_seg = current_psp;
                   1149:        process_t *process;
                   1150:        bool check[0x10000] = {0};
                   1151:        
                   1152:        buffer[0] = '\0';
                   1153:        
                   1154:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1155:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1156:                char *file = process->module_path, *s;
                   1157:                char tmp[8192];
                   1158:                
                   1159:                while((s = strstr(file, "\\")) != NULL) {
                   1160:                        file = s + 1;
                   1161:                }
                   1162:                sprintf(tmp, "PSP=%04X  ENV=%04X  RETURN=%04X:%04X  PROGRAM=%s\n", psp_seg, psp->env_seg, psp->int_22h.w.h, psp->int_22h.w.l, my_strupr(file));
                   1163:                strcat(tmp, buffer);
                   1164:                strcpy(buffer, tmp);
                   1165:                
                   1166:                check[psp_seg] = true;
                   1167:                psp_seg = psp->parent_psp;
                   1168:        }
                   1169: }
                   1170: 
                   1171: UINT32 debugger_get_val(const char *str)
                   1172: {
                   1173:        char tmp[1024];
                   1174:        
                   1175:        if(str == NULL || strlen(str) == 0) {
                   1176:                return(0);
                   1177:        }
                   1178:        strcpy(tmp, str);
                   1179:        
                   1180:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1181:                // ank
                   1182:                return(tmp[1] & 0xff);
                   1183:        } else if(tmp[0] == '%') {
                   1184:                // decimal
                   1185:                return(strtoul(tmp + 1, NULL, 10));
                   1186:        }
                   1187:        return(strtoul(tmp, NULL, 16));
                   1188: }
                   1189: 
                   1190: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1191: {
                   1192:        char tmp[1024], *s;
                   1193:        
                   1194:        if(str == NULL || strlen(str) == 0) {
                   1195:                return(val);
                   1196:        }
                   1197:        strcpy(tmp, str);
                   1198:        
                   1199:        if((s = strstr(tmp, ":")) != NULL) {
                   1200:                // 0000:0000
                   1201:                *s = '\0';
                   1202:                return(debugger_get_val(tmp));
                   1203:        }
                   1204:        return(val);
                   1205: }
                   1206: 
                   1207: UINT32 debugger_get_ofs(const char *str)
                   1208: {
                   1209:        char tmp[1024], *s;
                   1210:        
                   1211:        if(str == NULL || strlen(str) == 0) {
                   1212:                return(0);
                   1213:        }
                   1214:        strcpy(tmp, str);
                   1215:        
                   1216:        if((s = strstr(tmp, ":")) != NULL) {
                   1217:                // 0000:0000
                   1218:                return(debugger_get_val(s + 1));
                   1219:        }
                   1220:        return(debugger_get_val(tmp));
                   1221: }
                   1222: 
                   1223: void debugger_main()
                   1224: {
                   1225:        telnet_command("\033[20h"); // cr-lf
                   1226:        
                   1227:        force_suspend = true;
                   1228:        now_going = false;
                   1229:        now_debugging = true;
                   1230:        Sleep(100);
                   1231:        
1.1.1.54  root     1232:        if(!m_exit && !now_suspended) {
1.1.1.33  root     1233:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1234:                telnet_printf("waiting until cpu is suspended...\n");
                   1235:        }
1.1.1.54  root     1236:        while(!m_exit && !now_suspended) {
1.1.1.33  root     1237:                if(telnet_disconnected()) {
                   1238:                        break;
                   1239:                }
                   1240:                Sleep(10);
                   1241:        }
                   1242:        
                   1243:        char buffer[8192];
                   1244:        
                   1245:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1246:        debugger_process_info(buffer);
                   1247:        telnet_printf("%s", buffer);
                   1248:        debugger_regs_info(buffer);
                   1249:        telnet_printf("%s", buffer);
                   1250:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1251:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1252:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1253:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1254:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1255:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1256:        
                   1257:        #define MAX_COMMAND_LEN 64
                   1258:        
                   1259:        char command[MAX_COMMAND_LEN + 1];
                   1260:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1261:        
                   1262:        UINT32 data_seg = SREG(DS);
                   1263:        UINT32 data_ofs = 0;
                   1264:        UINT32 dasm_seg = SREG(CS);
                   1265:        UINT32 dasm_ofs = m_eip;
                   1266:        
1.1.1.54  root     1267:        while(!m_exit) {
1.1.1.33  root     1268:                telnet_printf("- ");
                   1269:                command[0] = '\0';
                   1270:                
                   1271:                if(fi_debugger != NULL) {
                   1272:                        while(command[0] == '\0') {
                   1273:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1274:                                        break;
                   1275:                                }
                   1276:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1277:                                        command[strlen(command) - 1] = '\0';
                   1278:                                }
                   1279:                        }
                   1280:                        if(command[0] != '\0') {
                   1281:                                telnet_command("%s\n", command);
                   1282:                        }
                   1283:                }
                   1284:                if(command[0] == '\0') {
                   1285:                        if(!telnet_gets(command, sizeof(command))) {
                   1286:                                break;
                   1287:                        }
                   1288:                }
                   1289:                if(command[0] == '\0') {
                   1290:                        strcpy(command, prev_command);
                   1291:                } else {
                   1292:                        strcpy(prev_command, command);
                   1293:                }
                   1294:                if(fp_debugger != NULL) {
                   1295:                        fprintf(fp_debugger, "%s\n", command);
                   1296:                }
                   1297:                
1.1.1.54  root     1298:                if(!m_exit && command[0] != 0) {
1.1.1.33  root     1299:                        char *params[32], *token = NULL;
                   1300:                        int num = 0;
                   1301:                        
                   1302:                        if((token = strtok(command, " ")) != NULL) {
                   1303:                                params[num++] = token;
                   1304:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1305:                                        params[num++] = token;
                   1306:                                }
                   1307:                        }
                   1308:                        if(stricmp(params[0], "D") == 0) {
                   1309:                                if(num <= 3) {
                   1310:                                        if(num >= 2) {
                   1311:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1312:                                                data_ofs = debugger_get_ofs(params[1]);
                   1313:                                        }
                   1314:                                        UINT32 end_seg = data_seg;
                   1315:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1316:                                        if(num == 3) {
                   1317:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1318:                                                end_ofs = debugger_get_ofs(params[2]);
                   1319:                                        }
                   1320:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1321:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1322: //                                     bool is_sjis = false;
1.1.1.33  root     1323:                                        
                   1324:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1325:                                                if((addr & 0x0f) == 0) {
                   1326:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1327:                                                                data_seg += 0x1000;
                   1328:                                                                data_ofs -= 0x10000;
                   1329:                                                        }
                   1330:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1331:                                                        memset(buffer, 0, sizeof(buffer));
                   1332:                                                }
                   1333:                                                if(addr < start_addr || addr > end_addr) {
                   1334:                                                        telnet_printf("   ");
                   1335:                                                        buffer[addr & 0x0f] = ' ';
                   1336:                                                } else {
                   1337:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1338:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1339: //                                                     if(is_sjis) {
1.1.1.33  root     1340: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1341: //                                                             is_sjis = false;
1.1.1.33  root     1342: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1343: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1344: //                                                             is_sjis = true;
1.1.1.33  root     1345: //                                                     } else
                   1346:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1347:                                                                buffer[addr & 0x0f] = data;
                   1348:                                                        } else {
                   1349:                                                                buffer[addr & 0x0f] = '.';
                   1350:                                                        }
                   1351:                                                }
                   1352:                                                if((addr & 0x0f) == 0x0f) {
                   1353:                                                        telnet_printf("  %s\n", buffer);
                   1354:                                                }
                   1355:                                        }
                   1356:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1357:                                                data_seg += 0x1000;
                   1358:                                                data_ofs -= 0x10000;
                   1359:                                        }
                   1360:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1361:                                } else {
                   1362:                                        telnet_printf("invalid parameter number\n");
                   1363:                                }
                   1364:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1365:                                if(num >= 3) {
                   1366:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1367:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1368:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1369:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1370:                                        }
                   1371:                                } else {
                   1372:                                        telnet_printf("invalid parameter number\n");
                   1373:                                }
                   1374:                        } else if(stricmp(params[0], "EW") == 0) {
                   1375:                                if(num >= 3) {
                   1376:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1377:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1378:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1379:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1380:                                        }
                   1381:                                } else {
                   1382:                                        telnet_printf("invalid parameter number\n");
                   1383:                                }
                   1384:                        } else if(stricmp(params[0], "ED") == 0) {
                   1385:                                if(num >= 3) {
                   1386:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1387:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1388:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1389:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1390:                                        }
                   1391:                                } else {
                   1392:                                        telnet_printf("invalid parameter number\n");
                   1393:                                }
                   1394:                        } else if(stricmp(params[0], "EA") == 0) {
                   1395:                                if(num >= 3) {
                   1396:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1397:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1398:                                        strcpy(buffer, prev_command);
                   1399:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1400:                                                int len = strlen(token);
                   1401:                                                for(int i = 0; i < len; i++) {
                   1402:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1403:                                                }
                   1404:                                        } else {
                   1405:                                                telnet_printf("invalid parameter\n");
                   1406:                                        }
                   1407:                                } else {
                   1408:                                        telnet_printf("invalid parameter number\n");
                   1409:                                }
                   1410:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1411:                                if(num == 2) {
                   1412:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1413:                                } else {
                   1414:                                        telnet_printf("invalid parameter number\n");
                   1415:                                }
                   1416:                        } else if(stricmp(params[0], "IW") == 0) {
                   1417:                                if(num == 2) {
                   1418:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1419:                                } else {
                   1420:                                        telnet_printf("invalid parameter number\n");
                   1421:                                }
                   1422:                        } else if(stricmp(params[0], "ID") == 0) {
                   1423:                                if(num == 2) {
                   1424:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1425:                                } else {
                   1426:                                        telnet_printf("invalid parameter number\n");
                   1427:                                }
                   1428:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1429:                                if(num == 3) {
                   1430:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1431:                                } else {
                   1432:                                        telnet_printf("invalid parameter number\n");
                   1433:                                }
                   1434:                        } else if(stricmp(params[0], "OW") == 0) {
                   1435:                                if(num == 3) {
                   1436:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1437:                                } else {
                   1438:                                        telnet_printf("invalid parameter number\n");
                   1439:                                }
                   1440:                        } else if(stricmp(params[0], "OD") == 0) {
                   1441:                                if(num == 3) {
                   1442:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1443:                                } else {
                   1444:                                        telnet_printf("invalid parameter number\n");
                   1445:                                }
                   1446:                        } else if(stricmp(params[0], "R") == 0) {
                   1447:                                if(num == 1) {
                   1448:                                        debugger_regs_info(buffer);
                   1449:                                        telnet_printf("%s", buffer);
                   1450:                                } else if(num == 3) {
                   1451: #if defined(HAS_I386)
                   1452:                                        if(stricmp(params[1], "EAX") == 0) {
                   1453:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1454:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1455:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1456:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1457:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1458:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1459:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1460:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1461:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1462:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1463:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1464:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1465:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1466:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1467:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1468:                                        } else
                   1469: #endif
                   1470:                                        if(stricmp(params[1], "AX") == 0) {
                   1471:                                                REG16(AX) = debugger_get_val(params[2]);
                   1472:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1473:                                                REG16(BX) = debugger_get_val(params[2]);
                   1474:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1475:                                                REG16(CX) = debugger_get_val(params[2]);
                   1476:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1477:                                                REG16(DX) = debugger_get_val(params[2]);
                   1478:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1479:                                                REG16(SP) = debugger_get_val(params[2]);
                   1480:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1481:                                                REG16(BP) = debugger_get_val(params[2]);
                   1482:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1483:                                                REG16(SI) = debugger_get_val(params[2]);
                   1484:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1485:                                                REG16(DI) = debugger_get_val(params[2]);
                   1486:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1487: #if defined(HAS_I386)
                   1488:                                                if(m_operand_size) {
                   1489:                                                        m_eip = debugger_get_val(params[2]);
                   1490:                                                } else {
                   1491:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1492:                                                }
                   1493:                                                CHANGE_PC(m_eip);
                   1494: #else
                   1495:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1496:                                                CHANGE_PC(m_pc);
                   1497: #endif
                   1498:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1499:                                                REG8(AL) = debugger_get_val(params[2]);
                   1500:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1501:                                                REG8(AH) = debugger_get_val(params[2]);
                   1502:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1503:                                                REG8(BL) = debugger_get_val(params[2]);
                   1504:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1505:                                                REG8(BH) = debugger_get_val(params[2]);
                   1506:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1507:                                                REG8(CL) = debugger_get_val(params[2]);
                   1508:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1509:                                                REG8(CH) = debugger_get_val(params[2]);
                   1510:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1511:                                                REG8(DL) = debugger_get_val(params[2]);
                   1512:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1513:                                                REG8(DH) = debugger_get_val(params[2]);
                   1514:                                        } else {
                   1515:                                                telnet_printf("unknown register %s\n", params[1]);
                   1516:                                        }
                   1517:                                } else {
                   1518:                                        telnet_printf("invalid parameter number\n");
                   1519:                                }
1.1.1.60  root     1520:                        } else if(stricmp(params[0], "S") == 0) {
1.1.1.33  root     1521:                                if(num >= 4) {
                   1522:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1523:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1524:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1525:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1526:                                        UINT8 list[32];
                   1527:                                        
                   1528:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1529:                                                list[j] = debugger_get_val(params[i]);
                   1530:                                        }
                   1531:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1532:                                                bool found = true;
                   1533:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1534:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1535:                                                                found = false;
                   1536:                                                                break;
                   1537:                                                        }
                   1538:                                                }
                   1539:                                                if(found) {
                   1540:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1541:                                                }
                   1542:                                                if((cur_ofs += 1) > 0xffff) {
                   1543:                                                        cur_seg += 0x1000;
                   1544:                                                        cur_ofs -= 0x10000;
                   1545:                                                }
                   1546:                                        }
                   1547:                                } else {
                   1548:                                        telnet_printf("invalid parameter number\n");
                   1549:                                }
                   1550:                        } else if(stricmp(params[0], "U") == 0) {
                   1551:                                if(num <= 3) {
                   1552:                                        if(num >= 2) {
                   1553:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1554:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1555:                                        }
                   1556:                                        if(num == 3) {
                   1557:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1558:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1559:                                                
                   1560:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1561:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1562:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1563:                                                        for(int i = 0; i < len; i++) {
                   1564:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1565:                                                        }
                   1566:                                                        for(int i = len; i < 8; i++) {
                   1567:                                                                telnet_printf("  ");
                   1568:                                                        }
                   1569:                                                        telnet_printf("  %s\n", buffer);
                   1570:                                                        if((dasm_ofs += len) > 0xffff) {
                   1571:                                                                dasm_seg += 0x1000;
                   1572:                                                                dasm_ofs -= 0x10000;
                   1573:                                                        }
                   1574:                                                }
                   1575:                                        } else {
                   1576:                                                for(int i = 0; i < 16; i++) {
                   1577:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1578:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1579:                                                        for(int i = 0; i < len; i++) {
                   1580:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1581:                                                        }
                   1582:                                                        for(int i = len; i < 8; i++) {
                   1583:                                                                telnet_printf("  ");
                   1584:                                                        }
                   1585:                                                        telnet_printf("  %s\n", buffer);
                   1586:                                                        if((dasm_ofs += len) > 0xffff) {
                   1587:                                                                dasm_seg += 0x1000;
                   1588:                                                                dasm_ofs -= 0x10000;
                   1589:                                                        }
                   1590:                                                }
                   1591:                                        }
                   1592:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1593:                                } else {
                   1594:                                        telnet_printf("invalid parameter number\n");
                   1595:                                }
                   1596:                        } else if(stricmp(params[0], "H") == 0) {
                   1597:                                if(num == 3) {
                   1598:                                        UINT32 l = debugger_get_val(params[1]);
                   1599:                                        UINT32 r = debugger_get_val(params[2]);
                   1600:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1601:                                } else {
                   1602:                                        telnet_printf("invalid parameter number\n");
                   1603:                                }
                   1604:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1605:                                break_point_t *break_point_ptr;
                   1606:                                #define GET_BREAK_POINT_PTR() { \
1.1.1.58  root     1607:                                        if(params[0][0] == 'R' || params[0][0] == 'r') { \
1.1.1.33  root     1608:                                                break_point_ptr = &rd_break_point; \
1.1.1.58  root     1609:                                        } else if(params[0][0] == 'W' || params[0][0] == 'w') { \
1.1.1.33  root     1610:                                                break_point_ptr = &wr_break_point; \
1.1.1.58  root     1611:                                        } else if(params[0][0] == 'I' || params[0][0] == 'i') { \
1.1.1.33  root     1612:                                                break_point_ptr = &in_break_point; \
1.1.1.58  root     1613:                                        } else if(params[0][0] == 'O' || params[0][0] == 'o') { \
1.1.1.33  root     1614:                                                break_point_ptr = &out_break_point; \
                   1615:                                        } else { \
                   1616:                                                break_point_ptr = &break_point; \
                   1617:                                        } \
                   1618:                                }
                   1619:                                GET_BREAK_POINT_PTR();
                   1620:                                if(num == 2) {
1.1.1.58  root     1621:                                        UINT32 seg = 0;
                   1622:                                        if(params[0][0] == 'R' || params[0][0] == 'r' || params[0][0] == 'W' || params[0][0] == 'w') {
                   1623:                                                seg = debugger_get_seg(params[1], data_seg);
                   1624:                                        } else {
                   1625:                                                seg = debugger_get_seg(params[1], SREG(CS));
                   1626:                                        }
1.1.1.33  root     1627:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1628:                                        bool found = false;
                   1629:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1630:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1631:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1632:                                                        break_point_ptr->table[i].seg = seg;
                   1633:                                                        break_point_ptr->table[i].ofs = ofs;
                   1634:                                                        break_point_ptr->table[i].status = 1;
                   1635:                                                        found = true;
                   1636:                                                }
                   1637:                                        }
                   1638:                                        if(!found) {
                   1639:                                                telnet_printf("too many break points\n");
                   1640:                                        }
                   1641:                                } else {
                   1642:                                        telnet_printf("invalid parameter number\n");
                   1643:                                }
                   1644:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1645:                                break_point_t *break_point_ptr;
                   1646:                                GET_BREAK_POINT_PTR();
                   1647:                                if(num == 2) {
                   1648:                                        UINT32 addr = debugger_get_val(params[1]);
                   1649:                                        bool found = false;
                   1650:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1651:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1652:                                                        break_point_ptr->table[i].addr = addr;
                   1653:                                                        break_point_ptr->table[i].status = 1;
                   1654:                                                        found = true;
                   1655:                                                }
                   1656:                                        }
                   1657:                                        if(!found) {
                   1658:                                                telnet_printf("too many break points\n");
                   1659:                                        }
                   1660:                                } else {
                   1661:                                        telnet_printf("invalid parameter number\n");
                   1662:                                }
                   1663:                        } else if(stricmp(params[0], "BC") == 0 || stricmp(params[0], "RBC") == 0 || stricmp(params[0], "WBC") == 0 || stricmp(params[0], "IBC") == 0 || stricmp(params[0], "OBC") == 0) {
                   1664:                                break_point_t *break_point_ptr;
                   1665:                                GET_BREAK_POINT_PTR();
                   1666:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1667:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1668:                                } else if(num >= 2) {
                   1669:                                        for(int i = 1; i < num; i++) {
                   1670:                                                int index = debugger_get_val(params[i]);
                   1671:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1672:                                                        telnet_printf("invalid index %x\n", index);
                   1673:                                                } else {
                   1674:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1675:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1676:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1677:                                                        break_point_ptr->table[index - 1].status = 0;
                   1678:                                                }
                   1679:                                        }
                   1680:                                } else {
                   1681:                                        telnet_printf("invalid parameter number\n");
                   1682:                                }
                   1683:                        } else if(stricmp(params[0], "BD") == 0 || stricmp(params[0], "RBD") == 0 || stricmp(params[0], "WBD") == 0 || stricmp(params[0], "IBD") == 0 || stricmp(params[0], "OBD") == 0 ||
                   1684:                                  stricmp(params[0], "BE") == 0 || stricmp(params[0], "RBE") == 0 || stricmp(params[0], "WBE") == 0 || stricmp(params[0], "IBE") == 0 || stricmp(params[0], "OBE") == 0) {
                   1685:                                break_point_t *break_point_ptr;
                   1686:                                GET_BREAK_POINT_PTR();
                   1687:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1688:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1689:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1690:                                                if(break_point_ptr->table[i].status != 0) {
                   1691:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1692:                                                }
                   1693:                                        }
                   1694:                                } else if(num >= 2) {
                   1695:                                        for(int i = 1; i < num; i++) {
                   1696:                                                int index = debugger_get_val(params[i]);
                   1697:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1698:                                                        telnet_printf("invalid index %x\n", index);
                   1699:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1700:                                                        telnet_printf("break point %x is null\n", index);
                   1701:                                                } else {
                   1702:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1703:                                                }
                   1704:                                        }
                   1705:                                } else {
                   1706:                                        telnet_printf("invalid parameter number\n");
                   1707:                                }
                   1708:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1709:                                break_point_t *break_point_ptr;
                   1710:                                GET_BREAK_POINT_PTR();
                   1711:                                if(num == 1) {
                   1712:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1713:                                                if(break_point_ptr->table[i].status) {
                   1714:                                                        telnet_printf("%d %c %04X:%04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].seg, break_point_ptr->table[i].ofs);
                   1715:                                                }
                   1716:                                        }
                   1717:                                } else {
                   1718:                                        telnet_printf("invalid parameter number\n");
                   1719:                                }
                   1720:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1721:                                break_point_t *break_point_ptr;
                   1722:                                GET_BREAK_POINT_PTR();
                   1723:                                if(num == 1) {
                   1724:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1725:                                                if(break_point_ptr->table[i].status) {
                   1726:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1727:                                                }
                   1728:                                        }
                   1729:                                } else {
                   1730:                                        telnet_printf("invalid parameter number\n");
                   1731:                                }
                   1732:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1733:                                if(num >= 2 && num <= 4) {
                   1734:                                        int int_num = debugger_get_val(params[1]);
                   1735:                                        UINT8 ah = 0, ah_registered = 0;
                   1736:                                        UINT8 al = 0, al_registered = 0;
                   1737:                                        if(num >= 3) {
                   1738:                                                ah = debugger_get_val(params[2]);
                   1739:                                                ah_registered = 1;
                   1740:                                        }
                   1741:                                        if(num == 4) {
                   1742:                                                al = debugger_get_val(params[3]);
                   1743:                                                al_registered = 1;
                   1744:                                        }
                   1745:                                        bool found = false;
                   1746:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1747:                                                if(int_break_point.table[i].status == 0 || (
                   1748:                                                   int_break_point.table[i].int_num == int_num &&
                   1749:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1750:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1751:                                                        int_break_point.table[i].int_num = int_num;
                   1752:                                                        int_break_point.table[i].ah = ah;
                   1753:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1754:                                                        int_break_point.table[i].al = al;
                   1755:                                                        int_break_point.table[i].al_registered = al_registered;
                   1756:                                                        int_break_point.table[i].status = 1;
                   1757:                                                        found = true;
                   1758:                                                }
                   1759:                                        }
                   1760:                                        if(!found) {
                   1761:                                                telnet_printf("too many break points\n");
                   1762:                                        }
                   1763:                                } else {
                   1764:                                        telnet_printf("invalid parameter number\n");
                   1765:                                }
                   1766:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1767:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1768:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1769:                                } else if(num >= 2) {
                   1770:                                        for(int i = 1; i < num; i++) {
                   1771:                                                int index = debugger_get_val(params[i]);
                   1772:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1773:                                                        telnet_printf("invalid index %x\n", index);
                   1774:                                                } else {
                   1775:                                                        int_break_point.table[index - 1].int_num = 0;
                   1776:                                                        int_break_point.table[index - 1].ah = 0;
                   1777:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1778:                                                        int_break_point.table[index - 1].al = 0;
                   1779:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1780:                                                        int_break_point.table[index - 1].status = 0;
                   1781:                                                }
                   1782:                                        }
                   1783:                                } else {
                   1784:                                        telnet_printf("invalid parameter number\n");
                   1785:                                }
                   1786:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1787:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1788:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1789:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1790:                                                if(int_break_point.table[i].status != 0) {
                   1791:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1792:                                                }
                   1793:                                        }
                   1794:                                } else if(num >= 2) {
                   1795:                                        for(int i = 1; i < num; i++) {
                   1796:                                                int index = debugger_get_val(params[i]);
                   1797:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1798:                                                        telnet_printf("invalid index %x\n", index);
                   1799:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1800:                                                        telnet_printf("break point %x is null\n", index);
                   1801:                                                } else {
                   1802:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1803:                                                }
                   1804:                                        }
                   1805:                                } else {
                   1806:                                        telnet_printf("invalid parameter number\n");
                   1807:                                }
                   1808:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1809:                                if(num == 1) {
                   1810:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1811:                                                if(int_break_point.table[i].status) {
                   1812:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1813:                                                        if(int_break_point.table[i].ah_registered) {
                   1814:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1815:                                                        }
                   1816:                                                        if(int_break_point.table[i].al_registered) {
                   1817:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1818:                                                        }
                   1819:                                                        telnet_printf("\n");
                   1820:                                                }
                   1821:                                        }
                   1822:                                } else {
                   1823:                                        telnet_printf("invalid parameter number\n");
                   1824:                                }
                   1825:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1826:                                if(num == 1 || num == 2) {
                   1827:                                        break_point_t break_point_stored;
                   1828:                                        bool break_points_stored = false;
                   1829:                                        
                   1830:                                        if(stricmp(params[0], "P") == 0) {
                   1831:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1832:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1833:                                                break_points_stored = true;
                   1834:                                                
                   1835:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1836:                                                break_point.table[0].status = 1;
                   1837:                                        } else if(num >= 2) {
                   1838:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1839:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1840:                                                break_points_stored = true;
                   1841:                                                
                   1842:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1843:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1844:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1845:                                                break_point.table[0].seg = seg;
                   1846:                                                break_point.table[0].ofs = ofs;
                   1847:                                                break_point.table[0].status = 1;
                   1848:                                        }
                   1849:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1850:                                        now_going = true;
                   1851:                                        now_suspended = false;
                   1852:                                        
                   1853:                                        telnet_command("\033[2l"); // key unlock
1.1.1.54  root     1854:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1855:                                                if(telnet_kbhit()) {
                   1856:                                                        break;
                   1857:                                                }
                   1858:                                                Sleep(10);
                   1859:                                        }
                   1860:                                        now_going = false;
                   1861:                                        telnet_command("\033[2h"); // key lock
                   1862:                                        
                   1863:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1864:                                                Sleep(100);
1.1.1.54  root     1865:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1866:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1867:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1868:                                                }
                   1869:                                        }
1.1.1.54  root     1870:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1871:                                                if(telnet_disconnected()) {
                   1872:                                                        break;
                   1873:                                                }
                   1874:                                                Sleep(10);
                   1875:                                        }
                   1876:                                        dasm_seg = SREG(CS);
                   1877:                                        dasm_ofs = m_eip;
                   1878:                                        
                   1879:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1880:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1881:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1882:                                        
                   1883:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1884:                                        debugger_regs_info(buffer);
                   1885:                                        telnet_printf("%s", buffer);
                   1886:                                        
                   1887:                                        if(break_point.hit) {
                   1888:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1889:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1890:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1891:                                                }
                   1892:                                        } else if(rd_break_point.hit) {
                   1893:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1894:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1895:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1896:                                                m_prev_cs, m_prev_eip);
                   1897:                                        } else if(wr_break_point.hit) {
                   1898:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1899:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1900:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1901:                                                m_prev_cs, m_prev_eip);
                   1902:                                        } else if(in_break_point.hit) {
                   1903:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1904:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1905:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1906:                                                m_prev_cs, m_prev_eip);
                   1907:                                        } else if(out_break_point.hit) {
                   1908:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1909:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1910:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1911:                                                m_prev_cs, m_prev_eip);
                   1912:                                        } else if(int_break_point.hit) {
                   1913:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1914:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1915:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1916:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1917:                                                }
                   1918:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1919:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1920:                                                }
                   1921:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1922:                                        } else {
                   1923:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1924:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1925:                                        }
                   1926:                                        if(break_points_stored) {
                   1927:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1928:                                        }
                   1929:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1930:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1931:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1932:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1933:                                } else {
                   1934:                                        telnet_printf("invalid parameter number\n");
                   1935:                                }
                   1936:                        } else if(stricmp(params[0], "T") == 0) {
                   1937:                                if(num == 1 || num == 2) {
                   1938:                                        int steps = 1;
                   1939:                                        if(num >= 2) {
                   1940:                                                steps = debugger_get_val(params[1]);
                   1941:                                        }
                   1942:                                        
                   1943:                                        telnet_command("\033[2l"); // key unlock
                   1944:                                        while(steps-- > 0) {
                   1945:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1946:                                                now_going = false;
                   1947:                                                now_suspended = false;
                   1948:                                                
1.1.1.54  root     1949:                                                while(!m_exit && !now_suspended) {
1.1.1.33  root     1950:                                                        if(telnet_disconnected()) {
                   1951:                                                                break;
                   1952:                                                        }
                   1953:                                                        Sleep(10);
                   1954:                                                }
                   1955:                                                dasm_seg = SREG(CS);
                   1956:                                                dasm_ofs = m_eip;
                   1957:                                                
                   1958:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1959:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1960:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1961:                                                
                   1962:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1963:                                                debugger_regs_info(buffer);
                   1964:                                                telnet_printf("%s", buffer);
                   1965:                                                
                   1966:                                                if(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit || telnet_kbhit()) {
                   1967:                                                        break;
                   1968:                                                }
                   1969:                                        }
                   1970:                                        telnet_command("\033[2h"); // key lock
                   1971:                                        
                   1972:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1973:                                                Sleep(100);
1.1.1.54  root     1974:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1975:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1976:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1977:                                                }
                   1978:                                        }
1.1.1.54  root     1979:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1980:                                                if(telnet_disconnected()) {
                   1981:                                                        break;
                   1982:                                                }
                   1983:                                                Sleep(10);
                   1984:                                        }
                   1985:                                        if(break_point.hit) {
                   1986:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1987:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1988:                                        } else if(rd_break_point.hit) {
                   1989:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1990:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1991:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1992:                                                m_prev_cs, m_prev_eip);
                   1993:                                        } else if(wr_break_point.hit) {
                   1994:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1995:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1996:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1997:                                                m_prev_cs, m_prev_eip);
                   1998:                                        } else if(in_break_point.hit) {
                   1999:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2000:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   2001:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   2002:                                                m_prev_cs, m_prev_eip);
                   2003:                                        } else if(out_break_point.hit) {
                   2004:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2005:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   2006:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   2007:                                                m_prev_cs, m_prev_eip);
                   2008:                                        } else if(int_break_point.hit) {
                   2009:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2010:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   2011:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   2012:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   2013:                                                }
                   2014:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   2015:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   2016:                                                }
                   2017:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   2018:                                        } else if(steps > 0) {
                   2019:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2020:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   2021:                                        }
                   2022:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2023:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   2024:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   2025:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2026:                                } else {
                   2027:                                        telnet_printf("invalid parameter number\n");
                   2028:                                }
                   2029:                        } else if(stricmp(params[0], "Q") == 0) {
                   2030:                                break;
                   2031:                        } else if(stricmp(params[0], "X") == 0) {
                   2032:                                debugger_process_info(buffer);
                   2033:                                telnet_printf("%s", buffer);
                   2034:                        } else if(stricmp(params[0], ">") == 0) {
                   2035:                                if(num == 2) {
                   2036:                                        if(fp_debugger != NULL) {
                   2037:                                                fclose(fp_debugger);
                   2038:                                                fp_debugger = NULL;
                   2039:                                        }
                   2040:                                        fp_debugger = fopen(params[1], "w");
                   2041:                                } else {
                   2042:                                        telnet_printf("invalid parameter number\n");
                   2043:                                }
                   2044:                        } else if(stricmp(params[0], "<") == 0) {
                   2045:                                if(num == 2) {
                   2046:                                        if(fi_debugger != NULL) {
                   2047:                                                fclose(fi_debugger);
                   2048:                                                fi_debugger = NULL;
                   2049:                                        }
                   2050:                                        fi_debugger = fopen(params[1], "r");
                   2051:                                } else {
                   2052:                                        telnet_printf("invalid parameter number\n");
                   2053:                                }
                   2054:                        } else if(stricmp(params[0], "?") == 0) {
                   2055:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   2056:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   2057:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   2058:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   2059:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   2060:                                
                   2061:                                telnet_printf("R - show registers\n");
                   2062:                                telnet_printf("R <reg> <value> - edit register\n");
                   2063:                                telnet_printf("S <start> <end> <list> - search\n");
                   2064:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   2065:                                
                   2066:                                telnet_printf("H <value> <value> - hexadd\n");
                   2067:                                
                   2068:                                telnet_printf("BP <address> - set breakpoint\n");
                   2069:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   2070:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   2071:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   2072:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   2073:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2074:                                
                   2075:                                telnet_printf("G - go (press enter key to break)\n");
                   2076:                                telnet_printf("G <address> - go and break at address\n");
                   2077:                                telnet_printf("P - trace one opcode (step over)\n");
                   2078:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2079:                                telnet_printf("Q - quit\n");
                   2080:                                telnet_printf("X - show dos process info\n");
                   2081:                                
                   2082:                                telnet_printf("> <filename> - output logfile\n");
                   2083:                                telnet_printf("< <filename> - input commands from file\n");
                   2084:                                
                   2085:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2086:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2087:                        } else {
                   2088:                                telnet_printf("unknown command %s\n", params[0]);
                   2089:                        }
                   2090:                }
                   2091:        }
                   2092:        if(fp_debugger != NULL) {
                   2093:                fclose(fp_debugger);
                   2094:                fp_debugger = NULL;
                   2095:        }
                   2096:        if(fi_debugger != NULL) {
                   2097:                fclose(fi_debugger);
                   2098:                fi_debugger = NULL;
                   2099:        }
                   2100:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2101:        closesocket(cli_socket);
                   2102: }
                   2103: 
                   2104: const char *debugger_get_ttermpro_path()
                   2105: {
                   2106:        static char path[MAX_PATH] = {0};
                   2107:        
                   2108:        if(getenv("ProgramFiles")) {
                   2109:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2110:        }
                   2111:        return(path);
                   2112: }
                   2113: 
                   2114: const char *debugger_get_ttermpro_x86_path()
                   2115: {
                   2116:        static char path[MAX_PATH] = {0};
                   2117:        
                   2118:        if(getenv("ProgramFiles(x86)")) {
                   2119:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2120:        }
                   2121:        return(path);
                   2122: }
                   2123: 
                   2124: const char *debugger_get_putty_path()
                   2125: {
                   2126:        static char path[MAX_PATH] = {0};
                   2127:        
                   2128:        if(getenv("ProgramFiles")) {
                   2129:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2130:        }
                   2131:        return(path);
                   2132: }
                   2133: 
                   2134: const char *debugger_get_putty_x86_path()
                   2135: {
                   2136:        static char path[MAX_PATH] = {0};
                   2137:        
                   2138:        if(getenv("ProgramFiles(x86)")) {
                   2139:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2140:        }
                   2141:        return(path);
                   2142: }
                   2143: 
                   2144: const char *debugger_get_telnet_path()
                   2145: {
                   2146:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2147:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2148:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2149:        // and 64bit version of telnet.exe will be installed in System32.
                   2150:        static char path[MAX_PATH] = {0};
                   2151:        
                   2152:        if(getenv("windir") != NULL) {
                   2153:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2154:        }
                   2155:        return(path);
                   2156: }
                   2157: 
                   2158: DWORD WINAPI debugger_thread(LPVOID)
                   2159: {
                   2160:        WSADATA was_data;
                   2161:        struct sockaddr_in svr_addr;
                   2162:        struct sockaddr_in cli_addr;
                   2163:        int cli_addr_len = sizeof(cli_addr);
                   2164:        int port = 23;
                   2165:        int bind_stat = SOCKET_ERROR;
                   2166:        struct timeval timeout;
                   2167:        
                   2168:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2169:        
                   2170:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2171:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2172:                svr_addr.sin_family = AF_INET;
                   2173:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2174:                
1.1.1.54  root     2175:                while(!m_exit && port < 10000) {
1.1.1.33  root     2176:                        svr_addr.sin_port = htons(port);
                   2177:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2178:                                break;
                   2179:                        } else {
                   2180:                                port = (port == 23) ? 9000 : (port + 1);
                   2181:                        }
                   2182:                }
                   2183:                if(bind_stat == 0) {
                   2184:                        timeout.tv_sec = 1;
                   2185:                        timeout.tv_usec = 0;
1.1.1.45  root     2186:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2187:                        
                   2188:                        listen(svr_socket, 1);
                   2189:                        
                   2190:                        char command[MAX_PATH] = {0};
1.1.1.60  root     2191:                        STARTUPINFOA si;
1.1.1.33  root     2192:                        PROCESS_INFORMATION pi;
                   2193:                        
                   2194:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2195:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2196:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2197:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2198:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2199:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2200:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2201:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2202:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2203:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2204:                        }
                   2205:                        if(command[0] != '\0') {
1.1.1.60  root     2206:                                memset(&si, 0, sizeof(STARTUPINFOA));
1.1.1.33  root     2207:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
1.1.1.60  root     2208:                                CreateProcessA(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
1.1.1.33  root     2209:                        }
                   2210:                        
1.1.1.54  root     2211:                        while(!m_exit) {
1.1.1.33  root     2212:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2213:                                        u_long val = 1;
                   2214:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2215:                                        debugger_main();
                   2216:                                }
                   2217:                        }
                   2218:                }
                   2219:        }
                   2220:        WSACleanup();
                   2221:        return(0);
                   2222: }
                   2223: #endif
                   2224: 
                   2225: /* ----------------------------------------------------------------------------
1.1       root     2226:        main
                   2227: ---------------------------------------------------------------------------- */
                   2228: 
1.1.1.28  root     2229: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2230: {
                   2231:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2232:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2233: #ifdef USE_SERVICE_THREAD
                   2234:                        EnterCriticalSection(&key_buf_crit_sect);
                   2235: #endif
1.1.1.51  root     2236:                        pcbios_clear_key_buffer();
1.1.1.35  root     2237: #ifdef USE_SERVICE_THREAD
                   2238:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2239: #endif
1.1.1.33  root     2240:                }
                   2241: //             key_code = key_recv = 0;
1.1.1.28  root     2242:                return TRUE;
                   2243:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2244:                return TRUE;
                   2245:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2246:                // this program will be terminated abnormally, do minimum end process
                   2247:                exit_handler();
                   2248:                exit(1);
                   2249:        }
                   2250:        return FALSE;
                   2251: }
                   2252: 
                   2253: void exit_handler()
                   2254: {
                   2255:        if(temp_file_created) {
1.1.1.60  root     2256:                DeleteFileA(temp_file_path);
1.1.1.28  root     2257:                temp_file_created = false;
                   2258:        }
                   2259:        if(key_buf_char != NULL) {
                   2260:                key_buf_char->release();
                   2261:                delete key_buf_char;
                   2262:                key_buf_char = NULL;
                   2263:        }
                   2264:        if(key_buf_scan != NULL) {
                   2265:                key_buf_scan->release();
                   2266:                delete key_buf_scan;
                   2267:                key_buf_scan = NULL;
                   2268:        }
1.1.1.57  root     2269:        if(key_buf_data != NULL) {
                   2270:                key_buf_data->release();
                   2271:                delete key_buf_data;
                   2272:                key_buf_data = NULL;
                   2273:        }
1.1.1.32  root     2274: #ifdef SUPPORT_XMS
                   2275:        msdos_xms_release();
                   2276: #endif
1.1.1.28  root     2277:        hardware_release();
                   2278: }
                   2279: 
1.1.1.35  root     2280: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2281: DWORD WINAPI vram_thread(LPVOID)
                   2282: {
1.1.1.54  root     2283:        while(!m_exit) {
1.1.1.28  root     2284:                EnterCriticalSection(&vram_crit_sect);
                   2285:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2286:                        vram_flush_char();
                   2287:                }
                   2288:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2289:                        vram_flush_attr();
                   2290:                }
                   2291:                vram_last_length_char = vram_length_char;
                   2292:                vram_last_length_attr = vram_length_attr;
                   2293:                LeaveCriticalSection(&vram_crit_sect);
                   2294:                // this is about half the maximum keyboard repeat rate - any
                   2295:                // lower tends to be jerky, any higher misses updates
                   2296:                Sleep(15);
                   2297:        }
                   2298:        return 0;
                   2299: }
                   2300: #endif
                   2301: 
1.1.1.45  root     2302: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2303: {
                   2304:        UINT8 header[0x400];
                   2305:        
                   2306:        long position = ftell(fp);
                   2307:        fseek(fp, 0, SEEK_SET);
                   2308:        fread(header, sizeof(header), 1, fp);
                   2309:        fseek(fp, position, SEEK_SET);
                   2310:        
                   2311:        try {
                   2312:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2313:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2314:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2315:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2316:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2317:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2318:                
                   2319:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2320:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2321:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2322:                                return(sectionHeader->PointerToRawData);
                   2323:                        }
                   2324:                }
                   2325:        } catch(...) {
                   2326:        }
                   2327:        return(0);
                   2328: }
                   2329: 
1.1.1.10  root     2330: bool is_started_from_command_prompt()
                   2331: {
1.1.1.58  root     2332:        bool result = false;
1.1.1.60  root     2333:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.58  root     2334:        
1.1.1.18  root     2335:        if(hLibrary) {
                   2336:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2337:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2338:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
1.1.1.58  root     2339:                if(lpfnGetConsoleProcessList) { // Windows XP or later
1.1.1.18  root     2340:                        DWORD pl;
1.1.1.58  root     2341:                        result = (lpfnGetConsoleProcessList(&pl, 1) > 1);
1.1.1.18  root     2342:                        FreeLibrary(hLibrary);
1.1.1.58  root     2343:                        return(result);
1.1.1.18  root     2344:                }
                   2345:                FreeLibrary(hLibrary);
                   2346:        }
                   2347:        
                   2348:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2349:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2350:                DWORD dwParentProcessID = 0;
                   2351:                PROCESSENTRY32 pe32;
                   2352:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2353:                if(Process32First(hSnapshot, &pe32)) {
                   2354:                        do {
                   2355:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2356:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2357:                                        break;
                   2358:                                }
                   2359:                        } while(Process32Next(hSnapshot, &pe32));
                   2360:                }
                   2361:                CloseHandle(hSnapshot);
                   2362:                if(dwParentProcessID != 0) {
                   2363:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2364:                        if(hProcess != NULL) {
                   2365:                                HMODULE hMod;
                   2366:                                DWORD cbNeeded;
                   2367:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2368:                                        char module_name[MAX_PATH];
1.1.1.60  root     2369:                                        if(GetModuleBaseNameA(hProcess, hMod, module_name, sizeof(module_name))) {
1.1.1.58  root     2370:                                                result = (_strnicmp(module_name, "cmd.exe", 7) == 0);
1.1.1.18  root     2371:                                        }
                   2372:                                }
                   2373:                                CloseHandle(hProcess);
                   2374:                        }
                   2375:                }
                   2376:        }
1.1.1.58  root     2377:        return(result);
1.1.1.14  root     2378: }
                   2379: 
                   2380: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2381: {
1.1.1.60  root     2382:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.14  root     2383:        
1.1.1.60  root     2384:        if(hLibrary) {
                   2385:                typedef ULONGLONG (WINAPI* VerSetConditionMaskFunction)(ULONGLONG, DWORD, BYTE);
                   2386:                typedef BOOL(WINAPI* VerifyVersionInfoFunction)(LPOSVERSIONINFOEXA, DWORD, DWORDLONG);
                   2387:                
                   2388:                VerSetConditionMaskFunction lpfnVerSetConditionMask = reinterpret_cast<VerSetConditionMaskFunction>(::GetProcAddress(hLibrary, "VerSetConditionMask"));
                   2389:                VerifyVersionInfoFunction lpfnVerifyVersionInfo = reinterpret_cast<VerifyVersionInfoFunction>(::GetProcAddress(hLibrary, "VerifyVersionInfoA"));
                   2390:                
                   2391:                if(lpfnVerSetConditionMask && lpfnVerifyVersionInfo) { // Windows 2000 or later
                   2392:                        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
                   2393:                        OSVERSIONINFOEXA osvi;
                   2394:                        DWORDLONG dwlConditionMask = 0;
                   2395:                        int op = VER_GREATER_EQUAL;
                   2396:                        
                   2397:                        // Initialize the OSVERSIONINFOEXA structure.
                   2398:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXA));
                   2399:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
                   2400:                        osvi.dwMajorVersion = dwMajorVersion;
                   2401:                        osvi.dwMinorVersion = dwMinorVersion;
                   2402:                        osvi.wServicePackMajor = wServicePackMajor;
                   2403:                        osvi.wServicePackMinor = wServicePackMinor;
                   2404:                        
                   2405:                         // Initialize the condition mask.
                   2406:                        #define MY_VER_SET_CONDITION(_m_,_t_,_c_) ((_m_)=lpfnVerSetConditionMask((_m_),(_t_),(_c_)))
                   2407:                        
                   2408:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2409:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2410:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2411:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2412:                        
                   2413:                        // Perform the test.
                   2414:                        BOOL result = lpfnVerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2415:                        FreeLibrary(hLibrary);
                   2416:                        return(result);
                   2417:                }
                   2418:                FreeLibrary(hLibrary);
                   2419:        }
                   2420:        
                   2421:        OSVERSIONINFOA osvi;
                   2422:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
                   2423:        
                   2424:        if(GetVersionExA((LPOSVERSIONINFOA)&osvi)) {
                   2425:                if(osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) {
                   2426:                        return(false);
                   2427:                } else if(osvi.dwMajorVersion > dwMajorVersion) {
                   2428:                        return(true);
                   2429:                } else if(osvi.dwMajorVersion < dwMajorVersion) {
                   2430:                        return(false);
                   2431:                } else if(osvi.dwMinorVersion > dwMinorVersion) {
                   2432:                        return(true);
                   2433:                } else if(osvi.dwMinorVersion < dwMinorVersion) {
                   2434:                        return(false);
                   2435:                }
                   2436:                // FIXME: check wServicePackMajor and wServicePackMinor :-(
                   2437:                return(true);
                   2438:        }
                   2439:        return(false);
1.1.1.14  root     2440: }
                   2441: 
1.1.1.61! root     2442: HWND get_console_window_handle()
1.1.1.58  root     2443: {
1.1.1.61! root     2444:        static HWND hwndFound = 0;
        !          2445:        
        !          2446:        if(hwndFound == 0) {
        !          2447:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
        !          2448:                char pszNewWindowTitle[1024];
        !          2449:                char pszOldWindowTitle[1024];
        !          2450:                
        !          2451:                GetConsoleTitleA(pszOldWindowTitle, 1024);
        !          2452:                wsprintfA(pszNewWindowTitle, "%d/%d", GetTickCount(), GetCurrentProcessId());
        !          2453:                SetConsoleTitleA(pszNewWindowTitle);
        !          2454:                Sleep(100);
        !          2455:                hwndFound = FindWindowA(NULL, pszNewWindowTitle);
        !          2456:                SetConsoleTitleA(pszOldWindowTitle);
        !          2457:        }
        !          2458:        return hwndFound;
        !          2459: }
        !          2460: 
        !          2461: HDC get_console_window_device_context()
        !          2462: {
        !          2463:        return GetDC(get_console_window_handle());
        !          2464: }
        !          2465: 
        !          2466: bool get_console_font_size(int *width, int *height)
        !          2467: {
        !          2468:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.58  root     2469:        bool result = false;
1.1.1.60  root     2470:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.58  root     2471:        
                   2472:        if(hLibrary) {
                   2473:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
                   2474:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
                   2475:                if(lpfnGetCurrentConsoleFont) { // Windows XP or later
                   2476:                        CONSOLE_FONT_INFO fi;
                   2477:                        if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0) {
                   2478:                                *width  = fi.dwFontSize.X;
                   2479:                                *height = fi.dwFontSize.Y;
                   2480:                                result = true;
                   2481:                        }
                   2482:                }
                   2483:                FreeLibrary(hLibrary);
                   2484:        }
                   2485:        return(result);
                   2486: }
                   2487: 
1.1.1.61! root     2488: bool set_console_font_size(int width, int height)
1.1.1.56  root     2489: {
                   2490:        // http://d.hatena.ne.jp/aharisu/20090427/1240852598
1.1.1.61! root     2491:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.56  root     2492:        bool result = false;
1.1.1.60  root     2493:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.56  root     2494:        
                   2495:        if(hLibrary) {
                   2496:                typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
                   2497:                typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
1.1.1.60  root     2498:                typedef COORD (WINAPI* GetConsoleFontSizeFunction)(HANDLE, DWORD);
1.1.1.56  root     2499:                typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
                   2500:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
1.1.1.61! root     2501:                typedef BOOL (WINAPI* GetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
        !          2502:                typedef BOOL (WINAPI* SetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
1.1.1.56  root     2503:                
                   2504:                GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
                   2505:                GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
1.1.1.60  root     2506:                GetConsoleFontSizeFunction lpfnGetConsoleFontSize = reinterpret_cast<GetConsoleFontSizeFunction>(::GetProcAddress(hLibrary, "GetConsoleFontSize"));
1.1.1.56  root     2507:                SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
                   2508:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
1.1.1.61! root     2509:                GetCurrentConsoleFontExFunction lpfnGetCurrentConsoleFontEx = reinterpret_cast<GetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFontEx"));
        !          2510:                SetCurrentConsoleFontExFunction lpfnSetCurrentConsoleFontEx = reinterpret_cast<SetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "SetCurrentConsoleFontEx"));
1.1.1.56  root     2511:                
1.1.1.61! root     2512:                if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont && lpfnGetCurrentConsoleFont) { // Windows XP or later
1.1.1.56  root     2513:                        DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
1.1.1.61! root     2514:                        if(dwFontNum) {
        !          2515:                                CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
        !          2516:                                lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
        !          2517:                                for(int i = 0; i < dwFontNum; i++) {
        !          2518:                                        fonts[i].dwFontSize = lpfnGetConsoleFontSize(hStdout, fonts[i].nFont);
        !          2519:                                        if(fonts[i].dwFontSize.X == width && fonts[i].dwFontSize.Y == height) {
        !          2520:                                                lpfnSetConsoleFont(hStdout, fonts[i].nFont);
        !          2521:                                                CONSOLE_FONT_INFO fi;
        !          2522:                                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
        !          2523:                                                        if(fi.dwFontSize.X == width && fi.dwFontSize.Y == height) {
1.1.1.58  root     2524:                                                                result = true;
1.1.1.61! root     2525:                                                                break;
1.1.1.58  root     2526:                                                        }
                   2527:                                                }
1.1.1.61! root     2528:                                        }
        !          2529:                                }
        !          2530:                                free(fonts);
        !          2531:                        } else if(lpfnGetCurrentConsoleFontEx && lpfnSetCurrentConsoleFontEx) {
        !          2532:                                // for Windows10 enhanced command prompt
        !          2533:                                CONSOLE_FONT_INFOEX fi_old, fi_new;
        !          2534:                                fi_old.cbSize = sizeof(CONSOLE_FONT_INFOEX);
        !          2535:                                if(lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_old)) {
        !          2536:                                        fi_new = fi_old;
        !          2537:                                        fi_new.dwFontSize.X = width;
        !          2538:                                        fi_new.dwFontSize.Y = height;
        !          2539:                                        if(lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_new)) {
        !          2540:                                                lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_new);
        !          2541:                                                if(fi_new.dwFontSize.X == width && fi_new.dwFontSize.Y == height) {
        !          2542:                                                        result = true;
        !          2543:                                                } else {
        !          2544:                                                        lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_old);
1.1.1.58  root     2545:                                                }
                   2546:                                        }
1.1.1.57  root     2547:                                }
1.1.1.56  root     2548:                        }
                   2549:                }
                   2550:                FreeLibrary(hLibrary);
                   2551:        }
                   2552:        return(result);
                   2553: }
                   2554: 
1.1.1.59  root     2555: bool is_cursor_blink_off()
                   2556: {
                   2557:        static int result = -1;
                   2558:        HKEY hKey;
                   2559:        char chData[64];
                   2560:        DWORD dwSize = sizeof(chData);
                   2561:        
                   2562:        if(result == -1) {
                   2563:                result = 0;
1.1.1.60  root     2564:                if(RegOpenKeyExA(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   2565:                        if(RegQueryValueExA(hKey, "CursorBlinkRate", NULL, NULL, (LPBYTE)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.59  root     2566:                                if(strncmp(chData, "-1", 2) == 0) {
                   2567:                                        result = 1;
                   2568:                                }
                   2569:                        }
                   2570:                        RegCloseKey(hKey);
                   2571:                }
                   2572:        }
                   2573:        return(result != 0);
                   2574: }
                   2575: 
1.1.1.27  root     2576: void get_sio_port_numbers()
                   2577: {
                   2578:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2579:        HDEVINFO hDevInfo = 0;
                   2580:        HKEY hKey = 0;
1.1.1.60  root     2581:        if((hDevInfo = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
1.1.1.27  root     2582:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2583:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2584:                                char chData[256];
                   2585:                                DWORD dwType = 0;
                   2586:                                DWORD dwSize = sizeof(chData);
                   2587:                                int port_number = 0;
                   2588:                                
1.1.1.60  root     2589:                                if(RegQueryValueExA(hKey, "PortName", NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.27  root     2590:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2591:                                                port_number = atoi(chData + 3);
                   2592:                                        }
                   2593:                                }
                   2594:                                RegCloseKey(hKey);
                   2595:                                
1.1.1.29  root     2596:                                if(sio_port_number[0] == port_number || sio_port_number[1] == port_number || sio_port_number[2] == port_number || sio_port_number[3] == port_number) {
1.1.1.27  root     2597:                                        continue;
                   2598:                                }
                   2599:                                if(sio_port_number[0] == 0) {
                   2600:                                        sio_port_number[0] = port_number;
                   2601:                                } else if(sio_port_number[1] == 0) {
                   2602:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2603:                                } else if(sio_port_number[2] == 0) {
                   2604:                                        sio_port_number[2] = port_number;
                   2605:                                } else if(sio_port_number[3] == 0) {
                   2606:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2607:                                }
1.1.1.29  root     2608:                                if(sio_port_number[0] != 0 && sio_port_number[1] != 0 && sio_port_number[2] != 0 && sio_port_number[3] != 0) {
1.1.1.27  root     2609:                                        break;
                   2610:                                }
                   2611:                        }
                   2612:                }
                   2613:        }
                   2614: }
                   2615: 
1.1.1.28  root     2616: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2617: 
1.1       root     2618: int main(int argc, char *argv[], char *envp[])
                   2619: {
1.1.1.9   root     2620:        int arg_offset = 0;
                   2621:        int standard_env = 0;
1.1.1.14  root     2622:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2623:        bool get_console_info_success = false;
1.1.1.56  root     2624:        bool get_console_font_success = false;
1.1.1.28  root     2625:        bool screen_size_changed = false;
                   2626:        
1.1.1.60  root     2627:        char path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2628:        GetModuleFileNameA(NULL, path, MAX_PATH);
                   2629:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     2630:        
1.1.1.27  root     2631:        char dummy_argv_0[] = "msdos.exe";
                   2632:        char dummy_argv_1[MAX_PATH];
                   2633:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2634:        char new_exec_file[MAX_PATH];
                   2635:        bool convert_cmd_file = false;
1.1.1.28  root     2636:        unsigned int code_page = 0;
1.1.1.27  root     2637:        
                   2638:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2639:                // check if command file is embedded to this execution file
                   2640:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2641:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2642:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2643:                if(offset != 0) {
1.1.1.30  root     2644:                        UINT8 buffer[16];
1.1.1.28  root     2645:                        fseek(fp, offset, SEEK_SET);
                   2646:                        fread(buffer, sizeof(buffer), 1, fp);
                   2647:                        
                   2648:                        // restore flags
                   2649:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2650:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2651:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2652:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2653:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2654:                        if((buffer[0] & 0x20) != 0) {
                   2655:                                get_sio_port_numbers();
                   2656:                        }
                   2657:                        if((buffer[0] & 0x40) != 0) {
                   2658:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2659:                                support_ems = true;
1.1.1.30  root     2660:                        }
1.1.1.27  root     2661: #ifdef SUPPORT_XMS
1.1.1.30  root     2662:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2663:                                support_xms = true;
                   2664:                        }
1.1.1.30  root     2665: #endif
1.1.1.28  root     2666:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2667:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2668:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2669:                        }
                   2670:                        if(buffer[5] != 0) {
1.1.1.30  root     2671:                                dos_major_version = buffer[5];
                   2672:                                dos_minor_version = buffer[6];
                   2673:                        }
                   2674:                        if(buffer[7] != 0) {
                   2675:                                win_major_version = buffer[7];
                   2676:                                win_minor_version = buffer[8];
1.1.1.28  root     2677:                        }
1.1.1.30  root     2678:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2679:                                SetConsoleCP(code_page);
                   2680:                                SetConsoleOutputCP(code_page);
                   2681:                        }
1.1.1.30  root     2682:                        int name_len = buffer[11];
                   2683:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2684:                        
                   2685:                        // restore command file name
                   2686:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2687:                        fread(dummy_argv_1, name_len, 1, fp);
                   2688:                        
                   2689:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2690:                                // if original command file exists, create a temporary file name
1.1.1.60  root     2691:                                if(GetTempFileNameA(".", "DOS", 0, dummy_argv_1) != 0) {
1.1.1.28  root     2692:                                        // create a temporary command file in the current director
1.1.1.60  root     2693:                                        DeleteFileA(dummy_argv_1);
1.1.1.27  root     2694:                                } else {
1.1.1.28  root     2695:                                        // create a temporary command file in the temporary folder
1.1.1.60  root     2696:                                        GetTempPathA(MAX_PATH, path);
                   2697:                                        if(GetTempFileNameA(path, "DOS", 0, dummy_argv_1) != 0) {
                   2698:                                                DeleteFileA(dummy_argv_1);
1.1.1.28  root     2699:                                        } else {
                   2700:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2701:                                        }
1.1.1.27  root     2702:                                }
1.1.1.28  root     2703:                                // check the command file type
                   2704:                                fread(buffer, 2, 1, fp);
                   2705:                                fseek(fp, -2, SEEK_CUR);
                   2706:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2707:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2708:                                } else {
                   2709:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2710:                                }
                   2711:                        }
1.1.1.28  root     2712:                        
                   2713:                        // restore command file
                   2714:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2715:                        for(int i = 0; i < file_len; i++) {
                   2716:                                fputc(fgetc(fp), fo);
                   2717:                        }
                   2718:                        fclose(fo);
                   2719:                        
1.1.1.60  root     2720:                        GetFullPathNameA(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
1.1.1.28  root     2721:                        temp_file_created = true;
1.1.1.60  root     2722:                        SetFileAttributesA(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
1.1.1.28  root     2723:                        
                   2724:                        // adjust argc/argv
                   2725:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2726:                                dummy_argv[i + 1] = argv[i];
                   2727:                        }
                   2728:                        argc++;
                   2729:                        argv = dummy_argv;
1.1.1.27  root     2730:                }
                   2731:                fclose(fp);
                   2732:        }
1.1.1.9   root     2733:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2734:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2735:                        stay_busy = true;
                   2736:                        arg_offset++;
1.1.1.27  root     2737:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2738:                        if(argv[i][2] != '\0') {
                   2739:                                strcpy(new_exec_file, &argv[i][2]);
                   2740:                        } else {
                   2741:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2742:                        }
                   2743:                        convert_cmd_file = true;
                   2744:                        arg_offset++;
1.1.1.28  root     2745:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2746:                        if(IS_NUMERIC(argv[i][2])) {
                   2747:                                code_page = atoi(&argv[i][2]);
                   2748:                        } else {
                   2749:                                code_page = GetConsoleCP();
                   2750:                        }
                   2751:                        arg_offset++;
1.1.1.25  root     2752:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2753:                        no_windows = true;
                   2754:                        arg_offset++;
                   2755:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2756:                        standard_env = 1;
                   2757:                        arg_offset++;
1.1.1.14  root     2758:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2759:                        ignore_illegal_insn = true;
                   2760:                        arg_offset++;
                   2761:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2762:                        limit_max_memory = true;
                   2763:                        arg_offset++;
                   2764:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2765:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2766:                        if(result == 1) {
                   2767:                                buf_width = 0;
                   2768:                        } else if(result != 2) {
1.1.1.17  root     2769:                                buf_width = buf_height = 0;
                   2770:                        }
                   2771:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2772:                                buf_width = 80;
                   2773:                        }
                   2774:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2775:                                buf_height = 25;
                   2776:                        }
1.1.1.14  root     2777:                        arg_offset++;
1.1.1.25  root     2778:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2779:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2780:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2781:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2782:                                        sio_port_number[1] = atoi(p1 + 1);
                   2783:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2784:                                                sio_port_number[2] = atoi(p2 + 1);
                   2785:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2786:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2787:                                                }
                   2788:                                        }
1.1.1.25  root     2789:                                }
1.1.1.29  root     2790:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2791:                        }
1.1.1.29  root     2792:                        if(sio_port_number[0] == 0 || sio_port_number[1] == 0 || sio_port_number[2] == 0 || sio_port_number[3] == 0) {
1.1.1.27  root     2793:                                get_sio_port_numbers();
1.1.1.25  root     2794:                        }
                   2795:                        arg_offset++;
1.1.1.9   root     2796:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2797:                        if(strlen(argv[i]) >= 5 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && (argv[i][5] == '\0' || IS_NUMERIC(argv[i][5]))) {
1.1.1.30  root     2798:                                dos_major_version = argv[i][2] - '0';
                   2799:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2800:                        }
                   2801:                        arg_offset++;
                   2802:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2803:                        if(strlen(argv[i]) >= 5 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && (argv[i][5] == '\0' || IS_NUMERIC(argv[i][5]))) {
                   2804:                                win_major_version = argv[i][2] - '0';
                   2805:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2806:                        }
                   2807:                        arg_offset++;
1.1.1.25  root     2808:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2809:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2810:                        support_ems = true;
                   2811: #ifdef SUPPORT_XMS
                   2812:                        support_xms = true;
                   2813: #endif
                   2814:                        arg_offset++;
1.1.1.9   root     2815:                } else {
                   2816:                        break;
                   2817:                }
                   2818:        }
                   2819:        if(argc < 2 + arg_offset) {
1.1       root     2820: #ifdef _WIN64
1.1.1.14  root     2821:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2822: #else
1.1.1.14  root     2823:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2824: #endif
1.1.1.25  root     2825:                fprintf(stderr,
1.1.1.28  root     2826:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2827:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2828:                        "\n"
                   2829:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2830: #ifdef _WIN64
1.1.1.27  root     2831:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2832: #else
1.1.1.27  root     2833:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2834: #endif
1.1.1.28  root     2835:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2836:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2837:                        "\t-e\tuse a reduced environment block\n"
                   2838:                        "\t-i\tignore invalid instructions\n"
                   2839:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2840:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2841:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2842:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2843:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2844: #ifdef SUPPORT_XMS
1.1.1.28  root     2845:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2846: #else
1.1.1.28  root     2847:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2848: #endif
                   2849:                );
1.1.1.10  root     2850:                
                   2851:                if(!is_started_from_command_prompt()) {
                   2852:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2853:                        while(!_kbhit()) {
                   2854:                                Sleep(10);
                   2855:                        }
                   2856:                }
1.1.1.20  root     2857: #ifdef _DEBUG
                   2858:                _CrtDumpMemoryLeaks();
                   2859: #endif
1.1       root     2860:                return(EXIT_FAILURE);
                   2861:        }
1.1.1.27  root     2862:        if(convert_cmd_file) {
                   2863:                retval = EXIT_FAILURE;
1.1.1.28  root     2864:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2865:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2866:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2867:                        
1.1.1.28  root     2868:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2869:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2870:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2871:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2872:                        } else {
1.1.1.28  root     2873:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2874:                                if(offset != 0) {
                   2875:                                        UINT8 buffer[14];
                   2876:                                        fseek(fp, offset, SEEK_SET);
                   2877:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2878:                                        memset(path, 0, sizeof(path));
                   2879:                                        fread(path, buffer[9], 1, fp);
                   2880:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2881:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2882:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2883:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2884:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2885:                                } else {
                   2886:                                        // read pe header of msdos.exe
                   2887:                                        UINT8 header[0x400];
                   2888:                                        fseek(fp, 0, SEEK_SET);
                   2889:                                        fread(header, sizeof(header), 1, fp);
                   2890:                                        
                   2891:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2892:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2893:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2894:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2895:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2896:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2897:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2898:                                        
                   2899:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2900:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2901:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2902:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2903:                                        if(dwExtraLastSectionBytes != 0) {
                   2904:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2905:                                                dwLastSectionSize += dwRemain;
                   2906:                                        }
                   2907:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2908:                                        
                   2909:                                        // store msdos.exe
                   2910:                                        fseek(fp, 0, SEEK_SET);
                   2911:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2912:                                                if((data = fgetc(fp)) != EOF) {
                   2913:                                                        fputc(data, fo);
                   2914:                                                } else {
                   2915:                                                        // we should not reach here :-(
                   2916:                                                        fputc(0, fo);
                   2917:                                                }
                   2918:                                        }
                   2919:                                        
                   2920:                                        // store options
                   2921:                                        UINT8 flags = 0;
                   2922:                                        if(stay_busy) {
                   2923:                                                flags |= 0x01;
                   2924:                                        }
                   2925:                                        if(no_windows) {
                   2926:                                                flags |= 0x02;
                   2927:                                        }
                   2928:                                        if(standard_env) {
                   2929:                                                flags |= 0x04;
                   2930:                                        }
                   2931:                                        if(ignore_illegal_insn) {
                   2932:                                                flags |= 0x08;
                   2933:                                        }
                   2934:                                        if(limit_max_memory) {
                   2935:                                                flags |= 0x10;
                   2936:                                        }
1.1.1.29  root     2937:                                        if(sio_port_number[0] != 0 || sio_port_number[1] != 0 || sio_port_number[2] != 0 || sio_port_number[3] != 0) {
1.1.1.28  root     2938:                                                flags |= 0x20;
                   2939:                                        }
                   2940:                                        if(support_ems) {
                   2941:                                                flags |= 0x40;
                   2942:                                        }
1.1.1.30  root     2943: #ifdef SUPPORT_XMS
                   2944:                                        if(support_xms) {
                   2945:                                                flags |= 0x80;
                   2946:                                        }
                   2947: #endif
1.1.1.28  root     2948:                                        
                   2949:                                        fputc(flags, fo);
                   2950:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2951:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2952:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2953:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2954:                                        fputc(dos_major_version, fo);
                   2955:                                        fputc(dos_minor_version, fo);
                   2956:                                        fputc(win_major_version, fo);
                   2957:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2958:                                        fputc((code_page >> 0) & 0xff, fo);
                   2959:                                        fputc((code_page >> 8) & 0xff, fo);
                   2960:                                        
                   2961:                                        // store command file info
1.1.1.60  root     2962:                                        GetFullPathNameA(argv[arg_offset + 1], MAX_PATH, full, &name);
1.1.1.28  root     2963:                                        int name_len = strlen(name);
                   2964:                                        fseek(fs, 0, SEEK_END);
                   2965:                                        long file_size = ftell(fs);
                   2966:                                        
                   2967:                                        fputc(name_len, fo);
                   2968:                                        fputc((file_size >>  0) & 0xff, fo);
                   2969:                                        fputc((file_size >>  8) & 0xff, fo);
                   2970:                                        fputc((file_size >> 16) & 0xff, fo);
                   2971:                                        fputc((file_size >> 24) & 0xff, fo);
                   2972:                                        fwrite(name, name_len, 1, fo);
                   2973:                                        
                   2974:                                        // store command file
                   2975:                                        fseek(fs, 0, SEEK_SET);
                   2976:                                        for(int i = 0; i < file_size; i++) {
                   2977:                                                if((data = fgetc(fs)) != EOF) {
                   2978:                                                        fputc(data, fo);
                   2979:                                                } else {
                   2980:                                                        // we should not reach here :-(
                   2981:                                                        fputc(0, fo);
                   2982:                                                }
                   2983:                                        }
                   2984:                                        
                   2985:                                        // store padding data and update pe header
1.1.1.29  root     2986:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2987:                                        coffHeader->NumberOfSections++;
                   2988:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2989:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2990:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2991:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2992:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2993:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2994:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2995:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2996:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2997:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2998:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2999:                                                        if(i < 2) {
                   3000:                                                                fputc(padding[i & 15], fo);
                   3001:                                                        } else {
                   3002:                                                                fputc(padding[(i - 2) & 15], fo);
                   3003:                                                        }
1.1.1.28  root     3004:                                                }
                   3005:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   3006:                                        }
                   3007:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   3008:                                        
                   3009:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   3010:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   3011:                                        if(dwExtraNewSectionBytes != 0) {
                   3012:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   3013:                                                dwNewSectionSize += dwRemain;
                   3014:                                        }
                   3015:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   3016:                                        
                   3017:                                        fseek(fo, 0, SEEK_SET);
                   3018:                                        fwrite(header, sizeof(header), 1, fo);
                   3019:                                        
                   3020:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   3021:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     3022:                                }
                   3023:                        }
                   3024:                        if(fp != NULL) {
                   3025:                                fclose(fp);
                   3026:                        }
                   3027:                        if(fs != NULL) {
                   3028:                                fclose(fs);
                   3029:                        }
                   3030:                        if(fo != NULL) {
                   3031:                                fclose(fo);
                   3032:                        }
                   3033:                }
                   3034: #ifdef _DEBUG
                   3035:                _CrtDumpMemoryLeaks();
                   3036: #endif
                   3037:                return(retval);
                   3038:        }
1.1       root     3039:        
1.1.1.54  root     3040:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     3041:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   3042:        
1.1.1.23  root     3043:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3044:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     3045:        CONSOLE_CURSOR_INFO ci;
1.1.1.61! root     3046:        UINT input_cp = GetConsoleCP();
        !          3047:        UINT output_cp = GetConsoleOutputCP();
        !          3048:        int multibyte_cp = _getmbcp();
1.1.1.23  root     3049:        
1.1.1.28  root     3050:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     3051:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.59  root     3052:        ci_old = ci_new = ci;
1.1.1.24  root     3053:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.61! root     3054:        get_console_font_success = get_console_font_size(&font_width, &font_height);
1.1       root     3055:        
1.1.1.14  root     3056:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   3057:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   3058:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3059:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     3060:                }
                   3061:        }
1.1.1.28  root     3062:        if(get_console_info_success) {
1.1.1.12  root     3063:                scr_width = csbi.dwSize.X;
1.1.1.14  root     3064:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   3065:                
1.1.1.28  root     3066:                // v-text shadow buffer size must be lesser than 0x7fd0
                   3067:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     3068:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   3069:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   3070:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     3071:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     3072:                                scr_width = 80;
                   3073:                                scr_height = 25;
                   3074:                        }
1.1.1.28  root     3075:                        screen_size_changed = true;
1.1.1.14  root     3076:                }
1.1.1.12  root     3077:        } else {
                   3078:                // for a proof (not a console)
                   3079:                scr_width = 80;
                   3080:                scr_height = 25;
                   3081:        }
1.1.1.14  root     3082:        scr_buf_size.X = scr_width;
                   3083:        scr_buf_size.Y = scr_height;
                   3084:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   3085:        scr_top = csbi.srWindow.Top;
1.1       root     3086:        cursor_moved = false;
1.1.1.59  root     3087:        cursor_moved_by_crtc = false;
1.1       root     3088:        
1.1.1.54  root     3089:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   3090:        
1.1.1.35  root     3091: #ifdef USE_SERVICE_THREAD
                   3092:        InitializeCriticalSection(&input_crit_sect);
                   3093:        InitializeCriticalSection(&key_buf_crit_sect);
                   3094:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     3095:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     3096: #endif
1.1.1.50  root     3097:        
1.1.1.25  root     3098:        key_buf_char = new FIFO(256);
                   3099:        key_buf_scan = new FIFO(256);
1.1.1.57  root     3100:        key_buf_data = new FIFO(256);
1.1       root     3101:        
                   3102:        hardware_init();
                   3103:        
1.1.1.33  root     3104: #ifdef USE_DEBUGGER
                   3105:        debugger_init();
                   3106: #endif
                   3107:        
1.1.1.9   root     3108:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     3109:                retval = EXIT_FAILURE;
                   3110:        } else {
1.1.1.27  root     3111: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     3112:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   3113: #endif
                   3114:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   3115:                
1.1.1.28  root     3116:                if(screen_size_changed) {
1.1.1.24  root     3117:                        change_console_size(scr_width, scr_height);
                   3118:                }
1.1.1.8   root     3119:                TIMECAPS caps;
                   3120:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   3121:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     3122: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3123:                InitializeCriticalSection(&vram_crit_sect);
                   3124:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   3125: #endif
1.1.1.33  root     3126: #ifdef USE_DEBUGGER
                   3127:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   3128:                // wait until telnet client starts and connects to me
                   3129:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   3130:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   3131:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   3132:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   3133:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   3134:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   3135:                                Sleep(100);
                   3136:                        }
                   3137:                }
                   3138: #endif
1.1       root     3139:                hardware_run();
1.1.1.35  root     3140: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3141:                vram_flush();
                   3142:                DeleteCriticalSection(&vram_crit_sect);
                   3143: #endif
1.1.1.24  root     3144:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     3145:                
1.1.1.24  root     3146:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.61! root     3147:                hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        !          3148:                
        !          3149:                // restore console settings
        !          3150:                _setmbcp(multibyte_cp);
        !          3151:                SetConsoleCP(input_cp);
        !          3152:                SetConsoleOutputCP(multibyte_cp);
        !          3153:                
1.1.1.28  root     3154:                if(get_console_info_success) {
1.1.1.12  root     3155:                        if(restore_console_on_exit) {
1.1.1.14  root     3156:                                // window can't be bigger than buffer,
                   3157:                                // buffer can't be smaller than window,
                   3158:                                // so make a tiny window,
                   3159:                                // set the required buffer,
                   3160:                                // then set the required window
1.1.1.61! root     3161:                                CONSOLE_SCREEN_BUFFER_INFO cur_csbi;
1.1.1.14  root     3162:                                SMALL_RECT rect;
1.1.1.61! root     3163:                                GetConsoleScreenBufferInfo(hStdout, &cur_csbi);
        !          3164:                                int min_width  = min(cur_csbi.srWindow.Right - cur_csbi.srWindow.Left + 1, csbi.srWindow.Right - csbi.srWindow.Left + 1);
        !          3165:                                int min_height = min(cur_csbi.srWindow.Bottom - cur_csbi.srWindow.Top + 1, csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
        !          3166:                                
        !          3167:                                SET_RECT(rect, 0, cur_csbi.srWindow.Top, min_width - 1, cur_csbi.srWindow.Top + min_height - 1);
1.1.1.14  root     3168:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     3169:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     3170:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.61! root     3171:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
        !          3172:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
        !          3173:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
        !          3174:                                }
        !          3175:                        }
        !          3176:                }
        !          3177:                if(get_console_font_success) {
        !          3178:                        set_console_font_size(font_width, font_height);
        !          3179:                }
        !          3180:                if(get_console_info_success) {
        !          3181:                        if(restore_console_on_exit) {
        !          3182:                                SMALL_RECT rect;
        !          3183:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
        !          3184:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
        !          3185:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
        !          3186:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
        !          3187:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
        !          3188:                                }
1.1.1.12  root     3189:                        }
1.1.1.14  root     3190:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3191:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3192:                }
1.1.1.24  root     3193:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   3194:                
1.1       root     3195:                msdos_finish();
1.1.1.14  root     3196:                
                   3197:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3198:        }
1.1.1.35  root     3199:        if(temp_file_created) {
1.1.1.60  root     3200:                DeleteFileA(temp_file_path);
1.1.1.35  root     3201:                temp_file_created = false;
                   3202:        }
1.1.1.10  root     3203:        hardware_finish();
                   3204:        
1.1.1.28  root     3205:        if(key_buf_char != NULL) {
                   3206:                key_buf_char->release();
                   3207:                delete key_buf_char;
                   3208:                key_buf_char = NULL;
                   3209:        }
                   3210:        if(key_buf_scan != NULL) {
                   3211:                key_buf_scan->release();
                   3212:                delete key_buf_scan;
                   3213:                key_buf_scan = NULL;
                   3214:        }
1.1.1.57  root     3215:        if(key_buf_data != NULL) {
                   3216:                key_buf_data->release();
                   3217:                delete key_buf_data;
                   3218:                key_buf_data = NULL;
                   3219:        }
1.1.1.35  root     3220: #ifdef USE_SERVICE_THREAD
                   3221:        DeleteCriticalSection(&input_crit_sect);
                   3222:        DeleteCriticalSection(&key_buf_crit_sect);
                   3223:        DeleteCriticalSection(&putch_crit_sect);
                   3224: #endif
1.1.1.20  root     3225: #ifdef _DEBUG
                   3226:        _CrtDumpMemoryLeaks();
                   3227: #endif
1.1       root     3228:        return(retval);
                   3229: }
                   3230: 
1.1.1.20  root     3231: /* ----------------------------------------------------------------------------
                   3232:        console
                   3233: ---------------------------------------------------------------------------- */
                   3234: 
1.1.1.14  root     3235: void change_console_size(int width, int height)
1.1.1.12  root     3236: {
1.1.1.23  root     3237:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3238:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3239:        SMALL_RECT rect;
                   3240:        COORD co;
                   3241:        
                   3242:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3243:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3244:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
1.1.1.60  root     3245:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
1.1.1.14  root     3246:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3247:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3248:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3249:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
1.1.1.60  root     3250:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3251:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3252:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3253:                }
                   3254:        }
1.1.1.14  root     3255:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3256:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3257:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3258:                SetConsoleCursorPosition(hStdout, co);
                   3259:                cursor_moved = true;
1.1.1.59  root     3260:                cursor_moved_by_crtc = false;
1.1.1.12  root     3261:        }
1.1.1.14  root     3262:        
                   3263:        // window can't be bigger than buffer,
                   3264:        // buffer can't be smaller than window,
                   3265:        // so make a tiny window,
                   3266:        // set the required buffer,
                   3267:        // then set the required window
1.1.1.61! root     3268:        int min_width  = min(csbi.srWindow.Right - csbi.srWindow.Left + 1, width);
        !          3269:        int min_height = min(csbi.srWindow.Bottom - csbi.srWindow.Top + 1, height);
        !          3270:        
        !          3271:        SET_RECT(rect, 0, csbi.srWindow.Top, min_width - 1, csbi.srWindow.Top + min_height - 1);
1.1.1.12  root     3272:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3273:        co.X = width;
                   3274:        co.Y = height;
1.1.1.12  root     3275:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3276:        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.61! root     3277:        if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
        !          3278:                SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
        !          3279:                SetConsoleWindowInfo(hStdout, TRUE, &rect);
        !          3280:        }
1.1.1.14  root     3281:        
                   3282:        scr_width = scr_buf_size.X = width;
                   3283:        scr_height = scr_buf_size.Y = height;
                   3284:        scr_top = 0;
                   3285:        
                   3286:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3287:        
                   3288:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3289:        text_vram_end_address = text_vram_top_address + regen;
                   3290:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3291:        
1.1.1.14  root     3292:        if(regen > 0x4000) {
                   3293:                regen = 0x8000;
                   3294:                vram_pages = 1;
                   3295:        } else if(regen > 0x2000) {
                   3296:                regen = 0x4000;
                   3297:                vram_pages = 2;
                   3298:        } else if(regen > 0x1000) {
                   3299:                regen = 0x2000;
                   3300:                vram_pages = 4;
                   3301:        } else {
                   3302:                regen = 0x1000;
                   3303:                vram_pages = 8;
                   3304:        }
1.1.1.15  root     3305:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3306:        *(UINT16 *)(mem + 0x44c) = regen;
                   3307:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3308:        
1.1.1.24  root     3309:        mouse.min_position.x = 0;
                   3310:        mouse.min_position.y = 0;
1.1.1.34  root     3311:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3312:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3313:        
1.1.1.15  root     3314:        restore_console_on_exit = true;
1.1.1.14  root     3315: }
                   3316: 
                   3317: void clear_scr_buffer(WORD attr)
                   3318: {
                   3319:        for(int y = 0; y < scr_height; y++) {
                   3320:                for(int x = 0; x < scr_width; x++) {
                   3321:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3322:                        SCR_BUF(y,x).Attributes = attr;
                   3323:                }
                   3324:        }
1.1.1.12  root     3325: }
                   3326: 
1.1.1.24  root     3327: bool update_console_input()
1.1       root     3328: {
1.1.1.35  root     3329: #ifdef USE_SERVICE_THREAD
                   3330:        EnterCriticalSection(&input_crit_sect);
                   3331: #endif
1.1.1.23  root     3332:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3333:        DWORD dwNumberOfEvents = 0;
1.1       root     3334:        DWORD dwRead;
                   3335:        INPUT_RECORD ir[16];
1.1.1.24  root     3336:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3337:        bool result = false;
1.1       root     3338:        
1.1.1.8   root     3339:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3340:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3341:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3342:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.59  root     3343:                                        if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
                   3344:                                                if(mouse.hidden == 0 || (mouse.call_addr_ps2.dw && mouse.enabled_ps2)) {
                   3345:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
                   3346:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3347:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3348:                                                        }
                   3349:                                                        // FIXME: character size is always 8x8 ???
                   3350:                                                        int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3351:                                                        int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3352:                                                        
                   3353:                                                        if(mouse.position.x != x || mouse.position.y != y) {
                   3354:                                                                mouse.position.x = x;
                   3355:                                                                mouse.position.y = y;
                   3356:                                                                mouse.status |= 1;
                   3357:                                                                mouse.status_alt |= 1;
                   3358:                                                        }
1.1.1.34  root     3359:                                                }
1.1.1.59  root     3360:                                        } else if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3361:                                                for(int j = 0; j < MAX_MOUSE_BUTTONS; j++) {
1.1.1.34  root     3362:                                                        static const DWORD bits[] = {
                   3363:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3364:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3365:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3366:                                                        };
1.1.1.59  root     3367:                                                        bool prev_status = mouse.buttons[j].status;
                   3368:                                                        mouse.buttons[j].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[j]) != 0);
1.1.1.34  root     3369:                                                        
1.1.1.59  root     3370:                                                        if(!prev_status && mouse.buttons[j].status) {
                   3371:                                                                mouse.buttons[j].pressed_times++;
                   3372:                                                                mouse.buttons[j].pressed_position.x = mouse.position.x;
                   3373:                                                                mouse.buttons[j].pressed_position.y = mouse.position.x;
                   3374:                                                                if(j < 2) {
                   3375:                                                                        mouse.status_alt |= 2 << (j * 2);
1.1.1.43  root     3376:                                                                }
1.1.1.59  root     3377:                                                                mouse.status |= 2 << (j * 2);
                   3378:                                                        } else if(prev_status && !mouse.buttons[j].status) {
                   3379:                                                                mouse.buttons[j].released_times++;
                   3380:                                                                mouse.buttons[j].released_position.x = mouse.position.x;
                   3381:                                                                mouse.buttons[j].released_position.y = mouse.position.x;
                   3382:                                                                if(j < 2) {
                   3383:                                                                        mouse.status_alt |= 4 << (j * 2);
1.1.1.43  root     3384:                                                                }
1.1.1.59  root     3385:                                                                mouse.status |= 4 << (j * 2);
1.1.1.14  root     3386:                                                        }
                   3387:                                                }
                   3388:                                        }
1.1.1.24  root     3389:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3390:                                        // update keyboard flags in bios data area
1.1.1.35  root     3391:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3392:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3393:                                        } else {
1.1.1.35  root     3394:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3395:                                        }
1.1.1.35  root     3396:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3397:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3398:                                        } else {
1.1.1.35  root     3399:                                                mem[0x417] &= ~0x20;
                   3400:                                        }
                   3401:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3402:                                                mem[0x417] |= 0x10;
                   3403:                                        } else {
                   3404:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3405:                                        }
                   3406:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3407:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3408:                                                        mouse.status_alt |= 0x80;
                   3409:                                                }
1.1.1.33  root     3410:                                                mem[0x417] |= 0x08;
                   3411:                                        } else {
                   3412:                                                mem[0x417] &= ~0x08;
                   3413:                                        }
1.1.1.35  root     3414:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3415:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3416:                                                        mouse.status_alt |= 0x40;
                   3417:                                                }
1.1.1.35  root     3418:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3419:                                        } else {
1.1.1.35  root     3420:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3421:                                        }
                   3422:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3423:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3424:                                                        mouse.status_alt |= 0x20;
                   3425:                                                }
1.1.1.33  root     3426:                                                if(!(mem[0x417] & 0x03)) {
                   3427:                                                        mem[0x417] |= 0x02; // left shift
                   3428:                                                }
                   3429:                                        } else {
                   3430:                                                mem[0x417] &= ~0x03;
                   3431:                                        }
1.1.1.35  root     3432:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3433:                                                mem[0x418] |= 0x02;
                   3434:                                        } else {
                   3435:                                                mem[0x418] &= ~0x02;
                   3436:                                        }
                   3437:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3438:                                                mem[0x418] |= 0x01;
                   3439:                                        } else {
                   3440:                                                mem[0x418] &= ~0x01;
                   3441:                                        }
1.1.1.33  root     3442:                                        
1.1.1.28  root     3443:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57  root     3444: //                                     kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
                   3445: //                                     kbd_status |= 1;
                   3446:                                        UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3447:                                        
                   3448:                                        // update dos key buffer
                   3449:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3450:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3451:                                        UINT8 scn_old = scn;
1.1.1.33  root     3452:                                        
                   3453:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3454:                                                // make
1.1.1.57  root     3455:                                                tmp_data &= 0x7f;
1.1.1.24  root     3456:                                                
1.1.1.33  root     3457:                                                if(chr == 0x00) {
1.1.1.24  root     3458:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3459:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3460:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3461:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3462:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3463:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3464:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3465:                                                                } else if(scn == 0x35) {
                   3466:                                                                        scn = 0xa4;             // keypad /
                   3467:                                                                }
                   3468:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3469:                                                                if(scn == 0x07) {
                   3470:                                                                        chr = 0x1e;     // Ctrl+^
                   3471:                                                                } else if(scn == 0x0c) {
                   3472:                                                                        chr = 0x1f;     // Ctrl+_
                   3473:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3474:                                                                        static const UINT8 ctrl_map[] = {
                   3475:                                                                                0x95,   // keypad /
                   3476:                                                                                0,
                   3477:                                                                                0x96,   // keypad *
                   3478:                                                                                0, 0, 0,
                   3479:                                                                                0x5e,   // F1
                   3480:                                                                                0x5f,   // F2
                   3481:                                                                                0x60,   // F3
                   3482:                                                                                0x61,   // F4
                   3483:                                                                                0x62,   // F5
                   3484:                                                                                0x63,   // F6
                   3485:                                                                                0x64,   // F7
                   3486:                                                                                0x65,   // F8
                   3487:                                                                                0x66,   // F9
                   3488:                                                                                0x67,   // F10
                   3489:                                                                                0,
                   3490:                                                                                0,
                   3491:                                                                                0x77,   // Home
                   3492:                                                                                0x8d,   // Up
                   3493:                                                                                0x84,   // PgUp
                   3494:                                                                                0x8e,   // keypad -
                   3495:                                                                                0x73,   // Left
                   3496:                                                                                0x8f,   // keypad center
                   3497:                                                                                0x74,   // Right
                   3498:                                                                                0x90,   // keyapd +
                   3499:                                                                                0x75,   // End
                   3500:                                                                                0x91,   // Down
                   3501:                                                                                0x76,   // PgDn
                   3502:                                                                                0x92,   // Insert
                   3503:                                                                                0x93,   // Delete
                   3504:                                                                                0, 0, 0,
                   3505:                                                                                0x89,   // F11
                   3506:                                                                                0x8a,   // F12
                   3507:                                                                        };
                   3508:                                                                        scn = ctrl_map[scn - 0x35];
                   3509:                                                                }
                   3510:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3511:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3512:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3513:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3514:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3515:                                                                }
                   3516:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3517:                                                                scn += 0x85 - 0x57;
                   3518:                                                        }
                   3519:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3520:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3521:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3522: #ifdef USE_SERVICE_THREAD
                   3523:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3524: #endif
1.1.1.32  root     3525:                                                                        if(chr == 0) {
1.1.1.51  root     3526:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3527:                                                                        }
1.1.1.51  root     3528:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3529: #ifdef USE_SERVICE_THREAD
                   3530:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3531: #endif
1.1.1.24  root     3532:                                                                }
                   3533:                                                        }
                   3534:                                                } else {
                   3535:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3536:                                                                chr = 0;
                   3537:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3538:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3539:                                                                }
                   3540:                                                        }
1.1.1.32  root     3541:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3542: #ifdef USE_SERVICE_THREAD
                   3543:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3544: #endif
1.1.1.51  root     3545:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3546: #ifdef USE_SERVICE_THREAD
                   3547:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3548: #endif
1.1.1.32  root     3549:                                                        }
1.1.1.24  root     3550:                                                }
1.1.1.57  root     3551:                                        } else {
                   3552:                                                if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3553:                                                        // ctrl-break, ctrl-c
                   3554:                                                        if(scn == 0x46) {
                   3555:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3556: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3557:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3558: #endif
1.1.1.57  root     3559:                                                                        pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3560: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3561:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3562: #endif
1.1.1.57  root     3563:                                                                }
                   3564:                                                                ctrl_break_pressed = true;
                   3565:                                                                mem[0x471] = 0x80;
                   3566:                                                                raise_int_1bh = true;
                   3567:                                                        } else {
                   3568:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3569: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3570:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3571: #endif
1.1.1.57  root     3572:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3573: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3574:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3575: #endif
1.1.1.57  root     3576:                                                                }
                   3577:                                                                ctrl_c_pressed = (scn == 0x2e);
1.1.1.33  root     3578:                                                        }
                   3579:                                                }
                   3580:                                                // break
1.1.1.57  root     3581:                                                tmp_data |= 0x80;
                   3582:                                        }
                   3583:                                        if(!(kbd_status & 1)) {
                   3584:                                                kbd_data = tmp_data;
                   3585:                                                kbd_status |= 1;
                   3586:                                        } else {
                   3587:                                                if(key_buf_data != NULL) {
                   3588: #ifdef USE_SERVICE_THREAD
                   3589:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3590: #endif
                   3591:                                                        key_buf_data->write(tmp_data);
                   3592: #ifdef USE_SERVICE_THREAD
                   3593:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3594: #endif
                   3595:                                                }
1.1       root     3596:                                        }
1.1.1.24  root     3597:                                        result = key_changed = true;
1.1.1.36  root     3598:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3599:                                        cursor_moved = true;
1.1       root     3600:                                }
                   3601:                        }
                   3602:                }
                   3603:        }
1.1.1.35  root     3604: #ifdef USE_SERVICE_THREAD
                   3605:        LeaveCriticalSection(&input_crit_sect);
                   3606: #endif
1.1.1.24  root     3607:        return(result);
1.1.1.8   root     3608: }
                   3609: 
1.1.1.14  root     3610: bool update_key_buffer()
1.1.1.8   root     3611: {
1.1.1.35  root     3612:        if(update_console_input()) {
                   3613:                return(true);
                   3614:        }
                   3615:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3616: #ifdef USE_SERVICE_THREAD
                   3617:                EnterCriticalSection(&key_buf_crit_sect);
                   3618: #endif
1.1.1.55  root     3619:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3620: #ifdef USE_SERVICE_THREAD
                   3621:                LeaveCriticalSection(&key_buf_crit_sect);
                   3622: #endif
                   3623:                if(!empty) return(true);
                   3624:        }
                   3625:        return(false);
1.1.1.8   root     3626: }
                   3627: 
1.1.1.20  root     3628: /* ----------------------------------------------------------------------------
                   3629:        MS-DOS virtual machine
                   3630: ---------------------------------------------------------------------------- */
                   3631: 
1.1.1.32  root     3632: static const struct {
1.1.1.33  root     3633:        char *name;
                   3634:        DWORD lcid;
                   3635:        char *std;
                   3636:        char *dlt;
                   3637: } tz_table[] = {
                   3638:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3639: //     0       GMT             Greenwich Mean Time             GMT0
                   3640:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3641:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3642:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3643:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3644: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3645:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3646:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3647: //     3       BST             Brazil Standard Time            BST3
                   3648:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3649:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3650:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3651: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3652:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3653: //     3       GST             Greenland Standard Time         GST3
                   3654:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3655: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3656:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3657: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3658:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3659: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3660:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3661:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3662: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3663:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3664:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3665:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3666: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3667:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3668: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3669:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3670: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3671:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3672: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3673:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3674:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3675:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3676: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3677:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3678: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3679:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3680:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3681:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3682: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3683:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3684:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3685: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3686: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3687:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3688: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3689:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3690:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3691: //     11      SST             Samoa Standard Time             SST11
                   3692:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3693: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3694:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3695: //     -10     GST             Guam Standard Time              GST-10
                   3696:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3697: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3698:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3699:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3700:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3701: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3702:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3703:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3704: //     -9      JST             Japan Standard Time             JST-9
                   3705:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3706: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3707:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3708:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3709: //     -8      HKT             Hong Kong Time                  HKT-8
                   3710:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3711: //     -8      CCT             China Coast Time                CCT-8
                   3712:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3713:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3714: //     -8      SST             Singapore Standard Time         SST-8
                   3715:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3716: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3717:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3718:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3719: //     -7:30   JT              Java Standard Time              JST-7:30
                   3720: //     -7      NST             North Sumatra Time              NST-7
                   3721:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3722: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3723:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3724: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3725:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3726: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3727:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3728:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3729: //     -2      EET             Eastern Europe Time             EET-2
                   3730:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3731:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3732:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3733:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3734: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3735:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3736: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3737: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3738: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3739: //     -1      CET     CES     Central European Time           CET-1CES
                   3740:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3741:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3742:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3743:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3744: //     -1      WAT             West African Time               WAT-1
                   3745:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3746:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3747: //     0       UTC             Universal Coordinated Time      UTC0
                   3748:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3749:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3750:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3751:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3752:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3753:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3754: };
                   3755: 
1.1.1.53  root     3756: // FIXME: consider to build on non-Japanese environment :-(
                   3757: // message_japanese string must be in shift-jis
                   3758: 
1.1.1.33  root     3759: static const struct {
1.1.1.32  root     3760:        UINT16 code;
                   3761:        char *message_english;
                   3762:        char *message_japanese;
                   3763: } standard_error_table[] = {
                   3764:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3765:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3766:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3767:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3768:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3769:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3770:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3771:        {0x08,  "Insufficient memory", "������������܂���."},
                   3772:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3773:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3774:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3775:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3776:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3777:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3778:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3779:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3780:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3781:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3782:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3783:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3784:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3785:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3786:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3787:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3788:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3789:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3790:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3791:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3792:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3793:        {0x1F,  "General failure", "�G���[�ł�."},
                   3794:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3795:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3796:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3797:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3798:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3799:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3800:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3801:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3802: /*
                   3803:        {0x32,  "Network request not supported", NULL},
                   3804:        {0x33,  "Remote computer not listening", NULL},
                   3805:        {0x34,  "Duplicate name on network", NULL},
                   3806:        {0x35,  "Network name not found", NULL},
                   3807:        {0x36,  "Network busy", NULL},
                   3808:        {0x37,  "Network device no longer exists", NULL},
                   3809:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3810:        {0x39,  "Network adapter hardware error", NULL},
                   3811:        {0x3A,  "Incorrect response from network", NULL},
                   3812:        {0x3B,  "Unexpected network error", NULL},
                   3813:        {0x3C,  "Incompatible remote adapter", NULL},
                   3814:        {0x3D,  "Print queue full", NULL},
                   3815:        {0x3E,  "Queue not full", NULL},
                   3816:        {0x3F,  "Not enough space to print file", NULL},
                   3817:        {0x40,  "Network name was deleted", NULL},
                   3818:        {0x41,  "Network: Access denied", NULL},
                   3819:        {0x42,  "Network device type incorrect", NULL},
                   3820:        {0x43,  "Network name not found", NULL},
                   3821:        {0x44,  "Network name limit exceeded", NULL},
                   3822:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3823:        {0x46,  "Temporarily paused", NULL},
                   3824:        {0x47,  "Network request not accepted", NULL},
                   3825:        {0x48,  "Network print/disk redirection paused", NULL},
                   3826:        {0x49,  "Network software not installed", NULL},
                   3827:        {0x4A,  "Unexpected adapter close", NULL},
                   3828: */
                   3829:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3830:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3831:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3832:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3833:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3834:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3835:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3836:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3837:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3838:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3839: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3840:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3841:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3842:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3843:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3844:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3845: #endif
1.1.1.32  root     3846:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3847:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3848:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3849:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3850:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3851:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3852: };
                   3853: 
                   3854: static const struct {
                   3855:        UINT16 code;
                   3856:        char *message_english;
                   3857:        char *message_japanese;
                   3858: } param_error_table[] = {
                   3859:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3860:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3861:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3862:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3863:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3864:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3865:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3866:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3867:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3868:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3869:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3870: };
                   3871: 
                   3872: static const struct {
                   3873:        UINT16 code;
                   3874:        char *message_english;
                   3875:        char *message_japanese;
                   3876: } critical_error_table[] = {
                   3877:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3878:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3879:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3880:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3881:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3882:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3883:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3884:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3885:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3886:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3887:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3888:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3889:        {0x0C,  "General failure", "�G���[�ł�."},
                   3890:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3891:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3892:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3893:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3894:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3895:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3896:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3897:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3898:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3899: };
                   3900: 
1.1.1.20  root     3901: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3902: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3903: void msdos_putch(UINT8 data);
1.1.1.50  root     3904: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3905: #ifdef USE_SERVICE_THREAD
                   3906: void msdos_putch_tmp(UINT8 data);
                   3907: #endif
1.1.1.45  root     3908: const char *msdos_short_path(const char *path);
1.1.1.44  root     3909: bool msdos_is_valid_drive(int drv);
                   3910: bool msdos_is_removable_drive(int drv);
                   3911: bool msdos_is_cdrom_drive(int drv);
                   3912: bool msdos_is_remote_drive(int drv);
                   3913: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3914: 
1.1       root     3915: // process info
                   3916: 
                   3917: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3918: {
                   3919:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3920:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3921:                        memset(&process[i], 0, sizeof(process_t));
                   3922:                        process[i].psp = psp_seg;
                   3923:                        return(&process[i]);
                   3924:                }
                   3925:        }
                   3926:        fatalerror("too many processes\n");
                   3927:        return(NULL);
                   3928: }
                   3929: 
1.1.1.52  root     3930: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3931: {
                   3932:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3933:                if(process[i].psp == psp_seg) {
                   3934:                        return(&process[i]);
                   3935:                }
                   3936:        }
1.1.1.33  root     3937:        if(show_error) {
                   3938:                fatalerror("invalid psp address\n");
                   3939:        }
1.1       root     3940:        return(NULL);
                   3941: }
                   3942: 
1.1.1.23  root     3943: void msdos_sda_update(int psp_seg)
                   3944: {
                   3945:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3946:        
                   3947:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3948:                if(process[i].psp == psp_seg) {
                   3949:                        sda->switchar = process[i].switchar;
                   3950:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3951:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3952:                        sda->current_psp = process[i].psp;
                   3953:                        break;
                   3954:                }
                   3955:        }
                   3956:        sda->malloc_strategy = malloc_strategy;
                   3957:        sda->return_code = retval;
                   3958:        sda->current_drive = _getdrive();
                   3959: }
                   3960: 
1.1.1.13  root     3961: // dta info
                   3962: 
                   3963: void msdos_dta_info_init()
                   3964: {
1.1.1.14  root     3965:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3966:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3967:        }
                   3968: }
                   3969: 
                   3970: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3971: {
                   3972:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3973:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3974:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3975:                        if(free_dta == NULL) {
1.1.1.13  root     3976:                                free_dta = &dtalist[i];
                   3977:                        }
1.1.1.14  root     3978:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3979:                        return(&dtalist[i]);
                   3980:                }
                   3981:        }
1.1.1.14  root     3982:        if(free_dta) {
1.1.1.13  root     3983:                free_dta->psp = psp_seg;
                   3984:                free_dta->dta = dta_laddr;
                   3985:                return(free_dta);
                   3986:        }
                   3987:        fatalerror("too many dta\n");
                   3988:        return(NULL);
                   3989: }
                   3990: 
                   3991: void msdos_dta_info_free(UINT16 psp_seg)
                   3992: {
1.1.1.14  root     3993:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3994:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3995:                        FindClose(dtalist[i].find_handle);
                   3996:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3997:                }
                   3998:        }
                   3999: }
                   4000: 
1.1       root     4001: void msdos_cds_update(int drv)
                   4002: {
1.1.1.44  root     4003:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     4004:        
1.1.1.44  root     4005:        memset(cds, 0, 88);
                   4006:        
                   4007:        if(msdos_is_valid_drive(drv)) {
                   4008:                char path[MAX_PATH];
                   4009:                if(msdos_is_remote_drive(drv)) {
                   4010:                        cds->drive_attrib = 0xc000;     // network drive
                   4011:                } else if(msdos_is_subst_drive(drv)) {
                   4012:                        cds->drive_attrib = 0x5000;     // subst drive
                   4013:                } else {
                   4014:                        cds->drive_attrib = 0x4000;     // physical drive
                   4015:                }
                   4016:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   4017:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   4018:                }
                   4019:        }
                   4020:        if(cds->path_name[0] == '\0') {
                   4021:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   4022:        }
                   4023:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   4024:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   4025:        cds->word_1 = cds->word_2 = 0xffff;
                   4026:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   4027:        cds->bs_offset = 2;
                   4028: }
                   4029: 
1.1.1.45  root     4030: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     4031: {
                   4032:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   4033:        
                   4034:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     4035: }
                   4036: 
1.1.1.17  root     4037: // nls information tables
                   4038: 
                   4039: // uppercase table (func 6502h)
                   4040: void msdos_upper_table_update()
                   4041: {
                   4042:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4043:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4044:                UINT8 c[4];
1.1.1.33  root     4045:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4046:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     4047:                c[0] = 0x80 + i;
                   4048:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   4049:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4050:        }
                   4051: }
                   4052: 
1.1.1.23  root     4053: // lowercase table (func 6503h)
                   4054: void msdos_lower_table_update()
                   4055: {
                   4056:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   4057:        for(unsigned i = 0; i < 0x80; ++i) {
                   4058:                UINT8 c[4];
1.1.1.33  root     4059:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4060:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     4061:                c[0] = 0x80 + i;
                   4062:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   4063:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4064:        }
                   4065: }
                   4066: 
1.1.1.17  root     4067: // filename uppercase table (func 6504h)
                   4068: void msdos_filename_upper_table_init()
                   4069: {
                   4070:        // depended on (file)system, not on active codepage
                   4071:        // temporary solution: just filling data
                   4072:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4073:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4074:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   4075:        }
                   4076: }
                   4077: 
                   4078: // filaname terminator table (func 6505h)
                   4079: void msdos_filename_terminator_table_init()
                   4080: {
                   4081:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   4082:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   4083:        
                   4084:        data[2] = 1;            // marker? (permissible character value)
                   4085:        data[3] = 0x00;         // 00h...FFh
                   4086:        data[4] = 0xff;
                   4087:        data[5] = 0;            // marker? (excluded character)
                   4088:        data[6] = 0x00;         // 00h...20h
                   4089:        data[7] = 0x20;
                   4090:        data[8] = 2;            // marker? (illegal characters for filename)
                   4091:        data[9] = (UINT8)strlen(illegal_chars);
                   4092:        memcpy(data + 10, illegal_chars, data[9]);
                   4093:        
                   4094:        // total length
                   4095:        *(UINT16 *)data = (10 - 2) + data[9];
                   4096: }
                   4097: 
                   4098: // collating table (func 6506h)
                   4099: void msdos_collating_table_update()
                   4100: {
                   4101:        // temporary solution: just filling data
                   4102:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     4103:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     4104:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   4105:        }
                   4106: }
                   4107: 
1.1       root     4108: // dbcs
                   4109: 
                   4110: void msdos_dbcs_table_update()
                   4111: {
                   4112:        UINT8 dbcs_data[DBCS_SIZE];
                   4113:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   4114:        
                   4115:        CPINFO info;
                   4116:        GetCPInfo(active_code_page, &info);
                   4117:        
                   4118:        if(info.MaxCharSize != 1) {
                   4119:                for(int i = 0;; i += 2) {
                   4120:                        UINT8 lo = info.LeadByte[i + 0];
                   4121:                        UINT8 hi = info.LeadByte[i + 1];
                   4122:                        dbcs_data[2 + i + 0] = lo;
                   4123:                        dbcs_data[2 + i + 1] = hi;
                   4124:                        if(lo == 0 && hi == 0) {
                   4125:                                dbcs_data[0] = i + 2;
                   4126:                                break;
                   4127:                        }
                   4128:                }
                   4129:        } else {
                   4130:                dbcs_data[0] = 2;       // ???
                   4131:        }
                   4132:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   4133: }
                   4134: 
1.1.1.17  root     4135: void msdos_dbcs_table_finish()
                   4136: {
1.1.1.32  root     4137:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     4138:                _setmbcp(system_code_page);
                   4139:        }
1.1.1.32  root     4140:        if(console_code_page != GetConsoleCP()) {
                   4141:                SetConsoleCP(console_code_page);
                   4142:                SetConsoleOutputCP(console_code_page);
                   4143:        }
1.1.1.17  root     4144: }
                   4145: 
                   4146: void msdos_nls_tables_init()
1.1       root     4147: {
1.1.1.32  root     4148:        active_code_page = console_code_page = GetConsoleCP();
                   4149:        system_code_page = _getmbcp();
                   4150:        
                   4151:        if(active_code_page != system_code_page) {
                   4152:                if(_setmbcp(active_code_page) != 0) {
                   4153:                        active_code_page = system_code_page;
                   4154:                }
                   4155:        }
                   4156:        
1.1.1.17  root     4157:        msdos_upper_table_update();
1.1.1.23  root     4158:        msdos_lower_table_update();
1.1.1.17  root     4159:        msdos_filename_terminator_table_init();
                   4160:        msdos_filename_upper_table_init();
                   4161:        msdos_collating_table_update();
1.1       root     4162:        msdos_dbcs_table_update();
                   4163: }
                   4164: 
1.1.1.17  root     4165: void msdos_nls_tables_update()
1.1       root     4166: {
1.1.1.17  root     4167:        msdos_dbcs_table_update();
                   4168:        msdos_upper_table_update();
1.1.1.23  root     4169:        msdos_lower_table_update();
                   4170: //     msdos_collating_table_update();
1.1       root     4171: }
                   4172: 
                   4173: int msdos_lead_byte_check(UINT8 code)
                   4174: {
                   4175:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   4176:        
                   4177:        for(int i = 0;; i += 2) {
                   4178:                UINT8 lo = dbcs_table[i + 0];
                   4179:                UINT8 hi = dbcs_table[i + 1];
                   4180:                if(lo == 0 && hi == 0) {
                   4181:                        break;
                   4182:                }
                   4183:                if(lo <= code && code <= hi) {
                   4184:                        return(1);
                   4185:                }
                   4186:        }
                   4187:        return(0);
                   4188: }
                   4189: 
1.1.1.20  root     4190: int msdos_ctrl_code_check(UINT8 code)
                   4191: {
1.1.1.22  root     4192:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     4193: }
                   4194: 
1.1.1.36  root     4195: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   4196: {
                   4197:        int is_kanji_1st = 0;
                   4198:        int is_kanji_2nd = 0;
                   4199:        
                   4200:        for(int p = 0;; p++) {
                   4201:                if(is_kanji_1st) {
                   4202:                        is_kanji_1st = 0;
                   4203:                        is_kanji_2nd = 1;
                   4204:                } else if(msdos_lead_byte_check(buf[p])) {
                   4205:                        is_kanji_1st = 1;
                   4206:                }
                   4207:                if(p == n) {
                   4208:                        return(is_kanji_2nd);
                   4209:                }
                   4210:                is_kanji_2nd = 0;
                   4211:        }
                   4212: }
                   4213: 
1.1       root     4214: // file control
                   4215: 
1.1.1.45  root     4216: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4217: {
                   4218:        static char tmp[MAX_PATH];
                   4219:        
                   4220:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4221:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4222:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4223:        } else {
                   4224:                strcpy(tmp, path);
                   4225:        }
                   4226:        return(tmp);
                   4227: }
                   4228: 
1.1.1.45  root     4229: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4230: {
                   4231:        static char tmp[MAX_PATH];
                   4232:        
                   4233:        strcpy(tmp, path);
1.1.1.45  root     4234:        
                   4235:        // for example "C:\" case, the end separator should not be removed
                   4236:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4237:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4238:        }
                   4239:        return(tmp);
                   4240: }
                   4241: 
1.1.1.45  root     4242: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4243: {
                   4244:        static char tmp[MAX_PATH];
1.1.1.45  root     4245:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4246:        
                   4247:        if(strlen(tmp_dir) == 0) {
                   4248:                strcpy(tmp, file);
                   4249:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4250:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4251:        } else {
                   4252:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4253:        }
                   4254:        return(tmp);
                   4255: }
                   4256: 
1.1.1.45  root     4257: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4258: {
                   4259:        static char tmp[MAX_PATH];
                   4260:        
                   4261:        if(lfn) {
                   4262:                strcpy(tmp, path);
                   4263:        } else {
                   4264:                // remove space in the path
1.1.1.45  root     4265:                const char *src = path;
                   4266:                char *dst = tmp;
1.1       root     4267:                
                   4268:                while(*src != '\0') {
                   4269:                        if(msdos_lead_byte_check(*src)) {
                   4270:                                *dst++ = *src++;
                   4271:                                *dst++ = *src++;
                   4272:                        } else if(*src != ' ') {
                   4273:                                *dst++ = *src++;
                   4274:                        } else {
                   4275:                                src++;  // skip space
                   4276:                        }
                   4277:                }
                   4278:                *dst = '\0';
                   4279:        }
1.1.1.14  root     4280:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4281:                // redirect C:\COMMAND.COM to comspec_path
                   4282:                strcpy(tmp, comspec_path);
                   4283:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4284:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4285:                static int root_drive_protected = -1;
                   4286:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4287:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4288:                
1.1.1.60  root     4289:                if(GetFullPathNameA(tmp, MAX_PATH, temp, &name_temp) != 0 &&
1.1.1.14  root     4290:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4291:                        strcpy(name, name_temp);
                   4292:                        name_temp[0] = '\0';
                   4293:                        
                   4294:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4295:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4296:                                if(root_drive_protected == -1) {
                   4297:                                        FILE *fp = NULL;
                   4298:                                        
                   4299:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4300:                                        root_drive_protected = 1;
                   4301:                                        try {
                   4302:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4303:                                                        if(fprintf(fp, "TEST") == 4) {
                   4304:                                                                root_drive_protected = 0;
                   4305:                                                        }
                   4306:                                                }
                   4307:                                        } catch(...) {
                   4308:                                        }
                   4309:                                        if(fp != NULL) {
                   4310:                                                fclose(fp);
                   4311:                                        }
                   4312:                                        if(_access(temp, 0) == 0) {
                   4313:                                                remove(temp);
                   4314:                                        }
                   4315:                                }
                   4316:                                if(root_drive_protected == 1) {
1.1.1.60  root     4317:                                        if(GetEnvironmentVariableA("TEMP", temp, MAX_PATH) != 0 ||
                   4318:                                           GetEnvironmentVariableA("TMP",  temp, MAX_PATH) != 0) {
1.1.1.14  root     4319:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4320:                                        }
                   4321:                                }
                   4322:                        }
                   4323:                }
                   4324:        }
1.1       root     4325:        return(tmp);
                   4326: }
                   4327: 
1.1.1.45  root     4328: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4329: {
1.1.1.32  root     4330:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4331:        static char env_path[ENV_SIZE];
                   4332:        char tmp[ENV_SIZE], *token;
                   4333:        
                   4334:        memset(env_path, 0, sizeof(env_path));
                   4335:        strcpy(tmp, src);
                   4336:        token = my_strtok(tmp, ";");
                   4337:        
                   4338:        while(token != NULL) {
                   4339:                if(token[0] != '\0') {
1.1.1.45  root     4340:                        const char *path = msdos_remove_double_quote(token);
                   4341:                        char short_path[MAX_PATH];
1.1.1.32  root     4342:                        if(path != NULL && strlen(path) != 0) {
                   4343:                                if(env_path[0] != '\0') {
                   4344:                                        strcat(env_path, ";");
                   4345:                                }
1.1.1.60  root     4346:                                if(GetShortPathNameA(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4347:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4348:                                } else {
                   4349:                                        my_strupr(short_path);
1.1.1.32  root     4350:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4351:                                }
                   4352:                        }
                   4353:                }
                   4354:                token = my_strtok(NULL, ";");
                   4355:        }
                   4356:        return(env_path);
                   4357: }
                   4358: 
1.1.1.45  root     4359: bool match(const char *text, const char *pattern)
1.1       root     4360: {
1.1.1.24  root     4361:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4362:        switch(*pattern) {
1.1       root     4363:        case '\0':
                   4364:                return !*text;
                   4365:        case '*':
1.1.1.14  root     4366:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4367:        case '?':
                   4368:                return *text && match(text + 1, pattern + 1);
                   4369:        default:
                   4370:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4371:        }
                   4372: }
                   4373: 
1.1.1.45  root     4374: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4375: {
1.1.1.45  root     4376:        const char *p = NULL;
1.1       root     4377:        
1.1.1.14  root     4378:        if(!*volume) {
                   4379:                return false;
                   4380:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4381:                return msdos_match_volume_label(p + 1, volume);
                   4382:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4383:                return msdos_match_volume_label(p + 1, volume);
                   4384:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4385:                char tmp[MAX_PATH];
                   4386:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4387:                return match(volume, tmp);
1.1       root     4388:        } else {
                   4389:                return match(volume, path);
                   4390:        }
                   4391: }
                   4392: 
1.1.1.45  root     4393: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4394: {
                   4395:        static char tmp[MAX_PATH];
                   4396:        char name[9], ext[4];
                   4397:        
                   4398:        memset(name, 0, sizeof(name));
                   4399:        memcpy(name, fcb->file_name, 8);
                   4400:        strcpy(name, msdos_trimmed_path(name, 0));
                   4401:        
                   4402:        memset(ext, 0, sizeof(ext));
                   4403:        memcpy(ext, fcb->file_name + 8, 3);
                   4404:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4405:        
                   4406:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4407:                strcpy(name, "*");
                   4408:        }
                   4409:        if(ext[0] == '\0') {
                   4410:                strcpy(tmp, name);
                   4411:        } else {
                   4412:                if(strcmp(ext, "???") == 0) {
                   4413:                        strcpy(ext, "*");
                   4414:                }
                   4415:                sprintf(tmp, "%s.%s", name, ext);
                   4416:        }
                   4417:        return(tmp);
                   4418: }
                   4419: 
1.1.1.45  root     4420: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4421: {
1.1.1.60  root     4422:        char tmp[MAX_PATH];
                   4423:        strcpy(tmp, path);
                   4424:        char *ext = my_strchr(tmp, '.');
1.1       root     4425:        
                   4426:        memset(fcb->file_name, 0x20, 8 + 3);
1.1.1.60  root     4427:        if(ext != NULL && tmp[0] != '.') {
1.1       root     4428:                *ext = '\0';
                   4429:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4430:        }
1.1.1.60  root     4431:        memcpy(fcb->file_name, tmp, strlen(tmp));
1.1       root     4432: }
                   4433: 
1.1.1.45  root     4434: const char *msdos_short_path(const char *path)
1.1       root     4435: {
                   4436:        static char tmp[MAX_PATH];
                   4437:        
1.1.1.60  root     4438:        if(GetShortPathNameA(path, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4439:                strcpy(tmp, path);
                   4440:        }
1.1       root     4441:        my_strupr(tmp);
                   4442:        return(tmp);
                   4443: }
                   4444: 
1.1.1.60  root     4445: const char *msdos_short_name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     4446: {
                   4447:        static char tmp[MAX_PATH];
1.1.1.45  root     4448:        
1.1.1.14  root     4449:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4450:                strcpy(tmp, fd->cAlternateFileName);
                   4451:        } else {
                   4452:                strcpy(tmp, fd->cFileName);
                   4453:        }
                   4454:        my_strupr(tmp);
                   4455:        return(tmp);
                   4456: }
                   4457: 
1.1.1.45  root     4458: const char *msdos_short_full_path(const char *path)
1.1       root     4459: {
                   4460:        static char tmp[MAX_PATH];
                   4461:        char full[MAX_PATH], *name;
                   4462:        
1.1.1.14  root     4463:        // Full works with non-existent files, but Short does not
1.1.1.60  root     4464:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1.1.14  root     4465:        *tmp = '\0';
1.1.1.60  root     4466:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0 && name > path) {
1.1.1.14  root     4467:                name[-1] = '\0';
1.1.1.60  root     4468:                DWORD len = GetShortPathNameA(full, tmp, MAX_PATH);
1.1.1.14  root     4469:                if(len == 0) {
                   4470:                        strcpy(tmp, full);
                   4471:                } else {
                   4472:                        tmp[len++] = '\\';
                   4473:                        strcpy(tmp + len, name);
                   4474:                }
                   4475:        }
1.1       root     4476:        my_strupr(tmp);
                   4477:        return(tmp);
                   4478: }
                   4479: 
1.1.1.45  root     4480: const char *msdos_short_full_dir(const char *path)
1.1       root     4481: {
                   4482:        static char tmp[MAX_PATH];
                   4483:        char full[MAX_PATH], *name;
                   4484:        
1.1.1.60  root     4485:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     4486:        name[-1] = '\0';
1.1.1.60  root     4487:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4488:                strcpy(tmp, full);
                   4489:        }
1.1       root     4490:        my_strupr(tmp);
                   4491:        return(tmp);
                   4492: }
                   4493: 
1.1.1.45  root     4494: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4495: {
1.1.1.45  root     4496:        static char trimmed[MAX_PATH];
                   4497:        
                   4498:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4499: #if 0
                   4500:        // I have forgotten the reason of this routine... :-(
1.1       root     4501:        if(_access(trimmed, 0) != 0) {
                   4502:                process_t *process = msdos_process_info_get(current_psp);
                   4503:                static char tmp[MAX_PATH];
                   4504:                
                   4505:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4506:                if(_access(tmp, 0) == 0) {
                   4507:                        return(tmp);
                   4508:                }
                   4509:        }
1.1.1.14  root     4510: #endif
1.1       root     4511:        return(trimmed);
                   4512: }
                   4513: 
1.1.1.45  root     4514: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4515: {
                   4516:        char full[MAX_PATH], *name;
                   4517:        
1.1.1.60  root     4518:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4519:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4520:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4521:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4522:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4523:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4524:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4525:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4526:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4527:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4528:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4529:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4530:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4531:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4532:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4533:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4534:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4535:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4536:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4537:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4538:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4539:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4540:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4541:                        return(true);
                   4542:                } else if(name != NULL) {
                   4543:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4544:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4545:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4546: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4547:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4548:                                return(true);
                   4549:                        }
                   4550:                }
1.1.1.24  root     4551:        }
                   4552:        return(false);
1.1.1.11  root     4553: }
                   4554: 
1.1.1.45  root     4555: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4556: {
1.1.1.14  root     4557:        char full[MAX_PATH], *name;
1.1.1.8   root     4558:        
1.1.1.60  root     4559:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4560:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4561:        }
                   4562:        return(false);
                   4563: }
                   4564: 
1.1.1.45  root     4565: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4566: {
                   4567:        char full[MAX_PATH], *name;
                   4568:        
1.1.1.60  root     4569:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4570:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4571:                        return(1);
                   4572:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4573:                        return(2);
                   4574:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4575:                        return(3);
                   4576:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4577:                        return(4);
1.1.1.24  root     4578:                }
                   4579:        }
1.1.1.29  root     4580:        return(0);
                   4581: }
                   4582: 
1.1.1.45  root     4583: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4584: {
                   4585:        // COM1:{110,150,300,600,1200,2400,4800,9600},{N,O,E,M,S},{8,7,6,5},{1,1.5,2}
1.1.1.45  root     4586:        const char *p = NULL;
1.1.1.37  root     4587:        
                   4588:        if((p = strstr(path, ":")) != NULL) {
                   4589:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4590:                
                   4591:                // baud rate
                   4592:                int baud = max(110, min(9600, atoi(p + 1)));
                   4593:                UINT16 divisor = 115200 / baud;
                   4594:                
                   4595:                if((p = strstr(p + 1, ",")) != NULL) {
                   4596:                        // parity
                   4597:                        if(p[1] == 'N' || p[1] == 'n') {
                   4598:                                selector = (selector & ~0x38) | 0x00;
                   4599:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4600:                                selector = (selector & ~0x38) | 0x08;
                   4601:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4602:                                selector = (selector & ~0x38) | 0x18;
                   4603:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4604:                                selector = (selector & ~0x38) | 0x28;
                   4605:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4606:                                selector = (selector & ~0x38) | 0x38;
                   4607:                        }
                   4608:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4609:                                // word length
                   4610:                                if(p[1] == '8') {
                   4611:                                        selector = (selector & ~0x03) | 0x03;
                   4612:                                } else if(p[1] == '7') {
                   4613:                                        selector = (selector & ~0x03) | 0x02;
                   4614:                                } else if(p[1] == '6') {
                   4615:                                        selector = (selector & ~0x03) | 0x01;
                   4616:                                } else if(p[1] == '5') {
                   4617:                                        selector = (selector & ~0x03) | 0x00;
                   4618:                                }
                   4619:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4620:                                        // stop bits
                   4621:                                        float bits = atof(p + 1);
                   4622:                                        if(bits > 1.0F) {
                   4623:                                                selector |= 0x04;
                   4624:                                        } else {
                   4625:                                                selector &= ~0x04;
                   4626:                                        }
                   4627:                                }
                   4628:                        }
                   4629:                }
                   4630:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4631:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4632:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4633:                sio_write(sio_port - 1, 3, selector);
                   4634:        }
                   4635: }
                   4636: 
1.1.1.45  root     4637: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4638: {
                   4639:        char full[MAX_PATH], *name;
                   4640:        
1.1.1.60  root     4641:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.30  root     4642:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4643:                        return(1);
                   4644:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4645:                        return(1);
                   4646:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4647:                        return(2);
                   4648:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4649:                        return(3);
                   4650:                }
                   4651:        }
                   4652:        return(0);
                   4653: }
                   4654: 
1.1.1.44  root     4655: bool msdos_is_valid_drive(int drv)
                   4656: {
                   4657:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4658: }
                   4659: 
                   4660: bool msdos_is_removable_drive(int drv)
                   4661: {
                   4662:        char volume[] = "A:\\";
                   4663:        
                   4664:        volume[0] = 'A' + drv;
                   4665:        
1.1.1.60  root     4666:        return(GetDriveTypeA(volume) == DRIVE_REMOVABLE);
1.1.1.44  root     4667: }
                   4668: 
                   4669: bool msdos_is_cdrom_drive(int drv)
                   4670: {
                   4671:        char volume[] = "A:\\";
                   4672:        
                   4673:        volume[0] = 'A' + drv;
                   4674:        
1.1.1.60  root     4675:        return(GetDriveTypeA(volume) == DRIVE_CDROM);
1.1.1.44  root     4676: }
                   4677: 
                   4678: bool msdos_is_remote_drive(int drv)
                   4679: {
                   4680:        char volume[] = "A:\\";
                   4681:        
                   4682:        volume[0] = 'A' + drv;
                   4683:        
1.1.1.60  root     4684:        return(GetDriveTypeA(volume) == DRIVE_REMOTE);
1.1.1.44  root     4685: }
                   4686: 
                   4687: bool msdos_is_subst_drive(int drv)
                   4688: {
                   4689:        char device[] = "A:", path[MAX_PATH];
                   4690:        
                   4691:        device[0] = 'A' + drv;
                   4692:        
1.1.1.60  root     4693:        if(QueryDosDeviceA(device, path, MAX_PATH)) {
1.1.1.44  root     4694:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4695:                        return(true);
                   4696:                }
                   4697:        }
                   4698:        return(false);
                   4699: }
                   4700: 
1.1.1.45  root     4701: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4702: {
                   4703:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
1.1.1.60  root     4704:        WIN32_FIND_DATAA fd;
1.1.1.24  root     4705:        HANDLE hFind;
                   4706:        
1.1.1.60  root     4707:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.24  root     4708:                FindClose(hFind);
1.1.1.60  root     4709:                return(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
1.1.1.24  root     4710:        }
                   4711:        return(false);
1.1.1.8   root     4712: }
                   4713: 
1.1.1.45  root     4714: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4715: {
                   4716:        static char tmp[MAX_PATH];
1.1.1.28  root     4717:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4718:        
1.1.1.28  root     4719:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.60  root     4720:        if(GetFullPathNameA(command_path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4721:                sprintf(file_name, "COMMAND.COM");
                   4722:                if(_access(tmp, 0) == 0) {
                   4723:                        return(tmp);
                   4724:                }
                   4725:        }
1.1.1.28  root     4726:        
                   4727:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.60  root     4728:        if(GetModuleFileNameA(NULL, path, MAX_PATH) != 0 && GetFullPathNameA(path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4729:                sprintf(file_name, "COMMAND.COM");
                   4730:                if(_access(tmp, 0) == 0) {
                   4731:                        return(tmp);
                   4732:                }
                   4733:        }
1.1.1.28  root     4734:        
                   4735:        // check if COMMAND.COM is in the current directory
1.1.1.60  root     4736:        if(GetFullPathNameA("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4737:                if(_access(tmp, 0) == 0) {
                   4738:                        return(tmp);
                   4739:                }
                   4740:        }
1.1.1.28  root     4741:        
                   4742:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4743:        strcpy(path, env_path);
                   4744:        char *token = my_strtok(path, ";");
1.1.1.9   root     4745:        while(token != NULL) {
1.1.1.14  root     4746:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4747:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4748:                        if(_access(tmp, 0) == 0) {
                   4749:                                return(tmp);
                   4750:                        }
                   4751:                }
                   4752:                token = my_strtok(NULL, ";");
                   4753:        }
                   4754:        return(NULL);
                   4755: }
                   4756: 
1.1.1.14  root     4757: int msdos_drive_number(const char *path)
1.1       root     4758: {
                   4759:        char tmp[MAX_PATH], *name;
                   4760:        
1.1.1.60  root     4761:        if(GetFullPathNameA(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
1.1.1.45  root     4762:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4763:                        return(tmp[0] - 'a');
                   4764:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4765:                        return(tmp[0] - 'A');
                   4766:                }
1.1       root     4767:        }
1.1.1.45  root     4768: //     return(msdos_drive_number("."));
                   4769:        return(_getdrive() - 1);
1.1       root     4770: }
                   4771: 
1.1.1.45  root     4772: const char *msdos_volume_label(const char *path)
1.1       root     4773: {
                   4774:        static char tmp[MAX_PATH];
                   4775:        char volume[] = "A:\\";
                   4776:        
                   4777:        if(path[1] == ':') {
                   4778:                volume[0] = path[0];
                   4779:        } else {
                   4780:                volume[0] = 'A' + _getdrive() - 1;
                   4781:        }
1.1.1.60  root     4782:        if(!GetVolumeInformationA(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
1.1       root     4783:                memset(tmp, 0, sizeof(tmp));
                   4784:        }
                   4785:        return(tmp);
                   4786: }
                   4787: 
1.1.1.45  root     4788: const char *msdos_short_volume_label(const char *label)
1.1       root     4789: {
                   4790:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4791:        const char *src = label;
1.1       root     4792:        int remain = strlen(label);
                   4793:        char *dst_n = tmp;
                   4794:        char *dst_e = tmp + 9;
                   4795:        
                   4796:        strcpy(tmp, "        .   ");
                   4797:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4798:                if(msdos_lead_byte_check(*src)) {
                   4799:                        if(++i == 8) {
                   4800:                                break;
                   4801:                        }
                   4802:                        *dst_n++ = *src++;
                   4803:                        remain--;
                   4804:                }
                   4805:                *dst_n++ = *src++;
                   4806:                remain--;
                   4807:        }
                   4808:        if(remain > 0) {
                   4809:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4810:                        if(msdos_lead_byte_check(*src)) {
                   4811:                                if(++i == 3) {
                   4812:                                        break;
                   4813:                                }
                   4814:                                *dst_e++ = *src++;
                   4815:                                remain--;
                   4816:                        }
                   4817:                        *dst_e++ = *src++;
                   4818:                        remain--;
                   4819:                }
                   4820:                *dst_e = '\0';
                   4821:        } else {
                   4822:                *dst_n = '\0';
                   4823:        }
                   4824:        my_strupr(tmp);
                   4825:        return(tmp);
                   4826: }
                   4827: 
1.1.1.13  root     4828: errno_t msdos_maperr(unsigned long oserrno)
                   4829: {
                   4830:        _doserrno = oserrno;
1.1.1.14  root     4831:        switch(oserrno) {
1.1.1.13  root     4832:        case ERROR_FILE_NOT_FOUND:         // 2
                   4833:        case ERROR_PATH_NOT_FOUND:         // 3
                   4834:        case ERROR_INVALID_DRIVE:          // 15
                   4835:        case ERROR_NO_MORE_FILES:          // 18
                   4836:        case ERROR_BAD_NETPATH:            // 53
                   4837:        case ERROR_BAD_NET_NAME:           // 67
                   4838:        case ERROR_BAD_PATHNAME:           // 161
                   4839:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4840:                return ENOENT;
                   4841:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4842:                return EMFILE;
                   4843:        case ERROR_ACCESS_DENIED:          // 5
                   4844:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4845:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4846:        case ERROR_CANNOT_MAKE:            // 82
                   4847:        case ERROR_FAIL_I24:               // 83
                   4848:        case ERROR_DRIVE_LOCKED:           // 108
                   4849:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4850:        case ERROR_NOT_LOCKED:             // 158
                   4851:        case ERROR_LOCK_FAILED:            // 167
                   4852:                return EACCES;
                   4853:        case ERROR_INVALID_HANDLE:         // 6
                   4854:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4855:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4856:                return EBADF;
                   4857:        case ERROR_ARENA_TRASHED:          // 7
                   4858:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4859:        case ERROR_INVALID_BLOCK:          // 9
                   4860:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4861:                return ENOMEM;
                   4862:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4863:                return E2BIG;
                   4864:        case ERROR_BAD_FORMAT:             // 11
                   4865:                return ENOEXEC;
                   4866:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4867:                return EXDEV;
                   4868:        case ERROR_FILE_EXISTS:            // 80
                   4869:        case ERROR_ALREADY_EXISTS:         // 183
                   4870:                return EEXIST;
                   4871:        case ERROR_NO_PROC_SLOTS:          // 89
                   4872:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4873:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4874:                return EAGAIN;
                   4875:        case ERROR_BROKEN_PIPE:            // 109
                   4876:                return EPIPE;
                   4877:        case ERROR_DISK_FULL:              // 112
                   4878:                return ENOSPC;
                   4879:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4880:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4881:                return ECHILD;
                   4882:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4883:                return ENOTEMPTY;
                   4884:        }
1.1.1.14  root     4885:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4886:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4887:                return EACCES;
                   4888:        }
1.1.1.14  root     4889:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4890:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4891:                return ENOEXEC;
                   4892:        }
                   4893:        return EINVAL;
                   4894: }
                   4895: 
1.1.1.45  root     4896: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4897: {
1.1.1.14  root     4898:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4899:                return(_open(path, oflag));
1.1.1.13  root     4900:        }
1.1.1.14  root     4901:        
                   4902:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4903:        DWORD disposition;
1.1.1.14  root     4904:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4905:        default:
1.1.1.13  root     4906:        case _O_EXCL:
                   4907:                disposition = OPEN_EXISTING;
                   4908:                break;
                   4909:        case _O_CREAT:
                   4910:                disposition = OPEN_ALWAYS;
                   4911:                break;
                   4912:        case _O_CREAT | _O_EXCL:
                   4913:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4914:                disposition = CREATE_NEW;
                   4915:                break;
                   4916:        case _O_TRUNC:
                   4917:        case _O_TRUNC | _O_EXCL:
                   4918:                disposition = TRUNCATE_EXISTING;
                   4919:                break;
                   4920:        case _O_CREAT | _O_TRUNC:
                   4921:                disposition = CREATE_ALWAYS;
                   4922:                break;
                   4923:        }
1.1.1.14  root     4924:        
1.1.1.60  root     4925:        HANDLE h = CreateFileA(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4926:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4927:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4928:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4929:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4930:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.60  root     4931:                h = CreateFileA(path, GENERIC_READ,
1.1.1.13  root     4932:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4933:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4934:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4935:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4936:                        return(-1);
1.1.1.13  root     4937:                }
                   4938:        }
1.1.1.14  root     4939:        
1.1.1.13  root     4940:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4941:        if(fd == -1) {
1.1.1.13  root     4942:                CloseHandle(h);
                   4943:        }
1.1.1.45  root     4944:        return(fd);
                   4945: }
                   4946: 
                   4947: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4948: {
                   4949:        int fd = -1;
                   4950:        
                   4951:        *sio_port = *lpt_port = 0;
                   4952:        
                   4953:        if(msdos_is_con_path(path)) {
                   4954:                // MODE.COM opens CON device with read/write mode :-(
                   4955:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4956:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4957:                        oflag |= _O_RDONLY;
                   4958:                }
                   4959:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4960: //                     fd = msdos_open("NUL", oflag);
                   4961:                }
                   4962:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4963:                fd = msdos_open("NUL", oflag);
                   4964:                msdos_set_comm_params(*sio_port, path);
                   4965:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4966:                fd = msdos_open("NUL", oflag);
                   4967:        } else if(msdos_is_device_path(path)) {
                   4968:                fd = msdos_open("NUL", oflag);
                   4969: //     } else if(oflag & _O_CREAT) {
                   4970: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4971: //     } else {
                   4972: //             fd = _open(path, oflag);
                   4973:        }
                   4974:        return(fd);
                   4975: }
                   4976: 
                   4977: UINT16 msdos_device_info(const char *path)
                   4978: {
                   4979:        if(msdos_is_con_path(path)) {
                   4980:                return(0x80d3);
                   4981:        } else if(msdos_is_comm_path(path)) {
                   4982:                return(0x80a0);
                   4983:        } else if(msdos_is_prn_path(path)) {
                   4984: //             return(0xa8c0);
                   4985:                return(0x80a0);
                   4986:        } else if(msdos_is_device_path(path)) {
                   4987:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4988:                        return(0xc0c0);
                   4989:                } else if(strstr(path, "MSCD001") != NULL) {
                   4990:                        return(0xc880);
                   4991:                } else {
                   4992:                        return(0x8084);
                   4993:                }
                   4994:        } else {
                   4995:                return(msdos_drive_number(path));
                   4996:        }
1.1.1.13  root     4997: }
                   4998: 
1.1.1.52  root     4999: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg, int sio_port = 0, int lpt_port = 0)
1.1       root     5000: {
                   5001:        static int id = 0;
                   5002:        char full[MAX_PATH], *name;
                   5003:        
1.1.1.60  root     5004:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1       root     5005:                strcpy(file_handler[fd].path, full);
                   5006:        } else {
                   5007:                strcpy(file_handler[fd].path, path);
                   5008:        }
1.1.1.14  root     5009:        // isatty makes no distinction between CON & NUL
                   5010:        // GetFileSize fails on CON, succeeds on NUL
                   5011:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     5012:                if(info == 0x80d3) {
                   5013:                        info = 0x8084;
                   5014:                }
1.1.1.14  root     5015:                atty = 0;
                   5016:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     5017: //             info = msdos_drive_number(".");
                   5018:                info = msdos_drive_number(path);
1.1.1.14  root     5019:        }
1.1       root     5020:        file_handler[fd].valid = 1;
                   5021:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     5022:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     5023:        file_handler[fd].mode = mode;
                   5024:        file_handler[fd].info = info;
                   5025:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     5026:        file_handler[fd].sio_port = sio_port;
                   5027:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     5028:        
                   5029:        // init system file table
                   5030:        if(fd < 20) {
                   5031:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   5032:                
                   5033:                memset(sft, 0, 0x3b);
                   5034:                
                   5035:                *(UINT16 *)(sft + 0x00) = 1;
                   5036:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
1.1.1.60  root     5037:                *(UINT8  *)(sft + 0x04) = GetFileAttributesA(file_handler[fd].path) & 0xff;
1.1.1.21  root     5038:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   5039:                
                   5040:                if(!(file_handler[fd].info & 0x80)) {
                   5041:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   5042:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   5043:                        
                   5044:                        FILETIME time, local;
                   5045:                        HANDLE hHandle;
                   5046:                        WORD dos_date = 0, dos_time = 0;
                   5047:                        DWORD file_size = 0;
                   5048:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   5049:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   5050:                                        FileTimeToLocalFileTime(&time, &local);
                   5051:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   5052:                                }
                   5053:                                file_size = GetFileSize(hHandle, NULL);
                   5054:                        }
                   5055:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   5056:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   5057:                        *(UINT32 *)(sft + 0x11) = file_size;
                   5058:                }
                   5059:                
                   5060:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   5061:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   5062:                my_strupr(fname);
                   5063:                my_strupr(ext);
                   5064:                memset(sft + 0x20, 0x20, 11);
                   5065:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   5066:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   5067:                
                   5068:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   5069:        }
1.1       root     5070: }
                   5071: 
                   5072: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   5073: {
                   5074:        strcpy(file_handler[dst].path, file_handler[src].path);
                   5075:        file_handler[dst].valid = 1;
                   5076:        file_handler[dst].id = file_handler[src].id;
                   5077:        file_handler[dst].atty = file_handler[src].atty;
                   5078:        file_handler[dst].mode = file_handler[src].mode;
                   5079:        file_handler[dst].info = file_handler[src].info;
                   5080:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     5081:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   5082:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     5083: }
                   5084: 
1.1.1.20  root     5085: void msdos_file_handler_close(int fd)
1.1       root     5086: {
                   5087:        file_handler[fd].valid = 0;
1.1.1.21  root     5088:        
                   5089:        if(fd < 20) {
                   5090:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   5091:        }
1.1       root     5092: }
                   5093: 
1.1.1.14  root     5094: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     5095: {
1.1.1.14  root     5096:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   5097:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   5098:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     5099: }
                   5100: 
                   5101: // find file
                   5102: 
                   5103: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   5104: {
                   5105:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5106:                return(0);      // search directory only !!!
                   5107:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   5108:                return(0);
                   5109:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   5110:                return(0);
                   5111:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5112:                return(0);
                   5113:        } else if((attribute & required_mask) != required_mask) {
                   5114:                return(0);
                   5115:        } else {
                   5116:                return(1);
                   5117:        }
                   5118: }
                   5119: 
1.1.1.60  root     5120: int msdos_find_file_has_8dot3name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     5121: {
1.1.1.14  root     5122:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     5123:                return(1);
1.1.1.13  root     5124:        }
                   5125:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     5126:        if(len > 12) {
1.1.1.42  root     5127:                return(0);
1.1.1.13  root     5128:        }
                   5129:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     5130:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     5131:                return(0);
1.1.1.13  root     5132:        }
1.1.1.42  root     5133:        return(1);
1.1.1.13  root     5134: }
                   5135: 
1.1.1.60  root     5136: void msdos_find_file_conv_local_time(WIN32_FIND_DATAA *fd)
1.1       root     5137: {
                   5138:        FILETIME local;
                   5139:        
                   5140:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   5141:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   5142:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   5143:        
                   5144:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   5145:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   5146:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   5147:        
                   5148:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   5149:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   5150:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   5151: }
                   5152: 
                   5153: // i/o
                   5154: 
                   5155: void msdos_stdio_reopen()
                   5156: {
                   5157:        if(!file_handler[0].valid) {
                   5158:                _dup2(DUP_STDIN, 0);
                   5159:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   5160:        }
                   5161:        if(!file_handler[1].valid) {
                   5162:                _dup2(DUP_STDOUT, 1);
                   5163:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   5164:        }
                   5165:        if(!file_handler[2].valid) {
                   5166:                _dup2(DUP_STDERR, 2);
                   5167:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   5168:        }
1.1.1.21  root     5169:        if(!file_handler[3].valid) {
                   5170:                _dup2(DUP_STDAUX, 3);
                   5171:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   5172:        }
                   5173:        if(!file_handler[4].valid) {
                   5174:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     5175: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   5176:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     5177:        }
                   5178:        for(int i = 0; i < 5; i++) {
                   5179:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   5180:                        msdos_psp_set_file_table(i, i, current_psp);
                   5181:                }
                   5182:        }
1.1       root     5183: }
                   5184: 
1.1.1.37  root     5185: int msdos_read(int fd, void *buffer, unsigned int count)
                   5186: {
                   5187:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5188:                // read from serial port
                   5189:                int read = 0;
                   5190:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5191:                        UINT8 *buf = (UINT8 *)buffer;
                   5192:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5193:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5194:                        DWORD timeout = timeGetTime() + 1000;
                   5195:                        while(read < count) {
                   5196:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   5197:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   5198:                                        timeout = timeGetTime() + 1000;
                   5199:                                } else {
                   5200:                                        if(timeGetTime() > timeout) {
                   5201:                                                break;
                   5202:                                        }
                   5203:                                        Sleep(10);
1.1.1.37  root     5204:                                }
                   5205:                        }
                   5206:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5207:                }
                   5208:                return(read);
                   5209:        }
                   5210:        return(_read(fd, buffer, count));
                   5211: }
                   5212: 
1.1       root     5213: int msdos_kbhit()
                   5214: {
                   5215:        msdos_stdio_reopen();
                   5216:        
1.1.1.20  root     5217:        process_t *process = msdos_process_info_get(current_psp);
                   5218:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5219:        
                   5220:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5221:                // stdin is redirected to file
1.1.1.20  root     5222:                return(eof(fd) == 0);
1.1       root     5223:        }
                   5224:        
                   5225:        // check keyboard status
1.1.1.35  root     5226:        if(key_recv != 0) {
1.1       root     5227:                return(1);
                   5228:        }
1.1.1.35  root     5229:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5230: #ifdef USE_SERVICE_THREAD
                   5231:                EnterCriticalSection(&key_buf_crit_sect);
                   5232: #endif
1.1.1.55  root     5233:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5234: #ifdef USE_SERVICE_THREAD
                   5235:                LeaveCriticalSection(&key_buf_crit_sect);
                   5236: #endif
                   5237:                if(!empty) return(1);
                   5238:        }
                   5239:        return(_kbhit());
1.1       root     5240: }
                   5241: 
                   5242: int msdos_getch_ex(int echo)
                   5243: {
                   5244:        static char prev = 0;
                   5245:        
                   5246:        msdos_stdio_reopen();
                   5247:        
1.1.1.20  root     5248:        process_t *process = msdos_process_info_get(current_psp);
                   5249:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5250:        
                   5251:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5252:                // stdin is redirected to file
                   5253: retry:
                   5254:                char data;
1.1.1.37  root     5255:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5256:                        char tmp = data;
                   5257:                        if(data == 0x0a) {
                   5258:                                if(prev == 0x0d) {
                   5259:                                        goto retry; // CRLF -> skip LF
                   5260:                                } else {
                   5261:                                        data = 0x0d; // LF only -> CR
                   5262:                                }
                   5263:                        }
                   5264:                        prev = tmp;
                   5265:                        return(data);
                   5266:                }
                   5267:                return(EOF);
                   5268:        }
                   5269:        
                   5270:        // input from console
1.1.1.5   root     5271:        int key_char, key_scan;
1.1.1.33  root     5272:        if(key_recv != 0) {
1.1.1.5   root     5273:                key_char = (key_code >> 0) & 0xff;
                   5274:                key_scan = (key_code >> 8) & 0xff;
                   5275:                key_code >>= 16;
1.1.1.33  root     5276:                key_recv >>= 16;
1.1.1.5   root     5277:        } else {
1.1.1.54  root     5278:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5279:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5280: #ifdef USE_SERVICE_THREAD
                   5281:                                EnterCriticalSection(&key_buf_crit_sect);
                   5282: #endif
1.1.1.55  root     5283:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5284: #ifdef USE_SERVICE_THREAD
                   5285:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5286: #endif
                   5287:                                if(!empty) break;
                   5288:                        }
1.1.1.23  root     5289:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5290:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5291:                                if(_kbhit()) {
1.1.1.32  root     5292:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5293: #ifdef USE_SERVICE_THREAD
                   5294:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5295: #endif
1.1.1.51  root     5296:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5297: #ifdef USE_SERVICE_THREAD
                   5298:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5299: #endif
1.1.1.32  root     5300:                                        }
1.1.1.23  root     5301:                                } else {
                   5302:                                        Sleep(10);
                   5303:                                }
                   5304:                        } else {
                   5305:                                if(!update_key_buffer()) {
                   5306:                                        Sleep(10);
                   5307:                                }
1.1.1.14  root     5308:                        }
                   5309:                }
1.1.1.54  root     5310:                if(m_exit) {
1.1.1.33  root     5311:                        // insert CR to terminate input loops
1.1.1.14  root     5312:                        key_char = 0x0d;
                   5313:                        key_scan = 0;
1.1.1.32  root     5314:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5315: #ifdef USE_SERVICE_THREAD
                   5316:                        EnterCriticalSection(&key_buf_crit_sect);
                   5317: #endif
1.1.1.51  root     5318:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5319: #ifdef USE_SERVICE_THREAD
                   5320:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5321: #endif
1.1.1.5   root     5322:                }
1.1       root     5323:        }
                   5324:        if(echo && key_char) {
                   5325:                msdos_putch(key_char);
                   5326:        }
                   5327:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5328: }
                   5329: 
                   5330: inline int msdos_getch()
                   5331: {
                   5332:        return(msdos_getch_ex(0));
                   5333: }
                   5334: 
                   5335: inline int msdos_getche()
                   5336: {
                   5337:        return(msdos_getch_ex(1));
                   5338: }
                   5339: 
                   5340: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5341: {
1.1.1.37  root     5342:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5343:                // write to serial port
1.1.1.38  root     5344:                int written = 0;
1.1.1.37  root     5345:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5346:                        UINT8 *buf = (UINT8 *)buffer;
                   5347:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5348:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5349:                        DWORD timeout = timeGetTime() + 1000;
                   5350:                        while(written < count) {
                   5351:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5352:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5353:                                        timeout = timeGetTime() + 1000;
                   5354:                                } else {
                   5355:                                        if(timeGetTime() > timeout) {
                   5356:                                                break;
                   5357:                                        }
                   5358:                                        Sleep(10);
                   5359:                                }
1.1.1.37  root     5360:                        }
                   5361:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5362:                }
1.1.1.38  root     5363:                return(written);
1.1.1.37  root     5364:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5365:                // write to printer port
                   5366:                UINT8 *buf = (UINT8 *)buffer;
                   5367:                for(unsigned int i = 0; i < count; i++) {
                   5368: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5369:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5370:                }
                   5371:                return(count);
                   5372:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5373:                // CR+LF -> LF
1.1.1.37  root     5374:                static int is_cr = 0;
1.1       root     5375:                UINT8 *buf = (UINT8 *)buffer;
                   5376:                for(unsigned int i = 0; i < count; i++) {
                   5377:                        UINT8 data = buf[i];
                   5378:                        if(is_cr) {
                   5379:                                if(data != 0x0a) {
                   5380:                                        UINT8 tmp = 0x0d;
                   5381:                                        _write(1, &tmp, 1);
                   5382:                                }
                   5383:                                _write(1, &data, 1);
                   5384:                                is_cr = 0;
                   5385:                        } else if(data == 0x0d) {
                   5386:                                is_cr = 1;
                   5387:                        } else {
                   5388:                                _write(1, &data, 1);
                   5389:                        }
                   5390:                }
                   5391:                return(count);
                   5392:        }
1.1.1.14  root     5393:        vram_flush();
1.1       root     5394:        return(_write(fd, buffer, count));
                   5395: }
                   5396: 
                   5397: void msdos_putch(UINT8 data)
1.1.1.50  root     5398: {
                   5399:        msdos_stdio_reopen();
                   5400:        
                   5401:        process_t *process = msdos_process_info_get(current_psp);
                   5402:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5403:        
                   5404:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5405:                // stdout is redirected to file
                   5406:                msdos_write(fd, &data, 1);
                   5407:                return;
                   5408:        }
                   5409:        
                   5410:        // call int 29h ?
1.1.1.58  root     5411:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     5412:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5413:                // int 29h is not hooked, no need to call int 29h
                   5414:                msdos_putch_fast(data);
                   5415: #ifdef USE_SERVICE_THREAD
                   5416:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5417:                // XXX: in usually we should not reach here
                   5418:                // this is called from service thread to echo the input
                   5419:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5420:                msdos_putch_fast(data);
                   5421: #endif
1.1.1.51  root     5422:        } else if(in_service_29h) {
1.1.1.50  root     5423:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5424:                msdos_putch_fast(data);
                   5425:        } else {
                   5426:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5427:                in_service_29h = true;
1.1.1.50  root     5428:                try {
                   5429:                        UINT32 tmp_pc = m_pc;
                   5430:                        UINT16 tmp_ax = REG16(AX);
                   5431:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5432:                        
                   5433:                        // call int 29h routine is at fffc:0027
                   5434:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5435:                        REG8(AL) = data;
                   5436:                        
                   5437:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5438:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5439:                                try {
                   5440:                                        hardware_run_cpu();
                   5441:                                } catch(...) {
                   5442:                                }
                   5443:                        }
                   5444:                        REG16(AX) = tmp_ax;
                   5445:                        REG16(BX) = tmp_bx;
                   5446:                } catch(...) {
                   5447:                }
1.1.1.51  root     5448:                in_service_29h = false;
1.1.1.50  root     5449:        }
                   5450: }
                   5451: 
                   5452: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5453: #ifdef USE_SERVICE_THREAD
                   5454: {
                   5455:        EnterCriticalSection(&putch_crit_sect);
                   5456:        msdos_putch_tmp(data);
                   5457:        LeaveCriticalSection(&putch_crit_sect);
                   5458: }
                   5459: void msdos_putch_tmp(UINT8 data)
                   5460: #endif
1.1       root     5461: {
1.1.1.34  root     5462:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5463:        SMALL_RECT rect;
                   5464:        COORD co;
1.1       root     5465:        static int p = 0;
                   5466:        static int is_kanji = 0;
                   5467:        static int is_esc = 0;
                   5468:        static int stored_x;
                   5469:        static int stored_y;
                   5470:        static WORD stored_a;
1.1.1.20  root     5471:        static char tmp[64], out[64];
1.1       root     5472:        
1.1.1.23  root     5473:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5474:        
                   5475:        // output to console
                   5476:        tmp[p++] = data;
                   5477:        
1.1.1.14  root     5478:        vram_flush();
                   5479:        
1.1       root     5480:        if(is_kanji) {
                   5481:                // kanji character
                   5482:                is_kanji = 0;
                   5483:        } else if(is_esc) {
                   5484:                // escape sequense
                   5485:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5486:                        p = is_esc = 0;
                   5487:                } else if(tmp[1] == '=' && p == 4) {
                   5488:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5489:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5490:                        SetConsoleCursorPosition(hStdout, co);
                   5491:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5492:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5493:                        cursor_moved = false;
1.1.1.59  root     5494:                        cursor_moved_by_crtc = false;
1.1       root     5495:                        p = is_esc = 0;
                   5496:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
1.1.1.59  root     5497:                        if(cursor_moved_by_crtc) {
                   5498:                                if(!restore_console_on_exit) {
                   5499:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5500:                                        scr_top = csbi.srWindow.Top;
                   5501:                                }
                   5502:                                co.X = mem[0x450 + REG8(BH) * 2];
                   5503:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5504:                                SetConsoleCursorPosition(hStdout, co);
                   5505:                                cursor_moved_by_crtc = false;
                   5506:                        }
1.1       root     5507:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5508:                        co.X = csbi.dwCursorPosition.X;
                   5509:                        co.Y = csbi.dwCursorPosition.Y;
                   5510:                        WORD wAttributes = csbi.wAttributes;
                   5511:                        
                   5512:                        if(tmp[1] == 'D') {
                   5513:                                co.Y++;
                   5514:                        } else if(tmp[1] == 'E') {
                   5515:                                co.X = 0;
                   5516:                                co.Y++;
                   5517:                        } else if(tmp[1] == 'M') {
                   5518:                                co.Y--;
                   5519:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5520:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5521:                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5522:                                co.X = 0;
                   5523:                                co.Y = csbi.srWindow.Top;
1.1       root     5524:                        } else if(tmp[1] == '[') {
                   5525:                                int param[256], params = 0;
                   5526:                                memset(param, 0, sizeof(param));
                   5527:                                for(int i = 2; i < p; i++) {
                   5528:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5529:                                                param[params] *= 10;
                   5530:                                                param[params] += tmp[i] - '0';
                   5531:                                        } else {
                   5532:                                                params++;
                   5533:                                        }
                   5534:                                }
                   5535:                                if(data == 'A') {
1.1.1.14  root     5536:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5537:                                } else if(data == 'B') {
1.1.1.14  root     5538:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5539:                                } else if(data == 'C') {
1.1.1.14  root     5540:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5541:                                } else if(data == 'D') {
1.1.1.14  root     5542:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5543:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5544:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5545:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5546:                                } else if(data == 'J') {
1.1.1.14  root     5547:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5548:                                        if(param[0] == 0) {
                   5549:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5550:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5551:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5552:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5553:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5554:                                                }
                   5555:                                        } else if(param[0] == 1) {
1.1.1.14  root     5556:                                                if(co.Y > csbi.srWindow.Top) {
                   5557:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
1.1.1.60  root     5558:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5559:                                                }
                   5560:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5561:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5562:                                        } else if(param[0] == 2) {
1.1.1.14  root     5563:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5564:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5565:                                                co.X = co.Y = 0;
                   5566:                                        }
                   5567:                                } else if(data == 'K') {
1.1.1.14  root     5568:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5569:                                        if(param[0] == 0) {
                   5570:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5571:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5572:                                        } else if(param[0] == 1) {
                   5573:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5574:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5575:                                        } else if(param[0] == 2) {
                   5576:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5577:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5578:                                        }
                   5579:                                } else if(data == 'L') {
1.1.1.14  root     5580:                                        if(params == 0) {
                   5581:                                                param[0] = 1;
1.1       root     5582:                                        }
1.1.1.14  root     5583:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5584:                                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5585:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5586:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5587:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5588:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.60  root     5589:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5590:                                        co.X = 0;
                   5591:                                } else if(data == 'M') {
1.1.1.14  root     5592:                                        if(params == 0) {
                   5593:                                                param[0] = 1;
                   5594:                                        }
                   5595:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5596:                                                clear_scr_buffer(csbi.wAttributes);
                   5597:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5598:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5599:                                        } else {
1.1.1.14  root     5600:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5601:                                                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5602:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5603:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5604:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5605:                                        }
                   5606:                                        co.X = 0;
                   5607:                                } else if(data == 'h') {
                   5608:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5609:                                                ci_new.bVisible = FALSE;
1.1       root     5610:                                        }
                   5611:                                } else if(data == 'l') {
                   5612:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5613:                                                ci_new.bVisible = TRUE;
1.1       root     5614:                                        }
                   5615:                                } else if(data == 'm') {
                   5616:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5617:                                        int reverse = 0, hidden = 0;
                   5618:                                        for(int i = 0; i < params; i++) {
                   5619:                                                if(param[i] == 1) {
                   5620:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5621:                                                } else if(param[i] == 4) {
                   5622:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5623:                                                } else if(param[i] == 7) {
                   5624:                                                        reverse = 1;
                   5625:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5626:                                                        hidden = 1;
                   5627:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5628:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5629:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5630:                                                                param[i] -= 16;
                   5631:                                                        } else {
                   5632:                                                                param[i] -= 30;
                   5633:                                                        }
                   5634:                                                        if(param[i] & 1) {
                   5635:                                                                wAttributes |= FOREGROUND_RED;
                   5636:                                                        }
                   5637:                                                        if(param[i] & 2) {
                   5638:                                                                wAttributes |= FOREGROUND_GREEN;
                   5639:                                                        }
                   5640:                                                        if(param[i] & 4) {
                   5641:                                                                wAttributes |= FOREGROUND_BLUE;
                   5642:                                                        }
                   5643:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5644:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5645:                                                        if((param[i] - 40) & 1) {
                   5646:                                                                wAttributes |= BACKGROUND_RED;
                   5647:                                                        }
                   5648:                                                        if((param[i] - 40) & 2) {
                   5649:                                                                wAttributes |= BACKGROUND_GREEN;
                   5650:                                                        }
                   5651:                                                        if((param[i] - 40) & 4) {
                   5652:                                                                wAttributes |= BACKGROUND_BLUE;
                   5653:                                                        }
                   5654:                                                }
                   5655:                                        }
                   5656:                                        if(reverse) {
                   5657:                                                wAttributes &= ~0xff;
                   5658:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5659:                                        }
                   5660:                                        if(hidden) {
                   5661:                                                wAttributes &= ~0x0f;
                   5662:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5663:                                        }
                   5664:                                } else if(data == 'n') {
                   5665:                                        if(param[0] == 6) {
                   5666:                                                char tmp[16];
                   5667:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5668:                                                int len = strlen(tmp);
1.1.1.32  root     5669:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5670: #ifdef USE_SERVICE_THREAD
                   5671:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5672: #endif
1.1.1.32  root     5673:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5674:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5675:                                                        }
1.1.1.35  root     5676: #ifdef USE_SERVICE_THREAD
                   5677:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5678: #endif
1.1       root     5679:                                                }
                   5680:                                        }
                   5681:                                } else if(data == 's') {
                   5682:                                        stored_x = co.X;
                   5683:                                        stored_y = co.Y;
                   5684:                                        stored_a = wAttributes;
                   5685:                                } else if(data == 'u') {
                   5686:                                        co.X = stored_x;
                   5687:                                        co.Y = stored_y;
                   5688:                                        wAttributes = stored_a;
                   5689:                                }
                   5690:                        }
                   5691:                        if(co.X < 0) {
                   5692:                                co.X = 0;
                   5693:                        } else if(co.X >= csbi.dwSize.X) {
                   5694:                                co.X = csbi.dwSize.X - 1;
                   5695:                        }
1.1.1.14  root     5696:                        if(co.Y < csbi.srWindow.Top) {
                   5697:                                co.Y = csbi.srWindow.Top;
                   5698:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5699:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5700:                        }
                   5701:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5702:                                SetConsoleCursorPosition(hStdout, co);
                   5703:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5704:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5705:                                cursor_moved = false;
                   5706:                        }
                   5707:                        if(wAttributes != csbi.wAttributes) {
                   5708:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5709:                        }
                   5710:                        p = is_esc = 0;
                   5711:                }
                   5712:                return;
                   5713:        } else {
                   5714:                if(msdos_lead_byte_check(data)) {
                   5715:                        is_kanji = 1;
                   5716:                        return;
                   5717:                } else if(data == 0x1b) {
                   5718:                        is_esc = 1;
                   5719:                        return;
                   5720:                }
                   5721:        }
1.1.1.20  root     5722:        
                   5723:        DWORD q = 0, num;
                   5724:        is_kanji = 0;
                   5725:        for(int i = 0; i < p; i++) {
                   5726:                UINT8 c = tmp[i];
                   5727:                if(is_kanji) {
                   5728:                        is_kanji = 0;
                   5729:                } else if(msdos_lead_byte_check(data)) {
                   5730:                        is_kanji = 1;
                   5731:                } else if(msdos_ctrl_code_check(data)) {
                   5732:                        out[q++] = '^';
                   5733:                        c += 'A' - 1;
                   5734:                }
                   5735:                out[q++] = c;
                   5736:        }
1.1.1.59  root     5737:        if(cursor_moved_by_crtc) {
                   5738:                if(!restore_console_on_exit) {
                   5739:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5740:                        scr_top = csbi.srWindow.Top;
                   5741:                }
                   5742:                co.X = mem[0x450 + REG8(BH) * 2];
                   5743:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5744:                SetConsoleCursorPosition(hStdout, co);
                   5745:                cursor_moved_by_crtc = false;
                   5746:        }
1.1.1.34  root     5747:        if(q == 1 && out[0] == 0x08) {
                   5748:                // back space
                   5749:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5750:                if(csbi.dwCursorPosition.X > 0) {
                   5751:                        co.X = csbi.dwCursorPosition.X - 1;
                   5752:                        co.Y = csbi.dwCursorPosition.Y;
                   5753:                        SetConsoleCursorPosition(hStdout, co);
                   5754:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5755:                        co.X = csbi.dwSize.X - 1;
                   5756:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5757:                        SetConsoleCursorPosition(hStdout, co);
                   5758:                } else {
1.1.1.60  root     5759:                        WriteConsoleA(hStdout, out, q, &num, NULL); // to make sure
1.1.1.34  root     5760:                }
                   5761:        } else {
1.1.1.60  root     5762:                WriteConsoleA(hStdout, out, q, &num, NULL);
1.1.1.34  root     5763:        }
1.1       root     5764:        p = 0;
1.1.1.14  root     5765:        
1.1.1.15  root     5766:        if(!restore_console_on_exit) {
                   5767:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5768:                scr_top = csbi.srWindow.Top;
                   5769:        }
1.1       root     5770:        cursor_moved = true;
                   5771: }
                   5772: 
                   5773: int msdos_aux_in()
                   5774: {
1.1.1.21  root     5775:        msdos_stdio_reopen();
                   5776:        
1.1.1.20  root     5777:        process_t *process = msdos_process_info_get(current_psp);
                   5778:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5779:        
                   5780:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5781:                char data = 0;
1.1.1.37  root     5782:                msdos_read(fd, &data, 1);
1.1       root     5783:                return(data);
                   5784:        } else {
                   5785:                return(EOF);
                   5786:        }
                   5787: }
                   5788: 
                   5789: void msdos_aux_out(char data)
                   5790: {
1.1.1.21  root     5791:        msdos_stdio_reopen();
                   5792:        
1.1.1.20  root     5793:        process_t *process = msdos_process_info_get(current_psp);
                   5794:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5795:        
                   5796:        if(fd < process->max_files && file_handler[fd].valid) {
                   5797:                msdos_write(fd, &data, 1);
1.1       root     5798:        }
                   5799: }
                   5800: 
                   5801: void msdos_prn_out(char data)
                   5802: {
1.1.1.21  root     5803:        msdos_stdio_reopen();
                   5804:        
1.1.1.20  root     5805:        process_t *process = msdos_process_info_get(current_psp);
                   5806:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5807:        
                   5808:        if(fd < process->max_files && file_handler[fd].valid) {
                   5809:                msdos_write(fd, &data, 1);
1.1       root     5810:        }
                   5811: }
                   5812: 
                   5813: // memory control
                   5814: 
1.1.1.52  root     5815: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5816: {
                   5817:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5818:        
                   5819:        mcb->mz = mz;
                   5820:        mcb->psp = psp;
1.1.1.30  root     5821:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5822:        
                   5823:        if(prog_name != NULL) {
                   5824:                memset(mcb->prog_name, 0, 8);
                   5825:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5826:        }
1.1       root     5827:        return(mcb);
                   5828: }
                   5829: 
                   5830: void msdos_mcb_check(mcb_t *mcb)
                   5831: {
                   5832:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5833:                #if 0
                   5834:                        // shutdown now !!!
                   5835:                        fatalerror("broken memory control block\n");
                   5836:                #else
                   5837:                        // return error code and continue
                   5838:                        throw(0x07); // broken memory control block
                   5839:                #endif
1.1       root     5840:        }
                   5841: }
                   5842: 
1.1.1.39  root     5843: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5844: {
                   5845:        int mcb_seg = seg - 1;
                   5846:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5847:        msdos_mcb_check(mcb);
                   5848:        
1.1.1.30  root     5849:        if(mcb->paragraphs > paragraphs) {
1.1       root     5850:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5851:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5852:                
                   5853:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5854:                mcb->mz = 'M';
1.1.1.30  root     5855:                mcb->paragraphs = paragraphs;
1.1       root     5856:        }
                   5857: }
                   5858: 
                   5859: void msdos_mem_merge(int seg)
                   5860: {
                   5861:        int mcb_seg = seg - 1;
                   5862:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5863:        msdos_mcb_check(mcb);
                   5864:        
                   5865:        while(1) {
                   5866:                if(mcb->mz == 'Z') {
                   5867:                        break;
                   5868:                }
1.1.1.30  root     5869:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5870:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5871:                msdos_mcb_check(next_mcb);
                   5872:                
                   5873:                if(next_mcb->psp != 0) {
                   5874:                        break;
                   5875:                }
                   5876:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5877:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5878:        }
                   5879: }
                   5880: 
1.1.1.8   root     5881: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5882: {
                   5883:        while(1) {
                   5884:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5885:                bool last_block;
1.1       root     5886:                
1.1.1.14  root     5887:                if(mcb->psp == 0) {
                   5888:                        msdos_mem_merge(mcb_seg + 1);
                   5889:                } else {
                   5890:                        msdos_mcb_check(mcb);
                   5891:                }
1.1.1.33  root     5892:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5893:                        // check if the next is dummy mcb to link to umb
                   5894:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5895:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5896:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5897:                }
                   5898:                if(!(new_process && !last_block)) {
1.1.1.30  root     5899:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5900:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5901:                                mcb->psp = current_psp;
                   5902:                                return(mcb_seg + 1);
                   5903:                        }
                   5904:                }
                   5905:                if(mcb->mz == 'Z') {
                   5906:                        break;
                   5907:                }
1.1.1.30  root     5908:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5909:        }
                   5910:        return(-1);
                   5911: }
                   5912: 
                   5913: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5914: {
                   5915:        int mcb_seg = seg - 1;
                   5916:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5917:        msdos_mcb_check(mcb);
1.1.1.30  root     5918:        int current_paragraphs = mcb->paragraphs;
1.1       root     5919:        
                   5920:        msdos_mem_merge(seg);
1.1.1.30  root     5921:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5922:                if(max_paragraphs) {
1.1.1.30  root     5923:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5924:                }
1.1       root     5925:                msdos_mem_split(seg, current_paragraphs);
                   5926:                return(-1);
                   5927:        }
                   5928:        msdos_mem_split(seg, paragraphs);
                   5929:        return(0);
                   5930: }
                   5931: 
                   5932: void msdos_mem_free(int seg)
                   5933: {
                   5934:        int mcb_seg = seg - 1;
                   5935:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5936:        msdos_mcb_check(mcb);
                   5937:        
                   5938:        mcb->psp = 0;
                   5939:        msdos_mem_merge(seg);
                   5940: }
                   5941: 
1.1.1.8   root     5942: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5943: {
                   5944:        int max_paragraphs = 0;
                   5945:        
                   5946:        while(1) {
                   5947:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5948:                bool last_block;
                   5949:                
1.1       root     5950:                msdos_mcb_check(mcb);
                   5951:                
1.1.1.33  root     5952:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5953:                        // check if the next is dummy mcb to link to umb
                   5954:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5955:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5956:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5957:                }
                   5958:                if(!(new_process && !last_block)) {
1.1.1.30  root     5959:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5960:                                max_paragraphs = mcb->paragraphs;
1.1       root     5961:                        }
                   5962:                }
                   5963:                if(mcb->mz == 'Z') {
                   5964:                        break;
                   5965:                }
1.1.1.30  root     5966:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5967:        }
1.1.1.14  root     5968:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5969: }
                   5970: 
1.1.1.8   root     5971: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5972: {
                   5973:        int last_seg = -1;
                   5974:        
                   5975:        while(1) {
                   5976:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5977:                msdos_mcb_check(mcb);
                   5978:                
1.1.1.14  root     5979:                if(mcb->psp == psp) {
1.1.1.8   root     5980:                        last_seg = mcb_seg;
                   5981:                }
1.1.1.14  root     5982:                if(mcb->mz == 'Z') {
                   5983:                        break;
                   5984:                }
1.1.1.30  root     5985:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5986:        }
                   5987:        return(last_seg);
                   5988: }
                   5989: 
1.1.1.19  root     5990: int msdos_mem_get_umb_linked()
                   5991: {
1.1.1.33  root     5992:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5993:        msdos_mcb_check(mcb);
1.1.1.19  root     5994:        
1.1.1.33  root     5995:        if(mcb->mz == 'M') {
                   5996:                return(-1);
1.1.1.19  root     5997:        }
                   5998:        return(0);
                   5999: }
                   6000: 
1.1.1.33  root     6001: void msdos_mem_link_umb()
1.1.1.19  root     6002: {
1.1.1.33  root     6003:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6004:        msdos_mcb_check(mcb);
1.1.1.19  root     6005:        
1.1.1.33  root     6006:        mcb->mz = 'M';
                   6007:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     6008:        
                   6009:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     6010: }
                   6011: 
1.1.1.33  root     6012: void msdos_mem_unlink_umb()
1.1.1.19  root     6013: {
1.1.1.33  root     6014:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6015:        msdos_mcb_check(mcb);
1.1.1.19  root     6016:        
1.1.1.33  root     6017:        mcb->mz = 'Z';
                   6018:        mcb->paragraphs = 0;
1.1.1.39  root     6019:        
                   6020:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     6021: }
                   6022: 
1.1.1.29  root     6023: #ifdef SUPPORT_HMA
                   6024: 
                   6025: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   6026: {
                   6027:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6028:        
                   6029:        mcb->ms[0] = 'M';
                   6030:        mcb->ms[1] = 'S';
                   6031:        mcb->owner = owner;
                   6032:        mcb->size = size;
                   6033:        mcb->next = next;
                   6034:        return(mcb);
                   6035: }
                   6036: 
                   6037: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   6038: {
                   6039:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   6040: }
                   6041: 
                   6042: int msdos_hma_mem_split(int offset, int size)
                   6043: {
                   6044:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6045:        
                   6046:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6047:                return(-1);
                   6048:        }
                   6049:        if(mcb->size >= size + 0x10) {
                   6050:                int new_offset = offset + 0x10 + size;
                   6051:                int new_size = mcb->size - 0x10 - size;
                   6052:                
                   6053:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   6054:                mcb->size = size;
                   6055:                mcb->next = new_offset;
                   6056:                return(0);
                   6057:        }
                   6058:        return(-1);
                   6059: }
                   6060: 
                   6061: void msdos_hma_mem_merge(int offset)
                   6062: {
                   6063:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6064:        
                   6065:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6066:                return;
                   6067:        }
                   6068:        while(1) {
                   6069:                if(mcb->next == 0) {
                   6070:                        break;
                   6071:                }
                   6072:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   6073:                
                   6074:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   6075:                        return;
                   6076:                }
                   6077:                if(next_mcb->owner != 0) {
                   6078:                        break;
                   6079:                }
                   6080:                mcb->size += 0x10 + next_mcb->size;
                   6081:                mcb->next = next_mcb->next;
                   6082:        }
                   6083: }
                   6084: 
                   6085: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   6086: {
                   6087:        int offset = 0x10; // first mcb in HMA
                   6088:        
                   6089:        while(1) {
                   6090:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6091:                
                   6092:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6093:                        return(-1);
                   6094:                }
                   6095:                if(mcb->owner == 0) {
                   6096:                        msdos_hma_mem_merge(offset);
                   6097:                }
                   6098:                if(mcb->owner == 0 && mcb->size >= size) {
                   6099:                        msdos_hma_mem_split(offset, size);
                   6100:                        mcb->owner = owner;
                   6101:                        return(offset);
                   6102:                }
                   6103:                if(mcb->next == 0) {
                   6104:                        break;
                   6105:                }
                   6106:                offset = mcb->next;
                   6107:        }
                   6108:        return(-1);
                   6109: }
                   6110: 
                   6111: int msdos_hma_mem_realloc(int offset, int size)
                   6112: {
                   6113:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6114:        
                   6115:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6116:                return(-1);
                   6117:        }
                   6118:        if(mcb->size < size) {
                   6119:                return(-1);
                   6120:        }
                   6121:        msdos_hma_mem_split(offset, size);
                   6122:        return(0);
                   6123: }
                   6124: 
                   6125: void msdos_hma_mem_free(int offset)
                   6126: {
                   6127:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6128:        
                   6129:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6130:                return;
                   6131:        }
                   6132:        mcb->owner = 0;
                   6133:        msdos_hma_mem_merge(offset);
                   6134: }
                   6135: 
                   6136: int msdos_hma_mem_get_free(int *available_offset)
                   6137: {
                   6138:        int offset = 0x10; // first mcb in HMA
                   6139:        int size = 0;
                   6140:        
                   6141:        while(1) {
                   6142:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6143:                
                   6144:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6145:                        return(0);
                   6146:                }
                   6147:                if(mcb->owner == 0 && size < mcb->size) {
                   6148:                        if(available_offset != NULL) {
                   6149:                                *available_offset = offset;
                   6150:                        }
                   6151:                        size = mcb->size;
                   6152:                }
                   6153:                if(mcb->next == 0) {
                   6154:                        break;
                   6155:                }
                   6156:                offset = mcb->next;
                   6157:        }
                   6158:        return(size);
                   6159: }
                   6160: 
                   6161: #endif
                   6162: 
1.1       root     6163: // environment
                   6164: 
1.1.1.45  root     6165: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     6166: {
                   6167:        char *dst = (char *)(mem + (env_seg << 4));
                   6168:        
                   6169:        while(1) {
                   6170:                if(dst[0] == 0) {
                   6171:                        break;
                   6172:                }
                   6173:                dst += strlen(dst) + 1;
                   6174:        }
                   6175:        *dst++ = 0; // end of environment
                   6176:        *dst++ = 1; // top of argv[0]
                   6177:        *dst++ = 0;
                   6178:        memcpy(dst, argv, strlen(argv));
                   6179:        dst += strlen(argv);
                   6180:        *dst++ = 0;
                   6181:        *dst++ = 0;
                   6182: }
                   6183: 
1.1.1.45  root     6184: const char *msdos_env_get_argv(int env_seg)
1.1       root     6185: {
                   6186:        static char env[ENV_SIZE];
                   6187:        char *src = env;
                   6188:        
                   6189:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6190:        while(1) {
                   6191:                if(src[0] == 0) {
                   6192:                        if(src[1] == 1) {
                   6193:                                return(src + 3);
                   6194:                        }
                   6195:                        break;
                   6196:                }
                   6197:                src += strlen(src) + 1;
                   6198:        }
                   6199:        return(NULL);
                   6200: }
                   6201: 
1.1.1.45  root     6202: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     6203: {
                   6204:        static char env[ENV_SIZE];
                   6205:        char *src = env;
                   6206:        
                   6207:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6208:        while(1) {
                   6209:                if(src[0] == 0) {
                   6210:                        break;
                   6211:                }
                   6212:                int len = strlen(src);
                   6213:                char *n = my_strtok(src, "=");
                   6214:                char *v = src + strlen(n) + 1;
                   6215:                
                   6216:                if(_stricmp(name, n) == 0) {
                   6217:                        return(v);
                   6218:                }
                   6219:                src += len + 1;
                   6220:        }
                   6221:        return(NULL);
                   6222: }
                   6223: 
1.1.1.45  root     6224: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     6225: {
                   6226:        char env[ENV_SIZE];
                   6227:        char *src = env;
                   6228:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     6229:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6230:        int done = 0;
                   6231:        
                   6232:        memcpy(src, dst, ENV_SIZE);
                   6233:        memset(dst, 0, ENV_SIZE);
                   6234:        while(1) {
                   6235:                if(src[0] == 0) {
                   6236:                        break;
                   6237:                }
                   6238:                int len = strlen(src);
                   6239:                char *n = my_strtok(src, "=");
                   6240:                char *v = src + strlen(n) + 1;
                   6241:                char tmp[1024];
                   6242:                
                   6243:                if(_stricmp(name, n) == 0) {
                   6244:                        sprintf(tmp, "%s=%s", n, value);
                   6245:                        done = 1;
                   6246:                } else {
                   6247:                        sprintf(tmp, "%s=%s", n, v);
                   6248:                }
                   6249:                memcpy(dst, tmp, strlen(tmp));
                   6250:                dst += strlen(tmp) + 1;
                   6251:                src += len + 1;
                   6252:        }
                   6253:        if(!done) {
                   6254:                char tmp[1024];
                   6255:                
                   6256:                sprintf(tmp, "%s=%s", name, value);
                   6257:                memcpy(dst, tmp, strlen(tmp));
                   6258:                dst += strlen(tmp) + 1;
                   6259:        }
                   6260:        if(argv) {
                   6261:                *dst++ = 0; // end of environment
                   6262:                *dst++ = 1; // top of argv[0]
                   6263:                *dst++ = 0;
                   6264:                memcpy(dst, argv, strlen(argv));
                   6265:                dst += strlen(argv);
                   6266:                *dst++ = 0;
                   6267:                *dst++ = 0;
                   6268:        }
                   6269: }
                   6270: 
                   6271: // process
                   6272: 
1.1.1.8   root     6273: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6274: {
                   6275:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6276:        
                   6277:        memset(psp, 0, PSP_SIZE);
                   6278:        psp->exit[0] = 0xcd;
                   6279:        psp->exit[1] = 0x20;
1.1.1.8   root     6280:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6281: #if 1
1.1.1.49  root     6282:        psp->call5[0] = 0xcd;   // int 30h
                   6283:        psp->call5[1] = 0x30;
1.1.1.46  root     6284:        psp->call5[2] = 0xc3;   // ret
                   6285: #else
                   6286:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6287:        psp->call5[1] = 0xe1;
                   6288:        psp->call5[2] = 0xcd;   // int 21h
                   6289:        psp->call5[3] = 0x21;
                   6290:        psp->call5[4] = 0xc3;   // ret
                   6291: #endif
1.1       root     6292:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6293:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6294:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6295:        psp->parent_psp = parent_psp;
1.1.1.20  root     6296:        if(parent_psp == (UINT16)-1) {
                   6297:                for(int i = 0; i < 20; i++) {
                   6298:                        if(file_handler[i].valid) {
                   6299:                                psp->file_table[i] = i;
                   6300:                        } else {
                   6301:                                psp->file_table[i] = 0xff;
                   6302:                        }
1.1       root     6303:                }
1.1.1.20  root     6304:        } else {
                   6305:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6306:        }
                   6307:        psp->env_seg = env_seg;
                   6308:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6309:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6310:        psp->file_table_size = 20;
                   6311:        psp->file_table_ptr.w.l = 0x18;
                   6312:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6313:        psp->service[0] = 0xcd;
                   6314:        psp->service[1] = 0x21;
                   6315:        psp->service[2] = 0xcb;
                   6316:        return(psp);
                   6317: }
                   6318: 
1.1.1.20  root     6319: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6320: {
                   6321:        if(psp_seg && fd < 20) {
                   6322:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6323:                psp->file_table[fd] = value;
                   6324:        }
                   6325: }
                   6326: 
                   6327: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6328: {
                   6329:        if(psp_seg && fd < 20) {
                   6330:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6331:                fd = psp->file_table[fd];
                   6332:        }
                   6333:        return fd;
                   6334: }
                   6335: 
1.1.1.52  root     6336: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6337: {
                   6338:        // load command file
                   6339:        int fd = -1;
1.1.1.45  root     6340:        int sio_port = 0;
                   6341:        int lpt_port = 0;
1.1       root     6342:        int dos_command = 0;
1.1.1.24  root     6343:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6344:        char pipe_stdin_path[MAX_PATH] = {0};
                   6345:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6346:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6347:        
                   6348:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6349:        int opt_len = mem[opt_ofs];
                   6350:        memset(opt, 0, sizeof(opt));
                   6351:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6352:        
1.1.1.14  root     6353:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6354:                // this is a batch file, run command.com
                   6355:                char tmp[MAX_PATH];
                   6356:                if(opt_len != 0) {
                   6357:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6358:                } else {
                   6359:                        sprintf(tmp, "/C %s", cmd);
                   6360:                }
                   6361:                strcpy(opt, tmp);
                   6362:                opt_len = strlen(opt);
                   6363:                mem[opt_ofs] = opt_len;
                   6364:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6365:                strcpy(command, comspec_path);
                   6366:                strcpy(name_tmp, "COMMAND.COM");
                   6367:        } else {
                   6368:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6369:                        // redirect C:\COMMAND.COM to comspec_path
                   6370:                        strcpy(command, comspec_path);
                   6371:                } else {
                   6372:                        strcpy(command, cmd);
                   6373:                }
1.1.1.60  root     6374:                if(GetFullPathNameA(command, MAX_PATH, path, &name) == 0) {
1.1.1.24  root     6375:                        return(-1);
                   6376:                }
1.1.1.14  root     6377:                memset(name_tmp, 0, sizeof(name_tmp));
                   6378:                strcpy(name_tmp, name);
                   6379:                
                   6380:                // check command.com
1.1.1.38  root     6381:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6382:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6383:                        if(opt_len == 0) {
                   6384: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6385:                                process_t *current_process = NULL;
                   6386:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6387:                                        if(process[i].psp == current_psp) {
                   6388:                                                current_process = &process[i];
                   6389:                                                break;
                   6390:                                        }
                   6391:                                }
                   6392:                                if(current_process != NULL) {
                   6393:                                        param->cmd_line.dw = current_process->dta.dw;
                   6394:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6395:                                        opt_len = mem[opt_ofs];
                   6396:                                        memset(opt, 0, sizeof(opt));
                   6397:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6398:                                }
                   6399:                        }
                   6400:                        for(int i = 0; i < opt_len; i++) {
                   6401:                                if(opt[i] == ' ') {
                   6402:                                        continue;
                   6403:                                }
                   6404:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6405:                                        for(int j = i + 3; j < opt_len; j++) {
                   6406:                                                if(opt[j] == ' ') {
                   6407:                                                        continue;
                   6408:                                                }
                   6409:                                                char *token = my_strtok(opt + j, " ");
                   6410:                                                
1.1.1.38  root     6411:                                                strcpy(command, token);
                   6412:                                                char tmp[MAX_PATH];
                   6413:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6414:                                                strcpy(opt, "");
                   6415:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6416:                                                        if(tmp[i] != ' ') {
                   6417:                                                                strcpy(opt, tmp + i);
                   6418:                                                                break;
                   6419:                                                        }
                   6420:                                                }
                   6421:                                                strcpy(tmp, opt);
1.1.1.38  root     6422:                                                
                   6423:                                                if(al == 0x00) {
1.1.1.39  root     6424:                                                        #define GET_FILE_PATH() { \
                   6425:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6426:                                                                        token++; \
                   6427:                                                                } \
                   6428:                                                                token++; \
                   6429:                                                                while(*token == ' ') { \
                   6430:                                                                        token++; \
                   6431:                                                                } \
                   6432:                                                                char *ptr = token; \
                   6433:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6434:                                                                        ptr++; \
                   6435:                                                                } \
                   6436:                                                                *ptr = '\0'; \
                   6437:                                                        }
                   6438:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6439:                                                                GET_FILE_PATH();
1.1.1.38  root     6440:                                                                strcpy(pipe_stdin_path, token);
                   6441:                                                                strcpy(opt, tmp);
                   6442:                                                        }
1.1.1.39  root     6443:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6444:                                                                GET_FILE_PATH();
1.1.1.38  root     6445:                                                                strcpy(pipe_stdout_path, token);
                   6446:                                                                strcpy(opt, tmp);
                   6447:                                                        }
1.1.1.39  root     6448:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6449:                                                                GET_FILE_PATH();
                   6450:                                                                strcpy(pipe_stderr_path, token);
                   6451:                                                                strcpy(opt, tmp);
                   6452:                                                        }
                   6453:                                                        #undef GET_FILE_PATH
                   6454:                                                        
                   6455:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6456:                                                                *token = '\0';
                   6457:                                                        }
                   6458:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6459:                                                                *token = '\0';
                   6460:                                                        }
                   6461:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6462:                                                                *token = '\0';
                   6463:                                                        }
1.1.1.38  root     6464:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6465:                                                                *token = '\0';
                   6466:                                                        }
                   6467:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6468:                                                                *token = '\0';
                   6469:                                                        }
1.1.1.14  root     6470:                                                }
1.1.1.39  root     6471:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6472:                                                        opt[i] = '\0';
                   6473:                                                }
1.1.1.38  root     6474:                                                opt_len = strlen(opt);
                   6475:                                                mem[opt_ofs] = opt_len;
                   6476:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6477:                                                dos_command = 1;
1.1.1.14  root     6478:                                                break;
1.1       root     6479:                                        }
                   6480:                                }
1.1.1.14  root     6481:                                break;
1.1       root     6482:                        }
                   6483:                }
                   6484:        }
                   6485:        
                   6486:        // load command file
                   6487:        strcpy(path, command);
                   6488:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6489:                sprintf(path, "%s.COM", command);
                   6490:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6491:                        sprintf(path, "%s.EXE", command);
                   6492:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6493:                                sprintf(path, "%s.BAT", command);
                   6494:                                if(_access(path, 0) == 0) {
                   6495:                                        // this is a batch file, run command.com
                   6496:                                        char tmp[MAX_PATH];
                   6497:                                        if(opt_len != 0) {
                   6498:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6499:                                        } else {
                   6500:                                                sprintf(tmp, "/C %s", path);
                   6501:                                        }
                   6502:                                        strcpy(opt, tmp);
                   6503:                                        opt_len = strlen(opt);
                   6504:                                        mem[opt_ofs] = opt_len;
                   6505:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6506:                                        strcpy(path, comspec_path);
                   6507:                                        strcpy(name_tmp, "COMMAND.COM");
                   6508:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6509:                                } else {
                   6510:                                        // search path in parent environments
                   6511:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6512:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6513:                                        if(env != NULL) {
                   6514:                                                char env_path[4096];
                   6515:                                                strcpy(env_path, env);
                   6516:                                                char *token = my_strtok(env_path, ";");
                   6517:                                                
                   6518:                                                while(token != NULL) {
                   6519:                                                        if(strlen(token) != 0) {
                   6520:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6521:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6522:                                                                        break;
                   6523:                                                                }
                   6524:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6525:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6526:                                                                        break;
                   6527:                                                                }
                   6528:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6529:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6530:                                                                        break;
                   6531:                                                                }
                   6532:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6533:                                                                if(_access(path, 0) == 0) {
                   6534:                                                                        // this is a batch file, run command.com
                   6535:                                                                        char tmp[MAX_PATH];
                   6536:                                                                        if(opt_len != 0) {
                   6537:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6538:                                                                        } else {
                   6539:                                                                                sprintf(tmp, "/C %s", path);
                   6540:                                                                        }
                   6541:                                                                        strcpy(opt, tmp);
                   6542:                                                                        opt_len = strlen(opt);
                   6543:                                                                        mem[opt_ofs] = opt_len;
                   6544:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6545:                                                                        strcpy(path, comspec_path);
                   6546:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6547:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6548:                                                                        break;
                   6549:                                                                }
1.1.1.8   root     6550:                                                        }
1.1.1.14  root     6551:                                                        token = my_strtok(NULL, ";");
1.1       root     6552:                                                }
                   6553:                                        }
                   6554:                                }
                   6555:                        }
                   6556:                }
                   6557:        }
                   6558:        if(fd == -1) {
1.1.1.38  root     6559:                // we can not find command.com in the path, so open comspec_path
                   6560:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6561:                        strcpy(command, comspec_path);
                   6562:                        strcpy(path, command);
                   6563:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6564:                }
                   6565:        }
                   6566:        if(fd == -1) {
1.1.1.52  root     6567:                if(!first_process && al == 0 && dos_command) {
1.1       root     6568:                        // may be dos command
                   6569:                        char tmp[MAX_PATH];
1.1.1.52  root     6570:                        if(opt_len != 0) {
                   6571:                                sprintf(tmp, "%s %s", command, opt);
                   6572:                        } else {
                   6573:                                sprintf(tmp, "%s", command);
                   6574:                        }
                   6575:                        retval = system(tmp);
1.1       root     6576:                        return(0);
                   6577:                } else {
                   6578:                        return(-1);
                   6579:                }
                   6580:        }
1.1.1.52  root     6581:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6582:        _read(fd, file_buffer, sizeof(file_buffer));
                   6583:        _close(fd);
                   6584:        
1.1.1.52  root     6585:        // check if this is win32 program
                   6586:        if(!first_process && al == 0) {
                   6587:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6588:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6589:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6590:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6591:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6592:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6593:                                char tmp[MAX_PATH];
                   6594:                                if(opt_len != 0) {
                   6595:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6596:                                } else {
                   6597:                                        sprintf(tmp, "\"%s\"", path);
                   6598:                                }
                   6599:                                retval = system(tmp);
                   6600:                                return(0);
                   6601:                        }
                   6602:                }
                   6603:        }
                   6604:        
1.1       root     6605:        // copy environment
1.1.1.29  root     6606:        int umb_linked, env_seg, psp_seg;
1.1       root     6607:        
1.1.1.29  root     6608:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6609:                msdos_mem_unlink_umb();
                   6610:        }
1.1.1.8   root     6611:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6612:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6613:                        if(umb_linked != 0) {
                   6614:                                msdos_mem_link_umb();
                   6615:                        }
                   6616:                        return(-1);
                   6617:                }
1.1       root     6618:        }
                   6619:        if(param->env_seg == 0) {
                   6620:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6621:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6622:        } else {
                   6623:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6624:        }
                   6625:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6626:        
                   6627:        // check exe header
                   6628:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6629:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6630:        UINT16 cs, ss, ip, sp;
                   6631:        
                   6632:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6633:                // memory allocation
                   6634:                int header_size = header->header_size * 16;
                   6635:                int load_size = header->pages * 512 - header_size;
                   6636:                if(header_size + load_size < 512) {
                   6637:                        load_size = 512 - header_size;
                   6638:                }
                   6639:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6640:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6641:                        msdos_mem_free(env_seg);
                   6642:                        return(-1);
                   6643:                }
                   6644:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6645:                if(paragraphs > free_paragraphs) {
                   6646:                        paragraphs = free_paragraphs;
                   6647:                }
1.1.1.8   root     6648:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6649:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6650:                                if(umb_linked != 0) {
                   6651:                                        msdos_mem_link_umb();
                   6652:                                }
                   6653:                                msdos_mem_free(env_seg);
                   6654:                                return(-1);
                   6655:                        }
1.1       root     6656:                }
                   6657:                // relocation
                   6658:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6659:                for(int i = 0; i < header->relocations; i++) {
                   6660:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6661:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6662:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6663:                }
                   6664:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6665:                // segments
                   6666:                cs = header->init_cs + start_seg;
                   6667:                ss = header->init_ss + start_seg;
                   6668:                ip = header->init_ip;
                   6669:                sp = header->init_sp - 2; // for symdeb
                   6670:        } else {
                   6671:                // memory allocation
                   6672:                paragraphs = free_paragraphs;
1.1.1.8   root     6673:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6674:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6675:                                if(umb_linked != 0) {
                   6676:                                        msdos_mem_link_umb();
                   6677:                                }
                   6678:                                msdos_mem_free(env_seg);
                   6679:                                return(-1);
                   6680:                        }
1.1       root     6681:                }
                   6682:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6683:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6684:                // segments
                   6685:                cs = ss = psp_seg;
                   6686:                ip = 0x100;
                   6687:                sp = 0xfffe;
                   6688:        }
1.1.1.29  root     6689:        if(umb_linked != 0) {
                   6690:                msdos_mem_link_umb();
                   6691:        }
1.1       root     6692:        
                   6693:        // create psp
1.1.1.3   root     6694:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6695:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6696:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6697:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6698:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6699:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6700:        
                   6701:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6702:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6703:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6704:        
1.1.1.4   root     6705:        for(int i = 0; i < 8; i++) {
                   6706:                if(name_tmp[i] == '.') {
                   6707:                        mcb_psp->prog_name[i] = '\0';
                   6708:                        break;
                   6709:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6710:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6711:                        i++;
                   6712:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6713:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6714:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6715:                } else {
                   6716:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6717:                }
                   6718:        }
                   6719:        
1.1       root     6720:        // process info
                   6721:        process_t *process = msdos_process_info_create(psp_seg);
                   6722:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6723: #ifdef USE_DEBUGGER
                   6724:        strcpy(process->module_path, path);
                   6725: #endif
1.1       root     6726:        process->dta.w.l = 0x80;
                   6727:        process->dta.w.h = psp_seg;
                   6728:        process->switchar = '/';
                   6729:        process->max_files = 20;
                   6730:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6731:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6732:        process->parent_ds = SREG(DS);
1.1.1.31  root     6733:        process->parent_es = SREG(ES);
1.1       root     6734:        
                   6735:        current_psp = psp_seg;
1.1.1.23  root     6736:        msdos_sda_update(current_psp);
1.1       root     6737:        
                   6738:        if(al == 0x00) {
                   6739:                int_10h_feh_called = int_10h_ffh_called = false;
                   6740:                
1.1.1.38  root     6741:                // pipe
                   6742:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6743: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6744:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6745:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6746:                        } else {
                   6747:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6748:                        }
                   6749:                        if(fd != -1) {
                   6750:                                msdos_file_handler_open(fd, pipe_stdin_path, _isatty(fd), 0, msdos_device_info(pipe_stdin_path), current_psp, sio_port, lpt_port);
1.1.1.38  root     6751:                                psp->file_table[0] = fd;
                   6752:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6753:                        }
                   6754:                }
                   6755:                if(pipe_stdout_path[0] != '\0') {
                   6756:                        if(_access(pipe_stdout_path, 0) == 0) {
1.1.1.60  root     6757:                                SetFileAttributesA(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6758:                                DeleteFileA(pipe_stdout_path);
1.1.1.38  root     6759:                        }
1.1.1.45  root     6760: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6761:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6762:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6763:                        } else {
                   6764:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6765:                        }
                   6766:                        if(fd != -1) {
                   6767:                                msdos_file_handler_open(fd, pipe_stdout_path, _isatty(fd), 1, msdos_device_info(pipe_stdout_path), current_psp, sio_port, lpt_port);
1.1.1.38  root     6768:                                psp->file_table[1] = fd;
                   6769:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6770:                        }
                   6771:                }
1.1.1.39  root     6772:                if(pipe_stderr_path[0] != '\0') {
                   6773:                        if(_access(pipe_stderr_path, 0) == 0) {
1.1.1.60  root     6774:                                SetFileAttributesA(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6775:                                DeleteFileA(pipe_stderr_path);
1.1.1.39  root     6776:                        }
1.1.1.45  root     6777: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6778:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6779:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6780:                        } else {
                   6781:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6782:                        }
                   6783:                        if(fd != -1) {
                   6784:                                msdos_file_handler_open(fd, pipe_stderr_path, _isatty(fd), 1, msdos_device_info(pipe_stderr_path), current_psp, sio_port, lpt_port);
1.1.1.39  root     6785:                                psp->file_table[2] = fd;
                   6786:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6787:                        }
                   6788:                }
1.1.1.38  root     6789:                
1.1       root     6790:                // registers and segments
                   6791:                REG16(AX) = REG16(BX) = 0x00;
                   6792:                REG16(CX) = 0xff;
                   6793:                REG16(DX) = psp_seg;
                   6794:                REG16(SI) = ip;
                   6795:                REG16(DI) = sp;
                   6796:                REG16(SP) = sp;
1.1.1.3   root     6797:                SREG(DS) = SREG(ES) = psp_seg;
                   6798:                SREG(SS) = ss;
                   6799:                i386_load_segment_descriptor(DS);
                   6800:                i386_load_segment_descriptor(ES);
                   6801:                i386_load_segment_descriptor(SS);
1.1       root     6802:                
                   6803:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6804:                i386_jmp_far(cs, ip);
                   6805:        } else if(al == 0x01) {
                   6806:                // copy ss:sp and cs:ip to param block
                   6807:                param->sp = sp;
                   6808:                param->ss = ss;
                   6809:                param->ip = ip;
                   6810:                param->cs = cs;
1.1.1.31  root     6811:                
                   6812:                // the AX value to be passed to the child program is put on top of the child's stack
                   6813:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6814:        }
                   6815:        return(0);
                   6816: }
                   6817: 
                   6818: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6819: {
                   6820:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6821:        
                   6822:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6823:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6824:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6825:        
1.1.1.3   root     6826:        SREG(SS) = psp->stack.w.h;
                   6827:        i386_load_segment_descriptor(SS);
1.1       root     6828:        REG16(SP) = psp->stack.w.l;
                   6829:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6830:        
1.1.1.28  root     6831: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6832:        process_t *current_process = NULL;
                   6833:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6834:                if(process[i].psp == psp_seg) {
                   6835:                        current_process = &process[i];
                   6836:                        break;
                   6837:                }
                   6838:        }
                   6839:        if(current_process == NULL) {
                   6840:                throw(0x1f); // general failure
                   6841:        }
                   6842:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6843:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6844:        if(current_process->called_by_int2eh) {
                   6845:                REG16(AX) = ret;
                   6846:        }
                   6847:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6848:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6849:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6850:        i386_load_segment_descriptor(ES);
1.1       root     6851:        
                   6852:        if(mem_free) {
1.1.1.8   root     6853:                int mcb_seg;
                   6854:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6855:                        msdos_mem_free(mcb_seg + 1);
                   6856:                }
                   6857:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6858:                        msdos_mem_free(mcb_seg + 1);
                   6859:                }
1.1       root     6860:                
                   6861:                for(int i = 0; i < MAX_FILES; i++) {
                   6862:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6863:                                _close(i);
1.1.1.20  root     6864:                                msdos_file_handler_close(i);
                   6865:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6866:                        }
                   6867:                }
1.1.1.13  root     6868:                msdos_dta_info_free(psp_seg);
1.1       root     6869:        }
1.1.1.14  root     6870:        msdos_stdio_reopen();
1.1       root     6871:        
1.1.1.28  root     6872:        memset(current_process, 0, sizeof(process_t));
1.1       root     6873:        
                   6874:        current_psp = psp->parent_psp;
                   6875:        retval = ret;
1.1.1.23  root     6876:        msdos_sda_update(current_psp);
1.1       root     6877: }
                   6878: 
                   6879: // drive
                   6880: 
1.1.1.42  root     6881: int pcbios_update_drive_param(int drive_num, int force_update);
                   6882: 
1.1       root     6883: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6884: {
1.1.1.41  root     6885:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6886:                return(0);
                   6887:        }
1.1.1.42  root     6888:        pcbios_update_drive_param(drive_num, force_update);
                   6889:        
                   6890:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6891:        *seg = DPB_TOP >> 4;
                   6892:        *ofs = sizeof(dpb_t) * drive_num;
                   6893:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6894:        
                   6895:        memset(dpb, 0, sizeof(dpb_t));
                   6896:        
1.1.1.41  root     6897:        dpb->drive_num = drive_num;
                   6898:        dpb->unit_num = drive_num;
1.1.1.42  root     6899:        
                   6900:        if(drive_param->valid) {
                   6901:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6902:                
                   6903:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6904:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6905:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6906:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6907:                switch(geo->MediaType) {
                   6908:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6909:                        dpb->media_type = 0xff;
                   6910:                        break;
                   6911:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6912:                        dpb->media_type = 0xfe;
                   6913:                        break;
                   6914:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6915:                        dpb->media_type = 0xfd;
                   6916:                        break;
                   6917:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6918:                        dpb->media_type = 0xfc;
                   6919:                        break;
                   6920:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6921:                case F3_1Pt2_512:
                   6922:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6923:                case F5_720_512:
                   6924:                        dpb->media_type = 0xf9;
                   6925:                        break;
                   6926:                case FixedMedia:        // hard disk
                   6927:                case RemovableMedia:
                   6928:                case Unknown:
                   6929:                        dpb->media_type = 0xf8;
                   6930:                        break;
                   6931:                default:
                   6932:                        dpb->media_type = 0xf0;
                   6933:                        break;
                   6934:                }
                   6935:        }
1.1.1.41  root     6936:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6937:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6938:        dpb->info_sector = 0xffff;
                   6939:        dpb->backup_boot_sector = 0xffff;
                   6940:        dpb->free_clusters = 0xffff;
                   6941:        dpb->free_search_cluster = 0xffffffff;
                   6942:        
                   6943:        return(drive_param->valid);
1.1       root     6944: }
                   6945: 
                   6946: // pc bios
                   6947: 
1.1.1.35  root     6948: #ifdef USE_SERVICE_THREAD
                   6949: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6950: {
                   6951: #if defined(HAS_I386)
                   6952:        if(m_SF != 0) {
                   6953:                m_SF = 0;
1.1.1.49  root     6954:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6955:        } else {
                   6956:                m_SF = 1;
1.1.1.49  root     6957:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6958:        }
                   6959: #else
                   6960:        if(m_SignVal < 0) {
                   6961:                m_SignVal = 0;
1.1.1.49  root     6962:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6963:        } else {
                   6964:                m_SignVal = -1;
1.1.1.49  root     6965:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6966:        }
                   6967: #endif
1.1.1.59  root     6968:        // dummy loop to wait BIOS/DOS service is done is at fffc:0013
1.1.1.49  root     6969:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     6970:        in_service = true;
                   6971:        service_exit = false;
                   6972:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6973: }
                   6974: 
                   6975: void finish_service_loop()
                   6976: {
                   6977:        if(in_service && service_exit) {
                   6978: #if defined(HAS_I386)
                   6979:                if(m_SF != 0) {
                   6980:                        m_SF = 0;
                   6981:                } else {
                   6982:                        m_SF = 1;
                   6983:                }
                   6984: #else
                   6985:                if(m_SignVal < 0) {
                   6986:                        m_SignVal = 0;
                   6987:                } else {
                   6988:                        m_SignVal = -1;
                   6989:                }
                   6990: #endif
                   6991:                in_service = false;
                   6992:        }
                   6993: }
                   6994: #endif
                   6995: 
1.1.1.19  root     6996: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6997: {
                   6998:        static unsigned __int64 start_msec_since_midnight = 0;
                   6999:        static unsigned __int64 start_msec_since_hostboot = 0;
                   7000:        
                   7001:        if(start_msec_since_midnight == 0) {
                   7002:                SYSTEMTIME time;
                   7003:                GetLocalTime(&time);
                   7004:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   7005:                start_msec_since_hostboot = cur_msec;
                   7006:        }
                   7007:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   7008:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   7009:        return (UINT32)tick;
                   7010: }
                   7011: 
                   7012: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   7013: {
                   7014:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   7015:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   7016:        
                   7017:        if(prev_tick > next_tick) {
                   7018:                mem[0x470] = 1;
                   7019:        }
                   7020:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   7021: }
                   7022: 
1.1.1.14  root     7023: inline void pcbios_irq0()
                   7024: {
                   7025:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     7026:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     7027: }
                   7028: 
1.1.1.16  root     7029: int pcbios_get_text_vram_address(int page)
1.1       root     7030: {
                   7031:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7032:                return TEXT_VRAM_TOP;
1.1       root     7033:        } else {
1.1.1.14  root     7034:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     7035:        }
                   7036: }
                   7037: 
1.1.1.16  root     7038: int pcbios_get_shadow_buffer_address(int page)
1.1       root     7039: {
1.1.1.14  root     7040:        if(!int_10h_feh_called) {
1.1.1.16  root     7041:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     7042:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7043:                return SHADOW_BUF_TOP;
                   7044:        } else {
1.1.1.14  root     7045:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     7046:        }
                   7047: }
                   7048: 
1.1.1.16  root     7049: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     7050: {
1.1.1.16  root     7051:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     7052: }
                   7053: 
1.1.1.56  root     7054: bool pcbios_set_font_size(int width, int height)
                   7055: {
1.1.1.61! root     7056:        if(set_console_font_size(width, height)) {
        !          7057:                *(UINT16 *)(mem + 0x485) = height;
        !          7058:                return(true);
        !          7059:        }
        !          7060:        return(false);
1.1.1.56  root     7061: }
                   7062: 
1.1.1.16  root     7063: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     7064: {
1.1.1.14  root     7065:        // clear the existing screen, not just the new one
                   7066:        int clr_height = max(height, scr_height);
                   7067:        
1.1.1.16  root     7068:        if(scr_width != width || scr_height != height) {
                   7069:                change_console_size(width, height);
1.1.1.14  root     7070:        }
                   7071:        mem[0x462] = 0;
                   7072:        *(UINT16 *)(mem + 0x44e) = 0;
                   7073:        
1.1.1.16  root     7074:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   7075:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   7076:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   7077:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     7078:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7079:        
1.1.1.23  root     7080:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     7081:        if(clr_screen) {
1.1.1.14  root     7082:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   7083:                        mem[ofs++] = 0x20;
                   7084:                        mem[ofs++] = 0x07;
                   7085:                }
                   7086:                
1.1.1.35  root     7087: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7088:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7089: #endif
1.1.1.14  root     7090:                for(int y = 0; y < clr_height; y++) {
                   7091:                        for(int x = 0; x < scr_width; x++) {
                   7092:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7093:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     7094:                        }
                   7095:                }
                   7096:                SMALL_RECT rect;
1.1.1.14  root     7097:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
1.1.1.60  root     7098:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7099:                vram_length_char = vram_last_length_char = 0;
                   7100:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     7101: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7102:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7103: #endif
1.1       root     7104:        }
1.1.1.14  root     7105:        COORD co;
                   7106:        co.X = 0;
                   7107:        co.Y = scr_top;
                   7108:        SetConsoleCursorPosition(hStdout, co);
                   7109:        cursor_moved = true;
1.1.1.59  root     7110:        cursor_moved_by_crtc = false;
1.1.1.14  root     7111:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     7112: }
                   7113: 
1.1.1.36  root     7114: void pcbios_update_cursor_position()
                   7115: {
                   7116:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7117:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   7118:        if(!restore_console_on_exit) {
                   7119:                scr_top = csbi.srWindow.Top;
                   7120:        }
                   7121:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   7122:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   7123: }
                   7124: 
1.1.1.16  root     7125: inline void pcbios_int_10h_00h()
                   7126: {
                   7127:        switch(REG8(AL) & 0x7f) {
                   7128:        case 0x70: // v-text mode
                   7129:        case 0x71: // extended cga v-text mode
                   7130:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   7131:                break;
1.1.1.61! root     7132:        case 0x73:
        !          7133:        case 0x03:
        !          7134:                change_console_size(80, 25); // for Windows10
        !          7135:                pcbios_set_font_size(font_width, font_height);
1.1.1.16  root     7136:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   7137:                break;
                   7138:        }
                   7139:        if(REG8(AL) & 0x80) {
                   7140:                mem[0x487] |= 0x80;
                   7141:        } else {
                   7142:                mem[0x487] &= ~0x80;
                   7143:        }
                   7144:        mem[0x449] = REG8(AL) & 0x7f;
                   7145: }
                   7146: 
1.1       root     7147: inline void pcbios_int_10h_01h()
                   7148: {
1.1.1.13  root     7149:        mem[0x460] = REG8(CL);
                   7150:        mem[0x461] = REG8(CH);
1.1.1.14  root     7151:        
1.1.1.60  root     7152:        int size = (int)(REG8(CL) & 7) - (int)(REG8(CH) & 7) + 1;
                   7153:        
                   7154:        if(!((REG8(CH) & 0x20) != 0 || size < 0)) {
                   7155:                ci_new.bVisible = TRUE;
                   7156:                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
                   7157:        } else {
                   7158:                ci_new.bVisible = FALSE;
                   7159:        }
1.1       root     7160: }
                   7161: 
                   7162: inline void pcbios_int_10h_02h()
                   7163: {
1.1.1.14  root     7164:        // continuously setting the cursor effectively stops it blinking
1.1.1.59  root     7165:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2] || cursor_moved_by_crtc)) {
1.1       root     7166:                COORD co;
                   7167:                co.X = REG8(DL);
1.1.1.14  root     7168:                co.Y = REG8(DH) + scr_top;
                   7169:                
                   7170:                // some programs hide the cursor by moving it off screen
                   7171:                static bool hidden = false;
1.1.1.23  root     7172:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7173:                
                   7174:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
1.1.1.59  root     7175:                        if(ci_new.bVisible) {
                   7176:                                ci_new.bVisible = FALSE;
1.1.1.14  root     7177:                                hidden = true;
                   7178:                        }
                   7179:                } else if(hidden) {
1.1.1.59  root     7180:                        if(!ci_new.bVisible) {
                   7181:                                ci_new.bVisible = TRUE;
1.1.1.14  root     7182:                        }
                   7183:                        hidden = false;
                   7184:                }
1.1.1.59  root     7185:                cursor_moved_by_crtc = false;
1.1       root     7186:        }
1.1.1.14  root     7187:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   7188:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     7189: }
                   7190: 
                   7191: inline void pcbios_int_10h_03h()
                   7192: {
1.1.1.14  root     7193:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7194:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7195:        REG8(CL) = mem[0x460];
                   7196:        REG8(CH) = mem[0x461];
                   7197: }
                   7198: 
                   7199: inline void pcbios_int_10h_05h()
                   7200: {
1.1.1.14  root     7201:        if(REG8(AL) >= vram_pages) {
                   7202:                return;
                   7203:        }
                   7204:        if(mem[0x462] != REG8(AL)) {
                   7205:                vram_flush();
                   7206:                
1.1.1.23  root     7207:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7208:                SMALL_RECT rect;
1.1.1.14  root     7209:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7210:                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7211:                
1.1.1.16  root     7212:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     7213:                        for(int x = 0; x < scr_width; x++) {
                   7214:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7215:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7216:                        }
                   7217:                }
1.1.1.16  root     7218:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     7219:                        for(int x = 0; x < scr_width; x++) {
                   7220:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   7221:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     7222:                        }
                   7223:                }
1.1.1.60  root     7224:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7225:                
                   7226:                COORD co;
1.1.1.14  root     7227:                co.X = mem[0x450 + REG8(AL) * 2];
                   7228:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   7229:                if(co.Y < scr_top + scr_height) {
                   7230:                        SetConsoleCursorPosition(hStdout, co);
                   7231:                }
1.1.1.59  root     7232:                cursor_moved_by_crtc = false;
1.1       root     7233:        }
1.1.1.14  root     7234:        mem[0x462] = REG8(AL);
                   7235:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7236:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7237:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7238:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7239:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7240:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7241:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7242: }
                   7243: 
                   7244: inline void pcbios_int_10h_06h()
                   7245: {
1.1.1.14  root     7246:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7247:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7248:                return;
                   7249:        }
                   7250:        vram_flush();
                   7251:        
1.1.1.23  root     7252:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7253:        SMALL_RECT rect;
1.1.1.14  root     7254:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7255:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7256:        
                   7257:        int right = min(REG8(DL), scr_width - 1);
                   7258:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7259:        
                   7260:        if(REG8(AL) == 0) {
1.1.1.14  root     7261:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7262:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7263:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7264:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7265:                        }
                   7266:                }
                   7267:        } else {
1.1.1.14  root     7268:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7269:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7270:                                if(y2 <= bottom) {
                   7271:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7272:                                } else {
1.1.1.14  root     7273:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7274:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7275:                                }
1.1.1.14  root     7276:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7277:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7278:                        }
                   7279:                }
                   7280:        }
1.1.1.60  root     7281:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7282: }
                   7283: 
                   7284: inline void pcbios_int_10h_07h()
                   7285: {
1.1.1.14  root     7286:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7287:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7288:                return;
                   7289:        }
                   7290:        vram_flush();
                   7291:        
1.1.1.23  root     7292:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7293:        SMALL_RECT rect;
1.1.1.14  root     7294:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7295:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7296:        
                   7297:        int right = min(REG8(DL), scr_width - 1);
                   7298:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7299:        
                   7300:        if(REG8(AL) == 0) {
1.1.1.14  root     7301:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7302:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7303:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7304:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7305:                        }
                   7306:                }
                   7307:        } else {
1.1.1.14  root     7308:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7309:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7310:                                if(y2 >= REG8(CH)) {
                   7311:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7312:                                } else {
1.1.1.14  root     7313:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7314:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7315:                                }
1.1.1.14  root     7316:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7317:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7318:                        }
                   7319:                }
                   7320:        }
1.1.1.60  root     7321:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7322: }
                   7323: 
                   7324: inline void pcbios_int_10h_08h()
                   7325: {
                   7326:        COORD co;
                   7327:        DWORD num;
                   7328:        
1.1.1.14  root     7329:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7330:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7331:        
                   7332:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7333:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7334:                co.Y += scr_top;
                   7335:                vram_flush();
1.1.1.60  root     7336:                ReadConsoleOutputCharacterA(hStdout, scr_char, 1, co, &num);
1.1       root     7337:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7338:                REG8(AL) = scr_char[0];
                   7339:                REG8(AH) = scr_attr[0];
                   7340:        } else {
1.1.1.16  root     7341:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7342:        }
                   7343: }
                   7344: 
                   7345: inline void pcbios_int_10h_09h()
                   7346: {
                   7347:        COORD co;
                   7348:        
1.1.1.14  root     7349:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7350:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7351:        
1.1.1.16  root     7352:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7353:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7354:        
                   7355:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7356: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7357:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7358: #endif
1.1.1.16  root     7359:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7360:                while(dest < end) {
                   7361:                        write_text_vram_char(dest - vram, REG8(AL));
                   7362:                        mem[dest++] = REG8(AL);
                   7363:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7364:                        mem[dest++] = REG8(BL);
1.1       root     7365:                }
1.1.1.35  root     7366: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7367:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7368: #endif
1.1       root     7369:        } else {
1.1.1.14  root     7370:                while(dest < end) {
1.1       root     7371:                        mem[dest++] = REG8(AL);
                   7372:                        mem[dest++] = REG8(BL);
                   7373:                }
                   7374:        }
                   7375: }
                   7376: 
                   7377: inline void pcbios_int_10h_0ah()
                   7378: {
                   7379:        COORD co;
                   7380:        
1.1.1.14  root     7381:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7382:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7383:        
1.1.1.16  root     7384:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7385:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7386:        
                   7387:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7388: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7389:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7390: #endif
1.1.1.16  root     7391:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7392:                while(dest < end) {
                   7393:                        write_text_vram_char(dest - vram, REG8(AL));
                   7394:                        mem[dest++] = REG8(AL);
                   7395:                        dest++;
1.1       root     7396:                }
1.1.1.35  root     7397: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7398:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7399: #endif
1.1       root     7400:        } else {
1.1.1.14  root     7401:                while(dest < end) {
1.1       root     7402:                        mem[dest++] = REG8(AL);
                   7403:                        dest++;
                   7404:                }
                   7405:        }
                   7406: }
                   7407: 
1.1.1.40  root     7408: inline void pcbios_int_10h_0ch()
                   7409: {
                   7410:        HDC hdc = get_console_window_device_context();
                   7411:        
                   7412:        if(hdc != NULL) {
                   7413:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7414:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7415:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7416:                
                   7417:                if(REG8(AL) & 0x80) {
                   7418:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7419:                        if(color != CLR_INVALID) {
                   7420:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7421:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7422:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7423:                        }
                   7424:                }
                   7425:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7426:        }
                   7427: }
                   7428: 
                   7429: inline void pcbios_int_10h_0dh()
                   7430: {
                   7431:        HDC hdc = get_console_window_device_context();
                   7432:        BYTE r = 0;
                   7433:        BYTE g = 0;
                   7434:        BYTE b = 0;
                   7435:        
                   7436:        if(hdc != NULL) {
                   7437:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7438:                if(color != CLR_INVALID) {
                   7439:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7440:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7441:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7442:                }
                   7443:        }
                   7444:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7445: }
                   7446: 
1.1       root     7447: inline void pcbios_int_10h_0eh()
                   7448: {
1.1.1.59  root     7449:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7450:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     7451:        DWORD num;
                   7452:        COORD co;
                   7453:        
1.1.1.59  root     7454:        if(cursor_moved_by_crtc) {
                   7455:                if(!restore_console_on_exit) {
                   7456:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7457:                        scr_top = csbi.srWindow.Top;
                   7458:                }
                   7459:                co.X = mem[0x450 + REG8(BH) * 2];
                   7460:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   7461:                SetConsoleCursorPosition(hStdout, co);
                   7462:                cursor_moved_by_crtc = false;
                   7463:        }
1.1.1.54  root     7464:        co.X = mem[0x450 + mem[0x462] * 2];
                   7465:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7466:        
                   7467:        if(REG8(AL) == 7) {
                   7468:                //MessageBeep(-1);
                   7469:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7470:                if(REG8(AL) == 10) {
                   7471:                        vram_flush();
                   7472:                }
1.1.1.60  root     7473:                WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7474:                cursor_moved = true;
                   7475:        } else {
1.1.1.54  root     7476:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7477: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7478:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7479: #endif
1.1.1.54  root     7480:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7481:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7482: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7483:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7484: #endif
1.1.1.54  root     7485:                
                   7486:                if(++co.X == scr_width) {
                   7487:                        co.X = 0;
                   7488:                        if(++co.Y == scr_height) {
                   7489:                                vram_flush();
1.1.1.60  root     7490:                                WriteConsoleA(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7491:                                cursor_moved = true;
                   7492:                        }
                   7493:                }
1.1.1.54  root     7494:                if(!cursor_moved) {
                   7495:                        co.Y += scr_top;
                   7496:                        SetConsoleCursorPosition(hStdout, co);
                   7497:                        cursor_moved = true;
                   7498:                }
1.1.1.14  root     7499:                mem[dest] = REG8(AL);
                   7500:        }
1.1       root     7501: }
                   7502: 
                   7503: inline void pcbios_int_10h_0fh()
                   7504: {
                   7505:        REG8(AL) = mem[0x449];
                   7506:        REG8(AH) = mem[0x44a];
                   7507:        REG8(BH) = mem[0x462];
                   7508: }
                   7509: 
1.1.1.14  root     7510: inline void pcbios_int_10h_11h()
                   7511: {
                   7512:        switch(REG8(AL)) {
1.1.1.58  root     7513:        case 0x00:
                   7514:        case 0x10:
1.1.1.61! root     7515:                if(REG8(BH)) {
        !          7516:                        change_console_size(80, 25); // for Windows10
        !          7517:                        if(!pcbios_set_font_size(font_width, (int)REG8(BH))) {
        !          7518:                                for(int h = min(font_height, (int)REG8(BH)); h <= max(font_height, (int)REG8(BH)); h++) {
        !          7519:                                        if(h != (int)REG8(BH)) {
        !          7520:                                                if(pcbios_set_font_size(font_width, h)) {
        !          7521:                                                        break;
        !          7522:                                                }
        !          7523:                                        }
        !          7524:                                }
        !          7525:                        }
1.1.1.58  root     7526:                        pcbios_set_console_size(80, (25 * 16) / REG8(BH), true);
                   7527:                }
                   7528:                break;
1.1.1.16  root     7529:        case 0x01:
1.1.1.14  root     7530:        case 0x11:
1.1.1.61! root     7531:                change_console_size(80, 28); // for Windows10
        !          7532:                if(!pcbios_set_font_size(font_width, 14)) {
        !          7533:                        for(int h = min(font_height, 14); h <= max(font_height, 14); h++) {
        !          7534:                                if(h != 14) {
        !          7535:                                        if(pcbios_set_font_size(font_width, h)) {
        !          7536:                                                break;
        !          7537:                                        }
        !          7538:                                }
        !          7539:                        }
1.1.1.56  root     7540:                }
1.1.1.61! root     7541:                pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
1.1.1.14  root     7542:                break;
1.1.1.16  root     7543:        case 0x02:
1.1.1.14  root     7544:        case 0x12:
1.1.1.61! root     7545:                change_console_size(80, 25); // for Windows10
        !          7546:                if(!pcbios_set_font_size(8, 8)) {
        !          7547:                        bool success = false;
        !          7548:                        for(int y = 8; y <= 14; y++) {
        !          7549:                                for(int x = min(font_width, 6); x <= max(font_width, 8); x++) {
        !          7550:                                        if(pcbios_set_font_size(x, y)) {
        !          7551:                                                success = true;
        !          7552:                                                break;
        !          7553:                                        }
        !          7554:                                }
        !          7555:                        }
        !          7556:                        if(!success) {
        !          7557:                                pcbios_set_font_size(font_width, font_height);
        !          7558:                        }
1.1.1.56  root     7559:                }
1.1.1.61! root     7560:                pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
1.1.1.14  root     7561:                break;
1.1.1.16  root     7562:        case 0x04:
1.1.1.14  root     7563:        case 0x14:
1.1.1.61! root     7564:                change_console_size(80, 25); // for Windows10
        !          7565:                pcbios_set_font_size(font_width, font_height);
        !          7566:                pcbios_set_console_size(80, 25, true);
1.1.1.58  root     7567:                break;
                   7568:        case 0x18:
1.1.1.61! root     7569:                change_console_size(80, 25); // for Windows10
        !          7570:                pcbios_set_font_size(font_width, font_height);
        !          7571:                pcbios_set_console_size(80, 25, true);
1.1.1.14  root     7572:                break;
                   7573:        case 0x30:
                   7574:                SREG(ES) = 0;
                   7575:                i386_load_segment_descriptor(ES);
                   7576:                REG16(BP) = 0;
                   7577:                REG16(CX) = mem[0x485];
                   7578:                REG8(DL) = mem[0x484];
                   7579:                break;
1.1.1.54  root     7580:        default:
                   7581:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7582:                m_CF = 1;
                   7583:                break;
1.1.1.14  root     7584:        }
                   7585: }
                   7586: 
                   7587: inline void pcbios_int_10h_12h()
                   7588: {
1.1.1.16  root     7589:        switch(REG8(BL)) {
                   7590:        case 0x10:
1.1.1.14  root     7591:                REG16(BX) = 0x0003;
                   7592:                REG16(CX) = 0x0009;
1.1.1.16  root     7593:                break;
1.1.1.14  root     7594:        }
                   7595: }
                   7596: 
1.1       root     7597: inline void pcbios_int_10h_13h()
                   7598: {
1.1.1.3   root     7599:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7600:        COORD co;
                   7601:        DWORD num;
                   7602:        
                   7603:        co.X = REG8(DL);
1.1.1.14  root     7604:        co.Y = REG8(DH) + scr_top;
                   7605:        
                   7606:        vram_flush();
1.1       root     7607:        
                   7608:        switch(REG8(AL)) {
                   7609:        case 0x00:
                   7610:        case 0x01:
                   7611:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7612:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7613:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7614:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7615:                        SetConsoleCursorPosition(hStdout, co);
                   7616:                        
                   7617:                        if(csbi.wAttributes != REG8(BL)) {
                   7618:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7619:                        }
1.1.1.60  root     7620:                        WriteConsoleA(hStdout, &mem[ofs], REG16(CX), &num, NULL);
1.1.1.14  root     7621:                        
1.1       root     7622:                        if(csbi.wAttributes != REG8(BL)) {
                   7623:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7624:                        }
                   7625:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7626:                                if(!restore_console_on_exit) {
                   7627:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7628:                                        scr_top = csbi.srWindow.Top;
                   7629:                                }
1.1.1.14  root     7630:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7631:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7632:                                SetConsoleCursorPosition(hStdout, co);
                   7633:                        } else {
                   7634:                                cursor_moved = true;
                   7635:                        }
1.1.1.59  root     7636:                        cursor_moved_by_crtc = false;
1.1       root     7637:                } else {
1.1.1.3   root     7638:                        m_CF = 1;
1.1       root     7639:                }
                   7640:                break;
                   7641:        case 0x02:
                   7642:        case 0x03:
                   7643:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7644:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7645:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7646:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7647:                        SetConsoleCursorPosition(hStdout, co);
                   7648:                        
                   7649:                        WORD wAttributes = csbi.wAttributes;
                   7650:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7651:                                if(wAttributes != mem[ofs + 1]) {
                   7652:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7653:                                        wAttributes = mem[ofs + 1];
                   7654:                                }
1.1.1.60  root     7655:                                WriteConsoleA(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7656:                        }
                   7657:                        if(csbi.wAttributes != wAttributes) {
                   7658:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7659:                        }
                   7660:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7661:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7662:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7663:                                SetConsoleCursorPosition(hStdout, co);
                   7664:                        } else {
                   7665:                                cursor_moved = true;
                   7666:                        }
1.1.1.59  root     7667:                        cursor_moved_by_crtc = false;
1.1       root     7668:                } else {
1.1.1.3   root     7669:                        m_CF = 1;
1.1       root     7670:                }
                   7671:                break;
                   7672:        case 0x10:
                   7673:        case 0x11:
                   7674:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7675:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.60  root     7676:                        ReadConsoleOutputCharacterA(hStdout, scr_char, REG16(CX), co, &num);
1.1       root     7677:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7678:                        for(int i = 0; i < num; i++) {
                   7679:                                mem[ofs++] = scr_char[i];
                   7680:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7681:                                if(REG8(AL) & 0x01) {
1.1       root     7682:                                        mem[ofs++] = 0;
                   7683:                                        mem[ofs++] = 0;
                   7684:                                }
                   7685:                        }
                   7686:                } else {
1.1.1.16  root     7687:                        for(int i = 0, src = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y - scr_top); i < REG16(CX); i++) {
1.1       root     7688:                                mem[ofs++] = mem[src++];
                   7689:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7690:                                if(REG8(AL) & 0x01) {
1.1       root     7691:                                        mem[ofs++] = 0;
                   7692:                                        mem[ofs++] = 0;
                   7693:                                }
1.1.1.14  root     7694:                                if(++co.X == scr_width) {
                   7695:                                        if(++co.Y == scr_height) {
1.1       root     7696:                                                break;
                   7697:                                        }
                   7698:                                        co.X = 0;
                   7699:                                }
                   7700:                        }
                   7701:                }
                   7702:                break;
1.1.1.45  root     7703:        case 0x12: // ???
                   7704:        case 0x13: // ???
1.1       root     7705:        case 0x20:
                   7706:        case 0x21:
                   7707:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7708:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7709:                        int len = min(REG16(CX), scr_width * scr_height);
                   7710:                        for(int i = 0; i < len; i++) {
1.1       root     7711:                                scr_char[i] = mem[ofs++];
                   7712:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7713:                                if(REG8(AL) & 0x01) {
1.1       root     7714:                                        ofs += 2;
                   7715:                                }
                   7716:                        }
1.1.1.60  root     7717:                        WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7718:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7719:                } else {
1.1.1.16  root     7720:                        for(int i = 0, dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y - scr_top); i < REG16(CX); i++) {
1.1       root     7721:                                mem[dest++] = mem[ofs++];
                   7722:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7723:                                if(REG8(AL) & 0x01) {
1.1       root     7724:                                        ofs += 2;
                   7725:                                }
1.1.1.14  root     7726:                                if(++co.X == scr_width) {
                   7727:                                        if(++co.Y == scr_height) {
1.1       root     7728:                                                break;
                   7729:                                        }
                   7730:                                        co.X = 0;
                   7731:                                }
                   7732:                        }
                   7733:                }
                   7734:                break;
                   7735:        default:
1.1.1.22  root     7736:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     7737:                m_CF = 1;
1.1       root     7738:                break;
                   7739:        }
                   7740: }
                   7741: 
1.1.1.30  root     7742: inline void pcbios_int_10h_18h()
                   7743: {
                   7744:        switch(REG8(AL)) {
                   7745:        case 0x00:
                   7746:        case 0x01:
                   7747: //             REG8(AL) = 0x86;
                   7748:                REG8(AL) = 0x00;
                   7749:                break;
                   7750:        default:
                   7751:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7752:                m_CF = 1;
                   7753:                break;
                   7754:        }
                   7755: }
                   7756: 
1.1.1.14  root     7757: inline void pcbios_int_10h_1ah()
                   7758: {
                   7759:        switch(REG8(AL)) {
                   7760:        case 0x00:
                   7761:                REG8(AL) = 0x1a;
                   7762:                REG8(BL) = 0x08;
                   7763:                REG8(BH) = 0x00;
                   7764:                break;
                   7765:        default:
1.1.1.22  root     7766:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     7767:                m_CF = 1;
                   7768:                break;
                   7769:        }
                   7770: }
                   7771: 
1.1       root     7772: inline void pcbios_int_10h_1dh()
                   7773: {
                   7774:        switch(REG8(AL)) {
1.1.1.43  root     7775:        case 0x00:
                   7776:                // DOS/V Shift Status Line Control is not supported
                   7777:                m_CF = 1;
                   7778:                break;
1.1       root     7779:        case 0x01:
                   7780:                break;
                   7781:        case 0x02:
                   7782:                REG16(BX) = 0;
                   7783:                break;
                   7784:        default:
1.1.1.22  root     7785:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7786:                m_CF = 1;
                   7787:                break;
                   7788:        }
                   7789: }
                   7790: 
                   7791: inline void pcbios_int_10h_4fh()
                   7792: {
                   7793:        switch(REG8(AL)) {
                   7794:        case 0x00:
                   7795:                REG8(AH) = 0x02; // not supported
                   7796:                break;
                   7797:        case 0x01:
                   7798:        case 0x02:
                   7799:        case 0x03:
                   7800:        case 0x04:
                   7801:        case 0x05:
                   7802:        case 0x06:
                   7803:        case 0x07:
                   7804:        case 0x08:
                   7805:        case 0x09:
                   7806:        case 0x0a:
                   7807:        case 0x0b:
                   7808:        case 0x0c:
                   7809:                REG8(AH) = 0x01; // failed
                   7810:                break;
                   7811:        default:
                   7812:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     7813:                m_CF = 1;
1.1       root     7814:                break;
                   7815:        }
                   7816: }
                   7817: 
                   7818: inline void pcbios_int_10h_82h()
                   7819: {
                   7820:        static UINT8 mode = 0;
                   7821:        
                   7822:        switch(REG8(AL)) {
1.1.1.22  root     7823:        case 0x00:
1.1       root     7824:                if(REG8(BL) != 0xff) {
                   7825:                        mode = REG8(BL);
                   7826:                }
                   7827:                REG8(AL) = mode;
                   7828:                break;
                   7829:        default:
1.1.1.22  root     7830:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     7831:                m_CF = 1;
1.1       root     7832:                break;
                   7833:        }
                   7834: }
                   7835: 
1.1.1.22  root     7836: inline void pcbios_int_10h_83h()
                   7837: {
                   7838:        static UINT8 mode = 0;
                   7839:        
                   7840:        switch(REG8(AL)) {
                   7841:        case 0x00:
                   7842:                REG16(AX) = 0; // offset???
                   7843:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7844:                i386_load_segment_descriptor(ES);
                   7845:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7846:                break;
                   7847:        default:
                   7848:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7849:                m_CF = 1;
                   7850:                break;
                   7851:        }
                   7852: }
                   7853: 
                   7854: inline void pcbios_int_10h_90h()
                   7855: {
                   7856:        REG8(AL) = mem[0x449];
                   7857: }
                   7858: 
                   7859: inline void pcbios_int_10h_91h()
                   7860: {
                   7861:        REG8(AL) = 0x04; // VGA
                   7862: }
                   7863: 
                   7864: inline void pcbios_int_10h_efh()
                   7865: {
                   7866:        REG16(DX) = 0xffff;
                   7867: }
                   7868: 
1.1       root     7869: inline void pcbios_int_10h_feh()
                   7870: {
                   7871:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7872:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7873:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7874:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7875:        }
                   7876:        int_10h_feh_called = true;
                   7877: }
                   7878: 
                   7879: inline void pcbios_int_10h_ffh()
                   7880: {
                   7881:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7882:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7883:                COORD co;
                   7884:                DWORD num;
                   7885:                
1.1.1.14  root     7886:                vram_flush();
                   7887:                
                   7888:                co.X = (REG16(DI) >> 1) % scr_width;
                   7889:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7890:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7891:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7892:                int len;
                   7893:                for(len = 0; ofs < end; len++) {
                   7894:                        scr_char[len] = mem[ofs++];
                   7895:                        scr_attr[len] = mem[ofs++];
                   7896:                }
                   7897:                co.Y += scr_top;
1.1.1.60  root     7898:                WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7899:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7900:        }
                   7901:        int_10h_ffh_called = true;
                   7902: }
                   7903: 
1.1.1.42  root     7904: int pcbios_update_drive_param(int drive_num, int force_update)
                   7905: {
                   7906:        if(drive_num >= 0 && drive_num < 26) {
                   7907:                drive_param_t *drive_param = &drive_params[drive_num];
                   7908:                
                   7909:                if(force_update || !drive_param->initialized) {
                   7910:                        drive_param->valid = 0;
                   7911:                        char dev[64];
                   7912:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7913:                        
1.1.1.60  root     7914:                        HANDLE hFile = CreateFileA(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.42  root     7915:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7916:                                DWORD dwSize;
                   7917:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7918:                                        drive_param->valid = 1;
                   7919:                                }
                   7920:                                CloseHandle(hFile);
                   7921:                        }
                   7922:                        drive_param->initialized = 1;
                   7923:                }
                   7924:                return(drive_param->valid);
                   7925:        }
                   7926:        return(0);
                   7927: }
                   7928: 
                   7929: inline void pcbios_int_13h_00h()
                   7930: {
                   7931:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7932:        
                   7933:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7934:                REG8(AH) = 0x00; // successful completion
                   7935:        } else {
                   7936:                if(REG8(DL) & 0x80) {
                   7937:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7938:                } else {
                   7939:                        REG8(AH) = 0x80; // timeout (not ready)
                   7940:                }
                   7941:                m_CF = 1;
                   7942:        }
                   7943: }
                   7944: 
                   7945: inline void pcbios_int_13h_02h()
                   7946: {
                   7947:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7948:        
                   7949:        if(REG8(AL) == 0) {
                   7950:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7951:                m_CF = 1;
                   7952:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7953:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7954:                m_CF = 1;
                   7955:        } else {
                   7956:                drive_param_t *drive_param = &drive_params[drive_num];
                   7957:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7958:                char dev[64];
                   7959:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7960:                
1.1.1.60  root     7961:                HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1.1.42  root     7962:                if(hFile == INVALID_HANDLE_VALUE) {
                   7963:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7964:                        m_CF = 1;
                   7965:                } else {
                   7966:                        UINT32 sector_num = REG8(AL);
                   7967:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7968:                        UINT32 head = REG8(DH);
                   7969:                        UINT32 sector = REG8(CL) & 0x3f;
                   7970:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7971:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7972:                        DWORD dwSize;
                   7973:                        
                   7974: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7975: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7976: //                             m_CF = 1;
                   7977: //                     } else 
                   7978:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7979:                                REG8(AH) = 0x04; // sector not found/read error
                   7980:                                m_CF = 1;
                   7981:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7982:                                REG8(AH) = 0x04; // sector not found/read error
                   7983:                                m_CF = 1;
                   7984:                        } else {
                   7985:                                REG8(AH) = 0x00; // successful completion
                   7986:                        }
                   7987:                        CloseHandle(hFile);
                   7988:                }
                   7989:        }
                   7990: }
                   7991: 
                   7992: inline void pcbios_int_13h_03h()
                   7993: {
                   7994:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7995:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7996:        
                   7997:        if(REG8(AL) == 0) {
                   7998:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7999:                m_CF = 1;
                   8000:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8001:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8002:                m_CF = 1;
                   8003:        } else if(!drive_params[drive_num].is_fdd()) {
                   8004:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8005:                m_CF = 1;
                   8006:        } else {
                   8007:                drive_param_t *drive_param = &drive_params[drive_num];
                   8008:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8009:                char dev[64];
                   8010:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8011:                
1.1.1.60  root     8012:                HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1.1.42  root     8013:                if(hFile == INVALID_HANDLE_VALUE) {
                   8014:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8015:                        m_CF = 1;
                   8016:                } else {
                   8017:                        UINT32 sector_num = REG8(AL);
                   8018:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8019:                        UINT32 head = REG8(DH);
                   8020:                        UINT32 sector = REG8(CL) & 0x3f;
                   8021:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8022:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8023:                        DWORD dwSize;
                   8024:                        
                   8025: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8026: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8027: //                             m_CF = 1;
                   8028: //                     } else 
                   8029:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8030:                                REG8(AH) = 0x04; // sector not found/read error
                   8031:                                m_CF = 1;
                   8032:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8033:                                REG8(AH) = 0x04; // sector not found/read error
                   8034:                                m_CF = 1;
                   8035:                        } else {
                   8036:                                REG8(AH) = 0x00; // successful completion
                   8037:                        }
                   8038:                        CloseHandle(hFile);
                   8039:                }
                   8040:        }
                   8041: }
                   8042: 
                   8043: inline void pcbios_int_13h_04h()
                   8044: {
                   8045:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8046:        
                   8047:        if(REG8(AL) == 0) {
                   8048:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8049:                m_CF = 1;
                   8050:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8051:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8052:                m_CF = 1;
                   8053:        } else {
                   8054:                drive_param_t *drive_param = &drive_params[drive_num];
                   8055:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8056:                char dev[64];
                   8057:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8058:                
1.1.1.60  root     8059:                HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1.1.42  root     8060:                if(hFile == INVALID_HANDLE_VALUE) {
                   8061:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8062:                        m_CF = 1;
                   8063:                } else {
                   8064:                        UINT32 sector_num = REG8(AL);
                   8065:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8066:                        UINT32 head = REG8(DH);
                   8067:                        UINT32 sector = REG8(CL) & 0x3f;
                   8068:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8069:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8070:                        DWORD dwSize;
                   8071:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   8072:                        
                   8073: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8074: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8075: //                             m_CF = 1;
                   8076: //                     } else 
                   8077:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8078:                                REG8(AH) = 0x04; // sector not found/read error
                   8079:                                m_CF = 1;
                   8080:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8081:                                REG8(AH) = 0x04; // sector not found/read error
                   8082:                                m_CF = 1;
                   8083:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   8084:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   8085:                                m_CF = 1;
                   8086:                        } else {
                   8087:                                REG8(AH) = 0x00; // successful completion
                   8088:                        }
                   8089:                        free(tmp_buffer);
                   8090:                        CloseHandle(hFile);
                   8091:                }
                   8092:        }
                   8093: }
                   8094: 
                   8095: inline void pcbios_int_13h_08h()
                   8096: {
                   8097:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8098:        
                   8099:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8100:                drive_param_t *drive_param = &drive_params[drive_num];
                   8101:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8102:                
                   8103:                REG16(AX) = 0x0000;
                   8104:                switch(geo->MediaType) {
                   8105:                case F5_360_512:
                   8106:                case F5_320_512:
                   8107:                case F5_320_1024:
                   8108:                case F5_180_512:
                   8109:                case F5_160_512:
                   8110:                        REG8(BL) = 0x01; // 320K/360K disk
                   8111:                        break;
                   8112:                case F5_1Pt2_512:
                   8113:                case F3_1Pt2_512:
                   8114:                case F3_1Pt23_1024:
                   8115:                case F5_1Pt23_1024:
                   8116:                        REG8(BL) = 0x02; // 1.2M disk
                   8117:                        break;
                   8118:                case F3_720_512:
                   8119:                case F3_640_512:
                   8120:                case F5_640_512:
                   8121:                case F5_720_512:
                   8122:                        REG8(BL) = 0x03; // 720K disk
                   8123:                        break;
                   8124:                case F3_1Pt44_512:
                   8125:                        REG8(BL) = 0x04; // 1.44M disk
                   8126:                        break;
                   8127:                case F3_2Pt88_512:
                   8128:                        REG8(BL) = 0x06; // 2.88M disk
                   8129:                        break;
                   8130:                case RemovableMedia:
                   8131:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   8132:                        break;
                   8133:                default:
                   8134:                        REG8(BL) = 0x00; // unknown
                   8135:                        break;
                   8136:                }
                   8137:                if(REG8(DL) & 0x80) {
                   8138:                        switch(GetLogicalDrives() & 0x0c) {
                   8139:                        case 0x00: REG8(DL) = 0x00; break;
                   8140:                        case 0x04:
                   8141:                        case 0x08: REG8(DL) = 0x01; break;
                   8142:                        case 0x0c: REG8(DL) = 0x02; break;
                   8143:                        }
                   8144:                } else {
                   8145:                        switch(GetLogicalDrives() & 0x03) {
                   8146:                        case 0x00: REG8(DL) = 0x00; break;
                   8147:                        case 0x01:
                   8148:                        case 0x02: REG8(DL) = 0x01; break;
                   8149:                        case 0x03: REG8(DL) = 0x02; break;
                   8150:                        }
                   8151:                }
                   8152:                REG8(DH) = drive_param->head_num();
                   8153:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   8154:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   8155:                REG8(CH) = cyl & 0xff;
                   8156:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   8157:        } else {
                   8158:                REG8(AH) = 0x07;
                   8159:                m_CF = 1;
                   8160:        }
                   8161: }
                   8162: 
                   8163: inline void pcbios_int_13h_10h()
                   8164: {
                   8165:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8166:        
                   8167:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8168:                REG8(AH) = 0x00; // successful completion
                   8169:        } else {
                   8170:                if(REG8(DL) & 0x80) {
                   8171:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   8172:                } else {
                   8173:                        REG8(AH) = 0x80; // timeout (not ready)
                   8174:                }
                   8175:                m_CF = 1;
                   8176:        }
                   8177: }
                   8178: 
                   8179: inline void pcbios_int_13h_15h()
                   8180: {
                   8181:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8182:        
                   8183:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8184:                if(REG8(DL) & 0x80) {
                   8185:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   8186:                } else {
                   8187:                        REG8(AH) = 0x03; // hard disk
                   8188:                }
                   8189:        } else {
                   8190:                REG8(AH) = 0x00; // no such drive
                   8191:        }
                   8192: }
                   8193: 
1.1.1.43  root     8194: inline void pcbios_int_13h_41h()
                   8195: {
                   8196:        if(REG16(BX) == 0x55aa) {
                   8197:                // IBM/MS INT 13 Extensions is not installed
                   8198:                REG8(AH) = 0x01;
                   8199:                m_CF = 1;
                   8200:        } else {
                   8201:                unimplemented_13h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x13, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8202:                REG8(AH) = 0x01;
                   8203:                m_CF = 1;
                   8204:        }
                   8205: }
                   8206: 
1.1.1.25  root     8207: inline void pcbios_int_14h_00h()
                   8208: {
1.1.1.29  root     8209:        if(REG16(DX) < 4) {
1.1.1.25  root     8210:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   8211:                UINT8 selector = sio_read(REG16(DX), 3);
                   8212:                selector &= ~0x3f;
                   8213:                selector |= REG8(AL) & 0x1f;
                   8214:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   8215:                sio_write(REG16(DX), 3, selector | 0x80);
                   8216:                sio_write(REG16(DX), 0, divisor & 0xff);
                   8217:                sio_write(REG16(DX), 1, divisor >> 8);
                   8218:                sio_write(REG16(DX), 3, selector);
                   8219:                REG8(AH) = sio_read(REG16(DX), 5);
                   8220:                REG8(AL) = sio_read(REG16(DX), 6);
                   8221:        } else {
                   8222:                REG8(AH) = 0x80;
                   8223:        }
                   8224: }
                   8225: 
                   8226: inline void pcbios_int_14h_01h()
                   8227: {
1.1.1.29  root     8228:        if(REG16(DX) < 4) {
1.1.1.25  root     8229:                UINT8 selector = sio_read(REG16(DX), 3);
                   8230:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8231:                sio_write(REG16(DX), 0, REG8(AL));
                   8232:                sio_write(REG16(DX), 3, selector);
                   8233:                REG8(AH) = sio_read(REG16(DX), 5);
                   8234:        } else {
                   8235:                REG8(AH) = 0x80;
                   8236:        }
                   8237: }
                   8238: 
                   8239: inline void pcbios_int_14h_02h()
                   8240: {
1.1.1.29  root     8241:        if(REG16(DX) < 4) {
1.1.1.25  root     8242:                UINT8 selector = sio_read(REG16(DX), 3);
                   8243:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8244:                REG8(AL) = sio_read(REG16(DX), 0);
                   8245:                sio_write(REG16(DX), 3, selector);
                   8246:                REG8(AH) = sio_read(REG16(DX), 5);
                   8247:        } else {
                   8248:                REG8(AH) = 0x80;
                   8249:        }
                   8250: }
                   8251: 
                   8252: inline void pcbios_int_14h_03h()
                   8253: {
1.1.1.29  root     8254:        if(REG16(DX) < 4) {
1.1.1.25  root     8255:                REG8(AH) = sio_read(REG16(DX), 5);
                   8256:                REG8(AL) = sio_read(REG16(DX), 6);
                   8257:        } else {
                   8258:                REG8(AH) = 0x80;
                   8259:        }
                   8260: }
                   8261: 
                   8262: inline void pcbios_int_14h_04h()
                   8263: {
1.1.1.29  root     8264:        if(REG16(DX) < 4) {
1.1.1.25  root     8265:                UINT8 selector = sio_read(REG16(DX), 3);
                   8266:                if(REG8(CH) <= 0x03) {
                   8267:                        selector = (selector & ~0x03) | REG8(CH);
                   8268:                }
                   8269:                if(REG8(BL) == 0x00) {
                   8270:                        selector &= ~0x04;
                   8271:                } else if(REG8(BL) == 0x01) {
                   8272:                        selector |= 0x04;
                   8273:                }
                   8274:                if(REG8(BH) == 0x00) {
                   8275:                        selector = (selector & ~0x38) | 0x00;
                   8276:                } else if(REG8(BH) == 0x01) {
                   8277:                        selector = (selector & ~0x38) | 0x08;
                   8278:                } else if(REG8(BH) == 0x02) {
                   8279:                        selector = (selector & ~0x38) | 0x18;
                   8280:                } else if(REG8(BH) == 0x03) {
                   8281:                        selector = (selector & ~0x38) | 0x28;
                   8282:                } else if(REG8(BH) == 0x04) {
                   8283:                        selector = (selector & ~0x38) | 0x38;
                   8284:                }
                   8285:                if(REG8(AL) == 0x00) {
                   8286:                        selector |= 0x40;
                   8287:                } else if(REG8(AL) == 0x01) {
                   8288:                        selector &= ~0x40;
                   8289:                }
                   8290:                if(REG8(CL) <= 0x0b) {
                   8291:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8292:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8293:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8294:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8295:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8296:                }
                   8297:                sio_write(REG16(DX), 3, selector);
                   8298:                REG8(AH) = sio_read(REG16(DX), 5);
                   8299:                REG8(AL) = sio_read(REG16(DX), 6);
                   8300:        } else {
                   8301:                REG8(AH) = 0x80;
                   8302:        }
                   8303: }
                   8304: 
                   8305: inline void pcbios_int_14h_05h()
                   8306: {
1.1.1.29  root     8307:        if(REG16(DX) < 4) {
1.1.1.25  root     8308:                if(REG8(AL) == 0x00) {
                   8309:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8310:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8311:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8312:                } else if(REG8(AL) == 0x01) {
                   8313:                        sio_write(REG16(DX), 4, REG8(BL));
                   8314:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8315:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8316:                } else {
                   8317:                        unimplemented_14h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x14, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8318:                }
                   8319:        } else {
                   8320:                REG8(AH) = 0x80;
                   8321:        }
                   8322: }
                   8323: 
1.1.1.14  root     8324: inline void pcbios_int_15h_10h()
                   8325: {
1.1.1.22  root     8326:        switch(REG8(AL)) {
                   8327:        case 0x00:
1.1.1.14  root     8328:                Sleep(10);
1.1.1.35  root     8329:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     8330:                break;
                   8331:        default:
                   8332:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     8333:                REG8(AH) = 0x86;
                   8334:                m_CF = 1;
                   8335:        }
                   8336: }
                   8337: 
1.1       root     8338: inline void pcbios_int_15h_23h()
                   8339: {
                   8340:        switch(REG8(AL)) {
1.1.1.22  root     8341:        case 0x00:
1.1.1.8   root     8342:                REG8(CL) = cmos_read(0x2d);
                   8343:                REG8(CH) = cmos_read(0x2e);
1.1       root     8344:                break;
1.1.1.22  root     8345:        case 0x01:
1.1.1.8   root     8346:                cmos_write(0x2d, REG8(CL));
                   8347:                cmos_write(0x2e, REG8(CH));
1.1       root     8348:                break;
                   8349:        default:
1.1.1.22  root     8350:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     8351:                REG8(AH) = 0x86;
1.1.1.3   root     8352:                m_CF = 1;
1.1       root     8353:                break;
                   8354:        }
                   8355: }
                   8356: 
                   8357: inline void pcbios_int_15h_24h()
                   8358: {
                   8359:        switch(REG8(AL)) {
1.1.1.22  root     8360:        case 0x00:
1.1.1.3   root     8361:                i386_set_a20_line(0);
1.1       root     8362:                REG8(AH) = 0;
                   8363:                break;
1.1.1.22  root     8364:        case 0x01:
1.1.1.3   root     8365:                i386_set_a20_line(1);
1.1       root     8366:                REG8(AH) = 0;
                   8367:                break;
1.1.1.22  root     8368:        case 0x02:
1.1       root     8369:                REG8(AH) = 0;
1.1.1.3   root     8370:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8371:                REG16(CX) = 0;
                   8372:                break;
1.1.1.22  root     8373:        case 0x03:
1.1       root     8374:                REG16(AX) = 0;
                   8375:                REG16(BX) = 0;
                   8376:                break;
1.1.1.22  root     8377:        default:
                   8378:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8379:                REG8(AH) = 0x86;
                   8380:                m_CF = 1;
                   8381:                break;
1.1       root     8382:        }
                   8383: }
                   8384: 
                   8385: inline void pcbios_int_15h_49h()
                   8386: {
1.1.1.27  root     8387:        REG8(AH) = 0x00;
                   8388:        REG8(BL) = 0x00; // DOS/V
1.1       root     8389: }
                   8390: 
1.1.1.22  root     8391: inline void pcbios_int_15h_50h()
                   8392: {
                   8393:        switch(REG8(AL)) {
                   8394:        case 0x00:
                   8395:        case 0x01:
                   8396:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8397:                        REG8(AH) = 0x01; // invalid font type in bh
                   8398:                        m_CF = 1;
1.1.1.27  root     8399:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8400:                        REG8(AH) = 0x02; // bl not zero
                   8401:                        m_CF = 1;
                   8402:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8403:                        REG8(AH) = 0x04; // invalid code page
                   8404:                        m_CF = 1;
1.1.1.27  root     8405:                } else if(REG8(AL) == 0x01) {
                   8406:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8407:                        m_CF = 1;
1.1.1.27  root     8408:                } else {
1.1.1.49  root     8409:                        // dummy font read routine is at fffc:000d
                   8410:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8411:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8412:                        REG16(BX) = 0x000d;
1.1.1.27  root     8413:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8414:                }
                   8415:                break;
                   8416:        default:
                   8417:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8418:                REG8(AH) = 0x86;
                   8419:                m_CF = 1;
                   8420:                break;
                   8421:        }
                   8422: }
                   8423: 
1.1.1.30  root     8424: inline void pcbios_int_15h_53h()
                   8425: {
                   8426:        switch(REG8(AL)) {
                   8427:        case 0x00:
                   8428:                // APM is not installed
                   8429:                REG8(AH) = 0x86;
                   8430:                m_CF = 1;
                   8431:                break;
                   8432:        default:
                   8433:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8434:                REG8(AH) = 0x86;
                   8435:                m_CF = 1;
                   8436:                break;
                   8437:        }
                   8438: }
                   8439: 
1.1.1.43  root     8440: inline void pcbios_int_15h_84h()
                   8441: {
                   8442:        // joystick support (from DOSBox)
                   8443:        switch(REG16(DX)) {
                   8444:        case 0x00:
                   8445:                REG16(AX) = 0x00f0;
                   8446:                REG16(DX) = 0x0201;
                   8447:                m_CF = 1;
                   8448:                break;
                   8449:        case 0x01:
                   8450:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8451:                m_CF = 1;
                   8452:                break;
                   8453:        default:
                   8454:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8455:                REG8(AH) = 0x86;
                   8456:                m_CF = 1;
                   8457:                break;
                   8458:        }
                   8459: }
1.1.1.35  root     8460: 
                   8461: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8462: {
                   8463:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8464:        UINT32 msec = usec / 1000;
                   8465:        
1.1.1.54  root     8466:        while(msec && !m_exit) {
1.1.1.14  root     8467:                UINT32 tmp = min(msec, 100);
                   8468:                if(msec - tmp < 10) {
                   8469:                        tmp = msec;
                   8470:                }
                   8471:                Sleep(tmp);
                   8472:                msec -= tmp;
                   8473:        }
1.1.1.35  root     8474:        
                   8475: #ifdef USE_SERVICE_THREAD
                   8476:        service_exit = true;
                   8477: #endif
                   8478:        return(0);
                   8479: }
                   8480: 
                   8481: inline void pcbios_int_15h_86h()
                   8482: {
                   8483:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8484: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8485:                if(!in_service && !in_service_29h) {
                   8486:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8487:                } else {
                   8488: #endif
                   8489:                        pcbios_int_15h_86h_thread(NULL);
                   8490:                        REQUEST_HARDWRE_UPDATE();
                   8491: #ifdef USE_SERVICE_THREAD
                   8492:                }
1.1.1.35  root     8493: #endif
                   8494:        }
1.1       root     8495: }
                   8496: 
                   8497: inline void pcbios_int_15h_87h()
                   8498: {
                   8499:        // copy extended memory (from DOSBox)
                   8500:        int len = REG16(CX) * 2;
1.1.1.3   root     8501:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8502:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8503:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8504:        memcpy(mem + dst, mem + src, len);
                   8505:        REG16(AX) = 0x00;
                   8506: }
                   8507: 
                   8508: inline void pcbios_int_15h_88h()
                   8509: {
1.1.1.17  root     8510:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8511: }
                   8512: 
                   8513: inline void pcbios_int_15h_89h()
                   8514: {
1.1.1.21  root     8515: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8516:        // switch to protected mode (from DOSBox)
                   8517:        write_io_byte(0x20, 0x10);
                   8518:        write_io_byte(0x21, REG8(BH));
                   8519:        write_io_byte(0x21, 0x00);
                   8520:        write_io_byte(0xa0, 0x10);
                   8521:        write_io_byte(0xa1, REG8(BL));
                   8522:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8523:        i386_set_a20_line(1);
                   8524:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8525:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8526:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8527:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8528:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8529: #if defined(HAS_I386)
                   8530:        m_cr[0] |= 1;
                   8531: #else
                   8532:        m_msw |= 1;
                   8533: #endif
                   8534:        SREG(DS) = 0x18;
                   8535:        SREG(ES) = 0x20;
                   8536:        SREG(SS) = 0x28;
                   8537:        i386_load_segment_descriptor(DS);
                   8538:        i386_load_segment_descriptor(ES);
                   8539:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8540:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8541:        REG16(SP) += 6;
1.1.1.3   root     8542: #if defined(HAS_I386)
1.1.1.21  root     8543:        UINT32 flags = get_flags();
                   8544:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8545:        set_flags(flags);
1.1.1.3   root     8546: #else
1.1.1.21  root     8547:        UINT32 flags = CompressFlags();
                   8548:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8549:        ExpandFlags(flags);
1.1.1.3   root     8550: #endif
1.1       root     8551:        REG16(AX) = 0x00;
1.1.1.21  root     8552:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8553: #else
1.1.1.21  root     8554:        // i86/i186/v30: protected mode is not supported
1.1       root     8555:        REG8(AH) = 0x86;
1.1.1.3   root     8556:        m_CF = 1;
1.1       root     8557: #endif
                   8558: }
                   8559: 
1.1.1.21  root     8560: inline void pcbios_int_15h_8ah()
                   8561: {
                   8562:        UINT32 size = MAX_MEM - 0x100000;
                   8563:        REG16(AX) = size & 0xffff;
                   8564:        REG16(DX) = size >> 16;
                   8565: }
                   8566: 
1.1.1.54  root     8567: #ifdef EXT_BIOS_TOP
                   8568: inline void pcbios_int_15h_c1h()
                   8569: {
                   8570:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8571:        i386_load_segment_descriptor(ES);
                   8572: }
                   8573: #endif
                   8574: 
                   8575: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8576: {
                   8577:        // from DOSBox DoPS2Callback()
                   8578:        UINT16 mdat = 0x08;
                   8579:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8580:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8581:        
1.1.1.59  root     8582: #if 1
                   8583:        if(xdiff > +16) xdiff = +16;
                   8584:        if(xdiff < -16) xdiff = -16;
                   8585:        if(ydiff > +16) ydiff = +16;
                   8586:        if(ydiff < -16) ydiff = -16;
                   8587: #endif
                   8588:        
1.1.1.54  root     8589:        if(mouse.buttons[0].status) {
                   8590:                mdat |= 0x01;
                   8591:        }
                   8592:        if(mouse.buttons[1].status) {
                   8593:                mdat |= 0x02;
                   8594:        }
                   8595:        mouse.prev_position.x = mouse.position.x;
                   8596:        mouse.prev_position.y = mouse.position.y;
1.1.1.59  root     8597:        
1.1.1.54  root     8598:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8599:                mdat |= 0x40;   // x overflow
                   8600:        }
                   8601:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8602:                mdat |= 0x80;   // y overflow
                   8603:        }
                   8604:        xdiff %= 256;
                   8605:        ydiff %= 256;
                   8606:        if(xdiff < 0) {
                   8607:                xdiff = (0x100 + xdiff);
                   8608:                mdat |= 0x10;
                   8609:        }
                   8610:        if(ydiff < 0) {
                   8611:                ydiff = (0x100 + ydiff);
                   8612:                mdat |= 0x20;
                   8613:        }
                   8614:        *data_1st = (UINT16)mdat;
                   8615:        *data_2nd = (UINT16)(xdiff % 256);
                   8616:        *data_3rd = (UINT16)(ydiff % 256);
                   8617: }
                   8618: 
                   8619: inline void pcbios_int_15h_c2h()
                   8620: {
                   8621:        static UINT8 sampling_rate = 5;
                   8622:        static UINT8 resolution = 2;
                   8623:        static UINT8 scaling = 1;
                   8624:        
                   8625:        switch(REG8(AL)) {
                   8626:        case 0x00:
1.1.1.59  root     8627:                if(REG8(BH) == 0x00) {
1.1.1.61! root     8628:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
1.1.1.59  root     8629:                        pic[1].imr |= 0x10; // disable irq12
                   8630:                        mouse.enabled_ps2 = false;
                   8631:                        REG8(AH) = 0x00; // successful
                   8632:                } else if(REG8(BH) == 0x01) {
1.1.1.61! root     8633:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~(ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE));
1.1.1.59  root     8634:                        pic[1].imr &= ~0x10; // enable irq12
                   8635:                        mouse.enabled_ps2 = true;
1.1.1.54  root     8636:                        REG8(AH) = 0x00; // successful
                   8637:                } else {
                   8638:                        REG8(AH) = 0x01; // invalid function
                   8639:                        m_CF = 1;
                   8640:                }
                   8641:                break;
                   8642:        case 0x01:
                   8643:                REG8(BH) = 0x00; // device id
                   8644:                REG8(BL) = 0xaa; // mouse
                   8645:        case 0x05:
1.1.1.61! root     8646:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
1.1.1.59  root     8647:                pic[1].imr |= 0x10; // disable irq12
                   8648:                mouse.enabled_ps2 = false;
1.1.1.54  root     8649:                sampling_rate = 5;
                   8650:                resolution = 2;
                   8651:                scaling = 1;
                   8652:                REG8(AH) = 0x00; // successful
                   8653:                break;
                   8654:        case 0x02:
                   8655:                sampling_rate = REG8(BH);
                   8656:                REG8(AH) = 0x00; // successful
                   8657:                break;
                   8658:        case 0x03:
                   8659:                resolution = REG8(BH);
                   8660:                REG8(AH) = 0x00; // successful
                   8661:                break;
                   8662:        case 0x04:
                   8663:                REG8(BH) = 0x00; // device id
                   8664:                REG8(AH) = 0x00; // successful
                   8665:                break;
                   8666:        case 0x06:
                   8667:                switch(REG8(BH)) {
                   8668:                case 0x00:
                   8669:                        REG8(BL) = 0x00;
                   8670:                        if(mouse.buttons[1].status) {
                   8671:                                REG8(BL) |= 0x01;
                   8672:                        }
                   8673:                        if(mouse.buttons[0].status) {
                   8674:                                REG8(BL) |= 0x04;
                   8675:                        }
                   8676:                        if(scaling == 2) {
                   8677:                                REG8(BL) |= 0x10;
                   8678:                        }
                   8679:                        REG8(CL) = resolution;
                   8680:                        switch(sampling_rate) {
                   8681:                        case 0:  REG8(DL) =  10; break;
                   8682:                        case 1:  REG8(DL) =  20; break;
                   8683:                        case 2:  REG8(DL) =  40; break;
                   8684:                        case 3:  REG8(DL) =  60; break;
                   8685:                        case 4:  REG8(DL) =  80; break;
                   8686: //                     case 5:  REG8(DL) = 100; break;
                   8687:                        case 6:  REG8(DL) = 200; break;
                   8688:                        default: REG8(DL) = 100; break;
                   8689:                        }
                   8690:                        REG8(AH) = 0x00; // successful
                   8691:                        break;
                   8692:                case 0x01:
                   8693:                case 0x02:
                   8694:                        scaling = REG8(BH);
                   8695:                        REG8(AH) = 0x00; // successful
                   8696:                        break;
                   8697:                default:
                   8698:                        unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8699:                        REG8(AH) = 0x01; // invalid function
                   8700:                        m_CF = 1;
                   8701:                        break;
                   8702:                }
                   8703:                break;
                   8704:        case 0x07: // set device handler addr
                   8705:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8706:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8707:                REG8(AH) = 0x00; // successful
                   8708:                break;
                   8709:        case 0x08:
                   8710:                REG8(AH) = 0x00; // successful
                   8711:                break;
                   8712:        case 0x09:
                   8713:                {
                   8714:                        UINT16 data_1st, data_2nd, data_3rd;
                   8715:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8716:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8717:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8718:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8719:                }
                   8720:                REG8(AH) = 0x00; // successful
                   8721:                break;
                   8722:        default:
                   8723:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8724: //             REG8(AH) = 0x86;
                   8725:                REG8(AH) = 0x01; // invalid function
                   8726:                m_CF = 1;
                   8727:                break;
                   8728:        }
                   8729: }
                   8730: 
1.1.1.3   root     8731: #if defined(HAS_I386)
1.1       root     8732: inline void pcbios_int_15h_c9h()
                   8733: {
                   8734:        REG8(AH) = 0x00;
                   8735:        REG8(CH) = cpu_type;
                   8736:        REG8(CL) = cpu_step;
                   8737: }
1.1.1.3   root     8738: #endif
1.1       root     8739: 
                   8740: inline void pcbios_int_15h_cah()
                   8741: {
                   8742:        switch(REG8(AL)) {
1.1.1.22  root     8743:        case 0x00:
1.1       root     8744:                if(REG8(BL) > 0x3f) {
                   8745:                        REG8(AH) = 0x03;
1.1.1.3   root     8746:                        m_CF = 1;
1.1       root     8747:                } else if(REG8(BL) < 0x0e) {
                   8748:                        REG8(AH) = 0x04;
1.1.1.3   root     8749:                        m_CF = 1;
1.1       root     8750:                } else {
1.1.1.8   root     8751:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8752:                }
                   8753:                break;
1.1.1.22  root     8754:        case 0x01:
1.1       root     8755:                if(REG8(BL) > 0x3f) {
                   8756:                        REG8(AH) = 0x03;
1.1.1.3   root     8757:                        m_CF = 1;
1.1       root     8758:                } else if(REG8(BL) < 0x0e) {
                   8759:                        REG8(AH) = 0x04;
1.1.1.3   root     8760:                        m_CF = 1;
1.1       root     8761:                } else {
1.1.1.8   root     8762:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8763:                }
                   8764:                break;
                   8765:        default:
1.1.1.22  root     8766:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     8767:                REG8(AH) = 0x86;
1.1.1.3   root     8768:                m_CF = 1;
1.1       root     8769:                break;
                   8770:        }
                   8771: }
                   8772: 
1.1.1.22  root     8773: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8774: {
1.1.1.22  root     8775:        switch(REG8(AL)) {
                   8776: #if defined(HAS_I386)
                   8777:        case 0x01:
                   8778:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8779:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8780:                break;
1.1.1.17  root     8781: #endif
1.1.1.22  root     8782:        default:
                   8783:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8784:                REG8(AH) = 0x86;
                   8785:                m_CF = 1;
                   8786:                break;
                   8787:        }
                   8788: }
1.1.1.17  root     8789: 
1.1.1.55  root     8790: bool pcbios_is_key_buffer_empty()
                   8791: {
                   8792:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8793: }
                   8794: 
1.1.1.51  root     8795: void pcbios_clear_key_buffer()
                   8796: {
                   8797:        key_buf_char->clear();
                   8798:        key_buf_scan->clear();
                   8799:        
                   8800:        // update key buffer
                   8801:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8802: }
                   8803: 
                   8804: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8805: {
                   8806:        // update key buffer
                   8807:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8808:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8809:        UINT16 next = tail + 2;
                   8810:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8811:                next = *(UINT16 *)(mem + 0x480);
                   8812:        }
                   8813:        if(next != head) {
                   8814:                *(UINT16 *)(mem + 0x41c) = next;
                   8815:                mem[0x400 + (tail++)] = key_char;
                   8816:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8817:        } else {
                   8818:                // store to extra key buffer
                   8819:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8820:                        key_buf_char->write(key_char);
                   8821:                        key_buf_scan->write(key_scan);
                   8822:                }
1.1.1.51  root     8823:        }
                   8824: }
                   8825: 
                   8826: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8827: {
                   8828:        // update key buffer
                   8829:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8830:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8831:        UINT16 next = head + 2;
                   8832:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8833:                next = *(UINT16 *)(mem + 0x480);
                   8834:        }
                   8835:        if(head != tail) {
                   8836:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8837:                *key_char = mem[0x400 + (head++)];
                   8838:                *key_scan = mem[0x400 + (head++)];
                   8839:                
                   8840:                // restore from extra key buffer
                   8841:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8842:                        if(!key_buf_char->empty()) {
                   8843:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8844:                        }
                   8845:                }
                   8846:                return(true);
                   8847:        } else {
                   8848:                *key_char = 0x00;
                   8849:                *key_scan = 0x00;
                   8850:                return(false);
1.1.1.51  root     8851:        }
                   8852: }
                   8853: 
1.1.1.60  root     8854: bool pcbios_check_key_buffer(int *key_char, int *key_scan)
                   8855: {
                   8856:        // do not remove from key buffer
                   8857:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8858:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8859:        if(head != tail) {
                   8860:                *key_char = mem[0x400 + (head++)];
                   8861:                *key_scan = mem[0x400 + (head++)];
                   8862:                return(true);
                   8863:        } else {
                   8864:                *key_char = 0x00;
                   8865:                *key_scan = 0x00;
                   8866:                return(false);
                   8867:        }
                   8868: }
                   8869: 
1.1.1.33  root     8870: void pcbios_update_key_code(bool wait)
1.1       root     8871: {
1.1.1.32  root     8872:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8873: #ifdef USE_SERVICE_THREAD
                   8874:                EnterCriticalSection(&key_buf_crit_sect);
                   8875: #endif
1.1.1.55  root     8876:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8877: #ifdef USE_SERVICE_THREAD
                   8878:                LeaveCriticalSection(&key_buf_crit_sect);
                   8879: #endif
                   8880:                if(empty) {
1.1.1.32  root     8881:                        if(!update_key_buffer()) {
1.1.1.33  root     8882:                                if(wait) {
1.1.1.32  root     8883:                                        Sleep(10);
                   8884:                                } else {
                   8885:                                        maybe_idle();
                   8886:                                }
1.1.1.14  root     8887:                        }
                   8888:                }
1.1.1.34  root     8889:        }
                   8890:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8891: #ifdef USE_SERVICE_THREAD
                   8892:                EnterCriticalSection(&key_buf_crit_sect);
                   8893: #endif
1.1.1.51  root     8894:                int key_char, key_scan;
                   8895:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8896:                        key_code  = key_char << 0;
                   8897:                        key_code |= key_scan << 8;
1.1.1.35  root     8898:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8899:                }
                   8900:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8901:                        key_code |= key_char << 16;
                   8902:                        key_code |= key_scan << 24;
1.1.1.33  root     8903:                        key_recv |= 0xffff0000;
1.1.1.32  root     8904:                }
1.1.1.35  root     8905: #ifdef USE_SERVICE_THREAD
                   8906:                LeaveCriticalSection(&key_buf_crit_sect);
                   8907: #endif
1.1       root     8908:        }
                   8909: }
                   8910: 
1.1.1.35  root     8911: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8912: {
1.1.1.54  root     8913:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     8914:                pcbios_update_key_code(true);
1.1       root     8915:        }
1.1.1.33  root     8916:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8917:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8918:                        if(REG8(AH) == 0x10) {
                   8919:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8920:                        } else {
                   8921:                                key_code = ((key_code >> 16) & 0xff00);
                   8922:                        }
                   8923:                        key_recv >>= 16;
1.1       root     8924:                }
                   8925:        }
                   8926:        REG16(AX) = key_code & 0xffff;
                   8927:        key_code >>= 16;
1.1.1.33  root     8928:        key_recv >>= 16;
1.1.1.35  root     8929:        
                   8930: #ifdef USE_SERVICE_THREAD
                   8931:        service_exit = true;
                   8932: #endif
                   8933:        return(0);
                   8934: }
                   8935: 
                   8936: inline void pcbios_int_16h_00h()
                   8937: {
                   8938: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8939:        if(!in_service && !in_service_29h) {
                   8940:                start_service_loop(pcbios_int_16h_00h_thread);
                   8941:        } else {
                   8942: #endif
                   8943:                pcbios_int_16h_00h_thread(NULL);
                   8944:                REQUEST_HARDWRE_UPDATE();
                   8945: #ifdef USE_SERVICE_THREAD
                   8946:        }
1.1.1.35  root     8947: #endif
1.1       root     8948: }
                   8949: 
                   8950: inline void pcbios_int_16h_01h()
                   8951: {
1.1.1.33  root     8952:        if(key_recv == 0) {
                   8953:                pcbios_update_key_code(false);
1.1.1.5   root     8954:        }
1.1.1.33  root     8955:        if(key_recv != 0) {
                   8956:                UINT32 key_code_tmp = key_code;
                   8957:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8958:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8959:                                if(REG8(AH) == 0x11) {
                   8960:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8961:                                } else {
                   8962:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8963:                                }
                   8964:                        }
1.1       root     8965:                }
1.1.1.5   root     8966:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8967: #if defined(HAS_I386)
1.1.1.33  root     8968:                m_ZF = 0;
                   8969: #else
                   8970:                m_ZeroVal = 1;
                   8971: #endif
                   8972:        } else {
                   8973: #if defined(HAS_I386)
                   8974:                m_ZF = 1;
1.1.1.3   root     8975: #else
1.1.1.33  root     8976:                m_ZeroVal = 0;
1.1.1.3   root     8977: #endif
1.1.1.33  root     8978:        }
1.1       root     8979: }
                   8980: 
                   8981: inline void pcbios_int_16h_02h()
                   8982: {
                   8983:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8984:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8985:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8986:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8987:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8988:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8989:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8990:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8991: }
                   8992: 
                   8993: inline void pcbios_int_16h_03h()
                   8994: {
                   8995:        static UINT16 status = 0;
                   8996:        
                   8997:        switch(REG8(AL)) {
                   8998:        case 0x05:
                   8999:                status = REG16(BX);
                   9000:                break;
                   9001:        case 0x06:
                   9002:                REG16(BX) = status;
                   9003:                break;
                   9004:        default:
1.1.1.3   root     9005:                m_CF = 1;
1.1       root     9006:                break;
                   9007:        }
                   9008: }
                   9009: 
                   9010: inline void pcbios_int_16h_05h()
                   9011: {
1.1.1.32  root     9012:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     9013: #ifdef USE_SERVICE_THREAD
                   9014:                EnterCriticalSection(&key_buf_crit_sect);
                   9015: #endif
1.1.1.51  root     9016:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     9017: #ifdef USE_SERVICE_THREAD
                   9018:                LeaveCriticalSection(&key_buf_crit_sect);
                   9019: #endif
1.1.1.32  root     9020:        }
1.1       root     9021:        REG8(AL) = 0x00;
                   9022: }
                   9023: 
1.1.1.60  root     9024: inline void pcbios_int_16h_09h()
                   9025: {
                   9026:        REG8(AL)  = 0x00;
                   9027: //     REG8(AL) |= 0x01;       // INT 16/AX=0300h supported    (set default delay and rate (PCjr and some PS/2))
                   9028: //     REG8(AL) |= 0x02;       // INT 16/AX=0304h supported    (turn off typematic repeat (PCjr and some PS/2))
                   9029:        REG8(AL) |= 0x04;       // INT 16/AX=0305h supported    (set repeat rate and delay (AT,PS))
                   9030:        REG8(AL) |= 0x08;       // INT 16/AX=0306h supported    (get current typematic rate and delay (newer PS/2s))
                   9031:        REG8(AL) |= 0x10;       // INT 16/AH=0Ah supported      (get keyboard id)
                   9032:        REG8(AL) |= 0x20;       // INT 16/AH=10h-12h supported  (enhanced keyboard support)
                   9033: //     REG8(AL) |= 0x40;       // INT 16/AH=20h-22h supported  (122-key keyboard support)
                   9034: //     REG8(AL) |= 0x80;       // reserved
                   9035: }
                   9036: 
                   9037: inline void pcbios_int_16h_0ah()
                   9038: {
                   9039: //     REG16(BX) = 0x41ab;     // MF2 Keyboard (usually in translate mode)
                   9040:        REG16(BX) = 0x83ab;     // MF2 Keyboard (pass-through mode)
                   9041: }
                   9042: 
                   9043: inline void pcbios_int_16h_11h()
                   9044: {
                   9045:        int key_char, key_scan;
                   9046:        
                   9047: #ifdef USE_SERVICE_THREAD
                   9048:        EnterCriticalSection(&key_buf_crit_sect);
                   9049: #endif
                   9050:        if(pcbios_check_key_buffer(&key_char, &key_scan)) {
                   9051:                REG8(AL) = key_char;
                   9052:                REG8(AH) = key_scan;
                   9053: #if defined(HAS_I386)
                   9054:                m_ZF = 0;
                   9055: #else
                   9056:                m_ZeroVal = 1;
                   9057: #endif
                   9058:        } else {
                   9059: #if defined(HAS_I386)
                   9060:                m_ZF = 1;
                   9061: #else
                   9062:                m_ZeroVal = 0;
                   9063: #endif
                   9064:        }
                   9065: #ifdef USE_SERVICE_THREAD
                   9066:        LeaveCriticalSection(&key_buf_crit_sect);
                   9067: #endif
                   9068: }
                   9069: 
1.1       root     9070: inline void pcbios_int_16h_12h()
                   9071: {
                   9072:        pcbios_int_16h_02h();
                   9073:        
                   9074:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   9075:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   9076:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   9077:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   9078:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   9079:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   9080:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   9081:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   9082: }
                   9083: 
                   9084: inline void pcbios_int_16h_13h()
                   9085: {
                   9086:        static UINT16 status = 0;
                   9087:        
                   9088:        switch(REG8(AL)) {
                   9089:        case 0x00:
                   9090:                status = REG16(DX);
                   9091:                break;
                   9092:        case 0x01:
                   9093:                REG16(DX) = status;
                   9094:                break;
                   9095:        default:
1.1.1.22  root     9096:                unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     9097:                m_CF = 1;
1.1       root     9098:                break;
                   9099:        }
                   9100: }
                   9101: 
                   9102: inline void pcbios_int_16h_14h()
                   9103: {
                   9104:        static UINT8 status = 0;
                   9105:        
                   9106:        switch(REG8(AL)) {
                   9107:        case 0x00:
                   9108:        case 0x01:
                   9109:                status = REG8(AL);
                   9110:                break;
                   9111:        case 0x02:
                   9112:                REG8(AL) = status;
                   9113:                break;
                   9114:        default:
1.1.1.22  root     9115:                unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     9116:                m_CF = 1;
1.1       root     9117:                break;
                   9118:        }
                   9119: }
                   9120: 
1.1.1.24  root     9121: inline void pcbios_int_16h_55h()
                   9122: {
                   9123:        switch(REG8(AL)) {
                   9124:        case 0x00:
                   9125:                // keyboard tsr is not present
                   9126:                break;
                   9127:        case 0xfe:
                   9128:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   9129:                break;
                   9130:        case 0xff:
                   9131:                break;
                   9132:        default:
                   9133:                unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   9134:                m_CF = 1;
                   9135:                break;
                   9136:        }
                   9137: }
                   9138: 
1.1.1.30  root     9139: inline void pcbios_int_16h_6fh()
                   9140: {
                   9141:        switch(REG8(AL)) {
                   9142:        case 0x00:
                   9143:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   9144:                break;
                   9145:        default:
                   9146:                unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   9147:                m_CF = 1;
                   9148:                break;
                   9149:        }
                   9150: }
                   9151: 
1.1.1.37  root     9152: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   9153: {
                   9154:        UINT8 hi = jis >> 8;
                   9155:        UINT8 lo = jis & 0xff;
                   9156:        
                   9157:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   9158:        hi = (hi - 0x21) / 2 + 0x81;
                   9159:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   9160:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   9161:        
                   9162:        return((hi << 8) + lo);
                   9163: }
                   9164: 
                   9165: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   9166: {
                   9167:        UINT8 hi = sjis >> 8;
                   9168:        UINT8 lo = sjis & 0xff;
                   9169:        
                   9170:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   9171:                return(0x2121);
                   9172:        }
                   9173:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   9174:                return(0x2121);
                   9175:        }
                   9176:        if(hi >= 0xf0 && hi <= 0xf3) {
                   9177:                // gaiji
                   9178:                if(lo >= 0x40 && lo <= 0x7e) {
                   9179:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   9180:                }
                   9181:                if(lo >= 0x80 && lo <= 0x9e) {
                   9182:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   9183:                }
                   9184:                if(lo >= 0x9f && lo <= 0xfc) {
                   9185:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   9186:                }
                   9187:        }
                   9188:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   9189:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   9190:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   9191:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   9192:        
                   9193:        return((hi << 8) + lo);
                   9194: }
                   9195: 
1.1.1.38  root     9196: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   9197: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   9198: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     9199: 
                   9200: void pcbios_printer_out(int c, UINT8 data)
                   9201: {
                   9202:        if(pio[c].conv_mode) {
                   9203:                if(pio[c].sjis_hi != 0) {
                   9204:                        if(!pio[c].jis_mode) {
                   9205:                                printer_out(c, 0x1c);
                   9206:                                printer_out(c, 0x26);
                   9207:                                pio[c].jis_mode = true;
                   9208:                        }
                   9209:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   9210:                        printer_out(c, jis >> 8);
                   9211:                        printer_out(c, jis & 0xff);
                   9212:                        pio[c].sjis_hi = 0;
                   9213:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   9214:                        printer_out(c, data);
                   9215:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9216:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9217:                        }
                   9218:                        pio[c].esc_len++;
                   9219:                        
                   9220:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9221:                        case 0x33: // 1Bh 33h XX
                   9222:                        case 0x4a: // 1Bh 4Ah XX
                   9223:                        case 0x4e: // 1Bh 4Eh XX
                   9224:                        case 0x51: // 1Bh 51h XX
                   9225:                        case 0x55: // 1Bh 55h XX
                   9226:                        case 0x6c: // 1Bh 6Ch XX
                   9227:                        case 0x71: // 1Bh 71h XX
                   9228:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     9229:                                if(pio[c].esc_len == 3) {
                   9230:                                        pio[c].esc_buf[0] = 0x00;
                   9231:                                }
                   9232:                                break;
1.1.1.38  root     9233:                        case 0x24: // 1Bh 24h XX XX
                   9234:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     9235:                                if(pio[c].esc_len == 4) {
                   9236:                                        pio[c].esc_buf[0] = 0x00;
                   9237:                                }
                   9238:                                break;
1.1.1.38  root     9239:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     9240:                                if(pio[c].esc_len >= 3) {
                   9241:                                        switch(pio[c].esc_buf[2]) {
                   9242:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   9243:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   9244:                                                        pio[c].esc_buf[0] = 0x00;
                   9245:                                                }
                   9246:                                                break;
                   9247:                                        case 32: case 33: case 38: case 39: case 40:
                   9248:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   9249:                                                        pio[c].esc_buf[0] = 0x00;
                   9250:                                                }
                   9251:                                                break;
1.1.1.38  root     9252:                                        case 71: case 72: case 73:
                   9253:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   9254:                                                        pio[c].esc_buf[0] = 0x00;
                   9255:                                                }
                   9256:                                                break;
1.1.1.37  root     9257:                                        default:
                   9258:                                                pio[c].esc_buf[0] = 0x00;
                   9259:                                                break;
                   9260:                                        }
                   9261:                                }
                   9262:                                break;
1.1.1.38  root     9263:                        case 0x40: // 1Bh 40h
1.1.1.37  root     9264:                                if(pio[c].jis_mode) {
                   9265:                                        printer_out(c, 0x1c);
                   9266:                                        printer_out(c, 0x2e);
                   9267:                                        pio[c].jis_mode = false;
                   9268:                                }
                   9269:                                pio[c].esc_buf[0] = 0x00;
                   9270:                                break;
1.1.1.38  root     9271:                        case 0x42: // 1Bh 42h data 00h
                   9272:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9273:                                if(pio[c].esc_len >= 3 && data == 0) {
                   9274:                                        pio[c].esc_buf[0] = 0x00;
                   9275:                                }
                   9276:                                break;
1.1.1.38  root     9277:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     9278:                                if(pio[c].esc_len >= 3 && data != 0) {
                   9279:                                        pio[c].esc_buf[0] = 0x00;
                   9280:                                }
                   9281:                                break;
1.1.1.38  root     9282:                        default: // 1Bh XX
1.1.1.37  root     9283:                                pio[c].esc_buf[0] = 0x00;
                   9284:                                break;
                   9285:                        }
                   9286:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   9287:                        printer_out(c, data);
                   9288:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9289:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9290:                        }
                   9291:                        pio[c].esc_len++;
                   9292:                        
                   9293:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9294:                        case 0x21: // 1Ch 21h XX
                   9295:                        case 0x2d: // 1Ch 2Dh XX
                   9296:                        case 0x57: // 1Ch 57h XX
                   9297:                        case 0x6b: // 1Ch 6Bh XX
                   9298:                        case 0x72: // 1Ch 72h XX
                   9299:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     9300:                                if(pio[c].esc_len == 3) {
                   9301:                                        pio[c].esc_buf[0] = 0x00;
                   9302:                                }
                   9303:                                break;
1.1.1.38  root     9304:                        case 0x26: // 1Ch 26h
1.1.1.37  root     9305:                                pio[c].jis_mode = true;
                   9306:                                pio[c].esc_buf[0] = 0x00;
                   9307:                                break;
1.1.1.38  root     9308:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     9309:                                pio[c].jis_mode = false;
                   9310:                                pio[c].esc_buf[0] = 0x00;
                   9311:                                break;
1.1.1.38  root     9312:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     9313:                                if(pio[c].esc_len == 76) {
                   9314:                                        pio[c].esc_buf[0] = 0x00;
                   9315:                                }
                   9316:                                break;
1.1.1.38  root     9317:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9318:                                if(pio[c].esc_len == 6) {
                   9319:                                        pio[c].esc_buf[0] = 0x00;
                   9320:                                }
                   9321:                                break;
1.1.1.38  root     9322:                        case 0x53: // 1Ch 53h XX XX
                   9323:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     9324:                                if(pio[c].esc_len == 4) {
                   9325:                                        pio[c].esc_buf[0] = 0x00;
                   9326:                                }
                   9327:                                break;
1.1.1.38  root     9328:                        default: // 1Ch XX
1.1.1.37  root     9329:                                pio[c].esc_buf[0] = 0x00;
                   9330:                                break;
                   9331:                        }
                   9332:                } else if(data == 0x1b || data == 0x1c) {
                   9333:                        printer_out(c, data);
                   9334:                        pio[c].esc_buf[0] = data;
                   9335:                        pio[c].esc_len = 1;
                   9336:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9337:                        pio[c].sjis_hi = data;
                   9338:                } else {
                   9339:                        if(pio[c].jis_mode) {
                   9340:                                printer_out(c, 0x1c);
                   9341:                                printer_out(c, 0x2e);
                   9342:                                pio[c].jis_mode = false;
                   9343:                        }
                   9344:                        printer_out(c, data);
                   9345:                }
                   9346:        } else {
                   9347:                if(pio[c].jis_mode) {
                   9348:                        printer_out(c, 0x1c);
                   9349:                        printer_out(c, 0x2e);
                   9350:                        pio[c].jis_mode = false;
                   9351:                }
                   9352:                printer_out(c, data);
                   9353:        }
                   9354: }
                   9355: 
                   9356: inline void pcbios_int_17h_00h()
                   9357: {
                   9358:        if(REG16(DX) < 3) {
                   9359:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9360:                REG8(AH) = 0xd0;
                   9361:        }
                   9362: }
                   9363: 
                   9364: inline void pcbios_int_17h_01h()
                   9365: {
                   9366:        if(REG16(DX) < 3) {
                   9367:                REG8(AH) = 0xd0;
                   9368:        }
                   9369: }
                   9370: 
                   9371: inline void pcbios_int_17h_02h()
                   9372: {
                   9373:        if(REG16(DX) < 3) {
                   9374:                REG8(AH) = 0xd0;
                   9375:        }
                   9376: }
                   9377: 
                   9378: inline void pcbios_int_17h_03h()
                   9379: {
                   9380:        switch(REG8(AL)) {
                   9381:        case 0x00:
                   9382:                if(REG16(DX) < 3) {
                   9383:                        if(pio[REG16(DX)].jis_mode) {
                   9384:                                printer_out(REG16(DX), 0x1c);
                   9385:                                printer_out(REG16(DX), 0x2e);
                   9386:                                pio[REG16(DX)].jis_mode = false;
                   9387:                        }
                   9388:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9389:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9390:                        }
                   9391:                        REG16(CX) = 0x0000;
                   9392:                        REG8(AH) = 0xd0;
                   9393:                }
                   9394:                break;
                   9395:        default:
                   9396:                unimplemented_17h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x17, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   9397:                break;
                   9398:        }
                   9399: }
                   9400: 
                   9401: inline void pcbios_int_17h_50h()
                   9402: {
                   9403:        switch(REG8(AL)) {
                   9404:        case 0x00:
                   9405:                if(REG16(DX) < 3) {
                   9406:                        if(REG16(BX) = 0x0001) {
                   9407:                                pio[REG16(DX)].conv_mode = false;
                   9408:                                REG8(AL) = 0x00;
                   9409:                        } else if(REG16(BX) = 0x0051) {
                   9410:                                pio[REG16(DX)].conv_mode = true;
                   9411:                                REG8(AL) = 0x00;
                   9412:                        } else {
                   9413:                                REG8(AL) = 0x01;
                   9414:                        }
                   9415:                } else {
                   9416:                        REG8(AL) = 0x02;
                   9417:                }
                   9418:                break;
                   9419:        case 0x01:
                   9420:                if(REG16(DX) < 3) {
                   9421:                        if(pio[REG16(DX)].conv_mode) {
                   9422:                                REG16(BX) = 0x0051;
                   9423:                        } else {
                   9424:                                REG16(BX) = 0x0001;
                   9425:                        }
                   9426:                        REG8(AL) = 0x00;
                   9427:                } else {
                   9428:                        REG8(AL) = 0x02;
                   9429:                }
                   9430:                break;
                   9431:        default:
                   9432:                unimplemented_17h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x17, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   9433:                break;
                   9434:        }
                   9435: }
                   9436: 
                   9437: inline void pcbios_int_17h_51h()
                   9438: {
                   9439:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9440:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9441:        } else {
                   9442:                REG16(DX) = 0x0000;
                   9443:        }
                   9444: }
                   9445: 
                   9446: inline void pcbios_int_17h_52h()
                   9447: {
                   9448:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9449:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9450:        } else {
                   9451:                REG16(DX) = 0x0000;
                   9452:        }
                   9453: }
                   9454: 
                   9455: inline void pcbios_int_17h_84h()
                   9456: {
                   9457:        if(REG16(DX) < 3) {
                   9458:                if(pio[REG16(DX)].jis_mode) {
                   9459:                        printer_out(REG16(DX), 0x1c);
                   9460:                        printer_out(REG16(DX), 0x2e);
                   9461:                        pio[REG16(DX)].jis_mode = false;
                   9462:                }
                   9463:                printer_out(REG16(DX), REG8(AL));
                   9464:                REG8(AH) = 0xd0;
                   9465:        }
                   9466: }
                   9467: 
                   9468: inline void pcbios_int_17h_85h()
                   9469: {
                   9470:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9471: }
                   9472: 
1.1       root     9473: inline void pcbios_int_1ah_00h()
                   9474: {
1.1.1.19  root     9475:        pcbios_update_daily_timer_counter(timeGetTime());
                   9476:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9477:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9478:        REG8(AL) = mem[0x470];
                   9479:        mem[0x470] = 0;
1.1       root     9480: }
                   9481: 
                   9482: inline int to_bcd(int t)
                   9483: {
                   9484:        int u = (t % 100) / 10;
                   9485:        return (u << 4) | (t % 10);
                   9486: }
                   9487: 
                   9488: inline void pcbios_int_1ah_02h()
                   9489: {
                   9490:        SYSTEMTIME time;
                   9491:        
                   9492:        GetLocalTime(&time);
                   9493:        REG8(CH) = to_bcd(time.wHour);
                   9494:        REG8(CL) = to_bcd(time.wMinute);
                   9495:        REG8(DH) = to_bcd(time.wSecond);
                   9496:        REG8(DL) = 0x00;
                   9497: }
                   9498: 
                   9499: inline void pcbios_int_1ah_04h()
                   9500: {
                   9501:        SYSTEMTIME time;
                   9502:        
                   9503:        GetLocalTime(&time);
                   9504:        REG8(CH) = to_bcd(time.wYear / 100);
                   9505:        REG8(CL) = to_bcd(time.wYear);
                   9506:        REG8(DH) = to_bcd(time.wMonth);
                   9507:        REG8(DL) = to_bcd(time.wDay);
                   9508: }
                   9509: 
                   9510: inline void pcbios_int_1ah_0ah()
                   9511: {
                   9512:        SYSTEMTIME time;
                   9513:        FILETIME file_time;
                   9514:        WORD dos_date, dos_time;
                   9515:        
                   9516:        GetLocalTime(&time);
                   9517:        SystemTimeToFileTime(&time, &file_time);
                   9518:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9519:        REG16(CX) = dos_date;
                   9520: }
                   9521: 
                   9522: // msdos system call
                   9523: 
1.1.1.43  root     9524: inline void msdos_int_21h_56h(int lfn);
                   9525: 
1.1       root     9526: inline void msdos_int_21h_00h()
                   9527: {
1.1.1.3   root     9528:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9529: }
                   9530: 
1.1.1.35  root     9531: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9532: {
                   9533:        REG8(AL) = msdos_getche();
1.1.1.33  root     9534:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9535:        
1.1.1.35  root     9536: #ifdef USE_SERVICE_THREAD
                   9537:        service_exit = true;
                   9538: #endif
                   9539:        return(0);
                   9540: }
                   9541: 
                   9542: inline void msdos_int_21h_01h()
                   9543: {
                   9544: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9545:        if(!in_service && !in_service_29h &&
1.1.1.58  root     9546:           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9547:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9548:                // msdos_putch() will be used in this service
                   9549:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9550:                start_service_loop(msdos_int_21h_01h_thread);
                   9551:        } else {
                   9552: #endif
                   9553:                msdos_int_21h_01h_thread(NULL);
                   9554:                REQUEST_HARDWRE_UPDATE();
                   9555: #ifdef USE_SERVICE_THREAD
                   9556:        }
1.1.1.35  root     9557: #endif
1.1       root     9558: }
                   9559: 
                   9560: inline void msdos_int_21h_02h()
                   9561: {
1.1.1.33  root     9562:        UINT8 data = REG8(DL);
                   9563:        msdos_putch(data);
                   9564:        REG8(AL) = data;
                   9565:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9566: }
                   9567: 
                   9568: inline void msdos_int_21h_03h()
                   9569: {
                   9570:        REG8(AL) = msdos_aux_in();
                   9571: }
                   9572: 
                   9573: inline void msdos_int_21h_04h()
                   9574: {
                   9575:        msdos_aux_out(REG8(DL));
                   9576: }
                   9577: 
                   9578: inline void msdos_int_21h_05h()
                   9579: {
                   9580:        msdos_prn_out(REG8(DL));
                   9581: }
                   9582: 
                   9583: inline void msdos_int_21h_06h()
                   9584: {
                   9585:        if(REG8(DL) == 0xff) {
                   9586:                if(msdos_kbhit()) {
                   9587:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9588: #if defined(HAS_I386)
                   9589:                        m_ZF = 0;
                   9590: #else
                   9591:                        m_ZeroVal = 1;
                   9592: #endif
1.1       root     9593:                } else {
                   9594:                        REG8(AL) = 0;
1.1.1.3   root     9595: #if defined(HAS_I386)
                   9596:                        m_ZF = 1;
                   9597: #else
                   9598:                        m_ZeroVal = 0;
                   9599: #endif
1.1.1.14  root     9600:                        maybe_idle();
1.1       root     9601:                }
                   9602:        } else {
1.1.1.33  root     9603:                UINT8 data = REG8(DL);
                   9604:                msdos_putch(data);
                   9605:                REG8(AL) = data;
1.1       root     9606:        }
                   9607: }
                   9608: 
1.1.1.35  root     9609: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9610: {
                   9611:        REG8(AL) = msdos_getch();
1.1.1.26  root     9612:        
1.1.1.35  root     9613: #ifdef USE_SERVICE_THREAD
                   9614:        service_exit = true;
                   9615: #endif
                   9616:        return(0);
1.1       root     9617: }
                   9618: 
1.1.1.35  root     9619: inline void msdos_int_21h_07h()
                   9620: {
                   9621: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9622:        if(!in_service && !in_service_29h) {
                   9623:                start_service_loop(msdos_int_21h_07h_thread);
                   9624:        } else {
                   9625: #endif
                   9626:                msdos_int_21h_07h_thread(NULL);
                   9627:                REQUEST_HARDWRE_UPDATE();
                   9628: #ifdef USE_SERVICE_THREAD
                   9629:        }
1.1.1.35  root     9630: #endif
                   9631: }
                   9632: 
                   9633: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9634: {
                   9635:        REG8(AL) = msdos_getch();
1.1.1.33  root     9636:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9637:        
1.1.1.35  root     9638: #ifdef USE_SERVICE_THREAD
                   9639:        service_exit = true;
                   9640: #endif
                   9641:        return(0);
                   9642: }
                   9643: 
                   9644: inline void msdos_int_21h_08h()
                   9645: {
                   9646: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9647:        if(!in_service && !in_service_29h) {
                   9648:                start_service_loop(msdos_int_21h_08h_thread);
                   9649:        } else {
                   9650: #endif
                   9651:                msdos_int_21h_08h_thread(NULL);
                   9652:                REQUEST_HARDWRE_UPDATE();
                   9653: #ifdef USE_SERVICE_THREAD
                   9654:        }
1.1.1.35  root     9655: #endif
1.1       root     9656: }
                   9657: 
                   9658: inline void msdos_int_21h_09h()
                   9659: {
1.1.1.21  root     9660:        msdos_stdio_reopen();
                   9661:        
1.1.1.20  root     9662:        process_t *process = msdos_process_info_get(current_psp);
                   9663:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9664:        
1.1.1.14  root     9665:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9666:        int len = 0;
1.1       root     9667:        
1.1.1.14  root     9668:        while(str[len] != '$' && len < 0x10000) {
                   9669:                len++;
                   9670:        }
1.1.1.20  root     9671:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9672:                // stdout is redirected to file
1.1.1.20  root     9673:                msdos_write(fd, str, len);
1.1       root     9674:        } else {
                   9675:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9676:                        msdos_putch(str[i]);
1.1       root     9677:                }
                   9678:        }
1.1.1.33  root     9679:        REG8(AL) = '$';
                   9680:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9681: }
                   9682: 
1.1.1.35  root     9683: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9684: {
1.1.1.3   root     9685:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9686:        int max = mem[ofs] - 1;
                   9687:        UINT8 *buf = mem + ofs + 2;
                   9688:        int chr, p = 0;
                   9689:        
                   9690:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9691:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9692:                        p = 0;
1.1.1.33  root     9693:                        msdos_putch(0x03);
                   9694:                        msdos_putch(0x0d);
                   9695:                        msdos_putch(0x0a);
1.1.1.26  root     9696:                        break;
1.1.1.33  root     9697:                } else if(ctrl_break_pressed) {
                   9698:                        // skip this byte
1.1.1.26  root     9699:                } else if(chr == 0x00) {
1.1       root     9700:                        // skip 2nd byte
                   9701:                        msdos_getch();
                   9702:                } else if(chr == 0x08) {
                   9703:                        // back space
                   9704:                        if(p > 0) {
                   9705:                                p--;
1.1.1.20  root     9706:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9707:                                        msdos_putch(0x08);
                   9708:                                        msdos_putch(0x08);
                   9709:                                        msdos_putch(0x20);
                   9710:                                        msdos_putch(0x20);
                   9711:                                        msdos_putch(0x08);
                   9712:                                        msdos_putch(0x08);
1.1.1.36  root     9713:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9714:                                        p--;
                   9715:                                        msdos_putch(0x08);
                   9716:                                        msdos_putch(0x08);
                   9717:                                        msdos_putch(0x20);
                   9718:                                        msdos_putch(0x20);
                   9719:                                        msdos_putch(0x08);
                   9720:                                        msdos_putch(0x08);
1.1.1.34  root     9721:                                } else {
                   9722:                                        msdos_putch(0x08);
                   9723:                                        msdos_putch(0x20);
                   9724:                                        msdos_putch(0x08);
                   9725:                                }
                   9726:                        }
                   9727:                } else if(chr == 0x1b) {
                   9728:                        // escape
                   9729:                        while(p > 0) {
                   9730:                                p--;
                   9731:                                if(msdos_ctrl_code_check(buf[p])) {
                   9732:                                        msdos_putch(0x08);
                   9733:                                        msdos_putch(0x08);
                   9734:                                        msdos_putch(0x20);
                   9735:                                        msdos_putch(0x20);
                   9736:                                        msdos_putch(0x08);
                   9737:                                        msdos_putch(0x08);
1.1.1.20  root     9738:                                } else {
1.1.1.34  root     9739:                                        msdos_putch(0x08);
                   9740:                                        msdos_putch(0x20);
                   9741:                                        msdos_putch(0x08);
1.1.1.20  root     9742:                                }
1.1       root     9743:                        }
                   9744:                } else if(p < max) {
                   9745:                        buf[p++] = chr;
                   9746:                        msdos_putch(chr);
                   9747:                }
                   9748:        }
                   9749:        buf[p] = 0x0d;
                   9750:        mem[ofs + 1] = p;
1.1.1.33  root     9751:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9752:        
1.1.1.35  root     9753: #ifdef USE_SERVICE_THREAD
                   9754:        service_exit = true;
                   9755: #endif
                   9756:        return(0);
                   9757: }
                   9758: 
                   9759: inline void msdos_int_21h_0ah()
                   9760: {
                   9761:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9762: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9763:                if(!in_service && !in_service_29h &&
1.1.1.58  root     9764:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9765:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9766:                        // msdos_putch() will be used in this service
                   9767:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9768:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9769:                } else {
                   9770: #endif
                   9771:                        msdos_int_21h_0ah_thread(NULL);
                   9772:                        REQUEST_HARDWRE_UPDATE();
                   9773: #ifdef USE_SERVICE_THREAD
                   9774:                }
1.1.1.35  root     9775: #endif
                   9776:        }
1.1       root     9777: }
                   9778: 
                   9779: inline void msdos_int_21h_0bh()
                   9780: {
                   9781:        if(msdos_kbhit()) {
                   9782:                REG8(AL) = 0xff;
                   9783:        } else {
                   9784:                REG8(AL) = 0x00;
1.1.1.14  root     9785:                maybe_idle();
1.1       root     9786:        }
1.1.1.33  root     9787:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9788: }
                   9789: 
                   9790: inline void msdos_int_21h_0ch()
                   9791: {
                   9792:        // clear key buffer
1.1.1.21  root     9793:        msdos_stdio_reopen();
                   9794:        
1.1.1.20  root     9795:        process_t *process = msdos_process_info_get(current_psp);
                   9796:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9797:        
                   9798:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9799:                // stdin is redirected to file
                   9800:        } else {
                   9801:                while(msdos_kbhit()) {
                   9802:                        msdos_getch();
                   9803:                }
                   9804:        }
                   9805:        
                   9806:        switch(REG8(AL)) {
                   9807:        case 0x01:
                   9808:                msdos_int_21h_01h();
                   9809:                break;
                   9810:        case 0x06:
                   9811:                msdos_int_21h_06h();
                   9812:                break;
                   9813:        case 0x07:
                   9814:                msdos_int_21h_07h();
                   9815:                break;
                   9816:        case 0x08:
                   9817:                msdos_int_21h_08h();
                   9818:                break;
                   9819:        case 0x0a:
                   9820:                msdos_int_21h_0ah();
                   9821:                break;
                   9822:        default:
1.1.1.48  root     9823:                // the buffer is flushed but no input is attempted
1.1       root     9824:                break;
                   9825:        }
                   9826: }
                   9827: 
                   9828: inline void msdos_int_21h_0dh()
                   9829: {
                   9830: }
                   9831: 
                   9832: inline void msdos_int_21h_0eh()
                   9833: {
                   9834:        if(REG8(DL) < 26) {
                   9835:                _chdrive(REG8(DL) + 1);
                   9836:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9837:                msdos_sda_update(current_psp);
1.1       root     9838:        }
                   9839:        REG8(AL) = 26; // zdrive
                   9840: }
                   9841: 
1.1.1.14  root     9842: inline void msdos_int_21h_0fh()
                   9843: {
                   9844:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9845:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9846:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9847:        HANDLE hFile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     9848:        
1.1.1.14  root     9849:        if(hFile == INVALID_HANDLE_VALUE) {
                   9850:                REG8(AL) = 0xff;
                   9851:        } else {
                   9852:                REG8(AL) = 0;
                   9853:                fcb->current_block = 0;
                   9854:                fcb->record_size = 128;
                   9855:                fcb->file_size = GetFileSize(hFile, NULL);
                   9856:                fcb->handle = hFile;
                   9857:                fcb->cur_record = 0;
                   9858:        }
                   9859: }
                   9860: 
                   9861: inline void msdos_int_21h_10h()
                   9862: {
                   9863:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9864:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9865:        
                   9866:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9867: }
                   9868: 
1.1       root     9869: inline void msdos_int_21h_11h()
                   9870: {
1.1.1.3   root     9871:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9872:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9873:        
                   9874:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9875:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9876:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9877:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9878:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9879:        WIN32_FIND_DATAA fd;
1.1       root     9880:        
1.1.1.13  root     9881:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9882:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9883:                FindClose(dtainfo->find_handle);
                   9884:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9885:        }
                   9886:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9887:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9888:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9889:        
1.1.1.14  root     9890:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9891:                dtainfo->allowable_mask &= ~8;
1.1       root     9892:        }
1.1.1.60  root     9893:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     9894:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9895:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     9896:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     9897:                                FindClose(dtainfo->find_handle);
                   9898:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9899:                                break;
                   9900:                        }
                   9901:                }
                   9902:        }
1.1.1.13  root     9903:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9904:                if(ext_fcb->flag == 0xff) {
                   9905:                        ext_find->flag = 0xff;
                   9906:                        memset(ext_find->reserved, 0, 5);
                   9907:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9908:                }
                   9909:                find->drive = _getdrive();
1.1.1.13  root     9910:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9911:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9912:                find->nt_res = 0;
                   9913:                msdos_find_file_conv_local_time(&fd);
                   9914:                find->create_time_ms = 0;
                   9915:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9916:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9917:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9918:                find->cluster_hi = find->cluster_lo = 0;
                   9919:                find->file_size = fd.nFileSizeLow;
                   9920:                REG8(AL) = 0x00;
1.1.1.14  root     9921:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9922:                if(ext_fcb->flag == 0xff) {
                   9923:                        ext_find->flag = 0xff;
                   9924:                        memset(ext_find->reserved, 0, 5);
                   9925:                        ext_find->attribute = 8;
                   9926:                }
                   9927:                find->drive = _getdrive();
                   9928:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9929:                find->attribute = 8;
                   9930:                find->nt_res = 0;
                   9931:                msdos_find_file_conv_local_time(&fd);
                   9932:                find->create_time_ms = 0;
                   9933:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9934:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9935:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9936:                find->cluster_hi = find->cluster_lo = 0;
                   9937:                find->file_size = 0;
1.1.1.14  root     9938:                dtainfo->allowable_mask &= ~8;
1.1       root     9939:                REG8(AL) = 0x00;
                   9940:        } else {
                   9941:                REG8(AL) = 0xff;
                   9942:        }
                   9943: }
                   9944: 
                   9945: inline void msdos_int_21h_12h()
                   9946: {
1.1.1.3   root     9947:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9948: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9949:        
                   9950:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9951:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9952:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9953:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.60  root     9954:        WIN32_FIND_DATAA fd;
1.1       root     9955:        
1.1.1.13  root     9956:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9957:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     9958:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9959:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9960:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     9961:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     9962:                                        FindClose(dtainfo->find_handle);
                   9963:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9964:                                        break;
                   9965:                                }
                   9966:                        }
                   9967:                } else {
1.1.1.13  root     9968:                        FindClose(dtainfo->find_handle);
                   9969:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9970:                }
                   9971:        }
1.1.1.13  root     9972:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9973:                if(ext_fcb->flag == 0xff) {
                   9974:                        ext_find->flag = 0xff;
                   9975:                        memset(ext_find->reserved, 0, 5);
                   9976:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9977:                }
                   9978:                find->drive = _getdrive();
1.1.1.13  root     9979:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9980:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9981:                find->nt_res = 0;
                   9982:                msdos_find_file_conv_local_time(&fd);
                   9983:                find->create_time_ms = 0;
                   9984:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9985:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9986:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9987:                find->cluster_hi = find->cluster_lo = 0;
                   9988:                find->file_size = fd.nFileSizeLow;
                   9989:                REG8(AL) = 0x00;
1.1.1.14  root     9990:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9991:                if(ext_fcb->flag == 0xff) {
                   9992:                        ext_find->flag = 0xff;
                   9993:                        memset(ext_find->reserved, 0, 5);
                   9994:                        ext_find->attribute = 8;
                   9995:                }
                   9996:                find->drive = _getdrive();
                   9997:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9998:                find->attribute = 8;
                   9999:                find->nt_res = 0;
                   10000:                msdos_find_file_conv_local_time(&fd);
                   10001:                find->create_time_ms = 0;
                   10002:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10003:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10004:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10005:                find->cluster_hi = find->cluster_lo = 0;
                   10006:                find->file_size = 0;
1.1.1.14  root     10007:                dtainfo->allowable_mask &= ~8;
1.1       root     10008:                REG8(AL) = 0x00;
                   10009:        } else {
                   10010:                REG8(AL) = 0xff;
                   10011:        }
                   10012: }
                   10013: 
                   10014: inline void msdos_int_21h_13h()
                   10015: {
1.1.1.3   root     10016:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     10017:                REG8(AL) = 0xff;
                   10018:        } else {
                   10019:                REG8(AL) = 0x00;
                   10020:        }
                   10021: }
                   10022: 
1.1.1.16  root     10023: inline void msdos_int_21h_14h()
                   10024: {
                   10025:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10026:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10027:        process_t *process = msdos_process_info_get(current_psp);
                   10028:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10029:        DWORD num = 0;
                   10030:        
                   10031:        memset(mem + dta_laddr, 0, fcb->record_size);
                   10032:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10033:                REG8(AL) = 1;
                   10034:        } else {
                   10035:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   10036:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   10037:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   10038:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10039:        }
                   10040: }
                   10041: 
                   10042: inline void msdos_int_21h_15h()
1.1.1.14  root     10043: {
                   10044:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10045:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     10046:        process_t *process = msdos_process_info_get(current_psp);
                   10047:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10048:        DWORD num = 0;
1.1.1.14  root     10049:        
1.1.1.16  root     10050:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10051:                REG8(AL) = 1;
                   10052:        } else {
                   10053:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10054:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   10055:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   10056:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   10057:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10058:        }
                   10059: }
                   10060: 
                   10061: inline void msdos_int_21h_16h()
                   10062: {
                   10063:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10064:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10065:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10066:        HANDLE hFile = CreateFileA(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, ext_fcb->flag == 0xff ? ext_fcb->attribute : FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     10067:        
1.1.1.14  root     10068:        if(hFile == INVALID_HANDLE_VALUE) {
                   10069:                REG8(AL) = 0xff;
                   10070:        } else {
                   10071:                REG8(AL) = 0;
                   10072:                fcb->current_block = 0;
                   10073:                fcb->record_size = 128;
                   10074:                fcb->file_size = 0;
                   10075:                fcb->handle = hFile;
                   10076:                fcb->cur_record = 0;
                   10077:        }
                   10078: }
                   10079: 
1.1.1.16  root     10080: inline void msdos_int_21h_17h()
                   10081: {
                   10082:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10083:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     10084: //     const char *path_src = msdos_fcb_path(fcb_src);
                   10085:        char path_src[MAX_PATH];
                   10086:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   10087:        
1.1.1.16  root     10088:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   10089:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     10090: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   10091:        char path_dst[MAX_PATH];
                   10092:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     10093:        
                   10094:        if(rename(path_src, path_dst)) {
                   10095:                REG8(AL) = 0xff;
                   10096:        } else {
                   10097:                REG8(AL) = 0;
                   10098:        }
                   10099: }
                   10100: 
1.1       root     10101: inline void msdos_int_21h_18h()
                   10102: {
                   10103:        REG8(AL) = 0x00;
                   10104: }
                   10105: 
                   10106: inline void msdos_int_21h_19h()
                   10107: {
                   10108:        REG8(AL) = _getdrive() - 1;
                   10109: }
                   10110: 
                   10111: inline void msdos_int_21h_1ah()
                   10112: {
                   10113:        process_t *process = msdos_process_info_get(current_psp);
                   10114:        
                   10115:        process->dta.w.l = REG16(DX);
1.1.1.3   root     10116:        process->dta.w.h = SREG(DS);
1.1.1.23  root     10117:        msdos_sda_update(current_psp);
1.1       root     10118: }
                   10119: 
                   10120: inline void msdos_int_21h_1bh()
                   10121: {
                   10122:        int drive_num = _getdrive() - 1;
                   10123:        UINT16 seg, ofs;
                   10124:        
                   10125:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10126:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10127:                REG8(AL) = dpb->highest_sector_num + 1;
                   10128:                REG16(CX) = dpb->bytes_per_sector;
                   10129:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10130:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10131:        } else {
                   10132:                REG8(AL) = 0xff;
1.1.1.3   root     10133:                m_CF = 1;
1.1       root     10134:        }
                   10135: 
                   10136: }
                   10137: 
                   10138: inline void msdos_int_21h_1ch()
                   10139: {
1.1.1.41  root     10140:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     10141:        UINT16 seg, ofs;
                   10142:        
                   10143:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10144:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10145:                REG8(AL) = dpb->highest_sector_num + 1;
                   10146:                REG16(CX) = dpb->bytes_per_sector;
                   10147:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10148:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10149:        } else {
                   10150:                REG8(AL) = 0xff;
1.1.1.3   root     10151:                m_CF = 1;
1.1       root     10152:        }
                   10153: 
                   10154: }
                   10155: 
                   10156: inline void msdos_int_21h_1dh()
                   10157: {
                   10158:        REG8(AL) = 0;
                   10159: }
                   10160: 
                   10161: inline void msdos_int_21h_1eh()
                   10162: {
                   10163:        REG8(AL) = 0;
                   10164: }
                   10165: 
                   10166: inline void msdos_int_21h_1fh()
                   10167: {
                   10168:        int drive_num = _getdrive() - 1;
                   10169:        UINT16 seg, ofs;
                   10170:        
                   10171:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10172:                REG8(AL) = 0;
1.1.1.3   root     10173:                SREG(DS) = seg;
                   10174:                i386_load_segment_descriptor(DS);
1.1       root     10175:                REG16(BX) = ofs;
                   10176:        } else {
                   10177:                REG8(AL) = 0xff;
1.1.1.3   root     10178:                m_CF = 1;
1.1       root     10179:        }
                   10180: }
                   10181: 
                   10182: inline void msdos_int_21h_20h()
                   10183: {
                   10184:        REG8(AL) = 0;
                   10185: }
                   10186: 
1.1.1.14  root     10187: inline void msdos_int_21h_21h()
                   10188: {
                   10189:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10190:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10191:        
                   10192:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10193:                REG8(AL) = 1;
                   10194:        } else {
                   10195:                process_t *process = msdos_process_info_get(current_psp);
                   10196:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10197:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     10198:                DWORD num = 0;
1.1.1.14  root     10199:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10200:                        REG8(AL) = 1;
                   10201:                } else {
                   10202:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10203:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10204:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     10205:                }
                   10206:        }
                   10207: }
                   10208: 
                   10209: inline void msdos_int_21h_22h()
                   10210: {
                   10211:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10212:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10213:        
                   10214:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10215:                REG8(AL) = 0xff;
                   10216:        } else {
                   10217:                process_t *process = msdos_process_info_get(current_psp);
                   10218:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     10219:                DWORD num = 0;
1.1.1.14  root     10220:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   10221:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10222:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10223:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10224:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     10225:        }
                   10226: }
                   10227: 
1.1.1.16  root     10228: inline void msdos_int_21h_23h()
                   10229: {
                   10230:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10231:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10232:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10233:        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     10234:        
                   10235:        if(hFile == INVALID_HANDLE_VALUE) {
                   10236:                REG8(AL) = 0xff;
                   10237:        } else {
                   10238:                UINT32 size = GetFileSize(hFile, NULL);
                   10239:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   10240:                REG8(AL) = 0;
                   10241:        }
                   10242: }
                   10243: 
                   10244: inline void msdos_int_21h_24h()
                   10245: {
                   10246:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10247:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10248:        
                   10249:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   10250: }
                   10251: 
1.1       root     10252: inline void msdos_int_21h_25h()
                   10253: {
                   10254:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     10255:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     10256: }
                   10257: 
                   10258: inline void msdos_int_21h_26h()
                   10259: {
                   10260:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10261:        
                   10262:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     10263:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     10264:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10265:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10266:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10267:        psp->parent_psp = 0;
                   10268: }
                   10269: 
1.1.1.16  root     10270: inline void msdos_int_21h_27h()
                   10271: {
                   10272:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10273:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10274:        
                   10275:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10276:                REG8(AL) = 1;
                   10277:        } else {
                   10278:                process_t *process = msdos_process_info_get(current_psp);
                   10279:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10280:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   10281:                DWORD num = 0;
                   10282:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   10283:                        REG8(AL) = 1;
                   10284:                } else {
                   10285:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10286:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10287:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10288:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10289:                }
                   10290:        }
                   10291: }
                   10292: 
                   10293: inline void msdos_int_21h_28h()
                   10294: {
                   10295:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10296:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10297:        
                   10298:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10299:                REG8(AL) = 0xff;
                   10300:        } else {
                   10301:                process_t *process = msdos_process_info_get(current_psp);
                   10302:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10303:                DWORD num = 0;
                   10304:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   10305:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10306:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10307:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10308:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10309:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10310:        }
                   10311: }
                   10312: 
1.1       root     10313: inline void msdos_int_21h_29h()
                   10314: {
1.1.1.20  root     10315:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   10316:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     10317:        UINT8 drv = 0;
                   10318:        char sep_chars[] = ":.;,=+";
                   10319:        char end_chars[] = "\\<>|/\"[]";
                   10320:        char spc_chars[] = " \t";
                   10321:        
1.1.1.20  root     10322:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   10323:        buffer[1023] = 0;
                   10324:        memset(name, 0x20, sizeof(name));
                   10325:        memset(ext, 0x20, sizeof(ext));
                   10326:        
1.1       root     10327:        if(REG8(AL) & 1) {
1.1.1.20  root     10328:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10329:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10330:                        ofs++;
                   10331:                }
                   10332:        }
1.1.1.20  root     10333:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10334:        
1.1.1.24  root     10335:        if(buffer[ofs + 1] == ':') {
                   10336:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10337:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10338:                        ofs += 2;
1.1.1.24  root     10339:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10340:                                ofs++;
                   10341:                        }
                   10342:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10343:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10344:                        ofs += 2;
1.1.1.24  root     10345:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10346:                                ofs++;
                   10347:                        }
1.1       root     10348:                }
                   10349:        }
1.1.1.20  root     10350:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10351:                UINT8 c = buffer[ofs];
                   10352:                if(is_kanji) {
                   10353:                        is_kanji = 0;
                   10354:                } else if(msdos_lead_byte_check(c)) {
                   10355:                        is_kanji = 1;
                   10356:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10357:                        break;
                   10358:                } else if(c >= 'a' && c <= 'z') {
                   10359:                        c -= 0x20;
                   10360:                }
                   10361:                ofs++;
                   10362:                name[i] = c;
                   10363:        }
1.1.1.20  root     10364:        if(buffer[ofs] == '.') {
1.1       root     10365:                ofs++;
1.1.1.20  root     10366:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10367:                        UINT8 c = buffer[ofs];
                   10368:                        if(is_kanji) {
                   10369:                                is_kanji = 0;
                   10370:                        } else if(msdos_lead_byte_check(c)) {
                   10371:                                is_kanji = 1;
                   10372:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10373:                                break;
                   10374:                        } else if(c >= 'a' && c <= 'z') {
                   10375:                                c -= 0x20;
                   10376:                        }
                   10377:                        ofs++;
                   10378:                        ext[i] = c;
                   10379:                }
                   10380:        }
1.1.1.20  root     10381:        int si = REG16(SI) + ofs;
1.1.1.3   root     10382:        int ds = SREG(DS);
1.1       root     10383:        while(si > 0xffff) {
                   10384:                si -= 0x10;
                   10385:                ds++;
                   10386:        }
                   10387:        REG16(SI) = si;
1.1.1.3   root     10388:        SREG(DS) = ds;
                   10389:        i386_load_segment_descriptor(DS);
1.1       root     10390:        
1.1.1.3   root     10391:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10392:        if(!(REG8(AL) & 2) || drv != 0) {
                   10393:                fcb[0] = drv;
                   10394:        }
                   10395:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10396:                memcpy(fcb + 1, name, 8);
                   10397:        }
                   10398:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10399:                memcpy(fcb + 9, ext, 3);
                   10400:        }
                   10401:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10402:                if(fcb[i] == '*') {
                   10403:                        found_star = 1;
                   10404:                }
                   10405:                if(found_star) {
                   10406:                        fcb[i] = '?';
                   10407:                }
                   10408:        }
1.1.1.20  root     10409:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10410:                if(fcb[i] == '*') {
                   10411:                        found_star = 1;
                   10412:                }
                   10413:                if(found_star) {
                   10414:                        fcb[i] = '?';
                   10415:                }
                   10416:        }
                   10417:        
1.1.1.44  root     10418:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10419:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10420:                        REG8(AL) = 0x01;
1.1.1.20  root     10421:                } else {
                   10422:                        REG8(AL) = 0x00;
1.1       root     10423:                }
                   10424:        } else {
                   10425:                REG8(AL) = 0xff;
                   10426:        }
                   10427: }
                   10428: 
                   10429: inline void msdos_int_21h_2ah()
                   10430: {
                   10431:        SYSTEMTIME sTime;
                   10432:        
                   10433:        GetLocalTime(&sTime);
                   10434:        REG16(CX) = sTime.wYear;
                   10435:        REG8(DH) = (UINT8)sTime.wMonth;
                   10436:        REG8(DL) = (UINT8)sTime.wDay;
                   10437:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10438: }
                   10439: 
                   10440: inline void msdos_int_21h_2bh()
                   10441: {
1.1.1.14  root     10442:        REG8(AL) = 0xff;
1.1       root     10443: }
                   10444: 
                   10445: inline void msdos_int_21h_2ch()
                   10446: {
                   10447:        SYSTEMTIME sTime;
                   10448:        
                   10449:        GetLocalTime(&sTime);
                   10450:        REG8(CH) = (UINT8)sTime.wHour;
                   10451:        REG8(CL) = (UINT8)sTime.wMinute;
                   10452:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10453:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10454: }
                   10455: 
                   10456: inline void msdos_int_21h_2dh()
                   10457: {
                   10458:        REG8(AL) = 0x00;
                   10459: }
                   10460: 
                   10461: inline void msdos_int_21h_2eh()
                   10462: {
                   10463:        process_t *process = msdos_process_info_get(current_psp);
                   10464:        
                   10465:        process->verify = REG8(AL);
                   10466: }
                   10467: 
                   10468: inline void msdos_int_21h_2fh()
                   10469: {
                   10470:        process_t *process = msdos_process_info_get(current_psp);
                   10471:        
                   10472:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10473:        SREG(ES) = process->dta.w.h;
                   10474:        i386_load_segment_descriptor(ES);
1.1       root     10475: }
                   10476: 
                   10477: inline void msdos_int_21h_30h()
                   10478: {
                   10479:        // Version Flag / OEM
1.1.1.27  root     10480:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10481: #ifdef SUPPORT_HMA
                   10482:                REG16(BX) = 0x0000;
                   10483: #else
                   10484:                REG16(BX) = 0x1000; // DOS is in HMA
                   10485: #endif
1.1       root     10486:        } else {
1.1.1.27  root     10487:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10488:                // but this is not correct on Windows 98 SE
                   10489: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10490:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10491:        }
1.1.1.27  root     10492:        REG16(CX) = 0x0000;
1.1.1.30  root     10493:        REG8(AL) = dos_major_version;   // 7
                   10494:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10495: }
                   10496: 
                   10497: inline void msdos_int_21h_31h()
                   10498: {
1.1.1.29  root     10499:        try {
                   10500:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10501:        } catch(...) {
                   10502:                // recover the broken mcb
                   10503:                int mcb_seg = current_psp - 1;
                   10504:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10505:                
1.1.1.29  root     10506:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10507:                        mcb->mz = 'M';
                   10508:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10509:                        
1.1.1.29  root     10510:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10511:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10512:                        } else {
1.1.1.39  root     10513:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10514:                        }
                   10515:                } else {
                   10516:                        mcb->mz = 'Z';
1.1.1.30  root     10517:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10518:                }
                   10519:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10520:        }
1.1       root     10521:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10522: }
                   10523: 
                   10524: inline void msdos_int_21h_32h()
                   10525: {
                   10526:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10527:        UINT16 seg, ofs;
                   10528:        
                   10529:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10530:                REG8(AL) = 0;
1.1.1.3   root     10531:                SREG(DS) = seg;
                   10532:                i386_load_segment_descriptor(DS);
1.1       root     10533:                REG16(BX) = ofs;
                   10534:        } else {
                   10535:                REG8(AL) = 0xff;
1.1.1.3   root     10536:                m_CF = 1;
1.1       root     10537:        }
                   10538: }
                   10539: 
                   10540: inline void msdos_int_21h_33h()
                   10541: {
                   10542:        char path[MAX_PATH];
1.1.1.48  root     10543:        char drive = 3; // C:
1.1       root     10544:        
                   10545:        switch(REG8(AL)) {
                   10546:        case 0x00:
1.1.1.33  root     10547:                REG8(DL) = ctrl_break_checking;
1.1       root     10548:                break;
                   10549:        case 0x01:
1.1.1.33  root     10550:                ctrl_break_checking = REG8(DL);
                   10551:                break;
                   10552:        case 0x02:
                   10553:                {
                   10554:                        UINT8 old = ctrl_break_checking;
                   10555:                        ctrl_break_checking = REG8(DL);
                   10556:                        REG8(DL) = old;
                   10557:                }
                   10558:                break;
                   10559:        case 0x03:
                   10560:        case 0x04:
                   10561:                // DOS 4.0+ - Unused
1.1       root     10562:                break;
                   10563:        case 0x05:
1.1.1.60  root     10564:                if(GetSystemDirectoryA(path, MAX_PATH) != 0) {
1.1.1.48  root     10565:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10566:                                drive = path[0] - 'a' + 1;
                   10567:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10568:                                drive = path[0] - 'A' + 1;
                   10569:                        }
1.1       root     10570:                }
1.1.1.48  root     10571:                REG8(DL) = (UINT8)drive;
1.1       root     10572:                break;
                   10573:        case 0x06:
1.1.1.2   root     10574:                // MS-DOS version (7.10)
1.1       root     10575:                REG8(BL) = 7;
1.1.1.2   root     10576:                REG8(BH) = 10;
1.1       root     10577:                REG8(DL) = 0;
1.1.1.29  root     10578: #ifdef SUPPORT_HMA
                   10579:                REG8(DH) = 0x00;
                   10580: #else
                   10581:                REG8(DH) = 0x10; // DOS is in HMA
                   10582: #endif
1.1       root     10583:                break;
1.1.1.6   root     10584:        case 0x07:
                   10585:                if(REG8(DL) == 0) {
                   10586:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10587:                } else if(REG8(DL) == 1) {
                   10588:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10589:                }
                   10590:                break;
1.1       root     10591:        default:
1.1.1.22  root     10592:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     10593: //             REG16(AX) = 0x01;
                   10594: //             m_CF = 1;
                   10595:                REG8(AL) = 0xff;
1.1       root     10596:                break;
                   10597:        }
                   10598: }
                   10599: 
1.1.1.23  root     10600: inline void msdos_int_21h_34h()
                   10601: {
                   10602:        SREG(ES) = SDA_TOP >> 4;
                   10603:        i386_load_segment_descriptor(ES);
                   10604:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10605: }
                   10606: 
1.1       root     10607: inline void msdos_int_21h_35h()
                   10608: {
                   10609:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10610:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10611:        i386_load_segment_descriptor(ES);
1.1       root     10612: }
                   10613: 
                   10614: inline void msdos_int_21h_36h()
                   10615: {
                   10616:        struct _diskfree_t df = {0};
                   10617:        
                   10618:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10619:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10620:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10621:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10622:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10623:        } else {
                   10624:                REG16(AX) = 0xffff;
                   10625:        }
                   10626: }
                   10627: 
                   10628: inline void msdos_int_21h_37h()
                   10629: {
1.1.1.22  root     10630:        static UINT8 dev_flag = 0xff;
1.1       root     10631:        
                   10632:        switch(REG8(AL)) {
                   10633:        case 0x00:
1.1.1.22  root     10634:                {
                   10635:                        process_t *process = msdos_process_info_get(current_psp);
                   10636:                        REG8(AL) = 0x00;
                   10637:                        REG8(DL) = process->switchar;
                   10638:                }
1.1       root     10639:                break;
                   10640:        case 0x01:
1.1.1.22  root     10641:                {
                   10642:                        process_t *process = msdos_process_info_get(current_psp);
                   10643:                        REG8(AL) = 0x00;
                   10644:                        process->switchar = REG8(DL);
1.1.1.23  root     10645:                        msdos_sda_update(current_psp);
1.1.1.22  root     10646:                }
                   10647:                break;
                   10648:        case 0x02:
                   10649:                REG8(DL) = dev_flag;
                   10650:                break;
                   10651:        case 0x03:
                   10652:                dev_flag = REG8(DL);
                   10653:                break;
                   10654:        case 0xd0:
                   10655:        case 0xd1:
                   10656:        case 0xd2:
                   10657:        case 0xd3:
                   10658:        case 0xd4:
                   10659:        case 0xd5:
                   10660:        case 0xd6:
                   10661:        case 0xd7:
                   10662:        case 0xdc:
                   10663:        case 0xdd:
                   10664:        case 0xde:
                   10665:        case 0xdf:
1.1.1.48  root     10666:                // DIET v1.43e
                   10667: //             REG16(AX) = 1;
                   10668:                REG8(AL) = 0xff;
1.1       root     10669:                break;
                   10670:        default:
1.1.1.22  root     10671:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     10672: //             REG16(AX) = 1;
                   10673:                REG8(AL) = 0xff;
1.1       root     10674:                break;
                   10675:        }
                   10676: }
                   10677: 
1.1.1.52  root     10678: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10679: {
                   10680:        char LCdata[80];
                   10681:        
1.1.1.19  root     10682:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.60  root     10683:        GetLocaleInfoA(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10684:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.60  root     10685:        GetLocaleInfoA(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10686:        ci->currency_format = *LCdata - '0';
1.1.1.60  root     10687:        GetLocaleInfoA(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10688:        ci->date_format = *LCdata - '0';
1.1.1.60  root     10689:        GetLocaleInfoA(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10690:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.60  root     10691:        GetLocaleInfoA(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10692:        *ci->date_sep = *LCdata;
1.1.1.60  root     10693:        GetLocaleInfoA(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10694:        *ci->dec_sep = *LCdata;
1.1.1.60  root     10695:        GetLocaleInfoA(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10696:        *ci->list_sep = *LCdata;
1.1.1.60  root     10697:        GetLocaleInfoA(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10698:        *ci->thou_sep = *LCdata;
1.1.1.60  root     10699:        GetLocaleInfoA(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10700:        *ci->time_sep = *LCdata;
1.1.1.60  root     10701:        GetLocaleInfoA(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10702:        if(strchr(LCdata, 'H') != NULL) {
                   10703:                ci->time_format = 1;
                   10704:        }
1.1.1.49  root     10705:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10706:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.60  root     10707:        GetLocaleInfoA(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10708:        return atoi(LCdata);
                   10709: }
                   10710: 
1.1.1.42  root     10711: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10712: {
                   10713:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10714: }
                   10715: 
1.1.1.43  root     10716: void set_country_info(country_info_t *ci, int size)
                   10717: {
                   10718:        char LCdata[80];
                   10719:        
                   10720:        if(size >= 0x00 + 2) {
                   10721:                memset(LCdata, 0, sizeof(LCdata));
                   10722:                *LCdata = '0' + ci->date_format;
1.1.1.60  root     10723:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
1.1.1.43  root     10724:        }
                   10725:        if(size >= 0x02 + 5) {
                   10726:                memset(LCdata, 0, sizeof(LCdata));
                   10727:                memcpy(LCdata, &ci->currency_symbol, 4);
1.1.1.60  root     10728:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
1.1.1.43  root     10729:        }
                   10730:        if(size >= 0x07 + 2) {
                   10731:                memset(LCdata, 0, sizeof(LCdata));
                   10732:                *LCdata = *ci->thou_sep;
1.1.1.60  root     10733:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
1.1.1.43  root     10734:        }
                   10735:        if(size >= 0x09 + 2) {
                   10736:                memset(LCdata, 0, sizeof(LCdata));
                   10737:                *LCdata = *ci->dec_sep;
1.1.1.60  root     10738:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
1.1.1.43  root     10739:        }
                   10740:        if(size >= 0x0b + 2) {
                   10741:                memset(LCdata, 0, sizeof(LCdata));
                   10742:                *LCdata = *ci->date_sep;
1.1.1.60  root     10743:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
1.1.1.43  root     10744:        }
                   10745:        if(size >= 0x0d + 2) {
                   10746:                memset(LCdata, 0, sizeof(LCdata));
                   10747:                *LCdata = *ci->time_sep;
1.1.1.60  root     10748:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
1.1.1.43  root     10749:        }
                   10750:        if(size >= 0x0f + 1) {
                   10751:                memset(LCdata, 0, sizeof(LCdata));
                   10752:                *LCdata = '0' + ci->currency_format;
1.1.1.60  root     10753:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
1.1.1.43  root     10754:        }
                   10755:        if(size >= 0x10 + 1) {
                   10756:                sprintf(LCdata, "%d", ci->currency_dec_digits);
1.1.1.60  root     10757:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
1.1.1.43  root     10758:        }
                   10759:        if(size >= 0x11 + 1) {
                   10760:                // FIXME: is time format always H/h:mm:ss ???
                   10761:                if(ci->time_format & 1) {
                   10762:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10763:                } else {
                   10764:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10765:                }
1.1.1.60  root     10766:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
1.1.1.43  root     10767:        }
                   10768:        if(size >= 0x12 + 4) {
                   10769:                // 12h  DWORD   address of case map routine
                   10770:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10771:        }
                   10772:        if(size >= 0x16 + 2) {
                   10773:                memset(LCdata, 0, sizeof(LCdata));
                   10774:                *LCdata = *ci->list_sep;
1.1.1.60  root     10775:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
1.1.1.43  root     10776:        }
                   10777: }
                   10778: 
1.1.1.42  root     10779: #ifndef SUBLANG_SWAHILI
                   10780:        #define SUBLANG_SWAHILI 0x01
                   10781: #endif
                   10782: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10783:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10784: #endif
                   10785: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10786:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10787: #endif
                   10788: #ifndef LANG_BANGLA
                   10789:        #define LANG_BANGLA 0x45
                   10790: #endif
                   10791: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10792:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10793: #endif
                   10794: 
                   10795: static const struct {
                   10796:        int code;
                   10797:        USHORT usPrimaryLanguage;
                   10798:        USHORT usSubLanguage;
                   10799: } country_table[] = {
                   10800:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10801:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10802:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10803:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10804:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10805:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10806: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10807: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10808: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10809: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10810: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10811:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10812:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10813: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10814:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10815: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10816:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10817:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10818:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10819:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10820:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10821:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10822:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10823: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10824: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10825:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10826:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10827:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10828:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10829:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10830: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10831: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10832: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10833:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10834: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10835: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10836: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10837: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10838:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10839:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10840:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10841: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10842:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10843:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10844:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10845:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10846:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10847:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10848:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10849:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10850:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10851:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10852:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10853:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10854:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10855: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10856:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10857:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10858:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10859:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10860:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10861:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10862:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10863:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10864:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10865:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10866: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10867:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10868: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10869:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10870:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10871:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10872:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10873:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10874:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10875:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10876:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10877: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10878:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10879: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10880: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10881:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10882: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10883:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10884:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10885:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10886:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10887:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10888:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10889:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10890: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10891: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10892:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10893: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10894:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10895:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10896:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10897:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10898: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10899: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10900: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10901:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10902:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10903:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10904:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10905:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10906:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10907:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10908:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10909:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10910:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10911:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10912:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10913:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10914:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10915:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10916:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10917:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10918:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10919:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10920:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10921:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10922: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10923:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10924: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10925:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10926:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10927:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10928:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10929:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10930:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10931:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10932:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10933:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10934:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10935:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10936:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10937:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10938:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10939:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10940:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10941:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10942:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10943:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10944:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10945:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10946:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10947:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10948:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10949:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10950: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10951:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10952:        {-1, 0, 0},
                   10953: };
                   10954: 
1.1.1.14  root     10955: inline void msdos_int_21h_38h()
                   10956: {
                   10957:        switch(REG8(AL)) {
                   10958:        case 0x00:
1.1.1.19  root     10959:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10960:                break;
                   10961:        default:
1.1.1.42  root     10962:                for(int i = 0;; i++) {
                   10963:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10964:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10965:                                break;
                   10966:                        } else if(country_table[i].code == -1) {
                   10967: //                             unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   10968: //                             REG16(AX) = 2;
                   10969: //                             m_CF = 1;
1.1.1.48  root     10970:                                // get current coutry info
1.1.1.42  root     10971:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10972:                                break;
                   10973:                        }
                   10974:                }
1.1.1.14  root     10975:                break;
                   10976:        }
                   10977: }
                   10978: 
1.1       root     10979: inline void msdos_int_21h_39h(int lfn)
                   10980: {
1.1.1.3   root     10981:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10982:                REG16(AX) = errno;
1.1.1.3   root     10983:                m_CF = 1;
1.1       root     10984:        }
                   10985: }
                   10986: 
                   10987: inline void msdos_int_21h_3ah(int lfn)
                   10988: {
1.1.1.3   root     10989:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10990:                REG16(AX) = errno;
1.1.1.3   root     10991:                m_CF = 1;
1.1       root     10992:        }
                   10993: }
                   10994: 
                   10995: inline void msdos_int_21h_3bh(int lfn)
                   10996: {
1.1.1.45  root     10997:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10998:        
                   10999:        if(_chdir(path)) {
1.1.1.17  root     11000:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     11001:                m_CF = 1;
1.1.1.44  root     11002:        } else {
                   11003:                int drv = _getdrive() - 1;
                   11004:                if(path[1] == ':') {
                   11005:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   11006:                                drv = path[0] - 'A';
                   11007:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   11008:                                drv = path[0] - 'a';
                   11009:                        }
                   11010:                }
                   11011:                msdos_cds_update(drv, path);
1.1       root     11012:        }
                   11013: }
                   11014: 
                   11015: inline void msdos_int_21h_3ch()
                   11016: {
1.1.1.45  root     11017:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     11018:        int attr = GetFileAttributesA(path);
1.1.1.37  root     11019:        int fd = -1;
                   11020:        int sio_port = 0;
                   11021:        int lpt_port = 0;
1.1       root     11022:        
1.1.1.45  root     11023:        if(msdos_is_device_path(path)) {
                   11024:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     11025:        } else {
                   11026:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11027:        }
                   11028:        if(fd != -1) {
                   11029:                if(attr == -1) {
                   11030:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11031:                }
1.1.1.60  root     11032:                SetFileAttributesA(path, attr);
1.1       root     11033:                REG16(AX) = fd;
1.1.1.45  root     11034:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11035:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11036:        } else {
                   11037:                REG16(AX) = errno;
1.1.1.3   root     11038:                m_CF = 1;
1.1       root     11039:        }
                   11040: }
                   11041: 
                   11042: inline void msdos_int_21h_3dh()
                   11043: {
1.1.1.45  root     11044:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11045:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     11046:        int fd = -1;
                   11047:        int sio_port = 0;
                   11048:        int lpt_port = 0;
1.1       root     11049:        
                   11050:        if(mode < 0x03) {
1.1.1.45  root     11051:                if(msdos_is_device_path(path)) {
                   11052:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     11053:                } else {
1.1.1.13  root     11054:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     11055:                }
1.1       root     11056:                if(fd != -1) {
                   11057:                        REG16(AX) = fd;
1.1.1.45  root     11058:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11059:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11060:                } else {
                   11061:                        REG16(AX) = errno;
1.1.1.3   root     11062:                        m_CF = 1;
1.1       root     11063:                }
                   11064:        } else {
                   11065:                REG16(AX) = 0x0c;
1.1.1.3   root     11066:                m_CF = 1;
1.1       root     11067:        }
                   11068: }
                   11069: 
                   11070: inline void msdos_int_21h_3eh()
                   11071: {
                   11072:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11073:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11074:        
1.1.1.20  root     11075:        if(fd < process->max_files && file_handler[fd].valid) {
                   11076:                _close(fd);
                   11077:                msdos_file_handler_close(fd);
                   11078:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     11079:        } else {
                   11080:                REG16(AX) = 0x06;
1.1.1.3   root     11081:                m_CF = 1;
1.1       root     11082:        }
                   11083: }
                   11084: 
1.1.1.35  root     11085: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   11086: {
                   11087:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   11088:        int max = REG16(CX);
                   11089:        int p = 0;
                   11090:        
                   11091:        while(max > p) {
                   11092:                int chr = msdos_getch();
                   11093:                
                   11094:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   11095:                        p = 0;
                   11096:                        buf[p++] = 0x0d;
                   11097:                        if(max > p) {
                   11098:                                buf[p++] = 0x0a;
                   11099:                        }
                   11100:                        msdos_putch(0x03);
                   11101:                        msdos_putch(0x0d);
                   11102:                        msdos_putch(0x0a);
                   11103:                        break;
                   11104:                } else if(ctrl_break_pressed) {
                   11105:                        // skip this byte
                   11106:                } else if(chr == 0x00) {
                   11107:                        // skip 2nd byte
                   11108:                        msdos_getch();
                   11109:                } else if(chr == 0x0d) {
                   11110:                        // carriage return
                   11111:                        buf[p++] = 0x0d;
                   11112:                        if(max > p) {
                   11113:                                buf[p++] = 0x0a;
                   11114:                        }
                   11115:                        msdos_putch('\n');
                   11116:                        break;
                   11117:                } else if(chr == 0x08) {
                   11118:                        // back space
                   11119:                        if(p > 0) {
                   11120:                                p--;
                   11121:                                if(msdos_ctrl_code_check(buf[p])) {
                   11122:                                        msdos_putch(0x08);
                   11123:                                        msdos_putch(0x08);
                   11124:                                        msdos_putch(0x20);
                   11125:                                        msdos_putch(0x20);
                   11126:                                        msdos_putch(0x08);
                   11127:                                        msdos_putch(0x08);
1.1.1.36  root     11128:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   11129:                                        p--;
                   11130:                                        msdos_putch(0x08);
                   11131:                                        msdos_putch(0x08);
                   11132:                                        msdos_putch(0x20);
                   11133:                                        msdos_putch(0x20);
                   11134:                                        msdos_putch(0x08);
                   11135:                                        msdos_putch(0x08);
1.1.1.35  root     11136:                                } else {
                   11137:                                        msdos_putch(0x08);
                   11138:                                        msdos_putch(0x20);
                   11139:                                        msdos_putch(0x08);
                   11140:                                }
                   11141:                        }
                   11142:                } else if(chr == 0x1b) {
                   11143:                        // escape
                   11144:                        while(p > 0) {
                   11145:                                p--;
                   11146:                                if(msdos_ctrl_code_check(buf[p])) {
                   11147:                                        msdos_putch(0x08);
                   11148:                                        msdos_putch(0x08);
                   11149:                                        msdos_putch(0x20);
                   11150:                                        msdos_putch(0x20);
                   11151:                                        msdos_putch(0x08);
                   11152:                                        msdos_putch(0x08);
                   11153:                                } else {
                   11154:                                        msdos_putch(0x08);
                   11155:                                        msdos_putch(0x20);
                   11156:                                        msdos_putch(0x08);
                   11157:                                }
                   11158:                        }
                   11159:                } else {
                   11160:                        buf[p++] = chr;
                   11161:                        msdos_putch(chr);
                   11162:                }
                   11163:        }
                   11164:        REG16(AX) = p;
                   11165:        
                   11166: #ifdef USE_SERVICE_THREAD
                   11167:        service_exit = true;
                   11168: #endif
                   11169:        return(0);
                   11170: }
                   11171: 
1.1       root     11172: inline void msdos_int_21h_3fh()
                   11173: {
                   11174:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11175:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11176:        
1.1.1.20  root     11177:        if(fd < process->max_files && file_handler[fd].valid) {
                   11178:                if(file_mode[file_handler[fd].mode].in) {
                   11179:                        if(file_handler[fd].atty) {
1.1       root     11180:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     11181:                                if(REG16(CX) != 0) {
                   11182: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     11183:                                        if(!in_service && !in_service_29h &&
1.1.1.58  root     11184:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     11185:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   11186:                                                // msdos_putch() will be used in this service
                   11187:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   11188:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   11189:                                        } else {
                   11190: #endif
                   11191:                                                msdos_int_21h_3fh_thread(NULL);
                   11192:                                                REQUEST_HARDWRE_UPDATE();
                   11193: #ifdef USE_SERVICE_THREAD
                   11194:                                        }
1.1.1.35  root     11195: #endif
                   11196:                                } else {
                   11197:                                        REG16(AX) = 0;
1.1       root     11198:                                }
                   11199:                        } else {
1.1.1.37  root     11200:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11201:                        }
                   11202:                } else {
                   11203:                        REG16(AX) = 0x05;
1.1.1.3   root     11204:                        m_CF = 1;
1.1       root     11205:                }
                   11206:        } else {
                   11207:                REG16(AX) = 0x06;
1.1.1.3   root     11208:                m_CF = 1;
1.1       root     11209:        }
                   11210: }
                   11211: 
                   11212: inline void msdos_int_21h_40h()
                   11213: {
                   11214:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11215:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11216:        
1.1.1.20  root     11217:        if(fd < process->max_files && file_handler[fd].valid) {
                   11218:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     11219:                        if(REG16(CX)) {
1.1.1.20  root     11220:                                if(file_handler[fd].atty) {
1.1       root     11221:                                        // BX is stdout/stderr or is redirected to stdout
                   11222:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     11223:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     11224:                                        }
                   11225:                                        REG16(AX) = REG16(CX);
                   11226:                                } else {
1.1.1.20  root     11227:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11228:                                }
                   11229:                        } else {
1.1.1.20  root     11230:                                UINT32 pos = _tell(fd);
                   11231:                                _lseek(fd, 0, SEEK_END);
                   11232:                                UINT32 size = _tell(fd);
1.1.1.12  root     11233:                                if(pos < size) {
1.1.1.20  root     11234:                                        _lseek(fd, pos, SEEK_SET);
                   11235:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     11236:                                } else {
                   11237:                                        for(UINT32 i = size; i < pos; i++) {
                   11238:                                                UINT8 tmp = 0;
1.1.1.23  root     11239:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     11240:                                        }
1.1.1.20  root     11241:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     11242:                                }
1.1.1.23  root     11243:                                REG16(AX) = 0;
1.1       root     11244:                        }
                   11245:                } else {
                   11246:                        REG16(AX) = 0x05;
1.1.1.3   root     11247:                        m_CF = 1;
1.1       root     11248:                }
                   11249:        } else {
                   11250:                REG16(AX) = 0x06;
1.1.1.3   root     11251:                m_CF = 1;
1.1       root     11252:        }
                   11253: }
                   11254: 
                   11255: inline void msdos_int_21h_41h(int lfn)
                   11256: {
1.1.1.3   root     11257:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11258:                REG16(AX) = errno;
1.1.1.3   root     11259:                m_CF = 1;
1.1       root     11260:        }
                   11261: }
                   11262: 
                   11263: inline void msdos_int_21h_42h()
                   11264: {
                   11265:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11266:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11267:        
1.1.1.20  root     11268:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11269:                if(REG8(AL) < 0x03) {
1.1.1.35  root     11270:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     11271:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   11272:                        UINT32 pos = _tell(fd);
1.1       root     11273:                        REG16(AX) = pos & 0xffff;
                   11274:                        REG16(DX) = (pos >> 16);
                   11275:                } else {
                   11276:                        REG16(AX) = 0x01;
1.1.1.3   root     11277:                        m_CF = 1;
1.1       root     11278:                }
                   11279:        } else {
                   11280:                REG16(AX) = 0x06;
1.1.1.3   root     11281:                m_CF = 1;
1.1       root     11282:        }
                   11283: }
                   11284: 
                   11285: inline void msdos_int_21h_43h(int lfn)
                   11286: {
1.1.1.45  root     11287:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     11288:        int attr;
                   11289:        
1.1.1.14  root     11290:        if(!lfn && REG8(AL) > 2) {
                   11291:                REG16(AX) = 0x01;
                   11292:                m_CF = 1;
                   11293:                return;
                   11294:        }
                   11295:        switch(REG8(lfn ? BL : AL)) {
1.1       root     11296:        case 0x00:
1.1.1.60  root     11297:                if((attr = GetFileAttributesA(path)) != -1) {
1.1.1.14  root     11298:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   11299:                } else {
                   11300:                        REG16(AX) = (UINT16)GetLastError();
                   11301:                        m_CF = 1;
                   11302:                }
                   11303:                break;
                   11304:        case 0x01:
1.1.1.60  root     11305:                if(!SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)))) {
1.1.1.14  root     11306:                        REG16(AX) = (UINT16)GetLastError();
                   11307:                        m_CF = 1;
                   11308:                }
                   11309:                break;
                   11310:        case 0x02:
                   11311:                {
1.1.1.60  root     11312:                        DWORD compressed_size = GetCompressedFileSizeA(path, NULL), file_size = 0;
1.1.1.45  root     11313:                        if(compressed_size != INVALID_FILE_SIZE) {
                   11314:                                if(compressed_size != 0) {
1.1.1.60  root     11315:                                        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.45  root     11316:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   11317:                                                file_size = GetFileSize(hFile, NULL);
                   11318:                                                CloseHandle(hFile);
                   11319:                                        }
                   11320:                                        if(compressed_size == file_size) {
                   11321:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11322:                                                // this isn't correct if the file is in the NTFS MFT
1.1.1.60  root     11323:                                                if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1.1.45  root     11324:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   11325:                                                }
1.1.1.14  root     11326:                                        }
                   11327:                                }
1.1.1.45  root     11328:                                REG16(AX) = LOWORD(compressed_size);
                   11329:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11330:                        } else {
                   11331:                                REG16(AX) = (UINT16)GetLastError();
                   11332:                                m_CF = 1;
1.1       root     11333:                        }
1.1.1.14  root     11334:                }
                   11335:                break;
                   11336:        case 0x03:
                   11337:        case 0x05:
                   11338:        case 0x07:
1.1.1.48  root     11339:                if(lfn) {
1.1.1.60  root     11340:                        HANDLE hFile = CreateFileA(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     11341:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11342:                                FILETIME local, time;
                   11343:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11344:                                if(REG8(BL) == 7) {
                   11345:                                        ULARGE_INTEGER hund;
                   11346:                                        hund.LowPart = local.dwLowDateTime;
                   11347:                                        hund.HighPart = local.dwHighDateTime;
                   11348:                                        hund.QuadPart += REG16(SI) * 100000;
                   11349:                                        local.dwLowDateTime = hund.LowPart;
                   11350:                                        local.dwHighDateTime = hund.HighPart;
                   11351:                                }
                   11352:                                LocalFileTimeToFileTime(&local, &time);
                   11353:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11354:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11355:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11356:                                        REG16(AX) = (UINT16)GetLastError();
                   11357:                                        m_CF = 1;
                   11358:                                }
                   11359:                                CloseHandle(hFile);
                   11360:                        } else {
                   11361:                                REG16(AX) = (UINT16)GetLastError();
                   11362:                                m_CF = 1;
1.1       root     11363:                        }
1.1.1.48  root     11364:                } else {
                   11365:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11366:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11367:                        // 214307 DR DOS 6.0 - Set File Owner
                   11368: //                     unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   11369:                        REG16(AX) = 0x01;
                   11370:                        m_CF = 1;
1.1.1.14  root     11371:                }
                   11372:                break;
                   11373:        case 0x04:
                   11374:        case 0x06:
                   11375:        case 0x08:
1.1.1.48  root     11376:                if(lfn) {
1.1.1.14  root     11377:                        WIN32_FILE_ATTRIBUTE_DATA fad;
1.1.1.60  root     11378:                        if(GetFileAttributesExA(path, GetFileExInfoStandard, (LPVOID)&fad)) {
1.1.1.14  root     11379:                                FILETIME *time, local;
                   11380:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11381:                                                   0x06 ? &fad.ftLastAccessTime :
                   11382:                                                          &fad.ftCreationTime;
                   11383:                                FileTimeToLocalFileTime(time, &local);
                   11384:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11385:                                if(REG8(BL) == 0x08) {
                   11386:                                        ULARGE_INTEGER hund;
                   11387:                                        hund.LowPart = local.dwLowDateTime;
                   11388:                                        hund.HighPart = local.dwHighDateTime;
                   11389:                                        hund.QuadPart /= 100000;
                   11390:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11391:                                }
                   11392:                        } else {
                   11393:                                REG16(AX) = (UINT16)GetLastError();
                   11394:                                m_CF = 1;
1.1       root     11395:                        }
1.1.1.48  root     11396:                } else {
                   11397:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11398:                        // 214306 DR DOS 6.0 - Get File Owner
                   11399: //                     unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   11400:                        REG16(AX) = 0x01;
                   11401:                        m_CF = 1;
1.1.1.14  root     11402:                }
                   11403:                break;
1.1.1.43  root     11404:        case 0xff:
1.1.1.48  root     11405:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11406:                        if(REG8(CL) == 0x39) {
                   11407:                                msdos_int_21h_39h(1);
                   11408:                                break;
                   11409:                        } else if(REG8(CL) == 0x56) {
                   11410:                                msdos_int_21h_56h(1);
                   11411:                                break;
                   11412:                        }
                   11413:                }
1.1.1.14  root     11414:        default:
1.1.1.22  root     11415:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     11416:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11417:                m_CF = 1;
                   11418:                break;
                   11419:        }
                   11420: }
                   11421: 
                   11422: inline void msdos_int_21h_44h()
                   11423: {
1.1.1.22  root     11424:        static UINT16 iteration_count = 0;
                   11425:        
1.1.1.44  root     11426:        process_t *process;
                   11427:        int fd, drv;
1.1.1.14  root     11428:        
                   11429:        switch(REG8(AL)) {
                   11430:        case 0x00:
                   11431:        case 0x01:
                   11432:        case 0x02:
                   11433:        case 0x03:
                   11434:        case 0x04:
                   11435:        case 0x05:
                   11436:        case 0x06:
                   11437:        case 0x07:
1.1.1.44  root     11438:                process = msdos_process_info_get(current_psp);
                   11439:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11440:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11441:                        REG16(AX) = 0x06;
                   11442:                        m_CF = 1;
                   11443:                        return;
1.1.1.14  root     11444:                }
                   11445:                break;
                   11446:        case 0x08:
                   11447:        case 0x09:
1.1.1.44  root     11448:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11449:                if(!msdos_is_valid_drive(drv)) {
                   11450:                        // invalid drive
1.1.1.14  root     11451:                        REG16(AX) = 0x0f;
                   11452:                        m_CF = 1;
                   11453:                        return;
1.1       root     11454:                }
                   11455:                break;
                   11456:        }
                   11457:        switch(REG8(AL)) {
1.1.1.48  root     11458:        case 0x00: // Get Device Information
1.1.1.20  root     11459:                REG16(DX) = file_handler[fd].info;
1.1       root     11460:                break;
1.1.1.48  root     11461:        case 0x01: // Set Device Information
1.1.1.45  root     11462:                if(REG8(DH) != 0) {
                   11463: //                     REG16(AX) = 0x0d; // data invalid
                   11464: //                     m_CF = 1;
                   11465:                        file_handler[fd].info = REG16(DX);
                   11466:                } else {
                   11467:                        file_handler[fd].info &= 0xff00;
                   11468:                        file_handler[fd].info |= REG8(DL);
                   11469:                }
1.1       root     11470:                break;
1.1.1.48  root     11471:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11472:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11473:                        // from DOSBox
                   11474:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11475:                        case 0x00:
                   11476:                                if(REG16(CX) >= 6) {
                   11477:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11478:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11479:                                        REG16(AX) = 6; // number of bytes actually read
                   11480:                                } else {
                   11481:                                        REG16(AX) = 0x0d; // data invalid
                   11482:                                        m_CF = 1;
                   11483:                                }
                   11484:                                break;
                   11485:                        case 0x01:
                   11486:                                if(REG16(CX) >= 6) {
                   11487:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11488:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11489:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11490:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11491:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11492:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11493:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11494:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11495:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11496:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11497:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11498:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11499:                                                } else {
                   11500:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11501:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11502:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11503:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11504:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11505:                                                }
                   11506:                                        }
                   11507:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11508:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11509:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11510:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11511:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11512:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11513:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11514:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11515:                                        
                   11516:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11517:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11518:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11519:                                        REG16(AX) = 6; // number of bytes actually read
                   11520:                                } else {
                   11521:                                        REG16(AX) = 0x0d; // data invalid
                   11522:                                        m_CF = 1;
                   11523:                                }
                   11524:                                break;
                   11525:                        case 0x02:
                   11526:                                if(REG16(CX) >= 2) {
                   11527:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11528:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11529:                                        REG16(AX) = 2; // number of bytes actually read
                   11530:                                } else {
                   11531:                                        REG16(AX) = 0x0d; // data invalid
                   11532:                                        m_CF = 1;
                   11533:                                }
                   11534:                                break;
                   11535:                        case 0x03:
                   11536:                                if(REG16(CX) >= 4) {
                   11537:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11538:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11539:                                        REG16(AX) = 4; // number of bytes actually read
                   11540:                                } else {
                   11541:                                        REG16(AX) = 0x0d; // data invalid
                   11542:                                        m_CF = 1;
                   11543:                                }
                   11544:                                break;
                   11545:                        default:
                   11546:                                REG16(AX) = 0x01; // function number invalid
                   11547:                                m_CF = 1;
                   11548:                        }
                   11549:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11550:                        if(REG16(CX) >= 5) {
                   11551:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11552:                                REG16(AX) = 5; // number of bytes actually read
                   11553:                        } else {
                   11554:                                REG16(AX) = 0x0d; // data invalid
                   11555:                                m_CF = 1;
                   11556:                        }
                   11557:                } else {
                   11558: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11559: //                     REG16(AX) = REG16(CX);
                   11560:                        REG16(AX) = 0x05; // access denied
                   11561:                        m_CF = 1;
                   11562:                }
                   11563:                break;
1.1.1.48  root     11564:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11565: //             REG16(AX) = 0x05;
                   11566: //             m_CF = 1;
                   11567:                REG16(AX) = 0x00; // success
                   11568:                break;
1.1.1.48  root     11569:        case 0x04: // Read From Block Device Control Channel
                   11570:        case 0x05: // Write To Block Device Control Channel
1.1       root     11571:                REG16(AX) = 0x05;
1.1.1.3   root     11572:                m_CF = 1;
1.1       root     11573:                break;
1.1.1.48  root     11574:        case 0x06: // Get Input Status
1.1.1.20  root     11575:                if(file_mode[file_handler[fd].mode].in) {
                   11576:                        if(file_handler[fd].atty) {
1.1.1.14  root     11577:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11578:                        } else {
1.1.1.20  root     11579:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11580:                        }
1.1.1.14  root     11581:                } else {
                   11582:                        REG8(AL) = 0x00;
1.1       root     11583:                }
                   11584:                break;
1.1.1.48  root     11585:        case 0x07: // Get Output Status
1.1.1.20  root     11586:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11587:                        REG8(AL) = 0xff;
                   11588:                } else {
                   11589:                        REG8(AL) = 0x00;
1.1       root     11590:                }
                   11591:                break;
1.1.1.48  root     11592:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11593:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11594:                        // removable drive
                   11595:                        REG16(AX) = 0x00;
1.1       root     11596:                } else {
1.1.1.14  root     11597:                        // fixed drive
                   11598:                        REG16(AX) = 0x01;
1.1       root     11599:                }
                   11600:                break;
1.1.1.48  root     11601:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11602:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11603:                        // remote drive
                   11604:                        REG16(DX) = 0x1000;
1.1.1.44  root     11605:                } else if(msdos_is_subst_drive(drv)) {
                   11606:                        // subst drive
                   11607:                        REG16(DX) = 0x8000;
1.1       root     11608:                } else {
1.1.1.14  root     11609:                        // local drive
1.1.1.44  root     11610:                        REG16(DX) = 0x0000;
1.1       root     11611:                }
                   11612:                break;
1.1.1.48  root     11613:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11614:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11615:                        REG16(DX) = 0x8000;
                   11616:                } else {
                   11617:                        REG16(DX) = 0x0000;
                   11618:                }
1.1.1.21  root     11619:                break;
1.1.1.48  root     11620:        case 0x0b: // Set Sharing Retry Count
1.1       root     11621:                break;
1.1.1.48  root     11622:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11623:                if(REG8(CL) == 0x45) {
                   11624:                        // set iteration (retry) count
                   11625:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11626:                } else if(REG8(CL) == 0x4a) {
                   11627:                        // select code page
                   11628:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11629:                        msdos_nls_tables_update();
1.1.1.44  root     11630:                } else if(REG8(CL) == 0x4c) {
                   11631:                        // start code-page preparation
                   11632:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11633:                        int count = 1, offset = 0;
                   11634:                        if(active_code_page != 437) {
                   11635:                                ids[count++] = active_code_page;
                   11636:                        }
                   11637:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11638:                                ids[count++] = system_code_page;
                   11639:                        }
                   11640:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11641:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11642:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11643:                        for(int i = 0; i < count; i++) {
                   11644:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11645:                        }
                   11646:                } else if(REG8(CL) == 0x4d) {
                   11647:                        // end code-page preparation
                   11648:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11649:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11650:                } else if(REG8(CL) == 0x5f) {
                   11651:                        // set display information
                   11652:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11653:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11654:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11655:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11656:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11657:                                
                   11658:                                if(cur_width != new_width || cur_height != new_height) {
                   11659:                                        pcbios_set_console_size(new_width, new_height, true);
                   11660:                                }
                   11661:                        }
1.1.1.22  root     11662:                } else if(REG8(CL) == 0x65) {
                   11663:                        // get iteration (retry) count
                   11664:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11665:                } else if(REG8(CL) == 0x6a) {
                   11666:                        // query selected code page
                   11667:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11668:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11669:                        
                   11670:                        CPINFO info;
                   11671:                        GetCPInfo(active_code_page, &info);
                   11672:                        
                   11673:                        if(info.MaxCharSize != 1) {
                   11674:                                for(int i = 0;; i++) {
                   11675:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11676:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11677:                                        
                   11678:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11679:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11680:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11681:                                        
                   11682:                                        if(lo == 0 && hi == 0) {
                   11683:                                                break;
                   11684:                                        }
                   11685:                                }
                   11686:                        }
1.1.1.44  root     11687:                } else if(REG8(CL) == 0x6b) {
                   11688:                        // query prepare list
                   11689:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11690:                        int count = 1, offset = 0;
                   11691:                        if(active_code_page != 437) {
                   11692:                                ids[count++] = active_code_page;
                   11693:                        }
                   11694:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11695:                                ids[count++] = system_code_page;
                   11696:                        }
                   11697:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11698:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11699:                        for(int i = 0; i < count; i++) {
                   11700:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11701:                        }
                   11702:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11703:                        for(int i = 0; i < count; i++) {
                   11704:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11705:                        }
1.1.1.22  root     11706:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11707:                        // get display information
1.1.1.50  root     11708:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11709:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11710:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11711:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11712:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11713:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11714:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11715:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11716:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11717:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11718:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11719:                } else {
                   11720:                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   11721:                        REG16(AX) = 0x01; // invalid function
                   11722:                        m_CF = 1;
                   11723:                }
                   11724:                break;
1.1.1.48  root     11725:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11726:                if(REG8(CL) == 0x40) {
                   11727:                        // set device parameters
1.1.1.48  root     11728: //             } else if(REG8(CL) == 0x41) {
                   11729: //                     // write logical device track
                   11730: //             } else if(REG8(CL) == 0x42) {
                   11731: //                     // format and verify logical device track
1.1.1.22  root     11732:                } else if(REG8(CL) == 0x46) {
                   11733:                        // set volume serial number
1.1.1.48  root     11734:                } else if(REG8(CL) == 0x47) {
                   11735:                        // set access flag
                   11736: //             } else if(REG8(CL) == 0x48) {
                   11737: //                     // set media lock state
                   11738: //             } else if(REG8(CL) == 0x49) {
                   11739: //                     // eject media in drive
1.1.1.22  root     11740:                } else if(REG8(CL) == 0x4a) {
                   11741:                        // lock logical volume
                   11742:                } else if(REG8(CL) == 0x4b) {
                   11743:                        // lock physical volume
                   11744:                } else if(REG8(CL) == 0x60) {
                   11745:                        // get device parameters
1.1.1.42  root     11746:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11747:                        
1.1.1.42  root     11748:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11749:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11750:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11751:                                
                   11752:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11753:                                switch(geo->MediaType) {
                   11754:                                case F5_360_512:
                   11755:                                case F5_320_512:
                   11756:                                case F5_320_1024:
                   11757:                                case F5_180_512:
                   11758:                                case F5_160_512:
                   11759:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11760:                                        break;
                   11761:                                case F5_1Pt2_512:
                   11762:                                case F3_1Pt2_512:
                   11763:                                case F3_1Pt23_1024:
                   11764:                                case F5_1Pt23_1024:
                   11765:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11766:                                        break;
                   11767:                                case F3_720_512:
                   11768:                                case F3_640_512:
                   11769:                                case F5_640_512:
                   11770:                                case F5_720_512:
                   11771:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11772:                                        break;
                   11773:                                case F8_256_128:
                   11774:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11775:                                        break;
                   11776:                                case FixedMedia:
                   11777:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11778:                                        break;
                   11779:                                case F3_1Pt44_512:
                   11780:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11781:                                        break;
                   11782:                                case F3_2Pt88_512:
                   11783:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11784:                                        break;
                   11785:                                default:
                   11786:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11787: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11788:                                        break;
1.1.1.22  root     11789:                                }
1.1.1.42  root     11790:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11791:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11792:                                switch(geo->MediaType) {
                   11793:                                case F5_360_512:
                   11794:                                case F5_320_512:
                   11795:                                case F5_320_1024:
                   11796:                                case F5_180_512:
                   11797:                                case F5_160_512:
                   11798:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11799:                                        break;
                   11800:                                default:
                   11801:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11802:                                        break;
                   11803:                                }
                   11804:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11805:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11806:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11807:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11808:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11809:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11810:                                switch(geo->MediaType) {
                   11811:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11812:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11813:                                        break;
                   11814:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11815:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11816:                                        break;
                   11817:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11818:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11819:                                        break;
                   11820:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11821:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11822:                                        break;
                   11823:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11824:                                case F3_1Pt2_512:
                   11825:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11826:                                case F5_720_512:
                   11827:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11828:                                        break;
                   11829:                                case FixedMedia:        // hard disk
                   11830:                                case RemovableMedia:
                   11831:                                case Unknown:
                   11832:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11833:                                        break;
                   11834:                                default:
                   11835:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11836:                                        break;
                   11837:                                }
                   11838:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11839:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11840:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11841:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11842:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11843:                                // 21h  BYTE    device type
                   11844:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11845:                        } else {
                   11846:                                REG16(AX) = 0x0f; // invalid drive
                   11847:                                m_CF = 1;
                   11848:                        }
1.1.1.48  root     11849: //             } else if(REG8(CL) == 0x61) {
                   11850: //                     // read logical device track
                   11851: //             } else if(REG8(CL) == 0x62) {
                   11852: //                     // verify logical device track
1.1.1.22  root     11853:                } else if(REG8(CL) == 0x66) {
                   11854:                        // get volume serial number
                   11855:                        char path[] = "A:\\";
                   11856:                        char volume_label[MAX_PATH];
                   11857:                        DWORD serial_number = 0;
                   11858:                        char file_system[MAX_PATH];
                   11859:                        
                   11860:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11861:                        
1.1.1.60  root     11862:                        if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1.1.22  root     11863:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11864:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11865:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11866:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11867:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11868:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11869:                        } else {
                   11870:                                REG16(AX) = 0x0f; // invalid drive
                   11871:                                m_CF = 1;
                   11872:                        }
                   11873:                } else if(REG8(CL) == 0x67) {
                   11874:                        // get access flag
                   11875:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11876:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11877:                } else if(REG8(CL) == 0x68) {
                   11878:                        // sense media type
1.1.1.42  root     11879:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11880:                        
1.1.1.42  root     11881:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11882:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11883:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11884:                                
                   11885:                                switch(geo->MediaType) {
                   11886:                                case F3_720_512:
                   11887:                                case F5_720_512:
                   11888:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11889:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11890:                                        break;
                   11891:                                case F3_1Pt44_512:
                   11892:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11893:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11894:                                        break;
                   11895:                                case F3_2Pt88_512:
                   11896:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11897:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11898:                                        break;
                   11899:                                default:
                   11900:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11901:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11902:                                        break;
1.1.1.22  root     11903:                                }
                   11904:                        } else {
                   11905:                                REG16(AX) = 0x0f; // invalid drive
                   11906:                                m_CF = 1;
                   11907:                        }
                   11908:                } else if(REG8(CL) == 0x6a) {
                   11909:                        // unlock logical volume
                   11910:                } else if(REG8(CL) == 0x6b) {
                   11911:                        // unlock physical volume
1.1.1.48  root     11912: //             } else if(REG8(CL) == 0x6c) {
                   11913: //                     // get lock flag
                   11914: //             } else if(REG8(CL) == 0x6d) {
                   11915: //                     // enumerate open files
                   11916: //             } else if(REG8(CL) == 0x6e) {
                   11917: //                     // find swap file
                   11918: //             } else if(REG8(CL) == 0x6f) {
                   11919: //                     // get drive map information
                   11920: //             } else if(REG8(CL) == 0x70) {
                   11921: //                     // get current lock state
                   11922: //             } else if(REG8(CL) == 0x71) {
                   11923: //                     // get first cluster
1.1.1.22  root     11924:                } else {
                   11925:                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   11926:                        REG16(AX) = 0x01; // invalid function
                   11927:                        m_CF = 1;
                   11928:                }
                   11929:                break;
1.1.1.48  root     11930:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11931:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11932:                        REG16(AX) = 0x0f; // invalid drive
                   11933:                        m_CF = 1;
                   11934:                } else {
                   11935:                        REG8(AL) = 0;
1.1.1.22  root     11936:                }
                   11937:                break;
1.1.1.48  root     11938:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11939:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11940:                        REG16(AX) = 0x0f; // invalid drive
                   11941:                        m_CF = 1;
1.1.1.22  root     11942:                }
                   11943:                break;
1.1.1.48  root     11944:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11945:                switch(REG8(CL)) {
                   11946:                case 0x45:
                   11947:                case 0x4a:
1.1.1.48  root     11948:                case 0x4c:
                   11949:                case 0x4d:
1.1.1.22  root     11950:                case 0x65:
                   11951:                case 0x6a:
1.1.1.48  root     11952:                case 0x6b:
1.1.1.22  root     11953:                case 0x7f:
                   11954:                        REG16(AX) = 0x0000; // supported
                   11955:                        break;
                   11956:                default:
                   11957:                        REG8(AL) = 0x01; // ioctl capability not available
                   11958:                        m_CF = 1;
                   11959:                        break;
                   11960:                }
                   11961:                break;
1.1.1.48  root     11962:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11963:                switch(REG8(CL)) {
                   11964:                case 0x40:
                   11965:                case 0x46:
                   11966:                case 0x4a:
                   11967:                case 0x4b:
                   11968:                case 0x60:
                   11969:                case 0x66:
                   11970:                case 0x67:
                   11971:                case 0x68:
                   11972:                case 0x6a:
                   11973:                case 0x6b:
1.1.1.48  root     11974:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   11975:                                // CH = 00h     Unknown
                   11976:                                // CH = 01h     COMn:
                   11977:                                // CH = 03h     CON
                   11978:                                // CH = 05h     LPTn:
                   11979:                                REG16(AX) = 0x0000; // supported
                   11980:                                break;
                   11981:                        }
1.1.1.22  root     11982:                default:
                   11983:                        REG8(AL) = 0x01; // ioctl capability not available
                   11984:                        m_CF = 1;
                   11985:                        break;
                   11986:                }
                   11987:                break;
1.1.1.48  root     11988:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   11989:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   11990:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   11991:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   11992:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   11993:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   11994:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   11995:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   11996:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   11997:        case 0x59: // DR Multiuser DOS 5.0 - API
                   11998:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11999:                m_CF = 1;
                   12000:                break;
1.1       root     12001:        default:
1.1.1.22  root     12002:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     12003:                REG16(AX) = 0x01;
1.1.1.3   root     12004:                m_CF = 1;
1.1       root     12005:                break;
                   12006:        }
                   12007: }
                   12008: 
                   12009: inline void msdos_int_21h_45h()
                   12010: {
                   12011:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12012:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12013:        
1.1.1.20  root     12014:        if(fd < process->max_files && file_handler[fd].valid) {
                   12015:                int dup_fd = _dup(fd);
                   12016:                if(dup_fd != -1) {
                   12017:                        REG16(AX) = dup_fd;
                   12018:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12019: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12020:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12021:                } else {
                   12022:                        REG16(AX) = errno;
1.1.1.3   root     12023:                        m_CF = 1;
1.1       root     12024:                }
                   12025:        } else {
                   12026:                REG16(AX) = 0x06;
1.1.1.3   root     12027:                m_CF = 1;
1.1       root     12028:        }
                   12029: }
                   12030: 
                   12031: inline void msdos_int_21h_46h()
                   12032: {
                   12033:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12034:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12035:        int dup_fd = REG16(CX);
                   12036:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     12037:        
1.1.1.20  root     12038:        if(REG16(BX) == REG16(CX)) {
                   12039:                REG16(AX) = 0x06;
                   12040:                m_CF = 1;
                   12041:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   12042:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   12043:                        _close(tmp_fd);
                   12044:                        msdos_file_handler_close(tmp_fd);
                   12045:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   12046:                }
                   12047:                if(_dup2(fd, dup_fd) != -1) {
                   12048:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12049: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12050:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12051:                } else {
                   12052:                        REG16(AX) = errno;
1.1.1.3   root     12053:                        m_CF = 1;
1.1       root     12054:                }
                   12055:        } else {
                   12056:                REG16(AX) = 0x06;
1.1.1.3   root     12057:                m_CF = 1;
1.1       root     12058:        }
                   12059: }
                   12060: 
                   12061: inline void msdos_int_21h_47h(int lfn)
                   12062: {
                   12063:        char path[MAX_PATH];
                   12064:        
                   12065:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     12066:                if(!lfn) {
                   12067:                        strcpy(path, msdos_short_path(path));
                   12068:                }
1.1       root     12069:                if(path[1] == ':') {
                   12070:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     12071:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     12072:                } else {
1.1.1.45  root     12073:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     12074:                }
                   12075:        } else {
                   12076:                REG16(AX) = errno;
1.1.1.3   root     12077:                m_CF = 1;
1.1       root     12078:        }
                   12079: }
                   12080: 
                   12081: inline void msdos_int_21h_48h()
                   12082: {
1.1.1.19  root     12083:        int seg, umb_linked;
1.1       root     12084:        
1.1.1.8   root     12085:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     12086:                // unlink umb not to allocate memory in umb
                   12087:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   12088:                        msdos_mem_unlink_umb();
                   12089:                }
1.1.1.8   root     12090:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12091:                        REG16(AX) = seg;
                   12092:                } else {
                   12093:                        REG16(AX) = 0x08;
                   12094:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   12095:                        m_CF = 1;
                   12096:                }
1.1.1.19  root     12097:                if(umb_linked != 0) {
                   12098:                        msdos_mem_link_umb();
                   12099:                }
1.1.1.8   root     12100:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   12101:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12102:                        REG16(AX) = seg;
                   12103:                } else {
                   12104:                        REG16(AX) = 0x08;
                   12105:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   12106:                        m_CF = 1;
                   12107:                }
                   12108:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   12109:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12110:                        REG16(AX) = seg;
                   12111:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12112:                        REG16(AX) = seg;
                   12113:                } else {
                   12114:                        REG16(AX) = 0x08;
                   12115:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   12116:                        m_CF = 1;
                   12117:                }
1.1       root     12118:        }
                   12119: }
                   12120: 
                   12121: inline void msdos_int_21h_49h()
                   12122: {
1.1.1.14  root     12123:        int mcb_seg = SREG(ES) - 1;
                   12124:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   12125:        
                   12126:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12127:                msdos_mem_free(SREG(ES));
                   12128:        } else {
1.1.1.33  root     12129:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     12130:                m_CF = 1;
                   12131:        }
1.1       root     12132: }
                   12133: 
                   12134: inline void msdos_int_21h_4ah()
                   12135: {
1.1.1.14  root     12136:        int mcb_seg = SREG(ES) - 1;
                   12137:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     12138:        int max_paragraphs;
                   12139:        
1.1.1.14  root     12140:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12141:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   12142:                        REG16(AX) = 0x08;
                   12143:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   12144:                        m_CF = 1;
                   12145:                }
                   12146:        } else {
1.1.1.33  root     12147:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     12148:                m_CF = 1;
1.1       root     12149:        }
                   12150: }
                   12151: 
                   12152: inline void msdos_int_21h_4bh()
                   12153: {
1.1.1.3   root     12154:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12155:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     12156:        
                   12157:        switch(REG8(AL)) {
                   12158:        case 0x00:
                   12159:        case 0x01:
                   12160:                if(msdos_process_exec(command, param, REG8(AL))) {
                   12161:                        REG16(AX) = 0x02;
1.1.1.3   root     12162:                        m_CF = 1;
1.1       root     12163:                }
                   12164:                break;
1.1.1.14  root     12165:        case 0x03:
                   12166:                {
                   12167:                        int fd;
                   12168:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   12169:                                REG16(AX) = 0x02;
                   12170:                                m_CF = 1;
                   12171:                                break;
                   12172:                        }
                   12173:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   12174:                        _close(fd);
                   12175:                        
                   12176:                        UINT16 *overlay = (UINT16 *)param;
                   12177:                        
                   12178:                        // check exe header
                   12179:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   12180:                        int header_size = 0;
                   12181:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   12182:                                header_size = header->header_size * 16;
                   12183:                                // relocation
                   12184:                                int start_seg = overlay[1];
                   12185:                                for(int i = 0; i < header->relocations; i++) {
                   12186:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   12187:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   12188:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   12189:                                }
                   12190:                        }
                   12191:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   12192:                }
                   12193:                break;
1.1.1.48  root     12194:        case 0x04:
                   12195:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   12196: //     case 0x05:
                   12197: //             // DOS 5+ - Set Execution State
                   12198:        case 0x80:
                   12199:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   12200:        case 0xf0:
                   12201:        case 0xf1:
                   12202:                // DIET v1.10+
1.1.1.43  root     12203:        case 0xfd:
                   12204:        case 0xfe:
                   12205:                // unknown function called in FreeCOM
                   12206:                REG16(AX) = 0x01;
                   12207:                m_CF = 1;
                   12208:                break;
1.1       root     12209:        default:
1.1.1.22  root     12210:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     12211:                REG16(AX) = 0x01;
1.1.1.3   root     12212:                m_CF = 1;
1.1       root     12213:                break;
                   12214:        }
                   12215: }
                   12216: 
                   12217: inline void msdos_int_21h_4ch()
                   12218: {
                   12219:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   12220: }
                   12221: 
                   12222: inline void msdos_int_21h_4dh()
                   12223: {
                   12224:        REG16(AX) = retval;
                   12225: }
                   12226: 
                   12227: inline void msdos_int_21h_4eh()
                   12228: {
                   12229:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12230:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12231:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     12232:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     12233:        WIN32_FIND_DATAA fd;
1.1       root     12234:        
1.1.1.14  root     12235:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12236:        find->find_magic = FIND_MAGIC;
                   12237:        find->dta_index = dtainfo - dtalist;
1.1       root     12238:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12239:        dtainfo->allowable_mask = REG8(CL);
1.1.1.58  root     12240:        // note: SO1 dir command sets 0x3f, but only directories and volue label are found if bit3 is set :-(
                   12241:        if((dtainfo->allowable_mask & 0x3f) == 0x3f) {
                   12242:                dtainfo->allowable_mask &= ~0x08;
                   12243:        }
1.1.1.14  root     12244:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12245:        
1.1.1.14  root     12246:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12247:                dtainfo->allowable_mask &= ~8;
1.1       root     12248:        }
1.1.1.60  root     12249:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     12250:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12251:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12252:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12253:                                FindClose(dtainfo->find_handle);
                   12254:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12255:                                break;
                   12256:                        }
                   12257:                }
                   12258:        }
1.1.1.13  root     12259:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12260:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12261:                msdos_find_file_conv_local_time(&fd);
                   12262:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12263:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12264:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12265:                REG16(AX) = 0;
1.1.1.14  root     12266:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12267:                find->attrib = 8;
                   12268:                find->size = 0;
                   12269:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12270:                dtainfo->allowable_mask &= ~8;
1.1       root     12271:                REG16(AX) = 0;
                   12272:        } else {
                   12273:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12274:                m_CF = 1;
1.1       root     12275:        }
                   12276: }
                   12277: 
                   12278: inline void msdos_int_21h_4fh()
                   12279: {
                   12280:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12281:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12282:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.60  root     12283:        WIN32_FIND_DATAA fd;
1.1       root     12284:        
1.1.1.14  root     12285:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   12286:                REG16(AX) = 0x12;
                   12287:                m_CF = 1;
                   12288:                return;
                   12289:        }
                   12290:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     12291:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     12292:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12293:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12294:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12295:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12296:                                        FindClose(dtainfo->find_handle);
                   12297:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12298:                                        break;
                   12299:                                }
                   12300:                        }
                   12301:                } else {
1.1.1.13  root     12302:                        FindClose(dtainfo->find_handle);
                   12303:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12304:                }
                   12305:        }
1.1.1.13  root     12306:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12307:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12308:                msdos_find_file_conv_local_time(&fd);
                   12309:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12310:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12311:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12312:                REG16(AX) = 0;
1.1.1.14  root     12313:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12314:                find->attrib = 8;
                   12315:                find->size = 0;
                   12316:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12317:                dtainfo->allowable_mask &= ~8;
1.1       root     12318:                REG16(AX) = 0;
                   12319:        } else {
                   12320:                REG16(AX) = 0x12;
1.1.1.3   root     12321:                m_CF = 1;
1.1       root     12322:        }
                   12323: }
                   12324: 
                   12325: inline void msdos_int_21h_50h()
                   12326: {
1.1.1.8   root     12327:        if(current_psp != REG16(BX)) {
                   12328:                process_t *process = msdos_process_info_get(current_psp);
                   12329:                if(process != NULL) {
                   12330:                        process->psp = REG16(BX);
                   12331:                }
                   12332:                current_psp = REG16(BX);
1.1.1.23  root     12333:                msdos_sda_update(current_psp);
1.1.1.8   root     12334:        }
1.1       root     12335: }
                   12336: 
                   12337: inline void msdos_int_21h_51h()
                   12338: {
                   12339:        REG16(BX) = current_psp;
                   12340: }
                   12341: 
                   12342: inline void msdos_int_21h_52h()
                   12343: {
1.1.1.25  root     12344:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12345:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12346:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12347: }
                   12348: 
1.1.1.43  root     12349: inline void msdos_int_21h_53h()
                   12350: {
                   12351:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12352:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12353:        
                   12354:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12355:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12356:        dpb->shift_count = 0;
                   12357:        dpb->reserved_sectors = 0;
                   12358:        dpb->fat_num = bpb->fat_num;
                   12359:        dpb->root_entries = bpb->root_entries;
                   12360:        dpb->first_data_sector = 0;
                   12361:        if(bpb->sectors_per_cluster != 0) {
                   12362:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12363:        } else {
                   12364:                dpb->highest_cluster_num = 0;
                   12365:        }
                   12366:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12367:        dpb->first_dir_sector = 0;
                   12368:        dpb->device_driver_header = 0;
                   12369:        dpb->media_type = bpb->media_type;
                   12370:        dpb->drive_accessed = 0;
                   12371:        dpb->next_dpb_ofs = 0xffff;
                   12372:        dpb->next_dpb_seg = 0xffff;
                   12373:        dpb->first_free_cluster = 0;
                   12374:        dpb->free_clusters = 0xffff;
                   12375: }
                   12376: 
1.1       root     12377: inline void msdos_int_21h_54h()
                   12378: {
                   12379:        process_t *process = msdos_process_info_get(current_psp);
                   12380:        
                   12381:        REG8(AL) = process->verify;
                   12382: }
                   12383: 
                   12384: inline void msdos_int_21h_55h()
                   12385: {
                   12386:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12387:        
                   12388:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12389:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12390:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12391:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12392:        psp->parent_psp = current_psp;
                   12393: }
                   12394: 
                   12395: inline void msdos_int_21h_56h(int lfn)
                   12396: {
                   12397:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12398:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12399:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12400:        
                   12401:        if(rename(src, dst)) {
                   12402:                REG16(AX) = errno;
1.1.1.3   root     12403:                m_CF = 1;
1.1       root     12404:        }
                   12405: }
                   12406: 
                   12407: inline void msdos_int_21h_57h()
                   12408: {
                   12409:        FILETIME time, local;
1.1.1.14  root     12410:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12411:        HANDLE hHandle;
1.1       root     12412:        
1.1.1.21  root     12413:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12414:                REG16(AX) = (UINT16)GetLastError();
                   12415:                m_CF = 1;
                   12416:                return;
                   12417:        }
                   12418:        ctime = atime = mtime = NULL;
                   12419:        
1.1       root     12420:        switch(REG8(AL)) {
                   12421:        case 0x00:
1.1.1.6   root     12422:        case 0x01:
1.1.1.14  root     12423:                mtime = &time;
1.1.1.6   root     12424:                break;
                   12425:        case 0x04:
                   12426:        case 0x05:
1.1.1.14  root     12427:                atime = &time;
1.1       root     12428:                break;
1.1.1.6   root     12429:        case 0x06:
                   12430:        case 0x07:
1.1.1.14  root     12431:                ctime = &time;
                   12432:                break;
                   12433:        default:
1.1.1.22  root     12434:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     12435:                REG16(AX) = 0x01;
                   12436:                m_CF = 1;
                   12437:                return;
                   12438:        }
                   12439:        if(REG8(AL) & 1) {
1.1       root     12440:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12441:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12442:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12443:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12444:                        m_CF = 1;
1.1       root     12445:                }
1.1.1.14  root     12446:        } else {
1.1.1.21  root     12447:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12448:                        // assume a device and use the current time
                   12449:                        GetSystemTimeAsFileTime(&time);
                   12450:                }
                   12451:                FileTimeToLocalFileTime(&time, &local);
                   12452:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12453:        }
                   12454: }
                   12455: 
                   12456: inline void msdos_int_21h_58h()
                   12457: {
                   12458:        switch(REG8(AL)) {
                   12459:        case 0x00:
1.1.1.7   root     12460:                REG16(AX) = malloc_strategy;
                   12461:                break;
                   12462:        case 0x01:
1.1.1.24  root     12463: //             switch(REG16(BX)) {
                   12464:                switch(REG8(BL)) {
1.1.1.7   root     12465:                case 0x0000:
                   12466:                case 0x0001:
                   12467:                case 0x0002:
                   12468:                case 0x0040:
                   12469:                case 0x0041:
                   12470:                case 0x0042:
                   12471:                case 0x0080:
                   12472:                case 0x0081:
                   12473:                case 0x0082:
                   12474:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12475:                        msdos_sda_update(current_psp);
1.1.1.7   root     12476:                        break;
                   12477:                default:
1.1.1.22  root     12478:                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.7   root     12479:                        REG16(AX) = 0x01;
                   12480:                        m_CF = 1;
                   12481:                        break;
                   12482:                }
                   12483:                break;
                   12484:        case 0x02:
1.1.1.19  root     12485:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12486:                break;
                   12487:        case 0x03:
1.1.1.24  root     12488: //             switch(REG16(BX)) {
                   12489:                switch(REG8(BL)) {
1.1.1.7   root     12490:                case 0x0000:
1.1.1.19  root     12491:                        msdos_mem_unlink_umb();
                   12492:                        break;
1.1.1.7   root     12493:                case 0x0001:
1.1.1.19  root     12494:                        msdos_mem_link_umb();
1.1.1.7   root     12495:                        break;
                   12496:                default:
1.1.1.22  root     12497:                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.7   root     12498:                        REG16(AX) = 0x01;
                   12499:                        m_CF = 1;
                   12500:                        break;
                   12501:                }
1.1       root     12502:                break;
                   12503:        default:
1.1.1.22  root     12504:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     12505:                REG16(AX) = 0x01;
1.1.1.3   root     12506:                m_CF = 1;
1.1       root     12507:                break;
                   12508:        }
                   12509: }
                   12510: 
                   12511: inline void msdos_int_21h_59h()
                   12512: {
1.1.1.47  root     12513:        if(REG16(BX) == 0x0000) {
                   12514:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12515:                
                   12516:                REG16(AX) = sda->extended_error_code;
                   12517:                REG8(BH)  = sda->error_class;
                   12518:                REG8(BL)  = sda->suggested_action;
                   12519:                REG8(CH)  = sda->locus_of_last_error;
                   12520:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12521:                if(sda->int21h_5d0ah_called != 0) {
                   12522:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12523:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12524: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12525:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12526: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12527: //                     i386_load_segment_descriptor(DS);
                   12528:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12529:                        i386_load_segment_descriptor(ES);
                   12530:                }
                   12531:                sda->int21h_5d0ah_called = 0;
                   12532: //     } else if(REG16(BX) == 0x0001) {
                   12533: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12534:        } else {
                   12535:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   12536:                REG16(AX) = 0x01;
                   12537:                m_CF = 1;
                   12538:        }
1.1       root     12539: }
                   12540: 
                   12541: inline void msdos_int_21h_5ah()
                   12542: {
1.1.1.3   root     12543:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12544:        int len = strlen(path);
                   12545:        char tmp[MAX_PATH];
                   12546:        
1.1.1.60  root     12547:        if(GetTempFileNameA(path, "TMP", 0, tmp)) {
1.1       root     12548:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12549:                
1.1.1.60  root     12550:                SetFileAttributesA(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12551:                REG16(AX) = fd;
                   12552:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12553:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12554:                
                   12555:                strcpy(path, tmp);
                   12556:                int dx = REG16(DX) + len;
1.1.1.3   root     12557:                int ds = SREG(DS);
1.1       root     12558:                while(dx > 0xffff) {
                   12559:                        dx -= 0x10;
                   12560:                        ds++;
                   12561:                }
                   12562:                REG16(DX) = dx;
1.1.1.3   root     12563:                SREG(DS) = ds;
                   12564:                i386_load_segment_descriptor(DS);
1.1       root     12565:        } else {
                   12566:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12567:                m_CF = 1;
1.1       root     12568:        }
                   12569: }
                   12570: 
                   12571: inline void msdos_int_21h_5bh()
                   12572: {
1.1.1.45  root     12573:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12574:        
1.1.1.45  root     12575: //     if(msdos_is_existing_file(path)) {
                   12576:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12577:                // already exists
                   12578:                REG16(AX) = 0x50;
1.1.1.3   root     12579:                m_CF = 1;
1.1       root     12580:        } else {
                   12581:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12582:                
                   12583:                if(fd != -1) {
1.1.1.60  root     12584:                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12585:                        REG16(AX) = fd;
                   12586:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12587:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12588:                } else {
                   12589:                        REG16(AX) = errno;
1.1.1.3   root     12590:                        m_CF = 1;
1.1       root     12591:                }
                   12592:        }
                   12593: }
                   12594: 
                   12595: inline void msdos_int_21h_5ch()
                   12596: {
                   12597:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12598:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12599:        
1.1.1.20  root     12600:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12601:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12602:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12603:                        UINT32 pos = _tell(fd);
                   12604:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12605:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12606:                                REG16(AX) = errno;
1.1.1.3   root     12607:                                m_CF = 1;
1.1       root     12608:                        }
1.1.1.20  root     12609:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12610:                        
1.1       root     12611:                        // some seconds may be passed in _locking()
1.1.1.35  root     12612:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12613:                } else {
                   12614:                        REG16(AX) = 0x01;
1.1.1.3   root     12615:                        m_CF = 1;
1.1       root     12616:                }
                   12617:        } else {
                   12618:                REG16(AX) = 0x06;
1.1.1.3   root     12619:                m_CF = 1;
1.1       root     12620:        }
                   12621: }
                   12622: 
1.1.1.22  root     12623: inline void msdos_int_21h_5dh()
                   12624: {
                   12625:        switch(REG8(AL)) {
1.1.1.45  root     12626:        case 0x00:
                   12627:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12628:                        // current system
                   12629:                        static bool reenter = false;
                   12630:                        if(!reenter) {
                   12631:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12632:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12633:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12634:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12635:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12636:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12637:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12638:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12639:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12640:                                i386_load_segment_descriptor(DS);
                   12641:                                i386_load_segment_descriptor(ES);
                   12642:                                reenter = true;
                   12643:                                try {
                   12644:                                        msdos_syscall(0x21);
                   12645:                                } catch(...) {
                   12646:                                }
                   12647:                                reenter = false;
                   12648:                        }
                   12649:                } else {
                   12650:                        REG16(AX) = 0x49; //  network software not installed
                   12651:                        m_CF = 1;
                   12652:                }
                   12653:                break;
1.1.1.22  root     12654:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12655:                SREG(DS) = (SDA_TOP >> 4);
                   12656:                i386_load_segment_descriptor(DS);
                   12657:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12658:                REG16(CX) = 0x80;
                   12659:                REG16(DX) = 0x1a;
                   12660:                break;
1.1.1.45  root     12661:        case 0x07: // get redirected printer mode
                   12662:        case 0x08: // set redirected printer mode
                   12663:        case 0x09: // flush redirected printer output
                   12664:                REG16(AX) = 0x49; //  network software not installed
                   12665:                m_CF = 1;
                   12666:                break;
1.1.1.43  root     12667:        case 0x0a: // set extended error information
                   12668:                {
                   12669:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12670:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12671:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12672:                        // XXX: which one is correct ???
                   12673: #if 1
                   12674:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12675:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12676:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12677:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12678:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12679: #else
                   12680:                        // PC DOS 7 Technical Update
                   12681:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12682:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12683:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12684:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12685: #endif
                   12686:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12687: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12688:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12689: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12690:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12691:                }
                   12692:                break;
1.1.1.23  root     12693:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12694:                REG16(AX) = 0x01;
                   12695:                m_CF = 1;
                   12696:                break;
                   12697:        default:
                   12698:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   12699:                REG16(AX) = 0x01;
                   12700:                m_CF = 1;
                   12701:                break;
                   12702:        }
                   12703: }
                   12704: 
1.1.1.42  root     12705: inline void msdos_int_21h_5eh()
                   12706: {
                   12707:        switch(REG8(AL)) {
                   12708:        case 0x00:
                   12709:                {
                   12710:                        char name[256] = {0};
                   12711:                        DWORD dwSize = 256;
                   12712:                        
1.1.1.60  root     12713:                        if(GetComputerNameA(name, &dwSize)) {
1.1.1.42  root     12714:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12715:                                for(int i = 0; i < 15; i++) {
                   12716:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12717:                                }
                   12718:                                dest[15] = '\0';
                   12719:                                REG8(CH) = 0x01; // nonzero valid
                   12720:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12721:                        } else {
                   12722:                                REG16(AX) = 0x01;
                   12723:                                m_CF = 1;
                   12724:                        }
                   12725:                }
                   12726:                break;
                   12727:        default:
1.1.1.45  root     12728: //             unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   12729: //             REG16(AX) = 0x01;
                   12730:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12731:                m_CF = 1;
                   12732:                break;
                   12733:        }
                   12734: }
                   12735: 
1.1.1.30  root     12736: inline void msdos_int_21h_5fh()
                   12737: {
                   12738:        switch(REG8(AL)) {
1.1.1.42  root     12739:        case 0x05:
1.1.1.44  root     12740:                REG16(BP) = 0;
                   12741:                for(int i = 0; i < 26; i++) {
                   12742:                        if(msdos_is_remote_drive(i)) {
                   12743:                                REG16(BP)++;
1.1.1.42  root     12744:                        }
                   12745:                }
1.1.1.30  root     12746:        case 0x02:
1.1.1.44  root     12747:                for(int i = 0, index = 0; i < 26; i++) {
                   12748:                        if(msdos_is_remote_drive(i)) {
                   12749:                                if(index == REG16(BX)) {
                   12750:                                        char volume[] = "A:";
1.1.1.30  root     12751:                                        volume[0] = 'A' + i;
1.1.1.44  root     12752:                                        DWORD dwSize = 128;
                   12753:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
1.1.1.60  root     12754:                                        WNetGetConnectionA(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
1.1.1.44  root     12755:                                        REG8(BH) = 0x00; // valid
                   12756:                                        REG8(BL) = 0x04; // disk drive
                   12757:                                        REG16(CX) = 0x00; // LANtastic
                   12758:                                        return;
1.1.1.30  root     12759:                                }
1.1.1.44  root     12760:                                index++;
1.1.1.30  root     12761:                        }
                   12762:                }
                   12763:                REG16(AX) = 0x12; // no more files
                   12764:                m_CF = 1;
                   12765:                break;
1.1.1.44  root     12766:        case 0x07:
                   12767:                if(msdos_is_valid_drive(REG8(DL))) {
                   12768:                        msdos_cds_update(REG8(DL));
                   12769:                } else {
                   12770:                        REG16(AX) = 0x0f; // invalid drive
                   12771:                        m_CF = 1;
                   12772:                }
                   12773:                break;
                   12774:        case 0x08:
                   12775:                if(msdos_is_valid_drive(REG8(DL))) {
                   12776:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12777:                        cds->drive_attrib = 0x0000;
                   12778:                } else {
                   12779:                        REG16(AX) = 0x0f; // invalid drive
                   12780:                        m_CF = 1;
                   12781:                }
                   12782:                break;
1.1.1.30  root     12783:        default:
1.1.1.45  root     12784: //             unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   12785: //             REG16(AX) = 0x01;
                   12786:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12787:                m_CF = 1;
                   12788:                break;
                   12789:        }
                   12790: }
                   12791: 
1.1       root     12792: inline void msdos_int_21h_60h(int lfn)
                   12793: {
1.1.1.45  root     12794:        char full[MAX_PATH];
                   12795:        const char *path = NULL;
1.1.1.14  root     12796:        
1.1       root     12797:        if(lfn) {
1.1.1.14  root     12798:                char *name;
                   12799:                *full = '\0';
1.1.1.60  root     12800:                GetFullPathNameA((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12801:                switch(REG8(CL)) {
                   12802:                case 1:
1.1.1.60  root     12803:                        GetShortPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12804:                        my_strupr(full);
                   12805:                        break;
                   12806:                case 2:
1.1.1.60  root     12807:                        GetLongPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12808:                        break;
                   12809:                }
                   12810:                path = full;
                   12811:        } else {
                   12812:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12813:        }
                   12814:        if(*path != '\0') {
                   12815:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12816:        } else {
1.1.1.14  root     12817:                REG16(AX) = (UINT16)GetLastError();
                   12818:                m_CF = 1;
1.1       root     12819:        }
                   12820: }
                   12821: 
                   12822: inline void msdos_int_21h_61h()
                   12823: {
                   12824:        REG8(AL) = 0;
                   12825: }
                   12826: 
                   12827: inline void msdos_int_21h_62h()
                   12828: {
                   12829:        REG16(BX) = current_psp;
                   12830: }
                   12831: 
                   12832: inline void msdos_int_21h_63h()
                   12833: {
                   12834:        switch(REG8(AL)) {
                   12835:        case 0x00:
1.1.1.3   root     12836:                SREG(DS) = (DBCS_TABLE >> 4);
                   12837:                i386_load_segment_descriptor(DS);
1.1       root     12838:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12839:                REG8(AL) = 0x00;
                   12840:                break;
1.1.1.22  root     12841:        case 0x01: // set korean input mode
                   12842:        case 0x02: // get korean input mode
                   12843:                REG8(AL) = 0xff; // not supported
                   12844:                break;
1.1       root     12845:        default:
1.1.1.22  root     12846:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     12847:                REG16(AX) = 0x01;
1.1.1.3   root     12848:                m_CF = 1;
1.1       root     12849:                break;
                   12850:        }
                   12851: }
                   12852: 
1.1.1.25  root     12853: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12854: {
1.1.1.25  root     12855:        switch(func) {
1.1.1.17  root     12856:        case 0x01:
                   12857:                if(REG16(CX) >= 5) {
1.1.1.19  root     12858:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12859:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12860:                                REG16(CX) = sizeof(data);
                   12861:                        ZeroMemory(data, sizeof(data));
                   12862:                        data[0] = 0x01;
                   12863:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12864:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12865:                        *(UINT16 *)(data + 5) = active_code_page;
                   12866:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12867: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12868:                } else {
1.1.1.25  root     12869:                        return(0x08); // insufficient memory
1.1.1.17  root     12870:                }
                   12871:                break;
                   12872:        case 0x02:
                   12873:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12874:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12875:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12876: //             REG16(AX) = active_code_page;
1.1.1.17  root     12877:                REG16(CX) = 0x05;
                   12878:                break;
1.1.1.23  root     12879:        case 0x03:
                   12880:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12881:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12882:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12883: //             REG16(AX) = active_code_page;
1.1.1.23  root     12884:                REG16(CX) = 0x05;
                   12885:                break;
1.1.1.17  root     12886:        case 0x04:
                   12887:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12888:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12889:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12890: //             REG16(AX) = active_code_page;
1.1.1.17  root     12891:                REG16(CX) = 0x05;
                   12892:                break;
                   12893:        case 0x05:
                   12894:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12895:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12896:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12897: //             REG16(AX) = active_code_page;
1.1.1.17  root     12898:                REG16(CX) = 0x05;
                   12899:                break;
                   12900:        case 0x06:
                   12901:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12902:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12903:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12904: //             REG16(AX) = active_code_page;
1.1.1.17  root     12905:                REG16(CX) = 0x05;
                   12906:                break;
1.1       root     12907:        case 0x07:
1.1.1.3   root     12908:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12909:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12910:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12911: //             REG16(AX) = active_code_page;
1.1       root     12912:                REG16(CX) = 0x05;
                   12913:                break;
1.1.1.25  root     12914:        default:
                   12915:                return(0x01); // function number invalid
                   12916:        }
                   12917:        return(0x00);
                   12918: }
                   12919: 
                   12920: inline void msdos_int_21h_65h()
                   12921: {
                   12922:        char tmp[0x10000];
                   12923:        
                   12924:        switch(REG8(AL)) {
1.1.1.43  root     12925:        case 0x00:
                   12926:                if(REG16(CX) >= 7) {
                   12927:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12928:                        REG16(AX) = system_code_page;
                   12929:                } else {
                   12930:                        REG16(AX) = 0x0c;
                   12931:                        m_CF = 1;
                   12932:                }
                   12933:                break;
1.1.1.25  root     12934:        case 0x01:
                   12935:        case 0x02:
                   12936:        case 0x03:
                   12937:        case 0x04:
                   12938:        case 0x05:
                   12939:        case 0x06:
                   12940:        case 0x07:
                   12941:                {
                   12942:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12943:                        if(result) {
                   12944:                                REG16(AX) = result;
                   12945:                                m_CF = 1;
                   12946:                        } else {
                   12947:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12948:                        }
                   12949:                }
                   12950:                break;
1.1       root     12951:        case 0x20:
1.1.1.25  root     12952:        case 0xa0:
1.1.1.19  root     12953:                memset(tmp, 0, sizeof(tmp));
                   12954:                tmp[0] = REG8(DL);
1.1       root     12955:                my_strupr(tmp);
                   12956:                REG8(DL) = tmp[0];
                   12957:                break;
                   12958:        case 0x21:
1.1.1.25  root     12959:        case 0xa1:
1.1       root     12960:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12961:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12962:                my_strupr(tmp);
1.1.1.3   root     12963:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12964:                break;
                   12965:        case 0x22:
1.1.1.25  root     12966:        case 0xa2:
1.1.1.3   root     12967:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12968:                break;
1.1.1.25  root     12969:        case 0x23:
                   12970:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12971:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12972:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12973:                        REG16(AX) = 0x00;
                   12974:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12975:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12976:                        REG16(AX) = 0x01;
                   12977:                } else {
                   12978:                        REG16(AX) = 0x02;
                   12979:                }
                   12980:                break;
1.1       root     12981:        default:
1.1.1.22  root     12982:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     12983:                REG16(AX) = 0x01;
1.1.1.3   root     12984:                m_CF = 1;
1.1       root     12985:                break;
                   12986:        }
                   12987: }
                   12988: 
                   12989: inline void msdos_int_21h_66h()
                   12990: {
                   12991:        switch(REG8(AL)) {
                   12992:        case 0x01:
                   12993:                REG16(BX) = active_code_page;
                   12994:                REG16(DX) = system_code_page;
                   12995:                break;
                   12996:        case 0x02:
                   12997:                if(active_code_page == REG16(BX)) {
                   12998:                        REG16(AX) = 0xeb41;
                   12999:                } else if(_setmbcp(REG16(BX)) == 0) {
                   13000:                        active_code_page = REG16(BX);
1.1.1.17  root     13001:                        msdos_nls_tables_update();
1.1       root     13002:                        REG16(AX) = 0xeb41;
1.1.1.32  root     13003:                        SetConsoleCP(active_code_page);
                   13004:                        SetConsoleOutputCP(active_code_page);
1.1       root     13005:                } else {
                   13006:                        REG16(AX) = 0x25;
1.1.1.3   root     13007:                        m_CF = 1;
1.1       root     13008:                }
                   13009:                break;
                   13010:        default:
1.1.1.22  root     13011:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     13012:                REG16(AX) = 0x01;
1.1.1.3   root     13013:                m_CF = 1;
1.1       root     13014:                break;
                   13015:        }
                   13016: }
                   13017: 
                   13018: inline void msdos_int_21h_67h()
                   13019: {
                   13020:        process_t *process = msdos_process_info_get(current_psp);
                   13021:        
                   13022:        if(REG16(BX) <= MAX_FILES) {
                   13023:                process->max_files = max(REG16(BX), 20);
                   13024:        } else {
                   13025:                REG16(AX) = 0x08;
1.1.1.3   root     13026:                m_CF = 1;
1.1       root     13027:        }
                   13028: }
                   13029: 
                   13030: inline void msdos_int_21h_68h()
                   13031: {
                   13032:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13033:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     13034:        
1.1.1.20  root     13035:        if(fd < process->max_files && file_handler[fd].valid) {
                   13036:                // fflush(_fdopen(fd, ""));
1.1       root     13037:        } else {
                   13038:                REG16(AX) = 0x06;
1.1.1.3   root     13039:                m_CF = 1;
1.1       root     13040:        }
                   13041: }
                   13042: 
                   13043: inline void msdos_int_21h_69h()
                   13044: {
1.1.1.3   root     13045:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13046:        char path[] = "A:\\";
                   13047:        char volume_label[MAX_PATH];
                   13048:        DWORD serial_number = 0;
                   13049:        char file_system[MAX_PATH];
                   13050:        
                   13051:        if(REG8(BL) == 0) {
                   13052:                path[0] = 'A' + _getdrive() - 1;
                   13053:        } else {
                   13054:                path[0] = 'A' + REG8(BL) - 1;
                   13055:        }
                   13056:        
                   13057:        switch(REG8(AL)) {
                   13058:        case 0x00:
1.1.1.60  root     13059:                if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1       root     13060:                        info->info_level = 0;
                   13061:                        info->serial_number = serial_number;
                   13062:                        memset(info->volume_label, 0x20, 11);
                   13063:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   13064:                        memset(info->file_system, 0x20, 8);
                   13065:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   13066:                } else {
                   13067:                        REG16(AX) = errno;
1.1.1.3   root     13068:                        m_CF = 1;
1.1       root     13069:                }
                   13070:                break;
                   13071:        case 0x01:
                   13072:                REG16(AX) = 0x03;
1.1.1.3   root     13073:                m_CF = 1;
1.1.1.45  root     13074:                break;
                   13075:        default:
                   13076:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   13077:                REG16(AX) = 0x01;
                   13078:                m_CF = 1;
                   13079:                break;
1.1       root     13080:        }
                   13081: }
                   13082: 
                   13083: inline void msdos_int_21h_6ah()
                   13084: {
                   13085:        REG8(AH) = 0x68;
                   13086:        msdos_int_21h_68h();
                   13087: }
                   13088: 
                   13089: inline void msdos_int_21h_6bh()
                   13090: {
1.1.1.45  root     13091:        REG8(AL) = 0x00;
1.1       root     13092: }
                   13093: 
                   13094: inline void msdos_int_21h_6ch(int lfn)
                   13095: {
1.1.1.45  root     13096:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     13097:        int mode = REG8(BL) & 0x03;
                   13098:        
                   13099:        if(mode < 0x03) {
1.1.1.29  root     13100:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     13101:                        // file exists
                   13102:                        if(REG8(DL) & 1) {
1.1.1.37  root     13103:                                int fd = -1;
                   13104:                                int sio_port = 0;
                   13105:                                int lpt_port = 0;
1.1       root     13106:                                
1.1.1.45  root     13107:                                if(msdos_is_device_path(path)) {
                   13108:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     13109:                                } else {
1.1.1.13  root     13110:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     13111:                                }
1.1       root     13112:                                if(fd != -1) {
                   13113:                                        REG16(AX) = fd;
                   13114:                                        REG16(CX) = 1;
1.1.1.45  root     13115:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13116:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13117:                                } else {
                   13118:                                        REG16(AX) = errno;
1.1.1.3   root     13119:                                        m_CF = 1;
1.1       root     13120:                                }
                   13121:                        } else if(REG8(DL) & 2) {
1.1.1.60  root     13122:                                int attr = GetFileAttributesA(path);
1.1.1.37  root     13123:                                int fd = -1;
                   13124:                                int sio_port = 0;
                   13125:                                int lpt_port = 0;
1.1       root     13126:                                
1.1.1.45  root     13127:                                if(msdos_is_device_path(path)) {
                   13128:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     13129:                                } else {
                   13130:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13131:                                }
                   13132:                                if(fd != -1) {
                   13133:                                        if(attr == -1) {
                   13134:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   13135:                                        }
1.1.1.60  root     13136:                                        SetFileAttributesA(path, attr);
1.1       root     13137:                                        REG16(AX) = fd;
                   13138:                                        REG16(CX) = 3;
1.1.1.45  root     13139:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13140:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13141:                                } else {
                   13142:                                        REG16(AX) = errno;
1.1.1.3   root     13143:                                        m_CF = 1;
1.1       root     13144:                                }
                   13145:                        } else {
                   13146:                                REG16(AX) = 0x50;
1.1.1.3   root     13147:                                m_CF = 1;
1.1       root     13148:                        }
                   13149:                } else {
                   13150:                        // file not exists
                   13151:                        if(REG8(DL) & 0x10) {
                   13152:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13153:                                
                   13154:                                if(fd != -1) {
1.1.1.60  root     13155:                                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     13156:                                        REG16(AX) = fd;
                   13157:                                        REG16(CX) = 2;
                   13158:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     13159:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13160:                                } else {
                   13161:                                        REG16(AX) = errno;
1.1.1.3   root     13162:                                        m_CF = 1;
1.1       root     13163:                                }
                   13164:                        } else {
                   13165:                                REG16(AX) = 0x02;
1.1.1.3   root     13166:                                m_CF = 1;
1.1       root     13167:                        }
                   13168:                }
                   13169:        } else {
                   13170:                REG16(AX) = 0x0c;
1.1.1.3   root     13171:                m_CF = 1;
1.1       root     13172:        }
                   13173: }
                   13174: 
1.1.1.43  root     13175: inline void msdos_int_21h_70h()
                   13176: {
                   13177:        switch(REG8(AL)) {
1.1.1.48  root     13178:        case 0x00: // get ??? info
                   13179:        case 0x01: // set above info
                   13180: //             unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   13181:                REG16(AX) = 0x7000;
                   13182:                m_CF = 1;
                   13183:                break;
                   13184:        case 0x02: // set general internationalization info
1.1.1.43  root     13185:                if(REG16(CX) >= 7) {
                   13186:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   13187:                        msdos_nls_tables_update();
                   13188:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   13189:                        REG16(AX) = system_code_page;
                   13190:                } else {
                   13191:                        REG16(AX) = 0x0c;
                   13192:                        m_CF = 1;
                   13193:                }
                   13194:                break;
                   13195:        default:
                   13196:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     13197:                REG16(AX) = 0x7000;
1.1.1.43  root     13198:                m_CF = 1;
                   13199:                break;
                   13200:        }
                   13201: }
                   13202: 
1.1       root     13203: inline void msdos_int_21h_710dh()
                   13204: {
                   13205:        // reset drive
                   13206: }
                   13207: 
1.1.1.48  root     13208: inline void msdos_int_21h_7141h()
1.1.1.17  root     13209: {
                   13210:        if(REG16(SI) == 0) {
1.1.1.48  root     13211:                msdos_int_21h_41h(1);
1.1.1.17  root     13212:                return;
                   13213:        }
                   13214:        if(REG16(SI) != 1) {
                   13215:                REG16(AX) = 5;
                   13216:                m_CF = 1;
                   13217:        }
                   13218:        /* wild card and matching attributes... */
                   13219:        char tmp[MAX_PATH * 2];
                   13220:        // copy search pathname (and quick check overrun)
                   13221:        ZeroMemory(tmp, sizeof(tmp));
                   13222:        tmp[MAX_PATH - 1] = '\0';
                   13223:        tmp[MAX_PATH] = 1;
                   13224:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   13225:        
                   13226:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   13227:                REG16(AX) = 1;
                   13228:                m_CF = 1;
                   13229:                return;
                   13230:        }
                   13231:        for(char *s = tmp; *s; ++s) {
                   13232:                if(*s == '/') {
                   13233:                        *s = '\\';
                   13234:                }
                   13235:        }
1.1.1.60  root     13236:        char *tmp_name = my_strrchr(tmp, '\\');
1.1.1.17  root     13237:        if(tmp_name) {
                   13238:                ++tmp_name;
                   13239:        } else {
                   13240:                tmp_name = strchr(tmp, ':');
                   13241:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   13242:        }
                   13243:        
                   13244:        WIN32_FIND_DATAA fd;
                   13245:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   13246:        if(fh == INVALID_HANDLE_VALUE) {
                   13247:                REG16(AX) = 2;
                   13248:                m_CF = 1;
                   13249:                return;
                   13250:        }
                   13251:        do {
                   13252:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   13253:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     13254:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     13255:                                REG16(AX) = 5;
                   13256:                                m_CF = 1;
                   13257:                                break;
                   13258:                        }
                   13259:                }
                   13260:        } while(FindNextFileA(fh, &fd));
                   13261:        if(!m_CF) {
                   13262:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   13263:                        m_CF = 1;
                   13264:                        REG16(AX) = 2;
                   13265:                }
                   13266:        }
                   13267:        FindClose(fh);
                   13268: }
                   13269: 
1.1       root     13270: inline void msdos_int_21h_714eh()
                   13271: {
                   13272:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13273:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   13274:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.60  root     13275:        WIN32_FIND_DATAA fd;
1.1       root     13276:        
1.1.1.13  root     13277:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   13278:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13279:                FindClose(dtainfo->find_handle);
                   13280:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13281:        }
                   13282:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     13283:        dtainfo->allowable_mask = REG8(CL);
                   13284:        dtainfo->required_mask = REG8(CH);
                   13285:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     13286:        
1.1.1.14  root     13287:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   13288:                dtainfo->allowable_mask &= ~8;
1.1       root     13289:        }
1.1.1.60  root     13290:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     13291:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13292:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13293:                                FindClose(dtainfo->find_handle);
                   13294:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13295:                                break;
                   13296:                        }
                   13297:                }
                   13298:        }
1.1.1.13  root     13299:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13300:                find->attrib = fd.dwFileAttributes;
                   13301:                msdos_find_file_conv_local_time(&fd);
                   13302:                if(REG16(SI) == 0) {
                   13303:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13304:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13305:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13306:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13307:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13308:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13309:                } else {
                   13310:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13311:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13312:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13313:                }
                   13314:                find->size_hi = fd.nFileSizeHigh;
                   13315:                find->size_lo = fd.nFileSizeLow;
                   13316:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13317:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13318:                REG16(AX) = dtainfo - dtalist + 1;
                   13319:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13320:                // volume label
                   13321:                find->attrib = 8;
                   13322:                find->size_hi = find->size_lo = 0;
                   13323:                strcpy(find->full_name, process->volume_label);
                   13324:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13325:                dtainfo->allowable_mask &= ~8;
                   13326:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     13327:        } else {
                   13328:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     13329:                m_CF = 1;
1.1       root     13330:        }
                   13331: }
                   13332: 
                   13333: inline void msdos_int_21h_714fh()
                   13334: {
                   13335:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13336:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1.1.60  root     13337:        WIN32_FIND_DATAA fd;
1.1       root     13338:        
1.1.1.14  root     13339:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13340:                REG16(AX) = 6;
1.1.1.13  root     13341:                m_CF = 1;
                   13342:                return;
                   13343:        }
1.1.1.14  root     13344:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13345:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     13346:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13347:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13348:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13349:                                        FindClose(dtainfo->find_handle);
                   13350:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13351:                                        break;
                   13352:                                }
                   13353:                        }
                   13354:                } else {
1.1.1.13  root     13355:                        FindClose(dtainfo->find_handle);
                   13356:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13357:                }
                   13358:        }
1.1.1.13  root     13359:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13360:                find->attrib = fd.dwFileAttributes;
                   13361:                msdos_find_file_conv_local_time(&fd);
                   13362:                if(REG16(SI) == 0) {
                   13363:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13364:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13365:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13366:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13367:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13368:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13369:                } else {
                   13370:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13371:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13372:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13373:                }
                   13374:                find->size_hi = fd.nFileSizeHigh;
                   13375:                find->size_lo = fd.nFileSizeLow;
                   13376:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13377:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13378:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13379:                // volume label
                   13380:                find->attrib = 8;
                   13381:                find->size_hi = find->size_lo = 0;
                   13382:                strcpy(find->full_name, process->volume_label);
                   13383:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13384:                dtainfo->allowable_mask &= ~8;
1.1       root     13385:        } else {
                   13386:                REG16(AX) = 0x12;
1.1.1.3   root     13387:                m_CF = 1;
1.1       root     13388:        }
                   13389: }
                   13390: 
                   13391: inline void msdos_int_21h_71a0h()
                   13392: {
                   13393:        DWORD max_component_len, file_sys_flag;
                   13394:        
1.1.1.60  root     13395:        if(GetVolumeInformationA((char *)(mem + SREG_BASE(DS) + REG16(DX)), NULL, 0, NULL, &max_component_len, &file_sys_flag, REG16(CX) == 0 ? NULL : (char *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX))) {
1.1.1.14  root     13396:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13397:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13398:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13399:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13400:        } else {
                   13401:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13402:                m_CF = 1;
1.1       root     13403:        }
                   13404: }
                   13405: 
                   13406: inline void msdos_int_21h_71a1h()
                   13407: {
1.1.1.14  root     13408:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13409:                REG16(AX) = 6;
1.1.1.13  root     13410:                m_CF = 1;
                   13411:                return;
                   13412:        }
1.1.1.14  root     13413:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13414:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13415:                FindClose(dtainfo->find_handle);
                   13416:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13417:        }
                   13418: }
                   13419: 
                   13420: inline void msdos_int_21h_71a6h()
                   13421: {
                   13422:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13423:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13424:        
1.1.1.3   root     13425:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13426:        struct _stat64 status;
                   13427:        DWORD serial_number = 0;
                   13428:        
1.1.1.20  root     13429:        if(fd < process->max_files && file_handler[fd].valid) {
                   13430:                if(_fstat64(fd, &status) == 0) {
                   13431:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13432:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13433:                                char volume[] = "A:\\";
1.1.1.20  root     13434:                                volume[0] = file_handler[fd].path[1];
1.1.1.60  root     13435:                                GetVolumeInformationA(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
1.1       root     13436:                        }
1.1.1.60  root     13437:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributesA(file_handler[fd].path);
1.1       root     13438:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13439:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13440:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13441:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13442:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13443:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13444:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13445:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13446:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13447:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13448:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13449:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13450:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13451:                } else {
                   13452:                        REG16(AX) = errno;
1.1.1.3   root     13453:                        m_CF = 1;
1.1       root     13454:                }
                   13455:        } else {
                   13456:                REG16(AX) = 0x06;
1.1.1.3   root     13457:                m_CF = 1;
1.1       root     13458:        }
                   13459: }
                   13460: 
                   13461: inline void msdos_int_21h_71a7h()
                   13462: {
                   13463:        switch(REG8(BL)) {
                   13464:        case 0x00:
1.1.1.3   root     13465:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13466:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13467:                        m_CF = 1;
1.1       root     13468:                }
                   13469:                break;
                   13470:        case 0x01:
                   13471:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13472:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13473:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13474:                        m_CF = 1;
1.1       root     13475:                }
                   13476:                break;
                   13477:        default:
1.1.1.22  root     13478:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     13479:                REG16(AX) = 0x7100;
1.1.1.3   root     13480:                m_CF = 1;
1.1       root     13481:                break;
                   13482:        }
                   13483: }
                   13484: 
                   13485: inline void msdos_int_21h_71a8h()
                   13486: {
                   13487:        if(REG8(DH) == 0) {
                   13488:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13489:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13490:                memset(fcb, 0x20, sizeof(fcb));
                   13491:                int len = strlen(tmp);
1.1.1.21  root     13492:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13493:                        if(tmp[i] == '.') {
                   13494:                                pos = 8;
                   13495:                        } else {
                   13496:                                if(msdos_lead_byte_check(tmp[i])) {
                   13497:                                        fcb[pos++] = tmp[i++];
                   13498:                                }
                   13499:                                fcb[pos++] = tmp[i];
                   13500:                        }
                   13501:                }
1.1.1.3   root     13502:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13503:        } else {
1.1.1.3   root     13504:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13505:        }
                   13506: }
                   13507: 
1.1.1.22  root     13508: inline void msdos_int_21h_71aah()
                   13509: {
                   13510:        char drv[] = "A:", path[MAX_PATH];
                   13511:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13512:        
                   13513:        if(REG8(BL) == 0) {
                   13514:                drv[0] = 'A' + _getdrive() - 1;
                   13515:        } else {
                   13516:                drv[0] = 'A' + REG8(BL) - 1;
                   13517:        }
                   13518:        switch(REG8(BH)) {
                   13519:        case 0x00:
1.1.1.44  root     13520:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13521:                        REG16(AX) = 0x0f; // invalid drive
                   13522:                        m_CF = 1;
1.1.1.60  root     13523:                } else if(DefineDosDeviceA(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
1.1.1.44  root     13524:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13525:                        m_CF = 1;
                   13526:                }
                   13527:                break;
                   13528:        case 0x01:
1.1.1.44  root     13529:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13530:                        REG16(AX) = 0x0f; // invalid drive
                   13531:                        m_CF = 1;
1.1.1.60  root     13532:                } else if(DefineDosDeviceA(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13533:                        REG16(AX) = 0x0f; // invalid drive
                   13534:                        m_CF = 1;
                   13535:                }
                   13536:                break;
                   13537:        case 0x02:
1.1.1.44  root     13538:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13539:                        REG16(AX) = 0x0f; // invalid drive
                   13540:                        m_CF = 1;
1.1.1.60  root     13541:                } else if(QueryDosDeviceA(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13542:                        REG16(AX) = 0x0f; // invalid drive
                   13543:                        m_CF = 1;
                   13544:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13545:                        REG16(AX) = 0x0f; // invalid drive
                   13546:                        m_CF = 1;
                   13547:                } else {
                   13548:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13549:                }
                   13550:                break;
                   13551:        default:
                   13552:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     13553:                REG16(AX) = 0x7100;
1.1.1.22  root     13554:                m_CF = 1;
                   13555:                break;
                   13556:        }
                   13557: }
                   13558: 
1.1.1.14  root     13559: inline void msdos_int_21h_7300h()
                   13560: {
1.1.1.44  root     13561:        REG8(AL) = REG8(CL);
                   13562:        REG8(AH) = 0;
1.1.1.14  root     13563: }
                   13564: 
                   13565: inline void msdos_int_21h_7302h()
                   13566: {
                   13567:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13568:        UINT16 seg, ofs;
                   13569:        
                   13570:        if(REG16(CX) < 0x3f) {
                   13571:                REG8(AL) = 0x18;
                   13572:                m_CF = 1;
                   13573:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13574:                REG8(AL) = 0xff;
                   13575:                m_CF = 1;
                   13576:        } else {
                   13577:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13578:        }
                   13579: }
                   13580: 
1.1       root     13581: inline void msdos_int_21h_7303h()
                   13582: {
1.1.1.3   root     13583:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13584:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13585:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13586:        
1.1.1.60  root     13587:        if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1       root     13588:                info->size_of_structure = sizeof(ext_space_info_t);
                   13589:                info->structure_version = 0;
                   13590:                info->sectors_per_cluster = sectors_per_cluster;
                   13591:                info->bytes_per_sector = bytes_per_sector;
                   13592:                info->available_clusters_on_drive = free_clusters;
                   13593:                info->total_clusters_on_drive = total_clusters;
                   13594:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13595:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13596:                info->available_allocation_units = free_clusters;       // ???
                   13597:                info->total_allocation_units = total_clusters;          // ???
                   13598:        } else {
                   13599:                REG16(AX) = errno;
1.1.1.3   root     13600:                m_CF = 1;
1.1       root     13601:        }
                   13602: }
                   13603: 
1.1.1.30  root     13604: inline void msdos_int_21h_dbh()
                   13605: {
                   13606:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13607:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13608:        REG8(AL) = dos_info->last_drive;
                   13609: }
                   13610: 
                   13611: inline void msdos_int_21h_dch()
                   13612: {
                   13613:        // Novell NetWare - Connection Services - Get Connection Number
                   13614:        REG8(AL) = 0x00;
                   13615: }
                   13616: 
1.1.1.32  root     13617: inline void msdos_int_24h()
                   13618: {
                   13619:        const char *message = NULL;
                   13620:        int key = 0;
                   13621:        
                   13622:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13623:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13624:                        if(active_code_page == 932) {
                   13625:                                message = critical_error_table[i].message_japanese;
                   13626:                        }
                   13627:                        if(message == NULL) {
                   13628:                                message = critical_error_table[i].message_english;
                   13629:                        }
                   13630:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13631:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13632:                        
                   13633:                        SREG(ES) = WORK_TOP >> 4;
                   13634:                        i386_load_segment_descriptor(ES);
                   13635:                        REG16(DI) = 0x0000;
                   13636:                        break;
                   13637:                }
                   13638:        }
                   13639:        fprintf(stderr, "\n%s", message);
                   13640:        if(!(REG8(AH) & 0x80)) {
                   13641:                if(REG8(AH) & 0x01) {
                   13642:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13643:                } else {
                   13644:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13645:                }
                   13646:        }
                   13647:        fprintf(stderr, "\n");
                   13648:        
1.1.1.33  root     13649:        {
1.1.1.32  root     13650:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13651:        }
1.1.1.32  root     13652:        if(REG8(AH) & 0x10) {
                   13653:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13654:        }
                   13655:        if(REG8(AH) & 0x20) {
                   13656:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13657:        }
                   13658:        if(REG8(AH) & 0x08) {
                   13659:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13660:        }
                   13661:        fprintf(stderr, "? ");
                   13662:        
                   13663:        while(1) {
                   13664:                while(!_kbhit()) {
                   13665:                        Sleep(10);
                   13666:                }
                   13667:                key = _getch();
                   13668:                
                   13669:                if(key == 'I' || key == 'i') {
                   13670:                        if(REG8(AH) & 0x20) {
                   13671:                                REG8(AL) = 0;
                   13672:                                break;
                   13673:                        }
                   13674:                } else if(key == 'R' || key == 'r') {
                   13675:                        if(REG8(AH) & 0x10) {
                   13676:                                REG8(AL) = 1;
                   13677:                                break;
                   13678:                        }
                   13679:                } else if(key == 'A' || key == 'a') {
                   13680:                        REG8(AL) = 2;
                   13681:                        break;
                   13682:                } else if(key == 'F' || key == 'f') {
                   13683:                        if(REG8(AH) & 0x08) {
                   13684:                                REG8(AL) = 3;
                   13685:                                break;
                   13686:                        }
                   13687:                }
                   13688:        }
                   13689:        fprintf(stderr, "%c\n", key);
                   13690: }
                   13691: 
1.1       root     13692: inline void msdos_int_25h()
                   13693: {
                   13694:        UINT16 seg, ofs;
                   13695:        DWORD dwSize;
                   13696:        
1.1.1.3   root     13697: #if defined(HAS_I386)
                   13698:        I386OP(pushf)();
                   13699: #else
                   13700:        PREFIX86(_pushf());
                   13701: #endif
1.1       root     13702:        
                   13703:        if(!(REG8(AL) < 26)) {
                   13704:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13705:                m_CF = 1;
1.1       root     13706:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13707:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13708:                m_CF = 1;
1.1       root     13709:        } else {
                   13710:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13711:                char dev[64];
                   13712:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13713:                
1.1.1.60  root     13714:                HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1       root     13715:                if(hFile == INVALID_HANDLE_VALUE) {
                   13716:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13717:                        m_CF = 1;
1.1       root     13718:                } else {
1.1.1.19  root     13719:                        UINT32 top_sector  = REG16(DX);
                   13720:                        UINT16 sector_num  = REG16(CX);
                   13721:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13722:                        
                   13723:                        if(sector_num == 0xffff) {
                   13724:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13725:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13726:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13727:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13728:                                buffer_addr = (seg << 4) + ofs;
                   13729:                        }
                   13730: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13731: //                             REG8(AL) = 0x02; // drive not ready
                   13732: //                             m_CF = 1;
                   13733: //                     } else 
                   13734:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13735:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13736:                                m_CF = 1;
1.1.1.19  root     13737:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13738:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13739:                                m_CF = 1;
1.1       root     13740:                        }
                   13741:                        CloseHandle(hFile);
                   13742:                }
                   13743:        }
                   13744: }
                   13745: 
                   13746: inline void msdos_int_26h()
                   13747: {
1.1.1.42  root     13748:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13749:        UINT16 seg, ofs;
                   13750:        DWORD dwSize;
                   13751:        
1.1.1.3   root     13752: #if defined(HAS_I386)
                   13753:        I386OP(pushf)();
                   13754: #else
                   13755:        PREFIX86(_pushf());
                   13756: #endif
1.1       root     13757:        
                   13758:        if(!(REG8(AL) < 26)) {
                   13759:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13760:                m_CF = 1;
1.1       root     13761:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13762:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13763:                m_CF = 1;
1.1       root     13764:        } else {
                   13765:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13766:                char dev[64];
                   13767:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13768:                
                   13769:                if(dpb->media_type == 0xf8) {
                   13770:                        // this drive is not a floppy
1.1.1.6   root     13771: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13772: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13773: //                     }
1.1       root     13774:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13775:                        m_CF = 1;
1.1       root     13776:                } else {
1.1.1.60  root     13777:                        HANDLE hFile = CreateFileA(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1       root     13778:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13779:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13780:                                m_CF = 1;
1.1       root     13781:                        } else {
1.1.1.19  root     13782:                                UINT32 top_sector  = REG16(DX);
                   13783:                                UINT16 sector_num  = REG16(CX);
                   13784:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13785:                                
                   13786:                                if(sector_num == 0xffff) {
                   13787:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13788:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13789:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13790:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13791:                                        buffer_addr = (seg << 4) + ofs;
                   13792:                                }
1.1       root     13793:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13794:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13795:                                        m_CF = 1;
1.1.1.19  root     13796:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13797:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13798:                                        m_CF = 1;
1.1.1.19  root     13799:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13800:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13801:                                        m_CF = 1;
1.1       root     13802:                                }
                   13803:                                CloseHandle(hFile);
                   13804:                        }
                   13805:                }
                   13806:        }
                   13807: }
                   13808: 
                   13809: inline void msdos_int_27h()
                   13810: {
1.1.1.29  root     13811:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13812:        try {
                   13813:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13814:        } catch(...) {
                   13815:                // recover the broken mcb
                   13816:                int mcb_seg = SREG(CS) - 1;
                   13817:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13818:                
1.1.1.29  root     13819:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13820:                        mcb->mz = 'M';
                   13821:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13822:                        
1.1.1.29  root     13823:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13824:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13825:                        } else {
1.1.1.39  root     13826:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13827:                        }
                   13828:                } else {
                   13829:                        mcb->mz = 'Z';
1.1.1.30  root     13830:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13831:                }
                   13832:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13833:        }
1.1.1.3   root     13834:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13835: }
                   13836: 
                   13837: inline void msdos_int_29h()
                   13838: {
1.1.1.50  root     13839:        msdos_putch_fast(REG8(AL));
1.1       root     13840: }
                   13841: 
                   13842: inline void msdos_int_2eh()
                   13843: {
                   13844:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13845:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13846:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13847:        char *token = my_strtok(tmp, " ");
                   13848:        strcpy(command, token);
                   13849:        strcpy(opt, token + strlen(token) + 1);
                   13850:        
                   13851:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13852:        param->env_seg = 0;
                   13853:        param->cmd_line.w.l = 44;
                   13854:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13855:        param->fcb1.w.l = 24;
                   13856:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13857:        param->fcb2.w.l = 24;
                   13858:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13859:        
                   13860:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13861:        
                   13862:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13863:        cmd_line->len = strlen(opt);
                   13864:        strcpy(cmd_line->cmd, opt);
                   13865:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13866:        
1.1.1.28  root     13867:        try {
                   13868:                if(msdos_process_exec(command, param, 0)) {
                   13869:                        REG16(AX) = 0xffff; // error before processing command
                   13870:                } else {
                   13871:                        // set flag to set retval to ax when the started process is terminated
                   13872:                        process_t *process = msdos_process_info_get(current_psp);
                   13873:                        process->called_by_int2eh = true;
                   13874:                }
                   13875:        } catch(...) {
                   13876:                REG16(AX) = 0xffff; // error before processing command
                   13877:        }
1.1       root     13878: }
                   13879: 
1.1.1.29  root     13880: inline void msdos_int_2fh_05h()
                   13881: {
                   13882:        switch(REG8(AL)) {
                   13883:        case 0x00:
1.1.1.49  root     13884:                // critical error handler is installed
1.1.1.32  root     13885:                REG8(AL) = 0xff;
                   13886:                break;
                   13887:        case 0x01:
                   13888:        case 0x02:
                   13889:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13890:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13891:                                const char *message = NULL;
                   13892:                                if(active_code_page == 932) {
                   13893:                                        message = standard_error_table[i].message_japanese;
                   13894:                                }
                   13895:                                if(message == NULL) {
                   13896:                                        message = standard_error_table[i].message_english;
                   13897:                                }
                   13898:                                strcpy((char *)(mem + WORK_TOP), message);
                   13899:                                
                   13900:                                SREG(ES) = WORK_TOP >> 4;
                   13901:                                i386_load_segment_descriptor(ES);
                   13902:                                REG16(DI) = 0x0000;
                   13903:                                REG8(AL) = 0x01;
                   13904:                                break;
                   13905:                        }
                   13906:                }
1.1.1.29  root     13907:                break;
                   13908:        default:
                   13909:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.49  root     13910:                REG16(AX) = 0x01;
1.1.1.29  root     13911:                m_CF = 1;
                   13912:        }
                   13913: }
                   13914: 
1.1.1.44  root     13915: inline void msdos_int_2fh_06h()
                   13916: {
                   13917:        switch(REG8(AL)) {
                   13918:        case 0x00:
                   13919:                // ASSIGN is not installed
1.1.1.49  root     13920: //             REG8(AL) = 0x00;
1.1.1.44  root     13921:                break;
                   13922:        case 0x01:
                   13923:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13924:                REG16(AX) = 0x01;
                   13925:                m_CF = 1;
                   13926:                break;
                   13927:        default:
                   13928:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   13929:                REG16(AX) = 0x01;
                   13930:                m_CF = 1;
                   13931:                break;
                   13932:        }
                   13933: }
                   13934: 
1.1.1.22  root     13935: inline void msdos_int_2fh_11h()
                   13936: {
                   13937:        switch(REG8(AL)) {
                   13938:        case 0x00:
1.1.1.29  root     13939:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     13940: #ifdef SUPPORT_MSCDEX
                   13941:                        // MSCDEX is installed
                   13942:                        REG8(AL) = 0xff;
                   13943:                        i386_write_stack(0xadad);
                   13944: #else
1.1.1.29  root     13945:                        // MSCDEX is not installed
                   13946: //                     REG8(AL) = 0x00;
1.1.1.53  root     13947: #endif
1.1.1.29  root     13948:                } else {
                   13949:                        // Network Redirector is not installed
                   13950: //                     REG8(AL) = 0x00;
                   13951:                }
1.1.1.22  root     13952:                break;
                   13953:        default:
1.1.1.43  root     13954: //             unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.29  root     13955:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13956:                m_CF = 1;
                   13957:                break;
                   13958:        }
                   13959: }
                   13960: 
1.1.1.21  root     13961: inline void msdos_int_2fh_12h()
                   13962: {
                   13963:        switch(REG8(AL)) {
1.1.1.22  root     13964:        case 0x00:
1.1.1.29  root     13965:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13966:                REG8(AL) = 0xff;
                   13967:                break;
1.1.1.29  root     13968: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13969:        case 0x02:
                   13970:                {
                   13971:                        UINT16 stack = i386_read_stack();
                   13972:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13973:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13974:                        i386_load_segment_descriptor(ES);
                   13975:                }
                   13976:                break;
1.1.1.30  root     13977:        case 0x03:
                   13978:                SREG(DS) = (DEVICE_TOP >> 4);
                   13979:                i386_load_segment_descriptor(DS);
                   13980:                break;
1.1.1.29  root     13981:        case 0x04:
                   13982:                {
                   13983:                        UINT16 stack = i386_read_stack();
                   13984:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13985: #if defined(HAS_I386)
                   13986:                        m_ZF = (REG8(AL) == '\\');
                   13987: #else
                   13988:                        m_ZeroVal = (REG8(AL) != '\\');
                   13989: #endif
                   13990:                }
                   13991:                break;
                   13992:        case 0x05:
1.1.1.49  root     13993:                {
                   13994:                        UINT16 c = i386_read_stack();
                   13995:                        if((c >> 0) & 0xff) {
                   13996:                                msdos_putch((c >> 0) & 0xff);
                   13997:                        }
                   13998:                        if((c >> 8) & 0xff) {
                   13999:                                msdos_putch((c >> 8) & 0xff);
                   14000:                        }
                   14001:                }
1.1.1.29  root     14002:                break;
1.1.1.49  root     14003: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     14004: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   14005: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     14006: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     14007: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   14008: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   14009: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   14010:        case 0x0d:
                   14011:                {
                   14012:                        SYSTEMTIME time;
                   14013:                        FILETIME file_time;
                   14014:                        WORD dos_date, dos_time;
                   14015:                        GetLocalTime(&time);
                   14016:                        SystemTimeToFileTime(&time, &file_time);
                   14017:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   14018:                        REG16(AX) = dos_date;
                   14019:                        REG16(DX) = dos_time;
                   14020:                }
                   14021:                break;
                   14022: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   14023: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   14024: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   14025:        case 0x11:
                   14026:                {
                   14027:                        char path[MAX_PATH], *p;
                   14028:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14029:                        my_strupr(path);
                   14030:                        while((p = my_strchr(path, '/')) != NULL) {
                   14031:                                *p = '\\';
                   14032:                        }
                   14033:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   14034:                }
                   14035:                break;
                   14036:        case 0x12:
                   14037:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14038:                break;
                   14039:        case 0x13:
                   14040:                {
                   14041:                        char tmp[2] = {0};
                   14042:                        tmp[0] = i386_read_stack();
                   14043:                        my_strupr(tmp);
                   14044:                        REG8(AL) = tmp[0];
                   14045:                }
                   14046:                break;
                   14047:        case 0x14:
                   14048: #if defined(HAS_I386)
                   14049:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   14050: #else
                   14051:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14052: #endif
                   14053:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14054:                break;
                   14055: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     14056:        case 0x16:
                   14057:                if(REG16(BX) < 20) {
                   14058:                        SREG(ES) = SFT_TOP >> 4;
                   14059:                        i386_load_segment_descriptor(ES);
                   14060:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   14061:                        
                   14062:                        // update system file table
                   14063:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   14064:                        if(file_handler[REG16(BX)].valid) {
                   14065:                                int count = 0;
                   14066:                                for(int i = 0; i < 20; i++) {
                   14067:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   14068:                                                count++;
                   14069:                                        }
                   14070:                                }
                   14071:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   14072:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   14073:                                _lseek(REG16(BX), 0, SEEK_END);
                   14074:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   14075:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   14076:                        } else {
                   14077:                                memset(sft, 0, 0x3b);
                   14078:                        }
                   14079:                } else {
                   14080:                        REG16(AX) = 0x06;
                   14081:                        m_CF = 1;
                   14082:                }
                   14083:                break;
1.1.1.49  root     14084:        case 0x17:
                   14085:                {
                   14086:                        UINT16 drive = i386_read_stack();
                   14087:                        if(msdos_is_valid_drive(drive)) {
                   14088:                                msdos_cds_update(drive);
                   14089:                        }
                   14090:                        REG16(SI) = 88 * drive;
                   14091:                        SREG(DS) = (CDS_TOP >> 4);
                   14092:                        i386_load_segment_descriptor(DS);
                   14093:                }
                   14094:                break;
1.1.1.29  root     14095: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   14096: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   14097:        case 0x1a:
                   14098:                {
                   14099:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   14100:                        if(path[1] == ':') {
                   14101:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   14102:                                        REG8(AL) = path[0] - 'a' + 1;
                   14103:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   14104:                                        REG8(AL) = path[0] - 'A' + 1;
                   14105:                                } else {
                   14106:                                        REG8(AL) = 0xff; // invalid
                   14107:                                }
                   14108:                                strcpy(full, path);
                   14109:                                strcpy(path, full + 2);
1.1.1.60  root     14110:                        } else if(GetFullPathNameA(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
1.1.1.29  root     14111:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   14112:                                        REG8(AL) = full[0] - 'a' + 1;
                   14113:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   14114:                                        REG8(AL) = full[0] - 'A' + 1;
                   14115:                                } else {
                   14116:                                        REG8(AL) = 0xff; // invalid
                   14117:                                }
                   14118:                        } else {
                   14119:                                REG8(AL) = 0x00; // default
                   14120:                        }
                   14121:                }
                   14122:                break;
                   14123:        case 0x1b:
                   14124:                {
                   14125:                        int year = REG16(CX) + 1980;
                   14126:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   14127:                }
                   14128:                break;
                   14129: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   14130: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   14131:        case 0x1e:
                   14132:                {
                   14133:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   14134:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
1.1.1.60  root     14135:                        if(GetFullPathNameA(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathNameA(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
1.1.1.29  root     14136: #if defined(HAS_I386)
                   14137:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   14138: #else
                   14139:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   14140: #endif
                   14141:                        } else {
                   14142: #if defined(HAS_I386)
                   14143:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   14144: #else
                   14145:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   14146: #endif
                   14147:                        }
                   14148:                }
                   14149:                break;
1.1.1.49  root     14150:        case 0x1f:
                   14151:                {
                   14152:                        UINT16 drive = i386_read_stack();
                   14153:                        if(msdos_is_valid_drive(drive)) {
                   14154:                                msdos_cds_update(drive);
                   14155:                        }
                   14156:                        REG16(SI) = 88 * drive;
                   14157:                        SREG(ES) = (CDS_TOP >> 4);
                   14158:                        i386_load_segment_descriptor(ES);
                   14159:                }
                   14160:                break;
1.1.1.21  root     14161:        case 0x20:
                   14162:                {
                   14163:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   14164:                        
                   14165:                        if(fd < 20) {
                   14166:                                SREG(ES) = current_psp;
                   14167:                                i386_load_segment_descriptor(ES);
                   14168:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   14169:                        } else {
                   14170:                                REG16(AX) = 0x06;
                   14171:                                m_CF = 1;
                   14172:                        }
                   14173:                }
                   14174:                break;
1.1.1.29  root     14175:        case 0x21:
                   14176:                msdos_int_21h_60h(0);
                   14177:                break;
1.1.1.49  root     14178:        case 0x22:
                   14179:                {
                   14180:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14181:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   14182:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   14183:                        }
                   14184:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   14185:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   14186:                        }
                   14187:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   14188:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   14189:                        }
                   14190:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   14191:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   14192:                        }
                   14193:                }
                   14194:                break;
1.1.1.29  root     14195: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   14196: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   14197:        case 0x25:
                   14198:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14199:                break;
                   14200:        case 0x26:
                   14201:                REG8(AL) = REG8(CL);
                   14202:                msdos_int_21h_3dh();
                   14203:                break;
                   14204:        case 0x27:
                   14205:                msdos_int_21h_3eh();
                   14206:                break;
                   14207:        case 0x28:
                   14208:                REG16(AX) = REG16(BP);
                   14209:                msdos_int_21h_42h();
                   14210:                break;
                   14211:        case 0x29:
                   14212:                msdos_int_21h_3fh();
                   14213:                break;
                   14214: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   14215:        case 0x2b:
                   14216:                REG16(AX) = REG16(BP);
                   14217:                msdos_int_21h_44h();
                   14218:                break;
                   14219:        case 0x2c:
                   14220:                REG16(BX) = DEVICE_TOP >> 4;
                   14221:                REG16(AX) = 22;
                   14222:                break;
                   14223:        case 0x2d:
                   14224:                {
                   14225:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14226:                        REG16(AX) = sda->extended_error_code;
                   14227:                }
                   14228:                break;
                   14229:        case 0x2e:
                   14230:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     14231:                        SREG(ES) = 0x0001;
                   14232:                        i386_load_segment_descriptor(ES);
                   14233:                        REG16(DI) = 0x00;
                   14234:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     14235:                        // dummy parameter error message read routine is at fffc:0010
                   14236:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     14237:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     14238:                        REG16(DI) = 0x0010;
1.1.1.22  root     14239:                }
                   14240:                break;
1.1.1.29  root     14241:        case 0x2f:
                   14242:                if(REG16(DX) != 0) {
1.1.1.30  root     14243:                        dos_major_version = REG8(DL);
                   14244:                        dos_minor_version = REG8(DH);
1.1.1.29  root     14245:                } else {
                   14246:                        REG8(DL) = 7;
                   14247:                        REG8(DH) = 10;
                   14248:                }
                   14249:                break;
                   14250: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   14251: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     14252:        default:
                   14253:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14254:                REG16(AX) = 0x01;
                   14255:                m_CF = 1;
                   14256:                break;
                   14257:        }
                   14258: }
                   14259: 
1.1.1.30  root     14260: inline void msdos_int_2fh_13h()
                   14261: {
                   14262:        static UINT16 prevDS = 0, prevDX = 0;
                   14263:        static UINT16 prevES = 0, prevBX = 0;
                   14264:        UINT16 tmp;
                   14265:        
                   14266:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   14267:        i386_load_segment_descriptor(DS);
                   14268:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   14269:        
                   14270:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   14271:        i386_load_segment_descriptor(ES);
                   14272:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   14273: }
                   14274: 
1.1.1.22  root     14275: inline void msdos_int_2fh_14h()
                   14276: {
                   14277:        switch(REG8(AL)) {
                   14278:        case 0x00:
1.1.1.29  root     14279:                // NLSFUNC.COM is installed
                   14280:                REG8(AL) = 0xff;
1.1.1.25  root     14281:                break;
                   14282:        case 0x01:
                   14283:        case 0x03:
                   14284:                REG8(AL) = 0x00;
                   14285:                active_code_page = REG16(BX);
                   14286:                msdos_nls_tables_update();
                   14287:                break;
                   14288:        case 0x02:
                   14289:                REG8(AL) = get_extended_country_info(REG16(BP));
                   14290:                break;
                   14291:        case 0x04:
1.1.1.42  root     14292:                for(int i = 0;; i++) {
                   14293:                        if(country_table[i].code == REG16(DX)) {
                   14294:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   14295:                                break;
                   14296:                        } else if(country_table[i].code == -1) {
                   14297:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14298:                                break;
                   14299:                        }
                   14300:                }
1.1.1.25  root     14301:                REG8(AL) = 0x00;
1.1.1.22  root     14302:                break;
                   14303:        default:
                   14304:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14305:                REG16(AX) = 0x01;
                   14306:                m_CF = 1;
                   14307:                break;
                   14308:        }
                   14309: }
                   14310: 
                   14311: inline void msdos_int_2fh_15h()
                   14312: {
                   14313:        switch(REG8(AL)) {
1.1.1.29  root     14314:        case 0x00: // CD-ROM - Installation Check
                   14315:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     14316: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     14317:                        // MSCDEX is installed
                   14318:                        REG16(BX) = 0;
                   14319:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     14320:                                if(msdos_is_cdrom_drive(i)) {
                   14321:                                        if(REG16(BX) == 0) {
                   14322:                                                REG16(CX) = i;
1.1.1.43  root     14323:                                        }
1.1.1.44  root     14324:                                        REG16(BX)++;
1.1.1.43  root     14325:                                }
                   14326:                        }
                   14327: #else
1.1.1.29  root     14328:                        // MSCDEX is not installed
                   14329: //                     REG8(AL) = 0x00;
1.1.1.43  root     14330: #endif
1.1.1.29  root     14331:                } else {
                   14332:                        // GRAPHICS.COM is not installed
                   14333: //                     REG8(AL) = 0x00;
                   14334:                }
1.1.1.22  root     14335:                break;
1.1.1.43  root     14336:        case 0x0b:
1.1.1.44  root     14337:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14338:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14339:                REG16(BX) = 0xadad;
1.1.1.43  root     14340:                break;
                   14341:        case 0x0d:
1.1.1.44  root     14342:                for(int i = 0, n = 0; i < 26; i++) {
                   14343:                        if(msdos_is_cdrom_drive(i)) {
                   14344:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14345:                        }
                   14346:                }
                   14347:                break;
1.1.1.22  root     14348:        case 0xff:
1.1.1.29  root     14349:                if(REG16(BX) == 0x0000) {
                   14350:                        // CORELCDX is not installed
                   14351:                } else {
                   14352:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14353:                        REG16(AX) = 0x01;
                   14354:                        m_CF = 1;
                   14355:                }
1.1.1.22  root     14356:                break;
1.1.1.21  root     14357:        default:
1.1.1.22  root     14358:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.21  root     14359:                REG16(AX) = 0x01;
                   14360:                m_CF = 1;
                   14361:                break;
                   14362:        }
                   14363: }
                   14364: 
1.1       root     14365: inline void msdos_int_2fh_16h()
                   14366: {
                   14367:        switch(REG8(AL)) {
                   14368:        case 0x00:
1.1.1.14  root     14369:                if(no_windows) {
1.1.1.29  root     14370:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14371: //                     REG8(AL) = 0x00;
1.1.1.14  root     14372:                } else {
1.1.1.30  root     14373:                        REG8(AL) = win_major_version;
                   14374:                        REG8(AH) = win_minor_version;
1.1       root     14375:                }
                   14376:                break;
1.1.1.43  root     14377:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14378:                // from DOSBox
                   14379:                i386_set_a20_line(1);
                   14380:                break;
1.1.1.49  root     14381:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14382:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14383:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14384:                break;
                   14385:        case 0x07:
                   14386:                // Virtual Device Call API
                   14387:                break;
1.1.1.22  root     14388:        case 0x0a:
                   14389:                if(!no_windows) {
                   14390:                        REG16(AX) = 0x0000;
1.1.1.30  root     14391:                        REG8(BH) = win_major_version;
                   14392:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14393: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14394:                        REG16(CX) = 0x0003; // enhanced
                   14395:                }
                   14396:                break;
1.1.1.30  root     14397:        case 0x0b:
                   14398:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14399:        case 0x0e:
                   14400:        case 0x0f:
1.1.1.30  root     14401:        case 0x10:
1.1.1.22  root     14402:        case 0x11:
                   14403:        case 0x12:
                   14404:        case 0x13:
                   14405:        case 0x14:
1.1.1.30  root     14406:        case 0x15:
1.1.1.43  root     14407:        case 0x81:
                   14408:        case 0x82:
1.1.1.44  root     14409:        case 0x84:
1.1.1.49  root     14410:        case 0x85:
1.1.1.33  root     14411:        case 0x86:
1.1.1.22  root     14412:        case 0x87:
1.1.1.30  root     14413:        case 0x89:
1.1.1.33  root     14414:        case 0x8a:
1.1.1.22  root     14415:                // function not supported, do not clear AX
                   14416:                break;
1.1.1.14  root     14417:        case 0x80:
                   14418:                Sleep(10);
1.1.1.35  root     14419:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14420:                REG8(AL) = 0x00;
1.1.1.14  root     14421:                break;
1.1.1.33  root     14422:        case 0x83:
                   14423:                REG16(BX) = 0x01; // system vm id
                   14424:                break;
1.1.1.22  root     14425:        case 0x8e:
                   14426:                REG16(AX) = 0x00; // failed
                   14427:                break;
1.1.1.20  root     14428:        case 0x8f:
                   14429:                switch(REG8(DH)) {
                   14430:                case 0x01:
1.1.1.49  root     14431: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14432: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14433:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14434:                        break;
                   14435:                default:
                   14436:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14437:                        break;
                   14438:                }
                   14439:                break;
1.1       root     14440:        default:
1.1.1.22  root     14441:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14442:                REG16(AX) = 0x01;
                   14443:                m_CF = 1;
                   14444:                break;
                   14445:        }
                   14446: }
                   14447: 
                   14448: inline void msdos_int_2fh_19h()
                   14449: {
                   14450:        switch(REG8(AL)) {
                   14451:        case 0x00:
1.1.1.29  root     14452:                // SHELLB.COM is not installed
                   14453: //             REG8(AL) = 0x00;
1.1.1.22  root     14454:                break;
                   14455:        case 0x01:
                   14456:        case 0x02:
                   14457:        case 0x03:
                   14458:        case 0x04:
                   14459:                REG16(AX) = 0x01;
                   14460:                m_CF = 1;
                   14461:                break;
1.1.1.29  root     14462:        case 0x80:
                   14463:                // IBM ROM-DOS v4.0 is not installed
                   14464: //             REG8(AL) = 0x00;
                   14465:                break;
1.1.1.22  root     14466:        default:
                   14467:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     14468:                REG16(AX) = 0x01;
1.1.1.3   root     14469:                m_CF = 1;
1.1       root     14470:                break;
                   14471:        }
                   14472: }
                   14473: 
                   14474: inline void msdos_int_2fh_1ah()
                   14475: {
                   14476:        switch(REG8(AL)) {
                   14477:        case 0x00:
1.1.1.29  root     14478:                // ANSI.SYS is installed
1.1       root     14479:                REG8(AL) = 0xff;
                   14480:                break;
1.1.1.49  root     14481:        case 0x01:
1.1.1.50  root     14482:                if(REG8(CL) == 0x5f) {
                   14483:                        // set display information
                   14484:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14485:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14486:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14487:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14488:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14489:                                
                   14490:                                if(cur_width != new_width || cur_height != new_height) {
                   14491:                                        pcbios_set_console_size(new_width, new_height, true);
                   14492:                                }
                   14493:                        }
                   14494:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14495:                        // get display information
1.1.1.50  root     14496:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14497:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14498:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14499:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14500:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14501:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14502:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14503:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14504:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14505:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14506:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14507:                } else {
                   14508:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14509:                        REG16(AX) = 0x01;
                   14510:                        m_CF = 1;
                   14511:                }
                   14512:                break;
1.1       root     14513:        default:
1.1.1.22  root     14514:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14515:                REG16(AX) = 0x01;
                   14516:                m_CF = 1;
                   14517:                break;
                   14518:        }
                   14519: }
                   14520: 
1.1.1.30  root     14521: inline void msdos_int_2fh_40h()
1.1.1.22  root     14522: {
                   14523:        switch(REG8(AL)) {
                   14524:        case 0x00:
1.1.1.30  root     14525:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14526:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14527:                break;
1.1.1.43  root     14528:        case 0x10:
                   14529:                // OS/2 v2.0+ - Installation Check
                   14530:                REG16(AX) = 0x01;
                   14531:                m_CF = 1;
                   14532:                break;
1.1.1.22  root     14533:        default:
                   14534:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     14535:                REG16(AX) = 0x01;
1.1.1.3   root     14536:                m_CF = 1;
1.1       root     14537:                break;
                   14538:        }
                   14539: }
                   14540: 
                   14541: inline void msdos_int_2fh_43h()
                   14542: {
                   14543:        switch(REG8(AL)) {
                   14544:        case 0x00:
1.1.1.29  root     14545:                // XMS is installed ?
1.1.1.19  root     14546: #ifdef SUPPORT_XMS
                   14547:                if(support_xms) {
                   14548:                        REG8(AL) = 0x80;
1.1.1.44  root     14549:                }
                   14550: #endif
                   14551:                break;
                   14552:        case 0x08:
                   14553: #ifdef SUPPORT_XMS
                   14554:                if(support_xms) {
                   14555:                        REG8(AL) = 0x43;
                   14556:                        REG8(BL) = 0x01; // IBM PC/AT
                   14557:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14558:                }
1.1.1.19  root     14559: #endif
                   14560:                break;
                   14561:        case 0x10:
                   14562:                SREG(ES) = XMS_TOP >> 4;
                   14563:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14564:                REG16(BX) = 0x15;
1.1       root     14565:                break;
1.1.1.44  root     14566:        case 0xe0:
                   14567:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14568:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14569:                        break;
                   14570:                }
1.1       root     14571:        default:
1.1.1.22  root     14572:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14573:                REG16(AX) = 0x01;
                   14574:                m_CF = 1;
                   14575:                break;
                   14576:        }
                   14577: }
                   14578: 
                   14579: inline void msdos_int_2fh_46h()
                   14580: {
                   14581:        switch(REG8(AL)) {
                   14582:        case 0x80:
1.1.1.29  root     14583:                // Windows v3.0 is not installed
                   14584: //             REG8(AL) = 0x00;
1.1.1.22  root     14585:                break;
                   14586:        default:
                   14587:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14588:                REG16(AX) = 0x01;
                   14589:                m_CF = 1;
                   14590:                break;
                   14591:        }
                   14592: }
                   14593: 
                   14594: inline void msdos_int_2fh_48h()
                   14595: {
                   14596:        switch(REG8(AL)) {
                   14597:        case 0x00:
1.1.1.29  root     14598:                // DOSKEY is not installed
                   14599: //             REG8(AL) = 0x00;
1.1.1.22  root     14600:                break;
                   14601:        case 0x10:
                   14602:                msdos_int_21h_0ah();
                   14603:                REG16(AX) = 0x00;
                   14604:                break;
                   14605:        default:
                   14606:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     14607:                REG16(AX) = 0x01;
1.1.1.3   root     14608:                m_CF = 1;
1.1       root     14609:                break;
                   14610:        }
                   14611: }
                   14612: 
                   14613: inline void msdos_int_2fh_4ah()
                   14614: {
                   14615:        switch(REG8(AL)) {
1.1.1.29  root     14616: #ifdef SUPPORT_HMA
                   14617:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14618:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14619:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14620:                                // restore first free mcb in high memory area
                   14621:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14622:                        }
                   14623:                        int offset = 0xffff;
                   14624:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14625:                                REG16(DI) = offset + 0x10;
                   14626:                        } else {
                   14627:                                REG16(DI) = 0xffff;
                   14628:                        }
                   14629:                } else {
                   14630:                        // HMA is already used
                   14631:                        REG16(BX) = 0;
                   14632:                        REG16(DI) = 0xffff;
                   14633:                }
                   14634:                SREG(ES) = 0xffff;
                   14635:                i386_load_segment_descriptor(ES);
                   14636:                break;
                   14637:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14638:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14639:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14640:                                // restore first free mcb in high memory area
                   14641:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14642:                        }
                   14643:                        int size = REG16(BX), offset;
                   14644:                        if((size % 16) != 0) {
                   14645:                                size &= ~15;
                   14646:                                size += 16;
                   14647:                        }
                   14648:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14649:                                REG16(BX) = size;
                   14650:                                REG16(DI) = offset + 0x10;
                   14651:                                is_hma_used_by_int_2fh = true;
                   14652:                        } else {
                   14653:                                REG16(BX) = 0;
                   14654:                                REG16(DI) = 0xffff;
                   14655:                        }
                   14656:                } else {
                   14657:                        // HMA is already used
                   14658:                        REG16(BX) = 0;
                   14659:                        REG16(DI) = 0xffff;
                   14660:                }
                   14661:                SREG(ES) = 0xffff;
                   14662:                i386_load_segment_descriptor(ES);
                   14663:                break;
                   14664:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14665:                if(REG8(DL) == 0x00) {
                   14666:                        if(!is_hma_used_by_xms) {
                   14667:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14668:                                        // restore first free mcb in high memory area
                   14669:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14670:                                        is_hma_used_by_int_2fh = false;
                   14671:                                }
                   14672:                                int size = REG16(BX), offset;
                   14673:                                if((size % 16) != 0) {
                   14674:                                        size &= ~15;
                   14675:                                        size += 16;
                   14676:                                }
                   14677:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14678: //                                     REG16(BX) = size;
                   14679:                                        SREG(ES) = 0xffff;
                   14680:                                        i386_load_segment_descriptor(ES);
                   14681:                                        REG16(DI) = offset + 0x10;
                   14682:                                        is_hma_used_by_int_2fh = true;
                   14683:                                } else {
                   14684:                                        REG16(DI) = 0xffff;
                   14685:                                }
                   14686:                        } else {
                   14687:                                REG16(DI) = 0xffff;
                   14688:                        }
                   14689:                } else if(REG8(DL) == 0x01) {
                   14690:                        if(!is_hma_used_by_xms) {
                   14691:                                int size = REG16(BX);
                   14692:                                if((size % 16) != 0) {
                   14693:                                        size &= ~15;
                   14694:                                        size += 16;
                   14695:                                }
                   14696:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14697:                                        // memory block address is not changed
                   14698:                                } else {
                   14699:                                        REG16(DI) = 0xffff;
                   14700:                                }
                   14701:                        } else {
                   14702:                                REG16(DI) = 0xffff;
                   14703:                        }
                   14704:                } else if(REG8(DL) == 0x02) {
                   14705:                        if(!is_hma_used_by_xms) {
                   14706:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14707:                                        // restore first free mcb in high memory area
                   14708:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14709:                                        is_hma_used_by_int_2fh = false;
                   14710:                                } else {
                   14711:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14712:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14713:                                                is_hma_used_by_int_2fh = false;
                   14714:                                        }
                   14715:                                }
                   14716:                        }
                   14717:                } else {
                   14718:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14719:                        REG16(AX) = 0x01;
                   14720:                        m_CF = 1;
                   14721:                }
                   14722:                break;
                   14723:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14724:                if(!is_hma_used_by_xms) {
                   14725:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14726:                                // restore first free mcb in high memory area
                   14727:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14728:                                is_hma_used_by_int_2fh = false;
                   14729:                        }
                   14730:                        REG16(AX) = 0x0000;
                   14731:                        SREG(ES) = 0xffff;
                   14732:                        i386_load_segment_descriptor(ES);
                   14733:                        REG16(DI) = 0x10;
                   14734:                }
                   14735:                break;
                   14736: #else
1.1       root     14737:        case 0x01:
                   14738:        case 0x02:
1.1.1.29  root     14739:                // HMA is already used
1.1.1.27  root     14740:                REG16(BX) = 0x0000;
1.1.1.3   root     14741:                SREG(ES) = 0xffff;
                   14742:                i386_load_segment_descriptor(ES);
1.1       root     14743:                REG16(DI) = 0xffff;
                   14744:                break;
1.1.1.19  root     14745:        case 0x03:
                   14746:                // unable to allocate
                   14747:                REG16(DI) = 0xffff;
                   14748:                break;
                   14749:        case 0x04:
                   14750:                // function not supported, do not clear AX
                   14751:                break;
1.1.1.29  root     14752: #endif
                   14753:        case 0x10:
1.1.1.42  root     14754:                switch(REG16(BX)) {
                   14755:                case 0x0000:
                   14756:                case 0x0001:
                   14757:                case 0x0002:
                   14758:                case 0x0003:
                   14759:                case 0x0004:
                   14760:                case 0x0005:
                   14761:                case 0x0006:
                   14762:                case 0x0007:
                   14763:                case 0x0008:
                   14764:                case 0x000a:
                   14765:                case 0x1234:
                   14766:                        // SMARTDRV v4.00+ is not installed
                   14767:                        break;
                   14768:                default:
1.1.1.29  root     14769:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14770:                        REG16(AX) = 0x01;
                   14771:                        m_CF = 1;
1.1.1.42  root     14772:                        break;
1.1.1.29  root     14773:                }
                   14774:                break;
                   14775:        case 0x11:
1.1.1.42  root     14776:                switch(REG16(BX)) {
                   14777:                case 0x0000:
                   14778:                case 0x0001:
                   14779:                case 0x0002:
                   14780:                case 0x0003:
                   14781:                case 0x0004:
                   14782:                case 0x0005:
                   14783:                case 0x0006:
                   14784:                case 0x0007:
                   14785:                case 0x0008:
                   14786:                case 0x0009:
                   14787:                case 0x000a:
                   14788:                case 0x000b:
                   14789:                case 0xfffe:
                   14790:                case 0xffff:
1.1.1.29  root     14791:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14792:                        break;
                   14793:                default:
                   14794:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14795:                        REG16(AX) = 0x01;
                   14796:                        m_CF = 1;
                   14797:                        break;
                   14798:                }
                   14799:                break;
                   14800:        case 0x12:
                   14801:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14802:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14803:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14804:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14805:                } else {
                   14806:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14807:                        REG16(AX) = 0x01;
                   14808:                        m_CF = 1;
                   14809:                }
1.1.1.22  root     14810:                break;
1.1.1.42  root     14811:        case 0x13:
                   14812:                // DBLSPACE.BIN is not installed
                   14813:                break;
1.1.1.22  root     14814:        default:
                   14815:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14816:                REG16(AX) = 0x01;
                   14817:                m_CF = 1;
                   14818:                break;
                   14819:        }
                   14820: }
                   14821: 
                   14822: inline void msdos_int_2fh_4bh()
                   14823: {
                   14824:        switch(REG8(AL)) {
1.1.1.24  root     14825:        case 0x01:
1.1.1.22  root     14826:        case 0x02:
1.1.1.29  root     14827:                // Task Switcher is not installed
1.1.1.24  root     14828:                break;
                   14829:        case 0x03:
                   14830:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14831:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14832:                break;
1.1.1.30  root     14833:        case 0x04:
                   14834:                REG16(BX) = 0x0000; // free switcher id successfully
                   14835:                break;
1.1.1.43  root     14836:        case 0x05:
                   14837:                REG16(BX) = 0x0000; // no instance data chain
                   14838:                SREG(ES) = 0x0000;
                   14839:                i386_load_segment_descriptor(ES);
                   14840:                break;
1.1       root     14841:        default:
1.1.1.22  root     14842:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     14843:                REG16(AX) = 0x01;
1.1.1.3   root     14844:                m_CF = 1;
1.1       root     14845:                break;
                   14846:        }
                   14847: }
                   14848: 
1.1.1.44  root     14849: inline void msdos_int_2fh_4dh()
                   14850: {
                   14851:        switch(REG8(AL)) {
                   14852:        case 0x00:
                   14853:                // KKCFUNC is not installed ???
                   14854:                break;
                   14855:        default:
                   14856: //             unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14857:                REG16(AX) = 0x01; // invalid function
                   14858:                m_CF = 1;
                   14859:                break;
                   14860:        }
                   14861: }
                   14862: 
1.1       root     14863: inline void msdos_int_2fh_4fh()
                   14864: {
                   14865:        switch(REG8(AL)) {
                   14866:        case 0x00:
1.1.1.29  root     14867:                // BILING is installed
1.1.1.27  root     14868:                REG16(AX) = 0x0000;
                   14869:                REG8(DL) = 0x01;        // major version
                   14870:                REG8(DH) = 0x00;        // minor version
1.1       root     14871:                break;
                   14872:        case 0x01:
1.1.1.27  root     14873:                REG16(AX) = 0x0000;
1.1       root     14874:                REG16(BX) = active_code_page;
                   14875:                break;
                   14876:        default:
1.1.1.22  root     14877:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14878:                REG16(AX) = 0x01;
                   14879:                m_CF = 1;
                   14880:                break;
                   14881:        }
                   14882: }
                   14883: 
                   14884: inline void msdos_int_2fh_55h()
                   14885: {
                   14886:        switch(REG8(AL)) {
                   14887:        case 0x00:
                   14888:        case 0x01:
                   14889: //             unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14890:                break;
                   14891:        default:
                   14892:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     14893:                REG16(AX) = 0x01;
1.1.1.3   root     14894:                m_CF = 1;
1.1       root     14895:                break;
                   14896:        }
                   14897: }
                   14898: 
1.1.1.44  root     14899: inline void msdos_int_2fh_56h()
                   14900: {
                   14901:        switch(REG8(AL)) {
                   14902:        case 0x00:
                   14903:                // INTERLNK is not installed
                   14904:                break;
                   14905:        case 0x01:
                   14906:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14907: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14908: //                     REG8(AL) = 0x00;
                   14909: //             }
                   14910:                break;
                   14911:        default:
                   14912:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14913:                REG16(AX) = 0x01;
                   14914:                m_CF = 1;
                   14915:                break;
                   14916:        }
                   14917: }
                   14918: 
1.1.1.24  root     14919: inline void msdos_int_2fh_adh()
                   14920: {
                   14921:        switch(REG8(AL)) {
                   14922:        case 0x00:
1.1.1.29  root     14923:                // DISPLAY.SYS is installed
1.1.1.24  root     14924:                REG8(AL) = 0xff;
                   14925:                REG16(BX) = 0x100; // ???
                   14926:                break;
                   14927:        case 0x01:
                   14928:                active_code_page = REG16(BX);
                   14929:                msdos_nls_tables_update();
                   14930:                REG16(AX) = 0x01;
                   14931:                break;
                   14932:        case 0x02:
                   14933:                REG16(BX) = active_code_page;
                   14934:                break;
                   14935:        case 0x03:
                   14936:                // FIXME
                   14937:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14938:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14939:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14940:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14941:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14942:                break;
                   14943:        case 0x80:
1.1.1.49  root     14944:                // KEYB.COM is not installed
                   14945:                break;
1.1.1.24  root     14946:        default:
                   14947:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14948:                REG16(AX) = 0x01;
                   14949:                m_CF = 1;
                   14950:                break;
                   14951:        }
                   14952: }
                   14953: 
1.1       root     14954: inline void msdos_int_2fh_aeh()
                   14955: {
                   14956:        switch(REG8(AL)) {
                   14957:        case 0x00:
1.1.1.28  root     14958:                // FIXME: we need to check the given command line
                   14959:                REG8(AL) = 0x00; // the command should be executed as usual
                   14960: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14961:                break;
                   14962:        case 0x01:
                   14963:                {
                   14964:                        char command[MAX_PATH];
                   14965:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14966:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14967:                        
                   14968:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14969:                        param->env_seg = 0;
                   14970:                        param->cmd_line.w.l = 44;
                   14971:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14972:                        param->fcb1.w.l = 24;
                   14973:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14974:                        param->fcb2.w.l = 24;
                   14975:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14976:                        
                   14977:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14978:                        
                   14979:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14980:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14981:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14982:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14983:                        
1.1.1.28  root     14984:                        try {
                   14985:                                msdos_process_exec(command, param, 0);
                   14986:                        } catch(...) {
                   14987:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14988:                        }
                   14989:                }
                   14990:                break;
                   14991:        default:
1.1.1.22  root     14992:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     14993:                REG16(AX) = 0x01;
1.1.1.3   root     14994:                m_CF = 1;
1.1       root     14995:                break;
                   14996:        }
                   14997: }
                   14998: 
1.1.1.34  root     14999: inline void msdos_int_2fh_b7h()
                   15000: {
                   15001:        switch(REG8(AL)) {
                   15002:        case 0x00:
                   15003:                // APPEND is not installed
                   15004: //             REG8(AL) = 0x00;
                   15005:                break;
1.1.1.44  root     15006:        case 0x06:
                   15007:                REG16(BX) = 0x0000;
                   15008:                break;
1.1.1.34  root     15009:        case 0x07:
1.1.1.43  root     15010:        case 0x11:
1.1.1.34  root     15011:                // COMMAND.COM calls this service without checking APPEND is installed
                   15012:                break;
                   15013:        default:
                   15014:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   15015:                REG16(AX) = 0x01;
                   15016:                m_CF = 1;
                   15017:                break;
                   15018:        }
                   15019: }
                   15020: 
1.1.1.24  root     15021: inline void msdos_int_33h_0000h()
                   15022: {
                   15023:        REG16(AX) = 0xffff; // hardware/driver installed
                   15024:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15025: }
                   15026: 
                   15027: inline void msdos_int_33h_0001h()
                   15028: {
1.1.1.34  root     15029:        if(mouse.hidden > 0) {
                   15030:                mouse.hidden--;
                   15031:        }
                   15032:        if(mouse.hidden == 0) {
1.1.1.61! root     15033:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~(ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE));
1.1.1.59  root     15034:                pic[1].imr &= ~0x10; // enable irq12
1.1.1.24  root     15035:        }
                   15036: }
                   15037: 
                   15038: inline void msdos_int_33h_0002h()
                   15039: {
1.1.1.34  root     15040:        mouse.hidden++;
1.1.1.61! root     15041:        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
1.1.1.59  root     15042:        pic[1].imr |= 0x10; // disable irq12
1.1.1.24  root     15043: }
                   15044: 
                   15045: inline void msdos_int_33h_0003h()
                   15046: {
1.1.1.34  root     15047: //     if(mouse.hidden > 0) {
                   15048:                update_console_input();
                   15049: //     }
1.1.1.24  root     15050:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     15051:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   15052:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   15053: }
                   15054: 
                   15055: inline void msdos_int_33h_0004h()
                   15056: {
                   15057:        mouse.position.x = REG16(CX);
                   15058:        mouse.position.x = REG16(DX);
1.1.1.24  root     15059: }
                   15060: 
                   15061: inline void msdos_int_33h_0005h()
                   15062: {
1.1.1.34  root     15063: //     if(mouse.hidden > 0) {
                   15064:                update_console_input();
                   15065: //     }
1.1.1.24  root     15066:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15067:                int idx = REG16(BX);
1.1.1.34  root     15068:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   15069:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   15070:                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.buttons[idx].pressed_position.y));
1.1.1.24  root     15071:                mouse.buttons[idx].pressed_times = 0;
                   15072:        } else {
                   15073:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15074:        }
                   15075:        REG16(AX) = mouse.get_buttons();
                   15076: }
                   15077: 
                   15078: inline void msdos_int_33h_0006h()
                   15079: {
1.1.1.34  root     15080: //     if(mouse.hidden > 0) {
                   15081:                update_console_input();
                   15082: //     }
1.1.1.24  root     15083:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15084:                int idx = REG16(BX);
1.1.1.34  root     15085:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   15086:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   15087:                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.buttons[idx].released_position.y));
1.1.1.24  root     15088:                mouse.buttons[idx].released_times = 0;
                   15089:        } else {
                   15090:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15091:        }
                   15092:        REG16(AX) = mouse.get_buttons();
                   15093: }
                   15094: 
                   15095: inline void msdos_int_33h_0007h()
                   15096: {
                   15097:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   15098:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   15099: }
                   15100: 
                   15101: inline void msdos_int_33h_0008h()
                   15102: {
                   15103:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   15104:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   15105: }
                   15106: 
                   15107: inline void msdos_int_33h_0009h()
                   15108: {
                   15109:        mouse.hot_spot[0] = REG16(BX);
                   15110:        mouse.hot_spot[1] = REG16(CX);
                   15111: }
                   15112: 
1.1.1.49  root     15113: inline void msdos_int_33h_000ah()
                   15114: {
                   15115:        mouse.screen_mask = REG16(CX);
                   15116:        mouse.cursor_mask = REG16(DX);
                   15117: }
                   15118: 
1.1.1.24  root     15119: inline void msdos_int_33h_000bh()
                   15120: {
1.1.1.34  root     15121: //     if(mouse.hidden > 0) {
                   15122:                update_console_input();
                   15123: //     }
1.1.1.24  root     15124:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15125:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15126:        mouse.prev_position.x = mouse.position.x;
                   15127:        mouse.prev_position.y = mouse.position.y;
                   15128:        REG16(CX) = dx;
                   15129:        REG16(DX) = dy;
                   15130: }
                   15131: 
                   15132: inline void msdos_int_33h_000ch()
                   15133: {
                   15134:        mouse.call_mask = REG16(CX);
                   15135:        mouse.call_addr.w.l = REG16(DX);
                   15136:        mouse.call_addr.w.h = SREG(ES);
                   15137: }
                   15138: 
                   15139: inline void msdos_int_33h_000fh()
                   15140: {
                   15141:        mouse.mickey.x = REG16(CX);
                   15142:        mouse.mickey.y = REG16(DX);
                   15143: }
                   15144: 
                   15145: inline void msdos_int_33h_0011h()
                   15146: {
                   15147:        REG16(AX) = 0xffff;
                   15148:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15149: }
                   15150: 
                   15151: inline void msdos_int_33h_0014h()
                   15152: {
                   15153:        UINT16 old_mask = mouse.call_mask;
                   15154:        UINT16 old_ofs = mouse.call_addr.w.l;
                   15155:        UINT16 old_seg = mouse.call_addr.w.h;
                   15156:        
                   15157:        mouse.call_mask = REG16(CX);
                   15158:        mouse.call_addr.w.l = REG16(DX);
                   15159:        mouse.call_addr.w.h = SREG(ES);
                   15160:        
                   15161:        REG16(CX) = old_mask;
                   15162:        REG16(DX) = old_ofs;
                   15163:        SREG(ES) = old_seg;
                   15164:        i386_load_segment_descriptor(ES);
                   15165: }
                   15166: 
                   15167: inline void msdos_int_33h_0015h()
                   15168: {
                   15169:        REG16(BX) = sizeof(mouse);
                   15170: }
                   15171: 
                   15172: inline void msdos_int_33h_0016h()
                   15173: {
                   15174:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   15175: }
                   15176: 
                   15177: inline void msdos_int_33h_0017h()
                   15178: {
                   15179:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   15180: }
                   15181: 
1.1.1.43  root     15182: inline void msdos_int_33h_0018h()
                   15183: {
                   15184:        for(int i = 0; i < 8; i++) {
                   15185:                if(REG16(CX) & (1 << i)) {
                   15186:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   15187:                                // event handler already exists
                   15188:                                REG16(AX) = 0xffff;
                   15189:                                break;
                   15190:                        }
                   15191:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   15192:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   15193:                }
                   15194:        }
                   15195: }
                   15196: 
                   15197: inline void msdos_int_33h_0019h()
                   15198: {
                   15199:        UINT16 call_mask = REG16(CX);
                   15200:        
                   15201:        REG16(CX) = 0;
                   15202:        
                   15203:        for(int i = 0; i < 8; i++) {
                   15204:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   15205:                        for(int j = 0; j < 8; j++) {
                   15206:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   15207:                                        REG16(CX) |= (1 << j);
                   15208:                                }
                   15209:                        }
                   15210:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   15211:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   15212:                        break;
                   15213:                }
                   15214:        }
                   15215: }
                   15216: 
1.1.1.24  root     15217: inline void msdos_int_33h_001ah()
                   15218: {
                   15219:        mouse.sensitivity[0] = REG16(BX);
                   15220:        mouse.sensitivity[1] = REG16(CX);
                   15221:        mouse.sensitivity[2] = REG16(DX);
                   15222: }
                   15223: 
                   15224: inline void msdos_int_33h_001bh()
                   15225: {
                   15226:        REG16(BX) = mouse.sensitivity[0];
                   15227:        REG16(CX) = mouse.sensitivity[1];
                   15228:        REG16(DX) = mouse.sensitivity[2];
                   15229: }
                   15230: 
                   15231: inline void msdos_int_33h_001dh()
                   15232: {
                   15233:        mouse.display_page = REG16(BX);
                   15234: }
                   15235: 
                   15236: inline void msdos_int_33h_001eh()
                   15237: {
                   15238:        REG16(BX) = mouse.display_page;
                   15239: }
                   15240: 
1.1.1.34  root     15241: inline void msdos_int_33h_001fh()
                   15242: {
                   15243:        // from DOSBox
                   15244:        REG16(BX) = 0x0000;
                   15245:        SREG(ES) = 0x0000;
                   15246:        i386_load_segment_descriptor(ES);
                   15247:        mouse.enabled = false;
                   15248:        mouse.old_hidden = mouse.hidden;
                   15249:        mouse.hidden = 1;
                   15250: }
                   15251: 
                   15252: inline void msdos_int_33h_0020h()
                   15253: {
                   15254:        // from DOSBox
                   15255:        mouse.enabled = true;
                   15256:        mouse.hidden = mouse.old_hidden;
                   15257: }
                   15258: 
1.1.1.24  root     15259: inline void msdos_int_33h_0021h()
                   15260: {
                   15261:        REG16(AX) = 0xffff;
                   15262:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15263: }
                   15264: 
                   15265: inline void msdos_int_33h_0022h()
                   15266: {
                   15267:        mouse.language = REG16(BX);
                   15268: }
                   15269: 
                   15270: inline void msdos_int_33h_0023h()
                   15271: {
                   15272:        REG16(BX) = mouse.language;
                   15273: }
                   15274: 
                   15275: inline void msdos_int_33h_0024h()
                   15276: {
                   15277:        REG16(BX) = 0x0805; // V8.05
                   15278:        REG16(CX) = 0x0400; // PS/2
                   15279: }
                   15280: 
1.1.1.49  root     15281: inline void msdos_int_33h_0025h()
                   15282: {
                   15283:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   15284: }
                   15285: 
1.1.1.24  root     15286: inline void msdos_int_33h_0026h()
                   15287: {
                   15288:        REG16(BX) = 0x0000;
                   15289:        REG16(CX) = mouse.max_position.x;
                   15290:        REG16(DX) = mouse.max_position.y;
                   15291: }
                   15292: 
1.1.1.49  root     15293: inline void msdos_int_33h_0027h()
                   15294: {
                   15295: //     if(mouse.hidden > 0) {
                   15296:                update_console_input();
                   15297: //     }
                   15298:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15299:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15300:        mouse.prev_position.x = mouse.position.x;
                   15301:        mouse.prev_position.y = mouse.position.y;
                   15302:        REG16(AX) = mouse.screen_mask;
                   15303:        REG16(BX) = mouse.cursor_mask;
                   15304:        REG16(CX) = dx;
                   15305:        REG16(DX) = dy;
                   15306: }
                   15307: 
                   15308: inline void msdos_int_33h_0028h()
                   15309: {
                   15310:        if(REG16(CX) != 0) {
                   15311:                UINT8 tmp = REG8(AL);
                   15312:                REG8(AL) = REG8(CL);
                   15313:                pcbios_int_10h_00h();
                   15314:                REG8(AL) = tmp;
                   15315:        }
                   15316:        REG8(CL) = 0x00; // successful
                   15317: }
                   15318: 
                   15319: inline void msdos_int_33h_0029h()
                   15320: {
                   15321:        switch(REG16(CX)) {
                   15322:        case 0x0000:
                   15323:                REG16(CX) = 0x0003;
                   15324:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   15325:                break;
                   15326:        case 0x0003:
                   15327:                REG16(CX) = 0x0070;
                   15328:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15329:                break;
                   15330:        case 0x0070:
                   15331:                REG16(CX) = 0x0071;
                   15332:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15333:                break;
                   15334:        case 0x0071:
                   15335:                REG16(CX) = 0x0073;
                   15336:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15337:                break;
                   15338:        default:
                   15339:                REG16(CX) = 0x0000;
                   15340:                break;
                   15341:        }
                   15342:        if(REG16(CX) != 0) {
                   15343:                SREG(DS) = (WORK_TOP >> 4);
                   15344:        } else {
                   15345:                SREG(DS) = 0x0000;
                   15346:        }
                   15347:        i386_load_segment_descriptor(DS);
                   15348:        REG16(DX) = 0x0000;
                   15349: }
                   15350: 
1.1.1.24  root     15351: inline void msdos_int_33h_002ah()
                   15352: {
1.1.1.34  root     15353:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15354:        REG16(BX) = mouse.hot_spot[0];
                   15355:        REG16(CX) = mouse.hot_spot[1];
                   15356:        REG16(DX) = 4; // PS/2
                   15357: }
                   15358: 
                   15359: inline void msdos_int_33h_0031h()
                   15360: {
                   15361:        REG16(AX) = mouse.min_position.x;
                   15362:        REG16(BX) = mouse.min_position.y;
                   15363:        REG16(CX) = mouse.max_position.x;
                   15364:        REG16(DX) = mouse.max_position.y;
                   15365: }
                   15366: 
                   15367: inline void msdos_int_33h_0032h()
                   15368: {
                   15369:        REG16(AX) = 0;
1.1.1.49  root     15370:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15371:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15372:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15373: //     REG16(AX) |= 0x1000; // 0028h
                   15374: //     REG16(AX) |= 0x0800; // 0029h
                   15375:        REG16(AX) |= 0x0400; // 002ah
                   15376: //     REG16(AX) |= 0x0200; // 002bh
                   15377: //     REG16(AX) |= 0x0100; // 002ch
                   15378: //     REG16(AX) |= 0x0080; // 002dh
                   15379: //     REG16(AX) |= 0x0040; // 002eh
                   15380:        REG16(AX) |= 0x0020; // 002fh
                   15381: //     REG16(AX) |= 0x0010; // 0030h
                   15382:        REG16(AX) |= 0x0008; // 0031h
                   15383:        REG16(AX) |= 0x0004; // 0032h
                   15384: //     REG16(AX) |= 0x0002; // 0033h
                   15385: //     REG16(AX) |= 0x0001; // 0034h
                   15386: }
                   15387: 
1.1.1.49  root     15388: inline void msdos_int_33h_004dh()
                   15389: {
                   15390:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15391: }
                   15392: 
                   15393: inline void msdos_int_33h_006dh()
                   15394: {
                   15395:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15396:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15397: }
                   15398: 
1.1.1.19  root     15399: inline void msdos_int_67h_40h()
                   15400: {
                   15401:        if(!support_ems) {
                   15402:                REG8(AH) = 0x84;
                   15403:        } else {
                   15404:                REG8(AH) = 0x00;
                   15405:        }
                   15406: }
                   15407: 
                   15408: inline void msdos_int_67h_41h()
                   15409: {
                   15410:        if(!support_ems) {
                   15411:                REG8(AH) = 0x84;
                   15412:        } else {
                   15413:                REG8(AH) = 0x00;
                   15414:                REG16(BX) = EMS_TOP >> 4;
                   15415:        }
                   15416: }
                   15417: 
                   15418: inline void msdos_int_67h_42h()
                   15419: {
                   15420:        if(!support_ems) {
                   15421:                REG8(AH) = 0x84;
                   15422:        } else {
                   15423:                REG8(AH) = 0x00;
                   15424:                REG16(BX) = free_ems_pages;
                   15425:                REG16(DX) = MAX_EMS_PAGES;
                   15426:        }
                   15427: }
                   15428: 
                   15429: inline void msdos_int_67h_43h()
                   15430: {
                   15431:        if(!support_ems) {
                   15432:                REG8(AH) = 0x84;
                   15433:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15434:                REG8(AH) = 0x87;
                   15435:        } else if(REG16(BX) > free_ems_pages) {
                   15436:                REG8(AH) = 0x88;
                   15437:        } else if(REG16(BX) == 0) {
                   15438:                REG8(AH) = 0x89;
                   15439:        } else {
1.1.1.31  root     15440:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15441:                        if(!ems_handles[i].allocated) {
                   15442:                                ems_allocate_pages(i, REG16(BX));
                   15443:                                REG8(AH) = 0x00;
                   15444:                                REG16(DX) = i;
                   15445:                                return;
                   15446:                        }
                   15447:                }
                   15448:                REG8(AH) = 0x85;
                   15449:        }
                   15450: }
                   15451: 
                   15452: inline void msdos_int_67h_44h()
                   15453: {
                   15454:        if(!support_ems) {
                   15455:                REG8(AH) = 0x84;
1.1.1.31  root     15456:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15457:                REG8(AH) = 0x83;
                   15458:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15459:                REG8(AH) = 0x8a;
                   15460: //     } else if(!(REG8(AL) < 4)) {
                   15461: //             REG8(AH) = 0x8b;
                   15462:        } else if(REG16(BX) == 0xffff) {
                   15463:                ems_unmap_page(REG8(AL) & 3);
                   15464:                REG8(AH) = 0x00;
                   15465:        } else {
                   15466:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15467:                REG8(AH) = 0x00;
                   15468:        }
                   15469: }
                   15470: 
                   15471: inline void msdos_int_67h_45h()
                   15472: {
                   15473:        if(!support_ems) {
                   15474:                REG8(AH) = 0x84;
1.1.1.31  root     15475:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15476:                REG8(AH) = 0x83;
                   15477:        } else {
                   15478:                ems_release_pages(REG16(DX));
                   15479:                REG8(AH) = 0x00;
                   15480:        }
                   15481: }
                   15482: 
                   15483: inline void msdos_int_67h_46h()
                   15484: {
                   15485:        if(!support_ems) {
                   15486:                REG8(AH) = 0x84;
                   15487:        } else {
1.1.1.29  root     15488: //             REG16(AX) = 0x0032; // EMS 3.2
                   15489:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15490:        }
                   15491: }
                   15492: 
                   15493: inline void msdos_int_67h_47h()
                   15494: {
                   15495:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15496:        process_t *process = msdos_process_info_get(current_psp);
                   15497:        
                   15498:        if(!support_ems) {
                   15499:                REG8(AH) = 0x84;
1.1.1.31  root     15500: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15501: //             REG8(AH) = 0x83;
                   15502:        } else if(process->ems_pages_stored) {
                   15503:                REG8(AH) = 0x8d;
                   15504:        } else {
                   15505:                for(int i = 0; i < 4; i++) {
                   15506:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15507:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15508:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15509:                }
                   15510:                process->ems_pages_stored = true;
                   15511:                REG8(AH) = 0x00;
                   15512:        }
                   15513: }
                   15514: 
                   15515: inline void msdos_int_67h_48h()
                   15516: {
                   15517:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15518:        process_t *process = msdos_process_info_get(current_psp);
                   15519:        
                   15520:        if(!support_ems) {
                   15521:                REG8(AH) = 0x84;
1.1.1.31  root     15522: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15523: //             REG8(AH) = 0x83;
                   15524:        } else if(!process->ems_pages_stored) {
                   15525:                REG8(AH) = 0x8e;
                   15526:        } else {
                   15527:                for(int i = 0; i < 4; i++) {
                   15528:                        if(process->ems_pages[i].mapped) {
                   15529:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15530:                        } else {
                   15531:                                ems_unmap_page(i);
                   15532:                        }
                   15533:                }
                   15534:                process->ems_pages_stored = false;
                   15535:                REG8(AH) = 0x00;
                   15536:        }
                   15537: }
                   15538: 
                   15539: inline void msdos_int_67h_4bh()
                   15540: {
                   15541:        if(!support_ems) {
                   15542:                REG8(AH) = 0x84;
                   15543:        } else {
                   15544:                REG8(AH) = 0x00;
                   15545:                REG16(BX) = 0;
1.1.1.31  root     15546:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15547:                        if(ems_handles[i].allocated) {
                   15548:                                REG16(BX)++;
                   15549:                        }
                   15550:                }
                   15551:        }
                   15552: }
                   15553: 
                   15554: inline void msdos_int_67h_4ch()
                   15555: {
                   15556:        if(!support_ems) {
                   15557:                REG8(AH) = 0x84;
1.1.1.31  root     15558:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15559:                REG8(AH) = 0x83;
                   15560:        } else {
                   15561:                REG8(AH) = 0x00;
                   15562:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15563:        }
                   15564: }
                   15565: 
                   15566: inline void msdos_int_67h_4dh()
                   15567: {
                   15568:        if(!support_ems) {
                   15569:                REG8(AH) = 0x84;
                   15570:        } else {
                   15571:                REG8(AH) = 0x00;
                   15572:                REG16(BX) = 0;
1.1.1.31  root     15573:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15574:                        if(ems_handles[i].allocated) {
                   15575:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15576:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15577:                                REG16(BX)++;
                   15578:                        }
                   15579:                }
                   15580:        }
                   15581: }
                   15582: 
1.1.1.20  root     15583: inline void msdos_int_67h_4eh()
                   15584: {
                   15585:        if(!support_ems) {
                   15586:                REG8(AH) = 0x84;
                   15587:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15588:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15589:                        // save page map
                   15590:                        for(int i = 0; i < 4; i++) {
                   15591:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15592:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15593:                        }
                   15594:                }
                   15595:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15596:                        // restore page map
                   15597:                        for(int i = 0; i < 4; i++) {
                   15598:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15599:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15600:                                
1.1.1.31  root     15601:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15602:                                        ems_map_page(i, handle, page);
                   15603:                                } else {
                   15604:                                        ems_unmap_page(i);
                   15605:                                }
                   15606:                        }
                   15607:                }
                   15608:                REG8(AH) = 0x00;
                   15609:        } else if(REG8(AL) == 0x03) {
                   15610:                REG8(AH) = 0x00;
1.1.1.21  root     15611:                REG8(AL) = 4 * 4;
                   15612:        } else {
1.1.1.22  root     15613:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.21  root     15614:                REG8(AH) = 0x8f;
                   15615:        }
                   15616: }
                   15617: 
                   15618: inline void msdos_int_67h_4fh()
                   15619: {
                   15620:        if(!support_ems) {
                   15621:                REG8(AH) = 0x84;
                   15622:        } else if(REG8(AL) == 0x00) {
                   15623:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15624:                
                   15625:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15626:                for(int i = 0; i < count; i++) {
                   15627:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15628:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15629:                        
                   15630: //                     if(!(physical < 4)) {
                   15631: //                             REG8(AH) = 0x8b;
                   15632: //                             return;
                   15633: //                     }
                   15634:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15635:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15636:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page   : 0xffff;
1.1.1.21  root     15637:                }
                   15638:                REG8(AH) = 0x00;
                   15639:        } else if(REG8(AL) == 0x01) {
                   15640:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15641:                
                   15642:                for(int i = 0; i < count; i++) {
                   15643:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15644:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15645:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15646:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15647:                        
                   15648: //                     if(!(physical < 4)) {
                   15649: //                             REG8(AH) = 0x8b;
                   15650: //                             return;
                   15651: //                     } else
1.1.1.41  root     15652:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15653:                                ems_map_page(physical & 3, handle, logical);
                   15654:                        } else {
1.1.1.41  root     15655:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15656:                        }
                   15657:                }
                   15658:                REG8(AH) = 0x00;
                   15659:        } else if(REG8(AL) == 0x02) {
                   15660:                REG8(AH) = 0x00;
                   15661:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15662:        } else {
1.1.1.22  root     15663:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20  root     15664:                REG8(AH) = 0x8f;
                   15665:        }
                   15666: }
                   15667: 
                   15668: inline void msdos_int_67h_50h()
                   15669: {
                   15670:        if(!support_ems) {
                   15671:                REG8(AH) = 0x84;
1.1.1.31  root     15672:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15673:                REG8(AH) = 0x83;
                   15674:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15675:                for(int i = 0; i < REG16(CX); i++) {
                   15676:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15677:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15678:                        
                   15679:                        if(REG8(AL) == 0x01) {
                   15680:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15681:                        }
                   15682: //                     if(!(physical < 4)) {
                   15683: //                             REG8(AH) = 0x8b;
                   15684: //                             return;
                   15685: //                     } else
                   15686:                        if(logical == 0xffff) {
                   15687:                                ems_unmap_page(physical & 3);
                   15688:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15689:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15690:                        } else {
                   15691:                                REG8(AH) = 0x8a;
                   15692:                                return;
                   15693:                        }
                   15694:                }
                   15695:                REG8(AH) = 0x00;
                   15696:        } else {
1.1.1.22  root     15697:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20  root     15698:                REG8(AH) = 0x8f;
                   15699:        }
                   15700: }
                   15701: 
1.1.1.19  root     15702: inline void msdos_int_67h_51h()
                   15703: {
                   15704:        if(!support_ems) {
                   15705:                REG8(AH) = 0x84;
1.1.1.31  root     15706:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15707:                REG8(AH) = 0x83;
                   15708:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15709:                REG8(AH) = 0x87;
                   15710:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15711:                REG8(AH) = 0x88;
                   15712:        } else {
                   15713:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15714:                REG8(AH) = 0x00;
                   15715:        }
                   15716: }
                   15717: 
1.1.1.20  root     15718: inline void msdos_int_67h_52h()
                   15719: {
                   15720:        if(!support_ems) {
                   15721:                REG8(AH) = 0x84;
1.1.1.31  root     15722: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15723: //             REG8(AH) = 0x83;
1.1.1.20  root     15724:        } else if(REG8(AL) == 0x00) {
                   15725:                REG8(AL) = 0x00; // handle is volatile
                   15726:                REG8(AH) = 0x00;
                   15727:        } else if(REG8(AL) == 0x01) {
                   15728:                if(REG8(BL) == 0x00) {
                   15729:                        REG8(AH) = 0x00;
                   15730:                } else {
                   15731:                        REG8(AH) = 0x90; // undefined attribute type
                   15732:                }
                   15733:        } else if(REG8(AL) == 0x02) {
                   15734:                REG8(AL) = 0x00; // only volatile handles supported
                   15735:                REG8(AH) = 0x00;
                   15736:        } else {
1.1.1.22  root     15737:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20  root     15738:                REG8(AH) = 0x8f;
                   15739:        }
                   15740: }
                   15741: 
1.1.1.19  root     15742: inline void msdos_int_67h_53h()
                   15743: {
                   15744:        if(!support_ems) {
                   15745:                REG8(AH) = 0x84;
1.1.1.31  root     15746:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15747:                REG8(AH) = 0x83;
                   15748:        } else if(REG8(AL) == 0x00) {
                   15749:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15750:                REG8(AH) = 0x00;
                   15751:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15752:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15753:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15754:                                REG8(AH) = 0xa1;
                   15755:                                return;
                   15756:                        }
                   15757:                }
                   15758:                REG8(AH) = 0x00;
                   15759:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15760:        } else {
1.1.1.22  root     15761:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20  root     15762:                REG8(AH) = 0x8f;
1.1.1.19  root     15763:        }
                   15764: }
                   15765: 
                   15766: inline void msdos_int_67h_54h()
                   15767: {
                   15768:        if(!support_ems) {
                   15769:                REG8(AH) = 0x84;
                   15770:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15771:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15772:                        if(ems_handles[i].allocated) {
                   15773:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15774:                        } else {
                   15775:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15776:                        }
                   15777:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15778:                }
                   15779:                REG8(AH) = 0x00;
                   15780:                REG8(AL) = MAX_EMS_HANDLES;
                   15781:        } else if(REG8(AL) == 0x01) {
                   15782:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15783:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15784:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15785:                                REG8(AH) = 0x00;
                   15786:                                REG16(DX) = i;
                   15787:                                break;
                   15788:                        }
                   15789:                }
                   15790:        } else if(REG8(AL) == 0x02) {
                   15791:                REG8(AH) = 0x00;
                   15792:                REG16(BX) = MAX_EMS_HANDLES;
                   15793:        } else {
1.1.1.22  root     15794:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20  root     15795:                REG8(AH) = 0x8f;
                   15796:        }
                   15797: }
                   15798: 
1.1.1.49  root     15799: inline void msdos_int_67h_55h()
                   15800: {
                   15801:        if(!support_ems) {
                   15802:                REG8(AH) = 0x84;
                   15803:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15804:                REG8(AH) = 0x83;
                   15805:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15806:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15807:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15808:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15809:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15810:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15811:                
                   15812:                for(int i = 0; i < (int)entries; i++) {
                   15813:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15814:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15815:                        
                   15816:                        if(REG8(AL) == 0x01) {
                   15817:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15818:                        }
                   15819: //                     if(!(physical < 4)) {
                   15820: //                             REG8(AH) = 0x8b;
                   15821: //                             return;
                   15822: //                     } else
                   15823:                        if(logical == 0xffff) {
                   15824:                                ems_unmap_page(physical & 3);
                   15825:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15826:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15827:                        } else {
                   15828:                                REG8(AH) = 0x8a;
                   15829:                                return;
                   15830:                        }
                   15831:                }
                   15832:                i386_jmp_far(jump_seg, jump_ofs);
                   15833:                REG8(AH) = 0x00;
                   15834:        } else {
                   15835:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   15836:                REG8(AH) = 0x8f;
                   15837:        }
                   15838: }
                   15839: 
                   15840: inline void msdos_int_67h_56h()
                   15841: {
                   15842:        if(!support_ems) {
                   15843:                REG8(AH) = 0x84;
                   15844:        } else if(REG8(AL) == 0x02) {
                   15845:                REG16(BX) = (2 + 2) * 4;
                   15846:                REG8(AH) = 0x00;
                   15847:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15848:                REG8(AH) = 0x83;
                   15849:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15850:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15851:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15852:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15853:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15854:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15855: #if 0
                   15856:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15857:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15858:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15859: #endif
                   15860:                UINT16 handles[4], pages[4];
                   15861:                
                   15862:                // alter page map and call routine is at fffc:001f
                   15863:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15864:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15865:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15866:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15867:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15868:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15869:                } else {
                   15870:                        // invalid call addr :-(
                   15871:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15872:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15873:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15874:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15875:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15876:                }
                   15877:                // do call far (push cs/ip) in old mapping
                   15878:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15879:                
                   15880:                // get old mapping data
                   15881: #if 0
                   15882:                for(int i = 0; i < (int)old_entries; i++) {
                   15883:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15884:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15885:                        
                   15886:                        if(REG8(AL) == 0x01) {
                   15887:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15888:                        }
                   15889: //                     if(!(physical < 4)) {
                   15890: //                             REG8(AH) = 0x8b;
                   15891: //                             return;
                   15892: //                     } else
                   15893:                        if(logical == 0xffff) {
                   15894:                                ems_unmap_page(physical & 3);
                   15895:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15896:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15897:                        } else {
                   15898:                                REG8(AH) = 0x8a;
                   15899:                                return;
                   15900:                        }
                   15901:                }
                   15902: #endif
                   15903:                for(int i = 0; i < 4; i++) {
                   15904:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15905:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15906:                }
                   15907:                
                   15908:                // set new mapping
                   15909:                for(int i = 0; i < (int)new_entries; i++) {
                   15910:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15911:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15912:                        
                   15913:                        if(REG8(AL) == 0x01) {
                   15914:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15915:                        }
                   15916: //                     if(!(physical < 4)) {
                   15917: //                             REG8(AH) = 0x8b;
                   15918: //                             return;
                   15919: //                     } else
                   15920:                        if(logical == 0xffff) {
                   15921:                                ems_unmap_page(physical & 3);
                   15922:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15923:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15924:                        } else {
                   15925:                                REG8(AH) = 0x8a;
                   15926:                                return;
                   15927:                        }
                   15928:                }
                   15929:                
                   15930:                // push old mapping data in new mapping
                   15931:                for(int i = 0; i < 4; i++) {
                   15932:                        i386_push16(handles[i]);
                   15933:                        i386_push16(pages  [i]);
                   15934:                }
                   15935:                REG8(AH) = 0x00;
                   15936:        } else {
                   15937:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   15938:                REG8(AH) = 0x8f;
                   15939:        }
                   15940: }
                   15941: 
1.1.1.20  root     15942: inline void msdos_int_67h_57h_tmp()
                   15943: {
                   15944:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15945:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15946:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15947:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15948:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15949:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15950:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15951:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15952:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15953:        
1.1.1.32  root     15954:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15955:        UINT32 src_addr, dest_addr;
                   15956:        UINT32 src_addr_max, dest_addr_max;
                   15957:        
                   15958:        if(src_type == 0) {
                   15959:                src_buffer = mem;
                   15960:                src_addr = (src_seg << 4) + src_ofs;
                   15961:                src_addr_max = MAX_MEM;
                   15962:        } else {
1.1.1.31  root     15963:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15964:                        REG8(AH) = 0x83;
                   15965:                        return;
                   15966:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15967:                        REG8(AH) = 0x8a;
                   15968:                        return;
                   15969:                }
1.1.1.32  root     15970:                if(ems_handles[src_handle].buffer != NULL) {
                   15971:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   15972:                }
1.1.1.20  root     15973:                src_addr = src_ofs;
1.1.1.32  root     15974:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     15975:        }
                   15976:        if(dest_type == 0) {
                   15977:                dest_buffer = mem;
                   15978:                dest_addr = (dest_seg << 4) + dest_ofs;
                   15979:                dest_addr_max = MAX_MEM;
                   15980:        } else {
1.1.1.31  root     15981:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     15982:                        REG8(AH) = 0x83;
                   15983:                        return;
                   15984:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   15985:                        REG8(AH) = 0x8a;
                   15986:                        return;
                   15987:                }
1.1.1.32  root     15988:                if(ems_handles[dest_handle].buffer != NULL) {
                   15989:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   15990:                }
1.1.1.20  root     15991:                dest_addr = dest_ofs;
1.1.1.32  root     15992:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     15993:        }
1.1.1.32  root     15994:        if(src_buffer != NULL && dest_buffer != NULL) {
                   15995:                for(int i = 0; i < copy_length; i++) {
                   15996:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15997:                                if(REG8(AL) == 0x00) {
                   15998:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15999:                                } else if(REG8(AL) == 0x01) {
                   16000:                                        UINT8 tmp = dest_buffer[dest_addr];
                   16001:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   16002:                                        src_buffer[src_addr++] = tmp;
                   16003:                                }
                   16004:                        } else {
                   16005:                                REG8(AH) = 0x93;
                   16006:                                return;
1.1.1.20  root     16007:                        }
                   16008:                }
1.1.1.32  root     16009:                REG8(AH) = 0x00;
                   16010:        } else {
                   16011:                REG8(AH) = 0x80;
1.1.1.20  root     16012:        }
                   16013: }
                   16014: 
                   16015: inline void msdos_int_67h_57h()
                   16016: {
                   16017:        if(!support_ems) {
                   16018:                REG8(AH) = 0x84;
                   16019:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   16020:                struct {
                   16021:                        UINT16 handle;
                   16022:                        UINT16 page;
                   16023:                        bool mapped;
                   16024:                } tmp_pages[4];
                   16025:                
                   16026:                // unmap pages to copy memory data to ems buffer
                   16027:                for(int i = 0; i < 4; i++) {
                   16028:                        tmp_pages[i].handle = ems_pages[i].handle;
                   16029:                        tmp_pages[i].page   = ems_pages[i].page;
                   16030:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   16031:                        ems_unmap_page(i);
                   16032:                }
                   16033:                
                   16034:                // run move/exchange operation
                   16035:                msdos_int_67h_57h_tmp();
                   16036:                
                   16037:                // restore unmapped pages
                   16038:                for(int i = 0; i < 4; i++) {
                   16039:                        if(tmp_pages[i].mapped) {
                   16040:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   16041:                        }
                   16042:                }
                   16043:        } else {
1.1.1.22  root     16044:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20  root     16045:                REG8(AH) = 0x8f;
                   16046:        }
                   16047: }
                   16048: 
                   16049: inline void msdos_int_67h_58h()
                   16050: {
                   16051:        if(!support_ems) {
                   16052:                REG8(AH) = 0x84;
                   16053:        } else if(REG8(AL) == 0x00) {
                   16054:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     16055:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   16056:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     16057:                }
                   16058:                REG8(AH) = 0x00;
                   16059:                REG16(CX) = 4;
                   16060:        } else if(REG8(AL) == 0x01) {
                   16061:                REG8(AH) = 0x00;
                   16062:                REG16(CX) = 4;
                   16063:        } else {
1.1.1.22  root     16064:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20  root     16065:                REG8(AH) = 0x8f;
                   16066:        }
                   16067: }
                   16068: 
1.1.1.42  root     16069: inline void msdos_int_67h_59h()
                   16070: {
                   16071:        if(!support_ems) {
                   16072:                REG8(AH) = 0x84;
                   16073:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     16074:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   16075:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   16076:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   16077:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   16078:                REG8(AH) = 0x00;
                   16079: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     16080:        } else if(REG8(AL) == 0x01) {
                   16081:                REG8(AH) = 0x00;
                   16082:                REG16(BX) = free_ems_pages;
                   16083:                REG16(DX) = MAX_EMS_PAGES;
                   16084:        } else {
                   16085:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   16086:                REG8(AH) = 0x8f;
                   16087:        }
                   16088: }
                   16089: 
1.1.1.20  root     16090: inline void msdos_int_67h_5ah()
                   16091: {
                   16092:        if(!support_ems) {
1.1.1.19  root     16093:                REG8(AH) = 0x84;
1.1.1.20  root     16094:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   16095:                REG8(AH) = 0x87;
                   16096:        } else if(REG16(BX) > free_ems_pages) {
                   16097:                REG8(AH) = 0x88;
                   16098: //     } else if(REG16(BX) == 0) {
                   16099: //             REG8(AH) = 0x89;
                   16100:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     16101:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     16102:                        if(!ems_handles[i].allocated) {
                   16103:                                ems_allocate_pages(i, REG16(BX));
                   16104:                                REG8(AH) = 0x00;
                   16105:                                REG16(DX) = i;
                   16106:                                return;
                   16107:                        }
                   16108:                }
                   16109:                REG8(AH) = 0x85;
                   16110:        } else {
1.1.1.22  root     16111:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20  root     16112:                REG8(AH) = 0x8f;
1.1.1.19  root     16113:        }
                   16114: }
                   16115: 
1.1.1.49  root     16116: inline void msdos_int_67h_5bh()
                   16117: {
                   16118:        static UINT8  stored_bl = 0x00;
                   16119:        static UINT16 stored_es = 0x0000;
                   16120:        static UINT16 stored_di = 0x0000;
                   16121:        
                   16122:        if(!support_ems) {
                   16123:                REG8(AH) = 0x84;
                   16124:        } else if(REG8(AL) == 0x00) {
                   16125:                if(stored_bl == 0x00) {
                   16126:                        if(!(stored_es == 0 && stored_di == 0)) {
                   16127:                                for(int i = 0; i < 4; i++) {
                   16128:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   16129:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   16130:                                }
                   16131:                        }
                   16132:                        SREG(ES) = stored_es;
                   16133:                        i386_load_segment_descriptor(ES);
                   16134:                        REG16(DI) = stored_di;
                   16135:                } else {
                   16136:                        REG8(BL) = stored_bl;
                   16137:                }
                   16138:                REG8(AH) = 0x00;
                   16139:        } else if(REG8(AL) == 0x01) {
                   16140:                if(REG8(BL) == 0x00) {
                   16141:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   16142:                                for(int i = 0; i < 4; i++) {
                   16143:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   16144:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   16145:                                        
                   16146:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   16147:                                                ems_map_page(i, handle, page);
                   16148:                                        } else {
                   16149:                                                ems_unmap_page(i);
                   16150:                                        }
                   16151:                                }
                   16152:                        }
                   16153:                }
                   16154:                stored_bl = REG8(BL);
                   16155:                stored_es = SREG(ES);
                   16156:                stored_di = REG16(DI);
                   16157:                REG8(AH) = 0x00;
                   16158:        } else if(REG8(AL) == 0x02) {
                   16159:                REG16(DX) = 4 * 4;
                   16160:                REG8(AH) = 0x00;
                   16161:        } else if(REG8(AL) == 0x03) {
                   16162:                REG8(BL) = 0x00; // not supported
                   16163:                REG8(AH) = 0x00;
                   16164:        } else if(REG8(AL) == 0x04) {
                   16165:                REG8(AH) = 0x00;
                   16166:        } else if(REG8(AL) == 0x05) {
                   16167:                REG8(BL) = 0x00; // not supported
                   16168:                REG8(AH) = 0x00;
                   16169:        } else {
                   16170:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   16171:                REG8(AH) = 0x8f;
                   16172:        }
                   16173: }
                   16174: 
1.1.1.43  root     16175: inline void msdos_int_67h_5dh()
                   16176: {
                   16177:        if(!support_ems) {
                   16178:                REG8(AH) = 0x84;
                   16179:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   16180:                REG8(AH) = 0xa4; // operating system denied access
                   16181:        } else {
                   16182:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   16183:                REG8(AH) = 0x8f;
                   16184:        }
                   16185: }
                   16186: 
1.1.1.49  root     16187: inline void msdos_int_67h_70h()
                   16188: {
                   16189:        if(!support_ems) {
                   16190:                REG8(AH) = 0x84;
                   16191:        } else if(REG8(AL) == 0x00) {
                   16192:                REG8(AL) = 0x00;
                   16193:                REG8(AH) = 0x00;
                   16194:        } else if(REG8(AL) == 0x01) {
                   16195:                REG8(AL) = 0x00;
                   16196: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   16197:                REG8(AH) = 0x00;
                   16198:        } else {
                   16199:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   16200:                REG8(AH) = 0x8f;
                   16201:        }
                   16202: }
                   16203: 
1.1.1.30  root     16204: inline void msdos_int_67h_deh()
                   16205: {
                   16206:        REG8(AH) = 0x84;
                   16207: }
                   16208: 
1.1.1.19  root     16209: #ifdef SUPPORT_XMS
                   16210: 
1.1.1.32  root     16211: void msdos_xms_init()
1.1.1.26  root     16212: {
1.1.1.30  root     16213:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16214:        emb_handle_top->address = EMB_TOP;
                   16215:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     16216:        xms_a20_local_enb_count = 0;
                   16217: }
                   16218: 
1.1.1.32  root     16219: void msdos_xms_finish()
                   16220: {
                   16221:        msdos_xms_release();
                   16222: }
                   16223: 
                   16224: void msdos_xms_release()
1.1.1.30  root     16225: {
                   16226:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   16227:                emb_handle_t *next_handle = emb_handle->next;
                   16228:                free(emb_handle);
                   16229:                emb_handle = next_handle;
                   16230:        }
                   16231: }
                   16232: 
                   16233: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   16234: {
                   16235:        if(handle != 0) {
                   16236:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16237:                        if(emb_handle->handle == handle) {
                   16238:                                return(emb_handle);
                   16239:                        }
                   16240:                }
                   16241:        }
                   16242:        return(NULL);
                   16243: }
                   16244: 
                   16245: int msdos_xms_get_unused_emb_handle_id()
                   16246: {
                   16247:        for(int handle = 1;; handle++) {
                   16248:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   16249:                        return(handle);
                   16250:                }
                   16251:        }
                   16252:        return(0);
                   16253: }
                   16254: 
                   16255: int msdos_xms_get_unused_emb_handle_count()
                   16256: {
                   16257:        int count = 64; //255;
                   16258:        
                   16259:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16260:                if(emb_handle->handle != 0) {
                   16261:                        if(--count == 1) {
                   16262:                                break;
                   16263:                        }
                   16264:                }
                   16265:        }
                   16266:        return(count);
                   16267: }
                   16268: 
                   16269: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   16270: {
                   16271:        if(emb_handle->size_kb > size_kb) {
                   16272:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16273:                
                   16274:                new_handle->address = emb_handle->address + size_kb * 1024;
                   16275:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   16276:                emb_handle->size_kb = size_kb;
                   16277:                
                   16278:                new_handle->prev = emb_handle;
                   16279:                new_handle->next = emb_handle->next;
                   16280:                if(emb_handle->next != NULL) {
                   16281:                        emb_handle->next->prev = new_handle;
                   16282:                }
                   16283:                emb_handle->next = new_handle;
                   16284:        }
                   16285: }
                   16286: 
                   16287: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   16288: {
                   16289:        emb_handle_t *next_handle = emb_handle->next;
                   16290:        
                   16291:        if(next_handle != NULL) {
                   16292:                emb_handle->size_kb += next_handle->size_kb;
                   16293:                
                   16294:                if(next_handle->next != NULL) {
                   16295:                        next_handle->next->prev = emb_handle;
                   16296:                }
                   16297:                emb_handle->next = next_handle->next;
                   16298:                free(next_handle);
                   16299:        }
                   16300: }
                   16301: 
                   16302: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   16303: {
                   16304:        emb_handle_t *target_handle = NULL;
                   16305:        
                   16306:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16307:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   16308:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   16309:                                target_handle = emb_handle;
                   16310:                        }
                   16311:                }
                   16312:        }
                   16313:        if(target_handle != NULL) {
                   16314:                if(target_handle->size_kb > size_kb) {
                   16315:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   16316:                }
                   16317: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16318:                return(target_handle);
                   16319:        }
                   16320:        return(NULL);
                   16321: }
                   16322: 
                   16323: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   16324: {
                   16325:        emb_handle_t *prev_handle = emb_handle->prev;
                   16326:        emb_handle_t *next_handle = emb_handle->next;
                   16327:        
                   16328:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   16329:                msdos_xms_combine_emb_handles(prev_handle);
                   16330:                emb_handle = prev_handle;
                   16331:        }
                   16332:        if(next_handle != NULL && next_handle->handle == 0) {
                   16333:                msdos_xms_combine_emb_handles(emb_handle);
                   16334:        }
                   16335:        emb_handle->handle = 0;
                   16336: }
                   16337: 
1.1.1.19  root     16338: inline void msdos_call_xms_00h()
                   16339: {
1.1.1.29  root     16340: #if defined(HAS_I386)
                   16341:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     16342: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     16343:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   16344: #else
                   16345:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16346:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16347: #endif
                   16348: //     REG16(DX) = 0x0000; // HMA does not exist
                   16349:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16350: }
                   16351: 
                   16352: inline void msdos_call_xms_01h()
                   16353: {
1.1.1.29  root     16354:        if(REG8(AL) == 0x40) {
                   16355:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16356:                // DX=KB free extended memory returned by last call of function 08h
                   16357:                REG16(AX) = 0x0000;
                   16358:                REG8(BL) = 0x91;
                   16359:                REG16(DX) = xms_dx_after_call_08h;
                   16360:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16361:                REG16(AX) = 0x0000;
                   16362:                REG8(BL) = 0x81; // Vdisk was detected
                   16363: #ifdef SUPPORT_HMA
                   16364:        } else if(is_hma_used_by_int_2fh) {
                   16365:                REG16(AX) = 0x0000;
                   16366:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16367:        } else if(is_hma_used_by_xms) {
                   16368:                REG16(AX) = 0x0000;
                   16369:                REG8(BL) = 0x91; // HMA is already in use
                   16370:        } else {
                   16371:                REG16(AX) = 0x0001;
                   16372:                is_hma_used_by_xms = true;
                   16373: #else
                   16374:        } else {
                   16375:                REG16(AX) = 0x0000;
                   16376:                REG8(BL) = 0x91; // HMA is already in use
                   16377: #endif
                   16378:        }
1.1.1.19  root     16379: }
                   16380: 
                   16381: inline void msdos_call_xms_02h()
                   16382: {
1.1.1.29  root     16383:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16384:                REG16(AX) = 0x0000;
                   16385:                REG8(BL) = 0x81; // Vdisk was detected
                   16386: #ifdef SUPPORT_HMA
                   16387:        } else if(is_hma_used_by_int_2fh) {
                   16388:                REG16(AX) = 0x0000;
                   16389:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16390:        } else if(!is_hma_used_by_xms) {
                   16391:                REG16(AX) = 0x0000;
                   16392:                REG8(BL) = 0x93; // HMA is not allocated
                   16393:        } else {
                   16394:                REG16(AX) = 0x0001;
                   16395:                is_hma_used_by_xms = false;
                   16396:                // restore first free mcb in high memory area
                   16397:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16398: #else
                   16399:        } else {
                   16400:                REG16(AX) = 0x0000;
                   16401:                REG8(BL) = 0x91; // HMA is already in use
                   16402: #endif
                   16403:        }
1.1.1.19  root     16404: }
                   16405: 
                   16406: inline void msdos_call_xms_03h()
                   16407: {
                   16408:        i386_set_a20_line(1);
                   16409:        REG16(AX) = 0x0001;
                   16410:        REG8(BL) = 0x00;
                   16411: }
                   16412: 
                   16413: inline void msdos_call_xms_04h()
                   16414: {
1.1.1.21  root     16415:        i386_set_a20_line(0);
                   16416:        REG16(AX) = 0x0001;
                   16417:        REG8(BL) = 0x00;
1.1.1.19  root     16418: }
                   16419: 
                   16420: inline void msdos_call_xms_05h()
                   16421: {
                   16422:        i386_set_a20_line(1);
                   16423:        REG16(AX) = 0x0001;
                   16424:        REG8(BL) = 0x00;
1.1.1.21  root     16425:        xms_a20_local_enb_count++;
1.1.1.19  root     16426: }
                   16427: 
                   16428: void msdos_call_xms_06h()
                   16429: {
1.1.1.21  root     16430:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16431:                if(--xms_a20_local_enb_count == 0) {
                   16432:                        i386_set_a20_line(0);
                   16433:                        REG16(AX) = 0x0001;
                   16434:                        REG8(BL) = 0x00;
                   16435:                } else {
                   16436:                        REG16(AX) = 0x0000;
                   16437:                        REG8(BL) = 0x94;
                   16438:                }
1.1.1.21  root     16439:        } else {
1.1.1.45  root     16440:                i386_set_a20_line(0);
1.1.1.21  root     16441:                REG16(AX) = 0x0001;
                   16442:                REG8(BL) = 0x00;
1.1.1.19  root     16443:        }
                   16444: }
                   16445: 
                   16446: inline void msdos_call_xms_07h()
                   16447: {
                   16448:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16449:        REG8(BL) = 0x00;
                   16450: }
                   16451: 
                   16452: inline void msdos_call_xms_08h()
                   16453: {
1.1.1.45  root     16454:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16455:        
1.1.1.30  root     16456:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16457:                if(emb_handle->handle == 0) {
1.1.1.45  root     16458:                        if(eax < emb_handle->size_kb) {
                   16459:                                eax = emb_handle->size_kb;
1.1.1.19  root     16460:                        }
1.1.1.45  root     16461:                        edx += emb_handle->size_kb;
1.1.1.19  root     16462:                }
                   16463:        }
1.1.1.45  root     16464:        if(eax > 65535) {
                   16465:                eax = 65535;
                   16466:        }
                   16467:        if(edx > 65535) {
                   16468:                edx = 65535;
                   16469:        }
                   16470:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16471:                REG8(BL) = 0xa0;
                   16472:        } else {
                   16473:                REG8(BL) = 0x00;
                   16474:        }
1.1.1.45  root     16475: #if defined(HAS_I386)
                   16476:        REG32(EAX) = eax;
                   16477:        REG32(EDX) = edx;
                   16478: #else
                   16479:        REG16(AX) = (UINT16)eax;
                   16480:        REG16(DX) = (UINT16)edx;
                   16481: #endif
1.1.1.29  root     16482:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16483: }
                   16484: 
1.1.1.30  root     16485: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16486: {
1.1.1.30  root     16487:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16488:        
                   16489:        if(emb_handle != NULL) {
                   16490:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16491:                
                   16492:                REG16(AX) = 0x0001;
                   16493:                REG16(DX) = emb_handle->handle;
                   16494:                REG8(BL) = 0x00;
                   16495:        } else {
                   16496:                REG16(AX) = REG16(DX) = 0x0000;
                   16497:                REG8(BL) = 0xa0;
1.1.1.19  root     16498:        }
1.1.1.30  root     16499: }
                   16500: 
                   16501: inline void msdos_call_xms_09h()
                   16502: {
                   16503:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16504: }
                   16505: 
                   16506: inline void msdos_call_xms_0ah()
                   16507: {
1.1.1.30  root     16508:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16509:        
                   16510:        if(emb_handle == NULL) {
1.1.1.19  root     16511:                REG16(AX) = 0x0000;
                   16512:                REG8(BL) = 0xa2;
1.1.1.45  root     16513: //     } else if(emb_handle->lock > 0) {
                   16514: //             REG16(AX) = 0x0000;
                   16515: //             REG8(BL) = 0xab;
1.1.1.19  root     16516:        } else {
1.1.1.30  root     16517:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16518:                
                   16519:                REG16(AX) = 0x0001;
                   16520:                REG8(BL) = 0x00;
                   16521:        }
                   16522: }
                   16523: 
                   16524: inline void msdos_call_xms_0bh()
                   16525: {
                   16526:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16527:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16528:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16529:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16530:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16531:        
                   16532:        UINT8 *src_buffer, *dest_buffer;
                   16533:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16534:        emb_handle_t *emb_handle;
1.1.1.19  root     16535:        
                   16536:        if(src_handle == 0) {
                   16537:                src_buffer = mem;
                   16538:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16539:                src_addr_max = MAX_MEM;
                   16540:        } else {
1.1.1.30  root     16541:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16542:                        REG16(AX) = 0x0000;
                   16543:                        REG8(BL) = 0xa3;
                   16544:                        return;
                   16545:                }
1.1.1.30  root     16546:                src_buffer = mem + emb_handle->address;
                   16547:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16548:        }
                   16549:        if(dest_handle == 0) {
                   16550:                dest_buffer = mem;
                   16551:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16552:                dest_addr_max = MAX_MEM;
                   16553:        } else {
1.1.1.30  root     16554:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16555:                        REG16(AX) = 0x0000;
                   16556:                        REG8(BL) = 0xa5;
                   16557:                        return;
                   16558:                }
1.1.1.30  root     16559:                dest_buffer = mem + emb_handle->address;
                   16560:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16561:        }
                   16562:        for(int i = 0; i < copy_length; i++) {
                   16563:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16564:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16565:                } else {
                   16566:                        break;
                   16567:                }
                   16568:        }
                   16569:        REG16(AX) = 0x0001;
                   16570:        REG8(BL) = 0x00;
                   16571: }
                   16572: 
                   16573: inline void msdos_call_xms_0ch()
                   16574: {
1.1.1.30  root     16575:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16576:        
                   16577:        if(emb_handle == NULL) {
1.1.1.19  root     16578:                REG16(AX) = 0x0000;
                   16579:                REG8(BL) = 0xa2;
                   16580:        } else {
1.1.1.45  root     16581:                if(emb_handle->lock < 255) {
                   16582:                        emb_handle->lock++;
                   16583:                }
1.1.1.19  root     16584:                REG16(AX) = 0x0001;
1.1.1.30  root     16585:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16586:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16587:        }
                   16588: }
                   16589: 
                   16590: inline void msdos_call_xms_0dh()
                   16591: {
1.1.1.30  root     16592:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16593:        
                   16594:        if(emb_handle == NULL) {
1.1.1.19  root     16595:                REG16(AX) = 0x0000;
                   16596:                REG8(BL) = 0xa2;
1.1.1.30  root     16597:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16598:                REG16(AX) = 0x0000;
                   16599:                REG8(BL) = 0xaa;
                   16600:        } else {
1.1.1.30  root     16601:                emb_handle->lock--;
1.1.1.19  root     16602:                REG16(AX) = 0x0001;
                   16603:                REG8(BL) = 0x00;
                   16604:        }
                   16605: }
                   16606: 
                   16607: inline void msdos_call_xms_0eh()
                   16608: {
1.1.1.30  root     16609:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16610:        
                   16611:        if(emb_handle == NULL) {
1.1.1.19  root     16612:                REG16(AX) = 0x0000;
                   16613:                REG8(BL) = 0xa2;
                   16614:        } else {
                   16615:                REG16(AX) = 0x0001;
1.1.1.30  root     16616:                REG8(BH) = emb_handle->lock;
                   16617:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16618:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16619:        }
                   16620: }
                   16621: 
1.1.1.30  root     16622: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16623: {
1.1.1.30  root     16624:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16625:        
                   16626:        if(emb_handle == NULL) {
1.1.1.19  root     16627:                REG16(AX) = 0x0000;
                   16628:                REG8(BL) = 0xa2;
1.1.1.30  root     16629:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16630:                REG16(AX) = 0x0000;
                   16631:                REG8(BL) = 0xab;
                   16632:        } else {
1.1.1.30  root     16633:                if(emb_handle->size_kb < size_kb) {
                   16634:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16635:                                msdos_xms_combine_emb_handles(emb_handle);
                   16636:                                if(emb_handle->size_kb > size_kb) {
                   16637:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16638:                                }
                   16639:                        } else {
                   16640:                                int old_handle = emb_handle->handle;
                   16641:                                int old_size_kb = emb_handle->size_kb;
                   16642:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16643:                                
                   16644:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16645:                                msdos_xms_free_emb_handle(emb_handle);
                   16646:                                
                   16647:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16648:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16649:                                }
                   16650:                                emb_handle->handle = old_handle;
                   16651:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16652:                                free(buffer);
                   16653:                        }
                   16654:                } else if(emb_handle->size_kb > size_kb) {
                   16655:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16656:                }
                   16657:                if(emb_handle->size_kb != size_kb) {
                   16658:                        REG16(AX) = 0x0000;
                   16659:                        REG8(BL) = 0xa0;
                   16660:                } else {
                   16661:                        REG16(AX) = 0x0001;
                   16662:                        REG8(BL) = 0x00;
                   16663:                }
1.1.1.19  root     16664:        }
                   16665: }
                   16666: 
1.1.1.30  root     16667: inline void msdos_call_xms_0fh()
                   16668: {
                   16669:        msdos_call_xms_0fh(REG16(BX));
                   16670: }
                   16671: 
1.1.1.19  root     16672: inline void msdos_call_xms_10h()
                   16673: {
                   16674:        int seg;
                   16675:        
                   16676:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16677:                REG16(AX) = 0x0001;
                   16678:                REG16(BX) = seg;
                   16679:        } else {
                   16680:                REG16(AX) = 0x0000;
                   16681:                REG8(BL) = 0xb0;
                   16682:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16683:        }
                   16684: }
                   16685: 
                   16686: inline void msdos_call_xms_11h()
                   16687: {
                   16688:        int mcb_seg = REG16(DX) - 1;
                   16689:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16690:        
                   16691:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16692:                msdos_mem_free(REG16(DX));
                   16693:                REG16(AX) = 0x0001;
                   16694:                REG8(BL) = 0x00;
                   16695:        } else {
                   16696:                REG16(AX) = 0x0000;
                   16697:                REG8(BL) = 0xb2;
                   16698:        }
                   16699: }
                   16700: 
                   16701: inline void msdos_call_xms_12h()
                   16702: {
                   16703:        int mcb_seg = REG16(DX) - 1;
                   16704:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16705:        int max_paragraphs;
                   16706:        
                   16707:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16708:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16709:                        REG16(AX) = 0x0001;
                   16710:                        REG8(BL) = 0x00;
                   16711:                } else {
                   16712:                        REG16(AX) = 0x0000;
                   16713:                        REG8(BL) = 0xb0;
                   16714:                        REG16(DX) = max_paragraphs;
                   16715:                }
                   16716:        } else {
                   16717:                REG16(AX) = 0x0000;
                   16718:                REG8(BL) = 0xb2;
                   16719:        }
                   16720: }
                   16721: 
1.1.1.29  root     16722: #if defined(HAS_I386)
                   16723: 
                   16724: inline void msdos_call_xms_88h()
                   16725: {
                   16726:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16727:        
1.1.1.30  root     16728:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16729:                if(emb_handle->handle == 0) {
                   16730:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16731:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16732:                        }
1.1.1.30  root     16733:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16734:                }
                   16735:        }
                   16736:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16737:                REG8(BL) = 0xa0;
                   16738:        } else {
                   16739:                REG8(BL) = 0x00;
                   16740:        }
                   16741:        REG32(ECX) = EMB_END - 1;
                   16742: }
                   16743: 
                   16744: inline void msdos_call_xms_89h()
                   16745: {
1.1.1.30  root     16746:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16747: }
                   16748: 
                   16749: inline void msdos_call_xms_8eh()
                   16750: {
1.1.1.30  root     16751:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16752:        
                   16753:        if(emb_handle == NULL) {
1.1.1.29  root     16754:                REG16(AX) = 0x0000;
                   16755:                REG8(BL) = 0xa2;
                   16756:        } else {
                   16757:                REG16(AX) = 0x0001;
1.1.1.30  root     16758:                REG8(BH) = emb_handle->lock;
                   16759:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16760:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16761:        }
                   16762: }
                   16763: 
                   16764: inline void msdos_call_xms_8fh()
                   16765: {
1.1.1.30  root     16766:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16767: }
                   16768: 
                   16769: #endif
1.1.1.19  root     16770: #endif
                   16771: 
1.1.1.26  root     16772: UINT16 msdos_get_equipment()
                   16773: {
                   16774:        static UINT16 equip = 0;
                   16775:        
                   16776:        if(equip == 0) {
                   16777: #ifdef SUPPORT_FPU
                   16778:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16779: #endif
                   16780:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16781:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16782: //             equip |= (1 << 8);      // 0 if DMA installed
                   16783:                equip |= (2 << 9);      // number of serial ports
                   16784:                equip |= (3 << 14);     // number of printer ports (NOTE: this number is 3 on Windows 98 SE though only LPT1 exists)
1.1.1.28  root     16785:                
                   16786:                // check only A: and B: if it is floppy drive
                   16787:                int n = 0;
                   16788:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16789:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16790:                                n++;
1.1.1.28  root     16791:                        }
                   16792:                }
                   16793:                if(n != 0) {
                   16794:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16795:                        n--;
                   16796:                        equip |= (n << 6);      // number of floppies installed less 1
                   16797:                }
                   16798: //             if(joyGetNumDevs() != 0) {
                   16799: //                     equip |= (1 << 12);     // game port installed
                   16800: //             }
1.1.1.26  root     16801:        }
                   16802:        return(equip);
                   16803: }
                   16804: 
1.1       root     16805: void msdos_syscall(unsigned num)
                   16806: {
1.1.1.22  root     16807: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16808:        if(num == 0x08 || num == 0x1c) {
                   16809:                // don't log the timer interrupts
1.1.1.45  root     16810: //             fprintf(fp_debug_log, "int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.50  root     16811:        } else if(num == 0x30) {
                   16812:                // dummy interrupt for call 0005h (call near)
                   16813:                fprintf(fp_debug_log, "call 0005h (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.59  root     16814:        } else if(num == 0x65) {
1.1.1.22  root     16815:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16816:                fprintf(fp_debug_log, "int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.59  root     16817:        } else if(num == 0x66) {
1.1.1.22  root     16818:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16819:                fprintf(fp_debug_log, "call XMS (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.59  root     16820:        } else if(num >= 0x68 && num <= 0x6f) {
1.1.1.45  root     16821:                // dummy interrupt
1.1.1.22  root     16822:        } else {
1.1.1.33  root     16823:                fprintf(fp_debug_log, "int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.22  root     16824:        }
                   16825: #endif
1.1.1.36  root     16826:        // update cursor position
                   16827:        if(cursor_moved) {
                   16828:                pcbios_update_cursor_position();
                   16829:                cursor_moved = false;
                   16830:        }
1.1.1.50  root     16831: #ifdef USE_SERVICE_THREAD
                   16832:        // this is called from dummy loop to wait until a serive that waits input is done
                   16833:        if(!in_service)
                   16834: #endif
1.1.1.33  root     16835:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16836:        
1.1       root     16837:        switch(num) {
                   16838:        case 0x00:
1.1.1.28  root     16839:                try {
                   16840:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16841:                        error("division by zero\n");
                   16842:                } catch(...) {
                   16843:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16844:                }
1.1       root     16845:                break;
                   16846:        case 0x04:
1.1.1.28  root     16847:                try {
                   16848:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16849:                        error("overflow\n");
                   16850:                } catch(...) {
                   16851:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16852:                }
1.1       root     16853:                break;
                   16854:        case 0x06:
                   16855:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16856:                if(!ignore_illegal_insn) {
1.1.1.28  root     16857:                        try {
                   16858:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16859:                                error("illegal instruction\n");
                   16860:                        } catch(...) {
                   16861:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16862:                        }
1.1.1.14  root     16863:                } else {
                   16864: #if defined(HAS_I386)
1.1.1.39  root     16865:                        m_eip = m_int6h_skip_eip;
                   16866: #elif defined(HAS_I286)
                   16867:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16868: #else
1.1.1.39  root     16869:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16870: #endif
                   16871:                }
1.1       root     16872:                break;
1.1.1.33  root     16873:        case 0x09:
                   16874:                // ctrl-break is pressed
                   16875:                if(raise_int_1bh) {
                   16876: #if defined(HAS_I386)
                   16877:                        m_ext = 0; // not an external interrupt
                   16878:                        i386_trap(0x1b, 1, 0);
                   16879:                        m_ext = 1;
                   16880: #else
                   16881:                        PREFIX86(_interrupt)(0x1b);
                   16882: #endif
                   16883:                        raise_int_1bh = false;
                   16884:                }
1.1.1.8   root     16885:        case 0x08:
1.1.1.14  root     16886: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16887:        case 0x0b:
                   16888:        case 0x0c:
                   16889:        case 0x0d:
                   16890:        case 0x0e:
                   16891:        case 0x0f:
                   16892:                // EOI
                   16893:                pic[0].isr &= ~(1 << (num - 0x08));
                   16894:                pic_update();
                   16895:                break;
1.1       root     16896:        case 0x10:
                   16897:                // PC BIOS - Video
1.1.1.14  root     16898:                if(!restore_console_on_exit) {
1.1.1.15  root     16899:                        change_console_size(scr_width, scr_height);
1.1       root     16900:                }
1.1.1.3   root     16901:                m_CF = 0;
1.1       root     16902:                switch(REG8(AH)) {
1.1.1.16  root     16903:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16904:                case 0x01: pcbios_int_10h_01h(); break;
                   16905:                case 0x02: pcbios_int_10h_02h(); break;
                   16906:                case 0x03: pcbios_int_10h_03h(); break;
                   16907:                case 0x05: pcbios_int_10h_05h(); break;
                   16908:                case 0x06: pcbios_int_10h_06h(); break;
                   16909:                case 0x07: pcbios_int_10h_07h(); break;
                   16910:                case 0x08: pcbios_int_10h_08h(); break;
                   16911:                case 0x09: pcbios_int_10h_09h(); break;
                   16912:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16913:                case 0x0b: break;
1.1.1.40  root     16914:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16915:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16916:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16917:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16918:                case 0x10: break;
1.1.1.14  root     16919:                case 0x11: pcbios_int_10h_11h(); break;
                   16920:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16921:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16922:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16923:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16924:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16925:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16926:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16927:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16928:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16929:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16930:                case 0x6f: break;
1.1.1.22  root     16931:                case 0x80: m_CF = 1; break; // unknown
                   16932:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16933:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16934:                case 0x83: pcbios_int_10h_83h(); break;
                   16935:                case 0x8b: break;
                   16936:                case 0x8c: m_CF = 1; break; // unknown
                   16937:                case 0x8d: m_CF = 1; break; // unknown
                   16938:                case 0x8e: m_CF = 1; break; // unknown
                   16939:                case 0x90: pcbios_int_10h_90h(); break;
                   16940:                case 0x91: pcbios_int_10h_91h(); break;
                   16941:                case 0x92: break;
                   16942:                case 0x93: break;
                   16943:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16944:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16945:                case 0xfe: pcbios_int_10h_feh(); break;
                   16946:                case 0xff: pcbios_int_10h_ffh(); break;
                   16947:                default:
1.1.1.22  root     16948:                        unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   16949:                        m_CF = 1;
1.1       root     16950:                        break;
                   16951:                }
                   16952:                break;
                   16953:        case 0x11:
                   16954:                // PC BIOS - Get Equipment List
1.1.1.26  root     16955:                REG16(AX) = msdos_get_equipment();
1.1       root     16956:                break;
                   16957:        case 0x12:
                   16958:                // PC BIOS - Get Memory Size
1.1.1.33  root     16959:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16960:                break;
                   16961:        case 0x13:
1.1.1.42  root     16962:                // PC BIOS - Disk I/O
                   16963:                {
                   16964:                        static UINT8 last = 0x00;
                   16965:                        switch(REG8(AH)) {
                   16966:                        case 0x00: pcbios_int_13h_00h(); break;
                   16967:                        case 0x01: // get last status
                   16968:                                REG8(AH) = last;
                   16969:                                break;
                   16970:                        case 0x02: pcbios_int_13h_02h(); break;
                   16971:                        case 0x03: pcbios_int_13h_03h(); break;
                   16972:                        case 0x04: pcbios_int_13h_04h(); break;
                   16973:                        case 0x08: pcbios_int_13h_08h(); break;
                   16974:                        case 0x0a: pcbios_int_13h_02h(); break;
                   16975:                        case 0x0b: pcbios_int_13h_03h(); break;
                   16976:                        case 0x0d: pcbios_int_13h_00h(); break;
                   16977:                        case 0x10: pcbios_int_13h_10h(); break;
                   16978:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     16979:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     16980:                        case 0x05: // format
                   16981:                        case 0x06:
                   16982:                        case 0x07:
                   16983:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   16984:                                m_CF = 1;
                   16985:                                break;
                   16986:                        case 0x09:
                   16987:                        case 0x0c: // seek
                   16988:                        case 0x11: // recalib
                   16989:                        case 0x14:
                   16990:                        case 0x17:
                   16991:                                REG8(AH) = 0x00; // successful completion
                   16992:                                break;
1.1.1.43  root     16993:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   16994:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   16995:                                REG8(AH) = 0x01; // invalid function
                   16996:                                m_CF = 1;
                   16997:                                break;
1.1.1.42  root     16998:                        default:
                   16999:                                unimplemented_13h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17000:                                REG8(AH) = 0x01; // invalid function
                   17001:                                m_CF = 1;
                   17002:                                break;
                   17003:                        }
                   17004:                        last = REG8(AH);
                   17005:                }
1.1       root     17006:                break;
                   17007:        case 0x14:
                   17008:                // PC BIOS - Serial I/O
1.1.1.25  root     17009:                switch(REG8(AH)) {
                   17010:                case 0x00: pcbios_int_14h_00h(); break;
                   17011:                case 0x01: pcbios_int_14h_01h(); break;
                   17012:                case 0x02: pcbios_int_14h_02h(); break;
                   17013:                case 0x03: pcbios_int_14h_03h(); break;
                   17014:                case 0x04: pcbios_int_14h_04h(); break;
                   17015:                case 0x05: pcbios_int_14h_05h(); break;
                   17016:                default:
                   17017:                        unimplemented_14h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17018:                        break;
                   17019:                }
1.1       root     17020:                break;
                   17021:        case 0x15:
                   17022:                // PC BIOS
1.1.1.3   root     17023:                m_CF = 0;
1.1       root     17024:                switch(REG8(AH)) {
1.1.1.14  root     17025:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     17026:                case 0x23: pcbios_int_15h_23h(); break;
                   17027:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     17028:                case 0x41: break;
1.1       root     17029:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     17030:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     17031:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     17032:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     17033:                case 0x86: pcbios_int_15h_86h(); break;
                   17034:                case 0x87: pcbios_int_15h_87h(); break;
                   17035:                case 0x88: pcbios_int_15h_88h(); break;
                   17036:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     17037:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     17038:                case 0xc0: // PS/2 ???
1.1.1.54  root     17039: #ifndef EXT_BIOS_TOP
1.1.1.22  root     17040:                case 0xc1:
1.1.1.54  root     17041: #endif
1.1.1.30  root     17042:                case 0xc3: // PS50+ ???
                   17043:                case 0xc4:
1.1.1.22  root     17044:                        REG8(AH) = 0x86;
                   17045:                        m_CF = 1;
                   17046:                        break;
1.1.1.54  root     17047: #ifdef EXT_BIOS_TOP
                   17048:                case 0xc1: pcbios_int_15h_c1h(); break;
                   17049: #endif
                   17050:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     17051: #if defined(HAS_I386)
1.1       root     17052:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     17053: #endif
1.1       root     17054:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     17055:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     17056:                default:
1.1.1.22  root     17057:                        unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17058:                        REG8(AH) = 0x86;
1.1.1.3   root     17059:                        m_CF = 1;
1.1       root     17060:                        break;
                   17061:                }
                   17062:                break;
                   17063:        case 0x16:
                   17064:                // PC BIOS - Keyboard
1.1.1.3   root     17065:                m_CF = 0;
1.1       root     17066:                switch(REG8(AH)) {
                   17067:                case 0x00: pcbios_int_16h_00h(); break;
                   17068:                case 0x01: pcbios_int_16h_01h(); break;
                   17069:                case 0x02: pcbios_int_16h_02h(); break;
                   17070:                case 0x03: pcbios_int_16h_03h(); break;
                   17071:                case 0x05: pcbios_int_16h_05h(); break;
1.1.1.60  root     17072:                case 0x09: pcbios_int_16h_09h(); break;
                   17073:                case 0x0a: pcbios_int_16h_0ah(); break;
1.1       root     17074:                case 0x10: pcbios_int_16h_00h(); break;
1.1.1.60  root     17075:                case 0x11: pcbios_int_16h_11h(); break;
1.1       root     17076:                case 0x12: pcbios_int_16h_12h(); break;
                   17077:                case 0x13: pcbios_int_16h_13h(); break;
                   17078:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     17079:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     17080:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     17081:                case 0xda: break; // unknown
1.1.1.43  root     17082:                case 0xdb: break; // unknown
1.1.1.22  root     17083:                case 0xff: break; // unknown
1.1       root     17084:                default:
1.1.1.22  root     17085:                        unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     17086:                        break;
                   17087:                }
                   17088:                break;
                   17089:        case 0x17:
                   17090:                // PC BIOS - Printer
1.1.1.37  root     17091:                m_CF = 0;
                   17092:                switch(REG8(AH)) {
                   17093:                case 0x00: pcbios_int_17h_00h(); break;
                   17094:                case 0x01: pcbios_int_17h_01h(); break;
                   17095:                case 0x02: pcbios_int_17h_02h(); break;
                   17096:                case 0x03: pcbios_int_17h_03h(); break;
                   17097:                case 0x50: pcbios_int_17h_50h(); break;
                   17098:                case 0x51: pcbios_int_17h_51h(); break;
                   17099:                case 0x52: pcbios_int_17h_52h(); break;
                   17100:                case 0x84: pcbios_int_17h_84h(); break;
                   17101:                case 0x85: pcbios_int_17h_85h(); break;
                   17102:                default:
                   17103:                        unimplemented_17h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17104:                        break;
                   17105:                }
1.1       root     17106:                break;
                   17107:        case 0x1a:
                   17108:                // PC BIOS - Timer
1.1.1.3   root     17109:                m_CF = 0;
1.1       root     17110:                switch(REG8(AH)) {
                   17111:                case 0x00: pcbios_int_1ah_00h(); break;
                   17112:                case 0x01: break;
                   17113:                case 0x02: pcbios_int_1ah_02h(); break;
                   17114:                case 0x03: break;
                   17115:                case 0x04: pcbios_int_1ah_04h(); break;
                   17116:                case 0x05: break;
                   17117:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   17118:                case 0x0b: break;
1.1.1.14  root     17119:                case 0x35: break; // Word Perfect Third Party Interface?
                   17120:                case 0x36: break; // Word Perfect Third Party Interface
                   17121:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     17122:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     17123:                case 0xb1: break; // PCI BIOS v2.0c+
                   17124:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     17125:                default:
1.1.1.22  root     17126:                        unimplemented_1ah("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     17127:                        break;
                   17128:                }
                   17129:                break;
1.1.1.33  root     17130:        case 0x1b:
                   17131:                mem[0x471] = 0x00;
                   17132:                break;
1.1       root     17133:        case 0x20:
1.1.1.28  root     17134:                try {
                   17135:                        msdos_process_terminate(SREG(CS), retval, 1);
                   17136:                } catch(...) {
                   17137:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   17138:                }
1.1       root     17139:                break;
1.1.1.49  root     17140:        case 0x30:
1.1.1.46  root     17141:                // dummy interrupt for case map routine pointed in the country info
                   17142: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   17143: //                     REG8(AL) = 0x00;
                   17144: //                     break;
                   17145: //             }
1.1       root     17146:        case 0x21:
                   17147:                // MS-DOS System Call
1.1.1.3   root     17148:                m_CF = 0;
1.1.1.28  root     17149:                try {
1.1.1.46  root     17150:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     17151:                        case 0x00: msdos_int_21h_00h(); break;
                   17152:                        case 0x01: msdos_int_21h_01h(); break;
                   17153:                        case 0x02: msdos_int_21h_02h(); break;
                   17154:                        case 0x03: msdos_int_21h_03h(); break;
                   17155:                        case 0x04: msdos_int_21h_04h(); break;
                   17156:                        case 0x05: msdos_int_21h_05h(); break;
                   17157:                        case 0x06: msdos_int_21h_06h(); break;
                   17158:                        case 0x07: msdos_int_21h_07h(); break;
                   17159:                        case 0x08: msdos_int_21h_08h(); break;
                   17160:                        case 0x09: msdos_int_21h_09h(); break;
                   17161:                        case 0x0a: msdos_int_21h_0ah(); break;
                   17162:                        case 0x0b: msdos_int_21h_0bh(); break;
                   17163:                        case 0x0c: msdos_int_21h_0ch(); break;
                   17164:                        case 0x0d: msdos_int_21h_0dh(); break;
                   17165:                        case 0x0e: msdos_int_21h_0eh(); break;
                   17166:                        case 0x0f: msdos_int_21h_0fh(); break;
                   17167:                        case 0x10: msdos_int_21h_10h(); break;
                   17168:                        case 0x11: msdos_int_21h_11h(); break;
                   17169:                        case 0x12: msdos_int_21h_12h(); break;
                   17170:                        case 0x13: msdos_int_21h_13h(); break;
                   17171:                        case 0x14: msdos_int_21h_14h(); break;
                   17172:                        case 0x15: msdos_int_21h_15h(); break;
                   17173:                        case 0x16: msdos_int_21h_16h(); break;
                   17174:                        case 0x17: msdos_int_21h_17h(); break;
                   17175:                        case 0x18: msdos_int_21h_18h(); break;
                   17176:                        case 0x19: msdos_int_21h_19h(); break;
                   17177:                        case 0x1a: msdos_int_21h_1ah(); break;
                   17178:                        case 0x1b: msdos_int_21h_1bh(); break;
                   17179:                        case 0x1c: msdos_int_21h_1ch(); break;
                   17180:                        case 0x1d: msdos_int_21h_1dh(); break;
                   17181:                        case 0x1e: msdos_int_21h_1eh(); break;
                   17182:                        case 0x1f: msdos_int_21h_1fh(); break;
                   17183:                        case 0x20: msdos_int_21h_20h(); break;
                   17184:                        case 0x21: msdos_int_21h_21h(); break;
                   17185:                        case 0x22: msdos_int_21h_22h(); break;
                   17186:                        case 0x23: msdos_int_21h_23h(); break;
                   17187:                        case 0x24: msdos_int_21h_24h(); break;
                   17188:                        case 0x25: msdos_int_21h_25h(); break;
                   17189:                        case 0x26: msdos_int_21h_26h(); break;
                   17190:                        case 0x27: msdos_int_21h_27h(); break;
                   17191:                        case 0x28: msdos_int_21h_28h(); break;
                   17192:                        case 0x29: msdos_int_21h_29h(); break;
                   17193:                        case 0x2a: msdos_int_21h_2ah(); break;
                   17194:                        case 0x2b: msdos_int_21h_2bh(); break;
                   17195:                        case 0x2c: msdos_int_21h_2ch(); break;
                   17196:                        case 0x2d: msdos_int_21h_2dh(); break;
                   17197:                        case 0x2e: msdos_int_21h_2eh(); break;
                   17198:                        case 0x2f: msdos_int_21h_2fh(); break;
                   17199:                        case 0x30: msdos_int_21h_30h(); break;
                   17200:                        case 0x31: msdos_int_21h_31h(); break;
                   17201:                        case 0x32: msdos_int_21h_32h(); break;
                   17202:                        case 0x33: msdos_int_21h_33h(); break;
                   17203:                        case 0x34: msdos_int_21h_34h(); break;
                   17204:                        case 0x35: msdos_int_21h_35h(); break;
                   17205:                        case 0x36: msdos_int_21h_36h(); break;
                   17206:                        case 0x37: msdos_int_21h_37h(); break;
                   17207:                        case 0x38: msdos_int_21h_38h(); break;
                   17208:                        case 0x39: msdos_int_21h_39h(0); break;
                   17209:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   17210:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   17211:                        case 0x3c: msdos_int_21h_3ch(); break;
                   17212:                        case 0x3d: msdos_int_21h_3dh(); break;
                   17213:                        case 0x3e: msdos_int_21h_3eh(); break;
                   17214:                        case 0x3f: msdos_int_21h_3fh(); break;
                   17215:                        case 0x40: msdos_int_21h_40h(); break;
                   17216:                        case 0x41: msdos_int_21h_41h(0); break;
                   17217:                        case 0x42: msdos_int_21h_42h(); break;
                   17218:                        case 0x43: msdos_int_21h_43h(0); break;
                   17219:                        case 0x44: msdos_int_21h_44h(); break;
                   17220:                        case 0x45: msdos_int_21h_45h(); break;
                   17221:                        case 0x46: msdos_int_21h_46h(); break;
                   17222:                        case 0x47: msdos_int_21h_47h(0); break;
                   17223:                        case 0x48: msdos_int_21h_48h(); break;
                   17224:                        case 0x49: msdos_int_21h_49h(); break;
                   17225:                        case 0x4a: msdos_int_21h_4ah(); break;
                   17226:                        case 0x4b: msdos_int_21h_4bh(); break;
                   17227:                        case 0x4c: msdos_int_21h_4ch(); break;
                   17228:                        case 0x4d: msdos_int_21h_4dh(); break;
                   17229:                        case 0x4e: msdos_int_21h_4eh(); break;
                   17230:                        case 0x4f: msdos_int_21h_4fh(); break;
                   17231:                        case 0x50: msdos_int_21h_50h(); break;
                   17232:                        case 0x51: msdos_int_21h_51h(); break;
                   17233:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     17234:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     17235:                        case 0x54: msdos_int_21h_54h(); break;
                   17236:                        case 0x55: msdos_int_21h_55h(); break;
                   17237:                        case 0x56: msdos_int_21h_56h(0); break;
                   17238:                        case 0x57: msdos_int_21h_57h(); break;
                   17239:                        case 0x58: msdos_int_21h_58h(); break;
                   17240:                        case 0x59: msdos_int_21h_59h(); break;
                   17241:                        case 0x5a: msdos_int_21h_5ah(); break;
                   17242:                        case 0x5b: msdos_int_21h_5bh(); break;
                   17243:                        case 0x5c: msdos_int_21h_5ch(); break;
                   17244:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     17245:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     17246:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     17247:                        case 0x60: msdos_int_21h_60h(0); break;
                   17248:                        case 0x61: msdos_int_21h_61h(); break;
                   17249:                        case 0x62: msdos_int_21h_62h(); break;
                   17250:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     17251:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     17252:                        case 0x65: msdos_int_21h_65h(); break;
                   17253:                        case 0x66: msdos_int_21h_66h(); break;
                   17254:                        case 0x67: msdos_int_21h_67h(); break;
                   17255:                        case 0x68: msdos_int_21h_68h(); break;
                   17256:                        case 0x69: msdos_int_21h_69h(); break;
                   17257:                        case 0x6a: msdos_int_21h_6ah(); break;
                   17258:                        case 0x6b: msdos_int_21h_6bh(); break;
                   17259:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     17260:                        case 0x6d: // Find First ROM Program
                   17261:                        case 0x6e: // Find Next ROM Program
                   17262:                        case 0x6f: // Get/Set ROM Scan Start Address
                   17263:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   17264:                                break;
1.1.1.43  root     17265:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     17266:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     17267:                                switch(REG8(AL)) {
                   17268:                                case 0x0d: msdos_int_21h_710dh(); break;
                   17269:                                case 0x39: msdos_int_21h_39h(1); break;
                   17270:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   17271:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     17272:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     17273:                                case 0x43: msdos_int_21h_43h(1); break;
                   17274:                                case 0x47: msdos_int_21h_47h(1); break;
                   17275:                                case 0x4e: msdos_int_21h_714eh(); break;
                   17276:                                case 0x4f: msdos_int_21h_714fh(); break;
                   17277:                                case 0x56: msdos_int_21h_56h(1); break;
                   17278:                                case 0x60: msdos_int_21h_60h(1); break;
                   17279:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   17280:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   17281:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   17282:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   17283:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   17284:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     17285:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     17286:                                case 0xaa: msdos_int_21h_71aah(); break;
                   17287:                                default:
                   17288:                                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17289:                                        REG16(AX) = 0x7100;
                   17290:                                        m_CF = 1;
                   17291:                                        break;
                   17292:                                }
                   17293:                                break;
1.1.1.48  root     17294:                        case 0x72: // Windows95 beta - LFN FindClose
                   17295: //                             unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17296:                                REG16(AX) = 0x7200;
                   17297:                                m_CF = 1;
                   17298:                                break;
                   17299:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     17300:                                switch(REG8(AL)) {
                   17301:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     17302:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     17303:                                case 0x02: msdos_int_21h_7302h(); break;
                   17304:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     17305:                                // 0x04: Set DPB to Use for Formatting
                   17306:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     17307:                                default:
                   17308:                                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17309:                                        REG16(AX) = 0x7300;
                   17310:                                        m_CF = 1;
                   17311:                                        break;
                   17312:                                }
1.1       root     17313:                                break;
1.1.1.30  root     17314:                        case 0xdb: msdos_int_21h_dbh(); break;
                   17315:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     17316:                        default:
1.1.1.22  root     17317:                                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.28  root     17318:                                REG16(AX) = 0x01;
1.1.1.3   root     17319:                                m_CF = 1;
1.1       root     17320:                                break;
                   17321:                        }
1.1.1.28  root     17322:                } catch(int error) {
                   17323:                        REG16(AX) = error;
                   17324:                        m_CF = 1;
                   17325:                } catch(...) {
                   17326:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     17327:                        m_CF = 1;
1.1       root     17328:                }
1.1.1.3   root     17329:                if(m_CF) {
1.1.1.23  root     17330:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     17331:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     17332:                        sda->extended_error_code = REG16(AX);
                   17333:                        switch(sda->extended_error_code) {
                   17334:                        case  4: // Too many open files
                   17335:                        case  8: // Insufficient memory
                   17336:                                sda->error_class = 1; // Out of resource
                   17337:                                break;
                   17338:                        case  5: // Access denied
                   17339:                                sda->error_class = 3; // Authorization
                   17340:                                break;
                   17341:                        case  7: // Memory control block destroyed
                   17342:                                sda->error_class = 4; // Internal
                   17343:                                break;
                   17344:                        case  2: // File not found
                   17345:                        case  3: // Path not found
                   17346:                        case 15: // Invaid drive specified
                   17347:                        case 18: // No more files
                   17348:                                sda->error_class = 8; // Not found
                   17349:                                break;
                   17350:                        case 32: // Sharing violation
                   17351:                        case 33: // Lock violation
                   17352:                                sda->error_class = 10; // Locked
                   17353:                                break;
                   17354: //                     case 16: // Removal of current directory attempted
                   17355:                        case 19: // Attempted write on protected disk
                   17356:                        case 21: // Drive not ready
                   17357: //                     case 29: // Write failure
                   17358: //                     case 30: // Read failure
                   17359: //                     case 82: // Cannot create subdirectory
                   17360:                                sda->error_class = 11; // Media
                   17361:                                break;
                   17362:                        case 80: // File already exists
                   17363:                                sda->error_class = 12; // Already exist
                   17364:                                break;
                   17365:                        default:
                   17366:                                sda->error_class = 13; // Unknown
                   17367:                                break;
                   17368:                        }
                   17369:                        sda->suggested_action = 1; // Retry
                   17370:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17371:                }
1.1.1.33  root     17372:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17373:                        // raise int 23h
                   17374: #if defined(HAS_I386)
                   17375:                        m_ext = 0; // not an external interrupt
                   17376:                        i386_trap(0x23, 1, 0);
                   17377:                        m_ext = 1;
                   17378: #else
                   17379:                        PREFIX86(_interrupt)(0x23);
                   17380: #endif
                   17381:                }
1.1       root     17382:                break;
                   17383:        case 0x22:
                   17384:                fatalerror("int 22h (terminate address)\n");
                   17385:        case 0x23:
1.1.1.28  root     17386:                try {
                   17387:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17388:                } catch(...) {
                   17389:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17390:                }
1.1       root     17391:                break;
                   17392:        case 0x24:
1.1.1.32  root     17393: /*
1.1.1.28  root     17394:                try {
                   17395:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17396:                } catch(...) {
                   17397:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17398:                }
1.1.1.32  root     17399: */
                   17400:                msdos_int_24h();
1.1       root     17401:                break;
                   17402:        case 0x25:
                   17403:                msdos_int_25h();
                   17404:                break;
                   17405:        case 0x26:
                   17406:                msdos_int_26h();
                   17407:                break;
                   17408:        case 0x27:
1.1.1.28  root     17409:                try {
                   17410:                        msdos_int_27h();
                   17411:                } catch(...) {
                   17412:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17413:                }
1.1       root     17414:                break;
                   17415:        case 0x28:
                   17416:                Sleep(10);
1.1.1.35  root     17417:                REQUEST_HARDWRE_UPDATE();
1.1       root     17418:                break;
                   17419:        case 0x29:
                   17420:                msdos_int_29h();
                   17421:                break;
                   17422:        case 0x2e:
                   17423:                msdos_int_2eh();
                   17424:                break;
                   17425:        case 0x2f:
                   17426:                // multiplex interrupt
                   17427:                switch(REG8(AH)) {
1.1.1.22  root     17428:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17429:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17430:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17431:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17432:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17433:                case 0x14: msdos_int_2fh_14h(); break;
                   17434:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17435:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17436:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17437:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17438:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17439:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17440:                case 0x46: msdos_int_2fh_46h(); break;
                   17441:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17442:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17443:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17444:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17445:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17446:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17447:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17448:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17449:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17450:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17451:                default:
1.1.1.30  root     17452:                        switch(REG8(AL)) {
                   17453:                        case 0x00:
                   17454:                                // This is not installed
                   17455: //                             REG8(AL) = 0x00;
                   17456:                                break;
1.1.1.33  root     17457:                        case 0x01:
1.1.1.42  root     17458:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17459:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17460:                                        break;
                   17461:                                }
1.1.1.33  root     17462:                                // Banyan VINES v4+ is not installed
                   17463:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17464:                                        break;
                   17465:                                }
1.1.1.42  root     17466:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17467:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17468:                                        break;
                   17469:                                }
1.1.1.30  root     17470:                        default:
1.1.1.42  root     17471:                                // NORTON UTILITIES 5.0+
                   17472:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17473:                                        break;
                   17474:                                }
1.1.1.30  root     17475:                                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.43  root     17476:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17477:                                m_CF = 1;
                   17478:                                break;
                   17479:                        }
                   17480:                        break;
1.1       root     17481:                }
                   17482:                break;
1.1.1.24  root     17483:        case 0x33:
                   17484:                switch(REG8(AH)) {
                   17485:                case 0x00:
                   17486:                        // Mouse
1.1.1.49  root     17487:                        switch(REG16(AX)) {
                   17488:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17489:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17490:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17491:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17492:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17493:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17494:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17495:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17496:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17497:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17498:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17499:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17500:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17501:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17502:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17503:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17504:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17505:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17506:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17507:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17508:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17509:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17510:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17511:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17512:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17513:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17514:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17515:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17516:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17517:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17518:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17519:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17520:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17521:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17522:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17523:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17524:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17525:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17526:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17527:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17528:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17529:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17530:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17531:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17532:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17533:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17534:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17535:                        case 0x002f: break; // Mouse Hardware Reset
                   17536:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17537:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17538:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17539:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17540:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17541:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17542:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17543:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17544:                        default:
                   17545:                                unimplemented_33h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17546:                                break;
                   17547:                        }
                   17548:                        break;
                   17549:                default:
                   17550:                        unimplemented_33h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17551:                        break;
                   17552:                }
                   17553:                break;
1.1.1.59  root     17554:        case 0x65:
1.1.1.19  root     17555:                // dummy interrupt for EMS (int 67h)
                   17556:                switch(REG8(AH)) {
                   17557:                case 0x40: msdos_int_67h_40h(); break;
                   17558:                case 0x41: msdos_int_67h_41h(); break;
                   17559:                case 0x42: msdos_int_67h_42h(); break;
                   17560:                case 0x43: msdos_int_67h_43h(); break;
                   17561:                case 0x44: msdos_int_67h_44h(); break;
                   17562:                case 0x45: msdos_int_67h_45h(); break;
                   17563:                case 0x46: msdos_int_67h_46h(); break;
                   17564:                case 0x47: msdos_int_67h_47h(); break;
                   17565:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17566:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17567:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17568:                case 0x4b: msdos_int_67h_4bh(); break;
                   17569:                case 0x4c: msdos_int_67h_4ch(); break;
                   17570:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17571:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17572:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17573:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17574:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17575:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17576:                case 0x53: msdos_int_67h_53h(); break;
                   17577:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17578:                case 0x55: msdos_int_67h_55h(); break;
                   17579:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17580:                case 0x57: msdos_int_67h_57h(); break;
                   17581:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17582:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17583:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17584:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17585:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17586:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17587:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17588:                // 0xde: VCPI
1.1.1.30  root     17589:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17590:                default:
1.1.1.22  root     17591:                        unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.19  root     17592:                        REG8(AH) = 0x84;
                   17593:                        break;
                   17594:                }
                   17595:                break;
                   17596: #ifdef SUPPORT_XMS
1.1.1.59  root     17597:        case 0x66:
1.1.1.19  root     17598:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17599:                try {
                   17600:                        switch(REG8(AH)) {
                   17601:                        case 0x00: msdos_call_xms_00h(); break;
                   17602:                        case 0x01: msdos_call_xms_01h(); break;
                   17603:                        case 0x02: msdos_call_xms_02h(); break;
                   17604:                        case 0x03: msdos_call_xms_03h(); break;
                   17605:                        case 0x04: msdos_call_xms_04h(); break;
                   17606:                        case 0x05: msdos_call_xms_05h(); break;
                   17607:                        case 0x06: msdos_call_xms_06h(); break;
                   17608:                        case 0x07: msdos_call_xms_07h(); break;
                   17609:                        case 0x08: msdos_call_xms_08h(); break;
                   17610:                        case 0x09: msdos_call_xms_09h(); break;
                   17611:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17612:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17613:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17614:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17615:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17616:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17617:                        case 0x10: msdos_call_xms_10h(); break;
                   17618:                        case 0x11: msdos_call_xms_11h(); break;
                   17619:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17620: #if defined(HAS_I386)
                   17621:                        case 0x88: msdos_call_xms_88h(); break;
                   17622:                        case 0x89: msdos_call_xms_89h(); break;
                   17623:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17624:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17625: #endif
1.1.1.28  root     17626:                        default:
                   17627:                                unimplemented_xms("call XMS (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   17628:                                REG16(AX) = 0x0000;
                   17629:                                REG8(BL) = 0x80; // function not implemented
                   17630:                                break;
                   17631:                        }
                   17632:                } catch(...) {
1.1.1.19  root     17633:                        REG16(AX) = 0x0000;
1.1.1.28  root     17634:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17635:                }
                   17636:                break;
                   17637: #endif
1.1.1.59  root     17638: /*
                   17639:        case 0x67:
                   17640:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 65h
                   17641:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17642:                break;
                   17643: */
                   17644:        case 0x69:
1.1.1.24  root     17645:                // irq12 (mouse)
                   17646:                mouse_push_ax = REG16(AX);
                   17647:                mouse_push_bx = REG16(BX);
                   17648:                mouse_push_cx = REG16(CX);
                   17649:                mouse_push_dx = REG16(DX);
                   17650:                mouse_push_si = REG16(SI);
                   17651:                mouse_push_di = REG16(DI);
                   17652:                
1.1.1.43  root     17653:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17654:                        REG16(AX) = mouse.status_irq;
                   17655:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17656:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17657:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17658:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17659:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17660:                        
1.1.1.49  root     17661:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17662:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17663:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17664:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17665:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.59  root     17666:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17667:                        mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17668:                        break;
1.1.1.24  root     17669:                }
1.1.1.43  root     17670:                for(int i = 0; i < 8; i++) {
                   17671:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17672:                                REG16(AX) = mouse.status_irq_alt;
                   17673:                                REG16(BX) = mouse.get_buttons();
                   17674:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17675:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17676:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17677:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17678:                                
1.1.1.49  root     17679:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17680:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17681:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17682:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17683:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.59  root     17684:                                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17685:                                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17686:                                break;
                   17687:                        }
                   17688:                }
1.1.1.59  root     17689:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     17690:                        UINT16 data_1st, data_2nd, data_3rd;
                   17691:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17692:                        i386_push16(data_1st);
                   17693:                        i386_push16(data_2nd);
                   17694:                        i386_push16(data_3rd);
                   17695:                        i386_push16(0x0000);
                   17696:                        
                   17697:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17698:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17699:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17700:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17701:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
1.1.1.59  root     17702:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   17703:                        mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.54  root     17704:                        break;
                   17705:                }
1.1.1.43  root     17706:                // invalid call addr :-(
1.1.1.49  root     17707:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17708:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17709:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17710:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17711:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.59  root     17712:                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17713:                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.24  root     17714:                break;
1.1.1.59  root     17715:        case 0x6a:
                   17716:                // end of ps/2 mouse bios
                   17717:                i386_pop16();
                   17718:                i386_pop16();
                   17719:                i386_pop16();
                   17720:                i386_pop16();
1.1.1.24  root     17721:        case 0x6b:
                   17722:                // end of irq12 (mouse)
                   17723:                REG16(AX) = mouse_push_ax;
                   17724:                REG16(BX) = mouse_push_bx;
                   17725:                REG16(CX) = mouse_push_cx;
                   17726:                REG16(DX) = mouse_push_dx;
                   17727:                REG16(SI) = mouse_push_si;
                   17728:                REG16(DI) = mouse_push_di;
                   17729:                
                   17730:                // EOI
                   17731:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17732:                        pic[0].isr &= ~(1 << 2); // master
                   17733:                }
                   17734:                pic_update();
                   17735:                break;
                   17736:        case 0x6c:
1.1.1.19  root     17737:                // dummy interrupt for case map routine pointed in the country info
                   17738:                if(REG8(AL) >= 0x80) {
                   17739:                        char tmp[2] = {0};
                   17740:                        tmp[0] = REG8(AL);
                   17741:                        my_strupr(tmp);
                   17742:                        REG8(AL) = tmp[0];
                   17743:                }
                   17744:                break;
1.1.1.27  root     17745:        case 0x6d:
                   17746:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17747:                REG8(AL) = 0x86; // not supported
                   17748:                m_CF = 1;
                   17749:                break;
1.1.1.32  root     17750:        case 0x6e:
                   17751:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17752:                {
                   17753:                        UINT16 code = REG16(AX);
                   17754:                        if(code & 0xf0) {
                   17755:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17756:                        }
                   17757:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17758:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17759:                                        const char *message = NULL;
                   17760:                                        if(active_code_page == 932) {
                   17761:                                                message = param_error_table[i].message_japanese;
                   17762:                                        }
                   17763:                                        if(message == NULL) {
                   17764:                                                message = param_error_table[i].message_english;
                   17765:                                        }
                   17766:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17767:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17768:                                        
                   17769:                                        SREG(ES) = WORK_TOP >> 4;
                   17770:                                        i386_load_segment_descriptor(ES);
                   17771:                                        REG16(DI) = 0x0000;
                   17772:                                        break;
                   17773:                                }
                   17774:                        }
                   17775:                }
                   17776:                break;
1.1.1.49  root     17777:        case 0x6f:
                   17778:                // dummy interrupt for end of alter page map and call
                   17779:                {
                   17780:                        UINT16 handles[4], pages[4];
                   17781:                        
                   17782:                        // pop old mapping data in new mapping
                   17783:                        for(int i = 0; i < 4; i++) {
                   17784:                                pages  [3 - i] = i386_pop16();
                   17785:                                handles[3 - i] = i386_pop16();
                   17786:                        }
                   17787:                        
                   17788:                        // restore old mapping
                   17789:                        for(int i = 0; i < 4; i++) {
                   17790:                                UINT16 handle = handles[i];
                   17791:                                UINT16 page   = pages  [i];
                   17792:                                
                   17793:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17794:                                        ems_map_page(i, handle, page);
                   17795:                                } else {
                   17796:                                        ems_unmap_page(i);
                   17797:                                }
                   17798:                        }
                   17799:                        // do ret_far (pop cs/ip) in old mapping
                   17800:                }
                   17801:                break;
1.1.1.8   root     17802:        case 0x70:
                   17803:        case 0x71:
                   17804:        case 0x72:
                   17805:        case 0x73:
                   17806:        case 0x74:
                   17807:        case 0x75:
                   17808:        case 0x76:
                   17809:        case 0x77:
                   17810:                // EOI
                   17811:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17812:                        pic[0].isr &= ~(1 << 2); // master
                   17813:                }
                   17814:                pic_update();
                   17815:                break;
1.1       root     17816:        default:
1.1.1.22  root     17817: //             fatalerror("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     17818:                break;
                   17819:        }
                   17820:        
                   17821:        // update cursor position
                   17822:        if(cursor_moved) {
1.1.1.36  root     17823:                pcbios_update_cursor_position();
1.1       root     17824:                cursor_moved = false;
                   17825:        }
                   17826: }
                   17827: 
                   17828: // init
                   17829: 
                   17830: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17831: {
                   17832:        // init file handler
                   17833:        memset(file_handler, 0, sizeof(file_handler));
                   17834:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17835:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17836:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17837: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17838:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17839: #else
                   17840:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17841: #endif
                   17842:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17843:        }
1.1.1.21  root     17844:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17845: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17846:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17847:        }
1.1       root     17848:        _dup2(0, DUP_STDIN);
                   17849:        _dup2(1, DUP_STDOUT);
                   17850:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17851:        _dup2(3, DUP_STDAUX);
                   17852:        _dup2(4, DUP_STDPRN);
1.1       root     17853:        
1.1.1.24  root     17854:        // init mouse
                   17855:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17856:        mouse.enabled = true;   // from DOSBox
                   17857:        mouse.hidden = 1;       // hidden in default ???
                   17858:        mouse.old_hidden = 1;   // from DOSBox
                   17859:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17860:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17861:        mouse.mickey.x = 8;
                   17862:        mouse.mickey.y = 16;
                   17863:        
1.1.1.26  root     17864: #ifdef SUPPORT_XMS
                   17865:        // init xms
                   17866:        msdos_xms_init();
                   17867: #endif
                   17868:        
1.1       root     17869:        // init process
                   17870:        memset(process, 0, sizeof(process));
                   17871:        
1.1.1.13  root     17872:        // init dtainfo
                   17873:        msdos_dta_info_init();
                   17874:        
1.1       root     17875:        // init memory
                   17876:        memset(mem, 0, sizeof(mem));
                   17877:        
                   17878:        // bios data area
1.1.1.23  root     17879:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17880:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17881:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.58  root     17882: //     CONSOLE_FONT_INFO cfi;
1.1.1.57  root     17883: //     GetCurrentConsoleFont(hStdout, FALSE, &cfi);
1.1.1.14  root     17884:        
                   17885:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17886:        text_vram_top_address = TEXT_VRAM_TOP;
                   17887:        text_vram_end_address = text_vram_top_address + regen;
                   17888:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17889:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     17890:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     17891:        
                   17892:        if(regen > 0x4000) {
                   17893:                regen = 0x8000;
                   17894:                vram_pages = 1;
                   17895:        } else if(regen > 0x2000) {
                   17896:                regen = 0x4000;
                   17897:                vram_pages = 2;
                   17898:        } else if(regen > 0x1000) {
                   17899:                regen = 0x2000;
                   17900:                vram_pages = 4;
                   17901:        } else {
                   17902:                regen = 0x1000;
                   17903:                vram_pages = 8;
                   17904:        }
1.1       root     17905:        
1.1.1.25  root     17906:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17907:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17908:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17909:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17910:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17911:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17912:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17913: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17914:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17915: #endif
1.1.1.26  root     17916:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17917:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17918:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17919:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17920:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17921:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17922:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17923:        *(UINT16 *)(mem + 0x44e) = 0;
                   17924:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17925:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17926:        *(UINT8  *)(mem + 0x460) = 7;
                   17927:        *(UINT8  *)(mem + 0x461) = 7;
                   17928:        *(UINT8  *)(mem + 0x462) = 0;
                   17929:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17930:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17931:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17932:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17933:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17934:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17935:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.58  root     17936:        *(UINT16 *)(mem + 0x485) = font_height;
1.1.1.14  root     17937:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17938:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17939: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17940:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17941: #endif
1.1.1.14  root     17942:        
                   17943:        // initial screen
                   17944:        SMALL_RECT rect;
                   17945:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     17946:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     17947:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17948:                for(int x = 0; x < scr_width; x++) {
                   17949:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17950:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17951:                }
                   17952:        }
1.1       root     17953:        
1.1.1.19  root     17954:        // init mcb
1.1       root     17955:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17956:        
                   17957:        // iret table
                   17958:        // note: int 2eh vector should address the routine in command.com,
                   17959:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17960:        // so move iret table into allocated memory block
                   17961:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.58  root     17962:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, (IRET_SIZE + 5 * 128) >> 4);
1.1.1.19  root     17963:        IRET_TOP = seg << 4;
1.1.1.58  root     17964:        seg += (IRET_SIZE + 5 * 128) >> 4;
1.1.1.25  root     17965:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17966:        
1.1.1.58  root     17967:        // note: SO1 checks int 21h vector and if it aims iret (cfh)
                   17968:        // it is recognized SO1 is not running on MS-DOS environment
                   17969:        for(int i = 0; i < 128; i++) {
                   17970:                // jmp far (IRET_TOP >> 4):(interrupt number)
                   17971:                *(UINT8  *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 0) = 0xea;
                   17972:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 1) = i;
                   17973:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 3) = IRET_TOP >> 4;
                   17974:        }
                   17975:        
1.1.1.19  root     17976:        // dummy xms/ems device
1.1.1.33  root     17977:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     17978:        XMS_TOP = seg << 4;
                   17979:        seg += XMS_SIZE >> 4;
                   17980:        
                   17981:        // environment
1.1.1.33  root     17982:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     17983:        int env_seg = seg;
                   17984:        int ofs = 0;
1.1.1.32  root     17985:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   17986:        char comspec_added = 0;
1.1.1.33  root     17987:        char lastdrive_added = 0;
1.1.1.32  root     17988:        char env_msdos_path[ENV_SIZE] = {0};
                   17989:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     17990:        char prompt_added = 0;
1.1.1.32  root     17991:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     17992:        char tz_added = 0;
1.1.1.45  root     17993:        const char *path, *short_path;
1.1.1.32  root     17994:        
                   17995:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   17996:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17997:                        strcpy(env_append, short_path);
                   17998:                }
                   17999:        }
                   18000:        if((path = getenv("APPEND")) != NULL) {
                   18001:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18002:                        if(env_append[0] != '\0') {
                   18003:                                strcat(env_append, ";");
                   18004:                        }
                   18005:                        strcat(env_append, short_path);
                   18006:                }
                   18007:        }
                   18008:        
                   18009:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   18010:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18011:                        strcpy(comspec_path, short_path);
                   18012:                }
                   18013:        }
                   18014:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   18015:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18016:                        strcpy(comspec_path, short_path);
                   18017:                }
                   18018:        }
1.1       root     18019:        
1.1.1.28  root     18020:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     18021:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18022:                        strcpy(env_msdos_path, short_path);
                   18023:                        strcpy(env_path, short_path);
1.1.1.14  root     18024:                }
                   18025:        }
1.1.1.28  root     18026:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     18027:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18028:                        if(env_path[0] != '\0') {
                   18029:                                strcat(env_path, ";");
                   18030:                        }
                   18031:                        strcat(env_path, short_path);
1.1.1.9   root     18032:                }
                   18033:        }
1.1.1.32  root     18034:        
1.1.1.60  root     18035:        if(GetTempPathA(ENV_SIZE, env_temp) != 0) {
1.1.1.32  root     18036:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     18037:        }
1.1.1.32  root     18038:        for(int i = 0; i < 4; i++) {
                   18039:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   18040:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   18041:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18042:                                strcpy(env_temp, short_path);
                   18043:                                break;
                   18044:                        }
                   18045:                }
1.1.1.24  root     18046:        }
1.1.1.32  root     18047:        
1.1.1.9   root     18048:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     18049:                // lower to upper
1.1.1.28  root     18050:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     18051:                strcpy(tmp, *p);
                   18052:                for(int i = 0;; i++) {
                   18053:                        if(tmp[i] == '=') {
                   18054:                                tmp[i] = '\0';
                   18055:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     18056:                                my_strupr(name);
1.1       root     18057:                                tmp[i] = '=';
                   18058:                                break;
                   18059:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     18060:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     18061:                        }
                   18062:                }
1.1.1.33  root     18063:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   18064:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   18065:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     18066:                        // ignore non standard environments
                   18067:                } else {
1.1.1.33  root     18068:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     18069:                                if(env_append[0] != '\0') {
                   18070:                                        sprintf(tmp, "APPEND=%s", env_append);
                   18071:                                } else {
                   18072:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   18073:                                }
                   18074:                                append_added = 1;
                   18075:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     18076:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     18077:                                comspec_added = 1;
1.1.1.33  root     18078:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   18079:                                char *env = getenv("MSDOS_LASTDRIVE");
                   18080:                                if(env != NULL) {
                   18081:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   18082:                                }
                   18083:                                lastdrive_added = 1;
                   18084:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     18085:                                if(env_msdos_path[0] != '\0') {
                   18086:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   18087:                                } else {
                   18088:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   18089:                                }
1.1.1.33  root     18090:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     18091:                                if(env_path[0] != '\0') {
                   18092:                                        sprintf(tmp, "PATH=%s", env_path);
                   18093:                                } else {
                   18094:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   18095:                                }
1.1.1.32  root     18096:                                path_added = 1;
1.1.1.33  root     18097:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   18098:                                prompt_added = 1;
1.1.1.28  root     18099:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   18100:                                if(env_temp[0] != '\0') {
                   18101:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   18102:                                } else {
                   18103:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   18104:                                }
1.1.1.32  root     18105:                                temp_added = 1;
1.1.1.33  root     18106:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     18107:                                if(env_temp[0] != '\0') {
                   18108:                                        sprintf(tmp, "TMP=%s", env_temp);
                   18109:                                } else {
                   18110:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     18111:                                }
1.1.1.32  root     18112:                                tmp_added = 1;
1.1.1.33  root     18113:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   18114:                                char *env = getenv("MSDOS_TZ");
                   18115:                                if(env != NULL) {
                   18116:                                        sprintf(tmp, "TZ=%s", env);
                   18117:                                }
                   18118:                                tz_added = 1;
1.1       root     18119:                        }
                   18120:                        int len = strlen(tmp);
1.1.1.14  root     18121:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     18122:                                fatalerror("too many environments\n");
                   18123:                        }
                   18124:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   18125:                        ofs += len + 1;
                   18126:                }
                   18127:        }
1.1.1.32  root     18128:        if(!append_added && env_append[0] != '\0') {
                   18129:                #define SET_ENV(name, value) { \
                   18130:                        char tmp[ENV_SIZE]; \
                   18131:                        sprintf(tmp, "%s=%s", name, value); \
                   18132:                        int len = strlen(tmp); \
                   18133:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   18134:                                fatalerror("too many environments\n"); \
                   18135:                        } \
                   18136:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   18137:                        ofs += len + 1; \
                   18138:                }
                   18139:                SET_ENV("APPEND", env_append);
                   18140:        }
                   18141:        if(!comspec_added) {
                   18142:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   18143:        }
1.1.1.33  root     18144:        if(!lastdrive_added) {
                   18145:                SET_ENV("LASTDRIVE", "Z");
                   18146:        }
1.1.1.32  root     18147:        if(!path_added) {
                   18148:                SET_ENV("PATH", env_path);
                   18149:        }
1.1.1.33  root     18150:        if(!prompt_added) {
                   18151:                SET_ENV("PROMPT", "$P$G");
                   18152:        }
1.1.1.32  root     18153:        if(!temp_added) {
                   18154:                SET_ENV("TEMP", env_temp);
                   18155:        }
                   18156:        if(!tmp_added) {
                   18157:                SET_ENV("TMP", env_temp);
                   18158:        }
1.1.1.33  root     18159:        if(!tz_added) {
                   18160:                TIME_ZONE_INFORMATION tzi;
                   18161:                HKEY hKey, hSubKey;
                   18162:                char tzi_std_name[64];
                   18163:                char tz_std[8] = "GMT";
                   18164:                char tz_dlt[8] = "GST";
                   18165:                char tz_value[32];
                   18166:                
                   18167:                // timezone name from GetTimeZoneInformation may not be english
                   18168:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   18169:                setlocale(LC_CTYPE, "");
                   18170:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   18171:                
                   18172:                // get english timezone name from registry
1.1.1.60  root     18173:                if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
1.1.1.33  root     18174:                        for(DWORD i = 0; !tz_added; i++) {
                   18175:                                char reg_name[256], sub_key[1024], std_name[256];
                   18176:                                DWORD size;
                   18177:                                FILETIME ftTime;
1.1.1.60  root     18178:                                LONG result = RegEnumKeyExA(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
1.1.1.33  root     18179:                                
                   18180:                                if(result == ERROR_SUCCESS) {
                   18181:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
1.1.1.60  root     18182:                                        if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   18183:                                                if(RegQueryValueExA(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
1.1.1.33  root     18184:                                                        // search english timezone name from table
1.1.1.37  root     18185:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     18186:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   18187:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   18188:                                                                                if(tz_table[j].std != NULL) {
                   18189:                                                                                        strcpy(tz_std, tz_table[j].std);
                   18190:                                                                                }
                   18191:                                                                                if(tz_table[j].dlt != NULL) {
                   18192:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   18193:                                                                                }
                   18194:                                                                                tz_added = 1;
                   18195:                                                                                break;
                   18196:                                                                        }
                   18197:                                                                }
                   18198:                                                        }
                   18199:                                                }
                   18200:                                                RegCloseKey(hSubKey);
                   18201:                                        }
                   18202:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   18203:                                        break;
                   18204:                                }
                   18205:                        }
                   18206:                        RegCloseKey(hKey);
                   18207:                }
                   18208:                if((tzi.Bias % 60) != 0) {
                   18209:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   18210:                } else {
                   18211:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   18212:                }
                   18213:                if(daylight) {
                   18214:                        strcat(tz_value, tz_dlt);
                   18215:                }
                   18216:                SET_ENV("TZ", tz_value);
                   18217:        }
1.1       root     18218:        seg += (ENV_SIZE >> 4);
                   18219:        
                   18220:        // psp
1.1.1.33  root     18221:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     18222:        current_psp = seg;
1.1.1.35  root     18223:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   18224:        psp->parent_psp = current_psp;
1.1       root     18225:        seg += (PSP_SIZE >> 4);
                   18226:        
1.1.1.19  root     18227:        // first free mcb in conventional memory
1.1.1.33  root     18228:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     18229:        first_mcb = seg;
                   18230:        
1.1.1.19  root     18231:        // dummy mcb to link to umb
1.1.1.33  root     18232: #if 0
1.1.1.39  root     18233:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     18234: #else
1.1.1.39  root     18235:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     18236: #endif
1.1.1.19  root     18237:        
                   18238:        // first free mcb in upper memory block
1.1.1.8   root     18239:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     18240:        
1.1.1.29  root     18241: #ifdef SUPPORT_HMA
                   18242:        // first free mcb in high memory area
                   18243:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   18244: #endif
                   18245:        
1.1.1.26  root     18246:        // interrupt vector
1.1.1.58  root     18247:        for(int i = 0; i < 256; i++) {
                   18248:                // 00-07: CPU exception handler
                   18249:                // 08-0F: IRQ 0-7
                   18250:                // 10-1F: PC BIOS
                   18251:                // 20-3F: MS-DOS system call
                   18252:                // 70-77: IRQ 8-15
                   18253:                *(UINT16 *)(mem + 4 * i + 0) = (i <= 0x3f || (i >= 0x70 && i <= 0x77)) ? (IRET_SIZE + 5 * i) : i;
1.1.1.26  root     18254:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   18255:        }
1.1.1.49  root     18256:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   18257:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18258:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   18259:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   18260:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   18261:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     18262:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   18263:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18264:        
1.1.1.29  root     18265:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     18266:        static const struct {
                   18267:                UINT16 attributes;
                   18268:                char *dev_name;
                   18269:        } dummy_devices[] = {
                   18270:                {0x8013, "CON     "},
                   18271:                {0x8000, "AUX     "},
                   18272:                {0xa0c0, "PRN     "},
                   18273:                {0x8008, "CLOCK$  "},
                   18274:                {0x8000, "COM1    "},
                   18275:                {0xa0c0, "LPT1    "},
                   18276:                {0xa0c0, "LPT2    "},
                   18277:                {0xa0c0, "LPT3    "},
                   18278:                {0x8000, "COM2    "},
                   18279:                {0x8000, "COM3    "},
                   18280:                {0x8000, "COM4    "},
1.1.1.30  root     18281: //             {0xc000, "CONFIG$ "},
                   18282:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     18283:        };
                   18284:        static const UINT8 dummy_device_routine[] = {
                   18285:                // from NUL device of Windows 98 SE
                   18286:                // or word ptr ES:[BX+03],0100
                   18287:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   18288:                // retf
                   18289:                0xcb,
                   18290:        };
1.1.1.29  root     18291:        device_t *last = NULL;
1.1.1.32  root     18292:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     18293:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     18294:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   18295:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18296:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     18297:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   18298:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     18299:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     18300:                last = device;
                   18301:        }
                   18302:        if(last != NULL) {
                   18303:                last->next_driver.w.l = 0;
                   18304:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     18305:        }
1.1.1.29  root     18306:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     18307:        
1.1.1.25  root     18308:        // dos info
                   18309:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   18310:        dos_info->magic_word = 1;
                   18311:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   18312:        dos_info->first_dpb.w.l = 0;
                   18313:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   18314:        dos_info->first_sft.w.l = 0;
                   18315:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     18316:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     18317:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18318:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     18319:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   18320:        dos_info->max_sector_len = 512;
                   18321:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   18322:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   18323:        dos_info->cds.w.l = 0;
                   18324:        dos_info->cds.w.h = CDS_TOP >> 4;
                   18325:        dos_info->fcb_table.w.l = 0;
                   18326:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   18327:        dos_info->last_drive = 'Z' - 'A' + 1;
                   18328:        dos_info->buffers_x = 20;
                   18329:        dos_info->buffers_y = 0;
                   18330:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     18331:        dos_info->nul_device.next_driver.w.l = 22;
                   18332:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     18333:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     18334:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   18335:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18336:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   18337:        dos_info->disk_buf_heads.w.l = 0;
                   18338:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     18339:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     18340:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   18341:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     18342:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     18343:        
                   18344:        char *env;
                   18345:        if((env = getenv("LASTDRIVE")) != NULL) {
                   18346:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18347:                        dos_info->last_drive = env[0] - 'A' + 1;
                   18348:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18349:                        dos_info->last_drive = env[0] - 'a' + 1;
                   18350:                }
                   18351:        }
                   18352:        if((env = getenv("windir")) != NULL) {
                   18353:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18354:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   18355:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18356:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   18357:                }
                   18358:        }
                   18359: #if defined(HAS_I386)
                   18360:        dos_info->i386_or_later = 1;
                   18361: #else
                   18362:        dos_info->i386_or_later = 0;
                   18363: #endif
                   18364:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18365:        
1.1.1.27  root     18366:        // ems (int 67h) and xms
1.1.1.25  root     18367:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18368:        xms_device->next_driver.w.l = 0xffff;
                   18369:        xms_device->next_driver.w.h = 0xffff;
                   18370:        xms_device->attributes = 0xc000;
1.1.1.29  root     18371:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18372:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18373:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18374:        
1.1.1.59  root     18375:        mem[XMS_TOP + 0x12] = 0xcd;     // int 65h (dummy)
                   18376:        mem[XMS_TOP + 0x13] = 0x65;
1.1.1.26  root     18377:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18378: #ifdef SUPPORT_XMS
                   18379:        if(support_xms) {
1.1.1.59  root     18380:                mem[XMS_TOP + 0x15] = 0xcd;     // int 66h (dummy)
                   18381:                mem[XMS_TOP + 0x16] = 0x66;
1.1.1.26  root     18382:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18383:        } else
                   18384: #endif
1.1.1.26  root     18385:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18386:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18387:        
1.1.1.26  root     18388:        // irq12 routine (mouse)
1.1.1.59  root     18389:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 69h (dummy)
                   18390:        mem[DUMMY_TOP + 0x01] = 0x69;
1.1.1.49  root     18391:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18392:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18393:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18394:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18395:        mem[DUMMY_TOP + 0x06] = 0xff;
1.1.1.59  root     18396: //     mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   18397: //     mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.49  root     18398:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18399:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18400:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18401:        
1.1.1.27  root     18402:        // case map routine
1.1.1.49  root     18403:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18404:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18405:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18406:        
                   18407:        // font read routine
1.1.1.49  root     18408:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18409:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18410:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18411:        
1.1.1.32  root     18412:        // error message read routine
1.1.1.49  root     18413:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18414:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18415:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18416:        
1.1.1.35  root     18417:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18418:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18419:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18420:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18421:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18422:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18423:        
1.1.1.50  root     18424:        // irq0 routine (system timer)
1.1.1.49  root     18425:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18426:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18427:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18428:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18429:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18430:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18431:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18432:        
                   18433:        // alter page map and call routine
                   18434:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18435:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18436:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18437:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18438:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18439:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18440:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18441:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18442:        
1.1.1.50  root     18443:        // call int 29h routine
                   18444:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18445:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18446:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18447:        
1.1.1.26  root     18448:        // boot routine
1.1.1.59  root     18449:        mem[0xffff0 + 0x00] = 0xf4;     // halt to exit MS-DOS Player
                   18450: #if 1
                   18451:        mem[0xffff0 + 0x05] = '0';      // rom date (same as DOSBox)
                   18452:        mem[0xffff0 + 0x06] = '1';
                   18453:        mem[0xffff0 + 0x07] = '/';
                   18454:        mem[0xffff0 + 0x08] = '0';
                   18455:        mem[0xffff0 + 0x09] = '1';
                   18456:        mem[0xffff0 + 0x0a] = '/';
                   18457:        mem[0xffff0 + 0x0b] = '9';
                   18458:        mem[0xffff0 + 0x0c] = '2';
                   18459:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
                   18460:        mem[0xffff0 + 0x0f] = 0x55;     // signature
                   18461: #else
                   18462:        mem[0xffff0 + 0x05] = '0';      // rom date (same as Windows 98 SE)
1.1.1.49  root     18463:        mem[0xffff0 + 0x06] = '2';
                   18464:        mem[0xffff0 + 0x07] = '/';
                   18465:        mem[0xffff0 + 0x08] = '2';
                   18466:        mem[0xffff0 + 0x09] = '2';
                   18467:        mem[0xffff0 + 0x0a] = '/';
                   18468:        mem[0xffff0 + 0x0b] = '0';
                   18469:        mem[0xffff0 + 0x0c] = '6';
1.1.1.59  root     18470:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
1.1.1.49  root     18471:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.59  root     18472: #endif
1.1.1.24  root     18473:        
1.1       root     18474:        // param block
                   18475:        // + 0: param block (22bytes)
                   18476:        // +24: fcb1/2 (20bytes)
                   18477:        // +44: command tail (128bytes)
                   18478:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18479:        param->env_seg = 0;
                   18480:        param->cmd_line.w.l = 44;
                   18481:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18482:        param->fcb1.w.l = 24;
                   18483:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18484:        param->fcb2.w.l = 24;
                   18485:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18486:        
                   18487:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18488:        
                   18489:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18490:        if(argc > 1) {
                   18491:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18492:                for(int i = 2; i < argc; i++) {
                   18493:                        char tmp[128];
                   18494:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18495:                        strcpy(cmd_line->cmd, tmp);
                   18496:                }
                   18497:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18498:        } else {
                   18499:                cmd_line->len = 0;
                   18500:        }
                   18501:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18502:        
                   18503:        // system file table
1.1.1.21  root     18504:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18505:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18506:        
1.1.1.19  root     18507:        // disk buffer header (from DOSBox)
                   18508:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18509:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18510:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18511:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18512:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18513:        
1.1       root     18514:        // fcb table
                   18515:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18516:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18517:        
1.1.1.41  root     18518:        // drive parameter block
1.1.1.42  root     18519:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18520:                // may be a floppy drive
1.1.1.44  root     18521:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18522:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18523:                cds->drive_attrib = 0x4000;     // physical drive
                   18524:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18525:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18526:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18527:                cds->bs_offset = 2;
                   18528:                
1.1.1.41  root     18529:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18530:                dpb->drive_num = i;
                   18531:                dpb->unit_num = i;
1.1.1.43  root     18532:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18533:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18534:        }
                   18535:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18536:                msdos_cds_update(i);
1.1.1.42  root     18537:                UINT16 seg, ofs;
                   18538:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18539:        }
                   18540:        
1.1.1.17  root     18541:        // nls stuff
                   18542:        msdos_nls_tables_init();
1.1       root     18543:        
                   18544:        // execute command
1.1.1.28  root     18545:        try {
1.1.1.52  root     18546:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18547:                        fatalerror("'%s' not found\n", argv[0]);
                   18548:                }
                   18549:        } catch(...) {
                   18550:                // we should not reach here :-(
                   18551:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18552:        }
                   18553:        retval = 0;
                   18554:        return(0);
                   18555: }
                   18556: 
                   18557: #define remove_std_file(path) { \
                   18558:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18559:        if(fd != -1) { \
                   18560:                _lseek(fd, 0, SEEK_END); \
                   18561:                int size = _tell(fd); \
                   18562:                _close(fd); \
                   18563:                if(size == 0) { \
                   18564:                        remove(path); \
                   18565:                } \
                   18566:        } \
                   18567: }
                   18568: 
                   18569: void msdos_finish()
                   18570: {
                   18571:        for(int i = 0; i < MAX_FILES; i++) {
                   18572:                if(file_handler[i].valid) {
                   18573:                        _close(i);
                   18574:                }
                   18575:        }
1.1.1.21  root     18576: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18577:        remove_std_file("stdaux.txt");
1.1.1.21  root     18578: #endif
1.1.1.30  root     18579: #ifdef SUPPORT_XMS
                   18580:        msdos_xms_finish();
                   18581: #endif
1.1       root     18582:        msdos_dbcs_table_finish();
                   18583: }
                   18584: 
                   18585: /* ----------------------------------------------------------------------------
                   18586:        PC/AT hardware emulation
                   18587: ---------------------------------------------------------------------------- */
                   18588: 
                   18589: void hardware_init()
                   18590: {
1.1.1.3   root     18591:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18592:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18593:        m_IF = 1;
1.1.1.3   root     18594: #if defined(HAS_I386)
1.1       root     18595:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18596:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18597: #endif
                   18598:        i386_set_a20_line(0);
1.1.1.14  root     18599:        
1.1.1.19  root     18600:        ems_init();
1.1.1.25  root     18601:        dma_init();
1.1       root     18602:        pic_init();
1.1.1.25  root     18603:        pio_init();
1.1.1.8   root     18604: #ifdef PIT_ALWAYS_RUNNING
                   18605:        pit_init();
                   18606: #else
1.1       root     18607:        pit_active = 0;
                   18608: #endif
1.1.1.25  root     18609:        sio_init();
1.1.1.8   root     18610:        cmos_init();
                   18611:        kbd_init();
1.1       root     18612: }
                   18613: 
1.1.1.10  root     18614: void hardware_finish()
                   18615: {
                   18616: #if defined(HAS_I386)
                   18617:        vtlb_free(m_vtlb);
                   18618: #endif
1.1.1.19  root     18619:        ems_finish();
1.1.1.37  root     18620:        pio_finish();
1.1.1.25  root     18621:        sio_finish();
1.1.1.10  root     18622: }
                   18623: 
1.1.1.28  root     18624: void hardware_release()
                   18625: {
                   18626:        // release hardware resources when this program will be terminated abnormally
                   18627: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18628:        if(fp_debug_log != NULL) {
                   18629:                fclose(fp_debug_log);
                   18630:                fp_debug_log = NULL;
1.1.1.28  root     18631:        }
                   18632: #endif
                   18633: #if defined(HAS_I386)
                   18634:        vtlb_free(m_vtlb);
                   18635: #endif
                   18636:        ems_release();
1.1.1.37  root     18637:        pio_release();
1.1.1.28  root     18638:        sio_release();
                   18639: }
                   18640: 
1.1       root     18641: void hardware_run()
                   18642: {
1.1.1.22  root     18643: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18644:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18645:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18646: #endif
1.1.1.51  root     18647: #ifdef USE_DEBUGGER
                   18648:        m_int_num = -1;
                   18649: #endif
1.1.1.54  root     18650:        while(!m_exit) {
1.1.1.50  root     18651:                hardware_run_cpu();
1.1       root     18652:        }
1.1.1.22  root     18653: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18654:        if(fp_debug_log != NULL) {
                   18655:                fclose(fp_debug_log);
                   18656:                fp_debug_log = NULL;
1.1.1.28  root     18657:        }
1.1.1.22  root     18658: #endif
1.1       root     18659: }
                   18660: 
1.1.1.50  root     18661: inline void hardware_run_cpu()
                   18662: {
                   18663: #if defined(HAS_I386)
                   18664:        CPU_EXECUTE_CALL(i386);
                   18665:        if(m_eip != m_prev_eip) {
                   18666:                idle_ops++;
                   18667:        }
                   18668: #else
                   18669:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18670:        if(m_pc != m_prevpc) {
                   18671:                idle_ops++;
                   18672:        }
                   18673: #endif
                   18674: #ifdef USE_DEBUGGER
                   18675:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18676:        if(m_int_num >= 0) {
                   18677:                unsigned num = (unsigned)m_int_num;
                   18678:                m_int_num = -1;
                   18679:                msdos_syscall(num);
                   18680:        }
                   18681: #endif
                   18682:        if(++update_ops == UPDATE_OPS) {
                   18683:                update_ops = 0;
                   18684:                hardware_update();
                   18685:        }
                   18686: }
                   18687: 
1.1       root     18688: void hardware_update()
                   18689: {
1.1.1.8   root     18690:        static UINT32 prev_time = 0;
                   18691:        UINT32 cur_time = timeGetTime();
                   18692:        
                   18693:        if(prev_time != cur_time) {
                   18694:                // update pit and raise irq0
                   18695: #ifndef PIT_ALWAYS_RUNNING
                   18696:                if(pit_active)
                   18697: #endif
                   18698:                {
                   18699:                        if(pit_run(0, cur_time)) {
                   18700:                                pic_req(0, 0, 1);
                   18701:                        }
                   18702:                        pit_run(1, cur_time);
                   18703:                        pit_run(2, cur_time);
                   18704:                }
1.1.1.24  root     18705:                
1.1.1.25  root     18706:                // update sio and raise irq4/3
1.1.1.29  root     18707:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18708:                        sio_update(c);
                   18709:                }
                   18710:                
1.1.1.24  root     18711:                // update keyboard and mouse
1.1.1.14  root     18712:                static UINT32 prev_tick = 0;
                   18713:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18714:                
1.1.1.14  root     18715:                if(prev_tick != cur_tick) {
                   18716:                        // update keyboard flags
                   18717:                        UINT8 state;
1.1.1.24  root     18718:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18719:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18720:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18721:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18722:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18723:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18724:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18725:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18726:                        mem[0x417] = state;
                   18727:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18728:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18729:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18730:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18731: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18732: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18733:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18734:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18735:                        mem[0x418] = state;
                   18736:                        
1.1.1.24  root     18737:                        // update console input if needed
1.1.1.34  root     18738:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18739:                                update_console_input();
                   18740:                        }
1.1.1.57  root     18741:                        if(!(kbd_status & 1)) {
                   18742:                                if(key_buf_data != NULL) {
                   18743: #ifdef USE_SERVICE_THREAD
                   18744:                                        EnterCriticalSection(&key_buf_crit_sect);
                   18745: #endif
                   18746:                                        if(!key_buf_data->empty()) {
                   18747:                                                kbd_data = key_buf_data->read();
                   18748:                                                kbd_status |= 1;
                   18749:                                                key_changed = true;
                   18750:                                        }
                   18751: #ifdef USE_SERVICE_THREAD
                   18752:                                        LeaveCriticalSection(&key_buf_crit_sect);
                   18753: #endif
                   18754:                                }
                   18755:                        }
1.1.1.24  root     18756:                        
1.1.1.57  root     18757:                        // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56  root     18758:                        if(!key_changed) {
1.1.1.55  root     18759: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18760:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18761: #endif
1.1.1.57  root     18762:                                if(!pcbios_is_key_buffer_empty()) {
                   18763: /*
                   18764:                                        if(!(kbd_status & 1)) {
                   18765:                                                UINT16 head = *(UINT16 *)(mem + 0x41a);
                   18766:                                                UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   18767:                                                if(head != tail) {
                   18768:                                                        int key_char = mem[0x400 + (head++)];
                   18769:                                                        int key_scan = mem[0x400 + (head++)];
                   18770:                                                        kbd_data = key_char ? key_char : key_scan;
                   18771:                                                        kbd_status |= 1;
                   18772:                                                }
                   18773:                                        }
                   18774: */
                   18775:                                        key_changed = true;
                   18776:                                }
1.1.1.55  root     18777: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18778:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18779: #endif
1.1.1.56  root     18780:                        }
                   18781:                        if(key_changed) {
1.1.1.8   root     18782:                                pic_req(0, 1, 1);
1.1.1.56  root     18783:                                key_changed = false;
1.1.1.24  root     18784:                        }
                   18785:                        
                   18786:                        // raise irq12 if mouse status is changed
1.1.1.59  root     18787:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     18788:                                mouse.status_irq = 0; // ???
                   18789:                                mouse.status_irq_alt = 0; // ???
                   18790:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   18791:                                mouse.status = 0;
                   18792:                                pic_req(1, 4, 1);
1.1.1.59  root     18793:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     18794:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18795:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     18796:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     18797:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18798:                                pic_req(1, 4, 1);
                   18799:                        } else {
                   18800:                                for(int i = 0; i < 8; i++) {
                   18801:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18802:                                                mouse.status_irq = 0; // ???
                   18803:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     18804:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     18805:                                                for(int j = 0; j < 8; j++) {
                   18806:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18807:                                                                mouse.status_irq_alt |=  (1 << j);
                   18808:                                                                mouse.status_alt     &= ~(1 << j);
                   18809:                                                        }
                   18810:                                                }
                   18811:                                                pic_req(1, 4, 1);
                   18812:                                                break;
                   18813:                                        }
                   18814:                                }
1.1.1.8   root     18815:                        }
1.1.1.24  root     18816:                        
1.1.1.60  root     18817:                        prev_tick = cur_tick;
                   18818:                }
                   18819:                
                   18820:                // update cursor size/position by crtc
                   18821:                if(crtc_changed[10] != 0 || crtc_changed[11] != 0) {
                   18822:                        int size = (int)(crtc_regs[11] & 7) - (int)(crtc_regs[10] & 7) + 1;
                   18823:                        if(!((crtc_regs[10] & 0x20) != 0 || size < 0)) {
                   18824:                                ci_new.bVisible = TRUE;
                   18825:                                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
1.1.1.59  root     18826:                        } else {
1.1.1.60  root     18827:                                ci_new.bVisible = FALSE;
                   18828:                        }
                   18829:                        crtc_changed[10] = crtc_changed[11] = 0;
                   18830:                }
                   18831:                if(crtc_changed[14] != 0 || crtc_changed[15] != 0) {
                   18832:                        if(cursor_moved) {
                   18833:                                pcbios_update_cursor_position();
                   18834:                                cursor_moved = false;
1.1.1.59  root     18835:                        }
1.1.1.60  root     18836:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   18837:                        int position = crtc_regs[14] * 256 + crtc_regs[15];
                   18838:                        int width = *(UINT16 *)(mem + 0x44a);
                   18839:                        COORD co;
                   18840:                        co.X = position % width;
                   18841:                        co.Y = position / width + scr_top;
                   18842:                        SetConsoleCursorPosition(hStdout, co);
1.1.1.59  root     18843:                        
1.1.1.60  root     18844:                        crtc_changed[14] = crtc_changed[15] = 0;
                   18845:                        cursor_moved_by_crtc = true;
                   18846:                }
                   18847:                
                   18848:                // update cursor info
                   18849:                if(!is_cursor_blink_off()) {
                   18850:                        ci_new.bVisible = TRUE;
                   18851:                }
                   18852:                if(!(ci_old.dwSize == ci_new.dwSize && ci_old.bVisible == ci_new.bVisible)) {
                   18853:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   18854:                        SetConsoleCursorInfo(hStdout, &ci_new);
1.1.1.8   root     18855:                }
1.1.1.60  root     18856:                ci_old = ci_new;
1.1.1.24  root     18857:                
1.1.1.19  root     18858:                // update daily timer counter
                   18859:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18860:                
1.1.1.8   root     18861:                prev_time = cur_time;
1.1       root     18862:        }
                   18863: }
                   18864: 
1.1.1.19  root     18865: // ems
                   18866: 
                   18867: void ems_init()
                   18868: {
                   18869:        memset(ems_handles, 0, sizeof(ems_handles));
                   18870:        memset(ems_pages, 0, sizeof(ems_pages));
                   18871:        free_ems_pages = MAX_EMS_PAGES;
                   18872: }
                   18873: 
                   18874: void ems_finish()
                   18875: {
1.1.1.28  root     18876:        ems_release();
                   18877: }
                   18878: 
                   18879: void ems_release()
                   18880: {
1.1.1.31  root     18881:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18882:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18883:                        free(ems_handles[i].buffer);
                   18884:                        ems_handles[i].buffer = NULL;
                   18885:                }
                   18886:        }
                   18887: }
                   18888: 
                   18889: void ems_allocate_pages(int handle, int pages)
                   18890: {
                   18891:        if(pages > 0) {
                   18892:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18893:        } else {
                   18894:                ems_handles[handle].buffer = NULL;
                   18895:        }
                   18896:        ems_handles[handle].pages = pages;
                   18897:        ems_handles[handle].allocated = true;
                   18898:        free_ems_pages -= pages;
                   18899: }
                   18900: 
                   18901: void ems_reallocate_pages(int handle, int pages)
                   18902: {
                   18903:        if(ems_handles[handle].allocated) {
                   18904:                if(ems_handles[handle].pages != pages) {
                   18905:                        UINT8 *new_buffer = NULL;
                   18906:                        
                   18907:                        if(pages > 0) {
                   18908:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18909:                        }
1.1.1.32  root     18910:                        if(ems_handles[handle].buffer != NULL) {
                   18911:                                if(new_buffer != NULL) {
1.1.1.19  root     18912:                                        if(pages > ems_handles[handle].pages) {
                   18913:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18914:                                        } else {
                   18915:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18916:                                        }
                   18917:                                }
                   18918:                                free(ems_handles[handle].buffer);
                   18919:                                ems_handles[handle].buffer = NULL;
                   18920:                        }
                   18921:                        free_ems_pages += ems_handles[handle].pages;
                   18922:                        
                   18923:                        ems_handles[handle].buffer = new_buffer;
                   18924:                        ems_handles[handle].pages = pages;
                   18925:                        free_ems_pages -= pages;
                   18926:                }
                   18927:        } else {
                   18928:                ems_allocate_pages(handle, pages);
                   18929:        }
                   18930: }
                   18931: 
                   18932: void ems_release_pages(int handle)
                   18933: {
                   18934:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18935:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18936:                        free(ems_handles[handle].buffer);
                   18937:                        ems_handles[handle].buffer = NULL;
                   18938:                }
                   18939:                free_ems_pages += ems_handles[handle].pages;
                   18940:                ems_handles[handle].allocated = false;
                   18941:        }
                   18942: }
                   18943: 
                   18944: void ems_map_page(int physical, int handle, int logical)
                   18945: {
                   18946:        if(ems_pages[physical].mapped) {
                   18947:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18948:                        return;
                   18949:                }
                   18950:                ems_unmap_page(physical);
                   18951:        }
1.1.1.32  root     18952:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18953:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18954:        }
                   18955:        ems_pages[physical].handle = handle;
                   18956:        ems_pages[physical].page = logical;
                   18957:        ems_pages[physical].mapped = true;
                   18958: }
                   18959: 
                   18960: void ems_unmap_page(int physical)
                   18961: {
                   18962:        if(ems_pages[physical].mapped) {
                   18963:                int handle = ems_pages[physical].handle;
                   18964:                int logical = ems_pages[physical].page;
                   18965:                
1.1.1.32  root     18966:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18967:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18968:                }
                   18969:                ems_pages[physical].mapped = false;
                   18970:        }
                   18971: }
                   18972: 
1.1.1.25  root     18973: // dma
1.1       root     18974: 
1.1.1.25  root     18975: void dma_init()
1.1       root     18976: {
1.1.1.26  root     18977:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     18978:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     18979: //             for(int ch = 0; ch < 4; ch++) {
                   18980: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   18981: //             }
1.1.1.25  root     18982:                dma_reset(c);
                   18983:        }
1.1       root     18984: }
                   18985: 
1.1.1.25  root     18986: void dma_reset(int c)
1.1       root     18987: {
1.1.1.25  root     18988:        dma[c].low_high = false;
                   18989:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   18990:        dma[c].mask = 0xff;
                   18991: }
                   18992: 
                   18993: void dma_write(int c, UINT32 addr, UINT8 data)
                   18994: {
                   18995:        int ch = (addr >> 1) & 3;
                   18996:        UINT8 bit = 1 << (data & 3);
                   18997:        
                   18998:        switch(addr & 0x0f) {
                   18999:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19000:                if(dma[c].low_high) {
                   19001:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     19002:                } else {
1.1.1.25  root     19003:                        dma[c].ch[ch].bareg.b.l = data;
                   19004:                }
                   19005:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19006:                dma[c].low_high = !dma[c].low_high;
                   19007:                break;
                   19008:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19009:                if(dma[c].low_high) {
                   19010:                        dma[c].ch[ch].bcreg.b.h = data;
                   19011:                } else {
                   19012:                        dma[c].ch[ch].bcreg.b.l = data;
                   19013:                }
                   19014:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19015:                dma[c].low_high = !dma[c].low_high;
                   19016:                break;
                   19017:        case 0x08:
                   19018:                // command register
                   19019:                dma[c].cmd = data;
                   19020:                break;
                   19021:        case 0x09:
                   19022:                // dma[c].request register
                   19023:                if(data & 4) {
                   19024:                        if(!(dma[c].req & bit)) {
                   19025:                                dma[c].req |= bit;
                   19026: //                             dma_run(c, ch);
                   19027:                        }
                   19028:                } else {
                   19029:                        dma[c].req &= ~bit;
                   19030:                }
                   19031:                break;
                   19032:        case 0x0a:
                   19033:                // single mask register
                   19034:                if(data & 4) {
                   19035:                        dma[c].mask |= bit;
                   19036:                } else {
                   19037:                        dma[c].mask &= ~bit;
                   19038:                }
                   19039:                break;
                   19040:        case 0x0b:
                   19041:                // mode register
                   19042:                dma[c].ch[data & 3].mode = data;
                   19043:                break;
                   19044:        case 0x0c:
                   19045:                dma[c].low_high = false;
                   19046:                break;
                   19047:        case 0x0d:
                   19048:                // clear master
                   19049:                dma_reset(c);
                   19050:                break;
                   19051:        case 0x0e:
                   19052:                // clear mask register
                   19053:                dma[c].mask = 0;
                   19054:                break;
                   19055:        case 0x0f:
                   19056:                // all mask register
                   19057:                dma[c].mask = data & 0x0f;
                   19058:                break;
                   19059:        }
                   19060: }
                   19061: 
                   19062: UINT8 dma_read(int c, UINT32 addr)
                   19063: {
                   19064:        int ch = (addr >> 1) & 3;
                   19065:        UINT8 val = 0xff;
                   19066:        
                   19067:        switch(addr & 0x0f) {
                   19068:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19069:                if(dma[c].low_high) {
                   19070:                        val = dma[c].ch[ch].areg.b.h;
                   19071:                } else {
                   19072:                        val = dma[c].ch[ch].areg.b.l;
                   19073:                }
                   19074:                dma[c].low_high = !dma[c].low_high;
                   19075:                return(val);
                   19076:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19077:                if(dma[c].low_high) {
                   19078:                        val = dma[c].ch[ch].creg.b.h;
                   19079:                } else {
                   19080:                        val = dma[c].ch[ch].creg.b.l;
                   19081:                }
                   19082:                dma[c].low_high = !dma[c].low_high;
                   19083:                return(val);
                   19084:        case 0x08:
                   19085:                // status register
                   19086:                val = (dma[c].req << 4) | dma[c].tc;
                   19087:                dma[c].tc = 0;
                   19088:                return(val);
                   19089:        case 0x0d:
1.1.1.26  root     19090:                // temporary register (intel 82374 does not support)
1.1.1.25  root     19091:                return(dma[c].tmp & 0xff);
1.1.1.26  root     19092:        case 0x0f:
                   19093:                // mask register (intel 82374 does support)
                   19094:                return(dma[c].mask);
1.1.1.25  root     19095:        }
                   19096:        return(0xff);
                   19097: }
                   19098: 
                   19099: void dma_page_write(int c, int ch, UINT8 data)
                   19100: {
                   19101:        dma[c].ch[ch].pagereg = data;
                   19102: }
                   19103: 
                   19104: UINT8 dma_page_read(int c, int ch)
                   19105: {
                   19106:        return(dma[c].ch[ch].pagereg);
                   19107: }
                   19108: 
                   19109: void dma_run(int c, int ch)
                   19110: {
                   19111:        UINT8 bit = 1 << ch;
                   19112:        
                   19113:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   19114:                // execute dma
                   19115:                while(dma[c].req & bit) {
                   19116:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   19117:                                // memory -> memory
                   19118:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   19119:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   19120:                                
                   19121:                                if(c == 0) {
                   19122:                                        dma[c].tmp = read_byte(saddr);
                   19123:                                        write_byte(daddr, dma[c].tmp);
                   19124:                                } else {
                   19125:                                        dma[c].tmp = read_word(saddr << 1);
                   19126:                                        write_word(daddr << 1, dma[c].tmp);
                   19127:                                }
                   19128:                                if(!(dma[c].cmd & 0x02)) {
                   19129:                                        if(dma[c].ch[0].mode & 0x20) {
                   19130:                                                dma[c].ch[0].areg.w--;
                   19131:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   19132:                                                        dma[c].ch[0].pagereg--;
                   19133:                                                }
                   19134:                                        } else {
                   19135:                                                dma[c].ch[0].areg.w++;
                   19136:                                                if(dma[c].ch[0].areg.w == 0) {
                   19137:                                                        dma[c].ch[0].pagereg++;
                   19138:                                                }
                   19139:                                        }
                   19140:                                }
                   19141:                                if(dma[c].ch[1].mode & 0x20) {
                   19142:                                        dma[c].ch[1].areg.w--;
                   19143:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   19144:                                                dma[c].ch[1].pagereg--;
                   19145:                                        }
                   19146:                                } else {
                   19147:                                        dma[c].ch[1].areg.w++;
                   19148:                                        if(dma[c].ch[1].areg.w == 0) {
                   19149:                                                dma[c].ch[1].pagereg++;
                   19150:                                        }
                   19151:                                }
                   19152:                                
                   19153:                                // check dma condition
                   19154:                                if(dma[c].ch[0].creg.w-- == 0) {
                   19155:                                        if(dma[c].ch[0].mode & 0x10) {
                   19156:                                                // self initialize
                   19157:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   19158:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   19159:                                        } else {
                   19160: //                                             dma[c].mask |= bit;
                   19161:                                        }
                   19162:                                }
                   19163:                                if(dma[c].ch[1].creg.w-- == 0) {
                   19164:                                        // terminal count
                   19165:                                        if(dma[c].ch[1].mode & 0x10) {
                   19166:                                                // self initialize
                   19167:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   19168:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   19169:                                        } else {
                   19170:                                                dma[c].mask |= bit;
                   19171:                                        }
                   19172:                                        dma[c].req &= ~bit;
                   19173:                                        dma[c].tc |= bit;
                   19174:                                }
                   19175:                        } else {
                   19176:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   19177:                                
                   19178:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   19179:                                        // verify
                   19180:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   19181:                                        // io -> memory
                   19182:                                        if(c == 0) {
                   19183:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   19184:                                                write_byte(addr, dma[c].tmp);
                   19185:                                        } else {
                   19186:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   19187:                                                write_word(addr << 1, dma[c].tmp);
                   19188:                                        }
                   19189:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   19190:                                        // memory -> io
                   19191:                                        if(c == 0) {
                   19192:                                                dma[c].tmp = read_byte(addr);
                   19193:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   19194:                                        } else {
                   19195:                                                dma[c].tmp = read_word(addr << 1);
                   19196:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   19197:                                        }
                   19198:                                }
                   19199:                                if(dma[c].ch[ch].mode & 0x20) {
                   19200:                                        dma[c].ch[ch].areg.w--;
                   19201:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   19202:                                                dma[c].ch[ch].pagereg--;
                   19203:                                        }
                   19204:                                } else {
                   19205:                                        dma[c].ch[ch].areg.w++;
                   19206:                                        if(dma[c].ch[ch].areg.w == 0) {
                   19207:                                                dma[c].ch[ch].pagereg++;
                   19208:                                        }
                   19209:                                }
                   19210:                                
                   19211:                                // check dma condition
                   19212:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   19213:                                        // terminal count
                   19214:                                        if(dma[c].ch[ch].mode & 0x10) {
                   19215:                                                // self initialize
                   19216:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19217:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19218:                                        } else {
                   19219:                                                dma[c].mask |= bit;
                   19220:                                        }
                   19221:                                        dma[c].req &= ~bit;
                   19222:                                        dma[c].tc |= bit;
                   19223:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   19224:                                        // single mode
                   19225:                                        break;
                   19226:                                }
                   19227:                        }
                   19228:                }
                   19229:        }
                   19230: }
                   19231: 
                   19232: // pic
                   19233: 
                   19234: void pic_init()
                   19235: {
                   19236:        memset(pic, 0, sizeof(pic));
                   19237:        pic[0].imr = pic[1].imr = 0xff;
                   19238:        
                   19239:        // from bochs bios
                   19240:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   19241:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   19242:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   19243:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   19244:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   19245:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   19246:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   19247:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   19248:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   19249: }
                   19250: 
                   19251: void pic_write(int c, UINT32 addr, UINT8 data)
                   19252: {
                   19253:        if(addr & 1) {
                   19254:                if(pic[c].icw2_r) {
                   19255:                        // icw2
                   19256:                        pic[c].icw2 = data;
                   19257:                        pic[c].icw2_r = 0;
                   19258:                } else if(pic[c].icw3_r) {
                   19259:                        // icw3
                   19260:                        pic[c].icw3 = data;
                   19261:                        pic[c].icw3_r = 0;
                   19262:                } else if(pic[c].icw4_r) {
                   19263:                        // icw4
                   19264:                        pic[c].icw4 = data;
                   19265:                        pic[c].icw4_r = 0;
                   19266:                } else {
                   19267:                        // ocw1
1.1       root     19268:                        pic[c].imr = data;
                   19269:                }
                   19270:        } else {
                   19271:                if(data & 0x10) {
                   19272:                        // icw1
                   19273:                        pic[c].icw1 = data;
                   19274:                        pic[c].icw2_r = 1;
                   19275:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   19276:                        pic[c].icw4_r = data & 1;
                   19277:                        pic[c].irr = 0;
                   19278:                        pic[c].isr = 0;
                   19279:                        pic[c].imr = 0;
                   19280:                        pic[c].prio = 0;
                   19281:                        if(!(pic[c].icw1 & 1)) {
                   19282:                                pic[c].icw4 = 0;
                   19283:                        }
                   19284:                        pic[c].ocw3 = 0;
                   19285:                } else if(data & 8) {
                   19286:                        // ocw3
                   19287:                        if(!(data & 2)) {
                   19288:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   19289:                        }
                   19290:                        if(!(data & 0x40)) {
                   19291:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   19292:                        }
                   19293:                        pic[c].ocw3 = data;
                   19294:                } else {
                   19295:                        // ocw2
                   19296:                        int level = 0;
                   19297:                        if(data & 0x40) {
                   19298:                                level = data & 7;
                   19299:                        } else {
                   19300:                                if(!pic[c].isr) {
                   19301:                                        return;
                   19302:                                }
                   19303:                                level = pic[c].prio;
                   19304:                                while(!(pic[c].isr & (1 << level))) {
                   19305:                                        level = (level + 1) & 7;
                   19306:                                }
                   19307:                        }
                   19308:                        if(data & 0x80) {
                   19309:                                pic[c].prio = (level + 1) & 7;
                   19310:                        }
                   19311:                        if(data & 0x20) {
                   19312:                                pic[c].isr &= ~(1 << level);
                   19313:                        }
                   19314:                }
                   19315:        }
                   19316:        pic_update();
                   19317: }
                   19318: 
                   19319: UINT8 pic_read(int c, UINT32 addr)
                   19320: {
                   19321:        if(addr & 1) {
                   19322:                return(pic[c].imr);
                   19323:        } else {
                   19324:                // polling mode is not supported...
                   19325:                //if(pic[c].ocw3 & 4) {
                   19326:                //      return ???;
                   19327:                //}
                   19328:                if(pic[c].ocw3 & 1) {
                   19329:                        return(pic[c].isr);
                   19330:                } else {
                   19331:                        return(pic[c].irr);
                   19332:                }
                   19333:        }
                   19334: }
                   19335: 
                   19336: void pic_req(int c, int level, int signal)
                   19337: {
                   19338:        if(signal) {
                   19339:                pic[c].irr |= (1 << level);
                   19340:        } else {
                   19341:                pic[c].irr &= ~(1 << level);
                   19342:        }
                   19343:        pic_update();
                   19344: }
                   19345: 
                   19346: int pic_ack()
                   19347: {
                   19348:        // ack (INTA=L)
                   19349:        pic[pic_req_chip].isr |= pic_req_bit;
                   19350:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   19351:        if(pic_req_chip > 0) {
                   19352:                // update isr and irr of master
                   19353:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   19354:                pic[pic_req_chip - 1].isr |= slave;
                   19355:                pic[pic_req_chip - 1].irr &= ~slave;
                   19356:        }
                   19357:        //if(pic[pic_req_chip].icw4 & 1) {
                   19358:                // 8086 mode
                   19359:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   19360:        //} else {
                   19361:        //      // 8080 mode
                   19362:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   19363:        //      if(pic[pic_req_chip].icw1 & 4) {
                   19364:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   19365:        //      } else {
                   19366:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   19367:        //      }
                   19368:        //      vector = 0xcd | (addr << 8);
                   19369:        //}
                   19370:        if(pic[pic_req_chip].icw4 & 2) {
                   19371:                // auto eoi
                   19372:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   19373:        }
                   19374:        return(vector);
                   19375: }
                   19376: 
                   19377: void pic_update()
                   19378: {
                   19379:        for(int c = 0; c < 2; c++) {
                   19380:                UINT8 irr = pic[c].irr;
                   19381:                if(c + 1 < 2) {
                   19382:                        // this is master
                   19383:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   19384:                                // request from slave
                   19385:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   19386:                        }
                   19387:                }
                   19388:                irr &= (~pic[c].imr);
                   19389:                if(!irr) {
                   19390:                        break;
                   19391:                }
                   19392:                if(!(pic[c].ocw3 & 0x20)) {
                   19393:                        irr |= pic[c].isr;
                   19394:                }
                   19395:                int level = pic[c].prio;
                   19396:                UINT8 bit = 1 << level;
                   19397:                while(!(irr & bit)) {
                   19398:                        level = (level + 1) & 7;
                   19399:                        bit = 1 << level;
                   19400:                }
                   19401:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   19402:                        // check slave
                   19403:                        continue;
                   19404:                }
                   19405:                if(pic[c].isr & bit) {
                   19406:                        break;
                   19407:                }
                   19408:                // interrupt request
                   19409:                pic_req_chip = c;
                   19410:                pic_req_level = level;
                   19411:                pic_req_bit = bit;
1.1.1.3   root     19412:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     19413:                return;
                   19414:        }
1.1.1.3   root     19415:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     19416: }
1.1       root     19417: 
1.1.1.25  root     19418: // pio
                   19419: 
                   19420: void pio_init()
                   19421: {
1.1.1.38  root     19422: //     bool conv_mode = (GetConsoleCP() == 932);
                   19423:        
1.1.1.26  root     19424:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     19425:        
1.1.1.25  root     19426:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     19427:                pio[c].stat = 0xdf;
1.1.1.25  root     19428:                pio[c].ctrl = 0x0c;
1.1.1.38  root     19429: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     19430:        }
                   19431: }
                   19432: 
1.1.1.37  root     19433: void pio_finish()
                   19434: {
                   19435:        pio_release();
                   19436: }
                   19437: 
                   19438: void pio_release()
                   19439: {
                   19440:        for(int c = 0; c < 2; c++) {
                   19441:                if(pio[c].fp != NULL) {
1.1.1.38  root     19442:                        if(pio[c].jis_mode) {
                   19443:                                fputc(0x1c, pio[c].fp);
                   19444:                                fputc(0x2e, pio[c].fp);
                   19445:                        }
1.1.1.37  root     19446:                        fclose(pio[c].fp);
                   19447:                        pio[c].fp = NULL;
                   19448:                }
                   19449:        }
                   19450: }
                   19451: 
1.1.1.25  root     19452: void pio_write(int c, UINT32 addr, UINT8 data)
                   19453: {
                   19454:        switch(addr & 3) {
                   19455:        case 0:
                   19456:                pio[c].data = data;
                   19457:                break;
                   19458:        case 2:
1.1.1.37  root     19459:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19460:                        // strobe H -> L
                   19461:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19462:                                // auto feed
                   19463:                                printer_out(c, 0x0d);
                   19464:                                printer_out(c, 0x0a);
                   19465:                        } else {
                   19466:                                printer_out(c, pio[c].data);
                   19467:                        }
                   19468:                        pio[c].stat &= ~0x40; // set ack
                   19469:                }
1.1.1.25  root     19470:                pio[c].ctrl = data;
                   19471:                break;
                   19472:        }
                   19473: }
                   19474: 
                   19475: UINT8 pio_read(int c, UINT32 addr)
                   19476: {
                   19477:        switch(addr & 3) {
                   19478:        case 0:
1.1.1.37  root     19479:                if(pio[c].ctrl & 0x20) {
                   19480:                        // input mode
                   19481:                        return(0xff);
                   19482:                }
1.1.1.25  root     19483:                return(pio[c].data);
                   19484:        case 1:
1.1.1.37  root     19485:                {
                   19486:                        UINT8 stat = pio[c].stat;
                   19487:                        pio[c].stat |= 0x40; // clear ack
                   19488:                        return(stat);
                   19489:                }
1.1.1.25  root     19490:        case 2:
                   19491:                return(pio[c].ctrl);
                   19492:        }
                   19493:        return(0xff);
                   19494: }
                   19495: 
1.1.1.37  root     19496: void printer_out(int c, UINT8 data)
                   19497: {
                   19498:        SYSTEMTIME time;
1.1.1.38  root     19499:        bool jis_mode = false;
1.1.1.37  root     19500:        
                   19501:        GetLocalTime(&time);
                   19502:        
                   19503:        if(pio[c].fp != NULL) {
                   19504:                // if at least 1000ms passed from last written, close the current file
                   19505:                FILETIME ftime1;
                   19506:                FILETIME ftime2;
                   19507:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19508:                SystemTimeToFileTime(&time, &ftime2);
                   19509:                INT64 *time1 = (INT64 *)&ftime1;
                   19510:                INT64 *time2 = (INT64 *)&ftime2;
                   19511:                INT64 msec = (*time2 - *time1) / 10000;
                   19512:                
                   19513:                if(msec >= 1000) {
1.1.1.38  root     19514:                        if(pio[c].jis_mode) {
                   19515:                                fputc(0x1c, pio[c].fp);
                   19516:                                fputc(0x2e, pio[c].fp);
                   19517:                                jis_mode = true;
                   19518:                        }
1.1.1.37  root     19519:                        fclose(pio[c].fp);
                   19520:                        pio[c].fp = NULL;
                   19521:                }
                   19522:        }
                   19523:        if(pio[c].fp == NULL) {
                   19524:                // create a new file in the temp folder
                   19525:                char file_name[MAX_PATH];
                   19526:                
                   19527:                sprintf(file_name, "%d-%0.2d-%0.2d_%0.2d-%0.2d-%0.2d.PRN", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
1.1.1.60  root     19528:                if(GetTempPathA(MAX_PATH, pio[c].path)) {
1.1.1.37  root     19529:                        strcat(pio[c].path, file_name);
                   19530:                } else {
                   19531:                        strcpy(pio[c].path, file_name);
                   19532:                }
1.1.1.38  root     19533:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19534:        }
                   19535:        if(pio[c].fp != NULL) {
1.1.1.38  root     19536:                if(jis_mode) {
                   19537:                        fputc(0x1c, pio[c].fp);
                   19538:                        fputc(0x26, pio[c].fp);
                   19539:                }
1.1.1.37  root     19540:                fputc(data, pio[c].fp);
1.1.1.38  root     19541:                
                   19542:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19543:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19544:                        UINT8 buffer[4];
                   19545:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19546:                        fread(buffer, 4, 1, pio[c].fp);
                   19547:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19548:                                fclose(pio[c].fp);
                   19549:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19550:                        }
                   19551:                }
1.1.1.37  root     19552:                pio[c].time = time;
                   19553:        }
                   19554: }
                   19555: 
1.1       root     19556: // pit
                   19557: 
1.1.1.22  root     19558: #define PIT_FREQ 1193182ULL
1.1       root     19559: #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)
                   19560: 
                   19561: void pit_init()
                   19562: {
1.1.1.8   root     19563:        memset(pit, 0, sizeof(pit));
1.1       root     19564:        for(int ch = 0; ch < 3; ch++) {
                   19565:                pit[ch].count = 0x10000;
                   19566:                pit[ch].ctrl_reg = 0x34;
                   19567:                pit[ch].mode = 3;
                   19568:        }
                   19569:        
                   19570:        // from bochs bios
                   19571:        pit_write(3, 0x34);
                   19572:        pit_write(0, 0x00);
                   19573:        pit_write(0, 0x00);
                   19574: }
                   19575: 
                   19576: void pit_write(int ch, UINT8 val)
                   19577: {
1.1.1.8   root     19578: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19579:        if(!pit_active) {
                   19580:                pit_active = 1;
                   19581:                pit_init();
                   19582:        }
1.1.1.8   root     19583: #endif
1.1       root     19584:        switch(ch) {
                   19585:        case 0:
                   19586:        case 1:
                   19587:        case 2:
                   19588:                // write count register
                   19589:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19590:                        if(pit[ch].ctrl_reg & 0x10) {
                   19591:                                pit[ch].low_write = 1;
                   19592:                        }
                   19593:                        if(pit[ch].ctrl_reg & 0x20) {
                   19594:                                pit[ch].high_write = 1;
                   19595:                        }
                   19596:                }
                   19597:                if(pit[ch].low_write) {
                   19598:                        pit[ch].count_reg = val;
                   19599:                        pit[ch].low_write = 0;
                   19600:                } else if(pit[ch].high_write) {
                   19601:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19602:                                pit[ch].count_reg = val << 8;
                   19603:                        } else {
                   19604:                                pit[ch].count_reg |= val << 8;
                   19605:                        }
                   19606:                        pit[ch].high_write = 0;
                   19607:                }
                   19608:                // start count
1.1.1.8   root     19609:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19610:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19611:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19612:                                pit[ch].prev_time = timeGetTime();
                   19613:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19614:                        }
                   19615:                }
                   19616:                break;
                   19617:        case 3: // ctrl reg
                   19618:                if((val & 0xc0) == 0xc0) {
                   19619:                        // i8254 read-back command
                   19620:                        for(ch = 0; ch < 3; ch++) {
                   19621:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19622:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19623:                                        pit[ch].status_latched = 1;
                   19624:                                }
                   19625:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19626:                                        pit_latch_count(ch);
                   19627:                                }
                   19628:                        }
                   19629:                        break;
                   19630:                }
                   19631:                ch = (val >> 6) & 3;
                   19632:                if(val & 0x30) {
1.1.1.35  root     19633:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19634:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19635:                        pit[ch].count_latched = 0;
                   19636:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19637:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19638:                        pit[ch].ctrl_reg = val;
                   19639:                        // stop count
1.1.1.8   root     19640:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19641:                        pit[ch].count_reg = 0;
                   19642:                } else if(!pit[ch].count_latched) {
                   19643:                        pit_latch_count(ch);
                   19644:                }
                   19645:                break;
                   19646:        }
                   19647: }
                   19648: 
                   19649: UINT8 pit_read(int ch)
                   19650: {
1.1.1.8   root     19651: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19652:        if(!pit_active) {
                   19653:                pit_active = 1;
                   19654:                pit_init();
                   19655:        }
1.1.1.8   root     19656: #endif
1.1       root     19657:        switch(ch) {
                   19658:        case 0:
                   19659:        case 1:
                   19660:        case 2:
                   19661:                if(pit[ch].status_latched) {
                   19662:                        pit[ch].status_latched = 0;
                   19663:                        return(pit[ch].status);
                   19664:                }
                   19665:                // if not latched, through current count
                   19666:                if(!pit[ch].count_latched) {
                   19667:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19668:                                pit_latch_count(ch);
                   19669:                        }
                   19670:                }
                   19671:                // return latched count
                   19672:                if(pit[ch].low_read) {
                   19673:                        pit[ch].low_read = 0;
                   19674:                        if(!pit[ch].high_read) {
                   19675:                                pit[ch].count_latched = 0;
                   19676:                        }
                   19677:                        return(pit[ch].latch & 0xff);
                   19678:                } else if(pit[ch].high_read) {
                   19679:                        pit[ch].high_read = 0;
                   19680:                        pit[ch].count_latched = 0;
                   19681:                        return((pit[ch].latch >> 8) & 0xff);
                   19682:                }
                   19683:        }
                   19684:        return(0xff);
                   19685: }
                   19686: 
1.1.1.8   root     19687: int pit_run(int ch, UINT32 cur_time)
1.1       root     19688: {
1.1.1.8   root     19689:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19690:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19691:                pit[ch].prev_time = pit[ch].expired_time;
                   19692:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19693:                if(cur_time >= pit[ch].expired_time) {
                   19694:                        pit[ch].prev_time = cur_time;
                   19695:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19696:                }
1.1.1.8   root     19697:                return(1);
1.1       root     19698:        }
1.1.1.8   root     19699:        return(0);
1.1       root     19700: }
                   19701: 
                   19702: void pit_latch_count(int ch)
                   19703: {
1.1.1.8   root     19704:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19705:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19706:                pit_run(ch, cur_time);
                   19707:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19708:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19709:                
                   19710:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19711:                        // decrement counter in 1msec period
                   19712:                        if(pit[ch].next_latch == 0) {
                   19713:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19714:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19715:                        }
                   19716:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19717:                                pit[ch].latch--;
                   19718:                        }
                   19719:                } else {
                   19720:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19721:                        pit[ch].next_latch = 0;
                   19722:                }
1.1.1.8   root     19723:        } else {
                   19724:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19725:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19726:        }
                   19727:        pit[ch].count_latched = 1;
                   19728:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19729:                // lower byte
                   19730:                pit[ch].low_read = 1;
                   19731:                pit[ch].high_read = 0;
                   19732:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19733:                // upper byte
                   19734:                pit[ch].low_read = 0;
                   19735:                pit[ch].high_read = 1;
                   19736:        } else {
                   19737:                // lower -> upper
1.1.1.14  root     19738:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19739:        }
                   19740: }
                   19741: 
1.1.1.8   root     19742: int pit_get_expired_time(int ch)
1.1       root     19743: {
1.1.1.22  root     19744:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19745:        UINT64 val = pit[ch].accum >> 10;
                   19746:        pit[ch].accum -= val << 10;
                   19747:        return((val != 0) ? val : 1);
1.1.1.8   root     19748: }
                   19749: 
1.1.1.25  root     19750: // sio
                   19751: 
                   19752: void sio_init()
                   19753: {
1.1.1.26  root     19754:        memset(sio, 0, sizeof(sio));
                   19755:        memset(sio_mt, 0, sizeof(sio_mt));
                   19756:        
1.1.1.29  root     19757:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19758:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19759:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19760:                
                   19761:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   19762:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     19763:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   19764:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     19765:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   19766:                sio[c].irq_identify = 0x01;     // no pending irq
                   19767:                
                   19768:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   19769:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   19770:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   19771:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   19772:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   19773:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   19774:                
1.1.1.26  root     19775:                if(sio_port_number[c] != 0) {
1.1.1.25  root     19776:                        sio[c].channel = c;
                   19777:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   19778:                }
                   19779:        }
                   19780: }
                   19781: 
                   19782: void sio_finish()
                   19783: {
1.1.1.29  root     19784:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19785:                if(sio_mt[c].hThread != NULL) {
                   19786:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   19787:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19788:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19789:                }
                   19790:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19791:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19792:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19793:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19794:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19795:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19796:        }
                   19797:        sio_release();
                   19798: }
                   19799: 
                   19800: void sio_release()
                   19801: {
1.1.1.29  root     19802:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19803:                // sio_thread() may access the resources :-(
1.1.1.32  root     19804:                bool running = (sio_mt[c].hThread != NULL);
                   19805:                
                   19806:                if(running) {
                   19807:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   19808:                }
                   19809:                if(sio[c].send_buffer != NULL) {
                   19810:                        sio[c].send_buffer->release();
                   19811:                        delete sio[c].send_buffer;
                   19812:                        sio[c].send_buffer = NULL;
                   19813:                }
                   19814:                if(running) {
                   19815:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19816:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   19817:                }
                   19818:                if(sio[c].recv_buffer != NULL) {
                   19819:                        sio[c].recv_buffer->release();
                   19820:                        delete sio[c].recv_buffer;
                   19821:                        sio[c].recv_buffer = NULL;
                   19822:                }
                   19823:                if(running) {
                   19824:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     19825:                }
1.1.1.25  root     19826:        }
                   19827: }
                   19828: 
                   19829: void sio_write(int c, UINT32 addr, UINT8 data)
                   19830: {
                   19831:        switch(addr & 7) {
                   19832:        case 0:
                   19833:                if(sio[c].selector & 0x80) {
                   19834:                        if(sio[c].divisor.b.l != data) {
                   19835:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19836:                                sio[c].divisor.b.l = data;
                   19837:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19838:                        }
                   19839:                } else {
                   19840:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19841:                        if(sio[c].send_buffer != NULL) {
                   19842:                                sio[c].send_buffer->write(data);
                   19843:                        }
1.1.1.25  root     19844:                        // transmitter holding/shift registers are not empty
                   19845:                        sio[c].line_stat_buf &= ~0x60;
                   19846:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19847:                        
                   19848:                        if(sio[c].irq_enable & 0x02) {
                   19849:                                sio_update_irq(c);
                   19850:                        }
                   19851:                }
                   19852:                break;
                   19853:        case 1:
                   19854:                if(sio[c].selector & 0x80) {
                   19855:                        if(sio[c].divisor.b.h != data) {
                   19856:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19857:                                sio[c].divisor.b.h = data;
                   19858:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19859:                        }
                   19860:                } else {
                   19861:                        if(sio[c].irq_enable != data) {
                   19862:                                sio[c].irq_enable = data;
                   19863:                                sio_update_irq(c);
                   19864:                        }
                   19865:                }
                   19866:                break;
                   19867:        case 3:
                   19868:                {
                   19869:                        UINT8 line_ctrl = data & 0x3f;
                   19870:                        bool set_brk = ((data & 0x40) != 0);
                   19871:                        
                   19872:                        if(sio[c].line_ctrl != line_ctrl) {
                   19873:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19874:                                sio[c].line_ctrl = line_ctrl;
                   19875:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19876:                        }
                   19877:                        if(sio[c].set_brk != set_brk) {
                   19878:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19879:                                sio[c].set_brk = set_brk;
                   19880:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19881:                        }
                   19882:                }
                   19883:                sio[c].selector = data;
                   19884:                break;
                   19885:        case 4:
                   19886:                {
                   19887:                        bool set_dtr = ((data & 0x01) != 0);
                   19888:                        bool set_rts = ((data & 0x02) != 0);
                   19889:                        
                   19890:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19891: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19892:                                sio[c].set_dtr = set_dtr;
                   19893:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19894: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19895:                                
                   19896:                                bool state_changed = false;
                   19897:                                
                   19898:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19899:                                if(set_dtr) {
                   19900:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19901:                                } else {
                   19902:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19903:                                }
                   19904:                                if(set_rts) {
                   19905:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19906:                                } else {
                   19907:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19908:                                }
                   19909:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19910:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19911:                                                if(sio[c].irq_enable & 0x08) {
                   19912:                                                        state_changed = true;
                   19913:                                                }
                   19914:                                                sio[c].modem_stat |= 0x02;
                   19915:                                        }
                   19916:                                }
                   19917:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19918:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19919:                                                if(sio[c].irq_enable & 0x08) {
                   19920:                                                        state_changed = true;
                   19921:                                                }
                   19922:                                                sio[c].modem_stat |= 0x01;
                   19923:                                        }
                   19924:                                }
                   19925:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19926:                                
                   19927:                                if(state_changed) {
                   19928:                                        sio_update_irq(c);
                   19929:                                }
1.1.1.25  root     19930:                        }
                   19931:                }
                   19932:                sio[c].modem_ctrl = data;
                   19933:                break;
                   19934:        case 7:
                   19935:                sio[c].scratch = data;
                   19936:                break;
                   19937:        }
                   19938: }
                   19939: 
                   19940: UINT8 sio_read(int c, UINT32 addr)
                   19941: {
                   19942:        switch(addr & 7) {
                   19943:        case 0:
                   19944:                if(sio[c].selector & 0x80) {
                   19945:                        return(sio[c].divisor.b.l);
                   19946:                } else {
                   19947:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19948:                        UINT8 data = 0;
                   19949:                        if(sio[c].recv_buffer != NULL) {
                   19950:                                data = sio[c].recv_buffer->read();
                   19951:                        }
1.1.1.25  root     19952:                        // data is not ready
                   19953:                        sio[c].line_stat_buf &= ~0x01;
                   19954:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19955:                        
                   19956:                        if(sio[c].irq_enable & 0x01) {
                   19957:                                sio_update_irq(c);
                   19958:                        }
                   19959:                        return(data);
                   19960:                }
                   19961:        case 1:
                   19962:                if(sio[c].selector & 0x80) {
                   19963:                        return(sio[c].divisor.b.h);
                   19964:                } else {
                   19965:                        return(sio[c].irq_enable);
                   19966:                }
                   19967:        case 2:
                   19968:                return(sio[c].irq_identify);
                   19969:        case 3:
                   19970:                return(sio[c].selector);
                   19971:        case 4:
                   19972:                return(sio[c].modem_ctrl);
                   19973:        case 5:
                   19974:                {
                   19975:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   19976:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   19977:                        sio[c].line_stat_err = 0x00;
                   19978:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19979:                        
                   19980:                        bool state_changed = false;
                   19981:                        
                   19982:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19983:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19984:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19985:                                        // transmitter holding register will be empty first
                   19986:                                        if(sio[c].irq_enable & 0x02) {
                   19987:                                                state_changed = true;
                   19988:                                        }
                   19989:                                        sio[c].line_stat_buf |= 0x20;
                   19990:                                }
                   19991:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19992:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19993:                                // transmitter shift register will be empty later
                   19994:                                sio[c].line_stat_buf |= 0x40;
                   19995:                        }
                   19996:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   19997:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19998:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19999:                                        // data is ready
                   20000:                                        if(sio[c].irq_enable & 0x01) {
                   20001:                                                state_changed = true;
                   20002:                                        }
                   20003:                                        sio[c].line_stat_buf |= 0x01;
                   20004:                                }
                   20005:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20006:                        }
                   20007:                        if(state_changed) {
                   20008:                                sio_update_irq(c);
                   20009:                        }
                   20010:                        return(val);
                   20011:                }
                   20012:        case 6:
                   20013:                {
                   20014:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   20015:                        UINT8 val = sio[c].modem_stat;
                   20016:                        sio[c].modem_stat &= 0xf0;
                   20017:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   20018:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20019:                        
                   20020:                        if(sio[c].modem_ctrl & 0x10) {
                   20021:                                // loop-back
                   20022:                                val &= 0x0f;
                   20023:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   20024:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   20025:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   20026:                        }
                   20027:                        return(val);
                   20028:                }
                   20029:        case 7:
                   20030:                return(sio[c].scratch);
                   20031:        }
                   20032:        return(0xff);
                   20033: }
                   20034: 
                   20035: void sio_update(int c)
                   20036: {
                   20037:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20038:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20039:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20040:                        // transmitter holding/shift registers will be empty
                   20041:                        sio[c].line_stat_buf |= 0x60;
                   20042:                }
                   20043:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20044:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20045:                // transmitter shift register will be empty
                   20046:                sio[c].line_stat_buf |= 0x40;
                   20047:        }
                   20048:        if(!(sio[c].line_stat_buf & 0x01)) {
                   20049:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20050:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20051:                        // data is ready
                   20052:                        sio[c].line_stat_buf |= 0x01;
                   20053:                }
                   20054:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20055:        }
                   20056:        sio_update_irq(c);
                   20057: }
                   20058: 
                   20059: void sio_update_irq(int c)
                   20060: {
                   20061:        int level = -1;
                   20062:        
                   20063:        if(sio[c].irq_enable & 0x08) {
                   20064:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20065:                if((sio[c].modem_stat & 0x0f) != 0) {
                   20066:                        level = 0;
                   20067:                }
                   20068:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20069:        }
                   20070:        if(sio[c].irq_enable & 0x02) {
                   20071:                if(sio[c].line_stat_buf & 0x20) {
                   20072:                        level = 1;
                   20073:                }
                   20074:        }
                   20075:        if(sio[c].irq_enable & 0x01) {
                   20076:                if(sio[c].line_stat_buf & 0x01) {
                   20077:                        level = 2;
                   20078:                }
                   20079:        }
                   20080:        if(sio[c].irq_enable & 0x04) {
                   20081:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   20082:                if(sio[c].line_stat_err != 0) {
                   20083:                        level = 3;
                   20084:                }
                   20085:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20086:        }
1.1.1.29  root     20087:        
                   20088:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     20089:        if(level != -1) {
                   20090:                sio[c].irq_identify = level << 1;
1.1.1.29  root     20091:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     20092:        } else {
                   20093:                sio[c].irq_identify = 1;
1.1.1.29  root     20094:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     20095:        }
                   20096: }
                   20097: 
                   20098: DWORD WINAPI sio_thread(void *lpx)
                   20099: {
                   20100:        volatile sio_t *p = (sio_t *)lpx;
                   20101:        sio_mt_t *q = &sio_mt[p->channel];
                   20102:        
                   20103:        char name[] = "COM1";
1.1.1.26  root     20104:        name[3] = '0' + sio_port_number[p->channel];
                   20105:        HANDLE hComm = NULL;
                   20106:        COMMPROP commProp;
                   20107:        DCB dcb;
                   20108:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   20109:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   20110:        
1.1.1.60  root     20111:        if((hComm = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
1.1.1.26  root     20112:                if(GetCommProperties(hComm, &commProp)) {
                   20113:                        dwSettableBaud = commProp.dwSettableBaud;
                   20114:                }
1.1.1.25  root     20115:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     20116: //             EscapeCommFunction(hComm, SETRTS);
                   20117: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     20118:                
1.1.1.54  root     20119:                while(!m_exit) {
1.1.1.25  root     20120:                        // setup comm port
                   20121:                        bool comm_state_changed = false;
                   20122:                        
                   20123:                        EnterCriticalSection(&q->csLineCtrl);
                   20124:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   20125:                                p->prev_divisor = p->divisor.w;
                   20126:                                p->prev_line_ctrl = p->line_ctrl;
                   20127:                                comm_state_changed = true;
                   20128:                        }
                   20129:                        LeaveCriticalSection(&q->csLineCtrl);
                   20130:                        
                   20131:                        if(comm_state_changed) {
1.1.1.26  root     20132:                                if(GetCommState(hComm, &dcb)) {
                   20133: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   20134:                                        DWORD baud = 115200 / p->prev_divisor;
                   20135:                                        dcb.BaudRate = 9600; // default
                   20136:                                        
                   20137:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   20138:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   20139:                                        // 134.5bps is not supported ???
                   20140: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   20141:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   20142:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   20143:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   20144:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   20145:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   20146:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   20147:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   20148:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   20149:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   20150: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   20151: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   20152: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   20153:                                        
                   20154:                                        switch(p->prev_line_ctrl & 0x03) {
                   20155:                                        case 0x00: dcb.ByteSize = 5; break;
                   20156:                                        case 0x01: dcb.ByteSize = 6; break;
                   20157:                                        case 0x02: dcb.ByteSize = 7; break;
                   20158:                                        case 0x03: dcb.ByteSize = 8; break;
                   20159:                                        }
                   20160:                                        switch(p->prev_line_ctrl & 0x04) {
                   20161:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   20162:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   20163:                                        }
                   20164:                                        switch(p->prev_line_ctrl & 0x38) {
                   20165:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   20166:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   20167:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   20168:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   20169:                                        default:   dcb.Parity = NOPARITY;    break;
                   20170:                                        }
                   20171:                                        dcb.fBinary = TRUE;
                   20172:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   20173:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   20174:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   20175:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   20176:                                        dcb.fTXContinueOnXoff = TRUE;
                   20177:                                        dcb.fOutX = dcb.fInX = FALSE;
                   20178:                                        dcb.fErrorChar = FALSE;
                   20179:                                        dcb.fNull = FALSE;
                   20180:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   20181:                                        dcb.fAbortOnError = FALSE;
                   20182:                                        
                   20183:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     20184:                                }
                   20185:                                
                   20186:                                // check again to apply all comm state changes
                   20187:                                Sleep(10);
                   20188:                                continue;
                   20189:                        }
                   20190:                        
                   20191:                        // set comm pins
                   20192:                        bool change_brk = false;
1.1.1.26  root     20193: //                     bool change_rts = false;
                   20194: //                     bool change_dtr = false;
1.1.1.25  root     20195:                        
                   20196:                        EnterCriticalSection(&q->csModemCtrl);
                   20197:                        if(p->prev_set_brk != p->set_brk) {
                   20198:                                p->prev_set_brk = p->set_brk;
                   20199:                                change_brk = true;
                   20200:                        }
1.1.1.26  root     20201: //                     if(p->prev_set_rts != p->set_rts) {
                   20202: //                             p->prev_set_rts = p->set_rts;
                   20203: //                             change_rts = true;
                   20204: //                     }
                   20205: //                     if(p->prev_set_dtr != p->set_dtr) {
                   20206: //                             p->prev_set_dtr = p->set_dtr;
                   20207: //                             change_dtr = true;
                   20208: //                     }
1.1.1.25  root     20209:                        LeaveCriticalSection(&q->csModemCtrl);
                   20210:                        
                   20211:                        if(change_brk) {
1.1.1.26  root     20212:                                static UINT32 clear_time = 0;
                   20213:                                if(p->prev_set_brk) {
                   20214:                                        EscapeCommFunction(hComm, SETBREAK);
                   20215:                                        clear_time = timeGetTime() + 200;
                   20216:                                } else {
                   20217:                                        // keep break for at least 200msec
                   20218:                                        UINT32 cur_time = timeGetTime();
                   20219:                                        if(clear_time > cur_time) {
                   20220:                                                Sleep(clear_time - cur_time);
                   20221:                                        }
                   20222:                                        EscapeCommFunction(hComm, CLRBREAK);
                   20223:                                }
1.1.1.25  root     20224:                        }
1.1.1.26  root     20225: //                     if(change_rts) {
                   20226: //                             if(p->prev_set_rts) {
                   20227: //                                     EscapeCommFunction(hComm, SETRTS);
                   20228: //                             } else {
                   20229: //                                     EscapeCommFunction(hComm, CLRRTS);
                   20230: //                             }
                   20231: //                     }
                   20232: //                     if(change_dtr) {
                   20233: //                             if(p->prev_set_dtr) {
                   20234: //                                     EscapeCommFunction(hComm, SETDTR);
                   20235: //                             } else {
                   20236: //                                     EscapeCommFunction(hComm, CLRDTR);
                   20237: //                             }
                   20238: //                     }
1.1.1.25  root     20239:                        
                   20240:                        // get comm pins
                   20241:                        DWORD dwModemStat = 0;
                   20242:                        
                   20243:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   20244:                                EnterCriticalSection(&q->csModemStat);
                   20245:                                if(dwModemStat & MS_RLSD_ON) {
                   20246:                                        p->modem_stat |= 0x80;
                   20247:                                } else {
                   20248:                                        p->modem_stat &= ~0x80;
                   20249:                                }
                   20250:                                if(dwModemStat & MS_RING_ON) {
                   20251:                                        p->modem_stat |= 0x40;
                   20252:                                } else {
                   20253:                                        p->modem_stat &= ~0x40;
                   20254:                                }
1.1.1.26  root     20255: //                             if(dwModemStat & MS_DSR_ON) {
                   20256: //                                     p->modem_stat |= 0x20;
                   20257: //                             } else {
                   20258: //                                     p->modem_stat &= ~0x20;
                   20259: //                             }
                   20260: //                             if(dwModemStat & MS_CTS_ON) {
                   20261: //                                     p->modem_stat |= 0x10;
                   20262: //                             } else {
                   20263: //                                     p->modem_stat &= ~0x10;
                   20264: //                             }
1.1.1.25  root     20265:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   20266:                                        p->modem_stat |= 0x08;
                   20267:                                }
                   20268:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   20269:                                        p->modem_stat |= 0x04;
                   20270:                                }
1.1.1.26  root     20271: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   20272: //                                     p->modem_stat |= 0x02;
                   20273: //                             }
                   20274: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   20275: //                                     p->modem_stat |= 0x01;
                   20276: //                             }
1.1.1.25  root     20277:                                LeaveCriticalSection(&q->csModemStat);
                   20278:                        }
                   20279:                        
                   20280:                        // send data
                   20281:                        DWORD dwSend = 0;
                   20282:                        
                   20283:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     20284:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     20285:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   20286:                        }
                   20287:                        LeaveCriticalSection(&q->csSendData);
                   20288:                        
                   20289:                        if(dwSend != 0) {
                   20290:                                DWORD dwWritten = 0;
                   20291:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   20292:                        }
                   20293:                        
                   20294:                        // get line status and recv data
                   20295:                        DWORD dwLineStat = 0;
                   20296:                        COMSTAT comStat;
                   20297:                        
                   20298:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   20299:                                EnterCriticalSection(&q->csLineStat);
                   20300:                                if(dwLineStat & CE_BREAK) {
                   20301:                                        p->line_stat_err |= 0x10;
                   20302:                                }
                   20303:                                if(dwLineStat & CE_FRAME) {
                   20304:                                        p->line_stat_err |= 0x08;
                   20305:                                }
                   20306:                                if(dwLineStat & CE_RXPARITY) {
                   20307:                                        p->line_stat_err |= 0x04;
                   20308:                                }
                   20309:                                if(dwLineStat & CE_OVERRUN) {
                   20310:                                        p->line_stat_err |= 0x02;
                   20311:                                }
                   20312:                                LeaveCriticalSection(&q->csLineStat);
                   20313:                                
                   20314:                                if(comStat.cbInQue != 0) {
                   20315:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20316:                                        DWORD dwRecv = 0;
                   20317:                                        if(p->recv_buffer != NULL) {
                   20318:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   20319:                                        }
1.1.1.25  root     20320:                                        LeaveCriticalSection(&q->csRecvData);
                   20321:                                        
                   20322:                                        if(dwRecv != 0) {
                   20323:                                                DWORD dwRead = 0;
                   20324:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   20325:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20326:                                                        if(p->recv_buffer != NULL) {
                   20327:                                                                for(int i = 0; i < dwRead; i++) {
                   20328:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   20329:                                                                }
1.1.1.25  root     20330:                                                        }
                   20331:                                                        LeaveCriticalSection(&q->csRecvData);
                   20332:                                                }
                   20333:                                        }
                   20334:                                }
                   20335:                        }
                   20336:                        Sleep(10);
                   20337:                }
                   20338:                CloseHandle(hComm);
                   20339:        }
                   20340:        return 0;
                   20341: }
                   20342: 
1.1.1.8   root     20343: // cmos
                   20344: 
                   20345: void cmos_init()
                   20346: {
                   20347:        memset(cmos, 0, sizeof(cmos));
                   20348:        cmos_addr = 0;
1.1       root     20349:        
1.1.1.8   root     20350:        // from DOSBox
                   20351:        cmos_write(0x0a, 0x26);
                   20352:        cmos_write(0x0b, 0x02);
                   20353:        cmos_write(0x0d, 0x80);
1.1       root     20354: }
                   20355: 
1.1.1.8   root     20356: void cmos_write(int addr, UINT8 val)
1.1       root     20357: {
1.1.1.8   root     20358:        cmos[addr & 0x7f] = val;
                   20359: }
                   20360: 
                   20361: #define CMOS_GET_TIME() { \
                   20362:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   20363:        if(prev_sec != cur_sec) { \
                   20364:                GetLocalTime(&time); \
                   20365:                prev_sec = cur_sec; \
                   20366:        } \
1.1       root     20367: }
1.1.1.8   root     20368: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     20369: 
1.1.1.8   root     20370: UINT8 cmos_read(int addr)
1.1       root     20371: {
1.1.1.8   root     20372:        static SYSTEMTIME time;
                   20373:        static UINT32 prev_sec = 0;
1.1       root     20374:        
1.1.1.8   root     20375:        switch(addr & 0x7f) {
                   20376:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   20377:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   20378:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   20379:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   20380:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   20381:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   20382:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   20383: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   20384:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   20385:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   20386:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   20387:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20388:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20389:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20390:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20391:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     20392:        }
1.1.1.8   root     20393:        return(cmos[addr & 0x7f]);
1.1       root     20394: }
                   20395: 
1.1.1.7   root     20396: // kbd (a20)
                   20397: 
                   20398: void kbd_init()
                   20399: {
1.1.1.8   root     20400:        kbd_data = kbd_command = 0;
1.1.1.7   root     20401:        kbd_status = 0x18;
                   20402: }
                   20403: 
                   20404: UINT8 kbd_read_data()
                   20405: {
1.1.1.57  root     20406:        UINT8 data = kbd_data;
                   20407:        kbd_data = 0;
1.1.1.8   root     20408:        kbd_status &= ~1;
1.1.1.57  root     20409:        return(data);
1.1.1.7   root     20410: }
                   20411: 
                   20412: void kbd_write_data(UINT8 val)
                   20413: {
                   20414:        switch(kbd_command) {
                   20415:        case 0xd1:
                   20416:                i386_set_a20_line((val >> 1) & 1);
                   20417:                break;
                   20418:        }
                   20419:        kbd_command = 0;
1.1.1.8   root     20420:        kbd_status &= ~8;
1.1.1.7   root     20421: }
                   20422: 
                   20423: UINT8 kbd_read_status()
                   20424: {
                   20425:        return(kbd_status);
                   20426: }
                   20427: 
                   20428: void kbd_write_command(UINT8 val)
                   20429: {
                   20430:        switch(val) {
                   20431:        case 0xd0:
                   20432:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     20433:                kbd_status |= 1;
1.1.1.7   root     20434:                break;
                   20435:        case 0xdd:
                   20436:                i386_set_a20_line(0);
                   20437:                break;
                   20438:        case 0xdf:
                   20439:                i386_set_a20_line(1);
                   20440:                break;
1.1.1.26  root     20441:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   20442:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     20443:                if(!(val & 1)) {
1.1.1.8   root     20444:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20445:                                // reset pic
                   20446:                                pic_init();
                   20447:                                pic[0].irr = pic[1].irr = 0x00;
                   20448:                                pic[0].imr = pic[1].imr = 0xff;
                   20449:                        }
                   20450:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20451:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20452:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20453:                        i386_jmp_far(selector, address);
1.1.1.7   root     20454:                }
                   20455:                i386_set_a20_line((val >> 1) & 1);
                   20456:                break;
                   20457:        }
                   20458:        kbd_command = val;
1.1.1.8   root     20459:        kbd_status |= 8;
1.1.1.7   root     20460: }
                   20461: 
1.1.1.9   root     20462: // vga
                   20463: 
                   20464: UINT8 vga_read_status()
                   20465: {
                   20466:        // 60hz
                   20467:        static const int period[3] = {16, 17, 17};
                   20468:        static int index = 0;
                   20469:        UINT32 time = timeGetTime() % period[index];
                   20470:        
                   20471:        index = (index + 1) % 3;
1.1.1.14  root     20472:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20473: }
                   20474: 
1.1       root     20475: // i/o bus
                   20476: 
1.1.1.29  root     20477: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20478: //#define SW1US_PATCH
                   20479: 
1.1.1.25  root     20480: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20481: #ifdef USE_DEBUGGER
1.1.1.25  root     20482: {
1.1.1.33  root     20483:        if(now_debugging) {
                   20484:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20485:                        if(in_break_point.table[i].status == 1) {
                   20486:                                if(addr == in_break_point.table[i].addr) {
                   20487:                                        in_break_point.hit = i + 1;
                   20488:                                        now_suspended = true;
                   20489:                                        break;
                   20490:                                }
                   20491:                        }
                   20492:                }
1.1.1.25  root     20493:        }
1.1.1.33  root     20494:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20495: }
1.1.1.33  root     20496: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20497: #endif
1.1       root     20498: {
1.1.1.33  root     20499:        UINT8 val = 0xff;
                   20500:        
1.1       root     20501:        switch(addr) {
1.1.1.29  root     20502: #ifdef SW1US_PATCH
                   20503:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20504:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20505:                val = sio_read(0, addr - 1);
                   20506:                break;
1.1.1.29  root     20507: #else
1.1.1.25  root     20508:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20509:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20510:                val = dma_read(0, addr);
                   20511:                break;
1.1.1.29  root     20512: #endif
1.1.1.25  root     20513:        case 0x20: case 0x21:
1.1.1.33  root     20514:                val = pic_read(0, addr);
                   20515:                break;
1.1.1.25  root     20516:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20517:                val = pit_read(addr & 0x03);
                   20518:                break;
1.1.1.7   root     20519:        case 0x60:
1.1.1.33  root     20520:                val = kbd_read_data();
                   20521:                break;
1.1.1.9   root     20522:        case 0x61:
1.1.1.33  root     20523:                val = system_port;
                   20524:                break;
1.1.1.7   root     20525:        case 0x64:
1.1.1.33  root     20526:                val = kbd_read_status();
                   20527:                break;
1.1       root     20528:        case 0x71:
1.1.1.33  root     20529:                val = cmos_read(cmos_addr);
                   20530:                break;
1.1.1.25  root     20531:        case 0x81:
1.1.1.33  root     20532:                val = dma_page_read(0, 2);
                   20533:                break;
1.1.1.25  root     20534:        case 0x82:
1.1.1.33  root     20535:                val = dma_page_read(0, 3);
                   20536:                break;
1.1.1.25  root     20537:        case 0x83:
1.1.1.33  root     20538:                val = dma_page_read(0, 1);
                   20539:                break;
1.1.1.25  root     20540:        case 0x87:
1.1.1.33  root     20541:                val = dma_page_read(0, 0);
                   20542:                break;
1.1.1.25  root     20543:        case 0x89:
1.1.1.33  root     20544:                val = dma_page_read(1, 2);
                   20545:                break;
1.1.1.25  root     20546:        case 0x8a:
1.1.1.33  root     20547:                val = dma_page_read(1, 3);
                   20548:                break;
1.1.1.25  root     20549:        case 0x8b:
1.1.1.33  root     20550:                val = dma_page_read(1, 1);
                   20551:                break;
1.1.1.25  root     20552:        case 0x8f:
1.1.1.33  root     20553:                val = dma_page_read(1, 0);
                   20554:                break;
1.1       root     20555:        case 0x92:
1.1.1.33  root     20556:                val = (m_a20_mask >> 19) & 2;
                   20557:                break;
1.1.1.25  root     20558:        case 0xa0: case 0xa1:
1.1.1.33  root     20559:                val = pic_read(1, addr);
                   20560:                break;
1.1.1.25  root     20561:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20562:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20563:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20564:                break;
1.1.1.37  root     20565:        case 0x278: case 0x279: case 0x27a:
                   20566:                val = pio_read(1, addr);
                   20567:                break;
1.1.1.29  root     20568:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20569:                val = sio_read(3, addr);
                   20570:                break;
1.1.1.25  root     20571:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20572:                val = sio_read(1, addr);
                   20573:                break;
1.1.1.25  root     20574:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20575:                val = pio_read(0, addr);
                   20576:                break;
1.1.1.25  root     20577:        case 0x3ba: case 0x3da:
1.1.1.33  root     20578:                val = vga_read_status();
                   20579:                break;
1.1.1.37  root     20580:        case 0x3bc: case 0x3bd: case 0x3be:
                   20581:                val = pio_read(2, addr);
                   20582:                break;
1.1.1.58  root     20583:        case 0x3d5:
                   20584:                if(crtc_addr < 16) {
                   20585:                        val = crtc_regs[crtc_addr];
                   20586:                }
                   20587:                break;
1.1.1.29  root     20588:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20589:                val = sio_read(2, addr);
                   20590:                break;
1.1.1.25  root     20591:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20592:                val = sio_read(0, addr);
                   20593:                break;
1.1       root     20594:        default:
1.1.1.33  root     20595: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20596:                break;
                   20597:        }
1.1.1.33  root     20598: #ifdef ENABLE_DEBUG_IOPORT
                   20599:        if(fp_debug_log != NULL) {
                   20600:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20601:        }
                   20602: #endif
                   20603:        return(val);
1.1       root     20604: }
                   20605: 
                   20606: UINT16 read_io_word(offs_t addr)
                   20607: {
                   20608:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20609: }
                   20610: 
1.1.1.33  root     20611: #ifdef USE_DEBUGGER
                   20612: UINT16 debugger_read_io_word(offs_t addr)
                   20613: {
                   20614:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20615: }
                   20616: #endif
                   20617: 
1.1       root     20618: UINT32 read_io_dword(offs_t addr)
                   20619: {
                   20620:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20621: }
                   20622: 
1.1.1.33  root     20623: #ifdef USE_DEBUGGER
                   20624: UINT32 debugger_read_io_dword(offs_t addr)
                   20625: {
                   20626:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8) | (debugger_read_io_byte(addr + 2) << 16) | (debugger_read_io_byte(addr + 3) << 24));
                   20627: }
                   20628: #endif
                   20629: 
1.1       root     20630: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20631: #ifdef USE_DEBUGGER
                   20632: {
                   20633:        if(now_debugging) {
                   20634:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20635:                        if(out_break_point.table[i].status == 1) {
                   20636:                                if(addr == out_break_point.table[i].addr) {
                   20637:                                        out_break_point.hit = i + 1;
                   20638:                                        now_suspended = true;
                   20639:                                        break;
                   20640:                                }
                   20641:                        }
                   20642:                }
                   20643:        }
                   20644:        debugger_write_io_byte(addr, val);
                   20645: }
                   20646: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20647: #endif
1.1       root     20648: {
1.1.1.25  root     20649: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20650:        if(fp_debug_log != NULL) {
1.1.1.43  root     20651: #ifdef USE_SERVICE_THREAD
                   20652:                if(addr != 0xf7)
                   20653: #endif
1.1.1.33  root     20654:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20655:        }
                   20656: #endif
1.1       root     20657:        switch(addr) {
1.1.1.29  root     20658: #ifdef SW1US_PATCH
                   20659:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20660:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20661:                sio_write(0, addr - 1, val);
                   20662:                break;
                   20663: #else
1.1.1.25  root     20664:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20665:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20666:                dma_write(0, addr, val);
                   20667:                break;
1.1.1.29  root     20668: #endif
1.1.1.25  root     20669:        case 0x20: case 0x21:
1.1       root     20670:                pic_write(0, addr, val);
                   20671:                break;
1.1.1.25  root     20672:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20673:                pit_write(addr & 0x03, val);
                   20674:                break;
1.1.1.7   root     20675:        case 0x60:
                   20676:                kbd_write_data(val);
                   20677:                break;
1.1.1.9   root     20678:        case 0x61:
                   20679:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20680:                        // beep on
                   20681: //                     MessageBeep(-1);
                   20682:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20683:                        // beep off
                   20684:                }
                   20685:                system_port = val;
                   20686:                break;
1.1       root     20687:        case 0x64:
1.1.1.7   root     20688:                kbd_write_command(val);
1.1       root     20689:                break;
                   20690:        case 0x70:
                   20691:                cmos_addr = val;
                   20692:                break;
                   20693:        case 0x71:
1.1.1.8   root     20694:                cmos_write(cmos_addr, val);
1.1       root     20695:                break;
1.1.1.25  root     20696:        case 0x81:
                   20697:                dma_page_write(0, 2, val);
                   20698:        case 0x82:
                   20699:                dma_page_write(0, 3, val);
                   20700:        case 0x83:
                   20701:                dma_page_write(0, 1, val);
                   20702:        case 0x87:
                   20703:                dma_page_write(0, 0, val);
                   20704:        case 0x89:
                   20705:                dma_page_write(1, 2, val);
                   20706:        case 0x8a:
                   20707:                dma_page_write(1, 3, val);
                   20708:        case 0x8b:
                   20709:                dma_page_write(1, 1, val);
                   20710:        case 0x8f:
                   20711:                dma_page_write(1, 0, val);
1.1       root     20712:        case 0x92:
1.1.1.7   root     20713:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20714:                break;
1.1.1.25  root     20715:        case 0xa0: case 0xa1:
1.1       root     20716:                pic_write(1, addr, val);
                   20717:                break;
1.1.1.25  root     20718:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20719:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20720:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20721:                break;
1.1.1.35  root     20722: #ifdef USE_SERVICE_THREAD
                   20723:        case 0xf7:
                   20724:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20725:                if(in_service && cursor_moved) {
                   20726:                        // update cursor position before service is done
                   20727:                        pcbios_update_cursor_position();
                   20728:                        cursor_moved = false;
                   20729:                }
1.1.1.35  root     20730:                finish_service_loop();
                   20731:                break;
                   20732: #endif
1.1.1.37  root     20733:        case 0x278: case 0x279: case 0x27a:
                   20734:                pio_write(1, addr, val);
                   20735:                break;
1.1.1.29  root     20736:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20737:                sio_write(3, addr, val);
                   20738:                break;
1.1.1.25  root     20739:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20740:                sio_write(1, addr, val);
                   20741:                break;
                   20742:        case 0x378: case 0x379: case 0x37a:
                   20743:                pio_write(0, addr, val);
                   20744:                break;
1.1.1.37  root     20745:        case 0x3bc: case 0x3bd: case 0x3be:
                   20746:                pio_write(2, addr, val);
                   20747:                break;
1.1.1.58  root     20748:        case 0x3d4:
                   20749:                crtc_addr = val;
                   20750:                break;
                   20751:        case 0x3d5:
                   20752:                if(crtc_addr < 16) {
                   20753:                        if(crtc_regs[crtc_addr] != val) {
                   20754:                                crtc_regs[crtc_addr] = val;
                   20755:                                crtc_changed[crtc_addr] = 1;
                   20756:                        }
                   20757:                }
                   20758:                break;
1.1.1.29  root     20759:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20760:                sio_write(2, addr, val);
                   20761:                break;
1.1.1.25  root     20762:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   20763:                sio_write(0, addr, val);
                   20764:                break;
1.1       root     20765:        default:
1.1.1.33  root     20766: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     20767:                break;
                   20768:        }
                   20769: }
                   20770: 
                   20771: void write_io_word(offs_t addr, UINT16 val)
                   20772: {
                   20773:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20774:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20775: }
                   20776: 
1.1.1.33  root     20777: #ifdef USE_DEBUGGER
                   20778: void debugger_write_io_word(offs_t addr, UINT16 val)
                   20779: {
                   20780:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20781:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20782: }
                   20783: #endif
                   20784: 
1.1       root     20785: void write_io_dword(offs_t addr, UINT32 val)
                   20786: {
                   20787:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20788:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20789:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20790:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20791: }
1.1.1.33  root     20792: 
                   20793: #ifdef USE_DEBUGGER
                   20794: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   20795: {
                   20796:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20797:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20798:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20799:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20800: }
                   20801: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.